1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2024 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
27 #include "safe-ctype.h"
28 #include "libiberty.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
40 /* This struct is used to pass information to routines called via
41 elf_link_hash_traverse which must return failure. */
43 struct elf_info_failed
45 struct bfd_link_info
*info
;
49 static bool _bfd_elf_fix_symbol_flags
50 (struct elf_link_hash_entry
*, struct elf_info_failed
*);
53 _bfd_elf_section_for_symbol (struct elf_reloc_cookie
*cookie
,
54 unsigned long r_symndx
,
57 if (r_symndx
>= cookie
->locsymcount
58 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
60 struct elf_link_hash_entry
*h
;
62 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
64 while (h
->root
.type
== bfd_link_hash_indirect
65 || h
->root
.type
== bfd_link_hash_warning
)
66 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
68 if ((h
->root
.type
== bfd_link_hash_defined
69 || h
->root
.type
== bfd_link_hash_defweak
)
70 && discarded_section (h
->root
.u
.def
.section
))
71 return h
->root
.u
.def
.section
;
77 /* It's not a relocation against a global symbol,
78 but it could be a relocation against a local
79 symbol for a discarded section. */
81 Elf_Internal_Sym
*isym
;
83 /* Need to: get the symbol; get the section. */
84 isym
= &cookie
->locsyms
[r_symndx
];
85 isec
= bfd_section_from_elf_index (cookie
->abfd
, isym
->st_shndx
);
87 && discard
? discarded_section (isec
) : 1)
93 /* Define a symbol in a dynamic linkage section. */
95 struct elf_link_hash_entry
*
96 _bfd_elf_define_linkage_sym (bfd
*abfd
,
97 struct bfd_link_info
*info
,
101 struct elf_link_hash_entry
*h
;
102 struct bfd_link_hash_entry
*bh
;
103 const struct elf_backend_data
*bed
;
105 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, false, false, false);
108 /* Zap symbol defined in an as-needed lib that wasn't linked.
109 This is a symptom of a larger problem: Absolute symbols
110 defined in shared libraries can't be overridden, because we
111 lose the link to the bfd which is via the symbol section. */
112 h
->root
.type
= bfd_link_hash_new
;
118 bed
= get_elf_backend_data (abfd
);
119 if (!_bfd_generic_link_add_one_symbol (info
, abfd
, name
, BSF_GLOBAL
,
120 sec
, 0, NULL
, false, bed
->collect
,
123 h
= (struct elf_link_hash_entry
*) bh
;
124 BFD_ASSERT (h
!= NULL
);
127 h
->root
.linker_def
= 1;
128 h
->type
= STT_OBJECT
;
129 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
130 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
132 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
137 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
141 struct elf_link_hash_entry
*h
;
142 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
143 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
145 /* This function may be called more than once. */
146 if (htab
->sgot
!= NULL
)
149 flags
= bed
->dynamic_sec_flags
;
151 s
= bfd_make_section_anyway_with_flags (abfd
,
152 (bed
->rela_plts_and_copies_p
153 ? ".rela.got" : ".rel.got"),
154 (bed
->dynamic_sec_flags
157 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
161 s
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
163 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
167 if (bed
->want_got_plt
)
169 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
171 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
176 /* The first bit of the global offset table is the header. */
177 s
->size
+= bed
->got_header_size
;
179 if (bed
->want_got_sym
)
181 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
182 (or .got.plt) section. We don't do this in the linker script
183 because we don't want to define the symbol if we are not creating
184 a global offset table. */
185 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
186 "_GLOBAL_OFFSET_TABLE_");
187 elf_hash_table (info
)->hgot
= h
;
195 /* Create a strtab to hold the dynamic symbol names. */
197 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
199 struct elf_link_hash_table
*hash_table
;
201 hash_table
= elf_hash_table (info
);
202 if (hash_table
->dynobj
== NULL
)
204 /* We may not set dynobj, an input file holding linker created
205 dynamic sections to abfd, which may be a dynamic object with
206 its own dynamic sections. We need to find a normal input file
207 to hold linker created sections if possible. */
208 if ((abfd
->flags
& (DYNAMIC
| BFD_PLUGIN
)) != 0)
212 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link
.next
)
214 & (DYNAMIC
| BFD_LINKER_CREATED
| BFD_PLUGIN
)) == 0
215 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
216 && elf_object_id (ibfd
) == elf_hash_table_id (hash_table
)
217 && !((s
= ibfd
->sections
) != NULL
218 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
))
224 hash_table
->dynobj
= abfd
;
227 if (hash_table
->dynstr
== NULL
)
229 hash_table
->dynstr
= _bfd_elf_strtab_init ();
230 if (hash_table
->dynstr
== NULL
)
236 /* Create some sections which will be filled in with dynamic linking
237 information. ABFD is an input file which requires dynamic sections
238 to be created. The dynamic sections take up virtual memory space
239 when the final executable is run, so we need to create them before
240 addresses are assigned to the output sections. We work out the
241 actual contents and size of these sections later. */
244 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
248 const struct elf_backend_data
*bed
;
249 struct elf_link_hash_entry
*h
;
251 if (! is_elf_hash_table (info
->hash
))
254 if (elf_hash_table (info
)->dynamic_sections_created
)
257 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
260 abfd
= elf_hash_table (info
)->dynobj
;
261 bed
= get_elf_backend_data (abfd
);
263 flags
= bed
->dynamic_sec_flags
;
265 /* A dynamically linked executable has a .interp section, but a
266 shared library does not. */
267 if (bfd_link_executable (info
) && !info
->nointerp
)
269 s
= bfd_make_section_anyway_with_flags (abfd
, ".interp",
270 flags
| SEC_READONLY
);
275 /* Create sections to hold version informations. These are removed
276 if they are not needed. */
277 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_d",
278 flags
| SEC_READONLY
);
280 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
283 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version",
284 flags
| SEC_READONLY
);
286 || !bfd_set_section_alignment (s
, 1))
289 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_r",
290 flags
| SEC_READONLY
);
292 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
295 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynsym",
296 flags
| SEC_READONLY
);
298 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
300 elf_hash_table (info
)->dynsym
= s
;
302 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynstr",
303 flags
| SEC_READONLY
);
307 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynamic", flags
);
309 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
312 /* The special symbol _DYNAMIC is always set to the start of the
313 .dynamic section. We could set _DYNAMIC in a linker script, but we
314 only want to define it if we are, in fact, creating a .dynamic
315 section. We don't want to define it if there is no .dynamic
316 section, since on some ELF platforms the start up code examines it
317 to decide how to initialize the process. */
318 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC");
319 elf_hash_table (info
)->hdynamic
= h
;
325 s
= bfd_make_section_anyway_with_flags (abfd
, ".hash",
326 flags
| SEC_READONLY
);
328 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
330 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
333 if (info
->emit_gnu_hash
&& bed
->record_xhash_symbol
== NULL
)
335 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.hash",
336 flags
| SEC_READONLY
);
338 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
340 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
341 4 32-bit words followed by variable count of 64-bit words, then
342 variable count of 32-bit words. */
343 if (bed
->s
->arch_size
== 64)
344 elf_section_data (s
)->this_hdr
.sh_entsize
= 0;
346 elf_section_data (s
)->this_hdr
.sh_entsize
= 4;
349 if (info
->enable_dt_relr
)
351 s
= bfd_make_section_anyway_with_flags (abfd
, ".relr.dyn",
352 (bed
->dynamic_sec_flags
355 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
357 elf_hash_table (info
)->srelrdyn
= s
;
360 /* Let the backend create the rest of the sections. This lets the
361 backend set the right flags. The backend will normally create
362 the .got and .plt sections. */
363 if (bed
->elf_backend_create_dynamic_sections
== NULL
364 || ! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
367 elf_hash_table (info
)->dynamic_sections_created
= true;
372 /* Create dynamic sections when linking against a dynamic object. */
375 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
377 flagword flags
, pltflags
;
378 struct elf_link_hash_entry
*h
;
380 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
381 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
383 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
384 .rel[a].bss sections. */
385 flags
= bed
->dynamic_sec_flags
;
388 if (bed
->plt_not_loaded
)
389 /* We do not clear SEC_ALLOC here because we still want the OS to
390 allocate space for the section; it's just that there's nothing
391 to read in from the object file. */
392 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
394 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
395 if (bed
->plt_readonly
)
396 pltflags
|= SEC_READONLY
;
398 s
= bfd_make_section_anyway_with_flags (abfd
, ".plt", pltflags
);
400 || !bfd_set_section_alignment (s
, bed
->plt_alignment
))
404 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
406 if (bed
->want_plt_sym
)
408 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
409 "_PROCEDURE_LINKAGE_TABLE_");
410 elf_hash_table (info
)->hplt
= h
;
415 s
= bfd_make_section_anyway_with_flags (abfd
,
416 (bed
->rela_plts_and_copies_p
417 ? ".rela.plt" : ".rel.plt"),
418 flags
| SEC_READONLY
);
420 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
424 if (! _bfd_elf_create_got_section (abfd
, info
))
427 if (bed
->want_dynbss
)
429 /* The .dynbss section is a place to put symbols which are defined
430 by dynamic objects, are referenced by regular objects, and are
431 not functions. We must allocate space for them in the process
432 image and use a R_*_COPY reloc to tell the dynamic linker to
433 initialize them at run time. The linker script puts the .dynbss
434 section into the .bss section of the final image. */
435 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynbss",
436 SEC_ALLOC
| SEC_LINKER_CREATED
);
441 if (bed
->want_dynrelro
)
443 /* Similarly, but for symbols that were originally in read-only
444 sections. This section doesn't really need to have contents,
445 but make it like other .data.rel.ro sections. */
446 s
= bfd_make_section_anyway_with_flags (abfd
, ".data.rel.ro",
453 /* The .rel[a].bss section holds copy relocs. This section is not
454 normally needed. We need to create it here, though, so that the
455 linker will map it to an output section. We can't just create it
456 only if we need it, because we will not know whether we need it
457 until we have seen all the input files, and the first time the
458 main linker code calls BFD after examining all the input files
459 (size_dynamic_sections) the input sections have already been
460 mapped to the output sections. If the section turns out not to
461 be needed, we can discard it later. We will never need this
462 section when generating a shared object, since they do not use
464 if (bfd_link_executable (info
))
466 s
= bfd_make_section_anyway_with_flags (abfd
,
467 (bed
->rela_plts_and_copies_p
468 ? ".rela.bss" : ".rel.bss"),
469 flags
| SEC_READONLY
);
471 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
475 if (bed
->want_dynrelro
)
477 s
= (bfd_make_section_anyway_with_flags
478 (abfd
, (bed
->rela_plts_and_copies_p
479 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
480 flags
| SEC_READONLY
));
482 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
484 htab
->sreldynrelro
= s
;
492 /* Record a new dynamic symbol. We record the dynamic symbols as we
493 read the input files, since we need to have a list of all of them
494 before we can determine the final sizes of the output sections.
495 Note that we may actually call this function even though we are not
496 going to output any dynamic symbols; in some cases we know that a
497 symbol should be in the dynamic symbol table, but only if there is
501 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
502 struct elf_link_hash_entry
*h
)
504 if (h
->dynindx
== -1)
506 struct elf_strtab_hash
*dynstr
;
511 if (h
->root
.type
== bfd_link_hash_defined
512 || h
->root
.type
== bfd_link_hash_defweak
)
514 /* An IR symbol should not be made dynamic. */
515 if (h
->root
.u
.def
.section
!= NULL
516 && h
->root
.u
.def
.section
->owner
!= NULL
517 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
521 /* XXX: The ABI draft says the linker must turn hidden and
522 internal symbols into STB_LOCAL symbols when producing the
523 DSO. However, if ld.so honors st_other in the dynamic table,
524 this would not be necessary. */
525 switch (ELF_ST_VISIBILITY (h
->other
))
529 if (h
->root
.type
!= bfd_link_hash_undefined
530 && h
->root
.type
!= bfd_link_hash_undefweak
)
540 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
541 ++elf_hash_table (info
)->dynsymcount
;
543 dynstr
= elf_hash_table (info
)->dynstr
;
546 /* Create a strtab to hold the dynamic symbol names. */
547 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
552 /* We don't put any version information in the dynamic string
554 name
= h
->root
.root
.string
;
555 p
= strchr (name
, ELF_VER_CHR
);
557 /* We know that the p points into writable memory. In fact,
558 there are only a few symbols that have read-only names, being
559 those like _GLOBAL_OFFSET_TABLE_ that are created specially
560 by the backends. Most symbols will have names pointing into
561 an ELF string table read from a file, or to objalloc memory. */
564 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
569 if (indx
== (size_t) -1)
571 h
->dynstr_index
= indx
;
577 /* Mark a symbol dynamic. */
580 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
581 struct elf_link_hash_entry
*h
,
582 Elf_Internal_Sym
*sym
)
584 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
586 /* It may be called more than once on the same H. */
587 if(h
->dynamic
|| bfd_link_relocatable (info
))
590 if ((info
->dynamic_data
591 && (h
->type
== STT_OBJECT
592 || h
->type
== STT_COMMON
594 && (ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
595 || ELF_ST_TYPE (sym
->st_info
) == STT_COMMON
))))
598 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
601 /* NB: If a symbol is made dynamic by --dynamic-list, it has
603 h
->root
.non_ir_ref_dynamic
= 1;
607 /* Record an assignment to a symbol made by a linker script. We need
608 this in case some dynamic object refers to this symbol. */
611 bfd_elf_record_link_assignment (bfd
*output_bfd
,
612 struct bfd_link_info
*info
,
617 struct elf_link_hash_entry
*h
, *hv
;
618 struct elf_link_hash_table
*htab
;
619 const struct elf_backend_data
*bed
;
621 if (!is_elf_hash_table (info
->hash
))
624 htab
= elf_hash_table (info
);
625 h
= elf_link_hash_lookup (htab
, name
, !provide
, true, false);
629 if (h
->root
.type
== bfd_link_hash_warning
)
630 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
632 if (h
->versioned
== unknown
)
634 /* Set versioned if symbol version is unknown. */
635 char *version
= strrchr (name
, ELF_VER_CHR
);
638 if (version
> name
&& version
[-1] != ELF_VER_CHR
)
639 h
->versioned
= versioned_hidden
;
641 h
->versioned
= versioned
;
645 /* Symbols defined in a linker script but not referenced anywhere
646 else will have non_elf set. */
649 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
653 switch (h
->root
.type
)
655 case bfd_link_hash_defined
:
656 case bfd_link_hash_defweak
:
657 case bfd_link_hash_common
:
659 case bfd_link_hash_undefweak
:
660 case bfd_link_hash_undefined
:
661 /* Since we're defining the symbol, don't let it seem to have not
662 been defined. record_dynamic_symbol and size_dynamic_sections
663 may depend on this. */
664 h
->root
.type
= bfd_link_hash_new
;
665 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
666 bfd_link_repair_undef_list (&htab
->root
);
668 case bfd_link_hash_new
:
670 case bfd_link_hash_indirect
:
671 /* We had a versioned symbol in a dynamic library. We make the
672 the versioned symbol point to this one. */
673 bed
= get_elf_backend_data (output_bfd
);
675 while (hv
->root
.type
== bfd_link_hash_indirect
676 || hv
->root
.type
== bfd_link_hash_warning
)
677 hv
= (struct elf_link_hash_entry
*) hv
->root
.u
.i
.link
;
678 /* We don't need to update h->root.u since linker will set them
680 h
->root
.type
= bfd_link_hash_undefined
;
681 hv
->root
.type
= bfd_link_hash_indirect
;
682 hv
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
683 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hv
);
690 /* If this symbol is being provided by the linker script, and it is
691 currently defined by a dynamic object, but not by a regular
692 object, then mark it as undefined so that the generic linker will
693 force the correct value. */
697 h
->root
.type
= bfd_link_hash_undefined
;
699 /* If this symbol is currently defined by a dynamic object, but not
700 by a regular object, then clear out any version information because
701 the symbol will not be associated with the dynamic object any
703 if (h
->def_dynamic
&& !h
->def_regular
)
704 h
->verinfo
.verdef
= NULL
;
706 /* Make sure this symbol is not garbage collected. */
713 bed
= get_elf_backend_data (output_bfd
);
714 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
715 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
716 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
719 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
721 if (!bfd_link_relocatable (info
)
723 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
724 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
729 || bfd_link_dll (info
))
733 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
736 /* If this is a weak defined symbol, and we know a corresponding
737 real symbol from the same dynamic object, make sure the real
738 symbol is also made into a dynamic symbol. */
741 struct elf_link_hash_entry
*def
= weakdef (h
);
743 if (def
->dynindx
== -1
744 && !bfd_elf_link_record_dynamic_symbol (info
, def
))
752 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
753 success, and 2 on a failure caused by attempting to record a symbol
754 in a discarded section, eg. a discarded link-once section symbol. */
757 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
762 struct elf_link_local_dynamic_entry
*entry
;
763 struct elf_link_hash_table
*eht
;
764 struct elf_strtab_hash
*dynstr
;
767 Elf_External_Sym_Shndx eshndx
;
768 char esym
[sizeof (Elf64_External_Sym
)];
770 if (! is_elf_hash_table (info
->hash
))
773 /* See if the entry exists already. */
774 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
775 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
778 amt
= sizeof (*entry
);
779 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
783 /* Go find the symbol, so that we can find it's name. */
784 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
785 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
787 bfd_release (input_bfd
, entry
);
791 if (entry
->isym
.st_shndx
!= SHN_UNDEF
792 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
796 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
797 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
799 /* We can still bfd_release here as nothing has done another
800 bfd_alloc. We can't do this later in this function. */
801 bfd_release (input_bfd
, entry
);
806 name
= (bfd_elf_string_from_elf_section
807 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
808 entry
->isym
.st_name
));
810 dynstr
= elf_hash_table (info
)->dynstr
;
813 /* Create a strtab to hold the dynamic symbol names. */
814 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
819 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, false);
820 if (dynstr_index
== (size_t) -1)
822 entry
->isym
.st_name
= dynstr_index
;
824 eht
= elf_hash_table (info
);
826 entry
->next
= eht
->dynlocal
;
827 eht
->dynlocal
= entry
;
828 entry
->input_bfd
= input_bfd
;
829 entry
->input_indx
= input_indx
;
832 /* Whatever binding the symbol had before, it's now local. */
834 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
836 /* The dynindx will be set at the end of size_dynamic_sections. */
841 /* Return the dynindex of a local dynamic symbol. */
844 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
848 struct elf_link_local_dynamic_entry
*e
;
850 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
851 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
856 /* This function is used to renumber the dynamic symbols, if some of
857 them are removed because they are marked as local. This is called
858 via elf_link_hash_traverse. */
861 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
864 size_t *count
= (size_t *) data
;
869 if (h
->dynindx
!= -1)
870 h
->dynindx
= ++(*count
);
876 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
877 STB_LOCAL binding. */
880 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
883 size_t *count
= (size_t *) data
;
885 if (!h
->forced_local
)
888 if (h
->dynindx
!= -1)
889 h
->dynindx
= ++(*count
);
894 /* Return true if the dynamic symbol for a given section should be
895 omitted when creating a shared library. */
897 _bfd_elf_omit_section_dynsym_default (bfd
*output_bfd ATTRIBUTE_UNUSED
,
898 struct bfd_link_info
*info
,
901 struct elf_link_hash_table
*htab
;
904 switch (elf_section_data (p
)->this_hdr
.sh_type
)
908 /* If sh_type is yet undecided, assume it could be
909 SHT_PROGBITS/SHT_NOBITS. */
911 htab
= elf_hash_table (info
);
912 if (htab
->text_index_section
!= NULL
)
913 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
915 return (htab
->dynobj
!= NULL
916 && (ip
= bfd_get_linker_section (htab
->dynobj
, p
->name
)) != NULL
917 && ip
->output_section
== p
);
919 /* There shouldn't be section relative relocations
920 against any other section. */
927 _bfd_elf_omit_section_dynsym_all
928 (bfd
*output_bfd ATTRIBUTE_UNUSED
,
929 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
930 asection
*p ATTRIBUTE_UNUSED
)
935 /* Assign dynsym indices. In a shared library we generate a section
936 symbol for each output section, which come first. Next come symbols
937 which have been forced to local binding. Then all of the back-end
938 allocated local dynamic syms, followed by the rest of the global
939 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
940 (This prevents the early call before elf_backend_init_index_section
941 and strip_excluded_output_sections setting dynindx for sections
942 that are stripped.) */
945 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
946 struct bfd_link_info
*info
,
947 unsigned long *section_sym_count
)
949 unsigned long dynsymcount
= 0;
950 bool do_sec
= section_sym_count
!= NULL
;
952 if (bfd_link_pic (info
)
953 || elf_hash_table (info
)->is_relocatable_executable
)
955 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
957 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
958 if ((p
->flags
& SEC_EXCLUDE
) == 0
959 && (p
->flags
& SEC_ALLOC
) != 0
960 && elf_hash_table (info
)->dynamic_relocs
961 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
965 elf_section_data (p
)->dynindx
= dynsymcount
;
968 elf_section_data (p
)->dynindx
= 0;
971 *section_sym_count
= dynsymcount
;
973 elf_link_hash_traverse (elf_hash_table (info
),
974 elf_link_renumber_local_hash_table_dynsyms
,
977 if (elf_hash_table (info
)->dynlocal
)
979 struct elf_link_local_dynamic_entry
*p
;
980 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
981 p
->dynindx
= ++dynsymcount
;
983 elf_hash_table (info
)->local_dynsymcount
= dynsymcount
;
985 elf_link_hash_traverse (elf_hash_table (info
),
986 elf_link_renumber_hash_table_dynsyms
,
989 /* There is an unused NULL entry at the head of the table which we
990 must account for in our count even if the table is empty since it
991 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
995 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
999 /* Merge st_other field. */
1002 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
1003 unsigned int st_other
, asection
*sec
,
1004 bool definition
, bool dynamic
)
1006 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1008 /* If st_other has a processor-specific meaning, specific
1009 code might be needed here. */
1010 if (bed
->elf_backend_merge_symbol_attribute
)
1011 (*bed
->elf_backend_merge_symbol_attribute
) (h
, st_other
, definition
,
1016 unsigned symvis
= ELF_ST_VISIBILITY (st_other
);
1017 unsigned hvis
= ELF_ST_VISIBILITY (h
->other
);
1019 /* Keep the most constraining visibility. Leave the remainder
1020 of the st_other field to elf_backend_merge_symbol_attribute. */
1021 if (symvis
- 1 < hvis
- 1)
1022 h
->other
= symvis
| (h
->other
& ~ELF_ST_VISIBILITY (-1));
1025 && ELF_ST_VISIBILITY (st_other
) != STV_DEFAULT
1026 && (sec
->flags
& SEC_READONLY
) == 0)
1027 h
->protected_def
= 1;
1030 /* This function is called when we want to merge a new symbol with an
1031 existing symbol. It handles the various cases which arise when we
1032 find a definition in a dynamic object, or when there is already a
1033 definition in a dynamic object. The new symbol is described by
1034 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1035 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1036 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1037 of an old common symbol. We set OVERRIDE if the old symbol is
1038 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1039 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1040 to change. By OK to change, we mean that we shouldn't warn if the
1041 type or size does change. */
1044 _bfd_elf_merge_symbol (bfd
*abfd
,
1045 struct bfd_link_info
*info
,
1047 Elf_Internal_Sym
*sym
,
1050 struct elf_link_hash_entry
**sym_hash
,
1053 unsigned int *pold_alignment
,
1056 bool *type_change_ok
,
1057 bool *size_change_ok
,
1060 asection
*sec
, *oldsec
;
1061 struct elf_link_hash_entry
*h
;
1062 struct elf_link_hash_entry
*hi
;
1063 struct elf_link_hash_entry
*flip
;
1066 bool newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
1067 bool newweak
, oldweak
, newfunc
, oldfunc
;
1068 const struct elf_backend_data
*bed
;
1070 bool default_sym
= *matched
;
1071 struct elf_link_hash_table
*htab
;
1077 bind
= ELF_ST_BIND (sym
->st_info
);
1079 if (! bfd_is_und_section (sec
))
1080 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, false, false);
1082 h
= ((struct elf_link_hash_entry
*)
1083 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, true, false, false));
1088 bed
= get_elf_backend_data (abfd
);
1090 /* NEW_VERSION is the symbol version of the new symbol. */
1091 if (h
->versioned
!= unversioned
)
1093 /* Symbol version is unknown or versioned. */
1094 new_version
= strrchr (name
, ELF_VER_CHR
);
1097 if (h
->versioned
== unknown
)
1099 if (new_version
> name
&& new_version
[-1] != ELF_VER_CHR
)
1100 h
->versioned
= versioned_hidden
;
1102 h
->versioned
= versioned
;
1105 if (new_version
[0] == '\0')
1109 h
->versioned
= unversioned
;
1114 /* For merging, we only care about real symbols. But we need to make
1115 sure that indirect symbol dynamic flags are updated. */
1117 while (h
->root
.type
== bfd_link_hash_indirect
1118 || h
->root
.type
== bfd_link_hash_warning
)
1119 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1123 if (hi
== h
|| h
->root
.type
== bfd_link_hash_new
)
1127 /* OLD_HIDDEN is true if the existing symbol is only visible
1128 to the symbol with the same symbol version. NEW_HIDDEN is
1129 true if the new symbol is only visible to the symbol with
1130 the same symbol version. */
1131 bool old_hidden
= h
->versioned
== versioned_hidden
;
1132 bool new_hidden
= hi
->versioned
== versioned_hidden
;
1133 if (!old_hidden
&& !new_hidden
)
1134 /* The new symbol matches the existing symbol if both
1139 /* OLD_VERSION is the symbol version of the existing
1143 if (h
->versioned
>= versioned
)
1144 old_version
= strrchr (h
->root
.root
.string
,
1149 /* The new symbol matches the existing symbol if they
1150 have the same symbol version. */
1151 *matched
= (old_version
== new_version
1152 || (old_version
!= NULL
1153 && new_version
!= NULL
1154 && strcmp (old_version
, new_version
) == 0));
1159 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1164 switch (h
->root
.type
)
1169 case bfd_link_hash_undefined
:
1170 case bfd_link_hash_undefweak
:
1171 oldbfd
= h
->root
.u
.undef
.abfd
;
1174 case bfd_link_hash_defined
:
1175 case bfd_link_hash_defweak
:
1176 oldbfd
= h
->root
.u
.def
.section
->owner
;
1177 oldsec
= h
->root
.u
.def
.section
;
1180 case bfd_link_hash_common
:
1181 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1182 oldsec
= h
->root
.u
.c
.p
->section
;
1184 *pold_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1187 if (poldbfd
&& *poldbfd
== NULL
)
1190 /* Differentiate strong and weak symbols. */
1191 newweak
= bind
== STB_WEAK
;
1192 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1193 || h
->root
.type
== bfd_link_hash_undefweak
);
1195 *pold_weak
= oldweak
;
1197 /* We have to check it for every instance since the first few may be
1198 references and not all compilers emit symbol type for undefined
1200 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1202 htab
= elf_hash_table (info
);
1204 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1205 respectively, is from a dynamic object. */
1207 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1209 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1210 syms and defined syms in dynamic libraries respectively.
1211 ref_dynamic on the other hand can be set for a symbol defined in
1212 a dynamic library, and def_dynamic may not be set; When the
1213 definition in a dynamic lib is overridden by a definition in the
1214 executable use of the symbol in the dynamic lib becomes a
1215 reference to the executable symbol. */
1218 if (bfd_is_und_section (sec
))
1220 if (bind
!= STB_WEAK
)
1222 h
->ref_dynamic_nonweak
= 1;
1223 hi
->ref_dynamic_nonweak
= 1;
1228 /* Update the existing symbol only if they match. */
1231 hi
->dynamic_def
= 1;
1235 /* If we just created the symbol, mark it as being an ELF symbol.
1236 Other than that, there is nothing to do--there is no merge issue
1237 with a newly defined symbol--so we just return. */
1239 if (h
->root
.type
== bfd_link_hash_new
)
1245 /* In cases involving weak versioned symbols, we may wind up trying
1246 to merge a symbol with itself. Catch that here, to avoid the
1247 confusion that results if we try to override a symbol with
1248 itself. The additional tests catch cases like
1249 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1250 dynamic object, which we do want to handle here. */
1252 && (newweak
|| oldweak
)
1253 && ((abfd
->flags
& DYNAMIC
) == 0
1254 || !h
->def_regular
))
1259 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1260 else if (oldsec
!= NULL
)
1262 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1263 indices used by MIPS ELF. */
1264 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1267 /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries. */
1268 if (!htab
->handling_dt_needed
1270 && (oldbfd
->flags
& BFD_PLUGIN
) != (abfd
->flags
& BFD_PLUGIN
))
1272 if (newdyn
!= olddyn
)
1274 /* Handle a case where plugin_notice won't be called and thus
1275 won't set the non_ir_ref flags on the first pass over
1277 h
->root
.non_ir_ref_dynamic
= true;
1278 hi
->root
.non_ir_ref_dynamic
= true;
1280 else if ((oldbfd
->flags
& BFD_PLUGIN
) != 0
1281 && hi
->root
.type
== bfd_link_hash_indirect
)
1283 /* Change indirect symbol from IR to undefined. */
1284 hi
->root
.type
= bfd_link_hash_undefined
;
1285 hi
->root
.u
.undef
.abfd
= oldbfd
;
1289 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1290 respectively, appear to be a definition rather than reference. */
1292 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1294 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1295 && h
->root
.type
!= bfd_link_hash_undefweak
1296 && h
->root
.type
!= bfd_link_hash_common
);
1298 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1299 respectively, appear to be a function. */
1301 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1302 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1304 oldfunc
= (h
->type
!= STT_NOTYPE
1305 && bed
->is_function_type (h
->type
));
1307 if (!(newfunc
&& oldfunc
)
1308 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1309 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1310 && h
->type
!= STT_NOTYPE
1311 && (newdef
|| bfd_is_com_section (sec
))
1312 && (olddef
|| h
->root
.type
== bfd_link_hash_common
))
1314 /* If creating a default indirect symbol ("foo" or "foo@") from
1315 a dynamic versioned definition ("foo@@") skip doing so if
1316 there is an existing regular definition with a different
1317 type. We don't want, for example, a "time" variable in the
1318 executable overriding a "time" function in a shared library. */
1326 /* When adding a symbol from a regular object file after we have
1327 created indirect symbols, undo the indirection and any
1334 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1335 h
->forced_local
= 0;
1339 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1341 h
->root
.type
= bfd_link_hash_undefined
;
1342 h
->root
.u
.undef
.abfd
= abfd
;
1346 h
->root
.type
= bfd_link_hash_new
;
1347 h
->root
.u
.undef
.abfd
= NULL
;
1353 /* Check TLS symbols. We don't check undefined symbols introduced
1354 by "ld -u" which have no type (and oldbfd NULL), and we don't
1355 check symbols from plugins because they also have no type. */
1357 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1358 && (abfd
->flags
& BFD_PLUGIN
) == 0
1359 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1360 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1364 asection
*ntsec
, *tsec
;
1366 if (h
->type
== STT_TLS
)
1387 /* xgettext:c-format */
1388 (_("%s: TLS definition in %pB section %pA "
1389 "mismatches non-TLS definition in %pB section %pA"),
1390 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
, ntsec
);
1391 else if (!tdef
&& !ntdef
)
1393 /* xgettext:c-format */
1394 (_("%s: TLS reference in %pB "
1395 "mismatches non-TLS reference in %pB"),
1396 h
->root
.root
.string
, tbfd
, ntbfd
);
1399 /* xgettext:c-format */
1400 (_("%s: TLS definition in %pB section %pA "
1401 "mismatches non-TLS reference in %pB"),
1402 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
);
1405 /* xgettext:c-format */
1406 (_("%s: TLS reference in %pB "
1407 "mismatches non-TLS definition in %pB section %pA"),
1408 h
->root
.root
.string
, tbfd
, ntbfd
, ntsec
);
1410 bfd_set_error (bfd_error_bad_value
);
1414 /* If the old symbol has non-default visibility, we ignore the new
1415 definition from a dynamic object. */
1417 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1418 && !bfd_is_und_section (sec
))
1421 /* Make sure this symbol is dynamic. */
1423 hi
->ref_dynamic
= 1;
1424 /* A protected symbol has external availability. Make sure it is
1425 recorded as dynamic.
1427 FIXME: Should we check type and size for protected symbol? */
1428 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1429 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1434 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1437 /* If the new symbol with non-default visibility comes from a
1438 relocatable file and the old definition comes from a dynamic
1439 object, we remove the old definition. */
1440 if (hi
->root
.type
== bfd_link_hash_indirect
)
1442 /* Handle the case where the old dynamic definition is
1443 default versioned. We need to copy the symbol info from
1444 the symbol with default version to the normal one if it
1445 was referenced before. */
1448 hi
->root
.type
= h
->root
.type
;
1449 h
->root
.type
= bfd_link_hash_indirect
;
1450 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1452 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1453 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1455 /* If the new symbol is hidden or internal, completely undo
1456 any dynamic link state. */
1457 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1458 h
->forced_local
= 0;
1465 /* FIXME: Should we check type and size for protected symbol? */
1475 /* If the old symbol was undefined before, then it will still be
1476 on the undefs list. If the new symbol is undefined or
1477 common, we can't make it bfd_link_hash_new here, because new
1478 undefined or common symbols will be added to the undefs list
1479 by _bfd_generic_link_add_one_symbol. Symbols may not be
1480 added twice to the undefs list. Also, if the new symbol is
1481 undefweak then we don't want to lose the strong undef. */
1482 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1484 h
->root
.type
= bfd_link_hash_undefined
;
1485 h
->root
.u
.undef
.abfd
= abfd
;
1489 h
->root
.type
= bfd_link_hash_new
;
1490 h
->root
.u
.undef
.abfd
= NULL
;
1493 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1495 /* If the new symbol is hidden or internal, completely undo
1496 any dynamic link state. */
1497 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1498 h
->forced_local
= 0;
1504 /* FIXME: Should we check type and size for protected symbol? */
1510 /* If a new weak symbol definition comes from a regular file and the
1511 old symbol comes from a dynamic library, we treat the new one as
1512 strong. Similarly, an old weak symbol definition from a regular
1513 file is treated as strong when the new symbol comes from a dynamic
1514 library. Further, an old weak symbol from a dynamic library is
1515 treated as strong if the new symbol is from a dynamic library.
1516 This reflects the way glibc's ld.so works.
1518 Also allow a weak symbol to override a linker script symbol
1519 defined by an early pass over the script. This is done so the
1520 linker knows the symbol is defined in an object file, for the
1521 DEFINED script function.
1523 Do this before setting *type_change_ok or *size_change_ok so that
1524 we warn properly when dynamic library symbols are overridden. */
1526 if (newdef
&& !newdyn
&& (olddyn
|| h
->root
.ldscript_def
))
1528 if (olddef
&& newdyn
)
1531 /* Allow changes between different types of function symbol. */
1532 if (newfunc
&& oldfunc
)
1533 *type_change_ok
= true;
1535 /* It's OK to change the type if either the existing symbol or the
1536 new symbol is weak. A type change is also OK if the old symbol
1537 is undefined and the new symbol is defined. */
1542 && h
->root
.type
== bfd_link_hash_undefined
))
1543 *type_change_ok
= true;
1545 /* It's OK to change the size if either the existing symbol or the
1546 new symbol is weak, or if the old symbol is undefined. */
1549 || h
->root
.type
== bfd_link_hash_undefined
)
1550 *size_change_ok
= true;
1552 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1553 symbol, respectively, appears to be a common symbol in a dynamic
1554 object. If a symbol appears in an uninitialized section, and is
1555 not weak, and is not a function, then it may be a common symbol
1556 which was resolved when the dynamic object was created. We want
1557 to treat such symbols specially, because they raise special
1558 considerations when setting the symbol size: if the symbol
1559 appears as a common symbol in a regular object, and the size in
1560 the regular object is larger, we must make sure that we use the
1561 larger size. This problematic case can always be avoided in C,
1562 but it must be handled correctly when using Fortran shared
1565 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1566 likewise for OLDDYNCOMMON and OLDDEF.
1568 Note that this test is just a heuristic, and that it is quite
1569 possible to have an uninitialized symbol in a shared object which
1570 is really a definition, rather than a common symbol. This could
1571 lead to some minor confusion when the symbol really is a common
1572 symbol in some regular object. However, I think it will be
1578 && (sec
->flags
& SEC_ALLOC
) != 0
1579 && (sec
->flags
& SEC_LOAD
) == 0
1582 newdyncommon
= true;
1584 newdyncommon
= false;
1588 && h
->root
.type
== bfd_link_hash_defined
1590 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1591 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1594 olddyncommon
= true;
1596 olddyncommon
= false;
1598 /* We now know everything about the old and new symbols. We ask the
1599 backend to check if we can merge them. */
1600 if (bed
->merge_symbol
!= NULL
)
1602 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1607 /* There are multiple definitions of a normal symbol. Skip the
1608 default symbol as well as definition from an IR object. */
1609 if (olddef
&& !olddyn
&& !oldweak
&& newdef
&& !newdyn
&& !newweak
1610 && !default_sym
&& h
->def_regular
1612 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1613 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1615 /* Handle a multiple definition. */
1616 (*info
->callbacks
->multiple_definition
) (info
, &h
->root
,
1617 abfd
, sec
, *pvalue
);
1622 /* If both the old and the new symbols look like common symbols in a
1623 dynamic object, set the size of the symbol to the larger of the
1628 && sym
->st_size
!= h
->size
)
1630 /* Since we think we have two common symbols, issue a multiple
1631 common warning if desired. Note that we only warn if the
1632 size is different. If the size is the same, we simply let
1633 the old symbol override the new one as normally happens with
1634 symbols defined in dynamic objects. */
1636 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1637 bfd_link_hash_common
, sym
->st_size
);
1638 if (sym
->st_size
> h
->size
)
1639 h
->size
= sym
->st_size
;
1641 *size_change_ok
= true;
1644 /* If we are looking at a dynamic object, and we have found a
1645 definition, we need to see if the symbol was already defined by
1646 some other object. If so, we want to use the existing
1647 definition, and we do not want to report a multiple symbol
1648 definition error; we do this by clobbering *PSEC to be
1649 bfd_und_section_ptr.
1651 We treat a common symbol as a definition if the symbol in the
1652 shared library is a function, since common symbols always
1653 represent variables; this can cause confusion in principle, but
1654 any such confusion would seem to indicate an erroneous program or
1655 shared library. We also permit a common symbol in a regular
1656 object to override a weak symbol in a shared object. */
1661 || (h
->root
.type
== bfd_link_hash_common
1662 && (newweak
|| newfunc
))))
1666 newdyncommon
= false;
1668 *psec
= sec
= bfd_und_section_ptr
;
1669 *size_change_ok
= true;
1671 /* If we get here when the old symbol is a common symbol, then
1672 we are explicitly letting it override a weak symbol or
1673 function in a dynamic object, and we don't want to warn about
1674 a type change. If the old symbol is a defined symbol, a type
1675 change warning may still be appropriate. */
1677 if (h
->root
.type
== bfd_link_hash_common
)
1678 *type_change_ok
= true;
1681 /* Handle the special case of an old common symbol merging with a
1682 new symbol which looks like a common symbol in a shared object.
1683 We change *PSEC and *PVALUE to make the new symbol look like a
1684 common symbol, and let _bfd_generic_link_add_one_symbol do the
1688 && h
->root
.type
== bfd_link_hash_common
)
1692 newdyncommon
= false;
1693 *pvalue
= sym
->st_size
;
1694 *psec
= sec
= bed
->common_section (oldsec
);
1695 *size_change_ok
= true;
1698 /* Skip weak definitions of symbols that are already defined. */
1699 if (newdef
&& olddef
&& newweak
)
1701 /* Don't skip new non-IR weak syms. */
1702 if (!(oldbfd
!= NULL
1703 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1704 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1710 /* Merge st_other. If the symbol already has a dynamic index,
1711 but visibility says it should not be visible, turn it into a
1713 elf_merge_st_other (abfd
, h
, sym
->st_other
, sec
, newdef
, newdyn
);
1714 if (h
->dynindx
!= -1)
1715 switch (ELF_ST_VISIBILITY (h
->other
))
1719 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1724 /* If the old symbol is from a dynamic object, and the new symbol is
1725 a definition which is not from a dynamic object, then the new
1726 symbol overrides the old symbol. Symbols from regular files
1727 always take precedence over symbols from dynamic objects, even if
1728 they are defined after the dynamic object in the link.
1730 As above, we again permit a common symbol in a regular object to
1731 override a definition in a shared object if the shared object
1732 symbol is a function or is weak. */
1737 || (bfd_is_com_section (sec
)
1738 && (oldweak
|| oldfunc
)))
1743 /* Change the hash table entry to undefined, and let
1744 _bfd_generic_link_add_one_symbol do the right thing with the
1747 h
->root
.type
= bfd_link_hash_undefined
;
1748 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1749 *size_change_ok
= true;
1752 olddyncommon
= false;
1754 /* We again permit a type change when a common symbol may be
1755 overriding a function. */
1757 if (bfd_is_com_section (sec
))
1761 /* If a common symbol overrides a function, make sure
1762 that it isn't defined dynamically nor has type
1765 h
->type
= STT_NOTYPE
;
1767 *type_change_ok
= true;
1770 if (hi
->root
.type
== bfd_link_hash_indirect
)
1773 /* This union may have been set to be non-NULL when this symbol
1774 was seen in a dynamic object. We must force the union to be
1775 NULL, so that it is correct for a regular symbol. */
1776 h
->verinfo
.vertree
= NULL
;
1779 /* Handle the special case of a new common symbol merging with an
1780 old symbol that looks like it might be a common symbol defined in
1781 a shared object. Note that we have already handled the case in
1782 which a new common symbol should simply override the definition
1783 in the shared library. */
1786 && bfd_is_com_section (sec
)
1789 /* It would be best if we could set the hash table entry to a
1790 common symbol, but we don't know what to use for the section
1791 or the alignment. */
1792 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1793 bfd_link_hash_common
, sym
->st_size
);
1795 /* If the presumed common symbol in the dynamic object is
1796 larger, pretend that the new symbol has its size. */
1798 if (h
->size
> *pvalue
)
1801 /* We need to remember the alignment required by the symbol
1802 in the dynamic object. */
1803 BFD_ASSERT (pold_alignment
);
1804 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1807 olddyncommon
= false;
1809 h
->root
.type
= bfd_link_hash_undefined
;
1810 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1812 *size_change_ok
= true;
1813 *type_change_ok
= true;
1815 if (hi
->root
.type
== bfd_link_hash_indirect
)
1818 h
->verinfo
.vertree
= NULL
;
1823 /* Handle the case where we had a versioned symbol in a dynamic
1824 library and now find a definition in a normal object. In this
1825 case, we make the versioned symbol point to the normal one. */
1826 flip
->root
.type
= h
->root
.type
;
1827 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1828 h
->root
.type
= bfd_link_hash_indirect
;
1829 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1830 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1834 flip
->ref_dynamic
= 1;
1841 /* This function is called to create an indirect symbol from the
1842 default for the symbol with the default version if needed. The
1843 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1844 set DYNSYM if the new indirect symbol is dynamic. */
1847 _bfd_elf_add_default_symbol (bfd
*abfd
,
1848 struct bfd_link_info
*info
,
1849 struct elf_link_hash_entry
*h
,
1851 Elf_Internal_Sym
*sym
,
1857 bool type_change_ok
;
1858 bool size_change_ok
;
1861 struct elf_link_hash_entry
*hi
;
1862 struct bfd_link_hash_entry
*bh
;
1863 const struct elf_backend_data
*bed
;
1868 size_t len
, shortlen
;
1872 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1875 /* If this symbol has a version, and it is the default version, we
1876 create an indirect symbol from the default name to the fully
1877 decorated name. This will cause external references which do not
1878 specify a version to be bound to this version of the symbol. */
1879 p
= strchr (name
, ELF_VER_CHR
);
1880 if (h
->versioned
== unknown
)
1884 h
->versioned
= unversioned
;
1889 if (p
[1] != ELF_VER_CHR
)
1891 h
->versioned
= versioned_hidden
;
1895 h
->versioned
= versioned
;
1900 /* PR ld/19073: We may see an unversioned definition after the
1906 bed
= get_elf_backend_data (abfd
);
1907 collect
= bed
->collect
;
1908 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1910 shortlen
= p
- name
;
1911 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1912 if (shortname
== NULL
)
1914 memcpy (shortname
, name
, shortlen
);
1915 shortname
[shortlen
] = '\0';
1917 /* We are going to create a new symbol. Merge it with any existing
1918 symbol with this name. For the purposes of the merge, act as
1919 though we were defining the symbol we just defined, although we
1920 actually going to define an indirect symbol. */
1921 type_change_ok
= false;
1922 size_change_ok
= false;
1925 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1926 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1927 &type_change_ok
, &size_change_ok
, &matched
))
1933 if (hi
->def_regular
|| ELF_COMMON_DEF_P (hi
))
1935 /* If the undecorated symbol will have a version added by a
1936 script different to H, then don't indirect to/from the
1937 undecorated symbol. This isn't ideal because we may not yet
1938 have seen symbol versions, if given by a script on the
1939 command line rather than via --version-script. */
1940 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
1945 = bfd_find_version_for_sym (info
->version_info
,
1946 hi
->root
.root
.string
, &hide
);
1947 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
1949 (*bed
->elf_backend_hide_symbol
) (info
, hi
, true);
1953 if (hi
->verinfo
.vertree
!= NULL
1954 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
1960 /* Add the default symbol if not performing a relocatable link. */
1961 if (! bfd_link_relocatable (info
))
1964 if (bh
->type
== bfd_link_hash_defined
1965 && bh
->u
.def
.section
->owner
!= NULL
1966 && (bh
->u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
1968 /* Mark the previous definition from IR object as
1969 undefined so that the generic linker will override
1971 bh
->type
= bfd_link_hash_undefined
;
1972 bh
->u
.undef
.abfd
= bh
->u
.def
.section
->owner
;
1974 if (! (_bfd_generic_link_add_one_symbol
1975 (info
, abfd
, shortname
, BSF_INDIRECT
,
1976 bfd_ind_section_ptr
,
1977 0, name
, false, collect
, &bh
)))
1979 hi
= (struct elf_link_hash_entry
*) bh
;
1984 /* In this case the symbol named SHORTNAME is overriding the
1985 indirect symbol we want to add. We were planning on making
1986 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1987 is the name without a version. NAME is the fully versioned
1988 name, and it is the default version.
1990 Overriding means that we already saw a definition for the
1991 symbol SHORTNAME in a regular object, and it is overriding
1992 the symbol defined in the dynamic object.
1994 When this happens, we actually want to change NAME, the
1995 symbol we just added, to refer to SHORTNAME. This will cause
1996 references to NAME in the shared object to become references
1997 to SHORTNAME in the regular object. This is what we expect
1998 when we override a function in a shared object: that the
1999 references in the shared object will be mapped to the
2000 definition in the regular object. */
2002 while (hi
->root
.type
== bfd_link_hash_indirect
2003 || hi
->root
.type
== bfd_link_hash_warning
)
2004 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2006 h
->root
.type
= bfd_link_hash_indirect
;
2007 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
2011 hi
->ref_dynamic
= 1;
2015 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
2020 /* Now set HI to H, so that the following code will set the
2021 other fields correctly. */
2025 /* Check if HI is a warning symbol. */
2026 if (hi
->root
.type
== bfd_link_hash_warning
)
2027 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2029 /* If there is a duplicate definition somewhere, then HI may not
2030 point to an indirect symbol. We will have reported an error to
2031 the user in that case. */
2033 if (hi
->root
.type
== bfd_link_hash_indirect
)
2035 struct elf_link_hash_entry
*ht
;
2037 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2038 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
2040 /* If we first saw a reference to SHORTNAME with non-default
2041 visibility, merge that visibility to the @@VER symbol. */
2042 elf_merge_st_other (abfd
, ht
, hi
->other
, sec
, true, dynamic
);
2044 /* A reference to the SHORTNAME symbol from a dynamic library
2045 will be satisfied by the versioned symbol at runtime. In
2046 effect, we have a reference to the versioned symbol. */
2047 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2048 hi
->dynamic_def
|= ht
->dynamic_def
;
2050 /* See if the new flags lead us to realize that the symbol must
2056 if (! bfd_link_executable (info
)
2063 if (hi
->ref_regular
)
2069 /* We also need to define an indirection from the nondefault version
2073 len
= strlen (name
);
2074 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
2075 if (shortname
== NULL
)
2077 memcpy (shortname
, name
, shortlen
);
2078 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
2080 /* Once again, merge with any existing symbol. */
2081 type_change_ok
= false;
2082 size_change_ok
= false;
2084 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
2085 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
2086 &type_change_ok
, &size_change_ok
, &matched
))
2092 && h
->root
.type
== bfd_link_hash_defweak
2093 && hi
->root
.type
== bfd_link_hash_defined
)
2095 /* We are handling a weak sym@@ver and attempting to define
2096 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2097 new weak sym@ver because there is already a strong sym@ver.
2098 However, sym@ver and sym@@ver are really the same symbol.
2099 The existing strong sym@ver ought to override sym@@ver. */
2100 h
->root
.type
= bfd_link_hash_defined
;
2101 h
->root
.u
.def
.section
= hi
->root
.u
.def
.section
;
2102 h
->root
.u
.def
.value
= hi
->root
.u
.def
.value
;
2103 hi
->root
.type
= bfd_link_hash_indirect
;
2104 hi
->root
.u
.i
.link
= &h
->root
;
2111 /* Here SHORTNAME is a versioned name, so we don't expect to see
2112 the type of override we do in the case above unless it is
2113 overridden by a versioned definition. */
2114 if (hi
->root
.type
!= bfd_link_hash_defined
2115 && hi
->root
.type
!= bfd_link_hash_defweak
)
2117 /* xgettext:c-format */
2118 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2125 if (! (_bfd_generic_link_add_one_symbol
2126 (info
, abfd
, shortname
, BSF_INDIRECT
,
2127 bfd_ind_section_ptr
, 0, name
, false, collect
, &bh
)))
2129 hi
= (struct elf_link_hash_entry
*) bh
;
2132 /* If there is a duplicate definition somewhere, then HI may not
2133 point to an indirect symbol. We will have reported an error
2134 to the user in that case. */
2135 if (hi
->root
.type
== bfd_link_hash_indirect
)
2137 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
2138 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2139 hi
->dynamic_def
|= h
->dynamic_def
;
2141 /* If we first saw a reference to @VER symbol with
2142 non-default visibility, merge that visibility to the
2144 elf_merge_st_other (abfd
, h
, hi
->other
, sec
, true, dynamic
);
2146 /* See if the new flags lead us to realize that the symbol
2152 if (! bfd_link_executable (info
)
2158 if (hi
->ref_regular
)
2167 /* This routine is used to export all defined symbols into the dynamic
2168 symbol table. It is called via elf_link_hash_traverse. */
2171 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
2173 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2175 /* Ignore indirect symbols. These are added by the versioning code. */
2176 if (h
->root
.type
== bfd_link_hash_indirect
)
2179 /* Ignore this if we won't export it. */
2180 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2183 if (h
->dynindx
== -1
2184 && (h
->def_regular
|| h
->ref_regular
)
2185 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2186 h
->root
.root
.string
))
2188 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2198 /* Return the glibc version reference if VERSION_DEP is added to the
2199 list of glibc version dependencies successfully. VERSION_DEP will
2200 be put into the .gnu.version_r section. */
2202 static Elf_Internal_Verneed
*
2203 elf_link_add_glibc_verneed (struct elf_find_verdep_info
*rinfo
,
2204 Elf_Internal_Verneed
*glibc_verref
,
2205 const char *version_dep
)
2207 Elf_Internal_Verneed
*t
;
2208 Elf_Internal_Vernaux
*a
;
2211 if (glibc_verref
!= NULL
)
2215 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2217 /* Return if VERSION_DEP dependency has been added. */
2218 if (a
->vna_nodename
== version_dep
2219 || strcmp (a
->vna_nodename
, version_dep
) == 0)
2227 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2231 const char *soname
= bfd_elf_get_dt_soname (t
->vn_bfd
);
2232 if (soname
!= NULL
&& startswith (soname
, "libc.so."))
2236 /* Skip the shared library if it isn't libc.so. */
2241 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2243 /* Return if VERSION_DEP dependency has been added. */
2244 if (a
->vna_nodename
== version_dep
2245 || strcmp (a
->vna_nodename
, version_dep
) == 0)
2248 /* Check if libc.so provides GLIBC_2.XX version. */
2249 if (!is_glibc
&& startswith (a
->vna_nodename
, "GLIBC_2."))
2253 /* Skip if it isn't linked against glibc. */
2259 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2262 rinfo
->failed
= true;
2266 a
->vna_nodename
= version_dep
;
2268 a
->vna_nextptr
= t
->vn_auxptr
;
2269 a
->vna_other
= rinfo
->vers
+ 1;
2277 /* Add VERSION_DEP to the list of version dependencies when linked
2281 _bfd_elf_link_add_glibc_version_dependency
2282 (struct elf_find_verdep_info
*rinfo
,
2283 const char *version_dep
[])
2285 Elf_Internal_Verneed
*t
= NULL
;
2289 t
= elf_link_add_glibc_verneed (rinfo
, t
, *version_dep
);
2290 /* Return if there is no glibc version reference. */
2295 while (*version_dep
!= NULL
);
2298 /* Add GLIBC_ABI_DT_RELR to the list of version dependencies when
2299 linked against glibc. */
2302 _bfd_elf_link_add_dt_relr_dependency (struct elf_find_verdep_info
*rinfo
)
2304 if (rinfo
->info
->enable_dt_relr
)
2306 const char *version
[] =
2308 "GLIBC_ABI_DT_RELR",
2311 _bfd_elf_link_add_glibc_version_dependency (rinfo
, version
);
2315 /* Look through the symbols which are defined in other shared
2316 libraries and referenced here. Update the list of version
2317 dependencies. This will be put into the .gnu.version_r section.
2318 This function is called via elf_link_hash_traverse. */
2321 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2324 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2325 Elf_Internal_Verneed
*t
;
2326 Elf_Internal_Vernaux
*a
;
2329 /* We only care about symbols defined in shared objects with version
2334 || h
->verinfo
.verdef
== NULL
2335 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2336 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2339 /* See if we already know about this version. */
2340 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2344 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2347 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2348 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2354 /* This is a new version. Add it to tree we are building. */
2359 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2362 rinfo
->failed
= true;
2366 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2367 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2368 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2372 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2375 rinfo
->failed
= true;
2379 /* Note that we are copying a string pointer here, and testing it
2380 above. If bfd_elf_string_from_elf_section is ever changed to
2381 discard the string data when low in memory, this will have to be
2383 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2385 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2386 a
->vna_nextptr
= t
->vn_auxptr
;
2388 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2391 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2398 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2399 hidden. Set *T_P to NULL if there is no match. */
2402 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info
*info
,
2403 struct elf_link_hash_entry
*h
,
2404 const char *version_p
,
2405 struct bfd_elf_version_tree
**t_p
,
2408 struct bfd_elf_version_tree
*t
;
2410 /* Look for the version. If we find it, it is no longer weak. */
2411 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
2413 if (strcmp (t
->name
, version_p
) == 0)
2417 struct bfd_elf_version_expr
*d
;
2419 len
= version_p
- h
->root
.root
.string
;
2420 alc
= (char *) bfd_malloc (len
);
2423 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2424 alc
[len
- 1] = '\0';
2425 if (alc
[len
- 2] == ELF_VER_CHR
)
2426 alc
[len
- 2] = '\0';
2428 h
->verinfo
.vertree
= t
;
2432 if (t
->globals
.list
!= NULL
)
2433 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2435 /* See if there is anything to force this symbol to
2437 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2439 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2442 && ! info
->export_dynamic
)
2456 /* Return TRUE if the symbol H is hidden by version script. */
2459 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info
*info
,
2460 struct elf_link_hash_entry
*h
)
2464 const struct elf_backend_data
*bed
2465 = get_elf_backend_data (info
->output_bfd
);
2467 /* Version script only hides symbols defined in regular objects. */
2468 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2471 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2472 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2474 struct bfd_elf_version_tree
*t
;
2477 if (*p
== ELF_VER_CHR
)
2481 && _bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
)
2485 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2490 /* If we don't have a version for this symbol, see if we can find
2492 if (h
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
2495 = bfd_find_version_for_sym (info
->version_info
,
2496 h
->root
.root
.string
, &hide
);
2497 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2499 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2507 /* Figure out appropriate versions for all the symbols. We may not
2508 have the version number script until we have read all of the input
2509 files, so until that point we don't know which symbols should be
2510 local. This function is called via elf_link_hash_traverse. */
2513 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2515 struct elf_info_failed
*sinfo
;
2516 struct bfd_link_info
*info
;
2517 const struct elf_backend_data
*bed
;
2518 struct elf_info_failed eif
;
2522 sinfo
= (struct elf_info_failed
*) data
;
2525 /* Fix the symbol flags. */
2528 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2531 sinfo
->failed
= true;
2535 bed
= get_elf_backend_data (info
->output_bfd
);
2537 /* We only need version numbers for symbols defined in regular
2539 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2541 /* Hide symbols defined in discarded input sections. */
2542 if ((h
->root
.type
== bfd_link_hash_defined
2543 || h
->root
.type
== bfd_link_hash_defweak
)
2544 && discarded_section (h
->root
.u
.def
.section
))
2545 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2550 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2551 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2553 struct bfd_elf_version_tree
*t
;
2556 if (*p
== ELF_VER_CHR
)
2559 /* If there is no version string, we can just return out. */
2563 if (!_bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
))
2565 sinfo
->failed
= true;
2570 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2572 /* If we are building an application, we need to create a
2573 version node for this version. */
2574 if (t
== NULL
&& bfd_link_executable (info
))
2576 struct bfd_elf_version_tree
**pp
;
2579 /* If we aren't going to export this symbol, we don't need
2580 to worry about it. */
2581 if (h
->dynindx
== -1)
2584 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2588 sinfo
->failed
= true;
2593 t
->name_indx
= (unsigned int) -1;
2597 /* Don't count anonymous version tag. */
2598 if (sinfo
->info
->version_info
!= NULL
2599 && sinfo
->info
->version_info
->vernum
== 0)
2601 for (pp
= &sinfo
->info
->version_info
;
2605 t
->vernum
= version_index
;
2609 h
->verinfo
.vertree
= t
;
2613 /* We could not find the version for a symbol when
2614 generating a shared archive. Return an error. */
2616 /* xgettext:c-format */
2617 (_("%pB: version node not found for symbol %s"),
2618 info
->output_bfd
, h
->root
.root
.string
);
2619 bfd_set_error (bfd_error_bad_value
);
2620 sinfo
->failed
= true;
2625 /* If we don't have a version for this symbol, see if we can find
2628 && h
->verinfo
.vertree
== NULL
2629 && sinfo
->info
->version_info
!= NULL
)
2632 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2633 h
->root
.root
.string
, &hide
);
2634 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2635 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2641 /* Read and swap the relocs from the section indicated by SHDR. This
2642 may be either a REL or a RELA section. The relocations are
2643 translated into RELA relocations and stored in INTERNAL_RELOCS,
2644 which should have already been allocated to contain enough space.
2645 The EXTERNAL_RELOCS are a buffer where the external form of the
2646 relocations should be stored.
2648 Returns FALSE if something goes wrong. */
2651 elf_link_read_relocs_from_section (bfd
*abfd
,
2653 Elf_Internal_Shdr
*shdr
,
2654 void *external_relocs
,
2655 Elf_Internal_Rela
*internal_relocs
)
2657 const struct elf_backend_data
*bed
;
2658 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2659 const bfd_byte
*erela
;
2660 const bfd_byte
*erelaend
;
2661 Elf_Internal_Rela
*irela
;
2662 Elf_Internal_Shdr
*symtab_hdr
;
2665 /* Position ourselves at the start of the section. */
2666 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2669 /* Read the relocations. */
2670 if (bfd_read (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2673 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2674 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2676 bed
= get_elf_backend_data (abfd
);
2678 /* Convert the external relocations to the internal format. */
2679 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2680 swap_in
= bed
->s
->swap_reloc_in
;
2681 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2682 swap_in
= bed
->s
->swap_reloca_in
;
2685 bfd_set_error (bfd_error_wrong_format
);
2689 erela
= (const bfd_byte
*) external_relocs
;
2690 /* Setting erelaend like this and comparing with <= handles case of
2691 a fuzzed object with sh_size not a multiple of sh_entsize. */
2692 erelaend
= erela
+ shdr
->sh_size
- shdr
->sh_entsize
;
2693 irela
= internal_relocs
;
2694 while (erela
<= erelaend
)
2698 (*swap_in
) (abfd
, erela
, irela
);
2699 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2700 if (bed
->s
->arch_size
== 64)
2704 if ((size_t) r_symndx
>= nsyms
)
2707 /* xgettext:c-format */
2708 (_("%pB: bad reloc symbol index (%#" PRIx64
" >= %#lx)"
2709 " for offset %#" PRIx64
" in section `%pA'"),
2710 abfd
, (uint64_t) r_symndx
, (unsigned long) nsyms
,
2711 (uint64_t) irela
->r_offset
, sec
);
2712 bfd_set_error (bfd_error_bad_value
);
2716 else if (r_symndx
!= STN_UNDEF
)
2719 /* xgettext:c-format */
2720 (_("%pB: non-zero symbol index (%#" PRIx64
")"
2721 " for offset %#" PRIx64
" in section `%pA'"
2722 " when the object file has no symbol table"),
2723 abfd
, (uint64_t) r_symndx
,
2724 (uint64_t) irela
->r_offset
, sec
);
2725 bfd_set_error (bfd_error_bad_value
);
2728 irela
+= bed
->s
->int_rels_per_ext_rel
;
2729 erela
+= shdr
->sh_entsize
;
2735 /* Read and swap the relocs for a section O. They may have been
2736 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2737 not NULL, they are used as buffers to read into. They are known to
2738 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2739 the return value is allocated using either malloc or bfd_alloc,
2740 according to the KEEP_MEMORY argument. If O has two relocation
2741 sections (both REL and RELA relocations), then the REL_HDR
2742 relocations will appear first in INTERNAL_RELOCS, followed by the
2743 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2744 update cache_size. */
2747 _bfd_elf_link_info_read_relocs (bfd
*abfd
,
2748 struct bfd_link_info
*info
,
2750 void *external_relocs
,
2751 Elf_Internal_Rela
*internal_relocs
,
2754 void *alloc1
= NULL
;
2755 Elf_Internal_Rela
*alloc2
= NULL
;
2756 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2757 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2758 Elf_Internal_Rela
*internal_rela_relocs
;
2760 if (esdo
->relocs
!= NULL
)
2761 return esdo
->relocs
;
2763 if (o
->reloc_count
== 0)
2766 if (internal_relocs
== NULL
)
2770 size
= (bfd_size_type
) o
->reloc_count
* sizeof (Elf_Internal_Rela
);
2773 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2775 info
->cache_size
+= size
;
2778 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2779 if (internal_relocs
== NULL
)
2783 if (external_relocs
== NULL
)
2785 bfd_size_type size
= 0;
2788 size
+= esdo
->rel
.hdr
->sh_size
;
2790 size
+= esdo
->rela
.hdr
->sh_size
;
2792 alloc1
= bfd_malloc (size
);
2795 external_relocs
= alloc1
;
2798 internal_rela_relocs
= internal_relocs
;
2801 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2805 external_relocs
= (((bfd_byte
*) external_relocs
)
2806 + esdo
->rel
.hdr
->sh_size
);
2807 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2808 * bed
->s
->int_rels_per_ext_rel
);
2812 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2814 internal_rela_relocs
)))
2817 /* Cache the results for next time, if we can. */
2819 esdo
->relocs
= internal_relocs
;
2823 /* Don't free alloc2, since if it was allocated we are passing it
2824 back (under the name of internal_relocs). */
2826 return internal_relocs
;
2833 bfd_release (abfd
, alloc2
);
2840 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2841 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2842 struct bfd_link_info. */
2845 _bfd_elf_link_read_relocs (bfd
*abfd
,
2847 void *external_relocs
,
2848 Elf_Internal_Rela
*internal_relocs
,
2851 return _bfd_elf_link_info_read_relocs (abfd
, NULL
, o
, external_relocs
,
2852 internal_relocs
, keep_memory
);
2856 /* Compute the size of, and allocate space for, REL_HDR which is the
2857 section header for a section containing relocations for O. */
2860 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2861 struct bfd_elf_section_reloc_data
*reldata
)
2863 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2865 /* That allows us to calculate the size of the section. */
2866 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2868 /* The contents field must last into write_object_contents, so we
2869 allocate it with bfd_alloc rather than malloc. Also since we
2870 cannot be sure that the contents will actually be filled in,
2871 we zero the allocated space. */
2872 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2873 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2876 if (reldata
->hashes
== NULL
&& reldata
->count
)
2878 struct elf_link_hash_entry
**p
;
2880 p
= ((struct elf_link_hash_entry
**)
2881 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2885 reldata
->hashes
= p
;
2891 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2892 originated from the section given by INPUT_REL_HDR) to the
2896 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2897 asection
*input_section
,
2898 Elf_Internal_Shdr
*input_rel_hdr
,
2899 Elf_Internal_Rela
*internal_relocs
,
2900 struct elf_link_hash_entry
**rel_hash
2903 Elf_Internal_Rela
*irela
;
2904 Elf_Internal_Rela
*irelaend
;
2906 struct bfd_elf_section_reloc_data
*output_reldata
;
2907 asection
*output_section
;
2908 const struct elf_backend_data
*bed
;
2909 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2910 struct bfd_elf_section_data
*esdo
;
2912 output_section
= input_section
->output_section
;
2914 bed
= get_elf_backend_data (output_bfd
);
2915 esdo
= elf_section_data (output_section
);
2916 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2918 output_reldata
= &esdo
->rel
;
2919 swap_out
= bed
->s
->swap_reloc_out
;
2921 else if (esdo
->rela
.hdr
2922 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2924 output_reldata
= &esdo
->rela
;
2925 swap_out
= bed
->s
->swap_reloca_out
;
2930 /* xgettext:c-format */
2931 (_("%pB: relocation size mismatch in %pB section %pA"),
2932 output_bfd
, input_section
->owner
, input_section
);
2933 bfd_set_error (bfd_error_wrong_format
);
2937 erel
= output_reldata
->hdr
->contents
;
2938 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2939 irela
= internal_relocs
;
2940 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2941 * bed
->s
->int_rels_per_ext_rel
);
2942 while (irela
< irelaend
)
2944 (*swap_out
) (output_bfd
, irela
, erel
);
2945 irela
+= bed
->s
->int_rels_per_ext_rel
;
2946 erel
+= input_rel_hdr
->sh_entsize
;
2949 /* Bump the counter, so that we know where to add the next set of
2951 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2956 /* Make weak undefined symbols in PIE dynamic. */
2959 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2960 struct elf_link_hash_entry
*h
)
2962 if (bfd_link_pie (info
)
2964 && h
->root
.type
== bfd_link_hash_undefweak
)
2965 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2970 /* Fix up the flags for a symbol. This handles various cases which
2971 can only be fixed after all the input files are seen. This is
2972 currently called by both adjust_dynamic_symbol and
2973 assign_sym_version, which is unnecessary but perhaps more robust in
2974 the face of future changes. */
2977 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2978 struct elf_info_failed
*eif
)
2980 const struct elf_backend_data
*bed
;
2982 /* If this symbol was mentioned in a non-ELF file, try to set
2983 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2984 permit a non-ELF file to correctly refer to a symbol defined in
2985 an ELF dynamic object. */
2988 while (h
->root
.type
== bfd_link_hash_indirect
)
2989 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2991 if (h
->root
.type
!= bfd_link_hash_defined
2992 && h
->root
.type
!= bfd_link_hash_defweak
)
2995 h
->ref_regular_nonweak
= 1;
2999 if (h
->root
.u
.def
.section
->owner
!= NULL
3000 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3001 == bfd_target_elf_flavour
))
3004 h
->ref_regular_nonweak
= 1;
3010 if (h
->dynindx
== -1
3014 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3023 /* Unfortunately, NON_ELF is only correct if the symbol
3024 was first seen in a non-ELF file. Fortunately, if the symbol
3025 was first seen in an ELF file, we're probably OK unless the
3026 symbol was defined in a non-ELF file. Catch that case here.
3027 FIXME: We're still in trouble if the symbol was first seen in
3028 a dynamic object, and then later in a non-ELF regular object. */
3029 if ((h
->root
.type
== bfd_link_hash_defined
3030 || h
->root
.type
== bfd_link_hash_defweak
)
3032 && (h
->root
.u
.def
.section
->owner
!= NULL
3033 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3034 != bfd_target_elf_flavour
)
3035 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
3036 && !h
->def_dynamic
)))
3040 /* Backend specific symbol fixup. */
3041 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
3042 if (bed
->elf_backend_fixup_symbol
3043 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
3046 /* If this is a final link, and the symbol was defined as a common
3047 symbol in a regular object file, and there was no definition in
3048 any dynamic object, then the linker will have allocated space for
3049 the symbol in a common section but the DEF_REGULAR
3050 flag will not have been set. */
3051 if (h
->root
.type
== bfd_link_hash_defined
3055 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
3058 /* Symbols defined in discarded sections shouldn't be dynamic. */
3059 if (h
->root
.type
== bfd_link_hash_undefined
&& h
->indx
== -3)
3060 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3062 /* If a weak undefined symbol has non-default visibility, we also
3063 hide it from the dynamic linker. */
3064 else if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
3065 && h
->root
.type
== bfd_link_hash_undefweak
)
3066 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3068 /* A hidden versioned symbol in executable should be forced local if
3069 it is is locally defined, not referenced by shared library and not
3071 else if (bfd_link_executable (eif
->info
)
3072 && h
->versioned
== versioned_hidden
3073 && !eif
->info
->export_dynamic
3077 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3079 /* If -Bsymbolic was used (which means to bind references to global
3080 symbols to the definition within the shared object), and this
3081 symbol was defined in a regular object, then it actually doesn't
3082 need a PLT entry. Likewise, if the symbol has non-default
3083 visibility. If the symbol has hidden or internal visibility, we
3084 will force it local. */
3085 else if (h
->needs_plt
3086 && bfd_link_pic (eif
->info
)
3087 && is_elf_hash_table (eif
->info
->hash
)
3088 && (SYMBOLIC_BIND (eif
->info
, h
)
3089 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
3094 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3095 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
3096 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
3099 /* If this is a weak defined symbol in a dynamic object, and we know
3100 the real definition in the dynamic object, copy interesting flags
3101 over to the real definition. */
3102 if (h
->is_weakalias
)
3104 struct elf_link_hash_entry
*def
= weakdef (h
);
3106 /* If the real definition is defined by a regular object file,
3107 don't do anything special. See the longer description in
3108 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3109 bfd_link_hash_defined as it was when put on the alias list
3110 then it must have originally been a versioned symbol (for
3111 which a non-versioned indirect symbol is created) and later
3112 a definition for the non-versioned symbol is found. In that
3113 case the indirection is flipped with the versioned symbol
3114 becoming an indirect pointing at the non-versioned symbol.
3115 Thus, not an alias any more. */
3116 if (def
->def_regular
3117 || def
->root
.type
!= bfd_link_hash_defined
)
3120 while ((h
= h
->u
.alias
) != def
)
3121 h
->is_weakalias
= 0;
3125 while (h
->root
.type
== bfd_link_hash_indirect
)
3126 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3127 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3128 || h
->root
.type
== bfd_link_hash_defweak
);
3129 BFD_ASSERT (def
->def_dynamic
);
3130 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, def
, h
);
3137 /* Make the backend pick a good value for a dynamic symbol. This is
3138 called via elf_link_hash_traverse, and also calls itself
3142 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
3144 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3145 struct elf_link_hash_table
*htab
;
3146 const struct elf_backend_data
*bed
;
3148 if (! is_elf_hash_table (eif
->info
->hash
))
3151 /* Ignore indirect symbols. These are added by the versioning code. */
3152 if (h
->root
.type
== bfd_link_hash_indirect
)
3155 /* Fix the symbol flags. */
3156 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
3159 htab
= elf_hash_table (eif
->info
);
3160 bed
= get_elf_backend_data (htab
->dynobj
);
3162 if (h
->root
.type
== bfd_link_hash_undefweak
)
3164 if (eif
->info
->dynamic_undefined_weak
== 0)
3165 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3166 else if (eif
->info
->dynamic_undefined_weak
> 0
3168 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3169 && !bfd_hide_sym_by_version (eif
->info
->version_info
,
3170 h
->root
.root
.string
))
3172 if (!bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3180 /* If this symbol does not require a PLT entry, and it is not
3181 defined by a dynamic object, or is not referenced by a regular
3182 object, ignore it. We do have to handle a weak defined symbol,
3183 even if no regular object refers to it, if we decided to add it
3184 to the dynamic symbol table. FIXME: Do we normally need to worry
3185 about symbols which are defined by one dynamic object and
3186 referenced by another one? */
3188 && h
->type
!= STT_GNU_IFUNC
3192 && (!h
->is_weakalias
|| weakdef (h
)->dynindx
== -1))))
3194 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
3198 /* If we've already adjusted this symbol, don't do it again. This
3199 can happen via a recursive call. */
3200 if (h
->dynamic_adjusted
)
3203 /* Don't look at this symbol again. Note that we must set this
3204 after checking the above conditions, because we may look at a
3205 symbol once, decide not to do anything, and then get called
3206 recursively later after REF_REGULAR is set below. */
3207 h
->dynamic_adjusted
= 1;
3209 /* If this is a weak definition, and we know a real definition, and
3210 the real symbol is not itself defined by a regular object file,
3211 then get a good value for the real definition. We handle the
3212 real symbol first, for the convenience of the backend routine.
3214 Note that there is a confusing case here. If the real definition
3215 is defined by a regular object file, we don't get the real symbol
3216 from the dynamic object, but we do get the weak symbol. If the
3217 processor backend uses a COPY reloc, then if some routine in the
3218 dynamic object changes the real symbol, we will not see that
3219 change in the corresponding weak symbol. This is the way other
3220 ELF linkers work as well, and seems to be a result of the shared
3223 I will clarify this issue. Most SVR4 shared libraries define the
3224 variable _timezone and define timezone as a weak synonym. The
3225 tzset call changes _timezone. If you write
3226 extern int timezone;
3228 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3229 you might expect that, since timezone is a synonym for _timezone,
3230 the same number will print both times. However, if the processor
3231 backend uses a COPY reloc, then actually timezone will be copied
3232 into your process image, and, since you define _timezone
3233 yourself, _timezone will not. Thus timezone and _timezone will
3234 wind up at different memory locations. The tzset call will set
3235 _timezone, leaving timezone unchanged. */
3237 if (h
->is_weakalias
)
3239 struct elf_link_hash_entry
*def
= weakdef (h
);
3241 /* If we get to this point, there is an implicit reference to
3242 the alias by a regular object file via the weak symbol H. */
3243 def
->ref_regular
= 1;
3245 /* Ensure that the backend adjust_dynamic_symbol function sees
3246 the strong alias before H by recursively calling ourselves. */
3247 if (!_bfd_elf_adjust_dynamic_symbol (def
, eif
))
3251 /* If a symbol has no type and no size and does not require a PLT
3252 entry, then we are probably about to do the wrong thing here: we
3253 are probably going to create a COPY reloc for an empty object.
3254 This case can arise when a shared object is built with assembly
3255 code, and the assembly code fails to set the symbol type. */
3257 && h
->type
== STT_NOTYPE
3260 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3261 h
->root
.root
.string
);
3263 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3272 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3276 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
3277 struct elf_link_hash_entry
*h
,
3280 unsigned int power_of_two
;
3282 asection
*sec
= h
->root
.u
.def
.section
;
3284 /* The section alignment of the definition is the maximum alignment
3285 requirement of symbols defined in the section. Since we don't
3286 know the symbol alignment requirement, we start with the
3287 maximum alignment and check low bits of the symbol address
3288 for the minimum alignment. */
3289 power_of_two
= bfd_section_alignment (sec
);
3290 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
3291 while ((h
->root
.u
.def
.value
& mask
) != 0)
3297 if (power_of_two
> bfd_section_alignment (dynbss
))
3299 /* Adjust the section alignment if needed. */
3300 if (!bfd_set_section_alignment (dynbss
, power_of_two
))
3304 /* We make sure that the symbol will be aligned properly. */
3305 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
3307 /* Define the symbol as being at this point in DYNBSS. */
3308 h
->root
.u
.def
.section
= dynbss
;
3309 h
->root
.u
.def
.value
= dynbss
->size
;
3311 /* Increment the size of DYNBSS to make room for the symbol. */
3312 dynbss
->size
+= h
->size
;
3314 /* No error if extern_protected_data is true. */
3315 if (h
->protected_def
3316 && (!info
->extern_protected_data
3317 || (info
->extern_protected_data
< 0
3318 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
3319 info
->callbacks
->einfo
3320 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3321 h
->root
.root
.string
);
3326 /* Adjust all external symbols pointing into SEC_MERGE sections
3327 to reflect the object merging within the sections. */
3330 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
3334 if ((h
->root
.type
== bfd_link_hash_defined
3335 || h
->root
.type
== bfd_link_hash_defweak
)
3336 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
3337 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
3339 bfd
*output_bfd
= (bfd
*) data
;
3341 h
->root
.u
.def
.value
=
3342 _bfd_merged_section_offset (output_bfd
,
3343 &h
->root
.u
.def
.section
,
3344 elf_section_data (sec
)->sec_info
,
3345 h
->root
.u
.def
.value
);
3351 /* Returns false if the symbol referred to by H should be considered
3352 to resolve local to the current module, and true if it should be
3353 considered to bind dynamically. */
3356 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
3357 struct bfd_link_info
*info
,
3358 bool not_local_protected
)
3360 bool binding_stays_local_p
;
3361 const struct elf_backend_data
*bed
;
3362 struct elf_link_hash_table
*hash_table
;
3367 while (h
->root
.type
== bfd_link_hash_indirect
3368 || h
->root
.type
== bfd_link_hash_warning
)
3369 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3371 /* If it was forced local, then clearly it's not dynamic. */
3372 if (h
->dynindx
== -1)
3374 if (h
->forced_local
)
3377 /* Identify the cases where name binding rules say that a
3378 visible symbol resolves locally. */
3379 binding_stays_local_p
= (bfd_link_executable (info
)
3380 || SYMBOLIC_BIND (info
, h
));
3382 switch (ELF_ST_VISIBILITY (h
->other
))
3389 hash_table
= elf_hash_table (info
);
3390 if (!is_elf_hash_table (&hash_table
->root
))
3393 bed
= get_elf_backend_data (hash_table
->dynobj
);
3395 /* Proper resolution for function pointer equality may require
3396 that these symbols perhaps be resolved dynamically, even though
3397 we should be resolving them to the current module. */
3398 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
3399 binding_stays_local_p
= true;
3406 /* If it isn't defined locally, then clearly it's dynamic. */
3407 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
3410 /* Otherwise, the symbol is dynamic if binding rules don't tell
3411 us that it remains local. */
3412 return !binding_stays_local_p
;
3415 /* Return true if the symbol referred to by H should be considered
3416 to resolve local to the current module, and false otherwise. Differs
3417 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3418 undefined symbols. The two functions are virtually identical except
3419 for the place where dynindx == -1 is tested. If that test is true,
3420 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3421 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3423 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3424 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3425 treatment of undefined weak symbols. For those that do not make
3426 undefined weak symbols dynamic, both functions may return false. */
3429 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3430 struct bfd_link_info
*info
,
3431 bool local_protected
)
3433 const struct elf_backend_data
*bed
;
3434 struct elf_link_hash_table
*hash_table
;
3436 /* If it's a local sym, of course we resolve locally. */
3440 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3441 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3442 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3445 /* Forced local symbols resolve locally. */
3446 if (h
->forced_local
)
3449 /* Common symbols that become definitions don't get the DEF_REGULAR
3450 flag set, so test it first, and don't bail out. */
3451 if (ELF_COMMON_DEF_P (h
))
3453 /* If we don't have a definition in a regular file, then we can't
3454 resolve locally. The sym is either undefined or dynamic. */
3455 else if (!h
->def_regular
)
3458 /* Non-dynamic symbols resolve locally. */
3459 if (h
->dynindx
== -1)
3462 /* At this point, we know the symbol is defined and dynamic. In an
3463 executable it must resolve locally, likewise when building symbolic
3464 shared libraries. */
3465 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3468 /* Now deal with defined dynamic symbols in shared libraries. Ones
3469 with default visibility might not resolve locally. */
3470 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3473 hash_table
= elf_hash_table (info
);
3474 if (!is_elf_hash_table (&hash_table
->root
))
3477 /* STV_PROTECTED symbols with indirect external access are local. */
3478 if (info
->indirect_extern_access
> 0)
3481 bed
= get_elf_backend_data (hash_table
->dynobj
);
3483 /* If extern_protected_data is false, STV_PROTECTED non-function
3484 symbols are local. */
3485 if ((!info
->extern_protected_data
3486 || (info
->extern_protected_data
< 0
3487 && !bed
->extern_protected_data
))
3488 && !bed
->is_function_type (h
->type
))
3491 /* Function pointer equality tests may require that STV_PROTECTED
3492 symbols be treated as dynamic symbols. If the address of a
3493 function not defined in an executable is set to that function's
3494 plt entry in the executable, then the address of the function in
3495 a shared library must also be the plt entry in the executable. */
3496 return local_protected
;
3499 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3500 aligned. Returns the first TLS output section. */
3502 struct bfd_section
*
3503 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3505 struct bfd_section
*sec
, *tls
;
3506 unsigned int align
= 0;
3508 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3509 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3513 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3514 if (sec
->alignment_power
> align
)
3515 align
= sec
->alignment_power
;
3517 elf_hash_table (info
)->tls_sec
= tls
;
3519 /* Ensure the alignment of the first section (usually .tdata) is the largest
3520 alignment, so that the tls segment starts aligned. */
3522 tls
->alignment_power
= align
;
3527 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3529 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3530 Elf_Internal_Sym
*sym
)
3532 const struct elf_backend_data
*bed
;
3534 /* Local symbols do not count, but target specific ones might. */
3535 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3536 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3539 bed
= get_elf_backend_data (abfd
);
3540 /* Function symbols do not count. */
3541 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3544 /* If the section is undefined, then so is the symbol. */
3545 if (sym
->st_shndx
== SHN_UNDEF
)
3548 /* If the symbol is defined in the common section, then
3549 it is a common definition and so does not count. */
3550 if (bed
->common_definition (sym
))
3553 /* If the symbol is in a target specific section then we
3554 must rely upon the backend to tell us what it is. */
3555 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3556 /* FIXME - this function is not coded yet:
3558 return _bfd_is_global_symbol_definition (abfd, sym);
3560 Instead for now assume that the definition is not global,
3561 Even if this is wrong, at least the linker will behave
3562 in the same way that it used to do. */
3568 /* Search the symbol table of the archive element of the archive ABFD
3569 whose archive map contains a mention of SYMDEF, and determine if
3570 the symbol is defined in this element. */
3572 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3574 Elf_Internal_Shdr
* hdr
;
3578 Elf_Internal_Sym
*isymbuf
;
3579 Elf_Internal_Sym
*isym
;
3580 Elf_Internal_Sym
*isymend
;
3583 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
, NULL
);
3587 if (! bfd_check_format (abfd
, bfd_object
))
3590 /* Select the appropriate symbol table. If we don't know if the
3591 object file is an IR object, give linker LTO plugin a chance to
3592 get the correct symbol table. */
3593 if (abfd
->plugin_format
== bfd_plugin_yes
3594 #if BFD_SUPPORTS_PLUGINS
3595 || (abfd
->plugin_format
== bfd_plugin_unknown
3596 && bfd_link_plugin_object_p (abfd
))
3600 /* Use the IR symbol table if the object has been claimed by
3602 abfd
= abfd
->plugin_dummy_bfd
;
3603 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3607 if (elf_use_dt_symtab_p (abfd
))
3609 bfd_set_error (bfd_error_wrong_format
);
3613 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3614 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3616 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3619 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3621 /* The sh_info field of the symtab header tells us where the
3622 external symbols start. We don't care about the local symbols. */
3623 if (elf_bad_symtab (abfd
))
3625 extsymcount
= symcount
;
3630 extsymcount
= symcount
- hdr
->sh_info
;
3631 extsymoff
= hdr
->sh_info
;
3634 if (extsymcount
== 0)
3637 /* Read in the symbol table. */
3638 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3640 if (isymbuf
== NULL
)
3643 /* Scan the symbol table looking for SYMDEF. */
3645 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3649 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3654 if (strcmp (name
, symdef
->name
) == 0)
3656 result
= is_global_data_symbol_definition (abfd
, isym
);
3666 /* Add an entry to the .dynamic table. */
3669 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3673 struct elf_link_hash_table
*hash_table
;
3674 const struct elf_backend_data
*bed
;
3676 bfd_size_type newsize
;
3677 bfd_byte
*newcontents
;
3678 Elf_Internal_Dyn dyn
;
3680 hash_table
= elf_hash_table (info
);
3681 if (! is_elf_hash_table (&hash_table
->root
))
3684 if (tag
== DT_RELA
|| tag
== DT_REL
)
3685 hash_table
->dynamic_relocs
= true;
3687 bed
= get_elf_backend_data (hash_table
->dynobj
);
3688 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3689 BFD_ASSERT (s
!= NULL
);
3691 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3692 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3693 if (newcontents
== NULL
)
3697 dyn
.d_un
.d_val
= val
;
3698 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3701 s
->contents
= newcontents
;
3706 /* Strip zero-sized dynamic sections. */
3709 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info
*info
)
3711 struct elf_link_hash_table
*hash_table
;
3712 const struct elf_backend_data
*bed
;
3713 asection
*s
, *sdynamic
, **pp
;
3714 asection
*rela_dyn
, *rel_dyn
;
3715 Elf_Internal_Dyn dyn
;
3716 bfd_byte
*extdyn
, *next
;
3717 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
3718 bool strip_zero_sized
;
3719 bool strip_zero_sized_plt
;
3721 if (bfd_link_relocatable (info
))
3724 hash_table
= elf_hash_table (info
);
3725 if (!is_elf_hash_table (&hash_table
->root
))
3728 if (!hash_table
->dynobj
)
3731 sdynamic
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3735 bed
= get_elf_backend_data (hash_table
->dynobj
);
3736 swap_dyn_in
= bed
->s
->swap_dyn_in
;
3738 strip_zero_sized
= false;
3739 strip_zero_sized_plt
= false;
3741 /* Strip zero-sized dynamic sections. */
3742 rela_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rela.dyn");
3743 rel_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rel.dyn");
3744 for (pp
= &info
->output_bfd
->sections
; (s
= *pp
) != NULL
;)
3748 || s
== hash_table
->srelplt
->output_section
3749 || s
== hash_table
->splt
->output_section
))
3752 info
->output_bfd
->section_count
--;
3753 strip_zero_sized
= true;
3758 else if (s
== hash_table
->splt
->output_section
)
3760 s
= hash_table
->splt
;
3761 strip_zero_sized_plt
= true;
3764 s
= hash_table
->srelplt
;
3765 s
->flags
|= SEC_EXCLUDE
;
3766 s
->output_section
= bfd_abs_section_ptr
;
3771 if (strip_zero_sized_plt
&& sdynamic
->size
!= 0)
3772 for (extdyn
= sdynamic
->contents
;
3773 extdyn
< sdynamic
->contents
+ sdynamic
->size
;
3776 next
= extdyn
+ bed
->s
->sizeof_dyn
;
3777 swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3785 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3786 the procedure linkage table (the .plt section) has been
3788 memmove (extdyn
, next
,
3789 sdynamic
->size
- (next
- sdynamic
->contents
));
3794 if (strip_zero_sized
)
3796 /* Regenerate program headers. */
3797 elf_seg_map (info
->output_bfd
) = NULL
;
3798 return _bfd_elf_map_sections_to_segments (info
->output_bfd
, info
,
3805 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3806 1 if a DT_NEEDED tag already exists, and 0 on success. */
3809 bfd_elf_add_dt_needed_tag (bfd
*abfd
, struct bfd_link_info
*info
)
3811 struct elf_link_hash_table
*hash_table
;
3815 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3818 hash_table
= elf_hash_table (info
);
3819 soname
= elf_dt_name (abfd
);
3820 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, false);
3821 if (strindex
== (size_t) -1)
3824 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3827 const struct elf_backend_data
*bed
;
3830 bed
= get_elf_backend_data (hash_table
->dynobj
);
3831 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3832 if (sdyn
!= NULL
&& sdyn
->size
!= 0)
3833 for (extdyn
= sdyn
->contents
;
3834 extdyn
< sdyn
->contents
+ sdyn
->size
;
3835 extdyn
+= bed
->s
->sizeof_dyn
)
3837 Elf_Internal_Dyn dyn
;
3839 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3840 if (dyn
.d_tag
== DT_NEEDED
3841 && dyn
.d_un
.d_val
== strindex
)
3843 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3849 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3852 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3858 /* Return true if SONAME is on the needed list between NEEDED and STOP
3859 (or the end of list if STOP is NULL), and needed by a library that
3863 on_needed_list (const char *soname
,
3864 struct bfd_link_needed_list
*needed
,
3865 struct bfd_link_needed_list
*stop
)
3867 struct bfd_link_needed_list
*look
;
3868 for (look
= needed
; look
!= stop
; look
= look
->next
)
3869 if (strcmp (soname
, look
->name
) == 0
3870 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3871 /* If needed by a library that itself is not directly
3872 needed, recursively check whether that library is
3873 indirectly needed. Since we add DT_NEEDED entries to
3874 the end of the list, library dependencies appear after
3875 the library. Therefore search prior to the current
3876 LOOK, preventing possible infinite recursion. */
3877 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3883 /* Sort symbol by value, section, size, and type. */
3885 elf_sort_symbol (const void *arg1
, const void *arg2
)
3887 const struct elf_link_hash_entry
*h1
;
3888 const struct elf_link_hash_entry
*h2
;
3889 bfd_signed_vma vdiff
;
3894 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3895 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3896 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3898 return vdiff
> 0 ? 1 : -1;
3900 sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3904 /* Sort so that sized symbols are selected over zero size symbols. */
3905 vdiff
= h1
->size
- h2
->size
;
3907 return vdiff
> 0 ? 1 : -1;
3909 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3910 if (h1
->type
!= h2
->type
)
3911 return h1
->type
- h2
->type
;
3913 /* If symbols are properly sized and typed, and multiple strong
3914 aliases are not defined in a shared library by the user we
3915 shouldn't get here. Unfortunately linker script symbols like
3916 __bss_start sometimes match a user symbol defined at the start of
3917 .bss without proper size and type. We'd like to preference the
3918 user symbol over reserved system symbols. Sort on leading
3920 n1
= h1
->root
.root
.string
;
3921 n2
= h2
->root
.root
.string
;
3934 /* Final sort on name selects user symbols like '_u' over reserved
3935 system symbols like '_Z' and also will avoid qsort instability. */
3939 /* This function is used to adjust offsets into .dynstr for
3940 dynamic symbols. This is called via elf_link_hash_traverse. */
3943 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3945 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3947 if (h
->dynindx
!= -1)
3948 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3952 /* Assign string offsets in .dynstr, update all structures referencing
3956 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3958 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3959 struct elf_link_local_dynamic_entry
*entry
;
3960 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3961 bfd
*dynobj
= hash_table
->dynobj
;
3964 const struct elf_backend_data
*bed
;
3967 _bfd_elf_strtab_finalize (dynstr
);
3968 size
= _bfd_elf_strtab_size (dynstr
);
3970 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3972 if (info
->callbacks
->examine_strtab
)
3973 info
->callbacks
->examine_strtab (dynstr
);
3975 bed
= get_elf_backend_data (dynobj
);
3976 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3977 BFD_ASSERT (sdyn
!= NULL
);
3979 /* Update all .dynamic entries referencing .dynstr strings. */
3980 for (extdyn
= sdyn
->contents
;
3981 extdyn
< PTR_ADD (sdyn
->contents
, sdyn
->size
);
3982 extdyn
+= bed
->s
->sizeof_dyn
)
3984 Elf_Internal_Dyn dyn
;
3986 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3990 dyn
.d_un
.d_val
= size
;
4000 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
4005 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
4008 /* Now update local dynamic symbols. */
4009 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
4010 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
4011 entry
->isym
.st_name
);
4013 /* And the rest of dynamic symbols. */
4014 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
4016 /* Adjust version definitions. */
4017 if (elf_tdata (output_bfd
)->cverdefs
)
4022 Elf_Internal_Verdef def
;
4023 Elf_Internal_Verdaux defaux
;
4025 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
4029 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
4031 p
+= sizeof (Elf_External_Verdef
);
4032 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
4034 for (i
= 0; i
< def
.vd_cnt
; ++i
)
4036 _bfd_elf_swap_verdaux_in (output_bfd
,
4037 (Elf_External_Verdaux
*) p
, &defaux
);
4038 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
4040 _bfd_elf_swap_verdaux_out (output_bfd
,
4041 &defaux
, (Elf_External_Verdaux
*) p
);
4042 p
+= sizeof (Elf_External_Verdaux
);
4045 while (def
.vd_next
);
4048 /* Adjust version references. */
4049 if (elf_tdata (output_bfd
)->verref
)
4054 Elf_Internal_Verneed need
;
4055 Elf_Internal_Vernaux needaux
;
4057 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
4061 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
4063 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
4064 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
4065 (Elf_External_Verneed
*) p
);
4066 p
+= sizeof (Elf_External_Verneed
);
4067 for (i
= 0; i
< need
.vn_cnt
; ++i
)
4069 _bfd_elf_swap_vernaux_in (output_bfd
,
4070 (Elf_External_Vernaux
*) p
, &needaux
);
4071 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
4073 _bfd_elf_swap_vernaux_out (output_bfd
,
4075 (Elf_External_Vernaux
*) p
);
4076 p
+= sizeof (Elf_External_Vernaux
);
4079 while (need
.vn_next
);
4085 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4086 The default is to only match when the INPUT and OUTPUT are exactly
4090 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
4091 const bfd_target
*output
)
4093 return input
== output
;
4096 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4097 This version is used when different targets for the same architecture
4098 are virtually identical. */
4101 _bfd_elf_relocs_compatible (const bfd_target
*input
,
4102 const bfd_target
*output
)
4104 const struct elf_backend_data
*obed
, *ibed
;
4106 if (input
== output
)
4109 ibed
= xvec_get_elf_backend_data (input
);
4110 obed
= xvec_get_elf_backend_data (output
);
4112 if (ibed
->arch
!= obed
->arch
)
4115 /* If both backends are using this function, deem them compatible. */
4116 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
4119 /* Make a special call to the linker "notice" function to tell it that
4120 we are about to handle an as-needed lib, or have finished
4121 processing the lib. */
4124 _bfd_elf_notice_as_needed (bfd
*ibfd
,
4125 struct bfd_link_info
*info
,
4126 enum notice_asneeded_action act
)
4128 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
4131 /* Call ACTION on each relocation in an ELF object file. */
4134 _bfd_elf_link_iterate_on_relocs
4135 (bfd
*abfd
, struct bfd_link_info
*info
,
4136 bool (*action
) (bfd
*, struct bfd_link_info
*, asection
*,
4137 const Elf_Internal_Rela
*))
4139 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4140 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
4142 /* If this object is the same format as the output object, and it is
4143 not a shared library, then let the backend look through the
4146 This is required to build global offset table entries and to
4147 arrange for dynamic relocs. It is not required for the
4148 particular common case of linking non PIC code, even when linking
4149 against shared libraries, but unfortunately there is no way of
4150 knowing whether an object file has been compiled PIC or not.
4151 Looking through the relocs is not particularly time consuming.
4152 The problem is that we must either (1) keep the relocs in memory,
4153 which causes the linker to require additional runtime memory or
4154 (2) read the relocs twice from the input file, which wastes time.
4155 This would be a good case for using mmap.
4157 I have no idea how to handle linking PIC code into a file of a
4158 different format. It probably can't be done. */
4159 if ((abfd
->flags
& DYNAMIC
) == 0
4160 && is_elf_hash_table (&htab
->root
)
4161 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
4162 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
4166 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4168 Elf_Internal_Rela
*internal_relocs
;
4171 /* Don't check relocations in excluded sections. Don't do
4172 anything special with non-loaded, non-alloced sections.
4173 In particular, any relocs in such sections should not
4174 affect GOT and PLT reference counting (ie. we don't
4175 allow them to create GOT or PLT entries), there's no
4176 possibility or desire to optimize TLS relocs, and
4177 there's not much point in propagating relocs to shared
4178 libs that the dynamic linker won't relocate. */
4179 if ((o
->flags
& SEC_ALLOC
) == 0
4180 || (o
->flags
& SEC_RELOC
) == 0
4181 || (o
->flags
& SEC_EXCLUDE
) != 0
4182 || o
->reloc_count
== 0
4183 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4184 && (o
->flags
& SEC_DEBUGGING
) != 0)
4185 || bfd_is_abs_section (o
->output_section
))
4188 internal_relocs
= _bfd_elf_link_info_read_relocs (abfd
, info
,
4191 _bfd_link_keep_memory (info
));
4192 if (internal_relocs
== NULL
)
4195 ok
= action (abfd
, info
, o
, internal_relocs
);
4197 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4198 free (internal_relocs
);
4208 /* Check relocations in an ELF object file. This is called after
4209 all input files have been opened. */
4212 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
4214 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4215 if (bed
->check_relocs
!= NULL
)
4216 return _bfd_elf_link_iterate_on_relocs (abfd
, info
,
4221 /* Add symbols from an ELF object file to the linker hash table. */
4224 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4226 Elf_Internal_Ehdr
*ehdr
;
4227 Elf_Internal_Shdr
*hdr
;
4231 struct elf_link_hash_entry
**sym_hash
;
4233 Elf_External_Versym
*extversym
= NULL
;
4234 Elf_External_Versym
*extversym_end
= NULL
;
4235 Elf_External_Versym
*ever
;
4236 struct elf_link_hash_entry
*weaks
;
4237 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
4238 size_t nondeflt_vers_cnt
= 0;
4239 Elf_Internal_Sym
*isymbuf
= NULL
;
4240 Elf_Internal_Sym
*isym
;
4241 Elf_Internal_Sym
*isymend
;
4242 const struct elf_backend_data
*bed
;
4244 struct elf_link_hash_table
*htab
;
4245 void *alloc_mark
= NULL
;
4246 struct bfd_hash_entry
**old_table
= NULL
;
4247 unsigned int old_size
= 0;
4248 unsigned int old_count
= 0;
4249 void *old_tab
= NULL
;
4251 struct bfd_link_hash_entry
*old_undefs
= NULL
;
4252 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
4253 void *old_strtab
= NULL
;
4258 htab
= elf_hash_table (info
);
4259 bed
= get_elf_backend_data (abfd
);
4261 if (elf_use_dt_symtab_p (abfd
))
4263 bfd_set_error (bfd_error_wrong_format
);
4267 if ((abfd
->flags
& DYNAMIC
) == 0)
4273 /* You can't use -r against a dynamic object. Also, there's no
4274 hope of using a dynamic object which does not exactly match
4275 the format of the output file. */
4276 if (bfd_link_relocatable (info
)
4277 || !is_elf_hash_table (&htab
->root
)
4278 || info
->output_bfd
->xvec
!= abfd
->xvec
)
4280 if (bfd_link_relocatable (info
))
4281 bfd_set_error (bfd_error_invalid_operation
);
4283 bfd_set_error (bfd_error_wrong_format
);
4288 ehdr
= elf_elfheader (abfd
);
4289 if (info
->warn_alternate_em
4290 && bed
->elf_machine_code
!= ehdr
->e_machine
4291 && ((bed
->elf_machine_alt1
!= 0
4292 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
4293 || (bed
->elf_machine_alt2
!= 0
4294 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
4296 /* xgettext:c-format */
4297 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4298 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
4300 /* As a GNU extension, any input sections which are named
4301 .gnu.warning.SYMBOL are treated as warning symbols for the given
4302 symbol. This differs from .gnu.warning sections, which generate
4303 warnings when they are included in an output file. */
4304 /* PR 12761: Also generate this warning when building shared libraries. */
4305 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4309 name
= bfd_section_name (s
);
4310 if (startswith (name
, ".gnu.warning."))
4315 name
+= sizeof ".gnu.warning." - 1;
4317 /* If this is a shared object, then look up the symbol
4318 in the hash table. If it is there, and it is already
4319 been defined, then we will not be using the entry
4320 from this shared object, so we don't need to warn.
4321 FIXME: If we see the definition in a regular object
4322 later on, we will warn, but we shouldn't. The only
4323 fix is to keep track of what warnings we are supposed
4324 to emit, and then handle them all at the end of the
4328 struct elf_link_hash_entry
*h
;
4330 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
4332 /* FIXME: What about bfd_link_hash_common? */
4334 && (h
->root
.type
== bfd_link_hash_defined
4335 || h
->root
.type
== bfd_link_hash_defweak
))
4340 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
4344 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
4349 if (! (_bfd_generic_link_add_one_symbol
4350 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
4351 false, bed
->collect
, NULL
)))
4354 if (bfd_link_executable (info
))
4356 /* Clobber the section size so that the warning does
4357 not get copied into the output file. */
4360 /* Also set SEC_EXCLUDE, so that symbols defined in
4361 the warning section don't get copied to the output. */
4362 s
->flags
|= SEC_EXCLUDE
;
4367 just_syms
= ((s
= abfd
->sections
) != NULL
4368 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
4373 /* If we are creating a shared library, create all the dynamic
4374 sections immediately. We need to attach them to something,
4375 so we attach them to this BFD, provided it is the right
4376 format and is not from ld --just-symbols. Always create the
4377 dynamic sections for -E/--dynamic-list. FIXME: If there
4378 are no input BFD's of the same format as the output, we can't
4379 make a shared library. */
4381 && (bfd_link_pic (info
)
4382 || (!bfd_link_relocatable (info
)
4384 && (info
->export_dynamic
|| info
->dynamic
)))
4385 && is_elf_hash_table (&htab
->root
)
4386 && info
->output_bfd
->xvec
== abfd
->xvec
4387 && !htab
->dynamic_sections_created
)
4389 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
4393 else if (!is_elf_hash_table (&htab
->root
))
4397 const char *soname
= NULL
;
4399 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
4400 const Elf_Internal_Phdr
*phdr
;
4401 struct elf_link_loaded_list
*loaded_lib
;
4403 /* ld --just-symbols and dynamic objects don't mix very well.
4404 ld shouldn't allow it. */
4408 /* If this dynamic lib was specified on the command line with
4409 --as-needed in effect, then we don't want to add a DT_NEEDED
4410 tag unless the lib is actually used. Similary for libs brought
4411 in by another lib's DT_NEEDED. When --no-add-needed is used
4412 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4413 any dynamic library in DT_NEEDED tags in the dynamic lib at
4415 add_needed
= (elf_dyn_lib_class (abfd
)
4416 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
4417 | DYN_NO_NEEDED
)) == 0;
4419 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4420 if (s
!= NULL
&& s
->size
!= 0 && (s
->flags
& SEC_HAS_CONTENTS
) != 0)
4424 unsigned int elfsec
;
4425 unsigned long shlink
;
4427 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
4434 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
4435 if (elfsec
== SHN_BAD
)
4436 goto error_free_dyn
;
4437 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
4439 for (extdyn
= dynbuf
;
4440 (size_t) (dynbuf
+ s
->size
- extdyn
) >= bed
->s
->sizeof_dyn
;
4441 extdyn
+= bed
->s
->sizeof_dyn
)
4443 Elf_Internal_Dyn dyn
;
4445 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
4446 if (dyn
.d_tag
== DT_SONAME
)
4448 unsigned int tagv
= dyn
.d_un
.d_val
;
4449 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4451 goto error_free_dyn
;
4453 if (dyn
.d_tag
== DT_NEEDED
)
4455 struct bfd_link_needed_list
*n
, **pn
;
4457 unsigned int tagv
= dyn
.d_un
.d_val
;
4458 size_t amt
= sizeof (struct bfd_link_needed_list
);
4460 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4461 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4462 if (n
== NULL
|| fnm
== NULL
)
4463 goto error_free_dyn
;
4464 amt
= strlen (fnm
) + 1;
4465 anm
= (char *) bfd_alloc (abfd
, amt
);
4467 goto error_free_dyn
;
4468 memcpy (anm
, fnm
, amt
);
4472 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4476 if (dyn
.d_tag
== DT_RUNPATH
)
4478 struct bfd_link_needed_list
*n
, **pn
;
4480 unsigned int tagv
= dyn
.d_un
.d_val
;
4481 size_t amt
= sizeof (struct bfd_link_needed_list
);
4483 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4484 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4485 if (n
== NULL
|| fnm
== NULL
)
4486 goto error_free_dyn
;
4487 amt
= strlen (fnm
) + 1;
4488 anm
= (char *) bfd_alloc (abfd
, amt
);
4490 goto error_free_dyn
;
4491 memcpy (anm
, fnm
, amt
);
4495 for (pn
= & runpath
;
4501 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4502 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
4504 struct bfd_link_needed_list
*n
, **pn
;
4506 unsigned int tagv
= dyn
.d_un
.d_val
;
4507 size_t amt
= sizeof (struct bfd_link_needed_list
);
4509 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4510 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4511 if (n
== NULL
|| fnm
== NULL
)
4512 goto error_free_dyn
;
4513 amt
= strlen (fnm
) + 1;
4514 anm
= (char *) bfd_alloc (abfd
, amt
);
4516 goto error_free_dyn
;
4517 memcpy (anm
, fnm
, amt
);
4527 if (dyn
.d_tag
== DT_AUDIT
)
4529 unsigned int tagv
= dyn
.d_un
.d_val
;
4530 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4532 if (dyn
.d_tag
== DT_FLAGS_1
)
4533 elf_tdata (abfd
)->is_pie
= (dyn
.d_un
.d_val
& DF_1_PIE
) != 0;
4539 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4540 frees all more recently bfd_alloc'd blocks as well. */
4546 struct bfd_link_needed_list
**pn
;
4547 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4552 /* If we have a PT_GNU_RELRO program header, mark as read-only
4553 all sections contained fully therein. This makes relro
4554 shared library sections appear as they will at run-time. */
4555 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4556 while (phdr
-- > elf_tdata (abfd
)->phdr
)
4557 if (phdr
->p_type
== PT_GNU_RELRO
)
4559 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4561 unsigned int opb
= bfd_octets_per_byte (abfd
, s
);
4563 if ((s
->flags
& SEC_ALLOC
) != 0
4564 && s
->vma
* opb
>= phdr
->p_vaddr
4565 && s
->vma
* opb
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4566 s
->flags
|= SEC_READONLY
;
4571 /* We do not want to include any of the sections in a dynamic
4572 object in the output file. We hack by simply clobbering the
4573 list of sections in the BFD. This could be handled more
4574 cleanly by, say, a new section flag; the existing
4575 SEC_NEVER_LOAD flag is not the one we want, because that one
4576 still implies that the section takes up space in the output
4578 bfd_section_list_clear (abfd
);
4580 /* Find the name to use in a DT_NEEDED entry that refers to this
4581 object. If the object has a DT_SONAME entry, we use it.
4582 Otherwise, if the generic linker stuck something in
4583 elf_dt_name, we use that. Otherwise, we just use the file
4585 if (soname
== NULL
|| *soname
== '\0')
4587 soname
= elf_dt_name (abfd
);
4588 if (soname
== NULL
|| *soname
== '\0')
4589 soname
= bfd_get_filename (abfd
);
4592 /* Save the SONAME because sometimes the linker emulation code
4593 will need to know it. */
4594 elf_dt_name (abfd
) = soname
;
4596 /* If we have already included this dynamic object in the
4597 link, just ignore it. There is no reason to include a
4598 particular dynamic object more than once. */
4599 for (loaded_lib
= htab
->dyn_loaded
;
4601 loaded_lib
= loaded_lib
->next
)
4603 if (strcmp (elf_dt_name (loaded_lib
->abfd
), soname
) == 0)
4607 /* Create dynamic sections for backends that require that be done
4608 before setup_gnu_properties. */
4610 && !_bfd_elf_link_create_dynamic_sections (abfd
, info
))
4613 /* Save the DT_AUDIT entry for the linker emulation code. */
4614 elf_dt_audit (abfd
) = audit
;
4617 /* If this is a dynamic object, we always link against the .dynsym
4618 symbol table, not the .symtab symbol table. The dynamic linker
4619 will only see the .dynsym symbol table, so there is no reason to
4620 look at .symtab for a dynamic object. */
4622 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4623 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4625 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4627 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4629 /* The sh_info field of the symtab header tells us where the
4630 external symbols start. We don't care about the local symbols at
4632 if (elf_bad_symtab (abfd
))
4634 extsymcount
= symcount
;
4639 extsymcount
= symcount
- hdr
->sh_info
;
4640 extsymoff
= hdr
->sh_info
;
4643 sym_hash
= elf_sym_hashes (abfd
);
4644 if (extsymcount
!= 0)
4646 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4648 if (isymbuf
== NULL
)
4651 if (sym_hash
== NULL
)
4653 /* We store a pointer to the hash table entry for each
4655 size_t amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4656 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4657 if (sym_hash
== NULL
)
4658 goto error_free_sym
;
4659 elf_sym_hashes (abfd
) = sym_hash
;
4665 /* Read in any version definitions. */
4666 if (!_bfd_elf_slurp_version_tables (abfd
,
4667 info
->default_imported_symver
))
4668 goto error_free_sym
;
4670 /* Read in the symbol versions, but don't bother to convert them
4671 to internal format. */
4672 if (elf_dynversym (abfd
) != 0)
4674 Elf_Internal_Shdr
*versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4675 bfd_size_type amt
= versymhdr
->sh_size
;
4677 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0)
4678 goto error_free_sym
;
4679 extversym
= (Elf_External_Versym
*)
4680 _bfd_malloc_and_read (abfd
, amt
, amt
);
4681 if (extversym
== NULL
)
4682 goto error_free_sym
;
4683 extversym_end
= extversym
+ amt
/ sizeof (*extversym
);
4687 /* If we are loading an as-needed shared lib, save the symbol table
4688 state before we start adding symbols. If the lib turns out
4689 to be unneeded, restore the state. */
4690 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4695 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4697 struct bfd_hash_entry
*p
;
4698 struct elf_link_hash_entry
*h
;
4700 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4702 h
= (struct elf_link_hash_entry
*) p
;
4703 entsize
+= htab
->root
.table
.entsize
;
4704 if (h
->root
.type
== bfd_link_hash_warning
)
4706 entsize
+= htab
->root
.table
.entsize
;
4707 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4709 if (h
->root
.type
== bfd_link_hash_common
)
4710 entsize
+= sizeof (*h
->root
.u
.c
.p
);
4714 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4715 old_tab
= bfd_malloc (tabsize
+ entsize
);
4716 if (old_tab
== NULL
)
4717 goto error_free_vers
;
4719 /* Remember the current objalloc pointer, so that all mem for
4720 symbols added can later be reclaimed. */
4721 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4722 if (alloc_mark
== NULL
)
4723 goto error_free_vers
;
4725 /* Make a special call to the linker "notice" function to
4726 tell it that we are about to handle an as-needed lib. */
4727 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4728 goto error_free_vers
;
4730 /* Clone the symbol table. Remember some pointers into the
4731 symbol table, and dynamic symbol count. */
4732 old_ent
= (char *) old_tab
+ tabsize
;
4733 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4734 old_undefs
= htab
->root
.undefs
;
4735 old_undefs_tail
= htab
->root
.undefs_tail
;
4736 old_table
= htab
->root
.table
.table
;
4737 old_size
= htab
->root
.table
.size
;
4738 old_count
= htab
->root
.table
.count
;
4740 if (htab
->dynstr
!= NULL
)
4742 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4743 if (old_strtab
== NULL
)
4744 goto error_free_vers
;
4747 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4749 struct bfd_hash_entry
*p
;
4750 struct elf_link_hash_entry
*h
;
4752 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4754 h
= (struct elf_link_hash_entry
*) p
;
4755 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4756 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4757 if (h
->root
.type
== bfd_link_hash_warning
)
4759 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4760 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4761 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4763 if (h
->root
.type
== bfd_link_hash_common
)
4765 memcpy (old_ent
, h
->root
.u
.c
.p
, sizeof (*h
->root
.u
.c
.p
));
4766 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
4773 if (extversym
== NULL
)
4775 else if (extversym
+ extsymoff
< extversym_end
)
4776 ever
= extversym
+ extsymoff
;
4779 /* xgettext:c-format */
4780 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4781 abfd
, (long) extsymoff
,
4782 (long) (extversym_end
- extversym
) / sizeof (* extversym
));
4783 bfd_set_error (bfd_error_bad_value
);
4784 goto error_free_vers
;
4787 if (!bfd_link_relocatable (info
)
4788 && bfd_get_lto_type (abfd
) == lto_slim_ir_object
)
4791 (_("%pB: plugin needed to handle lto object"), abfd
);
4794 for (isym
= isymbuf
, isymend
= PTR_ADD (isymbuf
, extsymcount
);
4796 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4800 asection
*sec
, *new_sec
;
4803 struct elf_link_hash_entry
*h
;
4804 struct elf_link_hash_entry
*hi
;
4806 bool size_change_ok
;
4807 bool type_change_ok
;
4813 unsigned int old_alignment
;
4814 unsigned int shindex
;
4820 flags
= BSF_NO_FLAGS
;
4822 value
= isym
->st_value
;
4823 common
= bed
->common_definition (isym
);
4824 if (common
&& info
->inhibit_common_definition
)
4826 /* Treat common symbol as undefined for --no-define-common. */
4827 isym
->st_shndx
= SHN_UNDEF
;
4832 bind
= ELF_ST_BIND (isym
->st_info
);
4836 /* This should be impossible, since ELF requires that all
4837 global symbols follow all local symbols, and that sh_info
4838 point to the first global symbol. Unfortunately, Irix 5
4840 if (elf_bad_symtab (abfd
))
4843 /* If we aren't prepared to handle locals within the globals
4844 then we'll likely segfault on a NULL symbol hash if the
4845 symbol is ever referenced in relocations. */
4846 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
4847 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, hdr
->sh_name
);
4848 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4849 " (>= sh_info of %lu)"),
4850 abfd
, name
, (long) (isym
- isymbuf
+ extsymoff
),
4853 /* Dynamic object relocations are not processed by ld, so
4854 ld won't run into the problem mentioned above. */
4857 bfd_set_error (bfd_error_bad_value
);
4858 goto error_free_vers
;
4861 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4869 case STB_GNU_UNIQUE
:
4870 flags
= BSF_GNU_UNIQUE
;
4874 /* Leave it up to the processor backend. */
4878 if (isym
->st_shndx
== SHN_UNDEF
)
4879 sec
= bfd_und_section_ptr
;
4880 else if (isym
->st_shndx
== SHN_ABS
)
4881 sec
= bfd_abs_section_ptr
;
4882 else if (isym
->st_shndx
== SHN_COMMON
)
4884 sec
= bfd_com_section_ptr
;
4885 /* What ELF calls the size we call the value. What ELF
4886 calls the value we call the alignment. */
4887 value
= isym
->st_size
;
4891 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4893 sec
= bfd_abs_section_ptr
;
4894 else if (discarded_section (sec
))
4896 /* Symbols from discarded section are undefined. We keep
4898 sec
= bfd_und_section_ptr
;
4900 isym
->st_shndx
= SHN_UNDEF
;
4902 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4906 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4909 goto error_free_vers
;
4911 if (isym
->st_shndx
== SHN_COMMON
4912 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4914 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4918 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4920 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4922 goto error_free_vers
;
4926 else if (isym
->st_shndx
== SHN_COMMON
4927 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4928 && !bfd_link_relocatable (info
))
4930 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4934 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4935 | SEC_LINKER_CREATED
);
4936 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4938 goto error_free_vers
;
4942 else if (bed
->elf_add_symbol_hook
)
4944 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4946 goto error_free_vers
;
4948 /* The hook function sets the name to NULL if this symbol
4949 should be skipped for some reason. */
4954 /* Sanity check that all possibilities were handled. */
4958 /* Silently discard TLS symbols from --just-syms. There's
4959 no way to combine a static TLS block with a new TLS block
4960 for this executable. */
4961 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4962 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4965 if (bfd_is_und_section (sec
)
4966 || bfd_is_com_section (sec
))
4971 size_change_ok
= false;
4972 type_change_ok
= bed
->type_change_ok
;
4979 if (is_elf_hash_table (&htab
->root
))
4981 Elf_Internal_Versym iver
;
4982 unsigned int vernum
= 0;
4987 if (info
->default_imported_symver
)
4988 /* Use the default symbol version created earlier. */
4989 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4993 else if (ever
>= extversym_end
)
4995 /* xgettext:c-format */
4996 _bfd_error_handler (_("%pB: not enough version information"),
4998 bfd_set_error (bfd_error_bad_value
);
4999 goto error_free_vers
;
5002 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
5004 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
5006 /* If this is a hidden symbol, or if it is not version
5007 1, we append the version name to the symbol name.
5008 However, we do not modify a non-hidden absolute symbol
5009 if it is not a function, because it might be the version
5010 symbol itself. FIXME: What if it isn't? */
5011 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
5013 && (!bfd_is_abs_section (sec
)
5014 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
5017 size_t namelen
, verlen
, newlen
;
5020 if (isym
->st_shndx
!= SHN_UNDEF
)
5022 if (vernum
> elf_tdata (abfd
)->cverdefs
)
5024 else if (vernum
> 1)
5026 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
5033 /* xgettext:c-format */
5034 (_("%pB: %s: invalid version %u (max %d)"),
5036 elf_tdata (abfd
)->cverdefs
);
5037 bfd_set_error (bfd_error_bad_value
);
5038 goto error_free_vers
;
5043 /* We cannot simply test for the number of
5044 entries in the VERNEED section since the
5045 numbers for the needed versions do not start
5047 Elf_Internal_Verneed
*t
;
5050 for (t
= elf_tdata (abfd
)->verref
;
5054 Elf_Internal_Vernaux
*a
;
5056 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5058 if (a
->vna_other
== vernum
)
5060 verstr
= a
->vna_nodename
;
5070 /* xgettext:c-format */
5071 (_("%pB: %s: invalid needed version %d"),
5072 abfd
, name
, vernum
);
5073 bfd_set_error (bfd_error_bad_value
);
5074 goto error_free_vers
;
5078 namelen
= strlen (name
);
5079 verlen
= strlen (verstr
);
5080 newlen
= namelen
+ verlen
+ 2;
5081 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
5082 && isym
->st_shndx
!= SHN_UNDEF
)
5085 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
5086 if (newname
== NULL
)
5087 goto error_free_vers
;
5088 memcpy (newname
, name
, namelen
);
5089 p
= newname
+ namelen
;
5091 /* If this is a defined non-hidden version symbol,
5092 we add another @ to the name. This indicates the
5093 default version of the symbol. */
5094 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
5095 && isym
->st_shndx
!= SHN_UNDEF
)
5097 memcpy (p
, verstr
, verlen
+ 1);
5102 /* If this symbol has default visibility and the user has
5103 requested we not re-export it, then mark it as hidden. */
5104 if (!bfd_is_und_section (sec
)
5107 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
5108 isym
->st_other
= (STV_HIDDEN
5109 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
5111 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
5112 sym_hash
, &old_bfd
, &old_weak
,
5113 &old_alignment
, &skip
, &override
,
5114 &type_change_ok
, &size_change_ok
,
5116 goto error_free_vers
;
5121 /* Override a definition only if the new symbol matches the
5123 if (override
&& matched
)
5127 while (h
->root
.type
== bfd_link_hash_indirect
5128 || h
->root
.type
== bfd_link_hash_warning
)
5129 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5131 if (h
->versioned
!= unversioned
5132 && elf_tdata (abfd
)->verdef
!= NULL
5135 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
5138 if (! (_bfd_generic_link_add_one_symbol
5139 (info
, override
? override
: abfd
, name
, flags
, sec
, value
,
5140 NULL
, false, bed
->collect
,
5141 (struct bfd_link_hash_entry
**) sym_hash
)))
5142 goto error_free_vers
;
5145 /* We need to make sure that indirect symbol dynamic flags are
5148 while (h
->root
.type
== bfd_link_hash_indirect
5149 || h
->root
.type
== bfd_link_hash_warning
)
5150 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5154 /* Setting the index to -3 tells elf_link_output_extsym that
5155 this symbol is defined in a discarded section. */
5156 if (discarded
&& is_elf_hash_table (&htab
->root
))
5159 new_weak
= (flags
& BSF_WEAK
) != 0;
5163 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
5164 && is_elf_hash_table (&htab
->root
)
5165 && h
->u
.alias
== NULL
)
5167 /* Keep a list of all weak defined non function symbols from
5168 a dynamic object, using the alias field. Later in this
5169 function we will set the alias field to the correct
5170 value. We only put non-function symbols from dynamic
5171 objects on this list, because that happens to be the only
5172 time we need to know the normal symbol corresponding to a
5173 weak symbol, and the information is time consuming to
5174 figure out. If the alias field is not already NULL,
5175 then this symbol was already defined by some previous
5176 dynamic object, and we will be using that previous
5177 definition anyhow. */
5183 /* Set the alignment of a common symbol. */
5184 if ((common
|| bfd_is_com_section (sec
))
5185 && h
->root
.type
== bfd_link_hash_common
)
5190 align
= bfd_log2 (isym
->st_value
);
5193 /* The new symbol is a common symbol in a shared object.
5194 We need to get the alignment from the section. */
5195 align
= new_sec
->alignment_power
;
5197 if (align
> old_alignment
)
5198 h
->root
.u
.c
.p
->alignment_power
= align
;
5200 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
5203 if (is_elf_hash_table (&htab
->root
))
5205 /* Set a flag in the hash table entry indicating the type of
5206 reference or definition we just found. A dynamic symbol
5207 is one which is referenced or defined by both a regular
5208 object and a shared object. */
5209 bool dynsym
= false;
5211 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5212 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5214 /* Except for this flag to track nonweak references. */
5216 && bind
!= STB_WEAK
)
5217 h
->ref_ir_nonweak
= 1;
5224 if (bind
!= STB_WEAK
)
5225 h
->ref_regular_nonweak
= 1;
5242 hi
->ref_dynamic
= 1;
5247 hi
->def_dynamic
= 1;
5251 /* If an indirect symbol has been forced local, don't
5252 make the real symbol dynamic. */
5253 if (h
!= hi
&& hi
->forced_local
)
5257 if (bfd_link_dll (info
)
5267 && weakdef (h
)->dynindx
!= -1))
5271 /* Check to see if we need to add an indirect symbol for
5272 the default name. */
5274 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
5276 && hi
->versioned
== versioned_hidden
))
5277 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
5278 sec
, value
, &old_bfd
, &dynsym
))
5279 goto error_free_vers
;
5281 /* Check the alignment when a common symbol is involved. This
5282 can change when a common symbol is overridden by a normal
5283 definition or a common symbol is ignored due to the old
5284 normal definition. We need to make sure the maximum
5285 alignment is maintained. */
5286 if ((old_alignment
|| common
)
5287 && h
->root
.type
!= bfd_link_hash_common
)
5289 unsigned int common_align
;
5290 unsigned int normal_align
;
5291 unsigned int symbol_align
;
5295 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
5296 || h
->root
.type
== bfd_link_hash_defweak
);
5298 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
5299 if (h
->root
.u
.def
.section
->owner
!= NULL
5300 && (h
->root
.u
.def
.section
->owner
->flags
5301 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
5303 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
5304 if (normal_align
> symbol_align
)
5305 normal_align
= symbol_align
;
5308 normal_align
= symbol_align
;
5312 common_align
= old_alignment
;
5313 common_bfd
= old_bfd
;
5318 common_align
= bfd_log2 (isym
->st_value
);
5320 normal_bfd
= old_bfd
;
5323 if (normal_align
< common_align
)
5325 /* PR binutils/2735 */
5326 if (normal_bfd
== NULL
)
5328 /* xgettext:c-format */
5329 (_("warning: alignment %u of common symbol `%s' in %pB is"
5330 " greater than the alignment (%u) of its section %pA"),
5331 1 << common_align
, name
, common_bfd
,
5332 1 << normal_align
, h
->root
.u
.def
.section
);
5335 /* xgettext:c-format */
5336 (_("warning: alignment %u of normal symbol `%s' in %pB"
5337 " is smaller than %u used by the common definition in %pB"),
5338 1 << normal_align
, name
, normal_bfd
,
5339 1 << common_align
, common_bfd
);
5341 /* PR 30499: make sure that users understand that this warning is serious. */
5343 (_("warning: NOTE: alignment discrepancies can cause real problems. Investigation is advised."));
5347 /* Remember the symbol size if it isn't undefined. */
5348 if (isym
->st_size
!= 0
5349 && isym
->st_shndx
!= SHN_UNDEF
5350 && (definition
|| h
->size
== 0))
5353 && h
->size
!= isym
->st_size
5354 && ! size_change_ok
)
5357 /* xgettext:c-format */
5358 (_("warning: size of symbol `%s' changed"
5359 " from %" PRIu64
" in %pB to %" PRIu64
" in %pB"),
5360 name
, (uint64_t) h
->size
, old_bfd
,
5361 (uint64_t) isym
->st_size
, abfd
);
5363 /* PR 30499: make sure that users understand that this warning is serious. */
5365 (_("warning: NOTE: size discrepancies can cause real problems. Investigation is advised."));
5368 h
->size
= isym
->st_size
;
5371 /* If this is a common symbol, then we always want H->SIZE
5372 to be the size of the common symbol. The code just above
5373 won't fix the size if a common symbol becomes larger. We
5374 don't warn about a size change here, because that is
5375 covered by --warn-common. Allow changes between different
5377 if (h
->root
.type
== bfd_link_hash_common
)
5378 h
->size
= h
->root
.u
.c
.size
;
5380 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
5381 && ((definition
&& !new_weak
)
5382 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
5383 || h
->type
== STT_NOTYPE
))
5385 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
5387 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5389 if (type
== STT_GNU_IFUNC
5390 && (abfd
->flags
& DYNAMIC
) != 0)
5393 if (h
->type
!= type
)
5395 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
5396 /* xgettext:c-format */
5398 (_("warning: type of symbol `%s' changed"
5399 " from %d to %d in %pB"),
5400 name
, h
->type
, type
, abfd
);
5406 /* Merge st_other field. */
5407 elf_merge_st_other (abfd
, h
, isym
->st_other
, sec
,
5408 definition
, dynamic
);
5410 /* We don't want to make debug symbol dynamic. */
5412 && (sec
->flags
& SEC_DEBUGGING
)
5413 && !bfd_link_relocatable (info
))
5416 /* Nor should we make plugin symbols dynamic. */
5417 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5422 h
->target_internal
= isym
->st_target_internal
;
5423 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
5426 /* Don't add indirect symbols for .symver x, x@FOO aliases
5427 in IR. Since all data or text symbols in IR have the
5428 same type, value and section, we can't tell if a symbol
5429 is an alias of another symbol by their types, values and
5433 && (abfd
->flags
& BFD_PLUGIN
) == 0)
5435 char *p
= strchr (name
, ELF_VER_CHR
);
5436 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
5438 /* Queue non-default versions so that .symver x, x@FOO
5439 aliases can be checked. */
5442 size_t amt
= ((isymend
- isym
+ 1)
5443 * sizeof (struct elf_link_hash_entry
*));
5445 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5447 goto error_free_vers
;
5449 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
5453 if (dynsym
&& h
->dynindx
== -1)
5455 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5456 goto error_free_vers
;
5458 && weakdef (h
)->dynindx
== -1)
5460 if (!bfd_elf_link_record_dynamic_symbol (info
, weakdef (h
)))
5461 goto error_free_vers
;
5464 else if (h
->dynindx
!= -1)
5465 /* If the symbol already has a dynamic index, but
5466 visibility says it should not be visible, turn it into
5468 switch (ELF_ST_VISIBILITY (h
->other
))
5472 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
5480 && h
->root
.type
!= bfd_link_hash_indirect
5482 && h
->ref_regular_nonweak
)
5484 && (old_bfd
->flags
& BFD_PLUGIN
) != 0
5485 && h
->ref_ir_nonweak
5486 && !info
->lto_all_symbols_read
)
5487 || (h
->ref_dynamic_nonweak
5488 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5489 && !on_needed_list (elf_dt_name (abfd
),
5490 htab
->needed
, NULL
))))
5492 const char *soname
= elf_dt_name (abfd
);
5494 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
5495 h
->root
.root
.string
);
5497 /* A symbol from a library loaded via DT_NEEDED of some
5498 other library is referenced by a regular object.
5499 Add a DT_NEEDED entry for it. Issue an error if
5500 --no-add-needed is used and the reference was not
5503 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
5506 /* xgettext:c-format */
5507 (_("%pB: undefined reference to symbol '%s'"),
5509 bfd_set_error (bfd_error_missing_dso
);
5510 goto error_free_vers
;
5513 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
5514 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
5516 /* Create dynamic sections for backends that require
5517 that be done before setup_gnu_properties. */
5518 if (!_bfd_elf_link_create_dynamic_sections (abfd
, info
))
5525 if (info
->lto_plugin_active
5526 && !bfd_link_relocatable (info
)
5527 && (abfd
->flags
& BFD_PLUGIN
) == 0
5533 if (bed
->s
->arch_size
== 32)
5538 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5539 referenced in regular objects so that linker plugin will get
5540 the correct symbol resolution. */
5542 sym_hash
= elf_sym_hashes (abfd
);
5543 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5545 Elf_Internal_Rela
*internal_relocs
;
5546 Elf_Internal_Rela
*rel
, *relend
;
5548 /* Don't check relocations in excluded sections. */
5549 if ((s
->flags
& SEC_RELOC
) == 0
5550 || s
->reloc_count
== 0
5551 || (s
->flags
& SEC_EXCLUDE
) != 0
5552 || ((info
->strip
== strip_all
5553 || info
->strip
== strip_debugger
)
5554 && (s
->flags
& SEC_DEBUGGING
) != 0))
5557 internal_relocs
= _bfd_elf_link_info_read_relocs (abfd
, info
,
5560 _bfd_link_keep_memory (info
));
5561 if (internal_relocs
== NULL
)
5562 goto error_free_vers
;
5564 rel
= internal_relocs
;
5565 relend
= rel
+ s
->reloc_count
;
5566 for ( ; rel
< relend
; rel
++)
5568 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
5569 struct elf_link_hash_entry
*h
;
5571 /* Skip local symbols. */
5572 if (r_symndx
< extsymoff
)
5575 h
= sym_hash
[r_symndx
- extsymoff
];
5577 h
->root
.non_ir_ref_regular
= 1;
5580 if (elf_section_data (s
)->relocs
!= internal_relocs
)
5581 free (internal_relocs
);
5590 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
5594 /* Restore the symbol table. */
5595 old_ent
= (char *) old_tab
+ tabsize
;
5596 memset (elf_sym_hashes (abfd
), 0,
5597 extsymcount
* sizeof (struct elf_link_hash_entry
*));
5598 htab
->root
.table
.table
= old_table
;
5599 htab
->root
.table
.size
= old_size
;
5600 htab
->root
.table
.count
= old_count
;
5601 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
5602 htab
->root
.undefs
= old_undefs
;
5603 htab
->root
.undefs_tail
= old_undefs_tail
;
5604 if (htab
->dynstr
!= NULL
)
5605 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
5608 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
5610 struct bfd_hash_entry
*p
;
5611 struct elf_link_hash_entry
*h
;
5612 unsigned int non_ir_ref_dynamic
;
5614 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
5616 /* Preserve non_ir_ref_dynamic so that this symbol
5617 will be exported when the dynamic lib becomes needed
5618 in the second pass. */
5619 h
= (struct elf_link_hash_entry
*) p
;
5620 if (h
->root
.type
== bfd_link_hash_warning
)
5621 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5622 non_ir_ref_dynamic
= h
->root
.non_ir_ref_dynamic
;
5624 h
= (struct elf_link_hash_entry
*) p
;
5625 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5626 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5627 if (h
->root
.type
== bfd_link_hash_warning
)
5629 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5630 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5631 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5633 if (h
->root
.type
== bfd_link_hash_common
)
5635 memcpy (h
->root
.u
.c
.p
, old_ent
, sizeof (*h
->root
.u
.c
.p
));
5636 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
5638 h
->root
.non_ir_ref_dynamic
= non_ir_ref_dynamic
;
5642 /* Make a special call to the linker "notice" function to
5643 tell it that symbols added for crefs may need to be removed. */
5644 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
5645 goto error_free_vers
;
5648 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
5650 free (nondeflt_vers
);
5654 if (old_tab
!= NULL
)
5656 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
5657 goto error_free_vers
;
5662 /* Now that all the symbols from this input file are created, if
5663 not performing a relocatable link, handle .symver foo, foo@BAR
5664 such that any relocs against foo become foo@BAR. */
5665 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5669 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5671 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5672 char *shortname
, *p
;
5675 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5677 || (h
->root
.type
!= bfd_link_hash_defined
5678 && h
->root
.type
!= bfd_link_hash_defweak
))
5681 amt
= p
- h
->root
.root
.string
;
5682 shortname
= (char *) bfd_malloc (amt
+ 1);
5684 goto error_free_vers
;
5685 memcpy (shortname
, h
->root
.root
.string
, amt
);
5686 shortname
[amt
] = '\0';
5688 hi
= (struct elf_link_hash_entry
*)
5689 bfd_link_hash_lookup (&htab
->root
, shortname
,
5690 false, false, false);
5692 && hi
->root
.type
== h
->root
.type
5693 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5694 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5696 (*bed
->elf_backend_hide_symbol
) (info
, hi
, true);
5697 hi
->root
.type
= bfd_link_hash_indirect
;
5698 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5699 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5700 sym_hash
= elf_sym_hashes (abfd
);
5702 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5703 if (sym_hash
[symidx
] == hi
)
5705 sym_hash
[symidx
] = h
;
5711 free (nondeflt_vers
);
5712 nondeflt_vers
= NULL
;
5715 /* Now set the alias field correctly for all the weak defined
5716 symbols we found. The only way to do this is to search all the
5717 symbols. Since we only need the information for non functions in
5718 dynamic objects, that's the only time we actually put anything on
5719 the list WEAKS. We need this information so that if a regular
5720 object refers to a symbol defined weakly in a dynamic object, the
5721 real symbol in the dynamic object is also put in the dynamic
5722 symbols; we also must arrange for both symbols to point to the
5723 same memory location. We could handle the general case of symbol
5724 aliasing, but a general symbol alias can only be generated in
5725 assembler code, handling it correctly would be very time
5726 consuming, and other ELF linkers don't handle general aliasing
5730 struct elf_link_hash_entry
**hpp
;
5731 struct elf_link_hash_entry
**hppend
;
5732 struct elf_link_hash_entry
**sorted_sym_hash
;
5733 struct elf_link_hash_entry
*h
;
5734 size_t sym_count
, amt
;
5736 /* Since we have to search the whole symbol list for each weak
5737 defined symbol, search time for N weak defined symbols will be
5738 O(N^2). Binary search will cut it down to O(NlogN). */
5739 amt
= extsymcount
* sizeof (*sorted_sym_hash
);
5740 sorted_sym_hash
= bfd_malloc (amt
);
5741 if (sorted_sym_hash
== NULL
)
5743 sym_hash
= sorted_sym_hash
;
5744 hpp
= elf_sym_hashes (abfd
);
5745 hppend
= hpp
+ extsymcount
;
5747 for (; hpp
< hppend
; hpp
++)
5751 && h
->root
.type
== bfd_link_hash_defined
5752 && !bed
->is_function_type (h
->type
))
5760 qsort (sorted_sym_hash
, sym_count
, sizeof (*sorted_sym_hash
),
5763 while (weaks
!= NULL
)
5765 struct elf_link_hash_entry
*hlook
;
5768 size_t i
, j
, idx
= 0;
5771 weaks
= hlook
->u
.alias
;
5772 hlook
->u
.alias
= NULL
;
5774 if (hlook
->root
.type
!= bfd_link_hash_defined
5775 && hlook
->root
.type
!= bfd_link_hash_defweak
)
5778 slook
= hlook
->root
.u
.def
.section
;
5779 vlook
= hlook
->root
.u
.def
.value
;
5785 bfd_signed_vma vdiff
;
5787 h
= sorted_sym_hash
[idx
];
5788 vdiff
= vlook
- h
->root
.u
.def
.value
;
5795 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5805 /* We didn't find a value/section match. */
5809 /* With multiple aliases, or when the weak symbol is already
5810 strongly defined, we have multiple matching symbols and
5811 the binary search above may land on any of them. Step
5812 one past the matching symbol(s). */
5815 h
= sorted_sym_hash
[idx
];
5816 if (h
->root
.u
.def
.section
!= slook
5817 || h
->root
.u
.def
.value
!= vlook
)
5821 /* Now look back over the aliases. Since we sorted by size
5822 as well as value and section, we'll choose the one with
5823 the largest size. */
5826 h
= sorted_sym_hash
[idx
];
5828 /* Stop if value or section doesn't match. */
5829 if (h
->root
.u
.def
.section
!= slook
5830 || h
->root
.u
.def
.value
!= vlook
)
5832 else if (h
!= hlook
)
5834 struct elf_link_hash_entry
*t
;
5837 hlook
->is_weakalias
= 1;
5839 if (t
->u
.alias
!= NULL
)
5840 while (t
->u
.alias
!= h
)
5844 /* If the weak definition is in the list of dynamic
5845 symbols, make sure the real definition is put
5847 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5849 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5852 free (sorted_sym_hash
);
5857 /* If the real definition is in the list of dynamic
5858 symbols, make sure the weak definition is put
5859 there as well. If we don't do this, then the
5860 dynamic loader might not merge the entries for the
5861 real definition and the weak definition. */
5862 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5864 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5865 goto err_free_sym_hash
;
5872 free (sorted_sym_hash
);
5875 if (bed
->check_directives
5876 && !(*bed
->check_directives
) (abfd
, info
))
5879 /* If this is a non-traditional link, try to optimize the handling
5880 of the .stab/.stabstr sections. */
5882 && ! info
->traditional_format
5883 && is_elf_hash_table (&htab
->root
)
5884 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5888 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5889 if (stabstr
!= NULL
)
5891 bfd_size_type string_offset
= 0;
5894 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5895 if (startswith (stab
->name
, ".stab")
5896 && (!stab
->name
[5] ||
5897 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5898 && (stab
->flags
& SEC_MERGE
) == 0
5899 && !bfd_is_abs_section (stab
->output_section
))
5901 struct bfd_elf_section_data
*secdata
;
5903 secdata
= elf_section_data (stab
);
5904 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5905 stabstr
, &secdata
->sec_info
,
5908 if (secdata
->sec_info
)
5909 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5914 if (dynamic
&& add_needed
)
5916 /* Add this bfd to the loaded list. */
5917 struct elf_link_loaded_list
*n
;
5919 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5923 n
->next
= htab
->dyn_loaded
;
5924 htab
->dyn_loaded
= n
;
5926 if (dynamic
&& !add_needed
5927 && (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) != 0)
5928 elf_dyn_lib_class (abfd
) |= DYN_NO_NEEDED
;
5935 free (nondeflt_vers
);
5943 /* Return the linker hash table entry of a symbol that might be
5944 satisfied by an archive symbol. Return -1 on error. */
5946 struct bfd_link_hash_entry
*
5947 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5948 struct bfd_link_info
*info
,
5951 struct bfd_link_hash_entry
*h
;
5955 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
5959 /* If this is a default version (the name contains @@), look up the
5960 symbol again with only one `@' as well as without the version.
5961 The effect is that references to the symbol with and without the
5962 version will be matched by the default symbol in the archive. */
5964 p
= strchr (name
, ELF_VER_CHR
);
5965 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5968 /* First check with only one `@'. */
5969 len
= strlen (name
);
5970 copy
= (char *) bfd_alloc (abfd
, len
);
5972 return (struct bfd_link_hash_entry
*) -1;
5974 first
= p
- name
+ 1;
5975 memcpy (copy
, name
, first
);
5976 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5978 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
5981 /* We also need to check references to the symbol without the
5983 copy
[first
- 1] = '\0';
5984 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
5987 bfd_release (abfd
, copy
);
5991 /* Add symbols from an ELF archive file to the linker hash table. We
5992 don't use _bfd_generic_link_add_archive_symbols because we need to
5993 handle versioned symbols.
5995 Fortunately, ELF archive handling is simpler than that done by
5996 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5997 oddities. In ELF, if we find a symbol in the archive map, and the
5998 symbol is currently undefined, we know that we must pull in that
6001 Unfortunately, we do have to make multiple passes over the symbol
6002 table until nothing further is resolved. */
6005 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
6008 unsigned char *included
= NULL
;
6012 const struct elf_backend_data
*bed
;
6013 struct bfd_link_hash_entry
* (*archive_symbol_lookup
)
6014 (bfd
*, struct bfd_link_info
*, const char *);
6016 if (! bfd_has_map (abfd
))
6018 /* An empty archive is a special case. */
6019 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
6021 bfd_set_error (bfd_error_no_armap
);
6025 /* Keep track of all symbols we know to be already defined, and all
6026 files we know to be already included. This is to speed up the
6027 second and subsequent passes. */
6028 c
= bfd_ardata (abfd
)->symdef_count
;
6031 amt
= c
* sizeof (*included
);
6032 included
= (unsigned char *) bfd_zmalloc (amt
);
6033 if (included
== NULL
)
6036 symdefs
= bfd_ardata (abfd
)->symdefs
;
6037 bed
= get_elf_backend_data (abfd
);
6038 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
6051 symdefend
= symdef
+ c
;
6052 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
6054 struct bfd_link_hash_entry
*h
;
6056 struct bfd_link_hash_entry
*undefs_tail
;
6061 if (symdef
->file_offset
== last
)
6067 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
6068 if (h
== (struct bfd_link_hash_entry
*) -1)
6074 if (h
->type
== bfd_link_hash_undefined
)
6076 /* If the archive element has already been loaded then one
6077 of the symbols defined by that element might have been
6078 made undefined due to being in a discarded section. */
6079 if (is_elf_hash_table (info
->hash
)
6080 && ((struct elf_link_hash_entry
*) h
)->indx
== -3)
6083 else if (h
->type
== bfd_link_hash_common
)
6085 /* We currently have a common symbol. The archive map contains
6086 a reference to this symbol, so we may want to include it. We
6087 only want to include it however, if this archive element
6088 contains a definition of the symbol, not just another common
6091 Unfortunately some archivers (including GNU ar) will put
6092 declarations of common symbols into their archive maps, as
6093 well as real definitions, so we cannot just go by the archive
6094 map alone. Instead we must read in the element's symbol
6095 table and check that to see what kind of symbol definition
6097 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
6102 if (h
->type
!= bfd_link_hash_undefweak
)
6103 /* Symbol must be defined. Don't check it again. */
6108 /* We need to include this archive member. */
6109 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
,
6111 if (element
== NULL
)
6114 if (! bfd_check_format (element
, bfd_object
))
6117 undefs_tail
= info
->hash
->undefs_tail
;
6119 if (!(*info
->callbacks
6120 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
6122 if (!bfd_link_add_symbols (element
, info
))
6125 /* If there are any new undefined symbols, we need to make
6126 another pass through the archive in order to see whether
6127 they can be defined. FIXME: This isn't perfect, because
6128 common symbols wind up on undefs_tail and because an
6129 undefined symbol which is defined later on in this pass
6130 does not require another pass. This isn't a bug, but it
6131 does make the code less efficient than it could be. */
6132 if (undefs_tail
!= info
->hash
->undefs_tail
)
6135 /* Look backward to mark all symbols from this object file
6136 which we have already seen in this pass. */
6140 included
[mark
] = true;
6145 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
6147 /* We mark subsequent symbols from this object file as we go
6148 on through the loop. */
6149 last
= symdef
->file_offset
;
6162 /* Given an ELF BFD, add symbols to the global hash table as
6166 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
6168 switch (bfd_get_format (abfd
))
6171 return elf_link_add_object_symbols (abfd
, info
);
6173 return elf_link_add_archive_symbols (abfd
, info
);
6175 bfd_set_error (bfd_error_wrong_format
);
6180 struct hash_codes_info
6182 unsigned long *hashcodes
;
6186 /* This function will be called though elf_link_hash_traverse to store
6187 all hash value of the exported symbols in an array. */
6190 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6192 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
6197 /* Ignore indirect symbols. These are added by the versioning code. */
6198 if (h
->dynindx
== -1)
6201 name
= h
->root
.root
.string
;
6202 if (h
->versioned
>= versioned
)
6204 char *p
= strchr (name
, ELF_VER_CHR
);
6207 alc
= (char *) bfd_malloc (p
- name
+ 1);
6213 memcpy (alc
, name
, p
- name
);
6214 alc
[p
- name
] = '\0';
6219 /* Compute the hash value. */
6220 ha
= bfd_elf_hash (name
);
6222 /* Store the found hash value in the array given as the argument. */
6223 *(inf
->hashcodes
)++ = ha
;
6225 /* And store it in the struct so that we can put it in the hash table
6227 h
->u
.elf_hash_value
= ha
;
6233 struct collect_gnu_hash_codes
6236 const struct elf_backend_data
*bed
;
6237 unsigned long int nsyms
;
6238 unsigned long int maskbits
;
6239 unsigned long int *hashcodes
;
6240 unsigned long int *hashval
;
6241 unsigned long int *indx
;
6242 unsigned long int *counts
;
6246 long int min_dynindx
;
6247 unsigned long int bucketcount
;
6248 unsigned long int symindx
;
6249 long int local_indx
;
6250 long int shift1
, shift2
;
6251 unsigned long int mask
;
6255 /* This function will be called though elf_link_hash_traverse to store
6256 all hash value of the exported symbols in an array. */
6259 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6261 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6266 /* Ignore indirect symbols. These are added by the versioning code. */
6267 if (h
->dynindx
== -1)
6270 /* Ignore also local symbols and undefined symbols. */
6271 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6274 name
= h
->root
.root
.string
;
6275 if (h
->versioned
>= versioned
)
6277 char *p
= strchr (name
, ELF_VER_CHR
);
6280 alc
= (char *) bfd_malloc (p
- name
+ 1);
6286 memcpy (alc
, name
, p
- name
);
6287 alc
[p
- name
] = '\0';
6292 /* Compute the hash value. */
6293 ha
= bfd_elf_gnu_hash (name
);
6295 /* Store the found hash value in the array for compute_bucket_count,
6296 and also for .dynsym reordering purposes. */
6297 s
->hashcodes
[s
->nsyms
] = ha
;
6298 s
->hashval
[h
->dynindx
] = ha
;
6300 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
6301 s
->min_dynindx
= h
->dynindx
;
6307 /* This function will be called though elf_link_hash_traverse to do
6308 final dynamic symbol renumbering in case of .gnu.hash.
6309 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6310 to the translation table. */
6313 elf_gnu_hash_process_symidx (struct elf_link_hash_entry
*h
, void *data
)
6315 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6316 unsigned long int bucket
;
6317 unsigned long int val
;
6319 /* Ignore indirect symbols. */
6320 if (h
->dynindx
== -1)
6323 /* Ignore also local symbols and undefined symbols. */
6324 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6326 if (h
->dynindx
>= s
->min_dynindx
)
6328 if (s
->bed
->record_xhash_symbol
!= NULL
)
6330 (*s
->bed
->record_xhash_symbol
) (h
, 0);
6334 h
->dynindx
= s
->local_indx
++;
6339 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
6340 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
6341 & ((s
->maskbits
>> s
->shift1
) - 1);
6342 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
6344 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
6345 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
6346 if (s
->counts
[bucket
] == 1)
6347 /* Last element terminates the chain. */
6349 bfd_put_32 (s
->output_bfd
, val
,
6350 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
6351 --s
->counts
[bucket
];
6352 if (s
->bed
->record_xhash_symbol
!= NULL
)
6354 bfd_vma xlat_loc
= s
->xlat
+ (s
->indx
[bucket
]++ - s
->symindx
) * 4;
6356 (*s
->bed
->record_xhash_symbol
) (h
, xlat_loc
);
6359 h
->dynindx
= s
->indx
[bucket
]++;
6363 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6366 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
6368 return !(h
->forced_local
6369 || h
->root
.type
== bfd_link_hash_undefined
6370 || h
->root
.type
== bfd_link_hash_undefweak
6371 || ((h
->root
.type
== bfd_link_hash_defined
6372 || h
->root
.type
== bfd_link_hash_defweak
)
6373 && h
->root
.u
.def
.section
->output_section
== NULL
));
6376 /* Array used to determine the number of hash table buckets to use
6377 based on the number of symbols there are. If there are fewer than
6378 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6379 fewer than 37 we use 17 buckets, and so forth. We never use more
6380 than 32771 buckets. */
6382 static const size_t elf_buckets
[] =
6384 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6388 /* Compute bucket count for hashing table. We do not use a static set
6389 of possible tables sizes anymore. Instead we determine for all
6390 possible reasonable sizes of the table the outcome (i.e., the
6391 number of collisions etc) and choose the best solution. The
6392 weighting functions are not too simple to allow the table to grow
6393 without bounds. Instead one of the weighting factors is the size.
6394 Therefore the result is always a good payoff between few collisions
6395 (= short chain lengths) and table size. */
6397 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6398 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
6399 unsigned long int nsyms
,
6402 size_t best_size
= 0;
6403 unsigned long int i
;
6409 uint64_t best_chlen
= ~((uint64_t) 0);
6410 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
6411 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
6412 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
6413 unsigned long int *counts
;
6415 unsigned int no_improvement_count
= 0;
6417 /* Possible optimization parameters: if we have NSYMS symbols we say
6418 that the hashing table must at least have NSYMS/4 and at most
6420 minsize
= nsyms
/ 4;
6423 best_size
= maxsize
= nsyms
* 2;
6428 if ((best_size
& 31) == 0)
6432 /* Create array where we count the collisions in. We must use bfd_malloc
6433 since the size could be large. */
6435 amt
*= sizeof (unsigned long int);
6436 counts
= (unsigned long int *) bfd_malloc (amt
);
6440 /* Compute the "optimal" size for the hash table. The criteria is a
6441 minimal chain length. The minor criteria is (of course) the size
6443 for (i
= minsize
; i
< maxsize
; ++i
)
6445 /* Walk through the array of hashcodes and count the collisions. */
6447 unsigned long int j
;
6448 unsigned long int fact
;
6450 if (gnu_hash
&& (i
& 31) == 0)
6453 memset (counts
, '\0', i
* sizeof (unsigned long int));
6455 /* Determine how often each hash bucket is used. */
6456 for (j
= 0; j
< nsyms
; ++j
)
6457 ++counts
[hashcodes
[j
] % i
];
6459 /* For the weight function we need some information about the
6460 pagesize on the target. This is information need not be 100%
6461 accurate. Since this information is not available (so far) we
6462 define it here to a reasonable default value. If it is crucial
6463 to have a better value some day simply define this value. */
6464 # ifndef BFD_TARGET_PAGESIZE
6465 # define BFD_TARGET_PAGESIZE (4096)
6468 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6470 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
6473 /* Variant 1: optimize for short chains. We add the squares
6474 of all the chain lengths (which favors many small chain
6475 over a few long chains). */
6476 for (j
= 0; j
< i
; ++j
)
6477 max
+= counts
[j
] * counts
[j
];
6479 /* This adds penalties for the overall size of the table. */
6480 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6483 /* Variant 2: Optimize a lot more for small table. Here we
6484 also add squares of the size but we also add penalties for
6485 empty slots (the +1 term). */
6486 for (j
= 0; j
< i
; ++j
)
6487 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
6489 /* The overall size of the table is considered, but not as
6490 strong as in variant 1, where it is squared. */
6491 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6495 /* Compare with current best results. */
6496 if (max
< best_chlen
)
6500 no_improvement_count
= 0;
6502 /* PR 11843: Avoid futile long searches for the best bucket size
6503 when there are a large number of symbols. */
6504 else if (++no_improvement_count
== 100)
6512 for (i
= 0; elf_buckets
[i
] != 0; i
++)
6514 best_size
= elf_buckets
[i
];
6515 if (nsyms
< elf_buckets
[i
+ 1])
6518 if (gnu_hash
&& best_size
< 2)
6525 /* Size any SHT_GROUP section for ld -r. */
6528 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
6533 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6534 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6535 && (s
= ibfd
->sections
) != NULL
6536 && s
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
6537 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
6542 /* Set a default stack segment size. The value in INFO wins. If it
6543 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6544 undefined it is initialized. */
6547 bfd_elf_stack_segment_size (bfd
*output_bfd
,
6548 struct bfd_link_info
*info
,
6549 const char *legacy_symbol
,
6550 bfd_vma default_size
)
6552 struct elf_link_hash_entry
*h
= NULL
;
6554 /* Look for legacy symbol. */
6556 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
6557 false, false, false);
6558 if (h
&& (h
->root
.type
== bfd_link_hash_defined
6559 || h
->root
.type
== bfd_link_hash_defweak
)
6561 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
6563 /* The symbol has no type if specified on the command line. */
6564 h
->type
= STT_OBJECT
;
6565 if (info
->stacksize
)
6566 /* xgettext:c-format */
6567 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6568 output_bfd
, legacy_symbol
);
6569 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
6570 /* xgettext:c-format */
6571 _bfd_error_handler (_("%pB: %s not absolute"),
6572 output_bfd
, legacy_symbol
);
6574 info
->stacksize
= h
->root
.u
.def
.value
;
6577 if (!info
->stacksize
)
6578 /* If the user didn't set a size, or explicitly inhibit the
6579 size, set it now. */
6580 info
->stacksize
= default_size
;
6582 /* Provide the legacy symbol, if it is referenced. */
6583 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
6584 || h
->root
.type
== bfd_link_hash_undefweak
))
6586 struct bfd_link_hash_entry
*bh
= NULL
;
6588 if (!(_bfd_generic_link_add_one_symbol
6589 (info
, output_bfd
, legacy_symbol
,
6590 BSF_GLOBAL
, bfd_abs_section_ptr
,
6591 info
->stacksize
>= 0 ? info
->stacksize
: 0,
6592 NULL
, false, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
6595 h
= (struct elf_link_hash_entry
*) bh
;
6597 h
->type
= STT_OBJECT
;
6603 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6605 struct elf_gc_sweep_symbol_info
6607 struct bfd_link_info
*info
;
6608 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
6613 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
6616 && (((h
->root
.type
== bfd_link_hash_defined
6617 || h
->root
.type
== bfd_link_hash_defweak
)
6618 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
6619 && h
->root
.u
.def
.section
->gc_mark
))
6620 || h
->root
.type
== bfd_link_hash_undefined
6621 || h
->root
.type
== bfd_link_hash_undefweak
))
6623 struct elf_gc_sweep_symbol_info
*inf
;
6625 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
6626 (*inf
->hide_symbol
) (inf
->info
, h
, true);
6629 h
->ref_regular_nonweak
= 0;
6635 /* Set up the sizes and contents of the ELF dynamic sections. This is
6636 called by the ELF linker emulation before_allocation routine. We
6637 must set the sizes of the sections before the linker sets the
6638 addresses of the various sections. */
6641 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
6644 const char *filter_shlib
,
6646 const char *depaudit
,
6647 const char * const *auxiliary_filters
,
6648 struct bfd_link_info
*info
,
6649 asection
**sinterpptr
)
6652 const struct elf_backend_data
*bed
;
6656 if (!is_elf_hash_table (info
->hash
))
6659 /* Any syms created from now on start with -1 in
6660 got.refcount/offset and plt.refcount/offset. */
6661 elf_hash_table (info
)->init_got_refcount
6662 = elf_hash_table (info
)->init_got_offset
;
6663 elf_hash_table (info
)->init_plt_refcount
6664 = elf_hash_table (info
)->init_plt_offset
;
6666 bed
= get_elf_backend_data (output_bfd
);
6668 /* The backend may have to create some sections regardless of whether
6669 we're dynamic or not. */
6670 if (bed
->elf_backend_early_size_sections
6671 && !bed
->elf_backend_early_size_sections (output_bfd
, info
))
6674 dynobj
= elf_hash_table (info
)->dynobj
;
6676 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6678 struct bfd_elf_version_tree
*verdefs
;
6679 struct elf_info_failed asvinfo
;
6680 struct bfd_elf_version_tree
*t
;
6681 struct bfd_elf_version_expr
*d
;
6685 /* If we are supposed to export all symbols into the dynamic symbol
6686 table (this is not the normal case), then do so. */
6687 if (info
->export_dynamic
6688 || (bfd_link_executable (info
) && info
->dynamic
))
6690 struct elf_info_failed eif
;
6694 elf_link_hash_traverse (elf_hash_table (info
),
6695 _bfd_elf_export_symbol
,
6703 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6705 if (soname_indx
== (size_t) -1
6706 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6710 soname_indx
= (size_t) -1;
6712 /* Make all global versions with definition. */
6713 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6714 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6715 if (!d
->symver
&& d
->literal
)
6717 const char *verstr
, *name
;
6718 size_t namelen
, verlen
, newlen
;
6719 char *newname
, *p
, leading_char
;
6720 struct elf_link_hash_entry
*newh
;
6722 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6724 namelen
= strlen (name
) + (leading_char
!= '\0');
6726 verlen
= strlen (verstr
);
6727 newlen
= namelen
+ verlen
+ 3;
6729 newname
= (char *) bfd_malloc (newlen
);
6730 if (newname
== NULL
)
6732 newname
[0] = leading_char
;
6733 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6735 /* Check the hidden versioned definition. */
6736 p
= newname
+ namelen
;
6738 memcpy (p
, verstr
, verlen
+ 1);
6739 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6740 newname
, false, false,
6743 || (newh
->root
.type
!= bfd_link_hash_defined
6744 && newh
->root
.type
!= bfd_link_hash_defweak
))
6746 /* Check the default versioned definition. */
6748 memcpy (p
, verstr
, verlen
+ 1);
6749 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6750 newname
, false, false,
6755 /* Mark this version if there is a definition and it is
6756 not defined in a shared object. */
6758 && !newh
->def_dynamic
6759 && (newh
->root
.type
== bfd_link_hash_defined
6760 || newh
->root
.type
== bfd_link_hash_defweak
))
6764 /* Attach all the symbols to their version information. */
6765 asvinfo
.info
= info
;
6766 asvinfo
.failed
= false;
6768 elf_link_hash_traverse (elf_hash_table (info
),
6769 _bfd_elf_link_assign_sym_version
,
6774 if (!info
->allow_undefined_version
)
6776 /* Check if all global versions have a definition. */
6777 bool all_defined
= true;
6778 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6779 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6780 if (d
->literal
&& !d
->symver
&& !d
->script
)
6783 (_("%s: undefined version: %s"),
6784 d
->pattern
, t
->name
);
6785 all_defined
= false;
6790 bfd_set_error (bfd_error_bad_value
);
6795 /* Set up the version definition section. */
6796 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6797 BFD_ASSERT (s
!= NULL
);
6799 /* We may have created additional version definitions if we are
6800 just linking a regular application. */
6801 verdefs
= info
->version_info
;
6803 /* Skip anonymous version tag. */
6804 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6805 verdefs
= verdefs
->next
;
6807 if (verdefs
== NULL
&& !info
->create_default_symver
)
6808 s
->flags
|= SEC_EXCLUDE
;
6814 Elf_Internal_Verdef def
;
6815 Elf_Internal_Verdaux defaux
;
6816 struct bfd_link_hash_entry
*bh
;
6817 struct elf_link_hash_entry
*h
;
6823 /* Make space for the base version. */
6824 size
+= sizeof (Elf_External_Verdef
);
6825 size
+= sizeof (Elf_External_Verdaux
);
6828 /* Make space for the default version. */
6829 if (info
->create_default_symver
)
6831 size
+= sizeof (Elf_External_Verdef
);
6835 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6837 struct bfd_elf_version_deps
*n
;
6839 /* Don't emit base version twice. */
6843 size
+= sizeof (Elf_External_Verdef
);
6844 size
+= sizeof (Elf_External_Verdaux
);
6847 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6848 size
+= sizeof (Elf_External_Verdaux
);
6852 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6853 if (s
->contents
== NULL
&& s
->size
!= 0)
6856 /* Fill in the version definition section. */
6860 def
.vd_version
= VER_DEF_CURRENT
;
6861 def
.vd_flags
= VER_FLG_BASE
;
6864 if (info
->create_default_symver
)
6866 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6867 def
.vd_next
= sizeof (Elf_External_Verdef
);
6871 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6872 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6873 + sizeof (Elf_External_Verdaux
));
6876 if (soname_indx
!= (size_t) -1)
6878 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6880 def
.vd_hash
= bfd_elf_hash (soname
);
6881 defaux
.vda_name
= soname_indx
;
6888 name
= lbasename (bfd_get_filename (output_bfd
));
6889 def
.vd_hash
= bfd_elf_hash (name
);
6890 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6892 if (indx
== (size_t) -1)
6894 defaux
.vda_name
= indx
;
6896 defaux
.vda_next
= 0;
6898 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6899 (Elf_External_Verdef
*) p
);
6900 p
+= sizeof (Elf_External_Verdef
);
6901 if (info
->create_default_symver
)
6903 /* Add a symbol representing this version. */
6905 if (! (_bfd_generic_link_add_one_symbol
6906 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6908 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6910 h
= (struct elf_link_hash_entry
*) bh
;
6913 h
->type
= STT_OBJECT
;
6914 h
->verinfo
.vertree
= NULL
;
6916 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6919 /* Create a duplicate of the base version with the same
6920 aux block, but different flags. */
6923 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6925 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6926 + sizeof (Elf_External_Verdaux
));
6929 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6930 (Elf_External_Verdef
*) p
);
6931 p
+= sizeof (Elf_External_Verdef
);
6933 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6934 (Elf_External_Verdaux
*) p
);
6935 p
+= sizeof (Elf_External_Verdaux
);
6937 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6940 struct bfd_elf_version_deps
*n
;
6942 /* Don't emit the base version twice. */
6947 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6950 /* Add a symbol representing this version. */
6952 if (! (_bfd_generic_link_add_one_symbol
6953 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6955 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6957 h
= (struct elf_link_hash_entry
*) bh
;
6960 h
->type
= STT_OBJECT
;
6961 h
->verinfo
.vertree
= t
;
6963 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6966 def
.vd_version
= VER_DEF_CURRENT
;
6968 if (t
->globals
.list
== NULL
6969 && t
->locals
.list
== NULL
6971 def
.vd_flags
|= VER_FLG_WEAK
;
6972 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6973 def
.vd_cnt
= cdeps
+ 1;
6974 def
.vd_hash
= bfd_elf_hash (t
->name
);
6975 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6978 /* If a basever node is next, it *must* be the last node in
6979 the chain, otherwise Verdef construction breaks. */
6980 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6981 BFD_ASSERT (t
->next
->next
== NULL
);
6983 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6984 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6985 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6987 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6988 (Elf_External_Verdef
*) p
);
6989 p
+= sizeof (Elf_External_Verdef
);
6991 defaux
.vda_name
= h
->dynstr_index
;
6992 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6994 defaux
.vda_next
= 0;
6995 if (t
->deps
!= NULL
)
6996 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6997 t
->name_indx
= defaux
.vda_name
;
6999 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
7000 (Elf_External_Verdaux
*) p
);
7001 p
+= sizeof (Elf_External_Verdaux
);
7003 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
7005 if (n
->version_needed
== NULL
)
7007 /* This can happen if there was an error in the
7009 defaux
.vda_name
= 0;
7013 defaux
.vda_name
= n
->version_needed
->name_indx
;
7014 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
7017 if (n
->next
== NULL
)
7018 defaux
.vda_next
= 0;
7020 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
7022 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
7023 (Elf_External_Verdaux
*) p
);
7024 p
+= sizeof (Elf_External_Verdaux
);
7028 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
7032 if (info
->gc_sections
&& bed
->can_gc_sections
)
7034 struct elf_gc_sweep_symbol_info sweep_info
;
7036 /* Remove the symbols that were in the swept sections from the
7037 dynamic symbol table. */
7038 sweep_info
.info
= info
;
7039 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
7040 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
7044 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7047 struct elf_find_verdep_info sinfo
;
7049 /* Work out the size of the version reference section. */
7051 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
7052 BFD_ASSERT (s
!= NULL
);
7055 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
7056 if (sinfo
.vers
== 0)
7058 sinfo
.failed
= false;
7060 elf_link_hash_traverse (elf_hash_table (info
),
7061 _bfd_elf_link_find_version_dependencies
,
7066 bed
->elf_backend_add_glibc_version_dependency (&sinfo
);
7070 if (elf_tdata (output_bfd
)->verref
== NULL
)
7071 s
->flags
|= SEC_EXCLUDE
;
7074 Elf_Internal_Verneed
*vn
;
7079 /* Build the version dependency section. */
7082 for (vn
= elf_tdata (output_bfd
)->verref
;
7084 vn
= vn
->vn_nextref
)
7086 Elf_Internal_Vernaux
*a
;
7088 size
+= sizeof (Elf_External_Verneed
);
7090 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7091 size
+= sizeof (Elf_External_Vernaux
);
7095 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7096 if (s
->contents
== NULL
)
7100 for (vn
= elf_tdata (output_bfd
)->verref
;
7102 vn
= vn
->vn_nextref
)
7105 Elf_Internal_Vernaux
*a
;
7109 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7112 vn
->vn_version
= VER_NEED_CURRENT
;
7114 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7115 elf_dt_name (vn
->vn_bfd
) != NULL
7116 ? elf_dt_name (vn
->vn_bfd
)
7117 : lbasename (bfd_get_filename
7120 if (indx
== (size_t) -1)
7123 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
7124 if (vn
->vn_nextref
== NULL
)
7127 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
7128 + caux
* sizeof (Elf_External_Vernaux
));
7130 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
7131 (Elf_External_Verneed
*) p
);
7132 p
+= sizeof (Elf_External_Verneed
);
7134 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7136 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
7137 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7138 a
->vna_nodename
, false);
7139 if (indx
== (size_t) -1)
7142 if (a
->vna_nextptr
== NULL
)
7145 a
->vna_next
= sizeof (Elf_External_Vernaux
);
7147 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
7148 (Elf_External_Vernaux
*) p
);
7149 p
+= sizeof (Elf_External_Vernaux
);
7153 elf_tdata (output_bfd
)->cverrefs
= crefs
;
7157 if (bfd_link_relocatable (info
)
7158 && !_bfd_elf_size_group_sections (info
))
7161 /* Determine any GNU_STACK segment requirements, after the backend
7162 has had a chance to set a default segment size. */
7163 if (info
->execstack
)
7165 /* If the user has explicitly requested warnings, then generate one even
7166 though the choice is the result of another command line option. */
7167 if (info
->warn_execstack
== 1)
7169 if (info
->error_execstack
)
7173 error: creating an executable stack because of -z execstack command line option"));
7179 warning: enabling an executable stack because of -z execstack command line option"));
7182 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
7184 else if (info
->noexecstack
)
7185 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
7189 asection
*notesec
= NULL
;
7190 bfd
*noteobj
= NULL
;
7191 bfd
*emptyobj
= NULL
;
7194 for (inputobj
= info
->input_bfds
;
7196 inputobj
= inputobj
->link
.next
)
7201 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
7203 s
= inputobj
->sections
;
7204 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
7207 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
7211 if (s
->flags
& SEC_CODE
)
7215 /* There is no point in scanning the remaining bfds. */
7219 else if (bed
->default_execstack
&& info
->default_execstack
)
7222 emptyobj
= inputobj
;
7226 if (notesec
|| info
->stacksize
> 0)
7230 if (info
->warn_execstack
!= 0)
7232 /* PR 29072: Because an executable stack is a serious
7233 security risk, make sure that the user knows that it is
7234 being enabled despite the fact that it was not requested
7235 on the command line. */
7238 if (info
->error_execstack
)
7240 _bfd_error_handler (_("\
7241 error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
7242 bfd_get_filename (noteobj
));
7246 _bfd_error_handler (_("\
7247 warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7248 bfd_get_filename (noteobj
));
7252 if (info
->error_execstack
)
7254 _bfd_error_handler (_("\
7255 error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
7256 bfd_get_filename (emptyobj
));
7260 _bfd_error_handler (_("\
7261 warning: %s: missing .note.GNU-stack section implies executable stack"),
7262 bfd_get_filename (emptyobj
));
7263 _bfd_error_handler (_("\
7264 NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7268 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
7271 if (notesec
&& exec
&& bfd_link_relocatable (info
)
7272 && notesec
->output_section
!= bfd_abs_section_ptr
)
7273 notesec
->output_section
->flags
|= SEC_CODE
;
7276 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7278 struct elf_info_failed eif
;
7279 struct elf_link_hash_entry
*h
;
7283 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
7284 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
7288 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
7290 info
->flags
|= DF_SYMBOLIC
;
7298 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
7300 if (indx
== (size_t) -1)
7303 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
7304 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
7308 if (filter_shlib
!= NULL
)
7312 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7313 filter_shlib
, true);
7314 if (indx
== (size_t) -1
7315 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
7319 if (auxiliary_filters
!= NULL
)
7321 const char * const *p
;
7323 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
7327 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7329 if (indx
== (size_t) -1
7330 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
7339 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
7341 if (indx
== (size_t) -1
7342 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
7346 if (depaudit
!= NULL
)
7350 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
7352 if (indx
== (size_t) -1
7353 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
7360 /* Find all symbols which were defined in a dynamic object and make
7361 the backend pick a reasonable value for them. */
7362 elf_link_hash_traverse (elf_hash_table (info
),
7363 _bfd_elf_adjust_dynamic_symbol
,
7368 /* Add some entries to the .dynamic section. We fill in some of the
7369 values later, in bfd_elf_final_link, but we must add the entries
7370 now so that we know the final size of the .dynamic section. */
7372 /* If there are initialization and/or finalization functions to
7373 call then add the corresponding DT_INIT/DT_FINI entries. */
7374 h
= (info
->init_function
7375 ? elf_link_hash_lookup (elf_hash_table (info
),
7376 info
->init_function
, false,
7383 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
7386 h
= (info
->fini_function
7387 ? elf_link_hash_lookup (elf_hash_table (info
),
7388 info
->fini_function
, false,
7395 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
7399 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
7400 if (s
!= NULL
&& s
->linker_has_input
)
7402 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7403 if (! bfd_link_executable (info
))
7408 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
7409 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
7410 && (o
= sub
->sections
) != NULL
7411 && o
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
)
7412 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7413 if (elf_section_data (o
)->this_hdr
.sh_type
7414 == SHT_PREINIT_ARRAY
)
7417 (_("%pB: .preinit_array section is not allowed in DSO"),
7422 bfd_set_error (bfd_error_nonrepresentable_section
);
7426 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
7427 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
7430 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
7431 if (s
!= NULL
&& s
->linker_has_input
)
7433 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
7434 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
7437 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
7438 if (s
!= NULL
&& s
->linker_has_input
)
7440 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
7441 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
7445 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
7446 /* If .dynstr is excluded from the link, we don't want any of
7447 these tags. Strictly, we should be checking each section
7448 individually; This quick check covers for the case where
7449 someone does a /DISCARD/ : { *(*) }. */
7450 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
7452 bfd_size_type strsize
;
7454 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7455 if ((info
->emit_hash
7456 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
7457 || (info
->emit_gnu_hash
7458 && (bed
->record_xhash_symbol
== NULL
7459 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0)))
7460 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
7461 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
7462 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
7463 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
7465 || (info
->gnu_flags_1
7466 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_FLAGS_1
,
7467 info
->gnu_flags_1
)))
7472 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
7475 /* The backend must work out the sizes of all the other dynamic
7477 if (bed
->elf_backend_late_size_sections
!= NULL
7478 && !bed
->elf_backend_late_size_sections (output_bfd
, info
))
7481 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7483 if (elf_tdata (output_bfd
)->cverdefs
)
7485 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
7487 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
7488 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
7492 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
7494 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
7497 else if (info
->flags
& DF_BIND_NOW
)
7499 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
7505 if (bfd_link_executable (info
))
7506 info
->flags_1
&= ~ (DF_1_INITFIRST
7509 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
7513 if (elf_tdata (output_bfd
)->cverrefs
)
7515 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
7517 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
7518 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
7522 if ((elf_tdata (output_bfd
)->cverrefs
== 0
7523 && elf_tdata (output_bfd
)->cverdefs
== 0)
7524 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, NULL
) <= 1)
7528 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7529 s
->flags
|= SEC_EXCLUDE
;
7535 /* Find the first non-excluded output section. We'll use its
7536 section symbol for some emitted relocs. */
7538 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
7541 asection
*found
= NULL
;
7543 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7544 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7545 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7548 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7551 elf_hash_table (info
)->text_index_section
= found
;
7554 /* Find two non-excluded output sections, one for code, one for data.
7555 We'll use their section symbols for some emitted relocs. */
7557 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
7560 asection
*found
= NULL
;
7562 /* Data first, since setting text_index_section changes
7563 _bfd_elf_omit_section_dynsym_default. */
7564 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7565 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7566 && !(s
->flags
& SEC_READONLY
)
7567 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7570 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7573 elf_hash_table (info
)->data_index_section
= found
;
7575 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7576 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7577 && (s
->flags
& SEC_READONLY
)
7578 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7583 elf_hash_table (info
)->text_index_section
= found
;
7586 #define GNU_HASH_SECTION_NAME(bed) \
7587 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7590 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
7592 const struct elf_backend_data
*bed
;
7593 unsigned long section_sym_count
;
7594 bfd_size_type dynsymcount
= 0;
7596 if (!is_elf_hash_table (info
->hash
))
7599 bed
= get_elf_backend_data (output_bfd
);
7600 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
7602 /* Assign dynsym indices. In a shared library we generate a section
7603 symbol for each output section, which come first. Next come all
7604 of the back-end allocated local dynamic syms, followed by the rest
7605 of the global symbols.
7607 This is usually not needed for static binaries, however backends
7608 can request to always do it, e.g. the MIPS backend uses dynamic
7609 symbol counts to lay out GOT, which will be produced in the
7610 presence of GOT relocations even in static binaries (holding fixed
7611 data in that case, to satisfy those relocations). */
7613 if (elf_hash_table (info
)->dynamic_sections_created
7614 || bed
->always_renumber_dynsyms
)
7615 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
7616 §ion_sym_count
);
7618 if (elf_hash_table (info
)->dynamic_sections_created
)
7622 unsigned int dtagcount
;
7624 dynobj
= elf_hash_table (info
)->dynobj
;
7626 /* Work out the size of the symbol version section. */
7627 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7628 BFD_ASSERT (s
!= NULL
);
7629 if ((s
->flags
& SEC_EXCLUDE
) == 0)
7631 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
7632 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7633 if (s
->contents
== NULL
)
7636 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
7640 /* Set the size of the .dynsym and .hash sections. We counted
7641 the number of dynamic symbols in elf_link_add_object_symbols.
7642 We will build the contents of .dynsym and .hash when we build
7643 the final symbol table, because until then we do not know the
7644 correct value to give the symbols. We built the .dynstr
7645 section as we went along in elf_link_add_object_symbols. */
7646 s
= elf_hash_table (info
)->dynsym
;
7647 BFD_ASSERT (s
!= NULL
);
7648 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
7650 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7651 if (s
->contents
== NULL
)
7654 /* The first entry in .dynsym is a dummy symbol. Clear all the
7655 section syms, in case we don't output them all. */
7656 ++section_sym_count
;
7657 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
7659 elf_hash_table (info
)->bucketcount
= 0;
7661 /* Compute the size of the hashing table. As a side effect this
7662 computes the hash values for all the names we export. */
7663 if (info
->emit_hash
)
7665 unsigned long int *hashcodes
;
7666 struct hash_codes_info hashinf
;
7668 unsigned long int nsyms
;
7670 size_t hash_entry_size
;
7672 /* Compute the hash values for all exported symbols. At the same
7673 time store the values in an array so that we could use them for
7675 amt
= dynsymcount
* sizeof (unsigned long int);
7676 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
7677 if (hashcodes
== NULL
)
7679 hashinf
.hashcodes
= hashcodes
;
7680 hashinf
.error
= false;
7682 /* Put all hash values in HASHCODES. */
7683 elf_link_hash_traverse (elf_hash_table (info
),
7684 elf_collect_hash_codes
, &hashinf
);
7691 nsyms
= hashinf
.hashcodes
- hashcodes
;
7693 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
7696 if (bucketcount
== 0 && nsyms
> 0)
7699 elf_hash_table (info
)->bucketcount
= bucketcount
;
7701 s
= bfd_get_linker_section (dynobj
, ".hash");
7702 BFD_ASSERT (s
!= NULL
);
7703 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
7704 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
7705 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7706 if (s
->contents
== NULL
)
7709 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
7710 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
7711 s
->contents
+ hash_entry_size
);
7714 if (info
->emit_gnu_hash
)
7717 unsigned char *contents
;
7718 struct collect_gnu_hash_codes cinfo
;
7722 memset (&cinfo
, 0, sizeof (cinfo
));
7724 /* Compute the hash values for all exported symbols. At the same
7725 time store the values in an array so that we could use them for
7727 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
7728 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
7729 if (cinfo
.hashcodes
== NULL
)
7732 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
7733 cinfo
.min_dynindx
= -1;
7734 cinfo
.output_bfd
= output_bfd
;
7737 /* Put all hash values in HASHCODES. */
7738 elf_link_hash_traverse (elf_hash_table (info
),
7739 elf_collect_gnu_hash_codes
, &cinfo
);
7742 free (cinfo
.hashcodes
);
7747 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
7749 if (bucketcount
== 0)
7751 free (cinfo
.hashcodes
);
7755 s
= bfd_get_linker_section (dynobj
, GNU_HASH_SECTION_NAME (bed
));
7756 BFD_ASSERT (s
!= NULL
);
7758 if (cinfo
.nsyms
== 0)
7760 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7761 BFD_ASSERT (cinfo
.min_dynindx
== -1);
7762 free (cinfo
.hashcodes
);
7763 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
7764 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7765 if (contents
== NULL
)
7767 s
->contents
= contents
;
7768 /* 1 empty bucket. */
7769 bfd_put_32 (output_bfd
, 1, contents
);
7770 /* SYMIDX above the special symbol 0. */
7771 bfd_put_32 (output_bfd
, 1, contents
+ 4);
7772 /* Just one word for bitmask. */
7773 bfd_put_32 (output_bfd
, 1, contents
+ 8);
7774 /* Only hash fn bloom filter. */
7775 bfd_put_32 (output_bfd
, 0, contents
+ 12);
7776 /* No hashes are valid - empty bitmask. */
7777 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
7778 /* No hashes in the only bucket. */
7779 bfd_put_32 (output_bfd
, 0,
7780 contents
+ 16 + bed
->s
->arch_size
/ 8);
7784 unsigned long int maskwords
, maskbitslog2
, x
;
7785 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
7789 while ((x
>>= 1) != 0)
7791 if (maskbitslog2
< 3)
7793 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
7794 maskbitslog2
= maskbitslog2
+ 3;
7796 maskbitslog2
= maskbitslog2
+ 2;
7797 if (bed
->s
->arch_size
== 64)
7799 if (maskbitslog2
== 5)
7805 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
7806 cinfo
.shift2
= maskbitslog2
;
7807 cinfo
.maskbits
= 1 << maskbitslog2
;
7808 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
7809 amt
= bucketcount
* sizeof (unsigned long int) * 2;
7810 amt
+= maskwords
* sizeof (bfd_vma
);
7811 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
7812 if (cinfo
.bitmask
== NULL
)
7814 free (cinfo
.hashcodes
);
7818 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
7819 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
7820 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
7821 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
7823 /* Determine how often each hash bucket is used. */
7824 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
7825 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
7826 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
7828 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
7829 if (cinfo
.counts
[i
] != 0)
7831 cinfo
.indx
[i
] = cnt
;
7832 cnt
+= cinfo
.counts
[i
];
7834 BFD_ASSERT (cnt
== dynsymcount
);
7835 cinfo
.bucketcount
= bucketcount
;
7836 cinfo
.local_indx
= cinfo
.min_dynindx
;
7838 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
7839 s
->size
+= cinfo
.maskbits
/ 8;
7840 if (bed
->record_xhash_symbol
!= NULL
)
7841 s
->size
+= cinfo
.nsyms
* 4;
7842 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7843 if (contents
== NULL
)
7845 free (cinfo
.bitmask
);
7846 free (cinfo
.hashcodes
);
7850 s
->contents
= contents
;
7851 bfd_put_32 (output_bfd
, bucketcount
, contents
);
7852 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
7853 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
7854 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
7855 contents
+= 16 + cinfo
.maskbits
/ 8;
7857 for (i
= 0; i
< bucketcount
; ++i
)
7859 if (cinfo
.counts
[i
] == 0)
7860 bfd_put_32 (output_bfd
, 0, contents
);
7862 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
7866 cinfo
.contents
= contents
;
7868 cinfo
.xlat
= contents
+ cinfo
.nsyms
* 4 - s
->contents
;
7869 /* Renumber dynamic symbols, if populating .gnu.hash section.
7870 If using .MIPS.xhash, populate the translation table. */
7871 elf_link_hash_traverse (elf_hash_table (info
),
7872 elf_gnu_hash_process_symidx
, &cinfo
);
7874 contents
= s
->contents
+ 16;
7875 for (i
= 0; i
< maskwords
; ++i
)
7877 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
7879 contents
+= bed
->s
->arch_size
/ 8;
7882 free (cinfo
.bitmask
);
7883 free (cinfo
.hashcodes
);
7887 s
= bfd_get_linker_section (dynobj
, ".dynstr");
7888 BFD_ASSERT (s
!= NULL
);
7890 elf_finalize_dynstr (output_bfd
, info
);
7892 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7894 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
7895 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
7902 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7905 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
7908 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
7909 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
7912 /* Finish SHF_MERGE section merging. */
7915 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
7920 if (!is_elf_hash_table (info
->hash
))
7923 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
7924 if ((ibfd
->flags
& DYNAMIC
) == 0
7925 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
7926 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
7927 == get_elf_backend_data (obfd
)->s
->elfclass
))
7928 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7929 if ((sec
->flags
& SEC_MERGE
) != 0
7930 && !bfd_is_abs_section (sec
->output_section
))
7932 struct bfd_elf_section_data
*secdata
;
7934 secdata
= elf_section_data (sec
);
7935 if (! _bfd_add_merge_section (obfd
,
7936 &elf_hash_table (info
)->merge_info
,
7937 sec
, &secdata
->sec_info
))
7939 else if (secdata
->sec_info
)
7940 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7943 if (elf_hash_table (info
)->merge_info
!= NULL
)
7944 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7945 merge_sections_remove_hook
);
7949 /* Create an entry in an ELF linker hash table. */
7951 struct bfd_hash_entry
*
7952 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7953 struct bfd_hash_table
*table
,
7956 /* Allocate the structure if it has not already been allocated by a
7960 entry
= (struct bfd_hash_entry
*)
7961 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7966 /* Call the allocation method of the superclass. */
7967 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7970 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7971 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7973 /* Set local fields. */
7976 ret
->got
= htab
->init_got_refcount
;
7977 ret
->plt
= htab
->init_plt_refcount
;
7978 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7979 - offsetof (struct elf_link_hash_entry
, size
)));
7980 /* Assume that we have been called by a non-ELF symbol reader.
7981 This flag is then reset by the code which reads an ELF input
7982 file. This ensures that a symbol created by a non-ELF symbol
7983 reader will have the flag set correctly. */
7990 /* Copy data from an indirect symbol to its direct symbol, hiding the
7991 old indirect symbol. Also used for copying flags to a weakdef. */
7994 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7995 struct elf_link_hash_entry
*dir
,
7996 struct elf_link_hash_entry
*ind
)
7998 struct elf_link_hash_table
*htab
;
8000 if (ind
->dyn_relocs
!= NULL
)
8002 if (dir
->dyn_relocs
!= NULL
)
8004 struct elf_dyn_relocs
**pp
;
8005 struct elf_dyn_relocs
*p
;
8007 /* Add reloc counts against the indirect sym to the direct sym
8008 list. Merge any entries against the same section. */
8009 for (pp
= &ind
->dyn_relocs
; (p
= *pp
) != NULL
; )
8011 struct elf_dyn_relocs
*q
;
8013 for (q
= dir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
8014 if (q
->sec
== p
->sec
)
8016 q
->pc_count
+= p
->pc_count
;
8017 q
->count
+= p
->count
;
8024 *pp
= dir
->dyn_relocs
;
8027 dir
->dyn_relocs
= ind
->dyn_relocs
;
8028 ind
->dyn_relocs
= NULL
;
8031 /* Copy down any references that we may have already seen to the
8032 symbol which just became indirect. */
8034 if (dir
->versioned
!= versioned_hidden
)
8035 dir
->ref_dynamic
|= ind
->ref_dynamic
;
8036 dir
->ref_regular
|= ind
->ref_regular
;
8037 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
8038 dir
->non_got_ref
|= ind
->non_got_ref
;
8039 dir
->needs_plt
|= ind
->needs_plt
;
8040 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
8042 if (ind
->root
.type
!= bfd_link_hash_indirect
)
8045 /* Copy over the global and procedure linkage table refcount entries.
8046 These may have been already set up by a check_relocs routine. */
8047 htab
= elf_hash_table (info
);
8048 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
8050 if (dir
->got
.refcount
< 0)
8051 dir
->got
.refcount
= 0;
8052 dir
->got
.refcount
+= ind
->got
.refcount
;
8053 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
8056 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
8058 if (dir
->plt
.refcount
< 0)
8059 dir
->plt
.refcount
= 0;
8060 dir
->plt
.refcount
+= ind
->plt
.refcount
;
8061 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
8064 if (ind
->dynindx
!= -1)
8066 if (dir
->dynindx
!= -1)
8067 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
8068 dir
->dynindx
= ind
->dynindx
;
8069 dir
->dynstr_index
= ind
->dynstr_index
;
8071 ind
->dynstr_index
= 0;
8076 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
8077 struct elf_link_hash_entry
*h
,
8080 /* STT_GNU_IFUNC symbol must go through PLT. */
8081 if (h
->type
!= STT_GNU_IFUNC
)
8083 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
8088 h
->forced_local
= 1;
8089 if (h
->dynindx
!= -1)
8091 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
8094 h
->dynstr_index
= 0;
8099 /* Hide a symbol. */
8102 _bfd_elf_link_hide_symbol (bfd
*output_bfd
,
8103 struct bfd_link_info
*info
,
8104 struct bfd_link_hash_entry
*h
)
8106 if (is_elf_hash_table (info
->hash
))
8108 const struct elf_backend_data
*bed
8109 = get_elf_backend_data (output_bfd
);
8110 struct elf_link_hash_entry
*eh
8111 = (struct elf_link_hash_entry
*) h
;
8112 bed
->elf_backend_hide_symbol (info
, eh
, true);
8113 eh
->def_dynamic
= 0;
8114 eh
->ref_dynamic
= 0;
8115 eh
->dynamic_def
= 0;
8119 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8123 _bfd_elf_link_hash_table_init
8124 (struct elf_link_hash_table
*table
,
8126 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
8127 struct bfd_hash_table
*,
8129 unsigned int entsize
,
8130 enum elf_target_id target_id
)
8133 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
8135 table
->init_got_refcount
.refcount
= can_refcount
- 1;
8136 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
8137 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
8138 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
8139 /* The first dynamic symbol is a dummy. */
8140 table
->dynsymcount
= 1;
8142 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
8144 table
->root
.type
= bfd_link_elf_hash_table
;
8145 table
->hash_table_id
= target_id
;
8146 table
->target_os
= get_elf_backend_data (abfd
)->target_os
;
8151 /* Create an ELF linker hash table. */
8153 struct bfd_link_hash_table
*
8154 _bfd_elf_link_hash_table_create (bfd
*abfd
)
8156 struct elf_link_hash_table
*ret
;
8157 size_t amt
= sizeof (struct elf_link_hash_table
);
8159 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
8163 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
8164 sizeof (struct elf_link_hash_entry
),
8170 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
8175 /* Destroy an ELF linker hash table. */
8178 _bfd_elf_link_hash_table_free (bfd
*obfd
)
8180 struct elf_link_hash_table
*htab
;
8182 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
8183 if (htab
->dynstr
!= NULL
)
8184 _bfd_elf_strtab_free (htab
->dynstr
);
8185 _bfd_merge_sections_free (htab
->merge_info
);
8186 _bfd_generic_link_hash_table_free (obfd
);
8189 /* This is a hook for the ELF emulation code in the generic linker to
8190 tell the backend linker what file name to use for the DT_NEEDED
8191 entry for a dynamic object. */
8194 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
8196 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8197 && bfd_get_format (abfd
) == bfd_object
)
8198 elf_dt_name (abfd
) = name
;
8202 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
8205 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8206 && bfd_get_format (abfd
) == bfd_object
)
8207 lib_class
= elf_dyn_lib_class (abfd
);
8214 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
8216 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8217 && bfd_get_format (abfd
) == bfd_object
)
8218 elf_dyn_lib_class (abfd
) = lib_class
;
8221 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8222 the linker ELF emulation code. */
8224 struct bfd_link_needed_list
*
8225 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
8226 struct bfd_link_info
*info
)
8228 if (! is_elf_hash_table (info
->hash
))
8230 return elf_hash_table (info
)->needed
;
8233 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8234 hook for the linker ELF emulation code. */
8236 struct bfd_link_needed_list
*
8237 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
8238 struct bfd_link_info
*info
)
8240 if (! is_elf_hash_table (info
->hash
))
8242 return elf_hash_table (info
)->runpath
;
8245 /* Get the name actually used for a dynamic object for a link. This
8246 is the SONAME entry if there is one. Otherwise, it is the string
8247 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8250 bfd_elf_get_dt_soname (bfd
*abfd
)
8252 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8253 && bfd_get_format (abfd
) == bfd_object
)
8254 return elf_dt_name (abfd
);
8258 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8259 the ELF linker emulation code. */
8262 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
8263 struct bfd_link_needed_list
**pneeded
)
8266 bfd_byte
*dynbuf
= NULL
;
8267 unsigned int elfsec
;
8268 unsigned long shlink
;
8269 bfd_byte
*extdyn
, *extdynend
;
8271 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
8275 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
8276 || bfd_get_format (abfd
) != bfd_object
)
8279 s
= bfd_get_section_by_name (abfd
, ".dynamic");
8280 if (s
== NULL
|| s
->size
== 0 || (s
->flags
& SEC_HAS_CONTENTS
) == 0)
8283 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
8286 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
8287 if (elfsec
== SHN_BAD
)
8290 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
8292 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
8293 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
8295 for (extdyn
= dynbuf
, extdynend
= dynbuf
+ s
->size
;
8296 (size_t) (extdynend
- extdyn
) >= extdynsize
;
8297 extdyn
+= extdynsize
)
8299 Elf_Internal_Dyn dyn
;
8301 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
8303 if (dyn
.d_tag
== DT_NULL
)
8306 if (dyn
.d_tag
== DT_NEEDED
)
8309 struct bfd_link_needed_list
*l
;
8310 unsigned int tagv
= dyn
.d_un
.d_val
;
8313 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
8318 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
8338 struct elf_symbuf_symbol
8340 unsigned long st_name
; /* Symbol name, index in string tbl */
8341 unsigned char st_info
; /* Type and binding attributes */
8342 unsigned char st_other
; /* Visibilty, and target specific */
8345 struct elf_symbuf_head
8347 struct elf_symbuf_symbol
*ssym
;
8349 unsigned int st_shndx
;
8356 Elf_Internal_Sym
*isym
;
8357 struct elf_symbuf_symbol
*ssym
;
8363 /* Sort references to symbols by ascending section number. */
8366 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
8368 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
8369 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
8371 if (s1
->st_shndx
!= s2
->st_shndx
)
8372 return s1
->st_shndx
> s2
->st_shndx
? 1 : -1;
8373 /* Final sort by the address of the sym in the symbuf ensures
8376 return s1
> s2
? 1 : -1;
8381 elf_sym_name_compare (const void *arg1
, const void *arg2
)
8383 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
8384 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
8385 int ret
= strcmp (s1
->name
, s2
->name
);
8388 if (s1
->u
.p
!= s2
->u
.p
)
8389 return s1
->u
.p
> s2
->u
.p
? 1 : -1;
8393 static struct elf_symbuf_head
*
8394 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
8396 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
8397 struct elf_symbuf_symbol
*ssym
;
8398 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
8399 size_t i
, shndx_count
, total_size
, amt
;
8401 amt
= symcount
* sizeof (*indbuf
);
8402 indbuf
= (Elf_Internal_Sym
**) bfd_malloc (amt
);
8406 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
8407 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
8408 *ind
++ = &isymbuf
[i
];
8411 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
8412 elf_sort_elf_symbol
);
8415 if (indbufend
> indbuf
)
8416 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
8417 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
8420 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
8421 + (indbufend
- indbuf
) * sizeof (*ssym
));
8422 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
8423 if (ssymbuf
== NULL
)
8429 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
8430 ssymbuf
->ssym
= NULL
;
8431 ssymbuf
->count
= shndx_count
;
8432 ssymbuf
->st_shndx
= 0;
8433 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
8435 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
8438 ssymhead
->ssym
= ssym
;
8439 ssymhead
->count
= 0;
8440 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
8442 ssym
->st_name
= (*ind
)->st_name
;
8443 ssym
->st_info
= (*ind
)->st_info
;
8444 ssym
->st_other
= (*ind
)->st_other
;
8447 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
8448 && (uintptr_t) ssym
- (uintptr_t) ssymbuf
== total_size
);
8454 /* Check if 2 sections define the same set of local and global
8458 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
8459 struct bfd_link_info
*info
)
8462 const struct elf_backend_data
*bed1
, *bed2
;
8463 Elf_Internal_Shdr
*hdr1
, *hdr2
;
8464 size_t symcount1
, symcount2
;
8465 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
8466 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
8467 Elf_Internal_Sym
*isym
, *isymend
;
8468 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
8469 size_t count1
, count2
, sec_count1
, sec_count2
, i
;
8470 unsigned int shndx1
, shndx2
;
8472 bool ignore_section_symbol_p
;
8477 /* Both sections have to be in ELF. */
8478 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
8479 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
8482 if (elf_section_type (sec1
) != elf_section_type (sec2
))
8485 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
8486 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
8487 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
8490 bed1
= get_elf_backend_data (bfd1
);
8491 bed2
= get_elf_backend_data (bfd2
);
8492 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
8493 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
8494 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
8495 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
8497 if (symcount1
== 0 || symcount2
== 0)
8503 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
8504 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
8506 /* Ignore section symbols only when matching non-debugging sections
8507 or linkonce section with comdat section. */
8508 ignore_section_symbol_p
8509 = ((sec1
->flags
& SEC_DEBUGGING
) == 0
8510 || ((elf_section_flags (sec1
) & SHF_GROUP
)
8511 != (elf_section_flags (sec2
) & SHF_GROUP
)));
8513 if (ssymbuf1
== NULL
)
8515 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
8517 if (isymbuf1
== NULL
)
8520 if (info
!= NULL
&& !info
->reduce_memory_overheads
)
8522 ssymbuf1
= elf_create_symbuf (symcount1
, isymbuf1
);
8523 elf_tdata (bfd1
)->symbuf
= ssymbuf1
;
8527 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
8529 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
8531 if (isymbuf2
== NULL
)
8534 if (ssymbuf1
!= NULL
&& info
!= NULL
&& !info
->reduce_memory_overheads
)
8536 ssymbuf2
= elf_create_symbuf (symcount2
, isymbuf2
);
8537 elf_tdata (bfd2
)->symbuf
= ssymbuf2
;
8541 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
8543 /* Optimized faster version. */
8545 struct elf_symbol
*symp
;
8546 struct elf_symbuf_symbol
*ssym
, *ssymend
;
8549 hi
= ssymbuf1
->count
;
8555 mid
= (lo
+ hi
) / 2;
8556 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
8558 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
8562 count1
= ssymbuf1
[mid
].count
;
8567 if (ignore_section_symbol_p
)
8569 for (i
= 0; i
< count1
; i
++)
8570 if (ELF_ST_TYPE (ssymbuf1
->ssym
[i
].st_info
) == STT_SECTION
)
8572 count1
-= sec_count1
;
8576 hi
= ssymbuf2
->count
;
8582 mid
= (lo
+ hi
) / 2;
8583 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
8585 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
8589 count2
= ssymbuf2
[mid
].count
;
8594 if (ignore_section_symbol_p
)
8596 for (i
= 0; i
< count2
; i
++)
8597 if (ELF_ST_TYPE (ssymbuf2
->ssym
[i
].st_info
) == STT_SECTION
)
8599 count2
-= sec_count2
;
8602 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8606 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
8608 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
8609 if (symtable1
== NULL
|| symtable2
== NULL
)
8613 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
+ sec_count1
;
8614 ssym
< ssymend
; ssym
++)
8616 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8618 symp
->u
.ssym
= ssym
;
8619 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
8626 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
+ sec_count2
;
8627 ssym
< ssymend
; ssym
++)
8629 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8631 symp
->u
.ssym
= ssym
;
8632 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
8638 /* Sort symbol by name. */
8639 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8640 elf_sym_name_compare
);
8641 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8642 elf_sym_name_compare
);
8644 for (i
= 0; i
< count1
; i
++)
8645 /* Two symbols must have the same binding, type and name. */
8646 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
8647 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
8648 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8655 symtable1
= (struct elf_symbol
*)
8656 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
8657 symtable2
= (struct elf_symbol
*)
8658 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
8659 if (symtable1
== NULL
|| symtable2
== NULL
)
8662 /* Count definitions in the section. */
8664 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
8665 if (isym
->st_shndx
== shndx1
8666 && (!ignore_section_symbol_p
8667 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8668 symtable1
[count1
++].u
.isym
= isym
;
8671 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
8672 if (isym
->st_shndx
== shndx2
8673 && (!ignore_section_symbol_p
8674 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8675 symtable2
[count2
++].u
.isym
= isym
;
8677 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8680 for (i
= 0; i
< count1
; i
++)
8682 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
8683 symtable1
[i
].u
.isym
->st_name
);
8685 for (i
= 0; i
< count2
; i
++)
8687 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
8688 symtable2
[i
].u
.isym
->st_name
);
8690 /* Sort symbol by name. */
8691 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8692 elf_sym_name_compare
);
8693 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8694 elf_sym_name_compare
);
8696 for (i
= 0; i
< count1
; i
++)
8697 /* Two symbols must have the same binding, type and name. */
8698 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
8699 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
8700 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8714 /* Return TRUE if 2 section types are compatible. */
8717 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
8718 bfd
*bbfd
, const asection
*bsec
)
8722 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
8723 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8726 return elf_section_type (asec
) == elf_section_type (bsec
);
8729 /* Final phase of ELF linker. */
8731 /* A structure we use to avoid passing large numbers of arguments. */
8733 struct elf_final_link_info
8735 /* General link information. */
8736 struct bfd_link_info
*info
;
8739 /* Symbol string table. */
8740 struct elf_strtab_hash
*symstrtab
;
8741 /* .hash section. */
8743 /* symbol version section (.gnu.version). */
8744 asection
*symver_sec
;
8745 /* Buffer large enough to hold contents of any section. */
8747 /* Buffer large enough to hold external relocs of any section. */
8748 void *external_relocs
;
8749 /* Buffer large enough to hold internal relocs of any section. */
8750 Elf_Internal_Rela
*internal_relocs
;
8751 /* Buffer large enough to hold external local symbols of any input
8753 bfd_byte
*external_syms
;
8754 /* And a buffer for symbol section indices. */
8755 Elf_External_Sym_Shndx
*locsym_shndx
;
8756 /* Buffer large enough to hold internal local symbols of any input
8758 Elf_Internal_Sym
*internal_syms
;
8759 /* Array large enough to hold a symbol index for each local symbol
8760 of any input BFD. */
8762 /* Array large enough to hold a section pointer for each local
8763 symbol of any input BFD. */
8764 asection
**sections
;
8765 /* Buffer for SHT_SYMTAB_SHNDX section. */
8766 Elf_External_Sym_Shndx
*symshndxbuf
;
8767 /* Number of STT_FILE syms seen. */
8768 size_t filesym_count
;
8769 /* Local symbol hash table. */
8770 struct bfd_hash_table local_hash_table
;
8773 struct local_hash_entry
8775 /* Base hash table entry structure. */
8776 struct bfd_hash_entry root
;
8777 /* Size of the local symbol name. */
8779 /* Number of the duplicated local symbol names. */
8783 /* Create an entry in the local symbol hash table. */
8785 static struct bfd_hash_entry
*
8786 local_hash_newfunc (struct bfd_hash_entry
*entry
,
8787 struct bfd_hash_table
*table
,
8791 /* Allocate the structure if it has not already been allocated by a
8795 entry
= bfd_hash_allocate (table
,
8796 sizeof (struct local_hash_entry
));
8801 /* Call the allocation method of the superclass. */
8802 entry
= bfd_hash_newfunc (entry
, table
, string
);
8805 ((struct local_hash_entry
*) entry
)->count
= 0;
8806 ((struct local_hash_entry
*) entry
)->size
= 0;
8812 /* This struct is used to pass information to elf_link_output_extsym. */
8814 struct elf_outext_info
8819 struct elf_final_link_info
*flinfo
;
8823 /* Support for evaluating a complex relocation.
8825 Complex relocations are generalized, self-describing relocations. The
8826 implementation of them consists of two parts: complex symbols, and the
8827 relocations themselves.
8829 The relocations use a reserved elf-wide relocation type code (R_RELC
8830 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8831 information (start bit, end bit, word width, etc) into the addend. This
8832 information is extracted from CGEN-generated operand tables within gas.
8834 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8835 internal) representing prefix-notation expressions, including but not
8836 limited to those sorts of expressions normally encoded as addends in the
8837 addend field. The symbol mangling format is:
8840 | <unary-operator> ':' <node>
8841 | <binary-operator> ':' <node> ':' <node>
8844 <literal> := 's' <digits=N> ':' <N character symbol name>
8845 | 'S' <digits=N> ':' <N character section name>
8849 <binary-operator> := as in C
8850 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8853 set_symbol_value (bfd
*bfd_with_globals
,
8854 Elf_Internal_Sym
*isymbuf
,
8859 struct elf_link_hash_entry
**sym_hashes
;
8860 struct elf_link_hash_entry
*h
;
8861 size_t extsymoff
= locsymcount
;
8863 if (symidx
< locsymcount
)
8865 Elf_Internal_Sym
*sym
;
8867 sym
= isymbuf
+ symidx
;
8868 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
8870 /* It is a local symbol: move it to the
8871 "absolute" section and give it a value. */
8872 sym
->st_shndx
= SHN_ABS
;
8873 sym
->st_value
= val
;
8876 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
8880 /* It is a global symbol: set its link type
8881 to "defined" and give it a value. */
8883 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
8884 h
= sym_hashes
[symidx
- extsymoff
];
8885 while (h
->root
.type
== bfd_link_hash_indirect
8886 || h
->root
.type
== bfd_link_hash_warning
)
8887 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8888 h
->root
.type
= bfd_link_hash_defined
;
8889 h
->root
.u
.def
.value
= val
;
8890 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
8894 resolve_symbol (const char *name
,
8896 struct elf_final_link_info
*flinfo
,
8898 Elf_Internal_Sym
*isymbuf
,
8901 Elf_Internal_Sym
*sym
;
8902 struct bfd_link_hash_entry
*global_entry
;
8903 const char *candidate
= NULL
;
8904 Elf_Internal_Shdr
*symtab_hdr
;
8907 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
8909 for (i
= 0; i
< locsymcount
; ++ i
)
8913 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
8916 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
8917 symtab_hdr
->sh_link
,
8920 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8921 name
, candidate
, (unsigned long) sym
->st_value
);
8923 if (candidate
&& strcmp (candidate
, name
) == 0)
8925 asection
*sec
= flinfo
->sections
[i
];
8927 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
8928 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
8930 printf ("Found symbol with value %8.8lx\n",
8931 (unsigned long) *result
);
8937 /* Hmm, haven't found it yet. perhaps it is a global. */
8938 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
8939 false, false, true);
8943 if (global_entry
->type
== bfd_link_hash_defined
8944 || global_entry
->type
== bfd_link_hash_defweak
)
8946 *result
= (global_entry
->u
.def
.value
8947 + global_entry
->u
.def
.section
->output_section
->vma
8948 + global_entry
->u
.def
.section
->output_offset
);
8950 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8951 global_entry
->root
.string
, (unsigned long) *result
);
8959 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8960 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8961 names like "foo.end" which is the end address of section "foo". */
8964 resolve_section (const char *name
,
8972 for (curr
= sections
; curr
; curr
= curr
->next
)
8973 if (strcmp (curr
->name
, name
) == 0)
8975 *result
= curr
->vma
;
8979 /* Hmm. still haven't found it. try pseudo-section names. */
8980 /* FIXME: This could be coded more efficiently... */
8981 for (curr
= sections
; curr
; curr
= curr
->next
)
8983 len
= strlen (curr
->name
);
8984 if (len
> strlen (name
))
8987 if (strncmp (curr
->name
, name
, len
) == 0)
8989 if (startswith (name
+ len
, ".end"))
8991 *result
= (curr
->vma
8992 + curr
->size
/ bfd_octets_per_byte (abfd
, curr
));
8996 /* Insert more pseudo-section names here, if you like. */
9004 undefined_reference (const char *reftype
, const char *name
)
9006 /* xgettext:c-format */
9007 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
9009 bfd_set_error (bfd_error_bad_value
);
9013 eval_symbol (bfd_vma
*result
,
9016 struct elf_final_link_info
*flinfo
,
9018 Elf_Internal_Sym
*isymbuf
,
9027 const char *sym
= *symp
;
9029 bool symbol_is_section
= false;
9034 if (len
< 1 || len
> sizeof (symbuf
))
9036 bfd_set_error (bfd_error_invalid_operation
);
9049 *result
= strtoul (sym
, (char **) symp
, 16);
9053 symbol_is_section
= true;
9057 symlen
= strtol (sym
, (char **) symp
, 10);
9058 sym
= *symp
+ 1; /* Skip the trailing ':'. */
9060 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
9062 bfd_set_error (bfd_error_invalid_operation
);
9066 memcpy (symbuf
, sym
, symlen
);
9067 symbuf
[symlen
] = '\0';
9068 *symp
= sym
+ symlen
;
9070 /* Is it always possible, with complex symbols, that gas "mis-guessed"
9071 the symbol as a section, or vice-versa. so we're pretty liberal in our
9072 interpretation here; section means "try section first", not "must be a
9073 section", and likewise with symbol. */
9075 if (symbol_is_section
)
9077 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
9078 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
9079 isymbuf
, locsymcount
))
9081 undefined_reference ("section", symbuf
);
9087 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
9088 isymbuf
, locsymcount
)
9089 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
9092 undefined_reference ("symbol", symbuf
);
9099 /* All that remains are operators. */
9101 #define UNARY_OP(op) \
9102 if (startswith (sym, #op)) \
9104 sym += strlen (#op); \
9108 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9109 isymbuf, locsymcount, signed_p)) \
9112 *result = op ((bfd_signed_vma) a); \
9118 #define BINARY_OP_HEAD(op) \
9119 if (startswith (sym, #op)) \
9121 sym += strlen (#op); \
9125 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9126 isymbuf, locsymcount, signed_p)) \
9129 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9130 isymbuf, locsymcount, signed_p)) \
9132 #define BINARY_OP_TAIL(op) \
9134 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9139 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9143 BINARY_OP_HEAD (<<);
9144 if (b
>= sizeof (a
) * CHAR_BIT
)
9150 BINARY_OP_TAIL (<<);
9151 BINARY_OP_HEAD (>>);
9152 if (b
>= sizeof (a
) * CHAR_BIT
)
9154 *result
= signed_p
&& (bfd_signed_vma
) a
< 0 ? -1 : 0;
9157 BINARY_OP_TAIL (>>);
9170 _bfd_error_handler (_("division by zero"));
9171 bfd_set_error (bfd_error_bad_value
);
9178 _bfd_error_handler (_("division by zero"));
9179 bfd_set_error (bfd_error_bad_value
);
9192 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
9193 bfd_set_error (bfd_error_invalid_operation
);
9199 put_value (bfd_vma size
,
9200 unsigned long chunksz
,
9205 location
+= (size
- chunksz
);
9207 for (; size
; size
-= chunksz
, location
-= chunksz
)
9212 bfd_put_8 (input_bfd
, x
, location
);
9216 bfd_put_16 (input_bfd
, x
, location
);
9220 bfd_put_32 (input_bfd
, x
, location
);
9221 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9227 bfd_put_64 (input_bfd
, x
, location
);
9228 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9241 get_value (bfd_vma size
,
9242 unsigned long chunksz
,
9249 /* Sanity checks. */
9250 BFD_ASSERT (chunksz
<= sizeof (x
)
9253 && (size
% chunksz
) == 0
9254 && input_bfd
!= NULL
9255 && location
!= NULL
);
9257 if (chunksz
== sizeof (x
))
9259 BFD_ASSERT (size
== chunksz
);
9261 /* Make sure that we do not perform an undefined shift operation.
9262 We know that size == chunksz so there will only be one iteration
9263 of the loop below. */
9267 shift
= 8 * chunksz
;
9269 for (; size
; size
-= chunksz
, location
+= chunksz
)
9274 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
9277 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
9280 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
9284 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
9295 decode_complex_addend (unsigned long *start
, /* in bits */
9296 unsigned long *oplen
, /* in bits */
9297 unsigned long *len
, /* in bits */
9298 unsigned long *wordsz
, /* in bytes */
9299 unsigned long *chunksz
, /* in bytes */
9300 unsigned long *lsb0_p
,
9301 unsigned long *signed_p
,
9302 unsigned long *trunc_p
,
9303 unsigned long encoded
)
9305 * start
= encoded
& 0x3F;
9306 * len
= (encoded
>> 6) & 0x3F;
9307 * oplen
= (encoded
>> 12) & 0x3F;
9308 * wordsz
= (encoded
>> 18) & 0xF;
9309 * chunksz
= (encoded
>> 22) & 0xF;
9310 * lsb0_p
= (encoded
>> 27) & 1;
9311 * signed_p
= (encoded
>> 28) & 1;
9312 * trunc_p
= (encoded
>> 29) & 1;
9315 bfd_reloc_status_type
9316 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
9317 asection
*input_section
,
9319 Elf_Internal_Rela
*rel
,
9322 bfd_vma shift
, x
, mask
;
9323 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
9324 bfd_reloc_status_type r
;
9325 bfd_size_type octets
;
9327 /* Perform this reloc, since it is complex.
9328 (this is not to say that it necessarily refers to a complex
9329 symbol; merely that it is a self-describing CGEN based reloc.
9330 i.e. the addend has the complete reloc information (bit start, end,
9331 word size, etc) encoded within it.). */
9333 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
9334 &chunksz
, &lsb0_p
, &signed_p
,
9335 &trunc_p
, rel
->r_addend
);
9337 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
9340 shift
= (start
+ 1) - len
;
9342 shift
= (8 * wordsz
) - (start
+ len
);
9344 octets
= rel
->r_offset
* bfd_octets_per_byte (input_bfd
, input_section
);
9345 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ octets
);
9348 printf ("Doing complex reloc: "
9349 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9350 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9351 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9352 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
9353 oplen
, (unsigned long) x
, (unsigned long) mask
,
9354 (unsigned long) relocation
);
9359 /* Now do an overflow check. */
9360 r
= bfd_check_overflow ((signed_p
9361 ? complain_overflow_signed
9362 : complain_overflow_unsigned
),
9363 len
, 0, (8 * wordsz
),
9367 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
9370 printf (" relocation: %8.8lx\n"
9371 " shifted mask: %8.8lx\n"
9372 " shifted/masked reloc: %8.8lx\n"
9373 " result: %8.8lx\n",
9374 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
9375 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
9377 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ octets
);
9381 /* Functions to read r_offset from external (target order) reloc
9382 entry. Faster than bfd_getl32 et al, because we let the compiler
9383 know the value is aligned. */
9386 ext32l_r_offset (const void *p
)
9393 const union aligned32
*a
9394 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9396 uint32_t aval
= ( (uint32_t) a
->c
[0]
9397 | (uint32_t) a
->c
[1] << 8
9398 | (uint32_t) a
->c
[2] << 16
9399 | (uint32_t) a
->c
[3] << 24);
9404 ext32b_r_offset (const void *p
)
9411 const union aligned32
*a
9412 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9414 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
9415 | (uint32_t) a
->c
[1] << 16
9416 | (uint32_t) a
->c
[2] << 8
9417 | (uint32_t) a
->c
[3]);
9422 ext64l_r_offset (const void *p
)
9429 const union aligned64
*a
9430 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9432 uint64_t aval
= ( (uint64_t) a
->c
[0]
9433 | (uint64_t) a
->c
[1] << 8
9434 | (uint64_t) a
->c
[2] << 16
9435 | (uint64_t) a
->c
[3] << 24
9436 | (uint64_t) a
->c
[4] << 32
9437 | (uint64_t) a
->c
[5] << 40
9438 | (uint64_t) a
->c
[6] << 48
9439 | (uint64_t) a
->c
[7] << 56);
9444 ext64b_r_offset (const void *p
)
9451 const union aligned64
*a
9452 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9454 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
9455 | (uint64_t) a
->c
[1] << 48
9456 | (uint64_t) a
->c
[2] << 40
9457 | (uint64_t) a
->c
[3] << 32
9458 | (uint64_t) a
->c
[4] << 24
9459 | (uint64_t) a
->c
[5] << 16
9460 | (uint64_t) a
->c
[6] << 8
9461 | (uint64_t) a
->c
[7]);
9465 /* When performing a relocatable link, the input relocations are
9466 preserved. But, if they reference global symbols, the indices
9467 referenced must be updated. Update all the relocations found in
9471 elf_link_adjust_relocs (bfd
*abfd
,
9473 struct bfd_elf_section_reloc_data
*reldata
,
9475 struct bfd_link_info
*info
)
9478 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9480 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9481 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9482 bfd_vma r_type_mask
;
9484 unsigned int count
= reldata
->count
;
9485 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
9487 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
9489 swap_in
= bed
->s
->swap_reloc_in
;
9490 swap_out
= bed
->s
->swap_reloc_out
;
9492 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
9494 swap_in
= bed
->s
->swap_reloca_in
;
9495 swap_out
= bed
->s
->swap_reloca_out
;
9500 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
9503 if (bed
->s
->arch_size
== 32)
9510 r_type_mask
= 0xffffffff;
9514 erela
= reldata
->hdr
->contents
;
9515 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
9517 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
9520 if (*rel_hash
== NULL
)
9523 if ((*rel_hash
)->indx
== -2
9524 && info
->gc_sections
9525 && ! info
->gc_keep_exported
)
9527 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9528 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9530 (*rel_hash
)->root
.root
.string
);
9531 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9533 bfd_set_error (bfd_error_invalid_operation
);
9536 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
9538 (*swap_in
) (abfd
, erela
, irela
);
9539 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
9540 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
9541 | (irela
[j
].r_info
& r_type_mask
));
9542 (*swap_out
) (abfd
, irela
, erela
);
9545 if (bed
->elf_backend_update_relocs
)
9546 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
9548 if (sort
&& count
!= 0)
9550 bfd_vma (*ext_r_off
) (const void *);
9553 bfd_byte
*base
, *end
, *p
, *loc
;
9554 bfd_byte
*buf
= NULL
;
9556 if (bed
->s
->arch_size
== 32)
9558 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9559 ext_r_off
= ext32l_r_offset
;
9560 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9561 ext_r_off
= ext32b_r_offset
;
9567 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9568 ext_r_off
= ext64l_r_offset
;
9569 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9570 ext_r_off
= ext64b_r_offset
;
9575 /* Must use a stable sort here. A modified insertion sort,
9576 since the relocs are mostly sorted already. */
9577 elt_size
= reldata
->hdr
->sh_entsize
;
9578 base
= reldata
->hdr
->contents
;
9579 end
= base
+ count
* elt_size
;
9580 if (elt_size
> sizeof (Elf64_External_Rela
))
9583 /* Ensure the first element is lowest. This acts as a sentinel,
9584 speeding the main loop below. */
9585 r_off
= (*ext_r_off
) (base
);
9586 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
9588 bfd_vma r_off2
= (*ext_r_off
) (p
);
9597 /* Don't just swap *base and *loc as that changes the order
9598 of the original base[0] and base[1] if they happen to
9599 have the same r_offset. */
9600 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
9601 memcpy (onebuf
, loc
, elt_size
);
9602 memmove (base
+ elt_size
, base
, loc
- base
);
9603 memcpy (base
, onebuf
, elt_size
);
9606 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
9608 /* base to p is sorted, *p is next to insert. */
9609 r_off
= (*ext_r_off
) (p
);
9610 /* Search the sorted region for location to insert. */
9612 while (r_off
< (*ext_r_off
) (loc
))
9617 /* Chances are there is a run of relocs to insert here,
9618 from one of more input files. Files are not always
9619 linked in order due to the way elf_link_input_bfd is
9620 called. See pr17666. */
9621 size_t sortlen
= p
- loc
;
9622 bfd_vma r_off2
= (*ext_r_off
) (loc
);
9623 size_t runlen
= elt_size
;
9624 bfd_vma r_off_runend
= r_off
;
9625 bfd_vma r_off_runend_next
;
9626 size_t buf_size
= 96 * 1024;
9627 while (p
+ runlen
< end
9628 && (sortlen
<= buf_size
9629 || runlen
+ elt_size
<= buf_size
)
9630 /* run must not break the ordering of base..loc+1 */
9631 && r_off2
> (r_off_runend_next
= (*ext_r_off
) (p
+ runlen
))
9632 /* run must be already sorted */
9633 && r_off_runend_next
>= r_off_runend
)
9636 r_off_runend
= r_off_runend_next
;
9640 buf
= bfd_malloc (buf_size
);
9644 if (runlen
< sortlen
)
9646 memcpy (buf
, p
, runlen
);
9647 memmove (loc
+ runlen
, loc
, sortlen
);
9648 memcpy (loc
, buf
, runlen
);
9652 memcpy (buf
, loc
, sortlen
);
9653 memmove (loc
, p
, runlen
);
9654 memcpy (loc
+ runlen
, buf
, sortlen
);
9656 p
+= runlen
- elt_size
;
9659 /* Hashes are no longer valid. */
9660 free (reldata
->hashes
);
9661 reldata
->hashes
= NULL
;
9667 struct elf_link_sort_rela
9673 enum elf_reloc_type_class type
;
9674 /* We use this as an array of size int_rels_per_ext_rel. */
9675 Elf_Internal_Rela rela
[1];
9678 /* qsort stability here and for cmp2 is only an issue if multiple
9679 dynamic relocations are emitted at the same address. But targets
9680 that apply a series of dynamic relocations each operating on the
9681 result of the prior relocation can't use -z combreloc as
9682 implemented anyway. Such schemes tend to be broken by sorting on
9683 symbol index. That leaves dynamic NONE relocs as the only other
9684 case where ld might emit multiple relocs at the same address, and
9685 those are only emitted due to target bugs. */
9688 elf_link_sort_cmp1 (const void *A
, const void *B
)
9690 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9691 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9692 int relativea
, relativeb
;
9694 relativea
= a
->type
== reloc_class_relative
;
9695 relativeb
= b
->type
== reloc_class_relative
;
9697 if (relativea
< relativeb
)
9699 if (relativea
> relativeb
)
9701 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
9703 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
9705 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9707 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9713 elf_link_sort_cmp2 (const void *A
, const void *B
)
9715 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9716 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9718 if (a
->type
< b
->type
)
9720 if (a
->type
> b
->type
)
9722 if (a
->u
.offset
< b
->u
.offset
)
9724 if (a
->u
.offset
> b
->u
.offset
)
9726 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9728 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9734 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
9736 asection
*dynamic_relocs
;
9739 bfd_size_type count
, size
;
9740 size_t i
, ret
, sort_elt
, ext_size
;
9741 bfd_byte
*sort
, *s_non_relative
, *p
;
9742 struct elf_link_sort_rela
*sq
;
9743 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9744 int i2e
= bed
->s
->int_rels_per_ext_rel
;
9745 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
9746 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9747 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9748 struct bfd_link_order
*lo
;
9752 /* Find a dynamic reloc section. */
9753 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
9754 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
9755 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
9756 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9758 bool use_rela_initialised
= false;
9760 /* This is just here to stop gcc from complaining.
9761 Its initialization checking code is not perfect. */
9764 /* Both sections are present. Examine the sizes
9765 of the indirect sections to help us choose. */
9766 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9767 if (lo
->type
== bfd_indirect_link_order
)
9769 asection
*o
= lo
->u
.indirect
.section
;
9771 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9773 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9774 /* Section size is divisible by both rel and rela sizes.
9775 It is of no help to us. */
9779 /* Section size is only divisible by rela. */
9780 if (use_rela_initialised
&& !use_rela
)
9782 _bfd_error_handler (_("%pB: unable to sort relocs - "
9783 "they are in more than one size"),
9785 bfd_set_error (bfd_error_invalid_operation
);
9791 use_rela_initialised
= true;
9795 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9797 /* Section size is only divisible by rel. */
9798 if (use_rela_initialised
&& use_rela
)
9800 _bfd_error_handler (_("%pB: unable to sort relocs - "
9801 "they are in more than one size"),
9803 bfd_set_error (bfd_error_invalid_operation
);
9809 use_rela_initialised
= true;
9814 /* The section size is not divisible by either -
9815 something is wrong. */
9816 _bfd_error_handler (_("%pB: unable to sort relocs - "
9817 "they are of an unknown size"), abfd
);
9818 bfd_set_error (bfd_error_invalid_operation
);
9823 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9824 if (lo
->type
== bfd_indirect_link_order
)
9826 asection
*o
= lo
->u
.indirect
.section
;
9828 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9830 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9831 /* Section size is divisible by both rel and rela sizes.
9832 It is of no help to us. */
9836 /* Section size is only divisible by rela. */
9837 if (use_rela_initialised
&& !use_rela
)
9839 _bfd_error_handler (_("%pB: unable to sort relocs - "
9840 "they are in more than one size"),
9842 bfd_set_error (bfd_error_invalid_operation
);
9848 use_rela_initialised
= true;
9852 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9854 /* Section size is only divisible by rel. */
9855 if (use_rela_initialised
&& use_rela
)
9857 _bfd_error_handler (_("%pB: unable to sort relocs - "
9858 "they are in more than one size"),
9860 bfd_set_error (bfd_error_invalid_operation
);
9866 use_rela_initialised
= true;
9871 /* The section size is not divisible by either -
9872 something is wrong. */
9873 _bfd_error_handler (_("%pB: unable to sort relocs - "
9874 "they are of an unknown size"), abfd
);
9875 bfd_set_error (bfd_error_invalid_operation
);
9880 if (! use_rela_initialised
)
9884 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
9886 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9893 dynamic_relocs
= rela_dyn
;
9894 ext_size
= bed
->s
->sizeof_rela
;
9895 swap_in
= bed
->s
->swap_reloca_in
;
9896 swap_out
= bed
->s
->swap_reloca_out
;
9900 dynamic_relocs
= rel_dyn
;
9901 ext_size
= bed
->s
->sizeof_rel
;
9902 swap_in
= bed
->s
->swap_reloc_in
;
9903 swap_out
= bed
->s
->swap_reloc_out
;
9907 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9908 if (lo
->type
== bfd_indirect_link_order
)
9909 size
+= lo
->u
.indirect
.section
->size
;
9911 if (size
!= dynamic_relocs
->size
)
9914 sort_elt
= (sizeof (struct elf_link_sort_rela
)
9915 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
9917 count
= dynamic_relocs
->size
/ ext_size
;
9920 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
9924 (*info
->callbacks
->warning
)
9925 (info
, _("not enough memory to sort relocations"), 0, abfd
, 0, 0);
9929 if (bed
->s
->arch_size
== 32)
9930 r_sym_mask
= ~(bfd_vma
) 0xff;
9932 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
9934 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9935 if (lo
->type
== bfd_indirect_link_order
)
9937 bfd_byte
*erel
, *erelend
;
9938 asection
*o
= lo
->u
.indirect
.section
;
9940 if (o
->contents
== NULL
&& o
->size
!= 0)
9942 /* This is a reloc section that is being handled as a normal
9943 section. See bfd_section_from_shdr. We can't combine
9944 relocs in this case. */
9949 erelend
= o
->contents
+ o
->size
;
9950 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
9952 while (erel
< erelend
)
9954 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9956 (*swap_in
) (abfd
, erel
, s
->rela
);
9957 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
9958 s
->u
.sym_mask
= r_sym_mask
;
9964 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
9966 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
9968 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9969 if (s
->type
!= reloc_class_relative
)
9975 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
9976 for (; i
< count
; i
++, p
+= sort_elt
)
9978 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
9979 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
9981 sp
->u
.offset
= sq
->rela
->r_offset
;
9984 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
9986 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
9987 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
9989 /* We have plt relocs in .rela.dyn. */
9990 sq
= (struct elf_link_sort_rela
*) sort
;
9991 for (i
= 0; i
< count
; i
++)
9992 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
9994 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
9996 struct bfd_link_order
**plo
;
9997 /* Put srelplt link_order last. This is so the output_offset
9998 set in the next loop is correct for DT_JMPREL. */
9999 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
10000 if ((*plo
)->type
== bfd_indirect_link_order
10001 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
10007 plo
= &(*plo
)->next
;
10010 dynamic_relocs
->map_tail
.link_order
= lo
;
10015 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
10016 if (lo
->type
== bfd_indirect_link_order
)
10018 bfd_byte
*erel
, *erelend
;
10019 asection
*o
= lo
->u
.indirect
.section
;
10021 erel
= o
->contents
;
10022 erelend
= o
->contents
+ o
->size
;
10023 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
10024 while (erel
< erelend
)
10026 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
10027 (*swap_out
) (abfd
, s
->rela
, erel
);
10034 *psec
= dynamic_relocs
;
10038 /* Add a symbol to the output symbol string table. */
10041 elf_link_output_symstrtab (void *finf
,
10043 Elf_Internal_Sym
*elfsym
,
10044 asection
*input_sec
,
10045 struct elf_link_hash_entry
*h
)
10047 struct elf_final_link_info
*flinfo
= finf
;
10048 int (*output_symbol_hook
)
10049 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
10050 struct elf_link_hash_entry
*);
10051 struct elf_link_hash_table
*hash_table
;
10052 const struct elf_backend_data
*bed
;
10053 bfd_size_type strtabsize
;
10055 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
10057 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10058 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
10059 if (output_symbol_hook
!= NULL
)
10061 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
10066 if (ELF_ST_TYPE (elfsym
->st_info
) == STT_GNU_IFUNC
)
10067 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_ifunc
;
10068 if (ELF_ST_BIND (elfsym
->st_info
) == STB_GNU_UNIQUE
)
10069 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_unique
;
10071 if (name
== NULL
|| *name
== '\0')
10072 elfsym
->st_name
= (unsigned long) -1;
10075 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10076 to get the final offset for st_name. */
10077 char *versioned_name
= (char *) name
;
10080 if (h
->versioned
== versioned
&& h
->def_dynamic
)
10082 /* Keep only one '@' for versioned symbols defined in
10084 char *version
= strrchr (name
, ELF_VER_CHR
);
10085 char *base_end
= strchr (name
, ELF_VER_CHR
);
10086 if (version
!= base_end
)
10089 size_t len
= strlen (name
);
10090 versioned_name
= bfd_alloc (flinfo
->output_bfd
, len
);
10091 if (versioned_name
== NULL
)
10093 base_len
= base_end
- name
;
10094 memcpy (versioned_name
, name
, base_len
);
10095 memcpy (versioned_name
+ base_len
, version
,
10100 else if (flinfo
->info
->unique_symbol
10101 && ELF_ST_BIND (elfsym
->st_info
) == STB_LOCAL
)
10103 struct local_hash_entry
*lh
;
10107 switch (ELF_ST_TYPE (elfsym
->st_info
))
10113 lh
= (struct local_hash_entry
*) bfd_hash_lookup
10114 (&flinfo
->local_hash_table
, name
, true, false);
10117 /* Always append ".COUNT" to local symbols to avoid
10118 potential conflicts with local symbol "XXX.COUNT". */
10119 sprintf (buf
, "%lx", lh
->count
);
10120 base_len
= lh
->size
;
10123 base_len
= strlen (name
);
10124 lh
->size
= base_len
;
10126 count_len
= strlen (buf
);
10127 versioned_name
= bfd_alloc (flinfo
->output_bfd
,
10128 base_len
+ count_len
+ 2);
10129 if (versioned_name
== NULL
)
10131 memcpy (versioned_name
, name
, base_len
);
10132 versioned_name
[base_len
] = '.';
10133 memcpy (versioned_name
+ base_len
+ 1, buf
,
10140 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
10141 versioned_name
, false);
10142 if (elfsym
->st_name
== (unsigned long) -1)
10146 hash_table
= elf_hash_table (flinfo
->info
);
10147 strtabsize
= hash_table
->strtabsize
;
10148 if (strtabsize
<= flinfo
->output_bfd
->symcount
)
10150 strtabsize
+= strtabsize
;
10151 hash_table
->strtabsize
= strtabsize
;
10152 strtabsize
*= sizeof (*hash_table
->strtab
);
10154 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
10156 if (hash_table
->strtab
== NULL
)
10159 hash_table
->strtab
[flinfo
->output_bfd
->symcount
].sym
= *elfsym
;
10160 hash_table
->strtab
[flinfo
->output_bfd
->symcount
].dest_index
10161 = flinfo
->output_bfd
->symcount
;
10162 flinfo
->output_bfd
->symcount
+= 1;
10167 /* Swap symbols out to the symbol table and flush the output symbols to
10171 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
10173 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
10176 const struct elf_backend_data
*bed
;
10178 Elf_Internal_Shdr
*hdr
;
10182 if (flinfo
->output_bfd
->symcount
== 0)
10185 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
10187 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10189 amt
= bed
->s
->sizeof_sym
* flinfo
->output_bfd
->symcount
;
10190 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
10191 if (symbuf
== NULL
)
10194 if (flinfo
->symshndxbuf
)
10196 amt
= sizeof (Elf_External_Sym_Shndx
);
10197 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
10198 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
10199 if (flinfo
->symshndxbuf
== NULL
)
10206 /* Now swap out the symbols. */
10207 for (i
= 0; i
< flinfo
->output_bfd
->symcount
; i
++)
10209 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
10210 if (elfsym
->sym
.st_name
== (unsigned long) -1)
10211 elfsym
->sym
.st_name
= 0;
10213 elfsym
->sym
.st_name
10214 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
10215 elfsym
->sym
.st_name
);
10217 /* Inform the linker of the addition of this symbol. */
10219 if (flinfo
->info
->callbacks
->ctf_new_symbol
)
10220 flinfo
->info
->callbacks
->ctf_new_symbol (elfsym
->dest_index
,
10223 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
10224 ((bfd_byte
*) symbuf
10225 + (elfsym
->dest_index
10226 * bed
->s
->sizeof_sym
)),
10227 NPTR_ADD (flinfo
->symshndxbuf
,
10228 elfsym
->dest_index
));
10231 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
10232 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
10233 amt
= bed
->s
->sizeof_sym
* flinfo
->output_bfd
->symcount
;
10234 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
10235 && bfd_write (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
10237 hdr
->sh_size
+= amt
;
10245 free (hash_table
->strtab
);
10246 hash_table
->strtab
= NULL
;
10251 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10254 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
10256 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
10257 && sym
->st_shndx
< SHN_LORESERVE
)
10259 /* The gABI doesn't support dynamic symbols in output sections
10262 /* xgettext:c-format */
10263 (_("%pB: too many sections: %d (>= %d)"),
10264 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
10265 bfd_set_error (bfd_error_nonrepresentable_section
);
10271 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10272 allowing an unsatisfied unversioned symbol in the DSO to match a
10273 versioned symbol that would normally require an explicit version.
10274 We also handle the case that a DSO references a hidden symbol
10275 which may be satisfied by a versioned symbol in another DSO. */
10278 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
10279 const struct elf_backend_data
*bed
,
10280 struct elf_link_hash_entry
*h
)
10283 struct elf_link_loaded_list
*loaded
;
10285 if (!is_elf_hash_table (info
->hash
))
10288 /* Check indirect symbol. */
10289 while (h
->root
.type
== bfd_link_hash_indirect
)
10290 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10292 switch (h
->root
.type
)
10298 case bfd_link_hash_undefined
:
10299 case bfd_link_hash_undefweak
:
10300 abfd
= h
->root
.u
.undef
.abfd
;
10302 || (abfd
->flags
& DYNAMIC
) == 0
10303 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
10307 case bfd_link_hash_defined
:
10308 case bfd_link_hash_defweak
:
10309 abfd
= h
->root
.u
.def
.section
->owner
;
10312 case bfd_link_hash_common
:
10313 abfd
= h
->root
.u
.c
.p
->section
->owner
;
10316 BFD_ASSERT (abfd
!= NULL
);
10318 for (loaded
= elf_hash_table (info
)->dyn_loaded
;
10320 loaded
= loaded
->next
)
10323 Elf_Internal_Shdr
*hdr
;
10325 size_t extsymcount
;
10327 Elf_Internal_Shdr
*versymhdr
;
10328 Elf_Internal_Sym
*isym
;
10329 Elf_Internal_Sym
*isymend
;
10330 Elf_Internal_Sym
*isymbuf
;
10331 Elf_External_Versym
*ever
;
10332 Elf_External_Versym
*extversym
;
10334 input
= loaded
->abfd
;
10336 /* We check each DSO for a possible hidden versioned definition. */
10338 || elf_dynversym (input
) == 0)
10341 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
10343 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10344 if (elf_bad_symtab (input
))
10346 extsymcount
= symcount
;
10351 extsymcount
= symcount
- hdr
->sh_info
;
10352 extsymoff
= hdr
->sh_info
;
10355 if (extsymcount
== 0)
10358 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
10360 if (isymbuf
== NULL
)
10363 /* Read in any version definitions. */
10364 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
10365 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
10366 || (extversym
= (Elf_External_Versym
*)
10367 _bfd_malloc_and_read (input
, versymhdr
->sh_size
,
10368 versymhdr
->sh_size
)) == NULL
)
10374 ever
= extversym
+ extsymoff
;
10375 isymend
= isymbuf
+ extsymcount
;
10376 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
10379 Elf_Internal_Versym iver
;
10380 unsigned short version_index
;
10382 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
10383 || isym
->st_shndx
== SHN_UNDEF
)
10386 name
= bfd_elf_string_from_elf_section (input
,
10389 if (strcmp (name
, h
->root
.root
.string
) != 0)
10392 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
10394 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
10395 && !(h
->def_regular
10396 && h
->forced_local
))
10398 /* If we have a non-hidden versioned sym, then it should
10399 have provided a definition for the undefined sym unless
10400 it is defined in a non-shared object and forced local.
10405 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
10406 if (version_index
== 1 || version_index
== 2)
10408 /* This is the base or first version. We can use it. */
10422 /* Convert ELF common symbol TYPE. */
10425 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
10427 /* Commom symbol can only appear in relocatable link. */
10428 if (!bfd_link_relocatable (info
))
10430 switch (info
->elf_stt_common
)
10434 case elf_stt_common
:
10437 case no_elf_stt_common
:
10444 /* Add an external symbol to the symbol table. This is called from
10445 the hash table traversal routine. When generating a shared object,
10446 we go through the symbol table twice. The first time we output
10447 anything that might have been forced to local scope in a version
10448 script. The second time we output the symbols that are still
10452 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
10454 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
10455 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
10456 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
10458 Elf_Internal_Sym sym
;
10459 asection
*input_sec
;
10460 const struct elf_backend_data
*bed
;
10465 if (h
->root
.type
== bfd_link_hash_warning
)
10467 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10468 if (h
->root
.type
== bfd_link_hash_new
)
10472 /* Decide whether to output this symbol in this pass. */
10473 if (eoinfo
->localsyms
)
10475 if (!h
->forced_local
)
10480 if (h
->forced_local
)
10484 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10486 if (h
->root
.type
== bfd_link_hash_undefined
)
10488 /* If we have an undefined symbol reference here then it must have
10489 come from a shared library that is being linked in. (Undefined
10490 references in regular files have already been handled unless
10491 they are in unreferenced sections which are removed by garbage
10493 bool ignore_undef
= false;
10495 /* Some symbols may be special in that the fact that they're
10496 undefined can be safely ignored - let backend determine that. */
10497 if (bed
->elf_backend_ignore_undef_symbol
)
10498 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
10500 /* If we are reporting errors for this situation then do so now. */
10502 && h
->ref_dynamic_nonweak
10503 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
10504 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
10505 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
10507 flinfo
->info
->callbacks
->undefined_symbol
10508 (flinfo
->info
, h
->root
.root
.string
,
10509 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
, NULL
, 0,
10510 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_DIAGNOSE
10511 && !flinfo
->info
->warn_unresolved_syms
);
10514 /* Strip a global symbol defined in a discarded section. */
10519 /* We should also warn if a forced local symbol is referenced from
10520 shared libraries. */
10521 if (bfd_link_executable (flinfo
->info
)
10526 && h
->ref_dynamic_nonweak
10527 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
10531 struct elf_link_hash_entry
*hi
= h
;
10533 /* Check indirect symbol. */
10534 while (hi
->root
.type
== bfd_link_hash_indirect
)
10535 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
10537 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
10538 /* xgettext:c-format */
10539 msg
= _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10540 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
10541 /* xgettext:c-format */
10542 msg
= _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10544 /* xgettext:c-format */
10545 msg
= _("%pB: local symbol `%s' in %pB is referenced by DSO");
10546 def_bfd
= flinfo
->output_bfd
;
10547 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
10548 def_bfd
= hi
->root
.u
.def
.section
->owner
;
10549 _bfd_error_handler (msg
, flinfo
->output_bfd
,
10550 h
->root
.root
.string
, def_bfd
);
10551 bfd_set_error (bfd_error_bad_value
);
10552 eoinfo
->failed
= true;
10556 /* We don't want to output symbols that have never been mentioned by
10557 a regular file, or that we have been told to strip. However, if
10558 h->indx is set to -2, the symbol is used by a reloc and we must
10563 else if ((h
->def_dynamic
10565 || h
->root
.type
== bfd_link_hash_new
)
10567 && !h
->ref_regular
)
10569 else if (flinfo
->info
->strip
== strip_all
)
10571 else if (flinfo
->info
->strip
== strip_some
10572 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
10573 h
->root
.root
.string
, false, false) == NULL
)
10575 else if ((h
->root
.type
== bfd_link_hash_defined
10576 || h
->root
.type
== bfd_link_hash_defweak
)
10577 && ((flinfo
->info
->strip_discarded
10578 && discarded_section (h
->root
.u
.def
.section
))
10579 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
10580 && h
->root
.u
.def
.section
->owner
!= NULL
10581 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
10583 else if ((h
->root
.type
== bfd_link_hash_undefined
10584 || h
->root
.type
== bfd_link_hash_undefweak
)
10585 && h
->root
.u
.undef
.abfd
!= NULL
10586 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
10591 /* If we're stripping it, and it's not a dynamic symbol, there's
10592 nothing else to do. However, if it is a forced local symbol or
10593 an ifunc symbol we need to give the backend finish_dynamic_symbol
10594 function a chance to make it dynamic. */
10596 && h
->dynindx
== -1
10597 && type
!= STT_GNU_IFUNC
10598 && !h
->forced_local
)
10602 sym
.st_size
= h
->size
;
10603 sym
.st_other
= h
->other
;
10604 switch (h
->root
.type
)
10607 case bfd_link_hash_new
:
10608 case bfd_link_hash_warning
:
10612 case bfd_link_hash_undefined
:
10613 case bfd_link_hash_undefweak
:
10614 input_sec
= bfd_und_section_ptr
;
10615 sym
.st_shndx
= SHN_UNDEF
;
10618 case bfd_link_hash_defined
:
10619 case bfd_link_hash_defweak
:
10621 input_sec
= h
->root
.u
.def
.section
;
10622 if (input_sec
->output_section
!= NULL
)
10625 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
10626 input_sec
->output_section
);
10627 if (sym
.st_shndx
== SHN_BAD
)
10630 /* xgettext:c-format */
10631 (_("%pB: could not find output section %pA for input section %pA"),
10632 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
10633 bfd_set_error (bfd_error_nonrepresentable_section
);
10634 eoinfo
->failed
= true;
10638 /* ELF symbols in relocatable files are section relative,
10639 but in nonrelocatable files they are virtual
10641 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
10642 if (!bfd_link_relocatable (flinfo
->info
))
10644 sym
.st_value
+= input_sec
->output_section
->vma
;
10645 if (h
->type
== STT_TLS
)
10647 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
10648 if (tls_sec
!= NULL
)
10649 sym
.st_value
-= tls_sec
->vma
;
10655 BFD_ASSERT (input_sec
->owner
== NULL
10656 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
10657 sym
.st_shndx
= SHN_UNDEF
;
10658 input_sec
= bfd_und_section_ptr
;
10663 case bfd_link_hash_common
:
10664 input_sec
= h
->root
.u
.c
.p
->section
;
10665 sym
.st_shndx
= bed
->common_section_index (input_sec
);
10666 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
10669 case bfd_link_hash_indirect
:
10670 /* These symbols are created by symbol versioning. They point
10671 to the decorated version of the name. For example, if the
10672 symbol foo@@GNU_1.2 is the default, which should be used when
10673 foo is used with no version, then we add an indirect symbol
10674 foo which points to foo@@GNU_1.2. We ignore these symbols,
10675 since the indirected symbol is already in the hash table. */
10679 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
10680 switch (h
->root
.type
)
10682 case bfd_link_hash_common
:
10683 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10685 case bfd_link_hash_defined
:
10686 case bfd_link_hash_defweak
:
10687 if (bed
->common_definition (&sym
))
10688 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10692 case bfd_link_hash_undefined
:
10693 case bfd_link_hash_undefweak
:
10699 if (h
->forced_local
)
10701 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
10702 /* Turn off visibility on local symbol. */
10703 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
10705 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10706 else if (h
->unique_global
&& h
->def_regular
)
10707 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
10708 else if (h
->root
.type
== bfd_link_hash_undefweak
10709 || h
->root
.type
== bfd_link_hash_defweak
)
10710 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
10712 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
10713 sym
.st_target_internal
= h
->target_internal
;
10715 /* Give the processor backend a chance to tweak the symbol value,
10716 and also to finish up anything that needs to be done for this
10717 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10718 forced local syms when non-shared is due to a historical quirk.
10719 STT_GNU_IFUNC symbol must go through PLT. */
10720 if ((h
->type
== STT_GNU_IFUNC
10722 && !bfd_link_relocatable (flinfo
->info
))
10723 || ((h
->dynindx
!= -1
10724 || h
->forced_local
)
10725 && ((bfd_link_pic (flinfo
->info
)
10726 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
10727 || h
->root
.type
!= bfd_link_hash_undefweak
))
10728 || !h
->forced_local
)
10729 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
10731 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
10732 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
10734 eoinfo
->failed
= true;
10739 /* If we are marking the symbol as undefined, and there are no
10740 non-weak references to this symbol from a regular object, then
10741 mark the symbol as weak undefined; if there are non-weak
10742 references, mark the symbol as strong. We can't do this earlier,
10743 because it might not be marked as undefined until the
10744 finish_dynamic_symbol routine gets through with it. */
10745 if (sym
.st_shndx
== SHN_UNDEF
10747 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
10748 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
10751 type
= ELF_ST_TYPE (sym
.st_info
);
10753 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10754 if (type
== STT_GNU_IFUNC
)
10757 if (h
->ref_regular_nonweak
)
10758 bindtype
= STB_GLOBAL
;
10760 bindtype
= STB_WEAK
;
10761 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
10764 /* If this is a symbol defined in a dynamic library, don't use the
10765 symbol size from the dynamic library. Relinking an executable
10766 against a new library may introduce gratuitous changes in the
10767 executable's symbols if we keep the size. */
10768 if (sym
.st_shndx
== SHN_UNDEF
10773 /* If a non-weak symbol with non-default visibility is not defined
10774 locally, it is a fatal error. */
10775 if (!bfd_link_relocatable (flinfo
->info
)
10776 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
10777 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
10778 && h
->root
.type
== bfd_link_hash_undefined
10779 && !h
->def_regular
)
10783 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
10784 /* xgettext:c-format */
10785 msg
= _("%pB: protected symbol `%s' isn't defined");
10786 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
10787 /* xgettext:c-format */
10788 msg
= _("%pB: internal symbol `%s' isn't defined");
10790 /* xgettext:c-format */
10791 msg
= _("%pB: hidden symbol `%s' isn't defined");
10792 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
10793 bfd_set_error (bfd_error_bad_value
);
10794 eoinfo
->failed
= true;
10798 /* If this symbol should be put in the .dynsym section, then put it
10799 there now. We already know the symbol index. We also fill in
10800 the entry in the .hash section. */
10801 if (h
->dynindx
!= -1
10802 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
10803 && elf_hash_table (flinfo
->info
)->dynsym
!= NULL
10804 && !discarded_section (elf_hash_table (flinfo
->info
)->dynsym
))
10808 /* Since there is no version information in the dynamic string,
10809 if there is no version info in symbol version section, we will
10810 have a run-time problem if not linking executable, referenced
10811 by shared library, or not bound locally. */
10812 if (h
->verinfo
.verdef
== NULL
10813 && (!bfd_link_executable (flinfo
->info
)
10815 || !h
->def_regular
))
10817 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
10819 if (p
&& p
[1] != '\0')
10822 /* xgettext:c-format */
10823 (_("%pB: no symbol version section for versioned symbol `%s'"),
10824 flinfo
->output_bfd
, h
->root
.root
.string
);
10825 eoinfo
->failed
= true;
10830 sym
.st_name
= h
->dynstr_index
;
10831 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
10832 + h
->dynindx
* bed
->s
->sizeof_sym
);
10833 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
10835 eoinfo
->failed
= true;
10839 /* Inform the linker of the addition of this symbol. */
10841 if (flinfo
->info
->callbacks
->ctf_new_dynsym
)
10842 flinfo
->info
->callbacks
->ctf_new_dynsym (h
->dynindx
, &sym
);
10844 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
10846 if (flinfo
->hash_sec
!= NULL
)
10848 size_t hash_entry_size
;
10849 bfd_byte
*bucketpos
;
10851 size_t bucketcount
;
10854 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
10855 bucket
= h
->u
.elf_hash_value
% bucketcount
;
10858 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
10859 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
10860 + (bucket
+ 2) * hash_entry_size
);
10861 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
10862 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
10864 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
10865 ((bfd_byte
*) flinfo
->hash_sec
->contents
10866 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
10869 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
10871 Elf_Internal_Versym iversym
;
10872 Elf_External_Versym
*eversym
;
10874 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
10876 if (h
->verinfo
.verdef
== NULL
10877 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
10878 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
10879 iversym
.vs_vers
= 1;
10881 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
10885 if (h
->verinfo
.vertree
== NULL
)
10886 iversym
.vs_vers
= 1;
10888 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
10889 if (flinfo
->info
->create_default_symver
)
10893 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10894 defined locally. */
10895 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
10896 iversym
.vs_vers
|= VERSYM_HIDDEN
;
10898 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
10899 eversym
+= h
->dynindx
;
10900 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
10904 /* If the symbol is undefined, and we didn't output it to .dynsym,
10905 strip it from .symtab too. Obviously we can't do this for
10906 relocatable output or when needed for --emit-relocs. */
10907 else if (input_sec
== bfd_und_section_ptr
10909 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10910 && (h
->mark
!= 1 || ELF_ST_BIND (sym
.st_info
) != STB_GLOBAL
)
10911 && !bfd_link_relocatable (flinfo
->info
))
10914 /* Also strip others that we couldn't earlier due to dynamic symbol
10918 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
10921 /* Output a FILE symbol so that following locals are not associated
10922 with the wrong input file. We need one for forced local symbols
10923 if we've seen more than one FILE symbol or when we have exactly
10924 one FILE symbol but global symbols are present in a file other
10925 than the one with the FILE symbol. We also need one if linker
10926 defined symbols are present. In practice these conditions are
10927 always met, so just emit the FILE symbol unconditionally. */
10928 if (eoinfo
->localsyms
10929 && !eoinfo
->file_sym_done
10930 && eoinfo
->flinfo
->filesym_count
!= 0)
10932 Elf_Internal_Sym fsym
;
10934 memset (&fsym
, 0, sizeof (fsym
));
10935 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10936 fsym
.st_shndx
= SHN_ABS
;
10937 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
10938 bfd_und_section_ptr
, NULL
))
10941 eoinfo
->file_sym_done
= true;
10944 indx
= bfd_get_symcount (flinfo
->output_bfd
);
10945 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
10949 eoinfo
->failed
= true;
10954 else if (h
->indx
== -2)
10960 /* Return TRUE if special handling is done for relocs in SEC against
10961 symbols defined in discarded sections. */
10964 elf_section_ignore_discarded_relocs (asection
*sec
)
10966 const struct elf_backend_data
*bed
;
10968 switch (sec
->sec_info_type
)
10970 case SEC_INFO_TYPE_STABS
:
10971 case SEC_INFO_TYPE_EH_FRAME
:
10972 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10973 case SEC_INFO_TYPE_SFRAME
:
10979 bed
= get_elf_backend_data (sec
->owner
);
10980 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
10981 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
10987 /* Return a mask saying how ld should treat relocations in SEC against
10988 symbols defined in discarded sections. If this function returns
10989 COMPLAIN set, ld will issue a warning message. If this function
10990 returns PRETEND set, and the discarded section was link-once and the
10991 same size as the kept link-once section, ld will pretend that the
10992 symbol was actually defined in the kept section. Otherwise ld will
10993 zero the reloc (at least that is the intent, but some cooperation by
10994 the target dependent code is needed, particularly for REL targets). */
10997 _bfd_elf_default_action_discarded (asection
*sec
)
10999 const struct elf_backend_data
*bed
;
11000 bed
= get_elf_backend_data (sec
->owner
);
11002 if (sec
->flags
& SEC_DEBUGGING
)
11005 if (strcmp (".eh_frame", sec
->name
) == 0)
11008 if (bed
->elf_backend_can_make_multiple_eh_frame
11009 && strncmp (sec
->name
, ".eh_frame.", 10) == 0)
11012 if (strcmp (".sframe", sec
->name
) == 0)
11015 if (strcmp (".gcc_except_table", sec
->name
) == 0)
11018 return COMPLAIN
| PRETEND
;
11021 /* Find a match between a section and a member of a section group. */
11024 match_group_member (asection
*sec
, asection
*group
,
11025 struct bfd_link_info
*info
)
11027 asection
*first
= elf_next_in_group (group
);
11028 asection
*s
= first
;
11032 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
11035 s
= elf_next_in_group (s
);
11043 /* Check if the kept section of a discarded section SEC can be used
11044 to replace it. Return the replacement if it is OK. Otherwise return
11048 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
11052 kept
= sec
->kept_section
;
11055 if ((kept
->flags
& SEC_GROUP
) != 0)
11056 kept
= match_group_member (sec
, kept
, info
);
11059 if ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
11060 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
))
11064 /* Get the real kept section. */
11066 for (next
= kept
->kept_section
;
11068 next
= next
->kept_section
)
11072 sec
->kept_section
= kept
;
11077 /* Link an input file into the linker output file. This function
11078 handles all the sections and relocations of the input file at once.
11079 This is so that we only have to read the local symbols once, and
11080 don't have to keep them in memory. */
11083 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
11085 int (*relocate_section
)
11086 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
11087 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
11089 Elf_Internal_Shdr
*symtab_hdr
;
11090 size_t locsymcount
;
11092 Elf_Internal_Sym
*isymbuf
;
11093 Elf_Internal_Sym
*isym
;
11094 Elf_Internal_Sym
*isymend
;
11096 asection
**ppsection
;
11098 const struct elf_backend_data
*bed
;
11099 struct elf_link_hash_entry
**sym_hashes
;
11100 bfd_size_type address_size
;
11101 bfd_vma r_type_mask
;
11103 bool have_file_sym
= false;
11105 output_bfd
= flinfo
->output_bfd
;
11106 bed
= get_elf_backend_data (output_bfd
);
11107 relocate_section
= bed
->elf_backend_relocate_section
;
11109 /* If this is a dynamic object, we don't want to do anything here:
11110 we don't want the local symbols, and we don't want the section
11112 if ((input_bfd
->flags
& DYNAMIC
) != 0)
11115 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
11116 if (elf_bad_symtab (input_bfd
))
11118 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
11123 locsymcount
= symtab_hdr
->sh_info
;
11124 extsymoff
= symtab_hdr
->sh_info
;
11127 /* Enable GNU OSABI features in the output BFD that are used in the input
11129 if (bed
->elf_osabi
== ELFOSABI_NONE
11130 || bed
->elf_osabi
== ELFOSABI_GNU
11131 || bed
->elf_osabi
== ELFOSABI_FREEBSD
)
11132 elf_tdata (output_bfd
)->has_gnu_osabi
11133 |= (elf_tdata (input_bfd
)->has_gnu_osabi
11134 & (bfd_link_relocatable (flinfo
->info
)
11135 ? -1 : ~elf_gnu_osabi_retain
));
11137 /* Read the local symbols. */
11138 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
11139 if (isymbuf
== NULL
&& locsymcount
!= 0)
11141 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
11142 flinfo
->internal_syms
,
11143 flinfo
->external_syms
,
11144 flinfo
->locsym_shndx
);
11145 if (isymbuf
== NULL
)
11149 /* Find local symbol sections and adjust values of symbols in
11150 SEC_MERGE sections. Write out those local symbols we know are
11151 going into the output file. */
11152 isymend
= PTR_ADD (isymbuf
, locsymcount
);
11153 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
11155 isym
++, pindex
++, ppsection
++)
11159 Elf_Internal_Sym osym
;
11165 if (elf_bad_symtab (input_bfd
))
11167 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
11174 if (isym
->st_shndx
== SHN_UNDEF
)
11175 isec
= bfd_und_section_ptr
;
11176 else if (isym
->st_shndx
== SHN_ABS
)
11177 isec
= bfd_abs_section_ptr
;
11178 else if (isym
->st_shndx
== SHN_COMMON
)
11179 isec
= bfd_com_section_ptr
;
11182 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
11185 /* Don't attempt to output symbols with st_shnx in the
11186 reserved range other than SHN_ABS and SHN_COMMON. */
11187 isec
= bfd_und_section_ptr
;
11189 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
11190 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
11192 _bfd_merged_section_offset (output_bfd
, &isec
,
11193 elf_section_data (isec
)->sec_info
,
11199 /* Don't output the first, undefined, symbol. In fact, don't
11200 output any undefined local symbol. */
11201 if (isec
== bfd_und_section_ptr
)
11204 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
11206 /* We never output section symbols. Instead, we use the
11207 section symbol of the corresponding section in the output
11212 /* If we are stripping all symbols, we don't want to output this
11214 if (flinfo
->info
->strip
== strip_all
)
11217 /* If we are discarding all local symbols, we don't want to
11218 output this one. If we are generating a relocatable output
11219 file, then some of the local symbols may be required by
11220 relocs; we output them below as we discover that they are
11222 if (flinfo
->info
->discard
== discard_all
)
11225 /* If this symbol is defined in a section which we are
11226 discarding, we don't need to keep it. */
11227 if (isym
->st_shndx
< SHN_LORESERVE
11228 && (isec
->output_section
== NULL
11229 || bfd_section_removed_from_list (output_bfd
,
11230 isec
->output_section
)))
11233 /* Get the name of the symbol. */
11234 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
11239 /* See if we are discarding symbols with this name. */
11240 if ((flinfo
->info
->strip
== strip_some
11241 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, false, false)
11243 || (((flinfo
->info
->discard
== discard_sec_merge
11244 && (isec
->flags
& SEC_MERGE
)
11245 && !bfd_link_relocatable (flinfo
->info
))
11246 || flinfo
->info
->discard
== discard_l
)
11247 && bfd_is_local_label_name (input_bfd
, name
)))
11250 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
11252 if (input_bfd
->lto_output
)
11253 /* -flto puts a temp file name here. This means builds
11254 are not reproducible. Discard the symbol. */
11256 have_file_sym
= true;
11257 flinfo
->filesym_count
+= 1;
11259 if (!have_file_sym
)
11261 /* In the absence of debug info, bfd_find_nearest_line uses
11262 FILE symbols to determine the source file for local
11263 function symbols. Provide a FILE symbol here if input
11264 files lack such, so that their symbols won't be
11265 associated with a previous input file. It's not the
11266 source file, but the best we can do. */
11267 const char *filename
;
11268 have_file_sym
= true;
11269 flinfo
->filesym_count
+= 1;
11270 memset (&osym
, 0, sizeof (osym
));
11271 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
11272 osym
.st_shndx
= SHN_ABS
;
11273 if (input_bfd
->lto_output
)
11276 filename
= lbasename (bfd_get_filename (input_bfd
));
11277 if (!elf_link_output_symstrtab (flinfo
, filename
, &osym
,
11278 bfd_abs_section_ptr
, NULL
))
11284 /* Adjust the section index for the output file. */
11285 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11286 isec
->output_section
);
11287 if (osym
.st_shndx
== SHN_BAD
)
11290 /* ELF symbols in relocatable files are section relative, but
11291 in executable files they are virtual addresses. Note that
11292 this code assumes that all ELF sections have an associated
11293 BFD section with a reasonable value for output_offset; below
11294 we assume that they also have a reasonable value for
11295 output_section. Any special sections must be set up to meet
11296 these requirements. */
11297 osym
.st_value
+= isec
->output_offset
;
11298 if (!bfd_link_relocatable (flinfo
->info
))
11300 osym
.st_value
+= isec
->output_section
->vma
;
11301 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
11303 /* STT_TLS symbols are relative to PT_TLS segment base. */
11304 if (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
)
11305 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
11307 osym
.st_info
= ELF_ST_INFO (ELF_ST_BIND (osym
.st_info
),
11312 indx
= bfd_get_symcount (output_bfd
);
11313 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
11320 if (bed
->s
->arch_size
== 32)
11322 r_type_mask
= 0xff;
11328 r_type_mask
= 0xffffffff;
11333 /* Relocate the contents of each section. */
11334 sym_hashes
= elf_sym_hashes (input_bfd
);
11335 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
11337 bfd_byte
*contents
;
11339 if (! o
->linker_mark
)
11341 /* This section was omitted from the link. */
11345 if (!flinfo
->info
->resolve_section_groups
11346 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
11348 /* Deal with the group signature symbol. */
11349 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
11350 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
11351 asection
*osec
= o
->output_section
;
11353 BFD_ASSERT (bfd_link_relocatable (flinfo
->info
));
11354 if (symndx
>= locsymcount
11355 || (elf_bad_symtab (input_bfd
)
11356 && flinfo
->sections
[symndx
] == NULL
))
11358 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
11359 while (h
->root
.type
== bfd_link_hash_indirect
11360 || h
->root
.type
== bfd_link_hash_warning
)
11361 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11362 /* Arrange for symbol to be output. */
11364 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
11366 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
11368 /* We'll use the output section target_index. */
11369 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11370 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
11374 if (flinfo
->indices
[symndx
] == -1)
11376 /* Otherwise output the local symbol now. */
11377 Elf_Internal_Sym sym
= isymbuf
[symndx
];
11378 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11383 name
= bfd_elf_string_from_elf_section (input_bfd
,
11384 symtab_hdr
->sh_link
,
11389 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11391 if (sym
.st_shndx
== SHN_BAD
)
11394 sym
.st_value
+= o
->output_offset
;
11396 indx
= bfd_get_symcount (output_bfd
);
11397 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
11402 flinfo
->indices
[symndx
] = indx
;
11406 elf_section_data (osec
)->this_hdr
.sh_info
11407 = flinfo
->indices
[symndx
];
11411 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
11412 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
11415 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
11417 /* Section was created by _bfd_elf_link_create_dynamic_sections
11422 /* Get the contents of the section. They have been cached by a
11423 relaxation routine. Note that o is a section in an input
11424 file, so the contents field will not have been set by any of
11425 the routines which work on output files. */
11426 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
11428 contents
= elf_section_data (o
)->this_hdr
.contents
;
11429 if (bed
->caches_rawsize
11431 && o
->rawsize
< o
->size
)
11433 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
11434 contents
= flinfo
->contents
;
11437 else if (!(o
->flags
& SEC_RELOC
)
11438 && !bed
->elf_backend_write_section
11439 && o
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
11440 /* A MERGE section that has no relocations doesn't need the
11441 contents anymore, they have been recorded earlier. Except
11442 if the backend has special provisions for writing sections. */
11446 contents
= flinfo
->contents
;
11447 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
11451 if ((o
->flags
& SEC_RELOC
) != 0)
11453 Elf_Internal_Rela
*internal_relocs
;
11454 Elf_Internal_Rela
*rel
, *relend
;
11455 int action_discarded
;
11458 /* Get the swapped relocs. */
11460 = _bfd_elf_link_info_read_relocs (input_bfd
, flinfo
->info
, o
,
11461 flinfo
->external_relocs
,
11462 flinfo
->internal_relocs
,
11464 if (internal_relocs
== NULL
11465 && o
->reloc_count
> 0)
11468 action_discarded
= -1;
11469 if (!elf_section_ignore_discarded_relocs (o
))
11470 action_discarded
= (*bed
->action_discarded
) (o
);
11472 /* Run through the relocs evaluating complex reloc symbols and
11473 looking for relocs against symbols from discarded sections
11474 or section symbols from removed link-once sections.
11475 Complain about relocs against discarded sections. Zero
11476 relocs against removed link-once sections. */
11478 rel
= internal_relocs
;
11479 relend
= rel
+ o
->reloc_count
;
11480 for ( ; rel
< relend
; rel
++)
11482 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
11483 unsigned int s_type
;
11484 asection
**ps
, *sec
;
11485 struct elf_link_hash_entry
*h
= NULL
;
11486 const char *sym_name
;
11488 if (r_symndx
== STN_UNDEF
)
11491 if (r_symndx
>= locsymcount
11492 || (elf_bad_symtab (input_bfd
)
11493 && flinfo
->sections
[r_symndx
] == NULL
))
11495 h
= sym_hashes
[r_symndx
- extsymoff
];
11497 /* Badly formatted input files can contain relocs that
11498 reference non-existant symbols. Check here so that
11499 we do not seg fault. */
11503 /* xgettext:c-format */
11504 (_("error: %pB contains a reloc (%#" PRIx64
") for section %pA "
11505 "that references a non-existent global symbol"),
11506 input_bfd
, (uint64_t) rel
->r_info
, o
);
11507 bfd_set_error (bfd_error_bad_value
);
11511 while (h
->root
.type
== bfd_link_hash_indirect
11512 || h
->root
.type
== bfd_link_hash_warning
)
11513 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11517 /* If a plugin symbol is referenced from a non-IR file,
11518 mark the symbol as undefined. Note that the
11519 linker may attach linker created dynamic sections
11520 to the plugin bfd. Symbols defined in linker
11521 created sections are not plugin symbols. */
11522 if ((h
->root
.non_ir_ref_regular
11523 || h
->root
.non_ir_ref_dynamic
)
11524 && (h
->root
.type
== bfd_link_hash_defined
11525 || h
->root
.type
== bfd_link_hash_defweak
)
11526 && (h
->root
.u
.def
.section
->flags
11527 & SEC_LINKER_CREATED
) == 0
11528 && h
->root
.u
.def
.section
->owner
!= NULL
11529 && (h
->root
.u
.def
.section
->owner
->flags
11530 & BFD_PLUGIN
) != 0)
11532 h
->root
.type
= bfd_link_hash_undefined
;
11533 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
11537 if (h
->root
.type
== bfd_link_hash_defined
11538 || h
->root
.type
== bfd_link_hash_defweak
)
11539 ps
= &h
->root
.u
.def
.section
;
11541 sym_name
= h
->root
.root
.string
;
11545 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
11547 s_type
= ELF_ST_TYPE (sym
->st_info
);
11548 ps
= &flinfo
->sections
[r_symndx
];
11549 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
11553 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
11554 && !bfd_link_relocatable (flinfo
->info
))
11557 bfd_vma dot
= (rel
->r_offset
11558 + o
->output_offset
+ o
->output_section
->vma
);
11560 printf ("Encountered a complex symbol!");
11561 printf (" (input_bfd %s, section %s, reloc %ld\n",
11562 bfd_get_filename (input_bfd
), o
->name
,
11563 (long) (rel
- internal_relocs
));
11564 printf (" symbol: idx %8.8lx, name %s\n",
11565 r_symndx
, sym_name
);
11566 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11567 (unsigned long) rel
->r_info
,
11568 (unsigned long) rel
->r_offset
);
11570 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
11571 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
11574 /* Symbol evaluated OK. Update to absolute value. */
11575 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
11580 if (action_discarded
!= -1 && ps
!= NULL
)
11582 /* Complain if the definition comes from a
11583 discarded section. */
11584 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
11586 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
11587 if (action_discarded
& COMPLAIN
)
11588 (*flinfo
->info
->callbacks
->einfo
)
11589 /* xgettext:c-format */
11590 (_("%X`%s' referenced in section `%pA' of %pB: "
11591 "defined in discarded section `%pA' of %pB\n"),
11592 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
11594 /* Try to do the best we can to support buggy old
11595 versions of gcc. Pretend that the symbol is
11596 really defined in the kept linkonce section.
11597 FIXME: This is quite broken. Modifying the
11598 symbol here means we will be changing all later
11599 uses of the symbol, not just in this section. */
11600 if (action_discarded
& PRETEND
)
11604 kept
= _bfd_elf_check_kept_section (sec
,
11616 /* Relocate the section by invoking a back end routine.
11618 The back end routine is responsible for adjusting the
11619 section contents as necessary, and (if using Rela relocs
11620 and generating a relocatable output file) adjusting the
11621 reloc addend as necessary.
11623 The back end routine does not have to worry about setting
11624 the reloc address or the reloc symbol index.
11626 The back end routine is given a pointer to the swapped in
11627 internal symbols, and can access the hash table entries
11628 for the external symbols via elf_sym_hashes (input_bfd).
11630 When generating relocatable output, the back end routine
11631 must handle STB_LOCAL/STT_SECTION symbols specially. The
11632 output symbol is going to be a section symbol
11633 corresponding to the output section, which will require
11634 the addend to be adjusted. */
11636 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
11637 input_bfd
, o
, contents
,
11645 || bfd_link_relocatable (flinfo
->info
)
11646 || flinfo
->info
->emitrelocations
)
11648 Elf_Internal_Rela
*irela
;
11649 Elf_Internal_Rela
*irelaend
, *irelamid
;
11650 bfd_vma last_offset
;
11651 struct elf_link_hash_entry
**rel_hash
;
11652 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
11653 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
11654 unsigned int next_erel
;
11656 struct bfd_elf_section_data
*esdi
, *esdo
;
11658 esdi
= elf_section_data (o
);
11659 esdo
= elf_section_data (o
->output_section
);
11660 rela_normal
= false;
11662 /* Adjust the reloc addresses and symbol indices. */
11664 irela
= internal_relocs
;
11665 irelaend
= irela
+ o
->reloc_count
;
11666 rel_hash
= PTR_ADD (esdo
->rel
.hashes
, esdo
->rel
.count
);
11667 /* We start processing the REL relocs, if any. When we reach
11668 IRELAMID in the loop, we switch to the RELA relocs. */
11670 if (esdi
->rel
.hdr
!= NULL
)
11671 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
11672 * bed
->s
->int_rels_per_ext_rel
);
11673 rel_hash_list
= rel_hash
;
11674 rela_hash_list
= NULL
;
11675 last_offset
= o
->output_offset
;
11676 if (!bfd_link_relocatable (flinfo
->info
))
11677 last_offset
+= o
->output_section
->vma
;
11678 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
11680 unsigned long r_symndx
;
11682 Elf_Internal_Sym sym
;
11684 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
11690 if (irela
== irelamid
)
11692 rel_hash
= PTR_ADD (esdo
->rela
.hashes
, esdo
->rela
.count
);
11693 rela_hash_list
= rel_hash
;
11694 if (bed
->is_rela_normal
!= NULL
)
11695 rela_normal
= bed
->is_rela_normal (irela
);
11697 rela_normal
= bed
->rela_normal
;
11700 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
11703 if (irela
->r_offset
>= (bfd_vma
) -2)
11705 /* This is a reloc for a deleted entry or somesuch.
11706 Turn it into an R_*_NONE reloc, at the same
11707 offset as the last reloc. elf_eh_frame.c and
11708 bfd_elf_discard_info rely on reloc offsets
11710 irela
->r_offset
= last_offset
;
11712 irela
->r_addend
= 0;
11716 irela
->r_offset
+= o
->output_offset
;
11718 /* Relocs in an executable have to be virtual addresses. */
11719 if (!bfd_link_relocatable (flinfo
->info
))
11720 irela
->r_offset
+= o
->output_section
->vma
;
11722 last_offset
= irela
->r_offset
;
11724 r_symndx
= irela
->r_info
>> r_sym_shift
;
11725 if (r_symndx
== STN_UNDEF
)
11728 if (r_symndx
>= locsymcount
11729 || (elf_bad_symtab (input_bfd
)
11730 && flinfo
->sections
[r_symndx
] == NULL
))
11732 struct elf_link_hash_entry
*rh
;
11733 unsigned long indx
;
11735 /* This is a reloc against a global symbol. We
11736 have not yet output all the local symbols, so
11737 we do not know the symbol index of any global
11738 symbol. We set the rel_hash entry for this
11739 reloc to point to the global hash table entry
11740 for this symbol. The symbol index is then
11741 set at the end of bfd_elf_final_link. */
11742 indx
= r_symndx
- extsymoff
;
11743 rh
= elf_sym_hashes (input_bfd
)[indx
];
11744 while (rh
->root
.type
== bfd_link_hash_indirect
11745 || rh
->root
.type
== bfd_link_hash_warning
)
11746 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
11748 /* Setting the index to -2 tells
11749 elf_link_output_extsym that this symbol is
11750 used by a reloc. */
11751 BFD_ASSERT (rh
->indx
< 0);
11758 /* This is a reloc against a local symbol. */
11761 sym
= isymbuf
[r_symndx
];
11762 sec
= flinfo
->sections
[r_symndx
];
11763 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
11765 /* I suppose the backend ought to fill in the
11766 section of any STT_SECTION symbol against a
11767 processor specific section. */
11768 r_symndx
= STN_UNDEF
;
11769 if (bfd_is_abs_section (sec
))
11771 else if (sec
== NULL
|| sec
->owner
== NULL
)
11773 bfd_set_error (bfd_error_bad_value
);
11778 asection
*osec
= sec
->output_section
;
11780 /* If we have discarded a section, the output
11781 section will be the absolute section. In
11782 case of discarded SEC_MERGE sections, use
11783 the kept section. relocate_section should
11784 have already handled discarded linkonce
11786 if (bfd_is_abs_section (osec
)
11787 && sec
->kept_section
!= NULL
11788 && sec
->kept_section
->output_section
!= NULL
)
11790 osec
= sec
->kept_section
->output_section
;
11791 irela
->r_addend
-= osec
->vma
;
11794 if (!bfd_is_abs_section (osec
))
11796 r_symndx
= osec
->target_index
;
11797 if (r_symndx
== STN_UNDEF
)
11799 irela
->r_addend
+= osec
->vma
;
11800 osec
= _bfd_nearby_section (output_bfd
, osec
,
11802 irela
->r_addend
-= osec
->vma
;
11803 r_symndx
= osec
->target_index
;
11808 /* Adjust the addend according to where the
11809 section winds up in the output section. */
11811 irela
->r_addend
+= sec
->output_offset
;
11815 if (flinfo
->indices
[r_symndx
] == -1)
11817 unsigned long shlink
;
11822 if (flinfo
->info
->strip
== strip_all
)
11824 /* You can't do ld -r -s. */
11825 bfd_set_error (bfd_error_invalid_operation
);
11829 /* This symbol was skipped earlier, but
11830 since it is needed by a reloc, we
11831 must output it now. */
11832 shlink
= symtab_hdr
->sh_link
;
11833 name
= (bfd_elf_string_from_elf_section
11834 (input_bfd
, shlink
, sym
.st_name
));
11838 osec
= sec
->output_section
;
11840 _bfd_elf_section_from_bfd_section (output_bfd
,
11842 if (sym
.st_shndx
== SHN_BAD
)
11845 sym
.st_value
+= sec
->output_offset
;
11846 if (!bfd_link_relocatable (flinfo
->info
))
11848 sym
.st_value
+= osec
->vma
;
11849 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
11851 struct elf_link_hash_table
*htab
11852 = elf_hash_table (flinfo
->info
);
11854 /* STT_TLS symbols are relative to PT_TLS
11856 if (htab
->tls_sec
!= NULL
)
11857 sym
.st_value
-= htab
->tls_sec
->vma
;
11860 = ELF_ST_INFO (ELF_ST_BIND (sym
.st_info
),
11865 indx
= bfd_get_symcount (output_bfd
);
11866 ret
= elf_link_output_symstrtab (flinfo
, name
,
11872 flinfo
->indices
[r_symndx
] = indx
;
11877 r_symndx
= flinfo
->indices
[r_symndx
];
11880 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
11881 | (irela
->r_info
& r_type_mask
));
11884 /* Swap out the relocs. */
11885 input_rel_hdr
= esdi
->rel
.hdr
;
11886 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
11888 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11893 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
11894 * bed
->s
->int_rels_per_ext_rel
);
11895 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
11898 input_rela_hdr
= esdi
->rela
.hdr
;
11899 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
11901 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11910 /* Write out the modified section contents. */
11911 if (bed
->elf_backend_write_section
11912 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
11915 /* Section written out. */
11917 else switch (o
->sec_info_type
)
11919 case SEC_INFO_TYPE_STABS
:
11920 if (! (_bfd_write_section_stabs
11922 &elf_hash_table (flinfo
->info
)->stab_info
,
11923 o
, &elf_section_data (o
)->sec_info
, contents
)))
11926 case SEC_INFO_TYPE_MERGE
:
11927 if (! _bfd_write_merged_section (output_bfd
, o
,
11928 elf_section_data (o
)->sec_info
))
11931 case SEC_INFO_TYPE_EH_FRAME
:
11933 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
11938 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
11940 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
11946 case SEC_INFO_TYPE_SFRAME
:
11948 /* Merge .sframe sections into the ctf frame encoder
11949 context of the output_bfd's section. The final .sframe
11950 output section will be written out later. */
11951 if (!_bfd_elf_merge_section_sframe (output_bfd
, flinfo
->info
,
11958 if (! (o
->flags
& SEC_EXCLUDE
))
11960 file_ptr offset
= (file_ptr
) o
->output_offset
;
11961 bfd_size_type todo
= o
->size
;
11963 offset
*= bfd_octets_per_byte (output_bfd
, o
);
11965 if ((o
->flags
& SEC_ELF_REVERSE_COPY
)
11966 && o
->size
> address_size
)
11968 /* Reverse-copy input section to output. */
11970 if ((o
->size
& (address_size
- 1)) != 0
11971 || (o
->reloc_count
!= 0
11972 && (o
->size
* bed
->s
->int_rels_per_ext_rel
11973 != o
->reloc_count
* address_size
)))
11976 /* xgettext:c-format */
11977 (_("error: %pB: size of section %pA is not "
11978 "multiple of address size"),
11980 bfd_set_error (bfd_error_bad_value
);
11986 todo
-= address_size
;
11987 if (! bfd_set_section_contents (output_bfd
,
11995 offset
+= address_size
;
11999 else if (! bfd_set_section_contents (output_bfd
,
12013 /* Generate a reloc when linking an ELF file. This is a reloc
12014 requested by the linker, and does not come from any input file. This
12015 is used to build constructor and destructor tables when linking
12019 elf_reloc_link_order (bfd
*output_bfd
,
12020 struct bfd_link_info
*info
,
12021 asection
*output_section
,
12022 struct bfd_link_order
*link_order
)
12024 reloc_howto_type
*howto
;
12028 struct bfd_elf_section_reloc_data
*reldata
;
12029 struct elf_link_hash_entry
**rel_hash_ptr
;
12030 Elf_Internal_Shdr
*rel_hdr
;
12031 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
12032 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
12035 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
12037 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
12040 bfd_set_error (bfd_error_bad_value
);
12044 addend
= link_order
->u
.reloc
.p
->addend
;
12047 reldata
= &esdo
->rel
;
12048 else if (esdo
->rela
.hdr
)
12049 reldata
= &esdo
->rela
;
12056 /* Figure out the symbol index. */
12057 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
12058 if (link_order
->type
== bfd_section_reloc_link_order
)
12060 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
12061 BFD_ASSERT (indx
!= 0);
12062 *rel_hash_ptr
= NULL
;
12066 struct elf_link_hash_entry
*h
;
12068 /* Treat a reloc against a defined symbol as though it were
12069 actually against the section. */
12070 h
= ((struct elf_link_hash_entry
*)
12071 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
12072 link_order
->u
.reloc
.p
->u
.name
,
12073 false, false, true));
12075 && (h
->root
.type
== bfd_link_hash_defined
12076 || h
->root
.type
== bfd_link_hash_defweak
))
12080 section
= h
->root
.u
.def
.section
;
12081 indx
= section
->output_section
->target_index
;
12082 *rel_hash_ptr
= NULL
;
12083 /* It seems that we ought to add the symbol value to the
12084 addend here, but in practice it has already been added
12085 because it was passed to constructor_callback. */
12086 addend
+= section
->output_section
->vma
+ section
->output_offset
;
12088 else if (h
!= NULL
)
12090 /* Setting the index to -2 tells elf_link_output_extsym that
12091 this symbol is used by a reloc. */
12098 (*info
->callbacks
->unattached_reloc
)
12099 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
12104 /* If this is an inplace reloc, we must write the addend into the
12106 if (howto
->partial_inplace
&& addend
!= 0)
12108 bfd_size_type size
;
12109 bfd_reloc_status_type rstat
;
12112 const char *sym_name
;
12113 bfd_size_type octets
;
12115 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
12116 buf
= (bfd_byte
*) bfd_zmalloc (size
);
12117 if (buf
== NULL
&& size
!= 0)
12119 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
12126 case bfd_reloc_outofrange
:
12129 case bfd_reloc_overflow
:
12130 if (link_order
->type
== bfd_section_reloc_link_order
)
12131 sym_name
= bfd_section_name (link_order
->u
.reloc
.p
->u
.section
);
12133 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
12134 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
12135 howto
->name
, addend
, NULL
, NULL
,
12140 octets
= link_order
->offset
* bfd_octets_per_byte (output_bfd
,
12142 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
12149 /* The address of a reloc is relative to the section in a
12150 relocatable file, and is a virtual address in an executable
12152 offset
= link_order
->offset
;
12153 if (! bfd_link_relocatable (info
))
12154 offset
+= output_section
->vma
;
12156 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
12158 irel
[i
].r_offset
= offset
;
12159 irel
[i
].r_info
= 0;
12160 irel
[i
].r_addend
= 0;
12162 if (bed
->s
->arch_size
== 32)
12163 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
12165 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
12167 rel_hdr
= reldata
->hdr
;
12168 erel
= rel_hdr
->contents
;
12169 if (rel_hdr
->sh_type
== SHT_REL
)
12171 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
12172 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
12176 irel
[0].r_addend
= addend
;
12177 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
12178 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
12186 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12187 Returns TRUE upon success, FALSE otherwise. */
12190 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
12194 const struct elf_backend_data
*bed
;
12196 enum bfd_architecture arch
;
12198 asymbol
**sympp
= NULL
;
12202 elf_symbol_type
*osymbuf
;
12205 implib_bfd
= info
->out_implib_bfd
;
12206 bed
= get_elf_backend_data (abfd
);
12208 if (!bfd_set_format (implib_bfd
, bfd_object
))
12211 /* Use flag from executable but make it a relocatable object. */
12212 flags
= bfd_get_file_flags (abfd
);
12213 flags
&= ~HAS_RELOC
;
12214 if (!bfd_set_start_address (implib_bfd
, 0)
12215 || !bfd_set_file_flags (implib_bfd
, flags
& ~EXEC_P
))
12218 /* Copy architecture of output file to import library file. */
12219 arch
= bfd_get_arch (abfd
);
12220 mach
= bfd_get_mach (abfd
);
12221 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
12222 && (abfd
->target_defaulted
12223 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
12226 /* Get symbol table size. */
12227 symsize
= bfd_get_symtab_upper_bound (abfd
);
12231 /* Read in the symbol table. */
12232 sympp
= (asymbol
**) bfd_malloc (symsize
);
12236 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
12240 /* Allow the BFD backend to copy any private header data it
12241 understands from the output BFD to the import library BFD. */
12242 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
12245 /* Filter symbols to appear in the import library. */
12246 if (bed
->elf_backend_filter_implib_symbols
)
12247 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
12250 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
12253 bfd_set_error (bfd_error_no_symbols
);
12254 _bfd_error_handler (_("%pB: no symbol found for import library"),
12260 /* Make symbols absolute. */
12261 amt
= symcount
* sizeof (*osymbuf
);
12262 osymbuf
= (elf_symbol_type
*) bfd_alloc (implib_bfd
, amt
);
12263 if (osymbuf
== NULL
)
12266 for (src_count
= 0; src_count
< symcount
; src_count
++)
12268 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
12269 sizeof (*osymbuf
));
12270 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
12271 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
12272 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
12273 osymbuf
[src_count
].internal_elf_sym
.st_value
=
12274 osymbuf
[src_count
].symbol
.value
;
12275 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
12278 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
12280 /* Allow the BFD backend to copy any private data it understands
12281 from the output BFD to the import library BFD. This is done last
12282 to permit the routine to look at the filtered symbol table. */
12283 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
12286 if (!bfd_close (implib_bfd
))
12297 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
12301 if (flinfo
->symstrtab
!= NULL
)
12302 _bfd_elf_strtab_free (flinfo
->symstrtab
);
12303 free (flinfo
->contents
);
12304 free (flinfo
->external_relocs
);
12305 free (flinfo
->internal_relocs
);
12306 free (flinfo
->external_syms
);
12307 free (flinfo
->locsym_shndx
);
12308 free (flinfo
->internal_syms
);
12309 free (flinfo
->indices
);
12310 free (flinfo
->sections
);
12311 if (flinfo
->symshndxbuf
!= (Elf_External_Sym_Shndx
*) -1)
12312 free (flinfo
->symshndxbuf
);
12313 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
12315 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12316 free (esdo
->rel
.hashes
);
12317 free (esdo
->rela
.hashes
);
12321 /* Do the final step of an ELF link. */
12324 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
12329 struct elf_final_link_info flinfo
;
12331 struct bfd_link_order
*p
;
12333 bfd_size_type max_contents_size
;
12334 bfd_size_type max_external_reloc_size
;
12335 bfd_size_type max_internal_reloc_count
;
12336 bfd_size_type max_sym_count
;
12337 bfd_size_type max_sym_shndx_count
;
12338 Elf_Internal_Sym elfsym
;
12340 Elf_Internal_Shdr
*symtab_hdr
;
12341 Elf_Internal_Shdr
*symtab_shndx_hdr
;
12342 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12343 struct elf_outext_info eoinfo
;
12345 size_t relativecount
;
12346 size_t relr_entsize
;
12347 asection
*reldyn
= 0;
12349 asection
*attr_section
= NULL
;
12350 bfd_vma attr_size
= 0;
12351 const char *std_attrs_section
;
12352 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
12353 bool sections_removed
;
12356 if (!is_elf_hash_table (&htab
->root
))
12359 if (bfd_link_pic (info
))
12360 abfd
->flags
|= DYNAMIC
;
12362 dynamic
= htab
->dynamic_sections_created
;
12363 dynobj
= htab
->dynobj
;
12365 emit_relocs
= (bfd_link_relocatable (info
)
12366 || info
->emitrelocations
);
12368 memset (&flinfo
, 0, sizeof (flinfo
));
12369 flinfo
.info
= info
;
12370 flinfo
.output_bfd
= abfd
;
12371 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
12372 if (flinfo
.symstrtab
== NULL
)
12377 flinfo
.hash_sec
= NULL
;
12378 flinfo
.symver_sec
= NULL
;
12382 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
12383 /* Note that dynsym_sec can be NULL (on VMS). */
12384 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
12385 /* Note that it is OK if symver_sec is NULL. */
12388 if (info
->unique_symbol
12389 && !bfd_hash_table_init (&flinfo
.local_hash_table
,
12390 local_hash_newfunc
,
12391 sizeof (struct local_hash_entry
)))
12394 /* The object attributes have been merged. Remove the input
12395 sections from the link, and set the contents of the output
12397 sections_removed
= false;
12398 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
12399 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12401 bool remove_section
= false;
12403 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
12404 || strcmp (o
->name
, ".gnu.attributes") == 0)
12406 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12408 asection
*input_section
;
12410 if (p
->type
!= bfd_indirect_link_order
)
12412 input_section
= p
->u
.indirect
.section
;
12413 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12414 elf_link_input_bfd ignores this section. */
12415 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
12418 attr_size
= bfd_elf_obj_attr_size (abfd
);
12419 bfd_set_section_size (o
, attr_size
);
12420 /* Skip this section later on. */
12421 o
->map_head
.link_order
= NULL
;
12425 remove_section
= true;
12427 else if ((o
->flags
& SEC_GROUP
) != 0 && o
->size
== 0)
12429 /* Remove empty group section from linker output. */
12430 remove_section
= true;
12432 if (remove_section
)
12434 o
->flags
|= SEC_EXCLUDE
;
12435 bfd_section_list_remove (abfd
, o
);
12436 abfd
->section_count
--;
12437 sections_removed
= true;
12440 if (sections_removed
)
12441 _bfd_fix_excluded_sec_syms (abfd
, info
);
12443 /* Count up the number of relocations we will output for each output
12444 section, so that we know the sizes of the reloc sections. We
12445 also figure out some maximum sizes. */
12446 max_contents_size
= 0;
12447 max_external_reloc_size
= 0;
12448 max_internal_reloc_count
= 0;
12450 max_sym_shndx_count
= 0;
12452 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12454 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12455 o
->reloc_count
= 0;
12457 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12459 unsigned int reloc_count
= 0;
12460 unsigned int additional_reloc_count
= 0;
12461 struct bfd_elf_section_data
*esdi
= NULL
;
12463 if (p
->type
== bfd_section_reloc_link_order
12464 || p
->type
== bfd_symbol_reloc_link_order
)
12466 else if (p
->type
== bfd_indirect_link_order
)
12470 sec
= p
->u
.indirect
.section
;
12472 /* Mark all sections which are to be included in the
12473 link. This will normally be every section. We need
12474 to do this so that we can identify any sections which
12475 the linker has decided to not include. */
12476 sec
->linker_mark
= true;
12478 if (sec
->flags
& SEC_MERGE
)
12481 if (sec
->rawsize
> max_contents_size
)
12482 max_contents_size
= sec
->rawsize
;
12483 if (sec
->size
> max_contents_size
)
12484 max_contents_size
= sec
->size
;
12486 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
12487 && (sec
->owner
->flags
& DYNAMIC
) == 0)
12491 /* We are interested in just local symbols, not all
12493 if (elf_bad_symtab (sec
->owner
))
12494 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
12495 / bed
->s
->sizeof_sym
);
12497 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
12499 if (sym_count
> max_sym_count
)
12500 max_sym_count
= sym_count
;
12502 if (sym_count
> max_sym_shndx_count
12503 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
12504 max_sym_shndx_count
= sym_count
;
12506 esdi
= elf_section_data (sec
);
12508 if (esdi
->this_hdr
.sh_type
== SHT_REL
12509 || esdi
->this_hdr
.sh_type
== SHT_RELA
)
12510 /* Some backends use reloc_count in relocation sections
12511 to count particular types of relocs. Of course,
12512 reloc sections themselves can't have relocations. */
12514 else if (emit_relocs
)
12516 reloc_count
= sec
->reloc_count
;
12517 if (bed
->elf_backend_count_additional_relocs
)
12520 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
12521 additional_reloc_count
+= c
;
12524 else if (bed
->elf_backend_count_relocs
)
12525 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
12527 if ((sec
->flags
& SEC_RELOC
) != 0)
12529 size_t ext_size
= 0;
12531 if (esdi
->rel
.hdr
!= NULL
)
12532 ext_size
= esdi
->rel
.hdr
->sh_size
;
12533 if (esdi
->rela
.hdr
!= NULL
)
12534 ext_size
+= esdi
->rela
.hdr
->sh_size
;
12536 if (ext_size
> max_external_reloc_size
)
12537 max_external_reloc_size
= ext_size
;
12538 if (sec
->reloc_count
> max_internal_reloc_count
)
12539 max_internal_reloc_count
= sec
->reloc_count
;
12544 if (reloc_count
== 0)
12547 reloc_count
+= additional_reloc_count
;
12548 o
->reloc_count
+= reloc_count
;
12550 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
12554 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
12555 esdo
->rel
.count
+= additional_reloc_count
;
12557 if (esdi
->rela
.hdr
)
12559 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
12560 esdo
->rela
.count
+= additional_reloc_count
;
12566 esdo
->rela
.count
+= reloc_count
;
12568 esdo
->rel
.count
+= reloc_count
;
12572 if (o
->reloc_count
> 0)
12573 o
->flags
|= SEC_RELOC
;
12576 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12577 set it (this is probably a bug) and if it is set
12578 assign_section_numbers will create a reloc section. */
12579 o
->flags
&=~ SEC_RELOC
;
12582 /* If the SEC_ALLOC flag is not set, force the section VMA to
12583 zero. This is done in elf_fake_sections as well, but forcing
12584 the VMA to 0 here will ensure that relocs against these
12585 sections are handled correctly. */
12586 if ((o
->flags
& SEC_ALLOC
) == 0
12587 && ! o
->user_set_vma
)
12591 if (! bfd_link_relocatable (info
) && merged
)
12592 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
12594 /* Figure out the file positions for everything but the symbol table
12595 and the relocs. We set symcount to force assign_section_numbers
12596 to create a symbol table. */
12597 abfd
->symcount
= info
->strip
!= strip_all
|| emit_relocs
;
12598 BFD_ASSERT (! abfd
->output_has_begun
);
12599 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
12602 /* Set sizes, and assign file positions for reloc sections. */
12603 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12605 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12606 if ((o
->flags
& SEC_RELOC
) != 0)
12609 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
12613 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
12617 /* _bfd_elf_compute_section_file_positions makes temporary use
12618 of target_index. Reset it. */
12619 o
->target_index
= 0;
12621 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12622 to count upwards while actually outputting the relocations. */
12623 esdo
->rel
.count
= 0;
12624 esdo
->rela
.count
= 0;
12626 if ((esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
12627 && !bfd_section_is_ctf (o
))
12629 /* Cache the section contents so that they can be compressed
12630 later. Use bfd_malloc since it will be freed by
12631 bfd_compress_section_contents. */
12632 unsigned char *contents
= esdo
->this_hdr
.contents
;
12633 if (contents
!= NULL
)
12636 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
12637 if (contents
== NULL
)
12639 esdo
->this_hdr
.contents
= contents
;
12643 /* We have now assigned file positions for all the sections except .symtab,
12644 .strtab, and non-loaded reloc and compressed debugging sections. We start
12645 the .symtab section at the current file position, and write directly to it.
12646 We build the .strtab section in memory. */
12647 abfd
->symcount
= 0;
12648 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12649 /* sh_name is set in prep_headers. */
12650 symtab_hdr
->sh_type
= SHT_SYMTAB
;
12651 /* sh_flags, sh_addr and sh_size all start off zero. */
12652 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
12653 /* sh_link is set in assign_section_numbers. */
12654 /* sh_info is set below. */
12655 /* sh_offset is set just below. */
12656 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
12658 if (max_sym_count
< 20)
12659 max_sym_count
= 20;
12660 htab
->strtabsize
= max_sym_count
;
12661 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
12662 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
12663 if (htab
->strtab
== NULL
)
12665 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12667 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
12668 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
12670 if (info
->strip
!= strip_all
|| emit_relocs
)
12672 file_ptr off
= elf_next_file_pos (abfd
);
12674 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
12676 /* Note that at this point elf_next_file_pos (abfd) is
12677 incorrect. We do not yet know the size of the .symtab section.
12678 We correct next_file_pos below, after we do know the size. */
12680 /* Start writing out the symbol table. The first symbol is always a
12682 elfsym
.st_value
= 0;
12683 elfsym
.st_size
= 0;
12684 elfsym
.st_info
= 0;
12685 elfsym
.st_other
= 0;
12686 elfsym
.st_shndx
= SHN_UNDEF
;
12687 elfsym
.st_target_internal
= 0;
12688 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
12689 bfd_und_section_ptr
, NULL
) != 1)
12692 /* Output a symbol for each section if asked or they are used for
12693 relocs. These symbols usually have no names. We store the
12694 index of each one in the index field of the section, so that
12695 we can find it again when outputting relocs. */
12697 if (bfd_keep_unused_section_symbols (abfd
) || emit_relocs
)
12699 bool name_local_sections
12700 = (bed
->elf_backend_name_local_section_symbols
12701 && bed
->elf_backend_name_local_section_symbols (abfd
));
12702 const char *name
= NULL
;
12704 elfsym
.st_size
= 0;
12705 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12706 elfsym
.st_other
= 0;
12707 elfsym
.st_value
= 0;
12708 elfsym
.st_target_internal
= 0;
12709 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12711 o
= bfd_section_from_elf_index (abfd
, i
);
12714 o
->target_index
= bfd_get_symcount (abfd
);
12715 elfsym
.st_shndx
= i
;
12716 if (!bfd_link_relocatable (info
))
12717 elfsym
.st_value
= o
->vma
;
12718 if (name_local_sections
)
12720 if (elf_link_output_symstrtab (&flinfo
, name
, &elfsym
, o
,
12728 /* On some targets like Irix 5 the symbol split between local and global
12729 ones recorded in the sh_info field needs to be done between section
12730 and all other symbols. */
12731 if (bed
->elf_backend_elfsym_local_is_section
12732 && bed
->elf_backend_elfsym_local_is_section (abfd
))
12733 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12735 /* Allocate some memory to hold information read in from the input
12737 if (max_contents_size
!= 0)
12739 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
12740 if (flinfo
.contents
== NULL
)
12744 if (max_external_reloc_size
!= 0)
12746 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
12747 if (flinfo
.external_relocs
== NULL
)
12751 if (max_internal_reloc_count
!= 0)
12753 amt
= max_internal_reloc_count
* sizeof (Elf_Internal_Rela
);
12754 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
12755 if (flinfo
.internal_relocs
== NULL
)
12759 if (max_sym_count
!= 0)
12761 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
12762 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
12763 if (flinfo
.external_syms
== NULL
)
12766 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
12767 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
12768 if (flinfo
.internal_syms
== NULL
)
12771 amt
= max_sym_count
* sizeof (long);
12772 flinfo
.indices
= (long int *) bfd_malloc (amt
);
12773 if (flinfo
.indices
== NULL
)
12776 amt
= max_sym_count
* sizeof (asection
*);
12777 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
12778 if (flinfo
.sections
== NULL
)
12782 if (max_sym_shndx_count
!= 0)
12784 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
12785 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
12786 if (flinfo
.locsym_shndx
== NULL
)
12792 bfd_vma base
, end
= 0; /* Both bytes. */
12795 for (sec
= htab
->tls_sec
;
12796 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
12799 bfd_size_type size
= sec
->size
;
12800 unsigned int opb
= bfd_octets_per_byte (abfd
, sec
);
12803 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
12805 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
12808 size
= ord
->offset
* opb
+ ord
->size
;
12810 end
= sec
->vma
+ size
/ opb
;
12812 base
= htab
->tls_sec
->vma
;
12813 /* Only align end of TLS section if static TLS doesn't have special
12814 alignment requirements. */
12815 if (bed
->static_tls_alignment
== 1)
12816 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
12817 htab
->tls_size
= end
- base
;
12820 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
12823 /* Finish relative relocations here after regular symbol processing
12824 is finished if DT_RELR is enabled. */
12825 if (info
->enable_dt_relr
12826 && bed
->finish_relative_relocs
12827 && !bed
->finish_relative_relocs (info
))
12828 info
->callbacks
->einfo
12829 (_("%F%P: %pB: failed to finish relative relocations\n"), abfd
);
12831 /* Since ELF permits relocations to be against local symbols, we
12832 must have the local symbols available when we do the relocations.
12833 Since we would rather only read the local symbols once, and we
12834 would rather not keep them in memory, we handle all the
12835 relocations for a single input file at the same time.
12837 Unfortunately, there is no way to know the total number of local
12838 symbols until we have seen all of them, and the local symbol
12839 indices precede the global symbol indices. This means that when
12840 we are generating relocatable output, and we see a reloc against
12841 a global symbol, we can not know the symbol index until we have
12842 finished examining all the local symbols to see which ones we are
12843 going to output. To deal with this, we keep the relocations in
12844 memory, and don't output them until the end of the link. This is
12845 an unfortunate waste of memory, but I don't see a good way around
12846 it. Fortunately, it only happens when performing a relocatable
12847 link, which is not the common case. FIXME: If keep_memory is set
12848 we could write the relocs out and then read them again; I don't
12849 know how bad the memory loss will be. */
12851 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12852 sub
->output_has_begun
= false;
12853 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12855 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12857 if (p
->type
== bfd_indirect_link_order
12858 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
12859 == bfd_target_elf_flavour
)
12860 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
12862 if (! sub
->output_has_begun
)
12864 if (! elf_link_input_bfd (&flinfo
, sub
))
12866 sub
->output_has_begun
= true;
12869 else if (p
->type
== bfd_section_reloc_link_order
12870 || p
->type
== bfd_symbol_reloc_link_order
)
12872 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
12877 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
12879 if (p
->type
== bfd_indirect_link_order
12880 && (bfd_get_flavour (sub
)
12881 == bfd_target_elf_flavour
)
12882 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
12883 != bed
->s
->elfclass
))
12885 const char *iclass
, *oclass
;
12887 switch (bed
->s
->elfclass
)
12889 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
12890 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
12891 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
12895 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
12897 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
12898 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
12899 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
12903 bfd_set_error (bfd_error_wrong_format
);
12905 /* xgettext:c-format */
12906 (_("%pB: file class %s incompatible with %s"),
12907 sub
, iclass
, oclass
);
12916 /* Free symbol buffer if needed. */
12917 if (!info
->reduce_memory_overheads
)
12919 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12920 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
12922 free (elf_tdata (sub
)->symbuf
);
12923 elf_tdata (sub
)->symbuf
= NULL
;
12929 /* Output any global symbols that got converted to local in a
12930 version script or due to symbol visibility. We do this in a
12931 separate step since ELF requires all local symbols to appear
12932 prior to any global symbols. FIXME: We should only do this if
12933 some global symbols were, in fact, converted to become local.
12934 FIXME: Will this work correctly with the Irix 5 linker? */
12935 eoinfo
.failed
= false;
12936 eoinfo
.flinfo
= &flinfo
;
12937 eoinfo
.localsyms
= true;
12938 eoinfo
.file_sym_done
= false;
12939 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12943 goto return_local_hash_table
;
12946 /* If backend needs to output some local symbols not present in the hash
12947 table, do it now. */
12948 if (bed
->elf_backend_output_arch_local_syms
)
12950 if (! ((*bed
->elf_backend_output_arch_local_syms
)
12951 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
12954 goto return_local_hash_table
;
12958 /* That wrote out all the local symbols. Finish up the symbol table
12959 with the global symbols. Even if we want to strip everything we
12960 can, we still need to deal with those global symbols that got
12961 converted to local in a version script. */
12963 /* The sh_info field records the index of the first non local symbol. */
12964 if (!symtab_hdr
->sh_info
)
12965 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12968 && htab
->dynsym
!= NULL
12969 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
12971 Elf_Internal_Sym sym
;
12972 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
12974 o
= htab
->dynsym
->output_section
;
12975 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
12977 /* Write out the section symbols for the output sections. */
12978 if (bfd_link_pic (info
)
12979 || htab
->is_relocatable_executable
)
12985 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12987 sym
.st_target_internal
= 0;
12989 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
12995 dynindx
= elf_section_data (s
)->dynindx
;
12998 indx
= elf_section_data (s
)->this_idx
;
12999 BFD_ASSERT (indx
> 0);
13000 sym
.st_shndx
= indx
;
13001 if (! check_dynsym (abfd
, &sym
))
13004 goto return_local_hash_table
;
13006 sym
.st_value
= s
->vma
;
13007 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
13009 /* Inform the linker of the addition of this symbol. */
13011 if (info
->callbacks
->ctf_new_dynsym
)
13012 info
->callbacks
->ctf_new_dynsym (dynindx
, &sym
);
13014 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
13018 /* Write out the local dynsyms. */
13019 if (htab
->dynlocal
)
13021 struct elf_link_local_dynamic_entry
*e
;
13022 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
13027 /* Copy the internal symbol and turn off visibility.
13028 Note that we saved a word of storage and overwrote
13029 the original st_name with the dynstr_index. */
13031 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
13032 sym
.st_shndx
= SHN_UNDEF
;
13034 s
= bfd_section_from_elf_index (e
->input_bfd
,
13037 && s
->output_section
!= NULL
13038 && elf_section_data (s
->output_section
) != NULL
)
13041 elf_section_data (s
->output_section
)->this_idx
;
13042 if (! check_dynsym (abfd
, &sym
))
13045 goto return_local_hash_table
;
13047 sym
.st_value
= (s
->output_section
->vma
13049 + e
->isym
.st_value
);
13052 /* Inform the linker of the addition of this symbol. */
13054 if (info
->callbacks
->ctf_new_dynsym
)
13055 info
->callbacks
->ctf_new_dynsym (e
->dynindx
, &sym
);
13057 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
13058 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
13063 /* We get the global symbols from the hash table. */
13064 eoinfo
.failed
= false;
13065 eoinfo
.localsyms
= false;
13066 eoinfo
.flinfo
= &flinfo
;
13067 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
13071 goto return_local_hash_table
;
13074 /* If backend needs to output some symbols not present in the hash
13075 table, do it now. */
13076 if (bed
->elf_backend_output_arch_syms
13077 && (info
->strip
!= strip_all
|| emit_relocs
))
13079 if (! ((*bed
->elf_backend_output_arch_syms
)
13080 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
13083 goto return_local_hash_table
;
13087 /* Finalize the .strtab section. */
13088 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
13090 /* Swap out the .strtab section. */
13091 if (!elf_link_swap_symbols_out (&flinfo
))
13094 goto return_local_hash_table
;
13097 /* Now we know the size of the symtab section. */
13098 if (bfd_get_symcount (abfd
) > 0)
13100 /* Finish up and write out the symbol string table (.strtab)
13102 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
13103 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
13105 if (elf_symtab_shndx_list (abfd
))
13107 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
13109 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
13111 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
13112 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
13113 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
13114 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
13115 symtab_shndx_hdr
->sh_size
= amt
;
13117 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
13120 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
13121 || (bfd_write (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
13124 goto return_local_hash_table
;
13129 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
13130 /* sh_name was set in prep_headers. */
13131 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
13132 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
13133 symstrtab_hdr
->sh_addr
= 0;
13134 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
13135 symstrtab_hdr
->sh_entsize
= 0;
13136 symstrtab_hdr
->sh_link
= 0;
13137 symstrtab_hdr
->sh_info
= 0;
13138 /* sh_offset is set just below. */
13139 symstrtab_hdr
->sh_addralign
= 1;
13141 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
13143 elf_next_file_pos (abfd
) = off
;
13145 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
13146 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
13149 goto return_local_hash_table
;
13153 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
13155 _bfd_error_handler (_("%pB: failed to generate import library"),
13156 info
->out_implib_bfd
);
13158 goto return_local_hash_table
;
13161 /* Adjust the relocs to have the correct symbol indices. */
13162 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
13164 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
13167 if ((o
->flags
& SEC_RELOC
) == 0)
13170 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
13171 if (esdo
->rel
.hdr
!= NULL
13172 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
, info
))
13175 goto return_local_hash_table
;
13177 if (esdo
->rela
.hdr
!= NULL
13178 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
, info
))
13181 goto return_local_hash_table
;
13184 /* Set the reloc_count field to 0 to prevent write_relocs from
13185 trying to swap the relocs out itself. */
13186 o
->reloc_count
= 0;
13190 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
13191 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
13194 if (htab
->srelrdyn
!= NULL
13195 && htab
->srelrdyn
->output_section
!= NULL
13196 && htab
->srelrdyn
->size
!= 0)
13198 asection
*s
= htab
->srelrdyn
->output_section
;
13199 relr_entsize
= elf_section_data (s
)->this_hdr
.sh_entsize
;
13200 if (relr_entsize
== 0)
13202 relr_entsize
= bed
->s
->arch_size
/ 8;
13203 elf_section_data (s
)->this_hdr
.sh_entsize
= relr_entsize
;
13207 /* If we are linking against a dynamic object, or generating a
13208 shared library, finish up the dynamic linking information. */
13211 bfd_byte
*dyncon
, *dynconend
;
13213 /* Fix up .dynamic entries. */
13214 o
= bfd_get_linker_section (dynobj
, ".dynamic");
13215 BFD_ASSERT (o
!= NULL
);
13217 dyncon
= o
->contents
;
13218 dynconend
= PTR_ADD (o
->contents
, o
->size
);
13219 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13221 Elf_Internal_Dyn dyn
;
13224 bfd_size_type sh_size
;
13227 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13234 if (relativecount
!= 0)
13236 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
13238 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
13239 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
13241 if (dyn
.d_tag
!= DT_NULL
13242 && dynconend
- dyncon
>= bed
->s
->sizeof_dyn
)
13244 dyn
.d_un
.d_val
= relativecount
;
13250 if (relr_entsize
!= 0)
13252 if (dynconend
- dyncon
>= 3 * bed
->s
->sizeof_dyn
)
13254 asection
*s
= htab
->srelrdyn
;
13255 dyn
.d_tag
= DT_RELR
;
13257 = s
->output_section
->vma
+ s
->output_offset
;
13258 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13259 dyncon
+= bed
->s
->sizeof_dyn
;
13261 dyn
.d_tag
= DT_RELRSZ
;
13262 dyn
.d_un
.d_val
= s
->size
;
13263 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13264 dyncon
+= bed
->s
->sizeof_dyn
;
13266 dyn
.d_tag
= DT_RELRENT
;
13267 dyn
.d_un
.d_val
= relr_entsize
;
13276 name
= info
->init_function
;
13279 name
= info
->fini_function
;
13282 struct elf_link_hash_entry
*h
;
13284 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
13286 && (h
->root
.type
== bfd_link_hash_defined
13287 || h
->root
.type
== bfd_link_hash_defweak
))
13289 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
13290 o
= h
->root
.u
.def
.section
;
13291 if (o
->output_section
!= NULL
)
13292 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
13293 + o
->output_offset
);
13296 /* The symbol is imported from another shared
13297 library and does not apply to this one. */
13298 dyn
.d_un
.d_ptr
= 0;
13305 case DT_PREINIT_ARRAYSZ
:
13306 name
= ".preinit_array";
13308 case DT_INIT_ARRAYSZ
:
13309 name
= ".init_array";
13311 case DT_FINI_ARRAYSZ
:
13312 name
= ".fini_array";
13314 o
= bfd_get_section_by_name (abfd
, name
);
13318 (_("could not find section %s"), name
);
13323 (_("warning: %s section has zero size"), name
);
13324 dyn
.d_un
.d_val
= o
->size
;
13327 case DT_PREINIT_ARRAY
:
13328 name
= ".preinit_array";
13330 case DT_INIT_ARRAY
:
13331 name
= ".init_array";
13333 case DT_FINI_ARRAY
:
13334 name
= ".fini_array";
13336 o
= bfd_get_section_by_name (abfd
, name
);
13343 name
= ".gnu.hash";
13352 name
= ".gnu.version_d";
13355 name
= ".gnu.version_r";
13358 name
= ".gnu.version";
13360 o
= bfd_get_linker_section (dynobj
, name
);
13362 if (o
== NULL
|| bfd_is_abs_section (o
->output_section
))
13365 (_("could not find section %s"), name
);
13368 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
13371 (_("warning: section '%s' is being made into a note"), name
);
13372 bfd_set_error (bfd_error_nonrepresentable_section
);
13375 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
13382 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
13388 for (i
= 1; i
< elf_numsections (abfd
); i
++)
13390 Elf_Internal_Shdr
*hdr
;
13392 hdr
= elf_elfsections (abfd
)[i
];
13393 if (hdr
->sh_type
== type
13394 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
13396 sh_size
+= hdr
->sh_size
;
13398 || sh_addr
> hdr
->sh_addr
)
13399 sh_addr
= hdr
->sh_addr
;
13403 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
13405 unsigned int opb
= bfd_octets_per_byte (abfd
, o
);
13407 /* Don't count procedure linkage table relocs in the
13408 overall reloc count. */
13409 sh_size
-= htab
->srelplt
->size
;
13411 /* If the size is zero, make the address zero too.
13412 This is to avoid a glibc bug. If the backend
13413 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13414 zero, then we'll put DT_RELA at the end of
13415 DT_JMPREL. glibc will interpret the end of
13416 DT_RELA matching the end of DT_JMPREL as the
13417 case where DT_RELA includes DT_JMPREL, and for
13418 LD_BIND_NOW will decide that processing DT_RELA
13419 will process the PLT relocs too. Net result:
13420 No PLT relocs applied. */
13423 /* If .rela.plt is the first .rela section, exclude
13424 it from DT_RELA. */
13425 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
13426 + htab
->srelplt
->output_offset
) * opb
)
13427 sh_addr
+= htab
->srelplt
->size
;
13430 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
13431 dyn
.d_un
.d_val
= sh_size
;
13433 dyn
.d_un
.d_ptr
= sh_addr
;
13436 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13440 /* If we have created any dynamic sections, then output them. */
13441 if (dynobj
!= NULL
)
13443 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
13446 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13447 if (bfd_link_textrel_check (info
)
13448 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
13451 bfd_byte
*dyncon
, *dynconend
;
13453 dyncon
= o
->contents
;
13454 dynconend
= o
->contents
+ o
->size
;
13455 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13457 Elf_Internal_Dyn dyn
;
13459 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13461 if (dyn
.d_tag
== DT_TEXTREL
)
13463 if (info
->textrel_check
== textrel_check_error
)
13464 info
->callbacks
->einfo
13465 (_("%P%X: read-only segment has dynamic relocations\n"));
13466 else if (bfd_link_dll (info
))
13467 info
->callbacks
->einfo
13468 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13469 else if (bfd_link_pde (info
))
13470 info
->callbacks
->einfo
13471 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13473 info
->callbacks
->einfo
13474 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13480 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
13482 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
13484 || o
->output_section
== bfd_abs_section_ptr
)
13486 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
13488 /* At this point, we are only interested in sections
13489 created by _bfd_elf_link_create_dynamic_sections. */
13492 if (htab
->stab_info
.stabstr
== o
)
13494 if (htab
->eh_info
.hdr_sec
== o
)
13496 if (strcmp (o
->name
, ".dynstr") != 0)
13498 bfd_size_type octets
= ((file_ptr
) o
->output_offset
13499 * bfd_octets_per_byte (abfd
, o
));
13500 if (!bfd_set_section_contents (abfd
, o
->output_section
,
13501 o
->contents
, octets
, o
->size
))
13506 /* The contents of the .dynstr section are actually in a
13510 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
13511 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
13512 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
13518 if (!info
->resolve_section_groups
)
13520 bool failed
= false;
13522 BFD_ASSERT (bfd_link_relocatable (info
));
13523 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
13528 /* If we have optimized stabs strings, output them. */
13529 if (htab
->stab_info
.stabstr
!= NULL
)
13531 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
13535 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
13538 if (! _bfd_elf_write_section_sframe (abfd
, info
))
13541 if (info
->callbacks
->emit_ctf
)
13542 info
->callbacks
->emit_ctf ();
13544 elf_final_link_free (abfd
, &flinfo
);
13548 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
13549 if (contents
== NULL
)
13551 /* Bail out and fail. */
13553 goto return_local_hash_table
;
13555 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
13556 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
13560 return_local_hash_table
:
13561 if (info
->unique_symbol
)
13562 bfd_hash_table_free (&flinfo
.local_hash_table
);
13566 elf_final_link_free (abfd
, &flinfo
);
13568 goto return_local_hash_table
;
13571 /* Initialize COOKIE for input bfd ABFD. */
13574 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
13575 struct bfd_link_info
*info
, bfd
*abfd
)
13577 Elf_Internal_Shdr
*symtab_hdr
;
13578 const struct elf_backend_data
*bed
;
13580 bed
= get_elf_backend_data (abfd
);
13581 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13583 cookie
->abfd
= abfd
;
13584 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
13585 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
13586 if (cookie
->bad_symtab
)
13588 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13589 cookie
->extsymoff
= 0;
13593 cookie
->locsymcount
= symtab_hdr
->sh_info
;
13594 cookie
->extsymoff
= symtab_hdr
->sh_info
;
13597 if (bed
->s
->arch_size
== 32)
13598 cookie
->r_sym_shift
= 8;
13600 cookie
->r_sym_shift
= 32;
13602 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
13603 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
13605 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
13606 cookie
->locsymcount
, 0,
13608 if (cookie
->locsyms
== NULL
)
13610 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
13613 if (_bfd_link_keep_memory (info
) )
13615 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
13616 info
->cache_size
+= (cookie
->locsymcount
13617 * sizeof (Elf_External_Sym_Shndx
));
13623 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13626 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
13628 Elf_Internal_Shdr
*symtab_hdr
;
13630 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13631 if (symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
13632 free (cookie
->locsyms
);
13635 /* Initialize the relocation information in COOKIE for input section SEC
13636 of input bfd ABFD. */
13639 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13640 struct bfd_link_info
*info
, bfd
*abfd
,
13643 if (sec
->reloc_count
== 0)
13645 cookie
->rels
= NULL
;
13646 cookie
->relend
= NULL
;
13650 cookie
->rels
= _bfd_elf_link_info_read_relocs (abfd
, info
, sec
,
13652 _bfd_link_keep_memory (info
));
13653 if (cookie
->rels
== NULL
)
13655 cookie
->rel
= cookie
->rels
;
13656 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
13658 cookie
->rel
= cookie
->rels
;
13662 /* Free the memory allocated by init_reloc_cookie_rels,
13666 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13669 if (elf_section_data (sec
)->relocs
!= cookie
->rels
)
13670 free (cookie
->rels
);
13673 /* Initialize the whole of COOKIE for input section SEC. */
13676 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13677 struct bfd_link_info
*info
,
13680 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
13682 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
13687 fini_reloc_cookie (cookie
, sec
->owner
);
13692 /* Free the memory allocated by init_reloc_cookie_for_section,
13696 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13699 fini_reloc_cookie_rels (cookie
, sec
);
13700 fini_reloc_cookie (cookie
, sec
->owner
);
13703 /* Garbage collect unused sections. */
13705 /* Default gc_mark_hook. */
13708 _bfd_elf_gc_mark_hook (asection
*sec
,
13709 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13710 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13711 struct elf_link_hash_entry
*h
,
13712 Elf_Internal_Sym
*sym
)
13716 switch (h
->root
.type
)
13718 case bfd_link_hash_defined
:
13719 case bfd_link_hash_defweak
:
13720 return h
->root
.u
.def
.section
;
13722 case bfd_link_hash_common
:
13723 return h
->root
.u
.c
.p
->section
;
13730 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
13735 /* Return the debug definition section. */
13738 elf_gc_mark_debug_section (asection
*sec ATTRIBUTE_UNUSED
,
13739 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13740 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13741 struct elf_link_hash_entry
*h
,
13742 Elf_Internal_Sym
*sym
)
13746 /* Return the global debug definition section. */
13747 if ((h
->root
.type
== bfd_link_hash_defined
13748 || h
->root
.type
== bfd_link_hash_defweak
)
13749 && (h
->root
.u
.def
.section
->flags
& SEC_DEBUGGING
) != 0)
13750 return h
->root
.u
.def
.section
;
13754 /* Return the local debug definition section. */
13755 asection
*isec
= bfd_section_from_elf_index (sec
->owner
,
13757 if (isec
!= NULL
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
13764 /* COOKIE->rel describes a relocation against section SEC, which is
13765 a section we've decided to keep. Return the section that contains
13766 the relocation symbol, or NULL if no section contains it. */
13769 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
13770 elf_gc_mark_hook_fn gc_mark_hook
,
13771 struct elf_reloc_cookie
*cookie
,
13774 unsigned long r_symndx
;
13775 struct elf_link_hash_entry
*h
, *hw
;
13777 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
13778 if (r_symndx
== STN_UNDEF
)
13781 if (r_symndx
>= cookie
->locsymcount
13782 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13786 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
13789 info
->callbacks
->einfo (_("%F%P: corrupt input: %pB\n"),
13793 while (h
->root
.type
== bfd_link_hash_indirect
13794 || h
->root
.type
== bfd_link_hash_warning
)
13795 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13797 was_marked
= h
->mark
;
13799 /* Keep all aliases of the symbol too. If an object symbol
13800 needs to be copied into .dynbss then all of its aliases
13801 should be present as dynamic symbols, not just the one used
13802 on the copy relocation. */
13804 while (hw
->is_weakalias
)
13810 if (!was_marked
&& h
->start_stop
&& !h
->root
.ldscript_def
)
13812 if (info
->start_stop_gc
)
13815 /* To work around a glibc bug, mark XXX input sections
13816 when there is a reference to __start_XXX or __stop_XXX
13818 else if (start_stop
!= NULL
)
13820 asection
*s
= h
->u2
.start_stop_section
;
13821 *start_stop
= true;
13826 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
13829 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
13830 &cookie
->locsyms
[r_symndx
]);
13833 /* COOKIE->rel describes a relocation against section SEC, which is
13834 a section we've decided to keep. Mark the section that contains
13835 the relocation symbol. */
13838 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
13840 elf_gc_mark_hook_fn gc_mark_hook
,
13841 struct elf_reloc_cookie
*cookie
)
13844 bool start_stop
= false;
13846 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
13847 while (rsec
!= NULL
)
13849 if (!rsec
->gc_mark
)
13851 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
13852 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
13854 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
13859 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
13864 /* The mark phase of garbage collection. For a given section, mark
13865 it and any sections in this section's group, and all the sections
13866 which define symbols to which it refers. */
13869 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
13871 elf_gc_mark_hook_fn gc_mark_hook
)
13874 asection
*group_sec
, *eh_frame
;
13878 /* Mark all the sections in the group. */
13879 group_sec
= elf_section_data (sec
)->next_in_group
;
13880 if (group_sec
&& !group_sec
->gc_mark
)
13881 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
13884 /* Look through the section relocs. */
13886 eh_frame
= elf_eh_frame_section (sec
->owner
);
13887 if ((sec
->flags
& SEC_RELOC
) != 0
13888 && sec
->reloc_count
> 0
13889 && sec
!= eh_frame
)
13891 struct elf_reloc_cookie cookie
;
13893 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
13897 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
13898 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
13903 fini_reloc_cookie_for_section (&cookie
, sec
);
13907 if (ret
&& eh_frame
&& elf_fde_list (sec
))
13909 struct elf_reloc_cookie cookie
;
13911 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
13915 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
13916 gc_mark_hook
, &cookie
))
13918 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
13922 eh_frame
= elf_section_eh_frame_entry (sec
);
13923 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
13924 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
13930 /* Scan and mark sections in a special or debug section group. */
13933 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
13935 /* Point to first section of section group. */
13937 /* Used to iterate the section group. */
13940 bool is_special_grp
= true;
13941 bool is_debug_grp
= true;
13943 /* First scan to see if group contains any section other than debug
13944 and special section. */
13945 ssec
= msec
= elf_next_in_group (grp
);
13948 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
13949 is_debug_grp
= false;
13951 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
13952 is_special_grp
= false;
13954 msec
= elf_next_in_group (msec
);
13956 while (msec
!= ssec
);
13958 /* If this is a pure debug section group or pure special section group,
13959 keep all sections in this group. */
13960 if (is_debug_grp
|| is_special_grp
)
13965 msec
= elf_next_in_group (msec
);
13967 while (msec
!= ssec
);
13971 /* Keep debug and special sections. */
13974 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
13975 elf_gc_mark_hook_fn mark_hook
)
13979 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13983 bool debug_frag_seen
;
13984 bool has_kept_debug_info
;
13986 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13988 isec
= ibfd
->sections
;
13989 if (isec
== NULL
|| isec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13992 /* Ensure all linker created sections are kept,
13993 see if any other section is already marked,
13994 and note if we have any fragmented debug sections. */
13995 debug_frag_seen
= some_kept
= has_kept_debug_info
= false;
13996 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13998 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
14000 else if (isec
->gc_mark
14001 && (isec
->flags
& SEC_ALLOC
) != 0
14002 && elf_section_type (isec
) != SHT_NOTE
)
14006 /* Since all sections, except for backend specific ones,
14007 have been garbage collected, call mark_hook on this
14008 section if any of its linked-to sections is marked. */
14009 asection
*linked_to_sec
;
14010 for (linked_to_sec
= elf_linked_to_section (isec
);
14011 linked_to_sec
!= NULL
&& !linked_to_sec
->linker_mark
;
14012 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
14014 if (linked_to_sec
->gc_mark
)
14016 if (!_bfd_elf_gc_mark (info
, isec
, mark_hook
))
14020 linked_to_sec
->linker_mark
= 1;
14022 for (linked_to_sec
= elf_linked_to_section (isec
);
14023 linked_to_sec
!= NULL
&& linked_to_sec
->linker_mark
;
14024 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
14025 linked_to_sec
->linker_mark
= 0;
14028 if (!debug_frag_seen
14029 && (isec
->flags
& SEC_DEBUGGING
)
14030 && startswith (isec
->name
, ".debug_line."))
14031 debug_frag_seen
= true;
14032 else if (strcmp (bfd_section_name (isec
),
14033 "__patchable_function_entries") == 0
14034 && elf_linked_to_section (isec
) == NULL
)
14035 info
->callbacks
->einfo (_("%F%P: %pB(%pA): error: "
14036 "need linked-to section "
14037 "for --gc-sections\n"),
14038 isec
->owner
, isec
);
14041 /* If no non-note alloc section in this file will be kept, then
14042 we can toss out the debug and special sections. */
14046 /* Keep debug and special sections like .comment when they are
14047 not part of a group. Also keep section groups that contain
14048 just debug sections or special sections. NB: Sections with
14049 linked-to section has been handled above. */
14050 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14052 if ((isec
->flags
& SEC_GROUP
) != 0)
14053 _bfd_elf_gc_mark_debug_special_section_group (isec
);
14054 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
14055 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
14056 && elf_next_in_group (isec
) == NULL
14057 && elf_linked_to_section (isec
) == NULL
)
14059 if (isec
->gc_mark
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
14060 has_kept_debug_info
= true;
14063 /* Look for CODE sections which are going to be discarded,
14064 and find and discard any fragmented debug sections which
14065 are associated with that code section. */
14066 if (debug_frag_seen
)
14067 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14068 if ((isec
->flags
& SEC_CODE
) != 0
14069 && isec
->gc_mark
== 0)
14074 ilen
= strlen (isec
->name
);
14076 /* Association is determined by the name of the debug
14077 section containing the name of the code section as
14078 a suffix. For example .debug_line.text.foo is a
14079 debug section associated with .text.foo. */
14080 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
14084 if (dsec
->gc_mark
== 0
14085 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
14088 dlen
= strlen (dsec
->name
);
14091 && strncmp (dsec
->name
+ (dlen
- ilen
),
14092 isec
->name
, ilen
) == 0)
14097 /* Mark debug sections referenced by kept debug sections. */
14098 if (has_kept_debug_info
)
14099 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14101 && (isec
->flags
& SEC_DEBUGGING
) != 0)
14102 if (!_bfd_elf_gc_mark (info
, isec
,
14103 elf_gc_mark_debug_section
))
14110 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
14113 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14115 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
14119 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
14120 || elf_object_id (sub
) != elf_hash_table_id (elf_hash_table (info
))
14121 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
14124 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14127 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
14129 /* When any section in a section group is kept, we keep all
14130 sections in the section group. If the first member of
14131 the section group is excluded, we will also exclude the
14133 if (o
->flags
& SEC_GROUP
)
14135 asection
*first
= elf_next_in_group (o
);
14136 o
->gc_mark
= first
->gc_mark
;
14142 /* Skip sweeping sections already excluded. */
14143 if (o
->flags
& SEC_EXCLUDE
)
14146 /* Since this is early in the link process, it is simple
14147 to remove a section from the output. */
14148 o
->flags
|= SEC_EXCLUDE
;
14150 if (info
->print_gc_sections
&& o
->size
!= 0)
14151 /* xgettext:c-format */
14152 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14160 /* Propagate collected vtable information. This is called through
14161 elf_link_hash_traverse. */
14164 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
14166 /* Those that are not vtables. */
14168 || h
->u2
.vtable
== NULL
14169 || h
->u2
.vtable
->parent
== NULL
)
14172 /* Those vtables that do not have parents, we cannot merge. */
14173 if (h
->u2
.vtable
->parent
== (struct elf_link_hash_entry
*) -1)
14176 /* If we've already been done, exit. */
14177 if (h
->u2
.vtable
->used
&& h
->u2
.vtable
->used
[-1])
14180 /* Make sure the parent's table is up to date. */
14181 elf_gc_propagate_vtable_entries_used (h
->u2
.vtable
->parent
, okp
);
14183 if (h
->u2
.vtable
->used
== NULL
)
14185 /* None of this table's entries were referenced. Re-use the
14187 h
->u2
.vtable
->used
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
14188 h
->u2
.vtable
->size
= h
->u2
.vtable
->parent
->u2
.vtable
->size
;
14195 /* Or the parent's entries into ours. */
14196 cu
= h
->u2
.vtable
->used
;
14198 pu
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
14201 const struct elf_backend_data
*bed
;
14202 unsigned int log_file_align
;
14204 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
14205 log_file_align
= bed
->s
->log_file_align
;
14206 n
= h
->u2
.vtable
->parent
->u2
.vtable
->size
>> log_file_align
;
14220 struct link_info_ok
14222 struct bfd_link_info
*info
;
14227 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
,
14231 bfd_vma hstart
, hend
;
14232 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
14233 const struct elf_backend_data
*bed
;
14234 unsigned int log_file_align
;
14235 struct link_info_ok
*info
= (struct link_info_ok
*) ptr
;
14237 /* Take care of both those symbols that do not describe vtables as
14238 well as those that are not loaded. */
14240 || h
->u2
.vtable
== NULL
14241 || h
->u2
.vtable
->parent
== NULL
)
14244 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
14245 || h
->root
.type
== bfd_link_hash_defweak
);
14247 sec
= h
->root
.u
.def
.section
;
14248 hstart
= h
->root
.u
.def
.value
;
14249 hend
= hstart
+ h
->size
;
14251 relstart
= _bfd_elf_link_info_read_relocs (sec
->owner
, info
->info
,
14252 sec
, NULL
, NULL
, true);
14254 return info
->ok
= false;
14255 bed
= get_elf_backend_data (sec
->owner
);
14256 log_file_align
= bed
->s
->log_file_align
;
14258 relend
= relstart
+ sec
->reloc_count
;
14260 for (rel
= relstart
; rel
< relend
; ++rel
)
14261 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
14263 /* If the entry is in use, do nothing. */
14264 if (h
->u2
.vtable
->used
14265 && (rel
->r_offset
- hstart
) < h
->u2
.vtable
->size
)
14267 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
14268 if (h
->u2
.vtable
->used
[entry
])
14271 /* Otherwise, kill it. */
14272 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
14278 /* Mark sections containing dynamically referenced symbols. When
14279 building shared libraries, we must assume that any visible symbol is
14283 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
14285 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
14286 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
14288 if ((h
->root
.type
== bfd_link_hash_defined
14289 || h
->root
.type
== bfd_link_hash_defweak
)
14291 || h
->root
.ldscript_def
14292 || !info
->start_stop_gc
)
14293 && ((h
->ref_dynamic
&& !h
->forced_local
)
14294 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
14295 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
14296 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
14297 && (!bfd_link_executable (info
)
14298 || info
->gc_keep_exported
14299 || info
->export_dynamic
14302 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
14303 && (h
->versioned
>= versioned
14304 || !bfd_hide_sym_by_version (info
->version_info
,
14305 h
->root
.root
.string
)))))
14306 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14311 /* Keep all sections containing symbols undefined on the command-line,
14312 and the section containing the entry symbol. */
14315 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
14317 struct bfd_sym_chain
*sym
;
14319 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
14321 struct elf_link_hash_entry
*h
;
14323 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
14324 false, false, false);
14327 && (h
->root
.type
== bfd_link_hash_defined
14328 || h
->root
.type
== bfd_link_hash_defweak
)
14329 && !bfd_is_const_section (h
->root
.u
.def
.section
))
14330 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14335 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
14336 struct bfd_link_info
*info
)
14338 bfd
*ibfd
= info
->input_bfds
;
14340 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
14343 struct elf_reloc_cookie cookie
;
14345 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
14347 sec
= ibfd
->sections
;
14348 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14351 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
14354 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
14356 if (startswith (bfd_section_name (sec
), ".eh_frame_entry")
14357 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
14359 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
14360 fini_reloc_cookie_rels (&cookie
, sec
);
14367 /* Do mark and sweep of unused sections. */
14370 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
14374 elf_gc_mark_hook_fn gc_mark_hook
;
14375 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14376 struct elf_link_hash_table
*htab
;
14377 struct link_info_ok info_ok
;
14379 if (!bed
->can_gc_sections
14380 || !is_elf_hash_table (info
->hash
))
14382 _bfd_error_handler(_("warning: gc-sections option ignored"));
14386 bed
->gc_keep (info
);
14387 htab
= elf_hash_table (info
);
14389 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14390 at the .eh_frame section if we can mark the FDEs individually. */
14391 for (sub
= info
->input_bfds
;
14392 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
14393 sub
= sub
->link
.next
)
14396 struct elf_reloc_cookie cookie
;
14398 sec
= sub
->sections
;
14399 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14401 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
14402 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
14404 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
14405 if (elf_section_data (sec
)->sec_info
14406 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
14407 elf_eh_frame_section (sub
) = sec
;
14408 fini_reloc_cookie_for_section (&cookie
, sec
);
14409 sec
= bfd_get_next_section_by_name (NULL
, sec
);
14413 /* Apply transitive closure to the vtable entry usage info. */
14414 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
14418 /* Kill the vtable relocations that were not used. */
14419 info_ok
.info
= info
;
14421 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &info_ok
);
14425 /* Mark dynamically referenced symbols. */
14426 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
14427 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
14429 /* Grovel through relocs to find out who stays ... */
14430 gc_mark_hook
= bed
->gc_mark_hook
;
14431 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
14435 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
14436 || elf_object_id (sub
) != elf_hash_table_id (htab
)
14437 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
14441 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14444 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14445 Also treat note sections as a root, if the section is not part
14446 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14447 well as FINI_ARRAY sections for ld -r. */
14448 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
14450 && (o
->flags
& SEC_EXCLUDE
) == 0
14451 && ((o
->flags
& SEC_KEEP
) != 0
14452 || (bfd_link_relocatable (info
)
14453 && ((elf_section_data (o
)->this_hdr
.sh_type
14454 == SHT_PREINIT_ARRAY
)
14455 || (elf_section_data (o
)->this_hdr
.sh_type
14457 || (elf_section_data (o
)->this_hdr
.sh_type
14458 == SHT_FINI_ARRAY
)))
14459 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
14460 && elf_next_in_group (o
) == NULL
14461 && elf_linked_to_section (o
) == NULL
)
14462 || ((elf_tdata (sub
)->has_gnu_osabi
& elf_gnu_osabi_retain
)
14463 && (elf_section_flags (o
) & SHF_GNU_RETAIN
))))
14465 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
14470 /* Allow the backend to mark additional target specific sections. */
14471 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
14473 /* ... and mark SEC_EXCLUDE for those that go. */
14474 return elf_gc_sweep (abfd
, info
);
14477 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14480 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
14482 struct elf_link_hash_entry
*h
,
14485 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
14486 struct elf_link_hash_entry
**search
, *child
;
14487 size_t extsymcount
;
14488 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14490 /* The sh_info field of the symtab header tells us where the
14491 external symbols start. We don't care about the local symbols at
14493 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
14494 if (!elf_bad_symtab (abfd
))
14495 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
14497 sym_hashes
= elf_sym_hashes (abfd
);
14498 sym_hashes_end
= PTR_ADD (sym_hashes
, extsymcount
);
14500 /* Hunt down the child symbol, which is in this section at the same
14501 offset as the relocation. */
14502 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
14504 if ((child
= *search
) != NULL
14505 && (child
->root
.type
== bfd_link_hash_defined
14506 || child
->root
.type
== bfd_link_hash_defweak
)
14507 && child
->root
.u
.def
.section
== sec
14508 && child
->root
.u
.def
.value
== offset
)
14512 /* xgettext:c-format */
14513 _bfd_error_handler (_("%pB: %pA+%#" PRIx64
": no symbol found for INHERIT"),
14514 abfd
, sec
, (uint64_t) offset
);
14515 bfd_set_error (bfd_error_invalid_operation
);
14519 if (!child
->u2
.vtable
)
14521 child
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14522 bfd_zalloc (abfd
, sizeof (*child
->u2
.vtable
)));
14523 if (!child
->u2
.vtable
)
14528 /* This *should* only be the absolute section. It could potentially
14529 be that someone has defined a non-global vtable though, which
14530 would be bad. It isn't worth paging in the local symbols to be
14531 sure though; that case should simply be handled by the assembler. */
14533 child
->u2
.vtable
->parent
= (struct elf_link_hash_entry
*) -1;
14536 child
->u2
.vtable
->parent
= h
;
14541 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14544 bfd_elf_gc_record_vtentry (bfd
*abfd
, asection
*sec
,
14545 struct elf_link_hash_entry
*h
,
14548 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14549 unsigned int log_file_align
= bed
->s
->log_file_align
;
14553 /* xgettext:c-format */
14554 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14556 bfd_set_error (bfd_error_bad_value
);
14562 h
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14563 bfd_zalloc (abfd
, sizeof (*h
->u2
.vtable
)));
14568 if (addend
>= h
->u2
.vtable
->size
)
14570 size_t size
, bytes
, file_align
;
14571 bool *ptr
= h
->u2
.vtable
->used
;
14573 /* While the symbol is undefined, we have to be prepared to handle
14575 file_align
= 1 << log_file_align
;
14576 if (h
->root
.type
== bfd_link_hash_undefined
)
14577 size
= addend
+ file_align
;
14581 if (addend
>= size
)
14583 /* Oops! We've got a reference past the defined end of
14584 the table. This is probably a bug -- shall we warn? */
14585 size
= addend
+ file_align
;
14588 size
= (size
+ file_align
- 1) & -file_align
;
14590 /* Allocate one extra entry for use as a "done" flag for the
14591 consolidation pass. */
14592 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bool);
14596 ptr
= (bool *) bfd_realloc (ptr
- 1, bytes
);
14602 oldbytes
= (((h
->u2
.vtable
->size
>> log_file_align
) + 1)
14604 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
14608 ptr
= (bool *) bfd_zmalloc (bytes
);
14613 /* And arrange for that done flag to be at index -1. */
14614 h
->u2
.vtable
->used
= ptr
+ 1;
14615 h
->u2
.vtable
->size
= size
;
14618 h
->u2
.vtable
->used
[addend
>> log_file_align
] = true;
14623 /* Map an ELF section header flag to its corresponding string. */
14627 flagword flag_value
;
14628 } elf_flags_to_name_table
;
14630 static const elf_flags_to_name_table elf_flags_to_names
[] =
14632 { "SHF_WRITE", SHF_WRITE
},
14633 { "SHF_ALLOC", SHF_ALLOC
},
14634 { "SHF_EXECINSTR", SHF_EXECINSTR
},
14635 { "SHF_MERGE", SHF_MERGE
},
14636 { "SHF_STRINGS", SHF_STRINGS
},
14637 { "SHF_INFO_LINK", SHF_INFO_LINK
},
14638 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
14639 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
14640 { "SHF_GROUP", SHF_GROUP
},
14641 { "SHF_TLS", SHF_TLS
},
14642 { "SHF_MASKOS", SHF_MASKOS
},
14643 { "SHF_EXCLUDE", SHF_EXCLUDE
},
14646 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14648 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
14649 struct flag_info
*flaginfo
,
14652 const bfd_vma sh_flags
= elf_section_flags (section
);
14654 if (!flaginfo
->flags_initialized
)
14656 bfd
*obfd
= info
->output_bfd
;
14657 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14658 struct flag_info_list
*tf
= flaginfo
->flag_list
;
14660 int without_hex
= 0;
14662 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
14665 flagword (*lookup
) (char *);
14667 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
14668 if (lookup
!= NULL
)
14670 flagword hexval
= (*lookup
) ((char *) tf
->name
);
14674 if (tf
->with
== with_flags
)
14675 with_hex
|= hexval
;
14676 else if (tf
->with
== without_flags
)
14677 without_hex
|= hexval
;
14682 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
14684 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
14686 if (tf
->with
== with_flags
)
14687 with_hex
|= elf_flags_to_names
[i
].flag_value
;
14688 else if (tf
->with
== without_flags
)
14689 without_hex
|= elf_flags_to_names
[i
].flag_value
;
14696 info
->callbacks
->einfo
14697 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
14701 flaginfo
->flags_initialized
= true;
14702 flaginfo
->only_with_flags
|= with_hex
;
14703 flaginfo
->not_with_flags
|= without_hex
;
14706 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
14709 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
14715 struct alloc_got_off_arg
{
14717 struct bfd_link_info
*info
;
14720 /* We need a special top-level link routine to convert got reference counts
14721 to real got offsets. */
14724 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
14726 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
14727 bfd
*obfd
= gofarg
->info
->output_bfd
;
14728 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14730 if (h
->got
.refcount
> 0)
14732 h
->got
.offset
= gofarg
->gotoff
;
14733 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
14736 h
->got
.offset
= (bfd_vma
) -1;
14741 /* And an accompanying bit to work out final got entry offsets once
14742 we're done. Should be called from final_link. */
14745 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
14746 struct bfd_link_info
*info
)
14749 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14751 struct alloc_got_off_arg gofarg
;
14753 BFD_ASSERT (abfd
== info
->output_bfd
);
14755 if (! is_elf_hash_table (info
->hash
))
14758 /* The GOT offset is relative to the .got section, but the GOT header is
14759 put into the .got.plt section, if the backend uses it. */
14760 if (bed
->want_got_plt
)
14763 gotoff
= bed
->got_header_size
;
14765 /* Do the local .got entries first. */
14766 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
14768 bfd_signed_vma
*local_got
;
14769 size_t j
, locsymcount
;
14770 Elf_Internal_Shdr
*symtab_hdr
;
14772 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
14775 local_got
= elf_local_got_refcounts (i
);
14779 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
14780 if (elf_bad_symtab (i
))
14781 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
14783 locsymcount
= symtab_hdr
->sh_info
;
14785 for (j
= 0; j
< locsymcount
; ++j
)
14787 if (local_got
[j
] > 0)
14789 local_got
[j
] = gotoff
;
14790 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
14793 local_got
[j
] = (bfd_vma
) -1;
14797 /* Then the global .got entries. .plt refcounts are handled by
14798 adjust_dynamic_symbol */
14799 gofarg
.gotoff
= gotoff
;
14800 gofarg
.info
= info
;
14801 elf_link_hash_traverse (elf_hash_table (info
),
14802 elf_gc_allocate_got_offsets
,
14807 /* Many folk need no more in the way of final link than this, once
14808 got entry reference counting is enabled. */
14811 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
14813 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
14816 /* Invoke the regular ELF backend linker to do all the work. */
14817 return bfd_elf_final_link (abfd
, info
);
14821 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
14823 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
14825 if (rcookie
->bad_symtab
)
14826 rcookie
->rel
= rcookie
->rels
;
14828 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
14830 unsigned long r_symndx
;
14832 if (! rcookie
->bad_symtab
)
14833 if (rcookie
->rel
->r_offset
> offset
)
14835 if (rcookie
->rel
->r_offset
!= offset
)
14838 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
14839 if (r_symndx
== STN_UNDEF
)
14842 if (r_symndx
>= rcookie
->locsymcount
14843 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
14845 struct elf_link_hash_entry
*h
;
14847 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
14849 while (h
->root
.type
== bfd_link_hash_indirect
14850 || h
->root
.type
== bfd_link_hash_warning
)
14851 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
14853 if ((h
->root
.type
== bfd_link_hash_defined
14854 || h
->root
.type
== bfd_link_hash_defweak
)
14855 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
14856 || h
->root
.u
.def
.section
->kept_section
!= NULL
14857 || discarded_section (h
->root
.u
.def
.section
)))
14862 /* It's not a relocation against a global symbol,
14863 but it could be a relocation against a local
14864 symbol for a discarded section. */
14866 Elf_Internal_Sym
*isym
;
14868 /* Need to: get the symbol; get the section. */
14869 isym
= &rcookie
->locsyms
[r_symndx
];
14870 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
14872 && (isec
->kept_section
!= NULL
14873 || discarded_section (isec
)))
14881 /* Discard unneeded references to discarded sections.
14882 Returns -1 on error, 1 if any section's size was changed, 0 if
14883 nothing changed. This function assumes that the relocations are in
14884 sorted order, which is true for all known assemblers. */
14887 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
14889 struct elf_reloc_cookie cookie
;
14894 if (info
->traditional_format
14895 || !is_elf_hash_table (info
->hash
))
14898 o
= bfd_get_section_by_name (output_bfd
, ".stab");
14903 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14906 || i
->reloc_count
== 0
14907 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
14911 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14914 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14917 if (_bfd_discard_section_stabs (abfd
, i
,
14918 elf_section_data (i
)->sec_info
,
14919 bfd_elf_reloc_symbol_deleted_p
,
14923 fini_reloc_cookie_for_section (&cookie
, i
);
14928 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
14929 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
14933 int eh_changed
= 0;
14934 unsigned int eh_alignment
; /* Octets. */
14936 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14942 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14945 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14948 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
14949 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
14950 bfd_elf_reloc_symbol_deleted_p
,
14954 if (i
->size
!= i
->rawsize
)
14958 fini_reloc_cookie_for_section (&cookie
, i
);
14961 eh_alignment
= ((1 << o
->alignment_power
)
14962 * bfd_octets_per_byte (output_bfd
, o
));
14963 /* Skip over zero terminator, and prevent empty sections from
14964 adding alignment padding at the end. */
14965 for (i
= o
->map_tail
.s
; i
!= NULL
; i
= i
->map_tail
.s
)
14967 i
->flags
|= SEC_EXCLUDE
;
14968 else if (i
->size
> 4)
14970 /* The last non-empty eh_frame section doesn't need padding. */
14973 /* Any prior sections must pad the last FDE out to the output
14974 section alignment. Otherwise we might have zero padding
14975 between sections, which would be seen as a terminator. */
14976 for (; i
!= NULL
; i
= i
->map_tail
.s
)
14978 /* All but the last zero terminator should have been removed. */
14983 = (i
->size
+ eh_alignment
- 1) & -eh_alignment
;
14984 if (i
->size
!= size
)
14992 elf_link_hash_traverse (elf_hash_table (info
),
14993 _bfd_elf_adjust_eh_frame_global_symbol
, NULL
);
14996 o
= bfd_get_section_by_name (output_bfd
, ".sframe");
15001 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
15007 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
15010 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
15013 if (_bfd_elf_parse_sframe (abfd
, info
, i
, &cookie
))
15015 if (_bfd_elf_discard_section_sframe (i
,
15016 bfd_elf_reloc_symbol_deleted_p
,
15019 if (i
->size
!= i
->rawsize
)
15023 fini_reloc_cookie_for_section (&cookie
, i
);
15025 /* Update the reference to the output .sframe section. Used to
15026 determine later if PT_GNU_SFRAME segment is to be generated. */
15027 if (!_bfd_elf_set_section_sframe (output_bfd
, info
))
15031 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
15033 const struct elf_backend_data
*bed
;
15036 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
15038 s
= abfd
->sections
;
15039 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
15042 bed
= get_elf_backend_data (abfd
);
15044 if (bed
->elf_backend_discard_info
!= NULL
)
15046 if (!init_reloc_cookie (&cookie
, info
, abfd
))
15049 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
15052 fini_reloc_cookie (&cookie
, abfd
);
15056 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
15057 _bfd_elf_end_eh_frame_parsing (info
);
15059 if (info
->eh_frame_hdr_type
15060 && !bfd_link_relocatable (info
)
15061 && _bfd_elf_discard_section_eh_frame_hdr (info
))
15068 _bfd_elf_section_already_linked (bfd
*abfd
,
15070 struct bfd_link_info
*info
)
15073 const char *name
, *key
;
15074 struct bfd_section_already_linked
*l
;
15075 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
15077 if (sec
->output_section
== bfd_abs_section_ptr
)
15080 flags
= sec
->flags
;
15082 /* Return if it isn't a linkonce section. A comdat group section
15083 also has SEC_LINK_ONCE set. */
15084 if ((flags
& SEC_LINK_ONCE
) == 0)
15087 /* Don't put group member sections on our list of already linked
15088 sections. They are handled as a group via their group section. */
15089 if (elf_sec_group (sec
) != NULL
)
15092 /* For a SHT_GROUP section, use the group signature as the key. */
15094 if ((flags
& SEC_GROUP
) != 0
15095 && elf_next_in_group (sec
) != NULL
15096 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
15097 key
= elf_group_name (elf_next_in_group (sec
));
15100 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
15101 if (startswith (name
, ".gnu.linkonce.")
15102 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
15105 /* Must be a user linkonce section that doesn't follow gcc's
15106 naming convention. In this case we won't be matching
15107 single member groups. */
15111 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
15113 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15115 /* We may have 2 different types of sections on the list: group
15116 sections with a signature of <key> (<key> is some string),
15117 and linkonce sections named .gnu.linkonce.<type>.<key>.
15118 Match like sections. LTO plugin sections are an exception.
15119 They are always named .gnu.linkonce.t.<key> and match either
15120 type of section. */
15121 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
15122 && ((flags
& SEC_GROUP
) != 0
15123 || strcmp (name
, l
->sec
->name
) == 0))
15124 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0
15125 || (sec
->owner
->flags
& BFD_PLUGIN
) != 0)
15127 /* The section has already been linked. See if we should
15128 issue a warning. */
15129 if (!_bfd_handle_already_linked (sec
, l
, info
))
15132 if (flags
& SEC_GROUP
)
15134 asection
*first
= elf_next_in_group (sec
);
15135 asection
*s
= first
;
15139 s
->output_section
= bfd_abs_section_ptr
;
15140 /* Record which group discards it. */
15141 s
->kept_section
= l
->sec
;
15142 s
= elf_next_in_group (s
);
15143 /* These lists are circular. */
15153 /* A single member comdat group section may be discarded by a
15154 linkonce section and vice versa. */
15155 if ((flags
& SEC_GROUP
) != 0)
15157 asection
*first
= elf_next_in_group (sec
);
15159 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
15160 /* Check this single member group against linkonce sections. */
15161 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15162 if ((l
->sec
->flags
& SEC_GROUP
) == 0
15163 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
15165 first
->output_section
= bfd_abs_section_ptr
;
15166 first
->kept_section
= l
->sec
;
15167 sec
->output_section
= bfd_abs_section_ptr
;
15172 /* Check this linkonce section against single member groups. */
15173 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15174 if (l
->sec
->flags
& SEC_GROUP
)
15176 asection
*first
= elf_next_in_group (l
->sec
);
15179 && elf_next_in_group (first
) == first
15180 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
15182 sec
->output_section
= bfd_abs_section_ptr
;
15183 sec
->kept_section
= first
;
15188 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15189 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15190 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15191 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15192 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15193 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15194 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15195 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15196 The reverse order cannot happen as there is never a bfd with only the
15197 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15198 matter as here were are looking only for cross-bfd sections. */
15200 if ((flags
& SEC_GROUP
) == 0 && startswith (name
, ".gnu.linkonce.r."))
15201 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15202 if ((l
->sec
->flags
& SEC_GROUP
) == 0
15203 && startswith (l
->sec
->name
, ".gnu.linkonce.t."))
15205 if (abfd
!= l
->sec
->owner
)
15206 sec
->output_section
= bfd_abs_section_ptr
;
15210 /* This is the first section with this name. Record it. */
15211 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
15212 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
15213 return sec
->output_section
== bfd_abs_section_ptr
;
15217 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
15219 return sym
->st_shndx
== SHN_COMMON
;
15223 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
15229 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
15231 return bfd_com_section_ptr
;
15235 _bfd_elf_default_got_elt_size (bfd
*abfd
,
15236 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
15237 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
15238 bfd
*ibfd ATTRIBUTE_UNUSED
,
15239 unsigned long symndx ATTRIBUTE_UNUSED
)
15241 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15242 return bed
->s
->arch_size
/ 8;
15245 /* Routines to support the creation of dynamic relocs. */
15247 /* Returns the name of the dynamic reloc section associated with SEC. */
15249 static const char *
15250 get_dynamic_reloc_section_name (bfd
* abfd
,
15255 const char *old_name
= bfd_section_name (sec
);
15256 const char *prefix
= is_rela
? ".rela" : ".rel";
15258 if (old_name
== NULL
)
15261 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
15262 sprintf (name
, "%s%s", prefix
, old_name
);
15267 /* Returns the dynamic reloc section associated with SEC.
15268 If necessary compute the name of the dynamic reloc section based
15269 on SEC's name (looked up in ABFD's string table) and the setting
15273 _bfd_elf_get_dynamic_reloc_section (bfd
*abfd
,
15277 asection
*reloc_sec
= elf_section_data (sec
)->sreloc
;
15279 if (reloc_sec
== NULL
)
15281 const char *name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
15285 reloc_sec
= bfd_get_linker_section (abfd
, name
);
15287 if (reloc_sec
!= NULL
)
15288 elf_section_data (sec
)->sreloc
= reloc_sec
;
15295 /* Returns the dynamic reloc section associated with SEC. If the
15296 section does not exist it is created and attached to the DYNOBJ
15297 bfd and stored in the SRELOC field of SEC's elf_section_data
15300 ALIGNMENT is the alignment for the newly created section and
15301 IS_RELA defines whether the name should be .rela.<SEC's name>
15302 or .rel.<SEC's name>. The section name is looked up in the
15303 string table associated with ABFD. */
15306 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
15308 unsigned int alignment
,
15312 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
15314 if (reloc_sec
== NULL
)
15316 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
15321 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
15323 if (reloc_sec
== NULL
)
15325 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
15326 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
15327 if ((sec
->flags
& SEC_ALLOC
) != 0)
15328 flags
|= SEC_ALLOC
| SEC_LOAD
;
15330 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
15331 if (reloc_sec
!= NULL
)
15333 /* _bfd_elf_get_sec_type_attr chooses a section type by
15334 name. Override as it may be wrong, eg. for a user
15335 section named "auto" we'll get ".relauto" which is
15336 seen to be a .rela section. */
15337 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
15338 if (!bfd_set_section_alignment (reloc_sec
, alignment
))
15343 elf_section_data (sec
)->sreloc
= reloc_sec
;
15349 /* Copy the ELF symbol type and other attributes for a linker script
15350 assignment from HSRC to HDEST. Generally this should be treated as
15351 if we found a strong non-dynamic definition for HDEST (except that
15352 ld ignores multiple definition errors). */
15354 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
15355 struct bfd_link_hash_entry
*hdest
,
15356 struct bfd_link_hash_entry
*hsrc
)
15358 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
15359 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
15360 Elf_Internal_Sym isym
;
15362 ehdest
->type
= ehsrc
->type
;
15363 ehdest
->target_internal
= ehsrc
->target_internal
;
15365 isym
.st_other
= ehsrc
->other
;
15366 elf_merge_st_other (abfd
, ehdest
, isym
.st_other
, NULL
, true, false);
15369 /* Append a RELA relocation REL to section S in BFD. */
15372 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15374 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15375 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
15376 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
15377 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
15380 /* Append a REL relocation REL to section S in BFD. */
15383 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15385 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15386 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
15387 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
15388 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);
15391 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15393 struct bfd_link_hash_entry
*
15394 bfd_elf_define_start_stop (struct bfd_link_info
*info
,
15395 const char *symbol
, asection
*sec
)
15397 struct elf_link_hash_entry
*h
;
15399 h
= elf_link_hash_lookup (elf_hash_table (info
), symbol
,
15400 false, false, true);
15401 /* NB: Common symbols will be turned into definition later. */
15403 && !h
->root
.ldscript_def
15404 && (h
->root
.type
== bfd_link_hash_undefined
15405 || h
->root
.type
== bfd_link_hash_undefweak
15406 || ((h
->ref_regular
|| h
->def_dynamic
)
15408 && h
->root
.type
!= bfd_link_hash_common
)))
15410 bool was_dynamic
= h
->ref_dynamic
|| h
->def_dynamic
;
15411 h
->verinfo
.verdef
= NULL
;
15412 h
->root
.type
= bfd_link_hash_defined
;
15413 h
->root
.u
.def
.section
= sec
;
15414 h
->root
.u
.def
.value
= 0;
15415 h
->def_regular
= 1;
15416 h
->def_dynamic
= 0;
15418 h
->u2
.start_stop_section
= sec
;
15419 if (symbol
[0] == '.')
15421 /* .startof. and .sizeof. symbols are local. */
15422 const struct elf_backend_data
*bed
;
15423 bed
= get_elf_backend_data (info
->output_bfd
);
15424 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
15428 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
15429 h
->other
= ((h
->other
& ~ELF_ST_VISIBILITY (-1))
15430 | info
->start_stop_visibility
);
15432 bfd_elf_link_record_dynamic_symbol (info
, h
);
15439 /* Find dynamic relocs for H that apply to read-only sections. */
15442 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry
*h
)
15444 struct elf_dyn_relocs
*p
;
15446 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
15448 asection
*s
= p
->sec
->output_section
;
15450 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
15456 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15457 read-only sections. */
15460 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry
*h
, void *inf
)
15464 if (h
->root
.type
== bfd_link_hash_indirect
)
15467 sec
= _bfd_elf_readonly_dynrelocs (h
);
15470 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
15472 info
->flags
|= DF_TEXTREL
;
15473 /* xgettext:c-format */
15474 info
->callbacks
->minfo (_("%pB: dynamic relocation against `%pT' "
15475 "in read-only section `%pA'\n"),
15476 sec
->owner
, h
->root
.root
.string
, sec
);
15478 if (bfd_link_textrel_check (info
))
15479 /* xgettext:c-format */
15480 info
->callbacks
->einfo (_("%P: %pB: warning: relocation against `%s' "
15481 "in read-only section `%pA'\n"),
15482 sec
->owner
, h
->root
.root
.string
, sec
);
15484 /* Not an error, just cut short the traversal. */
15490 /* Add dynamic tags. */
15493 _bfd_elf_add_dynamic_tags (bfd
*output_bfd
, struct bfd_link_info
*info
,
15494 bool need_dynamic_reloc
)
15496 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
15498 if (htab
->dynamic_sections_created
)
15500 /* Add some entries to the .dynamic section. We fill in the
15501 values later, in finish_dynamic_sections, but we must add
15502 the entries now so that we get the correct size for the
15503 .dynamic section. The DT_DEBUG entry is filled in by the
15504 dynamic linker and used by the debugger. */
15505 #define add_dynamic_entry(TAG, VAL) \
15506 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15508 const struct elf_backend_data
*bed
15509 = get_elf_backend_data (output_bfd
);
15511 if (bfd_link_executable (info
))
15513 if (!add_dynamic_entry (DT_DEBUG
, 0))
15517 if (htab
->dt_pltgot_required
|| htab
->splt
->size
!= 0)
15519 /* DT_PLTGOT is used by prelink even if there is no PLT
15521 if (!add_dynamic_entry (DT_PLTGOT
, 0))
15525 if (htab
->dt_jmprel_required
|| htab
->srelplt
->size
!= 0)
15527 if (!add_dynamic_entry (DT_PLTRELSZ
, 0)
15528 || !add_dynamic_entry (DT_PLTREL
,
15529 (bed
->rela_plts_and_copies_p
15530 ? DT_RELA
: DT_REL
))
15531 || !add_dynamic_entry (DT_JMPREL
, 0))
15535 if (htab
->tlsdesc_plt
15536 && (!add_dynamic_entry (DT_TLSDESC_PLT
, 0)
15537 || !add_dynamic_entry (DT_TLSDESC_GOT
, 0)))
15540 if (need_dynamic_reloc
)
15542 if (bed
->rela_plts_and_copies_p
)
15544 if (!add_dynamic_entry (DT_RELA
, 0)
15545 || !add_dynamic_entry (DT_RELASZ
, 0)
15546 || !add_dynamic_entry (DT_RELAENT
,
15547 bed
->s
->sizeof_rela
))
15552 if (!add_dynamic_entry (DT_REL
, 0)
15553 || !add_dynamic_entry (DT_RELSZ
, 0)
15554 || !add_dynamic_entry (DT_RELENT
,
15555 bed
->s
->sizeof_rel
))
15559 /* If any dynamic relocs apply to a read-only section,
15560 then we need a DT_TEXTREL entry. */
15561 if ((info
->flags
& DF_TEXTREL
) == 0)
15562 elf_link_hash_traverse (htab
, _bfd_elf_maybe_set_textrel
,
15565 if ((info
->flags
& DF_TEXTREL
) != 0)
15567 if (htab
->ifunc_resolvers
)
15568 info
->callbacks
->einfo
15569 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15570 "may result in a segfault at runtime; recompile with %s\n"),
15571 bfd_link_dll (info
) ? "-fPIC" : "-fPIE");
15573 if (!add_dynamic_entry (DT_TEXTREL
, 0))
15578 #undef add_dynamic_entry