1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2021 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"
42 /* This struct is used to pass information to routines called via
43 elf_link_hash_traverse which must return failure. */
45 struct elf_info_failed
47 struct bfd_link_info
*info
;
51 /* This structure is used to pass information to
52 _bfd_elf_link_find_version_dependencies. */
54 struct elf_find_verdep_info
56 /* General link information. */
57 struct bfd_link_info
*info
;
58 /* The number of dependencies. */
60 /* Whether we had a failure. */
64 static bfd_boolean _bfd_elf_fix_symbol_flags
65 (struct elf_link_hash_entry
*, struct elf_info_failed
*);
68 _bfd_elf_section_for_symbol (struct elf_reloc_cookie
*cookie
,
69 unsigned long r_symndx
,
72 if (r_symndx
>= cookie
->locsymcount
73 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
75 struct elf_link_hash_entry
*h
;
77 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
79 while (h
->root
.type
== bfd_link_hash_indirect
80 || h
->root
.type
== bfd_link_hash_warning
)
81 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
83 if ((h
->root
.type
== bfd_link_hash_defined
84 || h
->root
.type
== bfd_link_hash_defweak
)
85 && discarded_section (h
->root
.u
.def
.section
))
86 return h
->root
.u
.def
.section
;
92 /* It's not a relocation against a global symbol,
93 but it could be a relocation against a local
94 symbol for a discarded section. */
96 Elf_Internal_Sym
*isym
;
98 /* Need to: get the symbol; get the section. */
99 isym
= &cookie
->locsyms
[r_symndx
];
100 isec
= bfd_section_from_elf_index (cookie
->abfd
, isym
->st_shndx
);
102 && discard
? discarded_section (isec
) : 1)
108 /* Define a symbol in a dynamic linkage section. */
110 struct elf_link_hash_entry
*
111 _bfd_elf_define_linkage_sym (bfd
*abfd
,
112 struct bfd_link_info
*info
,
116 struct elf_link_hash_entry
*h
;
117 struct bfd_link_hash_entry
*bh
;
118 const struct elf_backend_data
*bed
;
120 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
123 /* Zap symbol defined in an as-needed lib that wasn't linked.
124 This is a symptom of a larger problem: Absolute symbols
125 defined in shared libraries can't be overridden, because we
126 lose the link to the bfd which is via the symbol section. */
127 h
->root
.type
= bfd_link_hash_new
;
133 bed
= get_elf_backend_data (abfd
);
134 if (!_bfd_generic_link_add_one_symbol (info
, abfd
, name
, BSF_GLOBAL
,
135 sec
, 0, NULL
, FALSE
, bed
->collect
,
138 h
= (struct elf_link_hash_entry
*) bh
;
139 BFD_ASSERT (h
!= NULL
);
142 h
->root
.linker_def
= 1;
143 h
->type
= STT_OBJECT
;
144 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
145 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
147 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
152 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
156 struct elf_link_hash_entry
*h
;
157 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
158 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
160 /* This function may be called more than once. */
161 if (htab
->sgot
!= NULL
)
164 flags
= bed
->dynamic_sec_flags
;
166 s
= bfd_make_section_anyway_with_flags (abfd
,
167 (bed
->rela_plts_and_copies_p
168 ? ".rela.got" : ".rel.got"),
169 (bed
->dynamic_sec_flags
172 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
176 s
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
178 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
182 if (bed
->want_got_plt
)
184 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
186 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
191 /* The first bit of the global offset table is the header. */
192 s
->size
+= bed
->got_header_size
;
194 if (bed
->want_got_sym
)
196 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
197 (or .got.plt) section. We don't do this in the linker script
198 because we don't want to define the symbol if we are not creating
199 a global offset table. */
200 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
201 "_GLOBAL_OFFSET_TABLE_");
202 elf_hash_table (info
)->hgot
= h
;
210 /* Create a strtab to hold the dynamic symbol names. */
212 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
214 struct elf_link_hash_table
*hash_table
;
216 hash_table
= elf_hash_table (info
);
217 if (hash_table
->dynobj
== NULL
)
219 /* We may not set dynobj, an input file holding linker created
220 dynamic sections to abfd, which may be a dynamic object with
221 its own dynamic sections. We need to find a normal input file
222 to hold linker created sections if possible. */
223 if ((abfd
->flags
& (DYNAMIC
| BFD_PLUGIN
)) != 0)
227 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link
.next
)
229 & (DYNAMIC
| BFD_LINKER_CREATED
| BFD_PLUGIN
)) == 0
230 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
231 && elf_object_id (ibfd
) == elf_hash_table_id (hash_table
)
232 && !((s
= ibfd
->sections
) != NULL
233 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
))
239 hash_table
->dynobj
= abfd
;
242 if (hash_table
->dynstr
== NULL
)
244 hash_table
->dynstr
= _bfd_elf_strtab_init ();
245 if (hash_table
->dynstr
== NULL
)
251 /* Create some sections which will be filled in with dynamic linking
252 information. ABFD is an input file which requires dynamic sections
253 to be created. The dynamic sections take up virtual memory space
254 when the final executable is run, so we need to create them before
255 addresses are assigned to the output sections. We work out the
256 actual contents and size of these sections later. */
259 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
263 const struct elf_backend_data
*bed
;
264 struct elf_link_hash_entry
*h
;
266 if (! is_elf_hash_table (info
->hash
))
269 if (elf_hash_table (info
)->dynamic_sections_created
)
272 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
275 abfd
= elf_hash_table (info
)->dynobj
;
276 bed
= get_elf_backend_data (abfd
);
278 flags
= bed
->dynamic_sec_flags
;
280 /* A dynamically linked executable has a .interp section, but a
281 shared library does not. */
282 if (bfd_link_executable (info
) && !info
->nointerp
)
284 s
= bfd_make_section_anyway_with_flags (abfd
, ".interp",
285 flags
| SEC_READONLY
);
290 /* Create sections to hold version informations. These are removed
291 if they are not needed. */
292 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_d",
293 flags
| SEC_READONLY
);
295 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
298 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version",
299 flags
| SEC_READONLY
);
301 || !bfd_set_section_alignment (s
, 1))
304 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_r",
305 flags
| SEC_READONLY
);
307 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
310 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynsym",
311 flags
| SEC_READONLY
);
313 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
315 elf_hash_table (info
)->dynsym
= s
;
317 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynstr",
318 flags
| SEC_READONLY
);
322 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynamic", flags
);
324 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
327 /* The special symbol _DYNAMIC is always set to the start of the
328 .dynamic section. We could set _DYNAMIC in a linker script, but we
329 only want to define it if we are, in fact, creating a .dynamic
330 section. We don't want to define it if there is no .dynamic
331 section, since on some ELF platforms the start up code examines it
332 to decide how to initialize the process. */
333 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC");
334 elf_hash_table (info
)->hdynamic
= h
;
340 s
= bfd_make_section_anyway_with_flags (abfd
, ".hash",
341 flags
| SEC_READONLY
);
343 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
345 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
348 if (info
->emit_gnu_hash
&& bed
->record_xhash_symbol
== NULL
)
350 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.hash",
351 flags
| SEC_READONLY
);
353 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
355 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
356 4 32-bit words followed by variable count of 64-bit words, then
357 variable count of 32-bit words. */
358 if (bed
->s
->arch_size
== 64)
359 elf_section_data (s
)->this_hdr
.sh_entsize
= 0;
361 elf_section_data (s
)->this_hdr
.sh_entsize
= 4;
364 /* Let the backend create the rest of the sections. This lets the
365 backend set the right flags. The backend will normally create
366 the .got and .plt sections. */
367 if (bed
->elf_backend_create_dynamic_sections
== NULL
368 || ! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
371 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
376 /* Create dynamic sections when linking against a dynamic object. */
379 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
381 flagword flags
, pltflags
;
382 struct elf_link_hash_entry
*h
;
384 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
385 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
387 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
388 .rel[a].bss sections. */
389 flags
= bed
->dynamic_sec_flags
;
392 if (bed
->plt_not_loaded
)
393 /* We do not clear SEC_ALLOC here because we still want the OS to
394 allocate space for the section; it's just that there's nothing
395 to read in from the object file. */
396 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
398 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
399 if (bed
->plt_readonly
)
400 pltflags
|= SEC_READONLY
;
402 s
= bfd_make_section_anyway_with_flags (abfd
, ".plt", pltflags
);
404 || !bfd_set_section_alignment (s
, bed
->plt_alignment
))
408 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
410 if (bed
->want_plt_sym
)
412 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
413 "_PROCEDURE_LINKAGE_TABLE_");
414 elf_hash_table (info
)->hplt
= h
;
419 s
= bfd_make_section_anyway_with_flags (abfd
,
420 (bed
->rela_plts_and_copies_p
421 ? ".rela.plt" : ".rel.plt"),
422 flags
| SEC_READONLY
);
424 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
428 if (! _bfd_elf_create_got_section (abfd
, info
))
431 if (bed
->want_dynbss
)
433 /* The .dynbss section is a place to put symbols which are defined
434 by dynamic objects, are referenced by regular objects, and are
435 not functions. We must allocate space for them in the process
436 image and use a R_*_COPY reloc to tell the dynamic linker to
437 initialize them at run time. The linker script puts the .dynbss
438 section into the .bss section of the final image. */
439 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynbss",
440 SEC_ALLOC
| SEC_LINKER_CREATED
);
445 if (bed
->want_dynrelro
)
447 /* Similarly, but for symbols that were originally in read-only
448 sections. This section doesn't really need to have contents,
449 but make it like other .data.rel.ro sections. */
450 s
= bfd_make_section_anyway_with_flags (abfd
, ".data.rel.ro",
457 /* The .rel[a].bss section holds copy relocs. This section is not
458 normally needed. We need to create it here, though, so that the
459 linker will map it to an output section. We can't just create it
460 only if we need it, because we will not know whether we need it
461 until we have seen all the input files, and the first time the
462 main linker code calls BFD after examining all the input files
463 (size_dynamic_sections) the input sections have already been
464 mapped to the output sections. If the section turns out not to
465 be needed, we can discard it later. We will never need this
466 section when generating a shared object, since they do not use
468 if (bfd_link_executable (info
))
470 s
= bfd_make_section_anyway_with_flags (abfd
,
471 (bed
->rela_plts_and_copies_p
472 ? ".rela.bss" : ".rel.bss"),
473 flags
| SEC_READONLY
);
475 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
479 if (bed
->want_dynrelro
)
481 s
= (bfd_make_section_anyway_with_flags
482 (abfd
, (bed
->rela_plts_and_copies_p
483 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
484 flags
| SEC_READONLY
));
486 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
488 htab
->sreldynrelro
= s
;
496 /* Record a new dynamic symbol. We record the dynamic symbols as we
497 read the input files, since we need to have a list of all of them
498 before we can determine the final sizes of the output sections.
499 Note that we may actually call this function even though we are not
500 going to output any dynamic symbols; in some cases we know that a
501 symbol should be in the dynamic symbol table, but only if there is
505 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
506 struct elf_link_hash_entry
*h
)
508 if (h
->dynindx
== -1)
510 struct elf_strtab_hash
*dynstr
;
515 if (h
->root
.type
== bfd_link_hash_defined
516 || h
->root
.type
== bfd_link_hash_defweak
)
518 /* An IR symbol should not be made dynamic. */
519 if (h
->root
.u
.def
.section
!= NULL
520 && h
->root
.u
.def
.section
->owner
!= NULL
521 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
525 /* XXX: The ABI draft says the linker must turn hidden and
526 internal symbols into STB_LOCAL symbols when producing the
527 DSO. However, if ld.so honors st_other in the dynamic table,
528 this would not be necessary. */
529 switch (ELF_ST_VISIBILITY (h
->other
))
533 if (h
->root
.type
!= bfd_link_hash_undefined
534 && h
->root
.type
!= bfd_link_hash_undefweak
)
537 if (!elf_hash_table (info
)->is_relocatable_executable
538 || ((h
->root
.type
== bfd_link_hash_defined
539 || h
->root
.type
== bfd_link_hash_defweak
)
540 && h
->root
.u
.def
.section
->owner
->no_export
)
541 || (h
->root
.type
== bfd_link_hash_common
542 && h
->root
.u
.c
.p
->section
->owner
->no_export
))
550 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
551 ++elf_hash_table (info
)->dynsymcount
;
553 dynstr
= elf_hash_table (info
)->dynstr
;
556 /* Create a strtab to hold the dynamic symbol names. */
557 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
562 /* We don't put any version information in the dynamic string
564 name
= h
->root
.root
.string
;
565 p
= strchr (name
, ELF_VER_CHR
);
567 /* We know that the p points into writable memory. In fact,
568 there are only a few symbols that have read-only names, being
569 those like _GLOBAL_OFFSET_TABLE_ that are created specially
570 by the backends. Most symbols will have names pointing into
571 an ELF string table read from a file, or to objalloc memory. */
574 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
579 if (indx
== (size_t) -1)
581 h
->dynstr_index
= indx
;
587 /* Mark a symbol dynamic. */
590 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
591 struct elf_link_hash_entry
*h
,
592 Elf_Internal_Sym
*sym
)
594 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
596 /* It may be called more than once on the same H. */
597 if(h
->dynamic
|| bfd_link_relocatable (info
))
600 if ((info
->dynamic_data
601 && (h
->type
== STT_OBJECT
602 || h
->type
== STT_COMMON
604 && (ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
605 || ELF_ST_TYPE (sym
->st_info
) == STT_COMMON
))))
608 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
611 /* NB: If a symbol is made dynamic by --dynamic-list, it has
613 h
->root
.non_ir_ref_dynamic
= 1;
617 /* Record an assignment to a symbol made by a linker script. We need
618 this in case some dynamic object refers to this symbol. */
621 bfd_elf_record_link_assignment (bfd
*output_bfd
,
622 struct bfd_link_info
*info
,
627 struct elf_link_hash_entry
*h
, *hv
;
628 struct elf_link_hash_table
*htab
;
629 const struct elf_backend_data
*bed
;
631 if (!is_elf_hash_table (info
->hash
))
634 htab
= elf_hash_table (info
);
635 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
639 if (h
->root
.type
== bfd_link_hash_warning
)
640 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
642 if (h
->versioned
== unknown
)
644 /* Set versioned if symbol version is unknown. */
645 char *version
= strrchr (name
, ELF_VER_CHR
);
648 if (version
> name
&& version
[-1] != ELF_VER_CHR
)
649 h
->versioned
= versioned_hidden
;
651 h
->versioned
= versioned
;
655 /* Symbols defined in a linker script but not referenced anywhere
656 else will have non_elf set. */
659 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
663 switch (h
->root
.type
)
665 case bfd_link_hash_defined
:
666 case bfd_link_hash_defweak
:
667 case bfd_link_hash_common
:
669 case bfd_link_hash_undefweak
:
670 case bfd_link_hash_undefined
:
671 /* Since we're defining the symbol, don't let it seem to have not
672 been defined. record_dynamic_symbol and size_dynamic_sections
673 may depend on this. */
674 h
->root
.type
= bfd_link_hash_new
;
675 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
676 bfd_link_repair_undef_list (&htab
->root
);
678 case bfd_link_hash_new
:
680 case bfd_link_hash_indirect
:
681 /* We had a versioned symbol in a dynamic library. We make the
682 the versioned symbol point to this one. */
683 bed
= get_elf_backend_data (output_bfd
);
685 while (hv
->root
.type
== bfd_link_hash_indirect
686 || hv
->root
.type
== bfd_link_hash_warning
)
687 hv
= (struct elf_link_hash_entry
*) hv
->root
.u
.i
.link
;
688 /* We don't need to update h->root.u since linker will set them
690 h
->root
.type
= bfd_link_hash_undefined
;
691 hv
->root
.type
= bfd_link_hash_indirect
;
692 hv
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
693 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hv
);
700 /* If this symbol is being provided by the linker script, and it is
701 currently defined by a dynamic object, but not by a regular
702 object, then mark it as undefined so that the generic linker will
703 force the correct value. */
707 h
->root
.type
= bfd_link_hash_undefined
;
709 /* If this symbol is currently defined by a dynamic object, but not
710 by a regular object, then clear out any version information because
711 the symbol will not be associated with the dynamic object any
713 if (h
->def_dynamic
&& !h
->def_regular
)
714 h
->verinfo
.verdef
= NULL
;
716 /* Make sure this symbol is not garbage collected. */
723 bed
= get_elf_backend_data (output_bfd
);
724 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
725 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
726 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
729 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
731 if (!bfd_link_relocatable (info
)
733 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
734 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
739 || bfd_link_dll (info
)
740 || elf_hash_table (info
)->is_relocatable_executable
)
744 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
747 /* If this is a weak defined symbol, and we know a corresponding
748 real symbol from the same dynamic object, make sure the real
749 symbol is also made into a dynamic symbol. */
752 struct elf_link_hash_entry
*def
= weakdef (h
);
754 if (def
->dynindx
== -1
755 && !bfd_elf_link_record_dynamic_symbol (info
, def
))
763 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
764 success, and 2 on a failure caused by attempting to record a symbol
765 in a discarded section, eg. a discarded link-once section symbol. */
768 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
773 struct elf_link_local_dynamic_entry
*entry
;
774 struct elf_link_hash_table
*eht
;
775 struct elf_strtab_hash
*dynstr
;
778 Elf_External_Sym_Shndx eshndx
;
779 char esym
[sizeof (Elf64_External_Sym
)];
781 if (! is_elf_hash_table (info
->hash
))
784 /* See if the entry exists already. */
785 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
786 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
789 amt
= sizeof (*entry
);
790 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
794 /* Go find the symbol, so that we can find it's name. */
795 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
796 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
798 bfd_release (input_bfd
, entry
);
802 if (entry
->isym
.st_shndx
!= SHN_UNDEF
803 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
807 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
808 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
810 /* We can still bfd_release here as nothing has done another
811 bfd_alloc. We can't do this later in this function. */
812 bfd_release (input_bfd
, entry
);
817 name
= (bfd_elf_string_from_elf_section
818 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
819 entry
->isym
.st_name
));
821 dynstr
= elf_hash_table (info
)->dynstr
;
824 /* Create a strtab to hold the dynamic symbol names. */
825 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
830 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
831 if (dynstr_index
== (size_t) -1)
833 entry
->isym
.st_name
= dynstr_index
;
835 eht
= elf_hash_table (info
);
837 entry
->next
= eht
->dynlocal
;
838 eht
->dynlocal
= entry
;
839 entry
->input_bfd
= input_bfd
;
840 entry
->input_indx
= input_indx
;
843 /* Whatever binding the symbol had before, it's now local. */
845 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
847 /* The dynindx will be set at the end of size_dynamic_sections. */
852 /* Return the dynindex of a local dynamic symbol. */
855 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
859 struct elf_link_local_dynamic_entry
*e
;
861 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
862 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
867 /* This function is used to renumber the dynamic symbols, if some of
868 them are removed because they are marked as local. This is called
869 via elf_link_hash_traverse. */
872 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
875 size_t *count
= (size_t *) data
;
880 if (h
->dynindx
!= -1)
881 h
->dynindx
= ++(*count
);
887 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
888 STB_LOCAL binding. */
891 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
894 size_t *count
= (size_t *) data
;
896 if (!h
->forced_local
)
899 if (h
->dynindx
!= -1)
900 h
->dynindx
= ++(*count
);
905 /* Return true if the dynamic symbol for a given section should be
906 omitted when creating a shared library. */
908 _bfd_elf_omit_section_dynsym_default (bfd
*output_bfd ATTRIBUTE_UNUSED
,
909 struct bfd_link_info
*info
,
912 struct elf_link_hash_table
*htab
;
915 switch (elf_section_data (p
)->this_hdr
.sh_type
)
919 /* If sh_type is yet undecided, assume it could be
920 SHT_PROGBITS/SHT_NOBITS. */
922 htab
= elf_hash_table (info
);
923 if (htab
->text_index_section
!= NULL
)
924 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
926 return (htab
->dynobj
!= NULL
927 && (ip
= bfd_get_linker_section (htab
->dynobj
, p
->name
)) != NULL
928 && ip
->output_section
== p
);
930 /* There shouldn't be section relative relocations
931 against any other section. */
938 _bfd_elf_omit_section_dynsym_all
939 (bfd
*output_bfd ATTRIBUTE_UNUSED
,
940 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
941 asection
*p ATTRIBUTE_UNUSED
)
946 /* Assign dynsym indices. In a shared library we generate a section
947 symbol for each output section, which come first. Next come symbols
948 which have been forced to local binding. Then all of the back-end
949 allocated local dynamic syms, followed by the rest of the global
950 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
951 (This prevents the early call before elf_backend_init_index_section
952 and strip_excluded_output_sections setting dynindx for sections
953 that are stripped.) */
956 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
957 struct bfd_link_info
*info
,
958 unsigned long *section_sym_count
)
960 unsigned long dynsymcount
= 0;
961 bfd_boolean do_sec
= section_sym_count
!= NULL
;
963 if (bfd_link_pic (info
)
964 || elf_hash_table (info
)->is_relocatable_executable
)
966 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
968 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
969 if ((p
->flags
& SEC_EXCLUDE
) == 0
970 && (p
->flags
& SEC_ALLOC
) != 0
971 && elf_hash_table (info
)->dynamic_relocs
972 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
976 elf_section_data (p
)->dynindx
= dynsymcount
;
979 elf_section_data (p
)->dynindx
= 0;
982 *section_sym_count
= dynsymcount
;
984 elf_link_hash_traverse (elf_hash_table (info
),
985 elf_link_renumber_local_hash_table_dynsyms
,
988 if (elf_hash_table (info
)->dynlocal
)
990 struct elf_link_local_dynamic_entry
*p
;
991 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
992 p
->dynindx
= ++dynsymcount
;
994 elf_hash_table (info
)->local_dynsymcount
= dynsymcount
;
996 elf_link_hash_traverse (elf_hash_table (info
),
997 elf_link_renumber_hash_table_dynsyms
,
1000 /* There is an unused NULL entry at the head of the table which we
1001 must account for in our count even if the table is empty since it
1002 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1003 .dynamic section. */
1006 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
1010 /* Merge st_other field. */
1013 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
1014 unsigned int st_other
, asection
*sec
,
1015 bfd_boolean definition
, bfd_boolean dynamic
)
1017 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1019 /* If st_other has a processor-specific meaning, specific
1020 code might be needed here. */
1021 if (bed
->elf_backend_merge_symbol_attribute
)
1022 (*bed
->elf_backend_merge_symbol_attribute
) (h
, st_other
, definition
,
1027 unsigned symvis
= ELF_ST_VISIBILITY (st_other
);
1028 unsigned hvis
= ELF_ST_VISIBILITY (h
->other
);
1030 /* Keep the most constraining visibility. Leave the remainder
1031 of the st_other field to elf_backend_merge_symbol_attribute. */
1032 if (symvis
- 1 < hvis
- 1)
1033 h
->other
= symvis
| (h
->other
& ~ELF_ST_VISIBILITY (-1));
1036 && ELF_ST_VISIBILITY (st_other
) != STV_DEFAULT
1037 && (sec
->flags
& SEC_READONLY
) == 0)
1038 h
->protected_def
= 1;
1041 /* This function is called when we want to merge a new symbol with an
1042 existing symbol. It handles the various cases which arise when we
1043 find a definition in a dynamic object, or when there is already a
1044 definition in a dynamic object. The new symbol is described by
1045 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1046 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1047 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1048 of an old common symbol. We set OVERRIDE if the old symbol is
1049 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1050 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1051 to change. By OK to change, we mean that we shouldn't warn if the
1052 type or size does change. */
1055 _bfd_elf_merge_symbol (bfd
*abfd
,
1056 struct bfd_link_info
*info
,
1058 Elf_Internal_Sym
*sym
,
1061 struct elf_link_hash_entry
**sym_hash
,
1063 bfd_boolean
*pold_weak
,
1064 unsigned int *pold_alignment
,
1067 bfd_boolean
*type_change_ok
,
1068 bfd_boolean
*size_change_ok
,
1069 bfd_boolean
*matched
)
1071 asection
*sec
, *oldsec
;
1072 struct elf_link_hash_entry
*h
;
1073 struct elf_link_hash_entry
*hi
;
1074 struct elf_link_hash_entry
*flip
;
1077 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
1078 bfd_boolean newweak
, oldweak
, newfunc
, oldfunc
;
1079 const struct elf_backend_data
*bed
;
1081 bfd_boolean default_sym
= *matched
;
1087 bind
= ELF_ST_BIND (sym
->st_info
);
1089 if (! bfd_is_und_section (sec
))
1090 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
1092 h
= ((struct elf_link_hash_entry
*)
1093 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
1098 bed
= get_elf_backend_data (abfd
);
1100 /* NEW_VERSION is the symbol version of the new symbol. */
1101 if (h
->versioned
!= unversioned
)
1103 /* Symbol version is unknown or versioned. */
1104 new_version
= strrchr (name
, ELF_VER_CHR
);
1107 if (h
->versioned
== unknown
)
1109 if (new_version
> name
&& new_version
[-1] != ELF_VER_CHR
)
1110 h
->versioned
= versioned_hidden
;
1112 h
->versioned
= versioned
;
1115 if (new_version
[0] == '\0')
1119 h
->versioned
= unversioned
;
1124 /* For merging, we only care about real symbols. But we need to make
1125 sure that indirect symbol dynamic flags are updated. */
1127 while (h
->root
.type
== bfd_link_hash_indirect
1128 || h
->root
.type
== bfd_link_hash_warning
)
1129 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1133 if (hi
== h
|| h
->root
.type
== bfd_link_hash_new
)
1137 /* OLD_HIDDEN is true if the existing symbol is only visible
1138 to the symbol with the same symbol version. NEW_HIDDEN is
1139 true if the new symbol is only visible to the symbol with
1140 the same symbol version. */
1141 bfd_boolean old_hidden
= h
->versioned
== versioned_hidden
;
1142 bfd_boolean new_hidden
= hi
->versioned
== versioned_hidden
;
1143 if (!old_hidden
&& !new_hidden
)
1144 /* The new symbol matches the existing symbol if both
1149 /* OLD_VERSION is the symbol version of the existing
1153 if (h
->versioned
>= versioned
)
1154 old_version
= strrchr (h
->root
.root
.string
,
1159 /* The new symbol matches the existing symbol if they
1160 have the same symbol version. */
1161 *matched
= (old_version
== new_version
1162 || (old_version
!= NULL
1163 && new_version
!= NULL
1164 && strcmp (old_version
, new_version
) == 0));
1169 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1174 switch (h
->root
.type
)
1179 case bfd_link_hash_undefined
:
1180 case bfd_link_hash_undefweak
:
1181 oldbfd
= h
->root
.u
.undef
.abfd
;
1184 case bfd_link_hash_defined
:
1185 case bfd_link_hash_defweak
:
1186 oldbfd
= h
->root
.u
.def
.section
->owner
;
1187 oldsec
= h
->root
.u
.def
.section
;
1190 case bfd_link_hash_common
:
1191 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1192 oldsec
= h
->root
.u
.c
.p
->section
;
1194 *pold_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1197 if (poldbfd
&& *poldbfd
== NULL
)
1200 /* Differentiate strong and weak symbols. */
1201 newweak
= bind
== STB_WEAK
;
1202 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1203 || h
->root
.type
== bfd_link_hash_undefweak
);
1205 *pold_weak
= oldweak
;
1207 /* We have to check it for every instance since the first few may be
1208 references and not all compilers emit symbol type for undefined
1210 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1212 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1213 respectively, is from a dynamic object. */
1215 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1217 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1218 syms and defined syms in dynamic libraries respectively.
1219 ref_dynamic on the other hand can be set for a symbol defined in
1220 a dynamic library, and def_dynamic may not be set; When the
1221 definition in a dynamic lib is overridden by a definition in the
1222 executable use of the symbol in the dynamic lib becomes a
1223 reference to the executable symbol. */
1226 if (bfd_is_und_section (sec
))
1228 if (bind
!= STB_WEAK
)
1230 h
->ref_dynamic_nonweak
= 1;
1231 hi
->ref_dynamic_nonweak
= 1;
1236 /* Update the existing symbol only if they match. */
1239 hi
->dynamic_def
= 1;
1243 /* If we just created the symbol, mark it as being an ELF symbol.
1244 Other than that, there is nothing to do--there is no merge issue
1245 with a newly defined symbol--so we just return. */
1247 if (h
->root
.type
== bfd_link_hash_new
)
1253 /* In cases involving weak versioned symbols, we may wind up trying
1254 to merge a symbol with itself. Catch that here, to avoid the
1255 confusion that results if we try to override a symbol with
1256 itself. The additional tests catch cases like
1257 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1258 dynamic object, which we do want to handle here. */
1260 && (newweak
|| oldweak
)
1261 && ((abfd
->flags
& DYNAMIC
) == 0
1262 || !h
->def_regular
))
1267 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1268 else if (oldsec
!= NULL
)
1270 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1271 indices used by MIPS ELF. */
1272 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1275 /* Handle a case where plugin_notice won't be called and thus won't
1276 set the non_ir_ref flags on the first pass over symbols. */
1278 && (oldbfd
->flags
& BFD_PLUGIN
) != (abfd
->flags
& BFD_PLUGIN
)
1279 && newdyn
!= olddyn
)
1281 h
->root
.non_ir_ref_dynamic
= TRUE
;
1282 hi
->root
.non_ir_ref_dynamic
= TRUE
;
1285 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1286 respectively, appear to be a definition rather than reference. */
1288 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1290 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1291 && h
->root
.type
!= bfd_link_hash_undefweak
1292 && h
->root
.type
!= bfd_link_hash_common
);
1294 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1295 respectively, appear to be a function. */
1297 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1298 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1300 oldfunc
= (h
->type
!= STT_NOTYPE
1301 && bed
->is_function_type (h
->type
));
1303 if (!(newfunc
&& oldfunc
)
1304 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1305 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1306 && h
->type
!= STT_NOTYPE
1307 && (newdef
|| bfd_is_com_section (sec
))
1308 && (olddef
|| h
->root
.type
== bfd_link_hash_common
))
1310 /* If creating a default indirect symbol ("foo" or "foo@") from
1311 a dynamic versioned definition ("foo@@") skip doing so if
1312 there is an existing regular definition with a different
1313 type. We don't want, for example, a "time" variable in the
1314 executable overriding a "time" function in a shared library. */
1322 /* When adding a symbol from a regular object file after we have
1323 created indirect symbols, undo the indirection and any
1330 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1331 h
->forced_local
= 0;
1335 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1337 h
->root
.type
= bfd_link_hash_undefined
;
1338 h
->root
.u
.undef
.abfd
= abfd
;
1342 h
->root
.type
= bfd_link_hash_new
;
1343 h
->root
.u
.undef
.abfd
= NULL
;
1349 /* Check TLS symbols. We don't check undefined symbols introduced
1350 by "ld -u" which have no type (and oldbfd NULL), and we don't
1351 check symbols from plugins because they also have no type. */
1353 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1354 && (abfd
->flags
& BFD_PLUGIN
) == 0
1355 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1356 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1359 bfd_boolean ntdef
, tdef
;
1360 asection
*ntsec
, *tsec
;
1362 if (h
->type
== STT_TLS
)
1383 /* xgettext:c-format */
1384 (_("%s: TLS definition in %pB section %pA "
1385 "mismatches non-TLS definition in %pB section %pA"),
1386 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
, ntsec
);
1387 else if (!tdef
&& !ntdef
)
1389 /* xgettext:c-format */
1390 (_("%s: TLS reference in %pB "
1391 "mismatches non-TLS reference in %pB"),
1392 h
->root
.root
.string
, tbfd
, ntbfd
);
1395 /* xgettext:c-format */
1396 (_("%s: TLS definition in %pB section %pA "
1397 "mismatches non-TLS reference in %pB"),
1398 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
);
1401 /* xgettext:c-format */
1402 (_("%s: TLS reference in %pB "
1403 "mismatches non-TLS definition in %pB section %pA"),
1404 h
->root
.root
.string
, tbfd
, ntbfd
, ntsec
);
1406 bfd_set_error (bfd_error_bad_value
);
1410 /* If the old symbol has non-default visibility, we ignore the new
1411 definition from a dynamic object. */
1413 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1414 && !bfd_is_und_section (sec
))
1417 /* Make sure this symbol is dynamic. */
1419 hi
->ref_dynamic
= 1;
1420 /* A protected symbol has external availability. Make sure it is
1421 recorded as dynamic.
1423 FIXME: Should we check type and size for protected symbol? */
1424 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1425 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1430 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1433 /* If the new symbol with non-default visibility comes from a
1434 relocatable file and the old definition comes from a dynamic
1435 object, we remove the old definition. */
1436 if (hi
->root
.type
== bfd_link_hash_indirect
)
1438 /* Handle the case where the old dynamic definition is
1439 default versioned. We need to copy the symbol info from
1440 the symbol with default version to the normal one if it
1441 was referenced before. */
1444 hi
->root
.type
= h
->root
.type
;
1445 h
->root
.type
= bfd_link_hash_indirect
;
1446 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1448 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1449 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1451 /* If the new symbol is hidden or internal, completely undo
1452 any dynamic link state. */
1453 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1454 h
->forced_local
= 0;
1461 /* FIXME: Should we check type and size for protected symbol? */
1471 /* If the old symbol was undefined before, then it will still be
1472 on the undefs list. If the new symbol is undefined or
1473 common, we can't make it bfd_link_hash_new here, because new
1474 undefined or common symbols will be added to the undefs list
1475 by _bfd_generic_link_add_one_symbol. Symbols may not be
1476 added twice to the undefs list. Also, if the new symbol is
1477 undefweak then we don't want to lose the strong undef. */
1478 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1480 h
->root
.type
= bfd_link_hash_undefined
;
1481 h
->root
.u
.undef
.abfd
= abfd
;
1485 h
->root
.type
= bfd_link_hash_new
;
1486 h
->root
.u
.undef
.abfd
= NULL
;
1489 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1491 /* If the new symbol is hidden or internal, completely undo
1492 any dynamic link state. */
1493 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1494 h
->forced_local
= 0;
1500 /* FIXME: Should we check type and size for protected symbol? */
1506 /* If a new weak symbol definition comes from a regular file and the
1507 old symbol comes from a dynamic library, we treat the new one as
1508 strong. Similarly, an old weak symbol definition from a regular
1509 file is treated as strong when the new symbol comes from a dynamic
1510 library. Further, an old weak symbol from a dynamic library is
1511 treated as strong if the new symbol is from a dynamic library.
1512 This reflects the way glibc's ld.so works.
1514 Also allow a weak symbol to override a linker script symbol
1515 defined by an early pass over the script. This is done so the
1516 linker knows the symbol is defined in an object file, for the
1517 DEFINED script function.
1519 Do this before setting *type_change_ok or *size_change_ok so that
1520 we warn properly when dynamic library symbols are overridden. */
1522 if (newdef
&& !newdyn
&& (olddyn
|| h
->root
.ldscript_def
))
1524 if (olddef
&& newdyn
)
1527 /* Allow changes between different types of function symbol. */
1528 if (newfunc
&& oldfunc
)
1529 *type_change_ok
= TRUE
;
1531 /* It's OK to change the type if either the existing symbol or the
1532 new symbol is weak. A type change is also OK if the old symbol
1533 is undefined and the new symbol is defined. */
1538 && h
->root
.type
== bfd_link_hash_undefined
))
1539 *type_change_ok
= TRUE
;
1541 /* It's OK to change the size if either the existing symbol or the
1542 new symbol is weak, or if the old symbol is undefined. */
1545 || h
->root
.type
== bfd_link_hash_undefined
)
1546 *size_change_ok
= TRUE
;
1548 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1549 symbol, respectively, appears to be a common symbol in a dynamic
1550 object. If a symbol appears in an uninitialized section, and is
1551 not weak, and is not a function, then it may be a common symbol
1552 which was resolved when the dynamic object was created. We want
1553 to treat such symbols specially, because they raise special
1554 considerations when setting the symbol size: if the symbol
1555 appears as a common symbol in a regular object, and the size in
1556 the regular object is larger, we must make sure that we use the
1557 larger size. This problematic case can always be avoided in C,
1558 but it must be handled correctly when using Fortran shared
1561 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1562 likewise for OLDDYNCOMMON and OLDDEF.
1564 Note that this test is just a heuristic, and that it is quite
1565 possible to have an uninitialized symbol in a shared object which
1566 is really a definition, rather than a common symbol. This could
1567 lead to some minor confusion when the symbol really is a common
1568 symbol in some regular object. However, I think it will be
1574 && (sec
->flags
& SEC_ALLOC
) != 0
1575 && (sec
->flags
& SEC_LOAD
) == 0
1578 newdyncommon
= TRUE
;
1580 newdyncommon
= FALSE
;
1584 && h
->root
.type
== bfd_link_hash_defined
1586 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1587 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1590 olddyncommon
= TRUE
;
1592 olddyncommon
= FALSE
;
1594 /* We now know everything about the old and new symbols. We ask the
1595 backend to check if we can merge them. */
1596 if (bed
->merge_symbol
!= NULL
)
1598 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1603 /* There are multiple definitions of a normal symbol. Skip the
1604 default symbol as well as definition from an IR object. */
1605 if (olddef
&& !olddyn
&& !oldweak
&& newdef
&& !newdyn
&& !newweak
1606 && !default_sym
&& h
->def_regular
1608 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1609 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1611 /* Handle a multiple definition. */
1612 (*info
->callbacks
->multiple_definition
) (info
, &h
->root
,
1613 abfd
, sec
, *pvalue
);
1618 /* If both the old and the new symbols look like common symbols in a
1619 dynamic object, set the size of the symbol to the larger of the
1624 && sym
->st_size
!= h
->size
)
1626 /* Since we think we have two common symbols, issue a multiple
1627 common warning if desired. Note that we only warn if the
1628 size is different. If the size is the same, we simply let
1629 the old symbol override the new one as normally happens with
1630 symbols defined in dynamic objects. */
1632 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1633 bfd_link_hash_common
, sym
->st_size
);
1634 if (sym
->st_size
> h
->size
)
1635 h
->size
= sym
->st_size
;
1637 *size_change_ok
= TRUE
;
1640 /* If we are looking at a dynamic object, and we have found a
1641 definition, we need to see if the symbol was already defined by
1642 some other object. If so, we want to use the existing
1643 definition, and we do not want to report a multiple symbol
1644 definition error; we do this by clobbering *PSEC to be
1645 bfd_und_section_ptr.
1647 We treat a common symbol as a definition if the symbol in the
1648 shared library is a function, since common symbols always
1649 represent variables; this can cause confusion in principle, but
1650 any such confusion would seem to indicate an erroneous program or
1651 shared library. We also permit a common symbol in a regular
1652 object to override a weak symbol in a shared object. */
1657 || (h
->root
.type
== bfd_link_hash_common
1658 && (newweak
|| newfunc
))))
1662 newdyncommon
= FALSE
;
1664 *psec
= sec
= bfd_und_section_ptr
;
1665 *size_change_ok
= TRUE
;
1667 /* If we get here when the old symbol is a common symbol, then
1668 we are explicitly letting it override a weak symbol or
1669 function in a dynamic object, and we don't want to warn about
1670 a type change. If the old symbol is a defined symbol, a type
1671 change warning may still be appropriate. */
1673 if (h
->root
.type
== bfd_link_hash_common
)
1674 *type_change_ok
= TRUE
;
1677 /* Handle the special case of an old common symbol merging with a
1678 new symbol which looks like a common symbol in a shared object.
1679 We change *PSEC and *PVALUE to make the new symbol look like a
1680 common symbol, and let _bfd_generic_link_add_one_symbol do the
1684 && h
->root
.type
== bfd_link_hash_common
)
1688 newdyncommon
= FALSE
;
1689 *pvalue
= sym
->st_size
;
1690 *psec
= sec
= bed
->common_section (oldsec
);
1691 *size_change_ok
= TRUE
;
1694 /* Skip weak definitions of symbols that are already defined. */
1695 if (newdef
&& olddef
&& newweak
)
1697 /* Don't skip new non-IR weak syms. */
1698 if (!(oldbfd
!= NULL
1699 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1700 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1706 /* Merge st_other. If the symbol already has a dynamic index,
1707 but visibility says it should not be visible, turn it into a
1709 elf_merge_st_other (abfd
, h
, sym
->st_other
, sec
, newdef
, newdyn
);
1710 if (h
->dynindx
!= -1)
1711 switch (ELF_ST_VISIBILITY (h
->other
))
1715 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1720 /* If the old symbol is from a dynamic object, and the new symbol is
1721 a definition which is not from a dynamic object, then the new
1722 symbol overrides the old symbol. Symbols from regular files
1723 always take precedence over symbols from dynamic objects, even if
1724 they are defined after the dynamic object in the link.
1726 As above, we again permit a common symbol in a regular object to
1727 override a definition in a shared object if the shared object
1728 symbol is a function or is weak. */
1733 || (bfd_is_com_section (sec
)
1734 && (oldweak
|| oldfunc
)))
1739 /* Change the hash table entry to undefined, and let
1740 _bfd_generic_link_add_one_symbol do the right thing with the
1743 h
->root
.type
= bfd_link_hash_undefined
;
1744 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1745 *size_change_ok
= TRUE
;
1748 olddyncommon
= FALSE
;
1750 /* We again permit a type change when a common symbol may be
1751 overriding a function. */
1753 if (bfd_is_com_section (sec
))
1757 /* If a common symbol overrides a function, make sure
1758 that it isn't defined dynamically nor has type
1761 h
->type
= STT_NOTYPE
;
1763 *type_change_ok
= TRUE
;
1766 if (hi
->root
.type
== bfd_link_hash_indirect
)
1769 /* This union may have been set to be non-NULL when this symbol
1770 was seen in a dynamic object. We must force the union to be
1771 NULL, so that it is correct for a regular symbol. */
1772 h
->verinfo
.vertree
= NULL
;
1775 /* Handle the special case of a new common symbol merging with an
1776 old symbol that looks like it might be a common symbol defined in
1777 a shared object. Note that we have already handled the case in
1778 which a new common symbol should simply override the definition
1779 in the shared library. */
1782 && bfd_is_com_section (sec
)
1785 /* It would be best if we could set the hash table entry to a
1786 common symbol, but we don't know what to use for the section
1787 or the alignment. */
1788 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1789 bfd_link_hash_common
, sym
->st_size
);
1791 /* If the presumed common symbol in the dynamic object is
1792 larger, pretend that the new symbol has its size. */
1794 if (h
->size
> *pvalue
)
1797 /* We need to remember the alignment required by the symbol
1798 in the dynamic object. */
1799 BFD_ASSERT (pold_alignment
);
1800 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1803 olddyncommon
= FALSE
;
1805 h
->root
.type
= bfd_link_hash_undefined
;
1806 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1808 *size_change_ok
= TRUE
;
1809 *type_change_ok
= TRUE
;
1811 if (hi
->root
.type
== bfd_link_hash_indirect
)
1814 h
->verinfo
.vertree
= NULL
;
1819 /* Handle the case where we had a versioned symbol in a dynamic
1820 library and now find a definition in a normal object. In this
1821 case, we make the versioned symbol point to the normal one. */
1822 flip
->root
.type
= h
->root
.type
;
1823 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1824 h
->root
.type
= bfd_link_hash_indirect
;
1825 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1826 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1830 flip
->ref_dynamic
= 1;
1837 /* This function is called to create an indirect symbol from the
1838 default for the symbol with the default version if needed. The
1839 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1840 set DYNSYM if the new indirect symbol is dynamic. */
1843 _bfd_elf_add_default_symbol (bfd
*abfd
,
1844 struct bfd_link_info
*info
,
1845 struct elf_link_hash_entry
*h
,
1847 Elf_Internal_Sym
*sym
,
1851 bfd_boolean
*dynsym
)
1853 bfd_boolean type_change_ok
;
1854 bfd_boolean size_change_ok
;
1857 struct elf_link_hash_entry
*hi
;
1858 struct bfd_link_hash_entry
*bh
;
1859 const struct elf_backend_data
*bed
;
1860 bfd_boolean collect
;
1861 bfd_boolean dynamic
;
1864 size_t len
, shortlen
;
1866 bfd_boolean matched
;
1868 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1871 /* If this symbol has a version, and it is the default version, we
1872 create an indirect symbol from the default name to the fully
1873 decorated name. This will cause external references which do not
1874 specify a version to be bound to this version of the symbol. */
1875 p
= strchr (name
, ELF_VER_CHR
);
1876 if (h
->versioned
== unknown
)
1880 h
->versioned
= unversioned
;
1885 if (p
[1] != ELF_VER_CHR
)
1887 h
->versioned
= versioned_hidden
;
1891 h
->versioned
= versioned
;
1896 /* PR ld/19073: We may see an unversioned definition after the
1902 bed
= get_elf_backend_data (abfd
);
1903 collect
= bed
->collect
;
1904 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1906 shortlen
= p
- name
;
1907 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1908 if (shortname
== NULL
)
1910 memcpy (shortname
, name
, shortlen
);
1911 shortname
[shortlen
] = '\0';
1913 /* We are going to create a new symbol. Merge it with any existing
1914 symbol with this name. For the purposes of the merge, act as
1915 though we were defining the symbol we just defined, although we
1916 actually going to define an indirect symbol. */
1917 type_change_ok
= FALSE
;
1918 size_change_ok
= FALSE
;
1921 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1922 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1923 &type_change_ok
, &size_change_ok
, &matched
))
1929 if (hi
->def_regular
|| ELF_COMMON_DEF_P (hi
))
1931 /* If the undecorated symbol will have a version added by a
1932 script different to H, then don't indirect to/from the
1933 undecorated symbol. This isn't ideal because we may not yet
1934 have seen symbol versions, if given by a script on the
1935 command line rather than via --version-script. */
1936 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
1941 = bfd_find_version_for_sym (info
->version_info
,
1942 hi
->root
.root
.string
, &hide
);
1943 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
1945 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
1949 if (hi
->verinfo
.vertree
!= NULL
1950 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
1956 /* Add the default symbol if not performing a relocatable link. */
1957 if (! bfd_link_relocatable (info
))
1960 if (bh
->type
== bfd_link_hash_defined
1961 && bh
->u
.def
.section
->owner
!= NULL
1962 && (bh
->u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
1964 /* Mark the previous definition from IR object as
1965 undefined so that the generic linker will override
1967 bh
->type
= bfd_link_hash_undefined
;
1968 bh
->u
.undef
.abfd
= bh
->u
.def
.section
->owner
;
1970 if (! (_bfd_generic_link_add_one_symbol
1971 (info
, abfd
, shortname
, BSF_INDIRECT
,
1972 bfd_ind_section_ptr
,
1973 0, name
, FALSE
, collect
, &bh
)))
1975 hi
= (struct elf_link_hash_entry
*) bh
;
1980 /* In this case the symbol named SHORTNAME is overriding the
1981 indirect symbol we want to add. We were planning on making
1982 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1983 is the name without a version. NAME is the fully versioned
1984 name, and it is the default version.
1986 Overriding means that we already saw a definition for the
1987 symbol SHORTNAME in a regular object, and it is overriding
1988 the symbol defined in the dynamic object.
1990 When this happens, we actually want to change NAME, the
1991 symbol we just added, to refer to SHORTNAME. This will cause
1992 references to NAME in the shared object to become references
1993 to SHORTNAME in the regular object. This is what we expect
1994 when we override a function in a shared object: that the
1995 references in the shared object will be mapped to the
1996 definition in the regular object. */
1998 while (hi
->root
.type
== bfd_link_hash_indirect
1999 || hi
->root
.type
== bfd_link_hash_warning
)
2000 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2002 h
->root
.type
= bfd_link_hash_indirect
;
2003 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
2007 hi
->ref_dynamic
= 1;
2011 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
2016 /* Now set HI to H, so that the following code will set the
2017 other fields correctly. */
2021 /* Check if HI is a warning symbol. */
2022 if (hi
->root
.type
== bfd_link_hash_warning
)
2023 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2025 /* If there is a duplicate definition somewhere, then HI may not
2026 point to an indirect symbol. We will have reported an error to
2027 the user in that case. */
2029 if (hi
->root
.type
== bfd_link_hash_indirect
)
2031 struct elf_link_hash_entry
*ht
;
2033 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2034 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
2036 /* If we first saw a reference to SHORTNAME with non-default
2037 visibility, merge that visibility to the @@VER symbol. */
2038 elf_merge_st_other (abfd
, ht
, hi
->other
, sec
, TRUE
, dynamic
);
2040 /* A reference to the SHORTNAME symbol from a dynamic library
2041 will be satisfied by the versioned symbol at runtime. In
2042 effect, we have a reference to the versioned symbol. */
2043 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2044 hi
->dynamic_def
|= ht
->dynamic_def
;
2046 /* See if the new flags lead us to realize that the symbol must
2052 if (! bfd_link_executable (info
)
2059 if (hi
->ref_regular
)
2065 /* We also need to define an indirection from the nondefault version
2069 len
= strlen (name
);
2070 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
2071 if (shortname
== NULL
)
2073 memcpy (shortname
, name
, shortlen
);
2074 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
2076 /* Once again, merge with any existing symbol. */
2077 type_change_ok
= FALSE
;
2078 size_change_ok
= FALSE
;
2080 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
2081 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
2082 &type_change_ok
, &size_change_ok
, &matched
))
2088 && h
->root
.type
== bfd_link_hash_defweak
2089 && hi
->root
.type
== bfd_link_hash_defined
)
2091 /* We are handling a weak sym@@ver and attempting to define
2092 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2093 new weak sym@ver because there is already a strong sym@ver.
2094 However, sym@ver and sym@@ver are really the same symbol.
2095 The existing strong sym@ver ought to override sym@@ver. */
2096 h
->root
.type
= bfd_link_hash_defined
;
2097 h
->root
.u
.def
.section
= hi
->root
.u
.def
.section
;
2098 h
->root
.u
.def
.value
= hi
->root
.u
.def
.value
;
2099 hi
->root
.type
= bfd_link_hash_indirect
;
2100 hi
->root
.u
.i
.link
= &h
->root
;
2107 /* Here SHORTNAME is a versioned name, so we don't expect to see
2108 the type of override we do in the case above unless it is
2109 overridden by a versioned definition. */
2110 if (hi
->root
.type
!= bfd_link_hash_defined
2111 && hi
->root
.type
!= bfd_link_hash_defweak
)
2113 /* xgettext:c-format */
2114 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2121 if (! (_bfd_generic_link_add_one_symbol
2122 (info
, abfd
, shortname
, BSF_INDIRECT
,
2123 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
2125 hi
= (struct elf_link_hash_entry
*) bh
;
2128 /* If there is a duplicate definition somewhere, then HI may not
2129 point to an indirect symbol. We will have reported an error
2130 to the user in that case. */
2131 if (hi
->root
.type
== bfd_link_hash_indirect
)
2133 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
2134 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2135 hi
->dynamic_def
|= h
->dynamic_def
;
2137 /* If we first saw a reference to @VER symbol with
2138 non-default visibility, merge that visibility to the
2140 elf_merge_st_other (abfd
, h
, hi
->other
, sec
, TRUE
, dynamic
);
2142 /* See if the new flags lead us to realize that the symbol
2148 if (! bfd_link_executable (info
)
2154 if (hi
->ref_regular
)
2163 /* This routine is used to export all defined symbols into the dynamic
2164 symbol table. It is called via elf_link_hash_traverse. */
2167 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
2169 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2171 /* Ignore indirect symbols. These are added by the versioning code. */
2172 if (h
->root
.type
== bfd_link_hash_indirect
)
2175 /* Ignore this if we won't export it. */
2176 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2179 if (h
->dynindx
== -1
2180 && (h
->def_regular
|| h
->ref_regular
)
2181 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2182 h
->root
.root
.string
))
2184 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2194 /* Look through the symbols which are defined in other shared
2195 libraries and referenced here. Update the list of version
2196 dependencies. This will be put into the .gnu.version_r section.
2197 This function is called via elf_link_hash_traverse. */
2200 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2203 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2204 Elf_Internal_Verneed
*t
;
2205 Elf_Internal_Vernaux
*a
;
2208 /* We only care about symbols defined in shared objects with version
2213 || h
->verinfo
.verdef
== NULL
2214 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2215 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2218 /* See if we already know about this version. */
2219 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2223 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2226 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2227 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2233 /* This is a new version. Add it to tree we are building. */
2238 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2241 rinfo
->failed
= TRUE
;
2245 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2246 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2247 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2251 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2254 rinfo
->failed
= TRUE
;
2258 /* Note that we are copying a string pointer here, and testing it
2259 above. If bfd_elf_string_from_elf_section is ever changed to
2260 discard the string data when low in memory, this will have to be
2262 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2264 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2265 a
->vna_nextptr
= t
->vn_auxptr
;
2267 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2270 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2277 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2278 hidden. Set *T_P to NULL if there is no match. */
2281 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info
*info
,
2282 struct elf_link_hash_entry
*h
,
2283 const char *version_p
,
2284 struct bfd_elf_version_tree
**t_p
,
2287 struct bfd_elf_version_tree
*t
;
2289 /* Look for the version. If we find it, it is no longer weak. */
2290 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
2292 if (strcmp (t
->name
, version_p
) == 0)
2296 struct bfd_elf_version_expr
*d
;
2298 len
= version_p
- h
->root
.root
.string
;
2299 alc
= (char *) bfd_malloc (len
);
2302 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2303 alc
[len
- 1] = '\0';
2304 if (alc
[len
- 2] == ELF_VER_CHR
)
2305 alc
[len
- 2] = '\0';
2307 h
->verinfo
.vertree
= t
;
2311 if (t
->globals
.list
!= NULL
)
2312 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2314 /* See if there is anything to force this symbol to
2316 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2318 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2321 && ! info
->export_dynamic
)
2335 /* Return TRUE if the symbol H is hidden by version script. */
2338 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info
*info
,
2339 struct elf_link_hash_entry
*h
)
2342 bfd_boolean hide
= FALSE
;
2343 const struct elf_backend_data
*bed
2344 = get_elf_backend_data (info
->output_bfd
);
2346 /* Version script only hides symbols defined in regular objects. */
2347 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2350 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2351 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2353 struct bfd_elf_version_tree
*t
;
2356 if (*p
== ELF_VER_CHR
)
2360 && _bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
)
2364 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2369 /* If we don't have a version for this symbol, see if we can find
2371 if (h
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
2374 = bfd_find_version_for_sym (info
->version_info
,
2375 h
->root
.root
.string
, &hide
);
2376 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2378 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2386 /* Figure out appropriate versions for all the symbols. We may not
2387 have the version number script until we have read all of the input
2388 files, so until that point we don't know which symbols should be
2389 local. This function is called via elf_link_hash_traverse. */
2392 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2394 struct elf_info_failed
*sinfo
;
2395 struct bfd_link_info
*info
;
2396 const struct elf_backend_data
*bed
;
2397 struct elf_info_failed eif
;
2401 sinfo
= (struct elf_info_failed
*) data
;
2404 /* Fix the symbol flags. */
2407 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2410 sinfo
->failed
= TRUE
;
2414 bed
= get_elf_backend_data (info
->output_bfd
);
2416 /* We only need version numbers for symbols defined in regular
2418 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2420 /* Hide symbols defined in discarded input sections. */
2421 if ((h
->root
.type
== bfd_link_hash_defined
2422 || h
->root
.type
== bfd_link_hash_defweak
)
2423 && discarded_section (h
->root
.u
.def
.section
))
2424 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2429 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2430 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2432 struct bfd_elf_version_tree
*t
;
2435 if (*p
== ELF_VER_CHR
)
2438 /* If there is no version string, we can just return out. */
2442 if (!_bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
))
2444 sinfo
->failed
= TRUE
;
2449 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2451 /* If we are building an application, we need to create a
2452 version node for this version. */
2453 if (t
== NULL
&& bfd_link_executable (info
))
2455 struct bfd_elf_version_tree
**pp
;
2458 /* If we aren't going to export this symbol, we don't need
2459 to worry about it. */
2460 if (h
->dynindx
== -1)
2463 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2467 sinfo
->failed
= TRUE
;
2472 t
->name_indx
= (unsigned int) -1;
2476 /* Don't count anonymous version tag. */
2477 if (sinfo
->info
->version_info
!= NULL
2478 && sinfo
->info
->version_info
->vernum
== 0)
2480 for (pp
= &sinfo
->info
->version_info
;
2484 t
->vernum
= version_index
;
2488 h
->verinfo
.vertree
= t
;
2492 /* We could not find the version for a symbol when
2493 generating a shared archive. Return an error. */
2495 /* xgettext:c-format */
2496 (_("%pB: version node not found for symbol %s"),
2497 info
->output_bfd
, h
->root
.root
.string
);
2498 bfd_set_error (bfd_error_bad_value
);
2499 sinfo
->failed
= TRUE
;
2504 /* If we don't have a version for this symbol, see if we can find
2507 && h
->verinfo
.vertree
== NULL
2508 && sinfo
->info
->version_info
!= NULL
)
2511 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2512 h
->root
.root
.string
, &hide
);
2513 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2514 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2520 /* Read and swap the relocs from the section indicated by SHDR. This
2521 may be either a REL or a RELA section. The relocations are
2522 translated into RELA relocations and stored in INTERNAL_RELOCS,
2523 which should have already been allocated to contain enough space.
2524 The EXTERNAL_RELOCS are a buffer where the external form of the
2525 relocations should be stored.
2527 Returns FALSE if something goes wrong. */
2530 elf_link_read_relocs_from_section (bfd
*abfd
,
2532 Elf_Internal_Shdr
*shdr
,
2533 void *external_relocs
,
2534 Elf_Internal_Rela
*internal_relocs
)
2536 const struct elf_backend_data
*bed
;
2537 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2538 const bfd_byte
*erela
;
2539 const bfd_byte
*erelaend
;
2540 Elf_Internal_Rela
*irela
;
2541 Elf_Internal_Shdr
*symtab_hdr
;
2544 /* Position ourselves at the start of the section. */
2545 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2548 /* Read the relocations. */
2549 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2552 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2553 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2555 bed
= get_elf_backend_data (abfd
);
2557 /* Convert the external relocations to the internal format. */
2558 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2559 swap_in
= bed
->s
->swap_reloc_in
;
2560 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2561 swap_in
= bed
->s
->swap_reloca_in
;
2564 bfd_set_error (bfd_error_wrong_format
);
2568 erela
= (const bfd_byte
*) external_relocs
;
2569 /* Setting erelaend like this and comparing with <= handles case of
2570 a fuzzed object with sh_size not a multiple of sh_entsize. */
2571 erelaend
= erela
+ shdr
->sh_size
- shdr
->sh_entsize
;
2572 irela
= internal_relocs
;
2573 while (erela
<= erelaend
)
2577 (*swap_in
) (abfd
, erela
, irela
);
2578 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2579 if (bed
->s
->arch_size
== 64)
2583 if ((size_t) r_symndx
>= nsyms
)
2586 /* xgettext:c-format */
2587 (_("%pB: bad reloc symbol index (%#" PRIx64
" >= %#lx)"
2588 " for offset %#" PRIx64
" in section `%pA'"),
2589 abfd
, (uint64_t) r_symndx
, (unsigned long) nsyms
,
2590 (uint64_t) irela
->r_offset
, sec
);
2591 bfd_set_error (bfd_error_bad_value
);
2595 else if (r_symndx
!= STN_UNDEF
)
2598 /* xgettext:c-format */
2599 (_("%pB: non-zero symbol index (%#" PRIx64
")"
2600 " for offset %#" PRIx64
" in section `%pA'"
2601 " when the object file has no symbol table"),
2602 abfd
, (uint64_t) r_symndx
,
2603 (uint64_t) irela
->r_offset
, sec
);
2604 bfd_set_error (bfd_error_bad_value
);
2607 irela
+= bed
->s
->int_rels_per_ext_rel
;
2608 erela
+= shdr
->sh_entsize
;
2614 /* Read and swap the relocs for a section O. They may have been
2615 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2616 not NULL, they are used as buffers to read into. They are known to
2617 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2618 the return value is allocated using either malloc or bfd_alloc,
2619 according to the KEEP_MEMORY argument. If O has two relocation
2620 sections (both REL and RELA relocations), then the REL_HDR
2621 relocations will appear first in INTERNAL_RELOCS, followed by the
2622 RELA_HDR relocations. */
2625 _bfd_elf_link_read_relocs (bfd
*abfd
,
2627 void *external_relocs
,
2628 Elf_Internal_Rela
*internal_relocs
,
2629 bfd_boolean keep_memory
)
2631 void *alloc1
= NULL
;
2632 Elf_Internal_Rela
*alloc2
= NULL
;
2633 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2634 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2635 Elf_Internal_Rela
*internal_rela_relocs
;
2637 if (esdo
->relocs
!= NULL
)
2638 return esdo
->relocs
;
2640 if (o
->reloc_count
== 0)
2643 if (internal_relocs
== NULL
)
2647 size
= (bfd_size_type
) o
->reloc_count
* sizeof (Elf_Internal_Rela
);
2649 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2651 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2652 if (internal_relocs
== NULL
)
2656 if (external_relocs
== NULL
)
2658 bfd_size_type size
= 0;
2661 size
+= esdo
->rel
.hdr
->sh_size
;
2663 size
+= esdo
->rela
.hdr
->sh_size
;
2665 alloc1
= bfd_malloc (size
);
2668 external_relocs
= alloc1
;
2671 internal_rela_relocs
= internal_relocs
;
2674 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2678 external_relocs
= (((bfd_byte
*) external_relocs
)
2679 + esdo
->rel
.hdr
->sh_size
);
2680 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2681 * bed
->s
->int_rels_per_ext_rel
);
2685 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2687 internal_rela_relocs
)))
2690 /* Cache the results for next time, if we can. */
2692 esdo
->relocs
= internal_relocs
;
2696 /* Don't free alloc2, since if it was allocated we are passing it
2697 back (under the name of internal_relocs). */
2699 return internal_relocs
;
2706 bfd_release (abfd
, alloc2
);
2713 /* Compute the size of, and allocate space for, REL_HDR which is the
2714 section header for a section containing relocations for O. */
2717 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2718 struct bfd_elf_section_reloc_data
*reldata
)
2720 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2722 /* That allows us to calculate the size of the section. */
2723 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2725 /* The contents field must last into write_object_contents, so we
2726 allocate it with bfd_alloc rather than malloc. Also since we
2727 cannot be sure that the contents will actually be filled in,
2728 we zero the allocated space. */
2729 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2730 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2733 if (reldata
->hashes
== NULL
&& reldata
->count
)
2735 struct elf_link_hash_entry
**p
;
2737 p
= ((struct elf_link_hash_entry
**)
2738 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2742 reldata
->hashes
= p
;
2748 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2749 originated from the section given by INPUT_REL_HDR) to the
2753 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2754 asection
*input_section
,
2755 Elf_Internal_Shdr
*input_rel_hdr
,
2756 Elf_Internal_Rela
*internal_relocs
,
2757 struct elf_link_hash_entry
**rel_hash
2760 Elf_Internal_Rela
*irela
;
2761 Elf_Internal_Rela
*irelaend
;
2763 struct bfd_elf_section_reloc_data
*output_reldata
;
2764 asection
*output_section
;
2765 const struct elf_backend_data
*bed
;
2766 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2767 struct bfd_elf_section_data
*esdo
;
2769 output_section
= input_section
->output_section
;
2771 bed
= get_elf_backend_data (output_bfd
);
2772 esdo
= elf_section_data (output_section
);
2773 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2775 output_reldata
= &esdo
->rel
;
2776 swap_out
= bed
->s
->swap_reloc_out
;
2778 else if (esdo
->rela
.hdr
2779 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2781 output_reldata
= &esdo
->rela
;
2782 swap_out
= bed
->s
->swap_reloca_out
;
2787 /* xgettext:c-format */
2788 (_("%pB: relocation size mismatch in %pB section %pA"),
2789 output_bfd
, input_section
->owner
, input_section
);
2790 bfd_set_error (bfd_error_wrong_format
);
2794 erel
= output_reldata
->hdr
->contents
;
2795 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2796 irela
= internal_relocs
;
2797 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2798 * bed
->s
->int_rels_per_ext_rel
);
2799 while (irela
< irelaend
)
2801 (*swap_out
) (output_bfd
, irela
, erel
);
2802 irela
+= bed
->s
->int_rels_per_ext_rel
;
2803 erel
+= input_rel_hdr
->sh_entsize
;
2806 /* Bump the counter, so that we know where to add the next set of
2808 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2813 /* Make weak undefined symbols in PIE dynamic. */
2816 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2817 struct elf_link_hash_entry
*h
)
2819 if (bfd_link_pie (info
)
2821 && h
->root
.type
== bfd_link_hash_undefweak
)
2822 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2827 /* Fix up the flags for a symbol. This handles various cases which
2828 can only be fixed after all the input files are seen. This is
2829 currently called by both adjust_dynamic_symbol and
2830 assign_sym_version, which is unnecessary but perhaps more robust in
2831 the face of future changes. */
2834 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2835 struct elf_info_failed
*eif
)
2837 const struct elf_backend_data
*bed
;
2839 /* If this symbol was mentioned in a non-ELF file, try to set
2840 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2841 permit a non-ELF file to correctly refer to a symbol defined in
2842 an ELF dynamic object. */
2845 while (h
->root
.type
== bfd_link_hash_indirect
)
2846 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2848 if (h
->root
.type
!= bfd_link_hash_defined
2849 && h
->root
.type
!= bfd_link_hash_defweak
)
2852 h
->ref_regular_nonweak
= 1;
2856 if (h
->root
.u
.def
.section
->owner
!= NULL
2857 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2858 == bfd_target_elf_flavour
))
2861 h
->ref_regular_nonweak
= 1;
2867 if (h
->dynindx
== -1
2871 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2880 /* Unfortunately, NON_ELF is only correct if the symbol
2881 was first seen in a non-ELF file. Fortunately, if the symbol
2882 was first seen in an ELF file, we're probably OK unless the
2883 symbol was defined in a non-ELF file. Catch that case here.
2884 FIXME: We're still in trouble if the symbol was first seen in
2885 a dynamic object, and then later in a non-ELF regular object. */
2886 if ((h
->root
.type
== bfd_link_hash_defined
2887 || h
->root
.type
== bfd_link_hash_defweak
)
2889 && (h
->root
.u
.def
.section
->owner
!= NULL
2890 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2891 != bfd_target_elf_flavour
)
2892 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2893 && !h
->def_dynamic
)))
2897 /* Backend specific symbol fixup. */
2898 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2899 if (bed
->elf_backend_fixup_symbol
2900 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2903 /* If this is a final link, and the symbol was defined as a common
2904 symbol in a regular object file, and there was no definition in
2905 any dynamic object, then the linker will have allocated space for
2906 the symbol in a common section but the DEF_REGULAR
2907 flag will not have been set. */
2908 if (h
->root
.type
== bfd_link_hash_defined
2912 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
2915 /* Symbols defined in discarded sections shouldn't be dynamic. */
2916 if (h
->root
.type
== bfd_link_hash_undefined
&& h
->indx
== -3)
2917 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2919 /* If a weak undefined symbol has non-default visibility, we also
2920 hide it from the dynamic linker. */
2921 else if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2922 && h
->root
.type
== bfd_link_hash_undefweak
)
2923 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2925 /* A hidden versioned symbol in executable should be forced local if
2926 it is is locally defined, not referenced by shared library and not
2928 else if (bfd_link_executable (eif
->info
)
2929 && h
->versioned
== versioned_hidden
2930 && !eif
->info
->export_dynamic
2934 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2936 /* If -Bsymbolic was used (which means to bind references to global
2937 symbols to the definition within the shared object), and this
2938 symbol was defined in a regular object, then it actually doesn't
2939 need a PLT entry. Likewise, if the symbol has non-default
2940 visibility. If the symbol has hidden or internal visibility, we
2941 will force it local. */
2942 else if (h
->needs_plt
2943 && bfd_link_pic (eif
->info
)
2944 && is_elf_hash_table (eif
->info
->hash
)
2945 && (SYMBOLIC_BIND (eif
->info
, h
)
2946 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2949 bfd_boolean force_local
;
2951 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2952 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2953 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2956 /* If this is a weak defined symbol in a dynamic object, and we know
2957 the real definition in the dynamic object, copy interesting flags
2958 over to the real definition. */
2959 if (h
->is_weakalias
)
2961 struct elf_link_hash_entry
*def
= weakdef (h
);
2963 /* If the real definition is defined by a regular object file,
2964 don't do anything special. See the longer description in
2965 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2966 bfd_link_hash_defined as it was when put on the alias list
2967 then it must have originally been a versioned symbol (for
2968 which a non-versioned indirect symbol is created) and later
2969 a definition for the non-versioned symbol is found. In that
2970 case the indirection is flipped with the versioned symbol
2971 becoming an indirect pointing at the non-versioned symbol.
2972 Thus, not an alias any more. */
2973 if (def
->def_regular
2974 || def
->root
.type
!= bfd_link_hash_defined
)
2977 while ((h
= h
->u
.alias
) != def
)
2978 h
->is_weakalias
= 0;
2982 while (h
->root
.type
== bfd_link_hash_indirect
)
2983 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2984 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2985 || h
->root
.type
== bfd_link_hash_defweak
);
2986 BFD_ASSERT (def
->def_dynamic
);
2987 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, def
, h
);
2994 /* Make the backend pick a good value for a dynamic symbol. This is
2995 called via elf_link_hash_traverse, and also calls itself
2999 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
3001 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3002 struct elf_link_hash_table
*htab
;
3003 const struct elf_backend_data
*bed
;
3005 if (! is_elf_hash_table (eif
->info
->hash
))
3008 /* Ignore indirect symbols. These are added by the versioning code. */
3009 if (h
->root
.type
== bfd_link_hash_indirect
)
3012 /* Fix the symbol flags. */
3013 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
3016 htab
= elf_hash_table (eif
->info
);
3017 bed
= get_elf_backend_data (htab
->dynobj
);
3019 if (h
->root
.type
== bfd_link_hash_undefweak
)
3021 if (eif
->info
->dynamic_undefined_weak
== 0)
3022 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
3023 else if (eif
->info
->dynamic_undefined_weak
> 0
3025 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3026 && !bfd_hide_sym_by_version (eif
->info
->version_info
,
3027 h
->root
.root
.string
))
3029 if (!bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3037 /* If this symbol does not require a PLT entry, and it is not
3038 defined by a dynamic object, or is not referenced by a regular
3039 object, ignore it. We do have to handle a weak defined symbol,
3040 even if no regular object refers to it, if we decided to add it
3041 to the dynamic symbol table. FIXME: Do we normally need to worry
3042 about symbols which are defined by one dynamic object and
3043 referenced by another one? */
3045 && h
->type
!= STT_GNU_IFUNC
3049 && (!h
->is_weakalias
|| weakdef (h
)->dynindx
== -1))))
3051 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
3055 /* If we've already adjusted this symbol, don't do it again. This
3056 can happen via a recursive call. */
3057 if (h
->dynamic_adjusted
)
3060 /* Don't look at this symbol again. Note that we must set this
3061 after checking the above conditions, because we may look at a
3062 symbol once, decide not to do anything, and then get called
3063 recursively later after REF_REGULAR is set below. */
3064 h
->dynamic_adjusted
= 1;
3066 /* If this is a weak definition, and we know a real definition, and
3067 the real symbol is not itself defined by a regular object file,
3068 then get a good value for the real definition. We handle the
3069 real symbol first, for the convenience of the backend routine.
3071 Note that there is a confusing case here. If the real definition
3072 is defined by a regular object file, we don't get the real symbol
3073 from the dynamic object, but we do get the weak symbol. If the
3074 processor backend uses a COPY reloc, then if some routine in the
3075 dynamic object changes the real symbol, we will not see that
3076 change in the corresponding weak symbol. This is the way other
3077 ELF linkers work as well, and seems to be a result of the shared
3080 I will clarify this issue. Most SVR4 shared libraries define the
3081 variable _timezone and define timezone as a weak synonym. The
3082 tzset call changes _timezone. If you write
3083 extern int timezone;
3085 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3086 you might expect that, since timezone is a synonym for _timezone,
3087 the same number will print both times. However, if the processor
3088 backend uses a COPY reloc, then actually timezone will be copied
3089 into your process image, and, since you define _timezone
3090 yourself, _timezone will not. Thus timezone and _timezone will
3091 wind up at different memory locations. The tzset call will set
3092 _timezone, leaving timezone unchanged. */
3094 if (h
->is_weakalias
)
3096 struct elf_link_hash_entry
*def
= weakdef (h
);
3098 /* If we get to this point, there is an implicit reference to
3099 the alias by a regular object file via the weak symbol H. */
3100 def
->ref_regular
= 1;
3102 /* Ensure that the backend adjust_dynamic_symbol function sees
3103 the strong alias before H by recursively calling ourselves. */
3104 if (!_bfd_elf_adjust_dynamic_symbol (def
, eif
))
3108 /* If a symbol has no type and no size and does not require a PLT
3109 entry, then we are probably about to do the wrong thing here: we
3110 are probably going to create a COPY reloc for an empty object.
3111 This case can arise when a shared object is built with assembly
3112 code, and the assembly code fails to set the symbol type. */
3114 && h
->type
== STT_NOTYPE
3117 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3118 h
->root
.root
.string
);
3120 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3129 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3133 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
3134 struct elf_link_hash_entry
*h
,
3137 unsigned int power_of_two
;
3139 asection
*sec
= h
->root
.u
.def
.section
;
3141 /* The section alignment of the definition is the maximum alignment
3142 requirement of symbols defined in the section. Since we don't
3143 know the symbol alignment requirement, we start with the
3144 maximum alignment and check low bits of the symbol address
3145 for the minimum alignment. */
3146 power_of_two
= bfd_section_alignment (sec
);
3147 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
3148 while ((h
->root
.u
.def
.value
& mask
) != 0)
3154 if (power_of_two
> bfd_section_alignment (dynbss
))
3156 /* Adjust the section alignment if needed. */
3157 if (!bfd_set_section_alignment (dynbss
, power_of_two
))
3161 /* We make sure that the symbol will be aligned properly. */
3162 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
3164 /* Define the symbol as being at this point in DYNBSS. */
3165 h
->root
.u
.def
.section
= dynbss
;
3166 h
->root
.u
.def
.value
= dynbss
->size
;
3168 /* Increment the size of DYNBSS to make room for the symbol. */
3169 dynbss
->size
+= h
->size
;
3171 /* No error if extern_protected_data is true. */
3172 if (h
->protected_def
3173 && (!info
->extern_protected_data
3174 || (info
->extern_protected_data
< 0
3175 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
3176 info
->callbacks
->einfo
3177 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3178 h
->root
.root
.string
);
3183 /* Adjust all external symbols pointing into SEC_MERGE sections
3184 to reflect the object merging within the sections. */
3187 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
3191 if ((h
->root
.type
== bfd_link_hash_defined
3192 || h
->root
.type
== bfd_link_hash_defweak
)
3193 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
3194 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
3196 bfd
*output_bfd
= (bfd
*) data
;
3198 h
->root
.u
.def
.value
=
3199 _bfd_merged_section_offset (output_bfd
,
3200 &h
->root
.u
.def
.section
,
3201 elf_section_data (sec
)->sec_info
,
3202 h
->root
.u
.def
.value
);
3208 /* Returns false if the symbol referred to by H should be considered
3209 to resolve local to the current module, and true if it should be
3210 considered to bind dynamically. */
3213 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
3214 struct bfd_link_info
*info
,
3215 bfd_boolean not_local_protected
)
3217 bfd_boolean binding_stays_local_p
;
3218 const struct elf_backend_data
*bed
;
3219 struct elf_link_hash_table
*hash_table
;
3224 while (h
->root
.type
== bfd_link_hash_indirect
3225 || h
->root
.type
== bfd_link_hash_warning
)
3226 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3228 /* If it was forced local, then clearly it's not dynamic. */
3229 if (h
->dynindx
== -1)
3231 if (h
->forced_local
)
3234 /* Identify the cases where name binding rules say that a
3235 visible symbol resolves locally. */
3236 binding_stays_local_p
= (bfd_link_executable (info
)
3237 || SYMBOLIC_BIND (info
, h
));
3239 switch (ELF_ST_VISIBILITY (h
->other
))
3246 hash_table
= elf_hash_table (info
);
3247 if (!is_elf_hash_table (hash_table
))
3250 bed
= get_elf_backend_data (hash_table
->dynobj
);
3252 /* Proper resolution for function pointer equality may require
3253 that these symbols perhaps be resolved dynamically, even though
3254 we should be resolving them to the current module. */
3255 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
3256 binding_stays_local_p
= TRUE
;
3263 /* If it isn't defined locally, then clearly it's dynamic. */
3264 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
3267 /* Otherwise, the symbol is dynamic if binding rules don't tell
3268 us that it remains local. */
3269 return !binding_stays_local_p
;
3272 /* Return true if the symbol referred to by H should be considered
3273 to resolve local to the current module, and false otherwise. Differs
3274 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3275 undefined symbols. The two functions are virtually identical except
3276 for the place where dynindx == -1 is tested. If that test is true,
3277 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3278 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3280 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3281 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3282 treatment of undefined weak symbols. For those that do not make
3283 undefined weak symbols dynamic, both functions may return false. */
3286 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3287 struct bfd_link_info
*info
,
3288 bfd_boolean local_protected
)
3290 const struct elf_backend_data
*bed
;
3291 struct elf_link_hash_table
*hash_table
;
3293 /* If it's a local sym, of course we resolve locally. */
3297 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3298 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3299 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3302 /* Forced local symbols resolve locally. */
3303 if (h
->forced_local
)
3306 /* Common symbols that become definitions don't get the DEF_REGULAR
3307 flag set, so test it first, and don't bail out. */
3308 if (ELF_COMMON_DEF_P (h
))
3310 /* If we don't have a definition in a regular file, then we can't
3311 resolve locally. The sym is either undefined or dynamic. */
3312 else if (!h
->def_regular
)
3315 /* Non-dynamic symbols resolve locally. */
3316 if (h
->dynindx
== -1)
3319 /* At this point, we know the symbol is defined and dynamic. In an
3320 executable it must resolve locally, likewise when building symbolic
3321 shared libraries. */
3322 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3325 /* Now deal with defined dynamic symbols in shared libraries. Ones
3326 with default visibility might not resolve locally. */
3327 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3330 hash_table
= elf_hash_table (info
);
3331 if (!is_elf_hash_table (hash_table
))
3334 bed
= get_elf_backend_data (hash_table
->dynobj
);
3336 /* If extern_protected_data is false, STV_PROTECTED non-function
3337 symbols are local. */
3338 if ((!info
->extern_protected_data
3339 || (info
->extern_protected_data
< 0
3340 && !bed
->extern_protected_data
))
3341 && !bed
->is_function_type (h
->type
))
3344 /* Function pointer equality tests may require that STV_PROTECTED
3345 symbols be treated as dynamic symbols. If the address of a
3346 function not defined in an executable is set to that function's
3347 plt entry in the executable, then the address of the function in
3348 a shared library must also be the plt entry in the executable. */
3349 return local_protected
;
3352 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3353 aligned. Returns the first TLS output section. */
3355 struct bfd_section
*
3356 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3358 struct bfd_section
*sec
, *tls
;
3359 unsigned int align
= 0;
3361 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3362 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3366 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3367 if (sec
->alignment_power
> align
)
3368 align
= sec
->alignment_power
;
3370 elf_hash_table (info
)->tls_sec
= tls
;
3372 /* Ensure the alignment of the first section (usually .tdata) is the largest
3373 alignment, so that the tls segment starts aligned. */
3375 tls
->alignment_power
= align
;
3380 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3382 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3383 Elf_Internal_Sym
*sym
)
3385 const struct elf_backend_data
*bed
;
3387 /* Local symbols do not count, but target specific ones might. */
3388 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3389 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3392 bed
= get_elf_backend_data (abfd
);
3393 /* Function symbols do not count. */
3394 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3397 /* If the section is undefined, then so is the symbol. */
3398 if (sym
->st_shndx
== SHN_UNDEF
)
3401 /* If the symbol is defined in the common section, then
3402 it is a common definition and so does not count. */
3403 if (bed
->common_definition (sym
))
3406 /* If the symbol is in a target specific section then we
3407 must rely upon the backend to tell us what it is. */
3408 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3409 /* FIXME - this function is not coded yet:
3411 return _bfd_is_global_symbol_definition (abfd, sym);
3413 Instead for now assume that the definition is not global,
3414 Even if this is wrong, at least the linker will behave
3415 in the same way that it used to do. */
3421 /* Search the symbol table of the archive element of the archive ABFD
3422 whose archive map contains a mention of SYMDEF, and determine if
3423 the symbol is defined in this element. */
3425 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3427 Elf_Internal_Shdr
* hdr
;
3431 Elf_Internal_Sym
*isymbuf
;
3432 Elf_Internal_Sym
*isym
;
3433 Elf_Internal_Sym
*isymend
;
3436 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
3440 if (! bfd_check_format (abfd
, bfd_object
))
3443 /* Select the appropriate symbol table. If we don't know if the
3444 object file is an IR object, give linker LTO plugin a chance to
3445 get the correct symbol table. */
3446 if (abfd
->plugin_format
== bfd_plugin_yes
3447 #if BFD_SUPPORTS_PLUGINS
3448 || (abfd
->plugin_format
== bfd_plugin_unknown
3449 && bfd_link_plugin_object_p (abfd
))
3453 /* Use the IR symbol table if the object has been claimed by
3455 abfd
= abfd
->plugin_dummy_bfd
;
3456 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3458 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3459 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3461 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3463 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3465 /* The sh_info field of the symtab header tells us where the
3466 external symbols start. We don't care about the local symbols. */
3467 if (elf_bad_symtab (abfd
))
3469 extsymcount
= symcount
;
3474 extsymcount
= symcount
- hdr
->sh_info
;
3475 extsymoff
= hdr
->sh_info
;
3478 if (extsymcount
== 0)
3481 /* Read in the symbol table. */
3482 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3484 if (isymbuf
== NULL
)
3487 /* Scan the symbol table looking for SYMDEF. */
3489 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3493 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3498 if (strcmp (name
, symdef
->name
) == 0)
3500 result
= is_global_data_symbol_definition (abfd
, isym
);
3510 /* Add an entry to the .dynamic table. */
3513 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3517 struct elf_link_hash_table
*hash_table
;
3518 const struct elf_backend_data
*bed
;
3520 bfd_size_type newsize
;
3521 bfd_byte
*newcontents
;
3522 Elf_Internal_Dyn dyn
;
3524 hash_table
= elf_hash_table (info
);
3525 if (! is_elf_hash_table (hash_table
))
3528 if (tag
== DT_RELA
|| tag
== DT_REL
)
3529 hash_table
->dynamic_relocs
= TRUE
;
3531 bed
= get_elf_backend_data (hash_table
->dynobj
);
3532 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3533 BFD_ASSERT (s
!= NULL
);
3535 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3536 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3537 if (newcontents
== NULL
)
3541 dyn
.d_un
.d_val
= val
;
3542 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3545 s
->contents
= newcontents
;
3550 /* Strip zero-sized dynamic sections. */
3553 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info
*info
)
3555 struct elf_link_hash_table
*hash_table
;
3556 const struct elf_backend_data
*bed
;
3557 asection
*s
, *sdynamic
, **pp
;
3558 asection
*rela_dyn
, *rel_dyn
;
3559 Elf_Internal_Dyn dyn
;
3560 bfd_byte
*extdyn
, *next
;
3561 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
3562 bfd_boolean strip_zero_sized
;
3563 bfd_boolean strip_zero_sized_plt
;
3565 if (bfd_link_relocatable (info
))
3568 hash_table
= elf_hash_table (info
);
3569 if (!is_elf_hash_table (hash_table
))
3572 if (!hash_table
->dynobj
)
3575 sdynamic
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3579 bed
= get_elf_backend_data (hash_table
->dynobj
);
3580 swap_dyn_in
= bed
->s
->swap_dyn_in
;
3582 strip_zero_sized
= FALSE
;
3583 strip_zero_sized_plt
= FALSE
;
3585 /* Strip zero-sized dynamic sections. */
3586 rela_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rela.dyn");
3587 rel_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rel.dyn");
3588 for (pp
= &info
->output_bfd
->sections
; (s
= *pp
) != NULL
;)
3592 || s
== hash_table
->srelplt
->output_section
3593 || s
== hash_table
->splt
->output_section
))
3596 info
->output_bfd
->section_count
--;
3597 strip_zero_sized
= TRUE
;
3602 else if (s
== hash_table
->splt
->output_section
)
3604 s
= hash_table
->splt
;
3605 strip_zero_sized_plt
= TRUE
;
3608 s
= hash_table
->srelplt
;
3609 s
->flags
|= SEC_EXCLUDE
;
3610 s
->output_section
= bfd_abs_section_ptr
;
3615 if (strip_zero_sized_plt
)
3616 for (extdyn
= sdynamic
->contents
;
3617 extdyn
< sdynamic
->contents
+ sdynamic
->size
;
3620 next
= extdyn
+ bed
->s
->sizeof_dyn
;
3621 swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3629 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3630 the procedure linkage table (the .plt section) has been
3632 memmove (extdyn
, next
,
3633 sdynamic
->size
- (next
- sdynamic
->contents
));
3638 if (strip_zero_sized
)
3640 /* Regenerate program headers. */
3641 elf_seg_map (info
->output_bfd
) = NULL
;
3642 return _bfd_elf_map_sections_to_segments (info
->output_bfd
, info
);
3648 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3649 1 if a DT_NEEDED tag already exists, and 0 on success. */
3652 bfd_elf_add_dt_needed_tag (bfd
*abfd
, struct bfd_link_info
*info
)
3654 struct elf_link_hash_table
*hash_table
;
3658 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3661 hash_table
= elf_hash_table (info
);
3662 soname
= elf_dt_name (abfd
);
3663 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3664 if (strindex
== (size_t) -1)
3667 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3670 const struct elf_backend_data
*bed
;
3673 bed
= get_elf_backend_data (hash_table
->dynobj
);
3674 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3676 for (extdyn
= sdyn
->contents
;
3677 extdyn
< sdyn
->contents
+ sdyn
->size
;
3678 extdyn
+= bed
->s
->sizeof_dyn
)
3680 Elf_Internal_Dyn dyn
;
3682 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3683 if (dyn
.d_tag
== DT_NEEDED
3684 && dyn
.d_un
.d_val
== strindex
)
3686 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3692 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3695 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3701 /* Return true if SONAME is on the needed list between NEEDED and STOP
3702 (or the end of list if STOP is NULL), and needed by a library that
3706 on_needed_list (const char *soname
,
3707 struct bfd_link_needed_list
*needed
,
3708 struct bfd_link_needed_list
*stop
)
3710 struct bfd_link_needed_list
*look
;
3711 for (look
= needed
; look
!= stop
; look
= look
->next
)
3712 if (strcmp (soname
, look
->name
) == 0
3713 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3714 /* If needed by a library that itself is not directly
3715 needed, recursively check whether that library is
3716 indirectly needed. Since we add DT_NEEDED entries to
3717 the end of the list, library dependencies appear after
3718 the library. Therefore search prior to the current
3719 LOOK, preventing possible infinite recursion. */
3720 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3726 /* Sort symbol by value, section, size, and type. */
3728 elf_sort_symbol (const void *arg1
, const void *arg2
)
3730 const struct elf_link_hash_entry
*h1
;
3731 const struct elf_link_hash_entry
*h2
;
3732 bfd_signed_vma vdiff
;
3737 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3738 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3739 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3741 return vdiff
> 0 ? 1 : -1;
3743 sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3747 /* Sort so that sized symbols are selected over zero size symbols. */
3748 vdiff
= h1
->size
- h2
->size
;
3750 return vdiff
> 0 ? 1 : -1;
3752 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3753 if (h1
->type
!= h2
->type
)
3754 return h1
->type
- h2
->type
;
3756 /* If symbols are properly sized and typed, and multiple strong
3757 aliases are not defined in a shared library by the user we
3758 shouldn't get here. Unfortunately linker script symbols like
3759 __bss_start sometimes match a user symbol defined at the start of
3760 .bss without proper size and type. We'd like to preference the
3761 user symbol over reserved system symbols. Sort on leading
3763 n1
= h1
->root
.root
.string
;
3764 n2
= h2
->root
.root
.string
;
3777 /* Final sort on name selects user symbols like '_u' over reserved
3778 system symbols like '_Z' and also will avoid qsort instability. */
3782 /* This function is used to adjust offsets into .dynstr for
3783 dynamic symbols. This is called via elf_link_hash_traverse. */
3786 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3788 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3790 if (h
->dynindx
!= -1)
3791 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3795 /* Assign string offsets in .dynstr, update all structures referencing
3799 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3801 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3802 struct elf_link_local_dynamic_entry
*entry
;
3803 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3804 bfd
*dynobj
= hash_table
->dynobj
;
3807 const struct elf_backend_data
*bed
;
3810 _bfd_elf_strtab_finalize (dynstr
);
3811 size
= _bfd_elf_strtab_size (dynstr
);
3813 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3815 if (info
->callbacks
->examine_strtab
)
3816 info
->callbacks
->examine_strtab (dynstr
);
3818 bed
= get_elf_backend_data (dynobj
);
3819 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3820 BFD_ASSERT (sdyn
!= NULL
);
3822 /* Update all .dynamic entries referencing .dynstr strings. */
3823 for (extdyn
= sdyn
->contents
;
3824 extdyn
< sdyn
->contents
+ sdyn
->size
;
3825 extdyn
+= bed
->s
->sizeof_dyn
)
3827 Elf_Internal_Dyn dyn
;
3829 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3833 dyn
.d_un
.d_val
= size
;
3843 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3848 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3851 /* Now update local dynamic symbols. */
3852 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3853 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3854 entry
->isym
.st_name
);
3856 /* And the rest of dynamic symbols. */
3857 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3859 /* Adjust version definitions. */
3860 if (elf_tdata (output_bfd
)->cverdefs
)
3865 Elf_Internal_Verdef def
;
3866 Elf_Internal_Verdaux defaux
;
3868 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3872 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3874 p
+= sizeof (Elf_External_Verdef
);
3875 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3877 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3879 _bfd_elf_swap_verdaux_in (output_bfd
,
3880 (Elf_External_Verdaux
*) p
, &defaux
);
3881 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3883 _bfd_elf_swap_verdaux_out (output_bfd
,
3884 &defaux
, (Elf_External_Verdaux
*) p
);
3885 p
+= sizeof (Elf_External_Verdaux
);
3888 while (def
.vd_next
);
3891 /* Adjust version references. */
3892 if (elf_tdata (output_bfd
)->verref
)
3897 Elf_Internal_Verneed need
;
3898 Elf_Internal_Vernaux needaux
;
3900 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
3904 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3906 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3907 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3908 (Elf_External_Verneed
*) p
);
3909 p
+= sizeof (Elf_External_Verneed
);
3910 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3912 _bfd_elf_swap_vernaux_in (output_bfd
,
3913 (Elf_External_Vernaux
*) p
, &needaux
);
3914 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3916 _bfd_elf_swap_vernaux_out (output_bfd
,
3918 (Elf_External_Vernaux
*) p
);
3919 p
+= sizeof (Elf_External_Vernaux
);
3922 while (need
.vn_next
);
3928 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3929 The default is to only match when the INPUT and OUTPUT are exactly
3933 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3934 const bfd_target
*output
)
3936 return input
== output
;
3939 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3940 This version is used when different targets for the same architecture
3941 are virtually identical. */
3944 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3945 const bfd_target
*output
)
3947 const struct elf_backend_data
*obed
, *ibed
;
3949 if (input
== output
)
3952 ibed
= xvec_get_elf_backend_data (input
);
3953 obed
= xvec_get_elf_backend_data (output
);
3955 if (ibed
->arch
!= obed
->arch
)
3958 /* If both backends are using this function, deem them compatible. */
3959 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3962 /* Make a special call to the linker "notice" function to tell it that
3963 we are about to handle an as-needed lib, or have finished
3964 processing the lib. */
3967 _bfd_elf_notice_as_needed (bfd
*ibfd
,
3968 struct bfd_link_info
*info
,
3969 enum notice_asneeded_action act
)
3971 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
3974 /* Check relocations an ELF object file. */
3977 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
3979 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3980 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
3982 /* If this object is the same format as the output object, and it is
3983 not a shared library, then let the backend look through the
3986 This is required to build global offset table entries and to
3987 arrange for dynamic relocs. It is not required for the
3988 particular common case of linking non PIC code, even when linking
3989 against shared libraries, but unfortunately there is no way of
3990 knowing whether an object file has been compiled PIC or not.
3991 Looking through the relocs is not particularly time consuming.
3992 The problem is that we must either (1) keep the relocs in memory,
3993 which causes the linker to require additional runtime memory or
3994 (2) read the relocs twice from the input file, which wastes time.
3995 This would be a good case for using mmap.
3997 I have no idea how to handle linking PIC code into a file of a
3998 different format. It probably can't be done. */
3999 if ((abfd
->flags
& DYNAMIC
) == 0
4000 && is_elf_hash_table (htab
)
4001 && bed
->check_relocs
!= NULL
4002 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
4003 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
4007 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4009 Elf_Internal_Rela
*internal_relocs
;
4012 /* Don't check relocations in excluded sections. Don't do
4013 anything special with non-loaded, non-alloced sections.
4014 In particular, any relocs in such sections should not
4015 affect GOT and PLT reference counting (ie. we don't
4016 allow them to create GOT or PLT entries), there's no
4017 possibility or desire to optimize TLS relocs, and
4018 there's not much point in propagating relocs to shared
4019 libs that the dynamic linker won't relocate. */
4020 if ((o
->flags
& SEC_ALLOC
) == 0
4021 || (o
->flags
& SEC_RELOC
) == 0
4022 || (o
->flags
& SEC_EXCLUDE
) != 0
4023 || o
->reloc_count
== 0
4024 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4025 && (o
->flags
& SEC_DEBUGGING
) != 0)
4026 || bfd_is_abs_section (o
->output_section
))
4029 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4031 if (internal_relocs
== NULL
)
4034 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
4036 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4037 free (internal_relocs
);
4047 /* Add symbols from an ELF object file to the linker hash table. */
4050 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4052 Elf_Internal_Ehdr
*ehdr
;
4053 Elf_Internal_Shdr
*hdr
;
4057 struct elf_link_hash_entry
**sym_hash
;
4058 bfd_boolean dynamic
;
4059 Elf_External_Versym
*extversym
= NULL
;
4060 Elf_External_Versym
*extversym_end
= NULL
;
4061 Elf_External_Versym
*ever
;
4062 struct elf_link_hash_entry
*weaks
;
4063 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
4064 size_t nondeflt_vers_cnt
= 0;
4065 Elf_Internal_Sym
*isymbuf
= NULL
;
4066 Elf_Internal_Sym
*isym
;
4067 Elf_Internal_Sym
*isymend
;
4068 const struct elf_backend_data
*bed
;
4069 bfd_boolean add_needed
;
4070 struct elf_link_hash_table
*htab
;
4071 void *alloc_mark
= NULL
;
4072 struct bfd_hash_entry
**old_table
= NULL
;
4073 unsigned int old_size
= 0;
4074 unsigned int old_count
= 0;
4075 void *old_tab
= NULL
;
4077 struct bfd_link_hash_entry
*old_undefs
= NULL
;
4078 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
4079 void *old_strtab
= NULL
;
4082 bfd_boolean just_syms
;
4084 htab
= elf_hash_table (info
);
4085 bed
= get_elf_backend_data (abfd
);
4087 if ((abfd
->flags
& DYNAMIC
) == 0)
4093 /* You can't use -r against a dynamic object. Also, there's no
4094 hope of using a dynamic object which does not exactly match
4095 the format of the output file. */
4096 if (bfd_link_relocatable (info
)
4097 || !is_elf_hash_table (htab
)
4098 || info
->output_bfd
->xvec
!= abfd
->xvec
)
4100 if (bfd_link_relocatable (info
))
4101 bfd_set_error (bfd_error_invalid_operation
);
4103 bfd_set_error (bfd_error_wrong_format
);
4108 ehdr
= elf_elfheader (abfd
);
4109 if (info
->warn_alternate_em
4110 && bed
->elf_machine_code
!= ehdr
->e_machine
4111 && ((bed
->elf_machine_alt1
!= 0
4112 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
4113 || (bed
->elf_machine_alt2
!= 0
4114 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
4116 /* xgettext:c-format */
4117 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4118 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
4120 /* As a GNU extension, any input sections which are named
4121 .gnu.warning.SYMBOL are treated as warning symbols for the given
4122 symbol. This differs from .gnu.warning sections, which generate
4123 warnings when they are included in an output file. */
4124 /* PR 12761: Also generate this warning when building shared libraries. */
4125 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4129 name
= bfd_section_name (s
);
4130 if (CONST_STRNEQ (name
, ".gnu.warning."))
4135 name
+= sizeof ".gnu.warning." - 1;
4137 /* If this is a shared object, then look up the symbol
4138 in the hash table. If it is there, and it is already
4139 been defined, then we will not be using the entry
4140 from this shared object, so we don't need to warn.
4141 FIXME: If we see the definition in a regular object
4142 later on, we will warn, but we shouldn't. The only
4143 fix is to keep track of what warnings we are supposed
4144 to emit, and then handle them all at the end of the
4148 struct elf_link_hash_entry
*h
;
4150 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
4152 /* FIXME: What about bfd_link_hash_common? */
4154 && (h
->root
.type
== bfd_link_hash_defined
4155 || h
->root
.type
== bfd_link_hash_defweak
))
4160 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
4164 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
4169 if (! (_bfd_generic_link_add_one_symbol
4170 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
4171 FALSE
, bed
->collect
, NULL
)))
4174 if (bfd_link_executable (info
))
4176 /* Clobber the section size so that the warning does
4177 not get copied into the output file. */
4180 /* Also set SEC_EXCLUDE, so that symbols defined in
4181 the warning section don't get copied to the output. */
4182 s
->flags
|= SEC_EXCLUDE
;
4187 just_syms
= ((s
= abfd
->sections
) != NULL
4188 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
4193 /* If we are creating a shared library, create all the dynamic
4194 sections immediately. We need to attach them to something,
4195 so we attach them to this BFD, provided it is the right
4196 format and is not from ld --just-symbols. Always create the
4197 dynamic sections for -E/--dynamic-list. FIXME: If there
4198 are no input BFD's of the same format as the output, we can't
4199 make a shared library. */
4201 && (bfd_link_pic (info
)
4202 || (!bfd_link_relocatable (info
)
4204 && (info
->export_dynamic
|| info
->dynamic
)))
4205 && is_elf_hash_table (htab
)
4206 && info
->output_bfd
->xvec
== abfd
->xvec
4207 && !htab
->dynamic_sections_created
)
4209 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
4213 else if (!is_elf_hash_table (htab
))
4217 const char *soname
= NULL
;
4219 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
4220 const Elf_Internal_Phdr
*phdr
;
4221 struct elf_link_loaded_list
*loaded_lib
;
4223 /* ld --just-symbols and dynamic objects don't mix very well.
4224 ld shouldn't allow it. */
4228 /* If this dynamic lib was specified on the command line with
4229 --as-needed in effect, then we don't want to add a DT_NEEDED
4230 tag unless the lib is actually used. Similary for libs brought
4231 in by another lib's DT_NEEDED. When --no-add-needed is used
4232 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4233 any dynamic library in DT_NEEDED tags in the dynamic lib at
4235 add_needed
= (elf_dyn_lib_class (abfd
)
4236 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
4237 | DYN_NO_NEEDED
)) == 0;
4239 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4244 unsigned int elfsec
;
4245 unsigned long shlink
;
4247 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
4254 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
4255 if (elfsec
== SHN_BAD
)
4256 goto error_free_dyn
;
4257 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
4259 for (extdyn
= dynbuf
;
4260 extdyn
<= dynbuf
+ s
->size
- bed
->s
->sizeof_dyn
;
4261 extdyn
+= bed
->s
->sizeof_dyn
)
4263 Elf_Internal_Dyn dyn
;
4265 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
4266 if (dyn
.d_tag
== DT_SONAME
)
4268 unsigned int tagv
= dyn
.d_un
.d_val
;
4269 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4271 goto error_free_dyn
;
4273 if (dyn
.d_tag
== DT_NEEDED
)
4275 struct bfd_link_needed_list
*n
, **pn
;
4277 unsigned int tagv
= dyn
.d_un
.d_val
;
4278 size_t amt
= sizeof (struct bfd_link_needed_list
);
4280 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4281 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4282 if (n
== NULL
|| fnm
== NULL
)
4283 goto error_free_dyn
;
4284 amt
= strlen (fnm
) + 1;
4285 anm
= (char *) bfd_alloc (abfd
, amt
);
4287 goto error_free_dyn
;
4288 memcpy (anm
, fnm
, amt
);
4292 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4296 if (dyn
.d_tag
== DT_RUNPATH
)
4298 struct bfd_link_needed_list
*n
, **pn
;
4300 unsigned int tagv
= dyn
.d_un
.d_val
;
4301 size_t amt
= sizeof (struct bfd_link_needed_list
);
4303 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4304 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4305 if (n
== NULL
|| fnm
== NULL
)
4306 goto error_free_dyn
;
4307 amt
= strlen (fnm
) + 1;
4308 anm
= (char *) bfd_alloc (abfd
, amt
);
4310 goto error_free_dyn
;
4311 memcpy (anm
, fnm
, amt
);
4315 for (pn
= & runpath
;
4321 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4322 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
4324 struct bfd_link_needed_list
*n
, **pn
;
4326 unsigned int tagv
= dyn
.d_un
.d_val
;
4327 size_t amt
= sizeof (struct bfd_link_needed_list
);
4329 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4330 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4331 if (n
== NULL
|| fnm
== NULL
)
4332 goto error_free_dyn
;
4333 amt
= strlen (fnm
) + 1;
4334 anm
= (char *) bfd_alloc (abfd
, amt
);
4336 goto error_free_dyn
;
4337 memcpy (anm
, fnm
, amt
);
4347 if (dyn
.d_tag
== DT_AUDIT
)
4349 unsigned int tagv
= dyn
.d_un
.d_val
;
4350 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4357 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4358 frees all more recently bfd_alloc'd blocks as well. */
4364 struct bfd_link_needed_list
**pn
;
4365 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4370 /* If we have a PT_GNU_RELRO program header, mark as read-only
4371 all sections contained fully therein. This makes relro
4372 shared library sections appear as they will at run-time. */
4373 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4374 while (phdr
-- > elf_tdata (abfd
)->phdr
)
4375 if (phdr
->p_type
== PT_GNU_RELRO
)
4377 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4379 unsigned int opb
= bfd_octets_per_byte (abfd
, s
);
4381 if ((s
->flags
& SEC_ALLOC
) != 0
4382 && s
->vma
* opb
>= phdr
->p_vaddr
4383 && s
->vma
* opb
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4384 s
->flags
|= SEC_READONLY
;
4389 /* We do not want to include any of the sections in a dynamic
4390 object in the output file. We hack by simply clobbering the
4391 list of sections in the BFD. This could be handled more
4392 cleanly by, say, a new section flag; the existing
4393 SEC_NEVER_LOAD flag is not the one we want, because that one
4394 still implies that the section takes up space in the output
4396 bfd_section_list_clear (abfd
);
4398 /* Find the name to use in a DT_NEEDED entry that refers to this
4399 object. If the object has a DT_SONAME entry, we use it.
4400 Otherwise, if the generic linker stuck something in
4401 elf_dt_name, we use that. Otherwise, we just use the file
4403 if (soname
== NULL
|| *soname
== '\0')
4405 soname
= elf_dt_name (abfd
);
4406 if (soname
== NULL
|| *soname
== '\0')
4407 soname
= bfd_get_filename (abfd
);
4410 /* Save the SONAME because sometimes the linker emulation code
4411 will need to know it. */
4412 elf_dt_name (abfd
) = soname
;
4414 /* If we have already included this dynamic object in the
4415 link, just ignore it. There is no reason to include a
4416 particular dynamic object more than once. */
4417 for (loaded_lib
= htab
->dyn_loaded
;
4419 loaded_lib
= loaded_lib
->next
)
4421 if (strcmp (elf_dt_name (loaded_lib
->abfd
), soname
) == 0)
4425 /* Create dynamic sections for backends that require that be done
4426 before setup_gnu_properties. */
4428 && !_bfd_elf_link_create_dynamic_sections (abfd
, info
))
4431 /* Save the DT_AUDIT entry for the linker emulation code. */
4432 elf_dt_audit (abfd
) = audit
;
4435 /* If this is a dynamic object, we always link against the .dynsym
4436 symbol table, not the .symtab symbol table. The dynamic linker
4437 will only see the .dynsym symbol table, so there is no reason to
4438 look at .symtab for a dynamic object. */
4440 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4441 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4443 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4445 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4447 /* The sh_info field of the symtab header tells us where the
4448 external symbols start. We don't care about the local symbols at
4450 if (elf_bad_symtab (abfd
))
4452 extsymcount
= symcount
;
4457 extsymcount
= symcount
- hdr
->sh_info
;
4458 extsymoff
= hdr
->sh_info
;
4461 sym_hash
= elf_sym_hashes (abfd
);
4462 if (extsymcount
!= 0)
4464 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4466 if (isymbuf
== NULL
)
4469 if (sym_hash
== NULL
)
4471 /* We store a pointer to the hash table entry for each
4473 size_t amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4474 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4475 if (sym_hash
== NULL
)
4476 goto error_free_sym
;
4477 elf_sym_hashes (abfd
) = sym_hash
;
4483 /* Read in any version definitions. */
4484 if (!_bfd_elf_slurp_version_tables (abfd
,
4485 info
->default_imported_symver
))
4486 goto error_free_sym
;
4488 /* Read in the symbol versions, but don't bother to convert them
4489 to internal format. */
4490 if (elf_dynversym (abfd
) != 0)
4492 Elf_Internal_Shdr
*versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4493 bfd_size_type amt
= versymhdr
->sh_size
;
4495 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0)
4496 goto error_free_sym
;
4497 extversym
= (Elf_External_Versym
*)
4498 _bfd_malloc_and_read (abfd
, amt
, amt
);
4499 if (extversym
== NULL
)
4500 goto error_free_sym
;
4501 extversym_end
= extversym
+ amt
/ sizeof (*extversym
);
4505 /* If we are loading an as-needed shared lib, save the symbol table
4506 state before we start adding symbols. If the lib turns out
4507 to be unneeded, restore the state. */
4508 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4513 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4515 struct bfd_hash_entry
*p
;
4516 struct elf_link_hash_entry
*h
;
4518 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4520 h
= (struct elf_link_hash_entry
*) p
;
4521 entsize
+= htab
->root
.table
.entsize
;
4522 if (h
->root
.type
== bfd_link_hash_warning
)
4524 entsize
+= htab
->root
.table
.entsize
;
4525 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4527 if (h
->root
.type
== bfd_link_hash_common
)
4528 entsize
+= sizeof (*h
->root
.u
.c
.p
);
4532 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4533 old_tab
= bfd_malloc (tabsize
+ entsize
);
4534 if (old_tab
== NULL
)
4535 goto error_free_vers
;
4537 /* Remember the current objalloc pointer, so that all mem for
4538 symbols added can later be reclaimed. */
4539 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4540 if (alloc_mark
== NULL
)
4541 goto error_free_vers
;
4543 /* Make a special call to the linker "notice" function to
4544 tell it that we are about to handle an as-needed lib. */
4545 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4546 goto error_free_vers
;
4548 /* Clone the symbol table. Remember some pointers into the
4549 symbol table, and dynamic symbol count. */
4550 old_ent
= (char *) old_tab
+ tabsize
;
4551 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4552 old_undefs
= htab
->root
.undefs
;
4553 old_undefs_tail
= htab
->root
.undefs_tail
;
4554 old_table
= htab
->root
.table
.table
;
4555 old_size
= htab
->root
.table
.size
;
4556 old_count
= htab
->root
.table
.count
;
4558 if (htab
->dynstr
!= NULL
)
4560 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4561 if (old_strtab
== NULL
)
4562 goto error_free_vers
;
4565 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4567 struct bfd_hash_entry
*p
;
4568 struct elf_link_hash_entry
*h
;
4570 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4572 h
= (struct elf_link_hash_entry
*) p
;
4573 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4574 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4575 if (h
->root
.type
== bfd_link_hash_warning
)
4577 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4578 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4579 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4581 if (h
->root
.type
== bfd_link_hash_common
)
4583 memcpy (old_ent
, h
->root
.u
.c
.p
, sizeof (*h
->root
.u
.c
.p
));
4584 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
4591 if (extversym
== NULL
)
4593 else if (extversym
+ extsymoff
< extversym_end
)
4594 ever
= extversym
+ extsymoff
;
4597 /* xgettext:c-format */
4598 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4599 abfd
, (long) extsymoff
,
4600 (long) (extversym_end
- extversym
) / sizeof (* extversym
));
4601 bfd_set_error (bfd_error_bad_value
);
4602 goto error_free_vers
;
4605 if (!bfd_link_relocatable (info
)
4606 && abfd
->lto_slim_object
)
4609 (_("%pB: plugin needed to handle lto object"), abfd
);
4612 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
4614 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4618 asection
*sec
, *new_sec
;
4621 struct elf_link_hash_entry
*h
;
4622 struct elf_link_hash_entry
*hi
;
4623 bfd_boolean definition
;
4624 bfd_boolean size_change_ok
;
4625 bfd_boolean type_change_ok
;
4626 bfd_boolean new_weak
;
4627 bfd_boolean old_weak
;
4630 bfd_boolean discarded
;
4631 unsigned int old_alignment
;
4632 unsigned int shindex
;
4634 bfd_boolean matched
;
4638 flags
= BSF_NO_FLAGS
;
4640 value
= isym
->st_value
;
4641 common
= bed
->common_definition (isym
);
4642 if (common
&& info
->inhibit_common_definition
)
4644 /* Treat common symbol as undefined for --no-define-common. */
4645 isym
->st_shndx
= SHN_UNDEF
;
4650 bind
= ELF_ST_BIND (isym
->st_info
);
4654 /* This should be impossible, since ELF requires that all
4655 global symbols follow all local symbols, and that sh_info
4656 point to the first global symbol. Unfortunately, Irix 5
4658 if (elf_bad_symtab (abfd
))
4661 /* If we aren't prepared to handle locals within the globals
4662 then we'll likely segfault on a NULL symbol hash if the
4663 symbol is ever referenced in relocations. */
4664 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
4665 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, hdr
->sh_name
);
4666 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4667 " (>= sh_info of %lu)"),
4668 abfd
, name
, (long) (isym
- isymbuf
+ extsymoff
),
4671 /* Dynamic object relocations are not processed by ld, so
4672 ld won't run into the problem mentioned above. */
4675 bfd_set_error (bfd_error_bad_value
);
4676 goto error_free_vers
;
4679 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4687 case STB_GNU_UNIQUE
:
4688 flags
= BSF_GNU_UNIQUE
;
4692 /* Leave it up to the processor backend. */
4696 if (isym
->st_shndx
== SHN_UNDEF
)
4697 sec
= bfd_und_section_ptr
;
4698 else if (isym
->st_shndx
== SHN_ABS
)
4699 sec
= bfd_abs_section_ptr
;
4700 else if (isym
->st_shndx
== SHN_COMMON
)
4702 sec
= bfd_com_section_ptr
;
4703 /* What ELF calls the size we call the value. What ELF
4704 calls the value we call the alignment. */
4705 value
= isym
->st_size
;
4709 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4711 sec
= bfd_abs_section_ptr
;
4712 else if (discarded_section (sec
))
4714 /* Symbols from discarded section are undefined. We keep
4716 sec
= bfd_und_section_ptr
;
4718 isym
->st_shndx
= SHN_UNDEF
;
4720 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4724 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4727 goto error_free_vers
;
4729 if (isym
->st_shndx
== SHN_COMMON
4730 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4732 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4736 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4738 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4740 goto error_free_vers
;
4744 else if (isym
->st_shndx
== SHN_COMMON
4745 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4746 && !bfd_link_relocatable (info
))
4748 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4752 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4753 | SEC_LINKER_CREATED
);
4754 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4756 goto error_free_vers
;
4760 else if (bed
->elf_add_symbol_hook
)
4762 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4764 goto error_free_vers
;
4766 /* The hook function sets the name to NULL if this symbol
4767 should be skipped for some reason. */
4772 /* Sanity check that all possibilities were handled. */
4776 /* Silently discard TLS symbols from --just-syms. There's
4777 no way to combine a static TLS block with a new TLS block
4778 for this executable. */
4779 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4780 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4783 if (bfd_is_und_section (sec
)
4784 || bfd_is_com_section (sec
))
4789 size_change_ok
= FALSE
;
4790 type_change_ok
= bed
->type_change_ok
;
4797 if (is_elf_hash_table (htab
))
4799 Elf_Internal_Versym iver
;
4800 unsigned int vernum
= 0;
4805 if (info
->default_imported_symver
)
4806 /* Use the default symbol version created earlier. */
4807 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4811 else if (ever
>= extversym_end
)
4813 /* xgettext:c-format */
4814 _bfd_error_handler (_("%pB: not enough version information"),
4816 bfd_set_error (bfd_error_bad_value
);
4817 goto error_free_vers
;
4820 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4822 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4824 /* If this is a hidden symbol, or if it is not version
4825 1, we append the version name to the symbol name.
4826 However, we do not modify a non-hidden absolute symbol
4827 if it is not a function, because it might be the version
4828 symbol itself. FIXME: What if it isn't? */
4829 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4831 && (!bfd_is_abs_section (sec
)
4832 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4835 size_t namelen
, verlen
, newlen
;
4838 if (isym
->st_shndx
!= SHN_UNDEF
)
4840 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4842 else if (vernum
> 1)
4844 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4851 /* xgettext:c-format */
4852 (_("%pB: %s: invalid version %u (max %d)"),
4854 elf_tdata (abfd
)->cverdefs
);
4855 bfd_set_error (bfd_error_bad_value
);
4856 goto error_free_vers
;
4861 /* We cannot simply test for the number of
4862 entries in the VERNEED section since the
4863 numbers for the needed versions do not start
4865 Elf_Internal_Verneed
*t
;
4868 for (t
= elf_tdata (abfd
)->verref
;
4872 Elf_Internal_Vernaux
*a
;
4874 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4876 if (a
->vna_other
== vernum
)
4878 verstr
= a
->vna_nodename
;
4888 /* xgettext:c-format */
4889 (_("%pB: %s: invalid needed version %d"),
4890 abfd
, name
, vernum
);
4891 bfd_set_error (bfd_error_bad_value
);
4892 goto error_free_vers
;
4896 namelen
= strlen (name
);
4897 verlen
= strlen (verstr
);
4898 newlen
= namelen
+ verlen
+ 2;
4899 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4900 && isym
->st_shndx
!= SHN_UNDEF
)
4903 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4904 if (newname
== NULL
)
4905 goto error_free_vers
;
4906 memcpy (newname
, name
, namelen
);
4907 p
= newname
+ namelen
;
4909 /* If this is a defined non-hidden version symbol,
4910 we add another @ to the name. This indicates the
4911 default version of the symbol. */
4912 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4913 && isym
->st_shndx
!= SHN_UNDEF
)
4915 memcpy (p
, verstr
, verlen
+ 1);
4920 /* If this symbol has default visibility and the user has
4921 requested we not re-export it, then mark it as hidden. */
4922 if (!bfd_is_und_section (sec
)
4925 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4926 isym
->st_other
= (STV_HIDDEN
4927 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4929 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4930 sym_hash
, &old_bfd
, &old_weak
,
4931 &old_alignment
, &skip
, &override
,
4932 &type_change_ok
, &size_change_ok
,
4934 goto error_free_vers
;
4939 /* Override a definition only if the new symbol matches the
4941 if (override
&& matched
)
4945 while (h
->root
.type
== bfd_link_hash_indirect
4946 || h
->root
.type
== bfd_link_hash_warning
)
4947 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4949 if (elf_tdata (abfd
)->verdef
!= NULL
4952 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4955 if (! (_bfd_generic_link_add_one_symbol
4956 (info
, override
? override
: abfd
, name
, flags
, sec
, value
,
4957 NULL
, FALSE
, bed
->collect
,
4958 (struct bfd_link_hash_entry
**) sym_hash
)))
4959 goto error_free_vers
;
4962 /* We need to make sure that indirect symbol dynamic flags are
4965 while (h
->root
.type
== bfd_link_hash_indirect
4966 || h
->root
.type
== bfd_link_hash_warning
)
4967 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4969 /* Setting the index to -3 tells elf_link_output_extsym that
4970 this symbol is defined in a discarded section. */
4976 new_weak
= (flags
& BSF_WEAK
) != 0;
4980 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4981 && is_elf_hash_table (htab
)
4982 && h
->u
.alias
== NULL
)
4984 /* Keep a list of all weak defined non function symbols from
4985 a dynamic object, using the alias field. Later in this
4986 function we will set the alias field to the correct
4987 value. We only put non-function symbols from dynamic
4988 objects on this list, because that happens to be the only
4989 time we need to know the normal symbol corresponding to a
4990 weak symbol, and the information is time consuming to
4991 figure out. If the alias field is not already NULL,
4992 then this symbol was already defined by some previous
4993 dynamic object, and we will be using that previous
4994 definition anyhow. */
5000 /* Set the alignment of a common symbol. */
5001 if ((common
|| bfd_is_com_section (sec
))
5002 && h
->root
.type
== bfd_link_hash_common
)
5007 align
= bfd_log2 (isym
->st_value
);
5010 /* The new symbol is a common symbol in a shared object.
5011 We need to get the alignment from the section. */
5012 align
= new_sec
->alignment_power
;
5014 if (align
> old_alignment
)
5015 h
->root
.u
.c
.p
->alignment_power
= align
;
5017 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
5020 if (is_elf_hash_table (htab
))
5022 /* Set a flag in the hash table entry indicating the type of
5023 reference or definition we just found. A dynamic symbol
5024 is one which is referenced or defined by both a regular
5025 object and a shared object. */
5026 bfd_boolean dynsym
= FALSE
;
5028 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5029 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5036 if (bind
!= STB_WEAK
)
5037 h
->ref_regular_nonweak
= 1;
5054 hi
->ref_dynamic
= 1;
5059 hi
->def_dynamic
= 1;
5063 /* If an indirect symbol has been forced local, don't
5064 make the real symbol dynamic. */
5065 if (h
!= hi
&& hi
->forced_local
)
5069 if (bfd_link_dll (info
)
5079 && weakdef (h
)->dynindx
!= -1))
5083 /* Check to see if we need to add an indirect symbol for
5084 the default name. */
5086 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
5088 && hi
->versioned
== versioned_hidden
))
5089 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
5090 sec
, value
, &old_bfd
, &dynsym
))
5091 goto error_free_vers
;
5093 /* Check the alignment when a common symbol is involved. This
5094 can change when a common symbol is overridden by a normal
5095 definition or a common symbol is ignored due to the old
5096 normal definition. We need to make sure the maximum
5097 alignment is maintained. */
5098 if ((old_alignment
|| common
)
5099 && h
->root
.type
!= bfd_link_hash_common
)
5101 unsigned int common_align
;
5102 unsigned int normal_align
;
5103 unsigned int symbol_align
;
5107 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
5108 || h
->root
.type
== bfd_link_hash_defweak
);
5110 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
5111 if (h
->root
.u
.def
.section
->owner
!= NULL
5112 && (h
->root
.u
.def
.section
->owner
->flags
5113 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
5115 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
5116 if (normal_align
> symbol_align
)
5117 normal_align
= symbol_align
;
5120 normal_align
= symbol_align
;
5124 common_align
= old_alignment
;
5125 common_bfd
= old_bfd
;
5130 common_align
= bfd_log2 (isym
->st_value
);
5132 normal_bfd
= old_bfd
;
5135 if (normal_align
< common_align
)
5137 /* PR binutils/2735 */
5138 if (normal_bfd
== NULL
)
5140 /* xgettext:c-format */
5141 (_("warning: alignment %u of common symbol `%s' in %pB is"
5142 " greater than the alignment (%u) of its section %pA"),
5143 1 << common_align
, name
, common_bfd
,
5144 1 << normal_align
, h
->root
.u
.def
.section
);
5147 /* xgettext:c-format */
5148 (_("warning: alignment %u of symbol `%s' in %pB"
5149 " is smaller than %u in %pB"),
5150 1 << normal_align
, name
, normal_bfd
,
5151 1 << common_align
, common_bfd
);
5155 /* Remember the symbol size if it isn't undefined. */
5156 if (isym
->st_size
!= 0
5157 && isym
->st_shndx
!= SHN_UNDEF
5158 && (definition
|| h
->size
== 0))
5161 && h
->size
!= isym
->st_size
5162 && ! size_change_ok
)
5164 /* xgettext:c-format */
5165 (_("warning: size of symbol `%s' changed"
5166 " from %" PRIu64
" in %pB to %" PRIu64
" in %pB"),
5167 name
, (uint64_t) h
->size
, old_bfd
,
5168 (uint64_t) isym
->st_size
, abfd
);
5170 h
->size
= isym
->st_size
;
5173 /* If this is a common symbol, then we always want H->SIZE
5174 to be the size of the common symbol. The code just above
5175 won't fix the size if a common symbol becomes larger. We
5176 don't warn about a size change here, because that is
5177 covered by --warn-common. Allow changes between different
5179 if (h
->root
.type
== bfd_link_hash_common
)
5180 h
->size
= h
->root
.u
.c
.size
;
5182 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
5183 && ((definition
&& !new_weak
)
5184 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
5185 || h
->type
== STT_NOTYPE
))
5187 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
5189 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5191 if (type
== STT_GNU_IFUNC
5192 && (abfd
->flags
& DYNAMIC
) != 0)
5195 if (h
->type
!= type
)
5197 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
5198 /* xgettext:c-format */
5200 (_("warning: type of symbol `%s' changed"
5201 " from %d to %d in %pB"),
5202 name
, h
->type
, type
, abfd
);
5208 /* Merge st_other field. */
5209 elf_merge_st_other (abfd
, h
, isym
->st_other
, sec
,
5210 definition
, dynamic
);
5212 /* We don't want to make debug symbol dynamic. */
5214 && (sec
->flags
& SEC_DEBUGGING
)
5215 && !bfd_link_relocatable (info
))
5218 /* Nor should we make plugin symbols dynamic. */
5219 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5224 h
->target_internal
= isym
->st_target_internal
;
5225 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
5228 if (definition
&& !dynamic
)
5230 char *p
= strchr (name
, ELF_VER_CHR
);
5231 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
5233 /* Queue non-default versions so that .symver x, x@FOO
5234 aliases can be checked. */
5237 size_t amt
= ((isymend
- isym
+ 1)
5238 * sizeof (struct elf_link_hash_entry
*));
5240 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5242 goto error_free_vers
;
5244 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
5248 if (dynsym
&& h
->dynindx
== -1)
5250 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5251 goto error_free_vers
;
5253 && weakdef (h
)->dynindx
== -1)
5255 if (!bfd_elf_link_record_dynamic_symbol (info
, weakdef (h
)))
5256 goto error_free_vers
;
5259 else if (h
->dynindx
!= -1)
5260 /* If the symbol already has a dynamic index, but
5261 visibility says it should not be visible, turn it into
5263 switch (ELF_ST_VISIBILITY (h
->other
))
5267 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
5276 && h
->ref_regular_nonweak
)
5278 && (old_bfd
->flags
& BFD_PLUGIN
) != 0
5279 && bind
!= STB_WEAK
)
5280 || (h
->ref_dynamic_nonweak
5281 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5282 && !on_needed_list (elf_dt_name (abfd
),
5283 htab
->needed
, NULL
))))
5285 const char *soname
= elf_dt_name (abfd
);
5287 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
5288 h
->root
.root
.string
);
5290 /* A symbol from a library loaded via DT_NEEDED of some
5291 other library is referenced by a regular object.
5292 Add a DT_NEEDED entry for it. Issue an error if
5293 --no-add-needed is used and the reference was not
5296 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
5299 /* xgettext:c-format */
5300 (_("%pB: undefined reference to symbol '%s'"),
5302 bfd_set_error (bfd_error_missing_dso
);
5303 goto error_free_vers
;
5306 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
5307 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
5309 /* Create dynamic sections for backends that require
5310 that be done before setup_gnu_properties. */
5311 if (!_bfd_elf_link_create_dynamic_sections (abfd
, info
))
5318 if (info
->lto_plugin_active
5319 && !bfd_link_relocatable (info
)
5320 && (abfd
->flags
& BFD_PLUGIN
) == 0
5326 if (bed
->s
->arch_size
== 32)
5331 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5332 referenced in regular objects so that linker plugin will get
5333 the correct symbol resolution. */
5335 sym_hash
= elf_sym_hashes (abfd
);
5336 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5338 Elf_Internal_Rela
*internal_relocs
;
5339 Elf_Internal_Rela
*rel
, *relend
;
5341 /* Don't check relocations in excluded sections. */
5342 if ((s
->flags
& SEC_RELOC
) == 0
5343 || s
->reloc_count
== 0
5344 || (s
->flags
& SEC_EXCLUDE
) != 0
5345 || ((info
->strip
== strip_all
5346 || info
->strip
== strip_debugger
)
5347 && (s
->flags
& SEC_DEBUGGING
) != 0))
5350 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, s
, NULL
,
5353 if (internal_relocs
== NULL
)
5354 goto error_free_vers
;
5356 rel
= internal_relocs
;
5357 relend
= rel
+ s
->reloc_count
;
5358 for ( ; rel
< relend
; rel
++)
5360 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
5361 struct elf_link_hash_entry
*h
;
5363 /* Skip local symbols. */
5364 if (r_symndx
< extsymoff
)
5367 h
= sym_hash
[r_symndx
- extsymoff
];
5369 h
->root
.non_ir_ref_regular
= 1;
5372 if (elf_section_data (s
)->relocs
!= internal_relocs
)
5373 free (internal_relocs
);
5382 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
5386 /* Restore the symbol table. */
5387 old_ent
= (char *) old_tab
+ tabsize
;
5388 memset (elf_sym_hashes (abfd
), 0,
5389 extsymcount
* sizeof (struct elf_link_hash_entry
*));
5390 htab
->root
.table
.table
= old_table
;
5391 htab
->root
.table
.size
= old_size
;
5392 htab
->root
.table
.count
= old_count
;
5393 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
5394 htab
->root
.undefs
= old_undefs
;
5395 htab
->root
.undefs_tail
= old_undefs_tail
;
5396 if (htab
->dynstr
!= NULL
)
5397 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
5400 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
5402 struct bfd_hash_entry
*p
;
5403 struct elf_link_hash_entry
*h
;
5404 unsigned int non_ir_ref_dynamic
;
5406 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
5408 /* Preserve non_ir_ref_dynamic so that this symbol
5409 will be exported when the dynamic lib becomes needed
5410 in the second pass. */
5411 h
= (struct elf_link_hash_entry
*) p
;
5412 if (h
->root
.type
== bfd_link_hash_warning
)
5413 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5414 non_ir_ref_dynamic
= h
->root
.non_ir_ref_dynamic
;
5416 h
= (struct elf_link_hash_entry
*) p
;
5417 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5418 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5419 if (h
->root
.type
== bfd_link_hash_warning
)
5421 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5422 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5423 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5425 if (h
->root
.type
== bfd_link_hash_common
)
5427 memcpy (h
->root
.u
.c
.p
, old_ent
, sizeof (*h
->root
.u
.c
.p
));
5428 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
5430 h
->root
.non_ir_ref_dynamic
= non_ir_ref_dynamic
;
5434 /* Make a special call to the linker "notice" function to
5435 tell it that symbols added for crefs may need to be removed. */
5436 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
5437 goto error_free_vers
;
5440 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
5442 free (nondeflt_vers
);
5446 if (old_tab
!= NULL
)
5448 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
5449 goto error_free_vers
;
5454 /* Now that all the symbols from this input file are created, if
5455 not performing a relocatable link, handle .symver foo, foo@BAR
5456 such that any relocs against foo become foo@BAR. */
5457 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5461 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5463 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5464 char *shortname
, *p
;
5467 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5469 || (h
->root
.type
!= bfd_link_hash_defined
5470 && h
->root
.type
!= bfd_link_hash_defweak
))
5473 amt
= p
- h
->root
.root
.string
;
5474 shortname
= (char *) bfd_malloc (amt
+ 1);
5476 goto error_free_vers
;
5477 memcpy (shortname
, h
->root
.root
.string
, amt
);
5478 shortname
[amt
] = '\0';
5480 hi
= (struct elf_link_hash_entry
*)
5481 bfd_link_hash_lookup (&htab
->root
, shortname
,
5482 FALSE
, FALSE
, FALSE
);
5484 && hi
->root
.type
== h
->root
.type
5485 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5486 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5488 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
5489 hi
->root
.type
= bfd_link_hash_indirect
;
5490 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5491 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5492 sym_hash
= elf_sym_hashes (abfd
);
5494 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5495 if (sym_hash
[symidx
] == hi
)
5497 sym_hash
[symidx
] = h
;
5503 free (nondeflt_vers
);
5504 nondeflt_vers
= NULL
;
5507 /* Now set the alias field correctly for all the weak defined
5508 symbols we found. The only way to do this is to search all the
5509 symbols. Since we only need the information for non functions in
5510 dynamic objects, that's the only time we actually put anything on
5511 the list WEAKS. We need this information so that if a regular
5512 object refers to a symbol defined weakly in a dynamic object, the
5513 real symbol in the dynamic object is also put in the dynamic
5514 symbols; we also must arrange for both symbols to point to the
5515 same memory location. We could handle the general case of symbol
5516 aliasing, but a general symbol alias can only be generated in
5517 assembler code, handling it correctly would be very time
5518 consuming, and other ELF linkers don't handle general aliasing
5522 struct elf_link_hash_entry
**hpp
;
5523 struct elf_link_hash_entry
**hppend
;
5524 struct elf_link_hash_entry
**sorted_sym_hash
;
5525 struct elf_link_hash_entry
*h
;
5526 size_t sym_count
, amt
;
5528 /* Since we have to search the whole symbol list for each weak
5529 defined symbol, search time for N weak defined symbols will be
5530 O(N^2). Binary search will cut it down to O(NlogN). */
5531 amt
= extsymcount
* sizeof (*sorted_sym_hash
);
5532 sorted_sym_hash
= bfd_malloc (amt
);
5533 if (sorted_sym_hash
== NULL
)
5535 sym_hash
= sorted_sym_hash
;
5536 hpp
= elf_sym_hashes (abfd
);
5537 hppend
= hpp
+ extsymcount
;
5539 for (; hpp
< hppend
; hpp
++)
5543 && h
->root
.type
== bfd_link_hash_defined
5544 && !bed
->is_function_type (h
->type
))
5552 qsort (sorted_sym_hash
, sym_count
, sizeof (*sorted_sym_hash
),
5555 while (weaks
!= NULL
)
5557 struct elf_link_hash_entry
*hlook
;
5560 size_t i
, j
, idx
= 0;
5563 weaks
= hlook
->u
.alias
;
5564 hlook
->u
.alias
= NULL
;
5566 if (hlook
->root
.type
!= bfd_link_hash_defined
5567 && hlook
->root
.type
!= bfd_link_hash_defweak
)
5570 slook
= hlook
->root
.u
.def
.section
;
5571 vlook
= hlook
->root
.u
.def
.value
;
5577 bfd_signed_vma vdiff
;
5579 h
= sorted_sym_hash
[idx
];
5580 vdiff
= vlook
- h
->root
.u
.def
.value
;
5587 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5597 /* We didn't find a value/section match. */
5601 /* With multiple aliases, or when the weak symbol is already
5602 strongly defined, we have multiple matching symbols and
5603 the binary search above may land on any of them. Step
5604 one past the matching symbol(s). */
5607 h
= sorted_sym_hash
[idx
];
5608 if (h
->root
.u
.def
.section
!= slook
5609 || h
->root
.u
.def
.value
!= vlook
)
5613 /* Now look back over the aliases. Since we sorted by size
5614 as well as value and section, we'll choose the one with
5615 the largest size. */
5618 h
= sorted_sym_hash
[idx
];
5620 /* Stop if value or section doesn't match. */
5621 if (h
->root
.u
.def
.section
!= slook
5622 || h
->root
.u
.def
.value
!= vlook
)
5624 else if (h
!= hlook
)
5626 struct elf_link_hash_entry
*t
;
5629 hlook
->is_weakalias
= 1;
5631 if (t
->u
.alias
!= NULL
)
5632 while (t
->u
.alias
!= h
)
5636 /* If the weak definition is in the list of dynamic
5637 symbols, make sure the real definition is put
5639 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5641 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5644 free (sorted_sym_hash
);
5649 /* If the real definition is in the list of dynamic
5650 symbols, make sure the weak definition is put
5651 there as well. If we don't do this, then the
5652 dynamic loader might not merge the entries for the
5653 real definition and the weak definition. */
5654 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5656 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5657 goto err_free_sym_hash
;
5664 free (sorted_sym_hash
);
5667 if (bed
->check_directives
5668 && !(*bed
->check_directives
) (abfd
, info
))
5671 /* If this is a non-traditional link, try to optimize the handling
5672 of the .stab/.stabstr sections. */
5674 && ! info
->traditional_format
5675 && is_elf_hash_table (htab
)
5676 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5680 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5681 if (stabstr
!= NULL
)
5683 bfd_size_type string_offset
= 0;
5686 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5687 if (CONST_STRNEQ (stab
->name
, ".stab")
5688 && (!stab
->name
[5] ||
5689 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5690 && (stab
->flags
& SEC_MERGE
) == 0
5691 && !bfd_is_abs_section (stab
->output_section
))
5693 struct bfd_elf_section_data
*secdata
;
5695 secdata
= elf_section_data (stab
);
5696 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5697 stabstr
, &secdata
->sec_info
,
5700 if (secdata
->sec_info
)
5701 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5706 if (dynamic
&& add_needed
)
5708 /* Add this bfd to the loaded list. */
5709 struct elf_link_loaded_list
*n
;
5711 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5715 n
->next
= htab
->dyn_loaded
;
5716 htab
->dyn_loaded
= n
;
5718 if (dynamic
&& !add_needed
5719 && (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) != 0)
5720 elf_dyn_lib_class (abfd
) |= DYN_NO_NEEDED
;
5727 free (nondeflt_vers
);
5735 /* Return the linker hash table entry of a symbol that might be
5736 satisfied by an archive symbol. Return -1 on error. */
5738 struct elf_link_hash_entry
*
5739 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5740 struct bfd_link_info
*info
,
5743 struct elf_link_hash_entry
*h
;
5747 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, TRUE
);
5751 /* If this is a default version (the name contains @@), look up the
5752 symbol again with only one `@' as well as without the version.
5753 The effect is that references to the symbol with and without the
5754 version will be matched by the default symbol in the archive. */
5756 p
= strchr (name
, ELF_VER_CHR
);
5757 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5760 /* First check with only one `@'. */
5761 len
= strlen (name
);
5762 copy
= (char *) bfd_alloc (abfd
, len
);
5764 return (struct elf_link_hash_entry
*) -1;
5766 first
= p
- name
+ 1;
5767 memcpy (copy
, name
, first
);
5768 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5770 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, TRUE
);
5773 /* We also need to check references to the symbol without the
5775 copy
[first
- 1] = '\0';
5776 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
5777 FALSE
, FALSE
, TRUE
);
5780 bfd_release (abfd
, copy
);
5784 /* Add symbols from an ELF archive file to the linker hash table. We
5785 don't use _bfd_generic_link_add_archive_symbols because we need to
5786 handle versioned symbols.
5788 Fortunately, ELF archive handling is simpler than that done by
5789 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5790 oddities. In ELF, if we find a symbol in the archive map, and the
5791 symbol is currently undefined, we know that we must pull in that
5794 Unfortunately, we do have to make multiple passes over the symbol
5795 table until nothing further is resolved. */
5798 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5801 unsigned char *included
= NULL
;
5805 const struct elf_backend_data
*bed
;
5806 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
5807 (bfd
*, struct bfd_link_info
*, const char *);
5809 if (! bfd_has_map (abfd
))
5811 /* An empty archive is a special case. */
5812 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5814 bfd_set_error (bfd_error_no_armap
);
5818 /* Keep track of all symbols we know to be already defined, and all
5819 files we know to be already included. This is to speed up the
5820 second and subsequent passes. */
5821 c
= bfd_ardata (abfd
)->symdef_count
;
5824 amt
= c
* sizeof (*included
);
5825 included
= (unsigned char *) bfd_zmalloc (amt
);
5826 if (included
== NULL
)
5829 symdefs
= bfd_ardata (abfd
)->symdefs
;
5830 bed
= get_elf_backend_data (abfd
);
5831 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5844 symdefend
= symdef
+ c
;
5845 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5847 struct elf_link_hash_entry
*h
;
5849 struct bfd_link_hash_entry
*undefs_tail
;
5854 if (symdef
->file_offset
== last
)
5860 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5861 if (h
== (struct elf_link_hash_entry
*) -1)
5867 if (h
->root
.type
== bfd_link_hash_undefined
)
5869 /* If the archive element has already been loaded then one
5870 of the symbols defined by that element might have been
5871 made undefined due to being in a discarded section. */
5875 else if (h
->root
.type
== bfd_link_hash_common
)
5877 /* We currently have a common symbol. The archive map contains
5878 a reference to this symbol, so we may want to include it. We
5879 only want to include it however, if this archive element
5880 contains a definition of the symbol, not just another common
5883 Unfortunately some archivers (including GNU ar) will put
5884 declarations of common symbols into their archive maps, as
5885 well as real definitions, so we cannot just go by the archive
5886 map alone. Instead we must read in the element's symbol
5887 table and check that to see what kind of symbol definition
5889 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5894 if (h
->root
.type
!= bfd_link_hash_undefweak
)
5895 /* Symbol must be defined. Don't check it again. */
5900 /* We need to include this archive member. */
5901 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5902 if (element
== NULL
)
5905 if (! bfd_check_format (element
, bfd_object
))
5908 undefs_tail
= info
->hash
->undefs_tail
;
5910 if (!(*info
->callbacks
5911 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5913 if (!bfd_link_add_symbols (element
, info
))
5916 /* If there are any new undefined symbols, we need to make
5917 another pass through the archive in order to see whether
5918 they can be defined. FIXME: This isn't perfect, because
5919 common symbols wind up on undefs_tail and because an
5920 undefined symbol which is defined later on in this pass
5921 does not require another pass. This isn't a bug, but it
5922 does make the code less efficient than it could be. */
5923 if (undefs_tail
!= info
->hash
->undefs_tail
)
5926 /* Look backward to mark all symbols from this object file
5927 which we have already seen in this pass. */
5931 included
[mark
] = TRUE
;
5936 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5938 /* We mark subsequent symbols from this object file as we go
5939 on through the loop. */
5940 last
= symdef
->file_offset
;
5953 /* Given an ELF BFD, add symbols to the global hash table as
5957 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5959 switch (bfd_get_format (abfd
))
5962 return elf_link_add_object_symbols (abfd
, info
);
5964 return elf_link_add_archive_symbols (abfd
, info
);
5966 bfd_set_error (bfd_error_wrong_format
);
5971 struct hash_codes_info
5973 unsigned long *hashcodes
;
5977 /* This function will be called though elf_link_hash_traverse to store
5978 all hash value of the exported symbols in an array. */
5981 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5983 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
5988 /* Ignore indirect symbols. These are added by the versioning code. */
5989 if (h
->dynindx
== -1)
5992 name
= h
->root
.root
.string
;
5993 if (h
->versioned
>= versioned
)
5995 char *p
= strchr (name
, ELF_VER_CHR
);
5998 alc
= (char *) bfd_malloc (p
- name
+ 1);
6004 memcpy (alc
, name
, p
- name
);
6005 alc
[p
- name
] = '\0';
6010 /* Compute the hash value. */
6011 ha
= bfd_elf_hash (name
);
6013 /* Store the found hash value in the array given as the argument. */
6014 *(inf
->hashcodes
)++ = ha
;
6016 /* And store it in the struct so that we can put it in the hash table
6018 h
->u
.elf_hash_value
= ha
;
6024 struct collect_gnu_hash_codes
6027 const struct elf_backend_data
*bed
;
6028 unsigned long int nsyms
;
6029 unsigned long int maskbits
;
6030 unsigned long int *hashcodes
;
6031 unsigned long int *hashval
;
6032 unsigned long int *indx
;
6033 unsigned long int *counts
;
6037 long int min_dynindx
;
6038 unsigned long int bucketcount
;
6039 unsigned long int symindx
;
6040 long int local_indx
;
6041 long int shift1
, shift2
;
6042 unsigned long int mask
;
6046 /* This function will be called though elf_link_hash_traverse to store
6047 all hash value of the exported symbols in an array. */
6050 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6052 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6057 /* Ignore indirect symbols. These are added by the versioning code. */
6058 if (h
->dynindx
== -1)
6061 /* Ignore also local symbols and undefined symbols. */
6062 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6065 name
= h
->root
.root
.string
;
6066 if (h
->versioned
>= versioned
)
6068 char *p
= strchr (name
, ELF_VER_CHR
);
6071 alc
= (char *) bfd_malloc (p
- name
+ 1);
6077 memcpy (alc
, name
, p
- name
);
6078 alc
[p
- name
] = '\0';
6083 /* Compute the hash value. */
6084 ha
= bfd_elf_gnu_hash (name
);
6086 /* Store the found hash value in the array for compute_bucket_count,
6087 and also for .dynsym reordering purposes. */
6088 s
->hashcodes
[s
->nsyms
] = ha
;
6089 s
->hashval
[h
->dynindx
] = ha
;
6091 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
6092 s
->min_dynindx
= h
->dynindx
;
6098 /* This function will be called though elf_link_hash_traverse to do
6099 final dynamic symbol renumbering in case of .gnu.hash.
6100 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6101 to the translation table. */
6104 elf_gnu_hash_process_symidx (struct elf_link_hash_entry
*h
, void *data
)
6106 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6107 unsigned long int bucket
;
6108 unsigned long int val
;
6110 /* Ignore indirect symbols. */
6111 if (h
->dynindx
== -1)
6114 /* Ignore also local symbols and undefined symbols. */
6115 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6117 if (h
->dynindx
>= s
->min_dynindx
)
6119 if (s
->bed
->record_xhash_symbol
!= NULL
)
6121 (*s
->bed
->record_xhash_symbol
) (h
, 0);
6125 h
->dynindx
= s
->local_indx
++;
6130 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
6131 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
6132 & ((s
->maskbits
>> s
->shift1
) - 1);
6133 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
6135 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
6136 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
6137 if (s
->counts
[bucket
] == 1)
6138 /* Last element terminates the chain. */
6140 bfd_put_32 (s
->output_bfd
, val
,
6141 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
6142 --s
->counts
[bucket
];
6143 if (s
->bed
->record_xhash_symbol
!= NULL
)
6145 bfd_vma xlat_loc
= s
->xlat
+ (s
->indx
[bucket
]++ - s
->symindx
) * 4;
6147 (*s
->bed
->record_xhash_symbol
) (h
, xlat_loc
);
6150 h
->dynindx
= s
->indx
[bucket
]++;
6154 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6157 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
6159 return !(h
->forced_local
6160 || h
->root
.type
== bfd_link_hash_undefined
6161 || h
->root
.type
== bfd_link_hash_undefweak
6162 || ((h
->root
.type
== bfd_link_hash_defined
6163 || h
->root
.type
== bfd_link_hash_defweak
)
6164 && h
->root
.u
.def
.section
->output_section
== NULL
));
6167 /* Array used to determine the number of hash table buckets to use
6168 based on the number of symbols there are. If there are fewer than
6169 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6170 fewer than 37 we use 17 buckets, and so forth. We never use more
6171 than 32771 buckets. */
6173 static const size_t elf_buckets
[] =
6175 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6179 /* Compute bucket count for hashing table. We do not use a static set
6180 of possible tables sizes anymore. Instead we determine for all
6181 possible reasonable sizes of the table the outcome (i.e., the
6182 number of collisions etc) and choose the best solution. The
6183 weighting functions are not too simple to allow the table to grow
6184 without bounds. Instead one of the weighting factors is the size.
6185 Therefore the result is always a good payoff between few collisions
6186 (= short chain lengths) and table size. */
6188 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6189 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
6190 unsigned long int nsyms
,
6193 size_t best_size
= 0;
6194 unsigned long int i
;
6196 /* We have a problem here. The following code to optimize the table
6197 size requires an integer type with more the 32 bits. If
6198 BFD_HOST_U_64_BIT is set we know about such a type. */
6199 #ifdef BFD_HOST_U_64_BIT
6204 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
6205 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
6206 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
6207 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
6208 unsigned long int *counts
;
6210 unsigned int no_improvement_count
= 0;
6212 /* Possible optimization parameters: if we have NSYMS symbols we say
6213 that the hashing table must at least have NSYMS/4 and at most
6215 minsize
= nsyms
/ 4;
6218 best_size
= maxsize
= nsyms
* 2;
6223 if ((best_size
& 31) == 0)
6227 /* Create array where we count the collisions in. We must use bfd_malloc
6228 since the size could be large. */
6230 amt
*= sizeof (unsigned long int);
6231 counts
= (unsigned long int *) bfd_malloc (amt
);
6235 /* Compute the "optimal" size for the hash table. The criteria is a
6236 minimal chain length. The minor criteria is (of course) the size
6238 for (i
= minsize
; i
< maxsize
; ++i
)
6240 /* Walk through the array of hashcodes and count the collisions. */
6241 BFD_HOST_U_64_BIT max
;
6242 unsigned long int j
;
6243 unsigned long int fact
;
6245 if (gnu_hash
&& (i
& 31) == 0)
6248 memset (counts
, '\0', i
* sizeof (unsigned long int));
6250 /* Determine how often each hash bucket is used. */
6251 for (j
= 0; j
< nsyms
; ++j
)
6252 ++counts
[hashcodes
[j
] % i
];
6254 /* For the weight function we need some information about the
6255 pagesize on the target. This is information need not be 100%
6256 accurate. Since this information is not available (so far) we
6257 define it here to a reasonable default value. If it is crucial
6258 to have a better value some day simply define this value. */
6259 # ifndef BFD_TARGET_PAGESIZE
6260 # define BFD_TARGET_PAGESIZE (4096)
6263 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6265 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
6268 /* Variant 1: optimize for short chains. We add the squares
6269 of all the chain lengths (which favors many small chain
6270 over a few long chains). */
6271 for (j
= 0; j
< i
; ++j
)
6272 max
+= counts
[j
] * counts
[j
];
6274 /* This adds penalties for the overall size of the table. */
6275 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6278 /* Variant 2: Optimize a lot more for small table. Here we
6279 also add squares of the size but we also add penalties for
6280 empty slots (the +1 term). */
6281 for (j
= 0; j
< i
; ++j
)
6282 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
6284 /* The overall size of the table is considered, but not as
6285 strong as in variant 1, where it is squared. */
6286 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6290 /* Compare with current best results. */
6291 if (max
< best_chlen
)
6295 no_improvement_count
= 0;
6297 /* PR 11843: Avoid futile long searches for the best bucket size
6298 when there are a large number of symbols. */
6299 else if (++no_improvement_count
== 100)
6306 #endif /* defined (BFD_HOST_U_64_BIT) */
6308 /* This is the fallback solution if no 64bit type is available or if we
6309 are not supposed to spend much time on optimizations. We select the
6310 bucket count using a fixed set of numbers. */
6311 for (i
= 0; elf_buckets
[i
] != 0; i
++)
6313 best_size
= elf_buckets
[i
];
6314 if (nsyms
< elf_buckets
[i
+ 1])
6317 if (gnu_hash
&& best_size
< 2)
6324 /* Size any SHT_GROUP section for ld -r. */
6327 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
6332 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6333 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6334 && (s
= ibfd
->sections
) != NULL
6335 && s
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
6336 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
6341 /* Set a default stack segment size. The value in INFO wins. If it
6342 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6343 undefined it is initialized. */
6346 bfd_elf_stack_segment_size (bfd
*output_bfd
,
6347 struct bfd_link_info
*info
,
6348 const char *legacy_symbol
,
6349 bfd_vma default_size
)
6351 struct elf_link_hash_entry
*h
= NULL
;
6353 /* Look for legacy symbol. */
6355 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
6356 FALSE
, FALSE
, FALSE
);
6357 if (h
&& (h
->root
.type
== bfd_link_hash_defined
6358 || h
->root
.type
== bfd_link_hash_defweak
)
6360 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
6362 /* The symbol has no type if specified on the command line. */
6363 h
->type
= STT_OBJECT
;
6364 if (info
->stacksize
)
6365 /* xgettext:c-format */
6366 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6367 output_bfd
, legacy_symbol
);
6368 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
6369 /* xgettext:c-format */
6370 _bfd_error_handler (_("%pB: %s not absolute"),
6371 output_bfd
, legacy_symbol
);
6373 info
->stacksize
= h
->root
.u
.def
.value
;
6376 if (!info
->stacksize
)
6377 /* If the user didn't set a size, or explicitly inhibit the
6378 size, set it now. */
6379 info
->stacksize
= default_size
;
6381 /* Provide the legacy symbol, if it is referenced. */
6382 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
6383 || h
->root
.type
== bfd_link_hash_undefweak
))
6385 struct bfd_link_hash_entry
*bh
= NULL
;
6387 if (!(_bfd_generic_link_add_one_symbol
6388 (info
, output_bfd
, legacy_symbol
,
6389 BSF_GLOBAL
, bfd_abs_section_ptr
,
6390 info
->stacksize
>= 0 ? info
->stacksize
: 0,
6391 NULL
, FALSE
, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
6394 h
= (struct elf_link_hash_entry
*) bh
;
6396 h
->type
= STT_OBJECT
;
6402 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6404 struct elf_gc_sweep_symbol_info
6406 struct bfd_link_info
*info
;
6407 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
6412 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
6415 && (((h
->root
.type
== bfd_link_hash_defined
6416 || h
->root
.type
== bfd_link_hash_defweak
)
6417 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
6418 && h
->root
.u
.def
.section
->gc_mark
))
6419 || h
->root
.type
== bfd_link_hash_undefined
6420 || h
->root
.type
== bfd_link_hash_undefweak
))
6422 struct elf_gc_sweep_symbol_info
*inf
;
6424 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
6425 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
6428 h
->ref_regular_nonweak
= 0;
6434 /* Set up the sizes and contents of the ELF dynamic sections. This is
6435 called by the ELF linker emulation before_allocation routine. We
6436 must set the sizes of the sections before the linker sets the
6437 addresses of the various sections. */
6440 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
6443 const char *filter_shlib
,
6445 const char *depaudit
,
6446 const char * const *auxiliary_filters
,
6447 struct bfd_link_info
*info
,
6448 asection
**sinterpptr
)
6451 const struct elf_backend_data
*bed
;
6455 if (!is_elf_hash_table (info
->hash
))
6458 dynobj
= elf_hash_table (info
)->dynobj
;
6460 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6462 struct bfd_elf_version_tree
*verdefs
;
6463 struct elf_info_failed asvinfo
;
6464 struct bfd_elf_version_tree
*t
;
6465 struct bfd_elf_version_expr
*d
;
6469 /* If we are supposed to export all symbols into the dynamic symbol
6470 table (this is not the normal case), then do so. */
6471 if (info
->export_dynamic
6472 || (bfd_link_executable (info
) && info
->dynamic
))
6474 struct elf_info_failed eif
;
6478 elf_link_hash_traverse (elf_hash_table (info
),
6479 _bfd_elf_export_symbol
,
6487 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6489 if (soname_indx
== (size_t) -1
6490 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6494 soname_indx
= (size_t) -1;
6496 /* Make all global versions with definition. */
6497 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6498 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6499 if (!d
->symver
&& d
->literal
)
6501 const char *verstr
, *name
;
6502 size_t namelen
, verlen
, newlen
;
6503 char *newname
, *p
, leading_char
;
6504 struct elf_link_hash_entry
*newh
;
6506 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6508 namelen
= strlen (name
) + (leading_char
!= '\0');
6510 verlen
= strlen (verstr
);
6511 newlen
= namelen
+ verlen
+ 3;
6513 newname
= (char *) bfd_malloc (newlen
);
6514 if (newname
== NULL
)
6516 newname
[0] = leading_char
;
6517 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6519 /* Check the hidden versioned definition. */
6520 p
= newname
+ namelen
;
6522 memcpy (p
, verstr
, verlen
+ 1);
6523 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6524 newname
, FALSE
, FALSE
,
6527 || (newh
->root
.type
!= bfd_link_hash_defined
6528 && newh
->root
.type
!= bfd_link_hash_defweak
))
6530 /* Check the default versioned definition. */
6532 memcpy (p
, verstr
, verlen
+ 1);
6533 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6534 newname
, FALSE
, FALSE
,
6539 /* Mark this version if there is a definition and it is
6540 not defined in a shared object. */
6542 && !newh
->def_dynamic
6543 && (newh
->root
.type
== bfd_link_hash_defined
6544 || newh
->root
.type
== bfd_link_hash_defweak
))
6548 /* Attach all the symbols to their version information. */
6549 asvinfo
.info
= info
;
6550 asvinfo
.failed
= FALSE
;
6552 elf_link_hash_traverse (elf_hash_table (info
),
6553 _bfd_elf_link_assign_sym_version
,
6558 if (!info
->allow_undefined_version
)
6560 /* Check if all global versions have a definition. */
6561 bfd_boolean all_defined
= TRUE
;
6562 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6563 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6564 if (d
->literal
&& !d
->symver
&& !d
->script
)
6567 (_("%s: undefined version: %s"),
6568 d
->pattern
, t
->name
);
6569 all_defined
= FALSE
;
6574 bfd_set_error (bfd_error_bad_value
);
6579 /* Set up the version definition section. */
6580 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6581 BFD_ASSERT (s
!= NULL
);
6583 /* We may have created additional version definitions if we are
6584 just linking a regular application. */
6585 verdefs
= info
->version_info
;
6587 /* Skip anonymous version tag. */
6588 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6589 verdefs
= verdefs
->next
;
6591 if (verdefs
== NULL
&& !info
->create_default_symver
)
6592 s
->flags
|= SEC_EXCLUDE
;
6598 Elf_Internal_Verdef def
;
6599 Elf_Internal_Verdaux defaux
;
6600 struct bfd_link_hash_entry
*bh
;
6601 struct elf_link_hash_entry
*h
;
6607 /* Make space for the base version. */
6608 size
+= sizeof (Elf_External_Verdef
);
6609 size
+= sizeof (Elf_External_Verdaux
);
6612 /* Make space for the default version. */
6613 if (info
->create_default_symver
)
6615 size
+= sizeof (Elf_External_Verdef
);
6619 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6621 struct bfd_elf_version_deps
*n
;
6623 /* Don't emit base version twice. */
6627 size
+= sizeof (Elf_External_Verdef
);
6628 size
+= sizeof (Elf_External_Verdaux
);
6631 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6632 size
+= sizeof (Elf_External_Verdaux
);
6636 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6637 if (s
->contents
== NULL
&& s
->size
!= 0)
6640 /* Fill in the version definition section. */
6644 def
.vd_version
= VER_DEF_CURRENT
;
6645 def
.vd_flags
= VER_FLG_BASE
;
6648 if (info
->create_default_symver
)
6650 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6651 def
.vd_next
= sizeof (Elf_External_Verdef
);
6655 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6656 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6657 + sizeof (Elf_External_Verdaux
));
6660 if (soname_indx
!= (size_t) -1)
6662 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6664 def
.vd_hash
= bfd_elf_hash (soname
);
6665 defaux
.vda_name
= soname_indx
;
6672 name
= lbasename (bfd_get_filename (output_bfd
));
6673 def
.vd_hash
= bfd_elf_hash (name
);
6674 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6676 if (indx
== (size_t) -1)
6678 defaux
.vda_name
= indx
;
6680 defaux
.vda_next
= 0;
6682 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6683 (Elf_External_Verdef
*) p
);
6684 p
+= sizeof (Elf_External_Verdef
);
6685 if (info
->create_default_symver
)
6687 /* Add a symbol representing this version. */
6689 if (! (_bfd_generic_link_add_one_symbol
6690 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6692 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6694 h
= (struct elf_link_hash_entry
*) bh
;
6697 h
->type
= STT_OBJECT
;
6698 h
->verinfo
.vertree
= NULL
;
6700 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6703 /* Create a duplicate of the base version with the same
6704 aux block, but different flags. */
6707 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6709 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6710 + sizeof (Elf_External_Verdaux
));
6713 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6714 (Elf_External_Verdef
*) p
);
6715 p
+= sizeof (Elf_External_Verdef
);
6717 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6718 (Elf_External_Verdaux
*) p
);
6719 p
+= sizeof (Elf_External_Verdaux
);
6721 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6724 struct bfd_elf_version_deps
*n
;
6726 /* Don't emit the base version twice. */
6731 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6734 /* Add a symbol representing this version. */
6736 if (! (_bfd_generic_link_add_one_symbol
6737 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6739 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6741 h
= (struct elf_link_hash_entry
*) bh
;
6744 h
->type
= STT_OBJECT
;
6745 h
->verinfo
.vertree
= t
;
6747 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6750 def
.vd_version
= VER_DEF_CURRENT
;
6752 if (t
->globals
.list
== NULL
6753 && t
->locals
.list
== NULL
6755 def
.vd_flags
|= VER_FLG_WEAK
;
6756 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6757 def
.vd_cnt
= cdeps
+ 1;
6758 def
.vd_hash
= bfd_elf_hash (t
->name
);
6759 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6762 /* If a basever node is next, it *must* be the last node in
6763 the chain, otherwise Verdef construction breaks. */
6764 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6765 BFD_ASSERT (t
->next
->next
== NULL
);
6767 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6768 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6769 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6771 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6772 (Elf_External_Verdef
*) p
);
6773 p
+= sizeof (Elf_External_Verdef
);
6775 defaux
.vda_name
= h
->dynstr_index
;
6776 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6778 defaux
.vda_next
= 0;
6779 if (t
->deps
!= NULL
)
6780 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6781 t
->name_indx
= defaux
.vda_name
;
6783 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6784 (Elf_External_Verdaux
*) p
);
6785 p
+= sizeof (Elf_External_Verdaux
);
6787 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6789 if (n
->version_needed
== NULL
)
6791 /* This can happen if there was an error in the
6793 defaux
.vda_name
= 0;
6797 defaux
.vda_name
= n
->version_needed
->name_indx
;
6798 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6801 if (n
->next
== NULL
)
6802 defaux
.vda_next
= 0;
6804 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6806 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6807 (Elf_External_Verdaux
*) p
);
6808 p
+= sizeof (Elf_External_Verdaux
);
6812 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6816 bed
= get_elf_backend_data (output_bfd
);
6818 if (info
->gc_sections
&& bed
->can_gc_sections
)
6820 struct elf_gc_sweep_symbol_info sweep_info
;
6822 /* Remove the symbols that were in the swept sections from the
6823 dynamic symbol table. */
6824 sweep_info
.info
= info
;
6825 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
6826 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
6830 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6833 struct elf_find_verdep_info sinfo
;
6835 /* Work out the size of the version reference section. */
6837 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6838 BFD_ASSERT (s
!= NULL
);
6841 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6842 if (sinfo
.vers
== 0)
6844 sinfo
.failed
= FALSE
;
6846 elf_link_hash_traverse (elf_hash_table (info
),
6847 _bfd_elf_link_find_version_dependencies
,
6852 if (elf_tdata (output_bfd
)->verref
== NULL
)
6853 s
->flags
|= SEC_EXCLUDE
;
6856 Elf_Internal_Verneed
*vn
;
6861 /* Build the version dependency section. */
6864 for (vn
= elf_tdata (output_bfd
)->verref
;
6866 vn
= vn
->vn_nextref
)
6868 Elf_Internal_Vernaux
*a
;
6870 size
+= sizeof (Elf_External_Verneed
);
6872 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6873 size
+= sizeof (Elf_External_Vernaux
);
6877 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6878 if (s
->contents
== NULL
)
6882 for (vn
= elf_tdata (output_bfd
)->verref
;
6884 vn
= vn
->vn_nextref
)
6887 Elf_Internal_Vernaux
*a
;
6891 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6894 vn
->vn_version
= VER_NEED_CURRENT
;
6896 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6897 elf_dt_name (vn
->vn_bfd
) != NULL
6898 ? elf_dt_name (vn
->vn_bfd
)
6899 : lbasename (bfd_get_filename
6902 if (indx
== (size_t) -1)
6905 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
6906 if (vn
->vn_nextref
== NULL
)
6909 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
6910 + caux
* sizeof (Elf_External_Vernaux
));
6912 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
6913 (Elf_External_Verneed
*) p
);
6914 p
+= sizeof (Elf_External_Verneed
);
6916 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6918 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6919 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6920 a
->vna_nodename
, FALSE
);
6921 if (indx
== (size_t) -1)
6924 if (a
->vna_nextptr
== NULL
)
6927 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6929 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6930 (Elf_External_Vernaux
*) p
);
6931 p
+= sizeof (Elf_External_Vernaux
);
6935 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6939 /* Any syms created from now on start with -1 in
6940 got.refcount/offset and plt.refcount/offset. */
6941 elf_hash_table (info
)->init_got_refcount
6942 = elf_hash_table (info
)->init_got_offset
;
6943 elf_hash_table (info
)->init_plt_refcount
6944 = elf_hash_table (info
)->init_plt_offset
;
6946 if (bfd_link_relocatable (info
)
6947 && !_bfd_elf_size_group_sections (info
))
6950 /* The backend may have to create some sections regardless of whether
6951 we're dynamic or not. */
6952 if (bed
->elf_backend_always_size_sections
6953 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
6956 /* Determine any GNU_STACK segment requirements, after the backend
6957 has had a chance to set a default segment size. */
6958 if (info
->execstack
)
6959 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
6960 else if (info
->noexecstack
)
6961 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
6965 asection
*notesec
= NULL
;
6968 for (inputobj
= info
->input_bfds
;
6970 inputobj
= inputobj
->link
.next
)
6975 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
6977 s
= inputobj
->sections
;
6978 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
6981 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
6984 if (s
->flags
& SEC_CODE
)
6988 else if (bed
->default_execstack
)
6991 if (notesec
|| info
->stacksize
> 0)
6992 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
6993 if (notesec
&& exec
&& bfd_link_relocatable (info
)
6994 && notesec
->output_section
!= bfd_abs_section_ptr
)
6995 notesec
->output_section
->flags
|= SEC_CODE
;
6998 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7000 struct elf_info_failed eif
;
7001 struct elf_link_hash_entry
*h
;
7005 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
7006 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
7010 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
7012 info
->flags
|= DF_SYMBOLIC
;
7020 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
7022 if (indx
== (size_t) -1)
7025 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
7026 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
7030 if (filter_shlib
!= NULL
)
7034 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7035 filter_shlib
, TRUE
);
7036 if (indx
== (size_t) -1
7037 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
7041 if (auxiliary_filters
!= NULL
)
7043 const char * const *p
;
7045 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
7049 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7051 if (indx
== (size_t) -1
7052 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
7061 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
7063 if (indx
== (size_t) -1
7064 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
7068 if (depaudit
!= NULL
)
7072 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
7074 if (indx
== (size_t) -1
7075 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
7082 /* Find all symbols which were defined in a dynamic object and make
7083 the backend pick a reasonable value for them. */
7084 elf_link_hash_traverse (elf_hash_table (info
),
7085 _bfd_elf_adjust_dynamic_symbol
,
7090 /* Add some entries to the .dynamic section. We fill in some of the
7091 values later, in bfd_elf_final_link, but we must add the entries
7092 now so that we know the final size of the .dynamic section. */
7094 /* If there are initialization and/or finalization functions to
7095 call then add the corresponding DT_INIT/DT_FINI entries. */
7096 h
= (info
->init_function
7097 ? elf_link_hash_lookup (elf_hash_table (info
),
7098 info
->init_function
, FALSE
,
7105 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
7108 h
= (info
->fini_function
7109 ? elf_link_hash_lookup (elf_hash_table (info
),
7110 info
->fini_function
, FALSE
,
7117 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
7121 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
7122 if (s
!= NULL
&& s
->linker_has_input
)
7124 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7125 if (! bfd_link_executable (info
))
7130 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
7131 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
7132 && (o
= sub
->sections
) != NULL
7133 && o
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
)
7134 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7135 if (elf_section_data (o
)->this_hdr
.sh_type
7136 == SHT_PREINIT_ARRAY
)
7139 (_("%pB: .preinit_array section is not allowed in DSO"),
7144 bfd_set_error (bfd_error_nonrepresentable_section
);
7148 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
7149 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
7152 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
7153 if (s
!= NULL
&& s
->linker_has_input
)
7155 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
7156 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
7159 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
7160 if (s
!= NULL
&& s
->linker_has_input
)
7162 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
7163 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
7167 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
7168 /* If .dynstr is excluded from the link, we don't want any of
7169 these tags. Strictly, we should be checking each section
7170 individually; This quick check covers for the case where
7171 someone does a /DISCARD/ : { *(*) }. */
7172 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
7174 bfd_size_type strsize
;
7176 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7177 if ((info
->emit_hash
7178 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
7179 || (info
->emit_gnu_hash
7180 && (bed
->record_xhash_symbol
== NULL
7181 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0)))
7182 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
7183 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
7184 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
7185 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
7187 || (info
->gnu_flags_1
7188 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_FLAGS_1
,
7189 info
->gnu_flags_1
)))
7194 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
7197 /* The backend must work out the sizes of all the other dynamic
7200 && bed
->elf_backend_size_dynamic_sections
!= NULL
7201 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
7204 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7206 if (elf_tdata (output_bfd
)->cverdefs
)
7208 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
7210 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
7211 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
7215 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
7217 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
7220 else if (info
->flags
& DF_BIND_NOW
)
7222 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
7228 if (bfd_link_executable (info
))
7229 info
->flags_1
&= ~ (DF_1_INITFIRST
7232 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
7236 if (elf_tdata (output_bfd
)->cverrefs
)
7238 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
7240 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
7241 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
7245 if ((elf_tdata (output_bfd
)->cverrefs
== 0
7246 && elf_tdata (output_bfd
)->cverdefs
== 0)
7247 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, NULL
) <= 1)
7251 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7252 s
->flags
|= SEC_EXCLUDE
;
7258 /* Find the first non-excluded output section. We'll use its
7259 section symbol for some emitted relocs. */
7261 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
7264 asection
*found
= NULL
;
7266 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7267 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7268 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7271 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7274 elf_hash_table (info
)->text_index_section
= found
;
7277 /* Find two non-excluded output sections, one for code, one for data.
7278 We'll use their section symbols for some emitted relocs. */
7280 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
7283 asection
*found
= NULL
;
7285 /* Data first, since setting text_index_section changes
7286 _bfd_elf_omit_section_dynsym_default. */
7287 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7288 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7289 && !(s
->flags
& SEC_READONLY
)
7290 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7293 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7296 elf_hash_table (info
)->data_index_section
= found
;
7298 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7299 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7300 && (s
->flags
& SEC_READONLY
)
7301 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7306 elf_hash_table (info
)->text_index_section
= found
;
7309 #define GNU_HASH_SECTION_NAME(bed) \
7310 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7313 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
7315 const struct elf_backend_data
*bed
;
7316 unsigned long section_sym_count
;
7317 bfd_size_type dynsymcount
= 0;
7319 if (!is_elf_hash_table (info
->hash
))
7322 bed
= get_elf_backend_data (output_bfd
);
7323 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
7325 /* Assign dynsym indices. In a shared library we generate a section
7326 symbol for each output section, which come first. Next come all
7327 of the back-end allocated local dynamic syms, followed by the rest
7328 of the global symbols.
7330 This is usually not needed for static binaries, however backends
7331 can request to always do it, e.g. the MIPS backend uses dynamic
7332 symbol counts to lay out GOT, which will be produced in the
7333 presence of GOT relocations even in static binaries (holding fixed
7334 data in that case, to satisfy those relocations). */
7336 if (elf_hash_table (info
)->dynamic_sections_created
7337 || bed
->always_renumber_dynsyms
)
7338 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
7339 §ion_sym_count
);
7341 if (elf_hash_table (info
)->dynamic_sections_created
)
7345 unsigned int dtagcount
;
7347 dynobj
= elf_hash_table (info
)->dynobj
;
7349 /* Work out the size of the symbol version section. */
7350 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7351 BFD_ASSERT (s
!= NULL
);
7352 if ((s
->flags
& SEC_EXCLUDE
) == 0)
7354 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
7355 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7356 if (s
->contents
== NULL
)
7359 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
7363 /* Set the size of the .dynsym and .hash sections. We counted
7364 the number of dynamic symbols in elf_link_add_object_symbols.
7365 We will build the contents of .dynsym and .hash when we build
7366 the final symbol table, because until then we do not know the
7367 correct value to give the symbols. We built the .dynstr
7368 section as we went along in elf_link_add_object_symbols. */
7369 s
= elf_hash_table (info
)->dynsym
;
7370 BFD_ASSERT (s
!= NULL
);
7371 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
7373 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7374 if (s
->contents
== NULL
)
7377 /* The first entry in .dynsym is a dummy symbol. Clear all the
7378 section syms, in case we don't output them all. */
7379 ++section_sym_count
;
7380 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
7382 elf_hash_table (info
)->bucketcount
= 0;
7384 /* Compute the size of the hashing table. As a side effect this
7385 computes the hash values for all the names we export. */
7386 if (info
->emit_hash
)
7388 unsigned long int *hashcodes
;
7389 struct hash_codes_info hashinf
;
7391 unsigned long int nsyms
;
7393 size_t hash_entry_size
;
7395 /* Compute the hash values for all exported symbols. At the same
7396 time store the values in an array so that we could use them for
7398 amt
= dynsymcount
* sizeof (unsigned long int);
7399 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
7400 if (hashcodes
== NULL
)
7402 hashinf
.hashcodes
= hashcodes
;
7403 hashinf
.error
= FALSE
;
7405 /* Put all hash values in HASHCODES. */
7406 elf_link_hash_traverse (elf_hash_table (info
),
7407 elf_collect_hash_codes
, &hashinf
);
7414 nsyms
= hashinf
.hashcodes
- hashcodes
;
7416 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
7419 if (bucketcount
== 0 && nsyms
> 0)
7422 elf_hash_table (info
)->bucketcount
= bucketcount
;
7424 s
= bfd_get_linker_section (dynobj
, ".hash");
7425 BFD_ASSERT (s
!= NULL
);
7426 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
7427 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
7428 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7429 if (s
->contents
== NULL
)
7432 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
7433 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
7434 s
->contents
+ hash_entry_size
);
7437 if (info
->emit_gnu_hash
)
7440 unsigned char *contents
;
7441 struct collect_gnu_hash_codes cinfo
;
7445 memset (&cinfo
, 0, sizeof (cinfo
));
7447 /* Compute the hash values for all exported symbols. At the same
7448 time store the values in an array so that we could use them for
7450 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
7451 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
7452 if (cinfo
.hashcodes
== NULL
)
7455 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
7456 cinfo
.min_dynindx
= -1;
7457 cinfo
.output_bfd
= output_bfd
;
7460 /* Put all hash values in HASHCODES. */
7461 elf_link_hash_traverse (elf_hash_table (info
),
7462 elf_collect_gnu_hash_codes
, &cinfo
);
7465 free (cinfo
.hashcodes
);
7470 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
7472 if (bucketcount
== 0)
7474 free (cinfo
.hashcodes
);
7478 s
= bfd_get_linker_section (dynobj
, GNU_HASH_SECTION_NAME (bed
));
7479 BFD_ASSERT (s
!= NULL
);
7481 if (cinfo
.nsyms
== 0)
7483 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7484 BFD_ASSERT (cinfo
.min_dynindx
== -1);
7485 free (cinfo
.hashcodes
);
7486 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
7487 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7488 if (contents
== NULL
)
7490 s
->contents
= contents
;
7491 /* 1 empty bucket. */
7492 bfd_put_32 (output_bfd
, 1, contents
);
7493 /* SYMIDX above the special symbol 0. */
7494 bfd_put_32 (output_bfd
, 1, contents
+ 4);
7495 /* Just one word for bitmask. */
7496 bfd_put_32 (output_bfd
, 1, contents
+ 8);
7497 /* Only hash fn bloom filter. */
7498 bfd_put_32 (output_bfd
, 0, contents
+ 12);
7499 /* No hashes are valid - empty bitmask. */
7500 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
7501 /* No hashes in the only bucket. */
7502 bfd_put_32 (output_bfd
, 0,
7503 contents
+ 16 + bed
->s
->arch_size
/ 8);
7507 unsigned long int maskwords
, maskbitslog2
, x
;
7508 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
7512 while ((x
>>= 1) != 0)
7514 if (maskbitslog2
< 3)
7516 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
7517 maskbitslog2
= maskbitslog2
+ 3;
7519 maskbitslog2
= maskbitslog2
+ 2;
7520 if (bed
->s
->arch_size
== 64)
7522 if (maskbitslog2
== 5)
7528 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
7529 cinfo
.shift2
= maskbitslog2
;
7530 cinfo
.maskbits
= 1 << maskbitslog2
;
7531 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
7532 amt
= bucketcount
* sizeof (unsigned long int) * 2;
7533 amt
+= maskwords
* sizeof (bfd_vma
);
7534 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
7535 if (cinfo
.bitmask
== NULL
)
7537 free (cinfo
.hashcodes
);
7541 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
7542 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
7543 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
7544 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
7546 /* Determine how often each hash bucket is used. */
7547 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
7548 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
7549 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
7551 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
7552 if (cinfo
.counts
[i
] != 0)
7554 cinfo
.indx
[i
] = cnt
;
7555 cnt
+= cinfo
.counts
[i
];
7557 BFD_ASSERT (cnt
== dynsymcount
);
7558 cinfo
.bucketcount
= bucketcount
;
7559 cinfo
.local_indx
= cinfo
.min_dynindx
;
7561 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
7562 s
->size
+= cinfo
.maskbits
/ 8;
7563 if (bed
->record_xhash_symbol
!= NULL
)
7564 s
->size
+= cinfo
.nsyms
* 4;
7565 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7566 if (contents
== NULL
)
7568 free (cinfo
.bitmask
);
7569 free (cinfo
.hashcodes
);
7573 s
->contents
= contents
;
7574 bfd_put_32 (output_bfd
, bucketcount
, contents
);
7575 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
7576 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
7577 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
7578 contents
+= 16 + cinfo
.maskbits
/ 8;
7580 for (i
= 0; i
< bucketcount
; ++i
)
7582 if (cinfo
.counts
[i
] == 0)
7583 bfd_put_32 (output_bfd
, 0, contents
);
7585 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
7589 cinfo
.contents
= contents
;
7591 cinfo
.xlat
= contents
+ cinfo
.nsyms
* 4 - s
->contents
;
7592 /* Renumber dynamic symbols, if populating .gnu.hash section.
7593 If using .MIPS.xhash, populate the translation table. */
7594 elf_link_hash_traverse (elf_hash_table (info
),
7595 elf_gnu_hash_process_symidx
, &cinfo
);
7597 contents
= s
->contents
+ 16;
7598 for (i
= 0; i
< maskwords
; ++i
)
7600 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
7602 contents
+= bed
->s
->arch_size
/ 8;
7605 free (cinfo
.bitmask
);
7606 free (cinfo
.hashcodes
);
7610 s
= bfd_get_linker_section (dynobj
, ".dynstr");
7611 BFD_ASSERT (s
!= NULL
);
7613 elf_finalize_dynstr (output_bfd
, info
);
7615 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7617 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
7618 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
7625 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7628 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
7631 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
7632 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
7635 /* Finish SHF_MERGE section merging. */
7638 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
7643 if (!is_elf_hash_table (info
->hash
))
7646 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
7647 if ((ibfd
->flags
& DYNAMIC
) == 0
7648 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
7649 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
7650 == get_elf_backend_data (obfd
)->s
->elfclass
))
7651 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7652 if ((sec
->flags
& SEC_MERGE
) != 0
7653 && !bfd_is_abs_section (sec
->output_section
))
7655 struct bfd_elf_section_data
*secdata
;
7657 secdata
= elf_section_data (sec
);
7658 if (! _bfd_add_merge_section (obfd
,
7659 &elf_hash_table (info
)->merge_info
,
7660 sec
, &secdata
->sec_info
))
7662 else if (secdata
->sec_info
)
7663 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7666 if (elf_hash_table (info
)->merge_info
!= NULL
)
7667 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7668 merge_sections_remove_hook
);
7672 /* Create an entry in an ELF linker hash table. */
7674 struct bfd_hash_entry
*
7675 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7676 struct bfd_hash_table
*table
,
7679 /* Allocate the structure if it has not already been allocated by a
7683 entry
= (struct bfd_hash_entry
*)
7684 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7689 /* Call the allocation method of the superclass. */
7690 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7693 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7694 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7696 /* Set local fields. */
7699 ret
->got
= htab
->init_got_refcount
;
7700 ret
->plt
= htab
->init_plt_refcount
;
7701 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7702 - offsetof (struct elf_link_hash_entry
, size
)));
7703 /* Assume that we have been called by a non-ELF symbol reader.
7704 This flag is then reset by the code which reads an ELF input
7705 file. This ensures that a symbol created by a non-ELF symbol
7706 reader will have the flag set correctly. */
7713 /* Copy data from an indirect symbol to its direct symbol, hiding the
7714 old indirect symbol. Also used for copying flags to a weakdef. */
7717 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7718 struct elf_link_hash_entry
*dir
,
7719 struct elf_link_hash_entry
*ind
)
7721 struct elf_link_hash_table
*htab
;
7723 if (ind
->dyn_relocs
!= NULL
)
7725 if (dir
->dyn_relocs
!= NULL
)
7727 struct elf_dyn_relocs
**pp
;
7728 struct elf_dyn_relocs
*p
;
7730 /* Add reloc counts against the indirect sym to the direct sym
7731 list. Merge any entries against the same section. */
7732 for (pp
= &ind
->dyn_relocs
; (p
= *pp
) != NULL
; )
7734 struct elf_dyn_relocs
*q
;
7736 for (q
= dir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
7737 if (q
->sec
== p
->sec
)
7739 q
->pc_count
+= p
->pc_count
;
7740 q
->count
+= p
->count
;
7747 *pp
= dir
->dyn_relocs
;
7750 dir
->dyn_relocs
= ind
->dyn_relocs
;
7751 ind
->dyn_relocs
= NULL
;
7754 /* Copy down any references that we may have already seen to the
7755 symbol which just became indirect. */
7757 if (dir
->versioned
!= versioned_hidden
)
7758 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7759 dir
->ref_regular
|= ind
->ref_regular
;
7760 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7761 dir
->non_got_ref
|= ind
->non_got_ref
;
7762 dir
->needs_plt
|= ind
->needs_plt
;
7763 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7765 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7768 /* Copy over the global and procedure linkage table refcount entries.
7769 These may have been already set up by a check_relocs routine. */
7770 htab
= elf_hash_table (info
);
7771 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7773 if (dir
->got
.refcount
< 0)
7774 dir
->got
.refcount
= 0;
7775 dir
->got
.refcount
+= ind
->got
.refcount
;
7776 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7779 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7781 if (dir
->plt
.refcount
< 0)
7782 dir
->plt
.refcount
= 0;
7783 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7784 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7787 if (ind
->dynindx
!= -1)
7789 if (dir
->dynindx
!= -1)
7790 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7791 dir
->dynindx
= ind
->dynindx
;
7792 dir
->dynstr_index
= ind
->dynstr_index
;
7794 ind
->dynstr_index
= 0;
7799 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7800 struct elf_link_hash_entry
*h
,
7801 bfd_boolean force_local
)
7803 /* STT_GNU_IFUNC symbol must go through PLT. */
7804 if (h
->type
!= STT_GNU_IFUNC
)
7806 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7811 h
->forced_local
= 1;
7812 if (h
->dynindx
!= -1)
7814 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7817 h
->dynstr_index
= 0;
7822 /* Hide a symbol. */
7825 _bfd_elf_link_hide_symbol (bfd
*output_bfd
,
7826 struct bfd_link_info
*info
,
7827 struct bfd_link_hash_entry
*h
)
7829 if (is_elf_hash_table (info
->hash
))
7831 const struct elf_backend_data
*bed
7832 = get_elf_backend_data (output_bfd
);
7833 struct elf_link_hash_entry
*eh
7834 = (struct elf_link_hash_entry
*) h
;
7835 bed
->elf_backend_hide_symbol (info
, eh
, TRUE
);
7836 eh
->def_dynamic
= 0;
7837 eh
->ref_dynamic
= 0;
7838 eh
->dynamic_def
= 0;
7842 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7846 _bfd_elf_link_hash_table_init
7847 (struct elf_link_hash_table
*table
,
7849 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
7850 struct bfd_hash_table
*,
7852 unsigned int entsize
,
7853 enum elf_target_id target_id
)
7856 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
7858 table
->init_got_refcount
.refcount
= can_refcount
- 1;
7859 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
7860 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
7861 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
7862 /* The first dynamic symbol is a dummy. */
7863 table
->dynsymcount
= 1;
7865 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
7867 table
->root
.type
= bfd_link_elf_hash_table
;
7868 table
->hash_table_id
= target_id
;
7869 table
->target_os
= get_elf_backend_data (abfd
)->target_os
;
7874 /* Create an ELF linker hash table. */
7876 struct bfd_link_hash_table
*
7877 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7879 struct elf_link_hash_table
*ret
;
7880 size_t amt
= sizeof (struct elf_link_hash_table
);
7882 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7886 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7887 sizeof (struct elf_link_hash_entry
),
7893 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7898 /* Destroy an ELF linker hash table. */
7901 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7903 struct elf_link_hash_table
*htab
;
7905 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7906 if (htab
->dynstr
!= NULL
)
7907 _bfd_elf_strtab_free (htab
->dynstr
);
7908 _bfd_merge_sections_free (htab
->merge_info
);
7909 _bfd_generic_link_hash_table_free (obfd
);
7912 /* This is a hook for the ELF emulation code in the generic linker to
7913 tell the backend linker what file name to use for the DT_NEEDED
7914 entry for a dynamic object. */
7917 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7919 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7920 && bfd_get_format (abfd
) == bfd_object
)
7921 elf_dt_name (abfd
) = name
;
7925 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
7928 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7929 && bfd_get_format (abfd
) == bfd_object
)
7930 lib_class
= elf_dyn_lib_class (abfd
);
7937 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
7939 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7940 && bfd_get_format (abfd
) == bfd_object
)
7941 elf_dyn_lib_class (abfd
) = lib_class
;
7944 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7945 the linker ELF emulation code. */
7947 struct bfd_link_needed_list
*
7948 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7949 struct bfd_link_info
*info
)
7951 if (! is_elf_hash_table (info
->hash
))
7953 return elf_hash_table (info
)->needed
;
7956 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7957 hook for the linker ELF emulation code. */
7959 struct bfd_link_needed_list
*
7960 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7961 struct bfd_link_info
*info
)
7963 if (! is_elf_hash_table (info
->hash
))
7965 return elf_hash_table (info
)->runpath
;
7968 /* Get the name actually used for a dynamic object for a link. This
7969 is the SONAME entry if there is one. Otherwise, it is the string
7970 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7973 bfd_elf_get_dt_soname (bfd
*abfd
)
7975 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7976 && bfd_get_format (abfd
) == bfd_object
)
7977 return elf_dt_name (abfd
);
7981 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7982 the ELF linker emulation code. */
7985 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
7986 struct bfd_link_needed_list
**pneeded
)
7989 bfd_byte
*dynbuf
= NULL
;
7990 unsigned int elfsec
;
7991 unsigned long shlink
;
7992 bfd_byte
*extdyn
, *extdynend
;
7994 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
7998 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
7999 || bfd_get_format (abfd
) != bfd_object
)
8002 s
= bfd_get_section_by_name (abfd
, ".dynamic");
8003 if (s
== NULL
|| s
->size
== 0)
8006 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
8009 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
8010 if (elfsec
== SHN_BAD
)
8013 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
8015 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
8016 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
8019 extdynend
= extdyn
+ s
->size
;
8020 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
8022 Elf_Internal_Dyn dyn
;
8024 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
8026 if (dyn
.d_tag
== DT_NULL
)
8029 if (dyn
.d_tag
== DT_NEEDED
)
8032 struct bfd_link_needed_list
*l
;
8033 unsigned int tagv
= dyn
.d_un
.d_val
;
8036 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
8041 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
8061 struct elf_symbuf_symbol
8063 unsigned long st_name
; /* Symbol name, index in string tbl */
8064 unsigned char st_info
; /* Type and binding attributes */
8065 unsigned char st_other
; /* Visibilty, and target specific */
8068 struct elf_symbuf_head
8070 struct elf_symbuf_symbol
*ssym
;
8072 unsigned int st_shndx
;
8079 Elf_Internal_Sym
*isym
;
8080 struct elf_symbuf_symbol
*ssym
;
8086 /* Sort references to symbols by ascending section number. */
8089 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
8091 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
8092 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
8094 if (s1
->st_shndx
!= s2
->st_shndx
)
8095 return s1
->st_shndx
> s2
->st_shndx
? 1 : -1;
8096 /* Final sort by the address of the sym in the symbuf ensures
8099 return s1
> s2
? 1 : -1;
8104 elf_sym_name_compare (const void *arg1
, const void *arg2
)
8106 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
8107 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
8108 int ret
= strcmp (s1
->name
, s2
->name
);
8111 if (s1
->u
.p
!= s2
->u
.p
)
8112 return s1
->u
.p
> s2
->u
.p
? 1 : -1;
8116 static struct elf_symbuf_head
*
8117 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
8119 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
8120 struct elf_symbuf_symbol
*ssym
;
8121 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
8122 size_t i
, shndx_count
, total_size
, amt
;
8124 amt
= symcount
* sizeof (*indbuf
);
8125 indbuf
= (Elf_Internal_Sym
**) bfd_malloc (amt
);
8129 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
8130 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
8131 *ind
++ = &isymbuf
[i
];
8134 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
8135 elf_sort_elf_symbol
);
8138 if (indbufend
> indbuf
)
8139 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
8140 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
8143 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
8144 + (indbufend
- indbuf
) * sizeof (*ssym
));
8145 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
8146 if (ssymbuf
== NULL
)
8152 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
8153 ssymbuf
->ssym
= NULL
;
8154 ssymbuf
->count
= shndx_count
;
8155 ssymbuf
->st_shndx
= 0;
8156 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
8158 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
8161 ssymhead
->ssym
= ssym
;
8162 ssymhead
->count
= 0;
8163 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
8165 ssym
->st_name
= (*ind
)->st_name
;
8166 ssym
->st_info
= (*ind
)->st_info
;
8167 ssym
->st_other
= (*ind
)->st_other
;
8170 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
8171 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
8178 /* Check if 2 sections define the same set of local and global
8182 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
8183 struct bfd_link_info
*info
)
8186 const struct elf_backend_data
*bed1
, *bed2
;
8187 Elf_Internal_Shdr
*hdr1
, *hdr2
;
8188 size_t symcount1
, symcount2
;
8189 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
8190 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
8191 Elf_Internal_Sym
*isym
, *isymend
;
8192 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
8193 size_t count1
, count2
, i
;
8194 unsigned int shndx1
, shndx2
;
8200 /* Both sections have to be in ELF. */
8201 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
8202 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
8205 if (elf_section_type (sec1
) != elf_section_type (sec2
))
8208 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
8209 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
8210 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
8213 bed1
= get_elf_backend_data (bfd1
);
8214 bed2
= get_elf_backend_data (bfd2
);
8215 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
8216 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
8217 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
8218 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
8220 if (symcount1
== 0 || symcount2
== 0)
8226 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
8227 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
8229 if (ssymbuf1
== NULL
)
8231 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
8233 if (isymbuf1
== NULL
)
8236 if (info
!= NULL
&& !info
->reduce_memory_overheads
)
8238 ssymbuf1
= elf_create_symbuf (symcount1
, isymbuf1
);
8239 elf_tdata (bfd1
)->symbuf
= ssymbuf1
;
8243 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
8245 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
8247 if (isymbuf2
== NULL
)
8250 if (ssymbuf1
!= NULL
&& info
!= NULL
&& !info
->reduce_memory_overheads
)
8252 ssymbuf2
= elf_create_symbuf (symcount2
, isymbuf2
);
8253 elf_tdata (bfd2
)->symbuf
= ssymbuf2
;
8257 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
8259 /* Optimized faster version. */
8261 struct elf_symbol
*symp
;
8262 struct elf_symbuf_symbol
*ssym
, *ssymend
;
8265 hi
= ssymbuf1
->count
;
8270 mid
= (lo
+ hi
) / 2;
8271 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
8273 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
8277 count1
= ssymbuf1
[mid
].count
;
8284 hi
= ssymbuf2
->count
;
8289 mid
= (lo
+ hi
) / 2;
8290 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
8292 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
8296 count2
= ssymbuf2
[mid
].count
;
8302 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8306 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
8308 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
8309 if (symtable1
== NULL
|| symtable2
== NULL
)
8313 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
8314 ssym
< ssymend
; ssym
++, symp
++)
8316 symp
->u
.ssym
= ssym
;
8317 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
8323 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
8324 ssym
< ssymend
; ssym
++, symp
++)
8326 symp
->u
.ssym
= ssym
;
8327 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
8332 /* Sort symbol by name. */
8333 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8334 elf_sym_name_compare
);
8335 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8336 elf_sym_name_compare
);
8338 for (i
= 0; i
< count1
; i
++)
8339 /* Two symbols must have the same binding, type and name. */
8340 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
8341 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
8342 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8349 symtable1
= (struct elf_symbol
*)
8350 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
8351 symtable2
= (struct elf_symbol
*)
8352 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
8353 if (symtable1
== NULL
|| symtable2
== NULL
)
8356 /* Count definitions in the section. */
8358 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
8359 if (isym
->st_shndx
== shndx1
)
8360 symtable1
[count1
++].u
.isym
= isym
;
8363 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
8364 if (isym
->st_shndx
== shndx2
)
8365 symtable2
[count2
++].u
.isym
= isym
;
8367 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8370 for (i
= 0; i
< count1
; i
++)
8372 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
8373 symtable1
[i
].u
.isym
->st_name
);
8375 for (i
= 0; i
< count2
; i
++)
8377 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
8378 symtable2
[i
].u
.isym
->st_name
);
8380 /* Sort symbol by name. */
8381 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8382 elf_sym_name_compare
);
8383 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8384 elf_sym_name_compare
);
8386 for (i
= 0; i
< count1
; i
++)
8387 /* Two symbols must have the same binding, type and name. */
8388 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
8389 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
8390 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8404 /* Return TRUE if 2 section types are compatible. */
8407 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
8408 bfd
*bbfd
, const asection
*bsec
)
8412 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
8413 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8416 return elf_section_type (asec
) == elf_section_type (bsec
);
8419 /* Final phase of ELF linker. */
8421 /* A structure we use to avoid passing large numbers of arguments. */
8423 struct elf_final_link_info
8425 /* General link information. */
8426 struct bfd_link_info
*info
;
8429 /* Symbol string table. */
8430 struct elf_strtab_hash
*symstrtab
;
8431 /* .hash section. */
8433 /* symbol version section (.gnu.version). */
8434 asection
*symver_sec
;
8435 /* Buffer large enough to hold contents of any section. */
8437 /* Buffer large enough to hold external relocs of any section. */
8438 void *external_relocs
;
8439 /* Buffer large enough to hold internal relocs of any section. */
8440 Elf_Internal_Rela
*internal_relocs
;
8441 /* Buffer large enough to hold external local symbols of any input
8443 bfd_byte
*external_syms
;
8444 /* And a buffer for symbol section indices. */
8445 Elf_External_Sym_Shndx
*locsym_shndx
;
8446 /* Buffer large enough to hold internal local symbols of any input
8448 Elf_Internal_Sym
*internal_syms
;
8449 /* Array large enough to hold a symbol index for each local symbol
8450 of any input BFD. */
8452 /* Array large enough to hold a section pointer for each local
8453 symbol of any input BFD. */
8454 asection
**sections
;
8455 /* Buffer for SHT_SYMTAB_SHNDX section. */
8456 Elf_External_Sym_Shndx
*symshndxbuf
;
8457 /* Number of STT_FILE syms seen. */
8458 size_t filesym_count
;
8459 /* Local symbol hash table. */
8460 struct bfd_hash_table local_hash_table
;
8463 struct local_hash_entry
8465 /* Base hash table entry structure. */
8466 struct bfd_hash_entry root
;
8467 /* Size of the local symbol name. */
8469 /* Number of the duplicated local symbol names. */
8473 /* Create an entry in the local symbol hash table. */
8475 static struct bfd_hash_entry
*
8476 local_hash_newfunc (struct bfd_hash_entry
*entry
,
8477 struct bfd_hash_table
*table
,
8481 /* Allocate the structure if it has not already been allocated by a
8485 entry
= bfd_hash_allocate (table
,
8486 sizeof (struct local_hash_entry
));
8491 /* Call the allocation method of the superclass. */
8492 entry
= bfd_hash_newfunc (entry
, table
, string
);
8495 ((struct local_hash_entry
*) entry
)->count
= 0;
8496 ((struct local_hash_entry
*) entry
)->size
= 0;
8502 /* This struct is used to pass information to elf_link_output_extsym. */
8504 struct elf_outext_info
8507 bfd_boolean localsyms
;
8508 bfd_boolean file_sym_done
;
8509 struct elf_final_link_info
*flinfo
;
8513 /* Support for evaluating a complex relocation.
8515 Complex relocations are generalized, self-describing relocations. The
8516 implementation of them consists of two parts: complex symbols, and the
8517 relocations themselves.
8519 The relocations use a reserved elf-wide relocation type code (R_RELC
8520 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8521 information (start bit, end bit, word width, etc) into the addend. This
8522 information is extracted from CGEN-generated operand tables within gas.
8524 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8525 internal) representing prefix-notation expressions, including but not
8526 limited to those sorts of expressions normally encoded as addends in the
8527 addend field. The symbol mangling format is:
8530 | <unary-operator> ':' <node>
8531 | <binary-operator> ':' <node> ':' <node>
8534 <literal> := 's' <digits=N> ':' <N character symbol name>
8535 | 'S' <digits=N> ':' <N character section name>
8539 <binary-operator> := as in C
8540 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8543 set_symbol_value (bfd
*bfd_with_globals
,
8544 Elf_Internal_Sym
*isymbuf
,
8549 struct elf_link_hash_entry
**sym_hashes
;
8550 struct elf_link_hash_entry
*h
;
8551 size_t extsymoff
= locsymcount
;
8553 if (symidx
< locsymcount
)
8555 Elf_Internal_Sym
*sym
;
8557 sym
= isymbuf
+ symidx
;
8558 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
8560 /* It is a local symbol: move it to the
8561 "absolute" section and give it a value. */
8562 sym
->st_shndx
= SHN_ABS
;
8563 sym
->st_value
= val
;
8566 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
8570 /* It is a global symbol: set its link type
8571 to "defined" and give it a value. */
8573 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
8574 h
= sym_hashes
[symidx
- extsymoff
];
8575 while (h
->root
.type
== bfd_link_hash_indirect
8576 || h
->root
.type
== bfd_link_hash_warning
)
8577 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8578 h
->root
.type
= bfd_link_hash_defined
;
8579 h
->root
.u
.def
.value
= val
;
8580 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
8584 resolve_symbol (const char *name
,
8586 struct elf_final_link_info
*flinfo
,
8588 Elf_Internal_Sym
*isymbuf
,
8591 Elf_Internal_Sym
*sym
;
8592 struct bfd_link_hash_entry
*global_entry
;
8593 const char *candidate
= NULL
;
8594 Elf_Internal_Shdr
*symtab_hdr
;
8597 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
8599 for (i
= 0; i
< locsymcount
; ++ i
)
8603 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
8606 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
8607 symtab_hdr
->sh_link
,
8610 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8611 name
, candidate
, (unsigned long) sym
->st_value
);
8613 if (candidate
&& strcmp (candidate
, name
) == 0)
8615 asection
*sec
= flinfo
->sections
[i
];
8617 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
8618 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
8620 printf ("Found symbol with value %8.8lx\n",
8621 (unsigned long) *result
);
8627 /* Hmm, haven't found it yet. perhaps it is a global. */
8628 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
8629 FALSE
, FALSE
, TRUE
);
8633 if (global_entry
->type
== bfd_link_hash_defined
8634 || global_entry
->type
== bfd_link_hash_defweak
)
8636 *result
= (global_entry
->u
.def
.value
8637 + global_entry
->u
.def
.section
->output_section
->vma
8638 + global_entry
->u
.def
.section
->output_offset
);
8640 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8641 global_entry
->root
.string
, (unsigned long) *result
);
8649 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8650 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8651 names like "foo.end" which is the end address of section "foo". */
8654 resolve_section (const char *name
,
8662 for (curr
= sections
; curr
; curr
= curr
->next
)
8663 if (strcmp (curr
->name
, name
) == 0)
8665 *result
= curr
->vma
;
8669 /* Hmm. still haven't found it. try pseudo-section names. */
8670 /* FIXME: This could be coded more efficiently... */
8671 for (curr
= sections
; curr
; curr
= curr
->next
)
8673 len
= strlen (curr
->name
);
8674 if (len
> strlen (name
))
8677 if (strncmp (curr
->name
, name
, len
) == 0)
8679 if (strncmp (".end", name
+ len
, 4) == 0)
8681 *result
= (curr
->vma
8682 + curr
->size
/ bfd_octets_per_byte (abfd
, curr
));
8686 /* Insert more pseudo-section names here, if you like. */
8694 undefined_reference (const char *reftype
, const char *name
)
8696 /* xgettext:c-format */
8697 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8699 bfd_set_error (bfd_error_bad_value
);
8703 eval_symbol (bfd_vma
*result
,
8706 struct elf_final_link_info
*flinfo
,
8708 Elf_Internal_Sym
*isymbuf
,
8717 const char *sym
= *symp
;
8719 bfd_boolean symbol_is_section
= FALSE
;
8724 if (len
< 1 || len
> sizeof (symbuf
))
8726 bfd_set_error (bfd_error_invalid_operation
);
8739 *result
= strtoul (sym
, (char **) symp
, 16);
8743 symbol_is_section
= TRUE
;
8747 symlen
= strtol (sym
, (char **) symp
, 10);
8748 sym
= *symp
+ 1; /* Skip the trailing ':'. */
8750 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
8752 bfd_set_error (bfd_error_invalid_operation
);
8756 memcpy (symbuf
, sym
, symlen
);
8757 symbuf
[symlen
] = '\0';
8758 *symp
= sym
+ symlen
;
8760 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8761 the symbol as a section, or vice-versa. so we're pretty liberal in our
8762 interpretation here; section means "try section first", not "must be a
8763 section", and likewise with symbol. */
8765 if (symbol_is_section
)
8767 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
8768 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8769 isymbuf
, locsymcount
))
8771 undefined_reference ("section", symbuf
);
8777 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8778 isymbuf
, locsymcount
)
8779 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8782 undefined_reference ("symbol", symbuf
);
8789 /* All that remains are operators. */
8791 #define UNARY_OP(op) \
8792 if (strncmp (sym, #op, strlen (#op)) == 0) \
8794 sym += strlen (#op); \
8798 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8799 isymbuf, locsymcount, signed_p)) \
8802 *result = op ((bfd_signed_vma) a); \
8808 #define BINARY_OP_HEAD(op) \
8809 if (strncmp (sym, #op, strlen (#op)) == 0) \
8811 sym += strlen (#op); \
8815 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8816 isymbuf, locsymcount, signed_p)) \
8819 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8820 isymbuf, locsymcount, signed_p)) \
8822 #define BINARY_OP_TAIL(op) \
8824 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8829 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
8833 BINARY_OP_HEAD (<<);
8834 if (b
>= sizeof (a
) * CHAR_BIT
)
8840 BINARY_OP_TAIL (<<);
8841 BINARY_OP_HEAD (>>);
8842 if (b
>= sizeof (a
) * CHAR_BIT
)
8844 *result
= signed_p
&& (bfd_signed_vma
) a
< 0 ? -1 : 0;
8847 BINARY_OP_TAIL (>>);
8860 _bfd_error_handler (_("division by zero"));
8861 bfd_set_error (bfd_error_bad_value
);
8868 _bfd_error_handler (_("division by zero"));
8869 bfd_set_error (bfd_error_bad_value
);
8882 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
8883 bfd_set_error (bfd_error_invalid_operation
);
8889 put_value (bfd_vma size
,
8890 unsigned long chunksz
,
8895 location
+= (size
- chunksz
);
8897 for (; size
; size
-= chunksz
, location
-= chunksz
)
8902 bfd_put_8 (input_bfd
, x
, location
);
8906 bfd_put_16 (input_bfd
, x
, location
);
8910 bfd_put_32 (input_bfd
, x
, location
);
8911 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8917 bfd_put_64 (input_bfd
, x
, location
);
8918 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8931 get_value (bfd_vma size
,
8932 unsigned long chunksz
,
8939 /* Sanity checks. */
8940 BFD_ASSERT (chunksz
<= sizeof (x
)
8943 && (size
% chunksz
) == 0
8944 && input_bfd
!= NULL
8945 && location
!= NULL
);
8947 if (chunksz
== sizeof (x
))
8949 BFD_ASSERT (size
== chunksz
);
8951 /* Make sure that we do not perform an undefined shift operation.
8952 We know that size == chunksz so there will only be one iteration
8953 of the loop below. */
8957 shift
= 8 * chunksz
;
8959 for (; size
; size
-= chunksz
, location
+= chunksz
)
8964 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
8967 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
8970 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
8974 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
8985 decode_complex_addend (unsigned long *start
, /* in bits */
8986 unsigned long *oplen
, /* in bits */
8987 unsigned long *len
, /* in bits */
8988 unsigned long *wordsz
, /* in bytes */
8989 unsigned long *chunksz
, /* in bytes */
8990 unsigned long *lsb0_p
,
8991 unsigned long *signed_p
,
8992 unsigned long *trunc_p
,
8993 unsigned long encoded
)
8995 * start
= encoded
& 0x3F;
8996 * len
= (encoded
>> 6) & 0x3F;
8997 * oplen
= (encoded
>> 12) & 0x3F;
8998 * wordsz
= (encoded
>> 18) & 0xF;
8999 * chunksz
= (encoded
>> 22) & 0xF;
9000 * lsb0_p
= (encoded
>> 27) & 1;
9001 * signed_p
= (encoded
>> 28) & 1;
9002 * trunc_p
= (encoded
>> 29) & 1;
9005 bfd_reloc_status_type
9006 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
9007 asection
*input_section
,
9009 Elf_Internal_Rela
*rel
,
9012 bfd_vma shift
, x
, mask
;
9013 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
9014 bfd_reloc_status_type r
;
9015 bfd_size_type octets
;
9017 /* Perform this reloc, since it is complex.
9018 (this is not to say that it necessarily refers to a complex
9019 symbol; merely that it is a self-describing CGEN based reloc.
9020 i.e. the addend has the complete reloc information (bit start, end,
9021 word size, etc) encoded within it.). */
9023 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
9024 &chunksz
, &lsb0_p
, &signed_p
,
9025 &trunc_p
, rel
->r_addend
);
9027 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
9030 shift
= (start
+ 1) - len
;
9032 shift
= (8 * wordsz
) - (start
+ len
);
9034 octets
= rel
->r_offset
* bfd_octets_per_byte (input_bfd
, input_section
);
9035 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ octets
);
9038 printf ("Doing complex reloc: "
9039 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9040 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9041 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9042 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
9043 oplen
, (unsigned long) x
, (unsigned long) mask
,
9044 (unsigned long) relocation
);
9049 /* Now do an overflow check. */
9050 r
= bfd_check_overflow ((signed_p
9051 ? complain_overflow_signed
9052 : complain_overflow_unsigned
),
9053 len
, 0, (8 * wordsz
),
9057 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
9060 printf (" relocation: %8.8lx\n"
9061 " shifted mask: %8.8lx\n"
9062 " shifted/masked reloc: %8.8lx\n"
9063 " result: %8.8lx\n",
9064 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
9065 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
9067 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ octets
);
9071 /* Functions to read r_offset from external (target order) reloc
9072 entry. Faster than bfd_getl32 et al, because we let the compiler
9073 know the value is aligned. */
9076 ext32l_r_offset (const void *p
)
9083 const union aligned32
*a
9084 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9086 uint32_t aval
= ( (uint32_t) a
->c
[0]
9087 | (uint32_t) a
->c
[1] << 8
9088 | (uint32_t) a
->c
[2] << 16
9089 | (uint32_t) a
->c
[3] << 24);
9094 ext32b_r_offset (const void *p
)
9101 const union aligned32
*a
9102 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9104 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
9105 | (uint32_t) a
->c
[1] << 16
9106 | (uint32_t) a
->c
[2] << 8
9107 | (uint32_t) a
->c
[3]);
9111 #ifdef BFD_HOST_64_BIT
9113 ext64l_r_offset (const void *p
)
9120 const union aligned64
*a
9121 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9123 uint64_t aval
= ( (uint64_t) a
->c
[0]
9124 | (uint64_t) a
->c
[1] << 8
9125 | (uint64_t) a
->c
[2] << 16
9126 | (uint64_t) a
->c
[3] << 24
9127 | (uint64_t) a
->c
[4] << 32
9128 | (uint64_t) a
->c
[5] << 40
9129 | (uint64_t) a
->c
[6] << 48
9130 | (uint64_t) a
->c
[7] << 56);
9135 ext64b_r_offset (const void *p
)
9142 const union aligned64
*a
9143 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9145 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
9146 | (uint64_t) a
->c
[1] << 48
9147 | (uint64_t) a
->c
[2] << 40
9148 | (uint64_t) a
->c
[3] << 32
9149 | (uint64_t) a
->c
[4] << 24
9150 | (uint64_t) a
->c
[5] << 16
9151 | (uint64_t) a
->c
[6] << 8
9152 | (uint64_t) a
->c
[7]);
9157 /* When performing a relocatable link, the input relocations are
9158 preserved. But, if they reference global symbols, the indices
9159 referenced must be updated. Update all the relocations found in
9163 elf_link_adjust_relocs (bfd
*abfd
,
9165 struct bfd_elf_section_reloc_data
*reldata
,
9167 struct bfd_link_info
*info
)
9170 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9172 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9173 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9174 bfd_vma r_type_mask
;
9176 unsigned int count
= reldata
->count
;
9177 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
9179 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
9181 swap_in
= bed
->s
->swap_reloc_in
;
9182 swap_out
= bed
->s
->swap_reloc_out
;
9184 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
9186 swap_in
= bed
->s
->swap_reloca_in
;
9187 swap_out
= bed
->s
->swap_reloca_out
;
9192 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
9195 if (bed
->s
->arch_size
== 32)
9202 r_type_mask
= 0xffffffff;
9206 erela
= reldata
->hdr
->contents
;
9207 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
9209 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
9212 if (*rel_hash
== NULL
)
9215 if ((*rel_hash
)->indx
== -2
9216 && info
->gc_sections
9217 && ! info
->gc_keep_exported
)
9219 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9220 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9222 (*rel_hash
)->root
.root
.string
);
9223 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9225 bfd_set_error (bfd_error_invalid_operation
);
9228 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
9230 (*swap_in
) (abfd
, erela
, irela
);
9231 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
9232 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
9233 | (irela
[j
].r_info
& r_type_mask
));
9234 (*swap_out
) (abfd
, irela
, erela
);
9237 if (bed
->elf_backend_update_relocs
)
9238 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
9240 if (sort
&& count
!= 0)
9242 bfd_vma (*ext_r_off
) (const void *);
9245 bfd_byte
*base
, *end
, *p
, *loc
;
9246 bfd_byte
*buf
= NULL
;
9248 if (bed
->s
->arch_size
== 32)
9250 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9251 ext_r_off
= ext32l_r_offset
;
9252 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9253 ext_r_off
= ext32b_r_offset
;
9259 #ifdef BFD_HOST_64_BIT
9260 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9261 ext_r_off
= ext64l_r_offset
;
9262 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9263 ext_r_off
= ext64b_r_offset
;
9269 /* Must use a stable sort here. A modified insertion sort,
9270 since the relocs are mostly sorted already. */
9271 elt_size
= reldata
->hdr
->sh_entsize
;
9272 base
= reldata
->hdr
->contents
;
9273 end
= base
+ count
* elt_size
;
9274 if (elt_size
> sizeof (Elf64_External_Rela
))
9277 /* Ensure the first element is lowest. This acts as a sentinel,
9278 speeding the main loop below. */
9279 r_off
= (*ext_r_off
) (base
);
9280 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
9282 bfd_vma r_off2
= (*ext_r_off
) (p
);
9291 /* Don't just swap *base and *loc as that changes the order
9292 of the original base[0] and base[1] if they happen to
9293 have the same r_offset. */
9294 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
9295 memcpy (onebuf
, loc
, elt_size
);
9296 memmove (base
+ elt_size
, base
, loc
- base
);
9297 memcpy (base
, onebuf
, elt_size
);
9300 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
9302 /* base to p is sorted, *p is next to insert. */
9303 r_off
= (*ext_r_off
) (p
);
9304 /* Search the sorted region for location to insert. */
9306 while (r_off
< (*ext_r_off
) (loc
))
9311 /* Chances are there is a run of relocs to insert here,
9312 from one of more input files. Files are not always
9313 linked in order due to the way elf_link_input_bfd is
9314 called. See pr17666. */
9315 size_t sortlen
= p
- loc
;
9316 bfd_vma r_off2
= (*ext_r_off
) (loc
);
9317 size_t runlen
= elt_size
;
9318 size_t buf_size
= 96 * 1024;
9319 while (p
+ runlen
< end
9320 && (sortlen
<= buf_size
9321 || runlen
+ elt_size
<= buf_size
)
9322 && r_off2
> (*ext_r_off
) (p
+ runlen
))
9326 buf
= bfd_malloc (buf_size
);
9330 if (runlen
< sortlen
)
9332 memcpy (buf
, p
, runlen
);
9333 memmove (loc
+ runlen
, loc
, sortlen
);
9334 memcpy (loc
, buf
, runlen
);
9338 memcpy (buf
, loc
, sortlen
);
9339 memmove (loc
, p
, runlen
);
9340 memcpy (loc
+ runlen
, buf
, sortlen
);
9342 p
+= runlen
- elt_size
;
9345 /* Hashes are no longer valid. */
9346 free (reldata
->hashes
);
9347 reldata
->hashes
= NULL
;
9353 struct elf_link_sort_rela
9359 enum elf_reloc_type_class type
;
9360 /* We use this as an array of size int_rels_per_ext_rel. */
9361 Elf_Internal_Rela rela
[1];
9364 /* qsort stability here and for cmp2 is only an issue if multiple
9365 dynamic relocations are emitted at the same address. But targets
9366 that apply a series of dynamic relocations each operating on the
9367 result of the prior relocation can't use -z combreloc as
9368 implemented anyway. Such schemes tend to be broken by sorting on
9369 symbol index. That leaves dynamic NONE relocs as the only other
9370 case where ld might emit multiple relocs at the same address, and
9371 those are only emitted due to target bugs. */
9374 elf_link_sort_cmp1 (const void *A
, const void *B
)
9376 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9377 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9378 int relativea
, relativeb
;
9380 relativea
= a
->type
== reloc_class_relative
;
9381 relativeb
= b
->type
== reloc_class_relative
;
9383 if (relativea
< relativeb
)
9385 if (relativea
> relativeb
)
9387 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
9389 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
9391 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9393 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9399 elf_link_sort_cmp2 (const void *A
, const void *B
)
9401 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9402 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9404 if (a
->type
< b
->type
)
9406 if (a
->type
> b
->type
)
9408 if (a
->u
.offset
< b
->u
.offset
)
9410 if (a
->u
.offset
> b
->u
.offset
)
9412 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9414 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9420 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
9422 asection
*dynamic_relocs
;
9425 bfd_size_type count
, size
;
9426 size_t i
, ret
, sort_elt
, ext_size
;
9427 bfd_byte
*sort
, *s_non_relative
, *p
;
9428 struct elf_link_sort_rela
*sq
;
9429 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9430 int i2e
= bed
->s
->int_rels_per_ext_rel
;
9431 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
9432 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9433 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9434 struct bfd_link_order
*lo
;
9436 bfd_boolean use_rela
;
9438 /* Find a dynamic reloc section. */
9439 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
9440 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
9441 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
9442 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9444 bfd_boolean use_rela_initialised
= FALSE
;
9446 /* This is just here to stop gcc from complaining.
9447 Its initialization checking code is not perfect. */
9450 /* Both sections are present. Examine the sizes
9451 of the indirect sections to help us choose. */
9452 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9453 if (lo
->type
== bfd_indirect_link_order
)
9455 asection
*o
= lo
->u
.indirect
.section
;
9457 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9459 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9460 /* Section size is divisible by both rel and rela sizes.
9461 It is of no help to us. */
9465 /* Section size is only divisible by rela. */
9466 if (use_rela_initialised
&& !use_rela
)
9468 _bfd_error_handler (_("%pB: unable to sort relocs - "
9469 "they are in more than one size"),
9471 bfd_set_error (bfd_error_invalid_operation
);
9477 use_rela_initialised
= TRUE
;
9481 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9483 /* Section size is only divisible by rel. */
9484 if (use_rela_initialised
&& use_rela
)
9486 _bfd_error_handler (_("%pB: unable to sort relocs - "
9487 "they are in more than one size"),
9489 bfd_set_error (bfd_error_invalid_operation
);
9495 use_rela_initialised
= TRUE
;
9500 /* The section size is not divisible by either -
9501 something is wrong. */
9502 _bfd_error_handler (_("%pB: unable to sort relocs - "
9503 "they are of an unknown size"), abfd
);
9504 bfd_set_error (bfd_error_invalid_operation
);
9509 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9510 if (lo
->type
== bfd_indirect_link_order
)
9512 asection
*o
= lo
->u
.indirect
.section
;
9514 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9516 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9517 /* Section size is divisible by both rel and rela sizes.
9518 It is of no help to us. */
9522 /* Section size is only divisible by rela. */
9523 if (use_rela_initialised
&& !use_rela
)
9525 _bfd_error_handler (_("%pB: unable to sort relocs - "
9526 "they are in more than one size"),
9528 bfd_set_error (bfd_error_invalid_operation
);
9534 use_rela_initialised
= TRUE
;
9538 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9540 /* Section size is only divisible by rel. */
9541 if (use_rela_initialised
&& use_rela
)
9543 _bfd_error_handler (_("%pB: unable to sort relocs - "
9544 "they are in more than one size"),
9546 bfd_set_error (bfd_error_invalid_operation
);
9552 use_rela_initialised
= TRUE
;
9557 /* The section size is not divisible by either -
9558 something is wrong. */
9559 _bfd_error_handler (_("%pB: unable to sort relocs - "
9560 "they are of an unknown size"), abfd
);
9561 bfd_set_error (bfd_error_invalid_operation
);
9566 if (! use_rela_initialised
)
9570 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
9572 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9579 dynamic_relocs
= rela_dyn
;
9580 ext_size
= bed
->s
->sizeof_rela
;
9581 swap_in
= bed
->s
->swap_reloca_in
;
9582 swap_out
= bed
->s
->swap_reloca_out
;
9586 dynamic_relocs
= rel_dyn
;
9587 ext_size
= bed
->s
->sizeof_rel
;
9588 swap_in
= bed
->s
->swap_reloc_in
;
9589 swap_out
= bed
->s
->swap_reloc_out
;
9593 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9594 if (lo
->type
== bfd_indirect_link_order
)
9595 size
+= lo
->u
.indirect
.section
->size
;
9597 if (size
!= dynamic_relocs
->size
)
9600 sort_elt
= (sizeof (struct elf_link_sort_rela
)
9601 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
9603 count
= dynamic_relocs
->size
/ ext_size
;
9606 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
9610 (*info
->callbacks
->warning
)
9611 (info
, _("not enough memory to sort relocations"), 0, abfd
, 0, 0);
9615 if (bed
->s
->arch_size
== 32)
9616 r_sym_mask
= ~(bfd_vma
) 0xff;
9618 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
9620 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9621 if (lo
->type
== bfd_indirect_link_order
)
9623 bfd_byte
*erel
, *erelend
;
9624 asection
*o
= lo
->u
.indirect
.section
;
9626 if (o
->contents
== NULL
&& o
->size
!= 0)
9628 /* This is a reloc section that is being handled as a normal
9629 section. See bfd_section_from_shdr. We can't combine
9630 relocs in this case. */
9635 erelend
= o
->contents
+ o
->size
;
9636 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
9638 while (erel
< erelend
)
9640 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9642 (*swap_in
) (abfd
, erel
, s
->rela
);
9643 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
9644 s
->u
.sym_mask
= r_sym_mask
;
9650 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
9652 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
9654 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9655 if (s
->type
!= reloc_class_relative
)
9661 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
9662 for (; i
< count
; i
++, p
+= sort_elt
)
9664 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
9665 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
9667 sp
->u
.offset
= sq
->rela
->r_offset
;
9670 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
9672 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
9673 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
9675 /* We have plt relocs in .rela.dyn. */
9676 sq
= (struct elf_link_sort_rela
*) sort
;
9677 for (i
= 0; i
< count
; i
++)
9678 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
9680 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
9682 struct bfd_link_order
**plo
;
9683 /* Put srelplt link_order last. This is so the output_offset
9684 set in the next loop is correct for DT_JMPREL. */
9685 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
9686 if ((*plo
)->type
== bfd_indirect_link_order
9687 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
9693 plo
= &(*plo
)->next
;
9696 dynamic_relocs
->map_tail
.link_order
= lo
;
9701 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9702 if (lo
->type
== bfd_indirect_link_order
)
9704 bfd_byte
*erel
, *erelend
;
9705 asection
*o
= lo
->u
.indirect
.section
;
9708 erelend
= o
->contents
+ o
->size
;
9709 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
9710 while (erel
< erelend
)
9712 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9713 (*swap_out
) (abfd
, s
->rela
, erel
);
9720 *psec
= dynamic_relocs
;
9724 /* Add a symbol to the output symbol string table. */
9727 elf_link_output_symstrtab (struct elf_final_link_info
*flinfo
,
9729 Elf_Internal_Sym
*elfsym
,
9730 asection
*input_sec
,
9731 struct elf_link_hash_entry
*h
)
9733 int (*output_symbol_hook
)
9734 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
9735 struct elf_link_hash_entry
*);
9736 struct elf_link_hash_table
*hash_table
;
9737 const struct elf_backend_data
*bed
;
9738 bfd_size_type strtabsize
;
9740 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9742 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9743 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
9744 if (output_symbol_hook
!= NULL
)
9746 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
9751 if (ELF_ST_TYPE (elfsym
->st_info
) == STT_GNU_IFUNC
)
9752 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_ifunc
;
9753 if (ELF_ST_BIND (elfsym
->st_info
) == STB_GNU_UNIQUE
)
9754 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_unique
;
9758 || (input_sec
->flags
& SEC_EXCLUDE
))
9759 elfsym
->st_name
= (unsigned long) -1;
9762 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9763 to get the final offset for st_name. */
9764 char *versioned_name
= (char *) name
;
9767 if (h
->versioned
== versioned
&& h
->def_dynamic
)
9769 /* Keep only one '@' for versioned symbols defined in
9771 char *version
= strrchr (name
, ELF_VER_CHR
);
9772 char *base_end
= strchr (name
, ELF_VER_CHR
);
9773 if (version
!= base_end
)
9776 size_t len
= strlen (name
);
9777 versioned_name
= bfd_alloc (flinfo
->output_bfd
, len
);
9778 if (versioned_name
== NULL
)
9780 base_len
= base_end
- name
;
9781 memcpy (versioned_name
, name
, base_len
);
9782 memcpy (versioned_name
+ base_len
, version
,
9787 else if (flinfo
->info
->unique_symbol
9788 && ELF_ST_BIND (elfsym
->st_info
) == STB_LOCAL
)
9790 struct local_hash_entry
*lh
;
9791 switch (ELF_ST_TYPE (elfsym
->st_info
))
9797 lh
= (struct local_hash_entry
*) bfd_hash_lookup
9798 (&flinfo
->local_hash_table
, name
, TRUE
, FALSE
);
9803 /* Append ".COUNT" to duplicated local symbols. */
9805 size_t base_len
= lh
->size
;
9807 sprintf (buf
, "%lx", lh
->count
);
9810 base_len
= strlen (name
);
9811 lh
->size
= base_len
;
9813 count_len
= strlen (buf
);
9814 versioned_name
= bfd_alloc (flinfo
->output_bfd
,
9815 base_len
+ count_len
+ 2);
9816 if (versioned_name
== NULL
)
9818 memcpy (versioned_name
, name
, base_len
);
9819 versioned_name
[base_len
] = '.';
9820 memcpy (versioned_name
+ base_len
+ 1, buf
,
9828 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
9829 versioned_name
, FALSE
);
9830 if (elfsym
->st_name
== (unsigned long) -1)
9834 hash_table
= elf_hash_table (flinfo
->info
);
9835 strtabsize
= hash_table
->strtabsize
;
9836 if (strtabsize
<= hash_table
->strtabcount
)
9838 strtabsize
+= strtabsize
;
9839 hash_table
->strtabsize
= strtabsize
;
9840 strtabsize
*= sizeof (*hash_table
->strtab
);
9842 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
9844 if (hash_table
->strtab
== NULL
)
9847 hash_table
->strtab
[hash_table
->strtabcount
].sym
= *elfsym
;
9848 hash_table
->strtab
[hash_table
->strtabcount
].dest_index
9849 = hash_table
->strtabcount
;
9850 hash_table
->strtab
[hash_table
->strtabcount
].destshndx_index
9851 = flinfo
->symshndxbuf
? bfd_get_symcount (flinfo
->output_bfd
) : 0;
9853 flinfo
->output_bfd
->symcount
+= 1;
9854 hash_table
->strtabcount
+= 1;
9859 /* Swap symbols out to the symbol table and flush the output symbols to
9863 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
9865 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
9868 const struct elf_backend_data
*bed
;
9870 Elf_Internal_Shdr
*hdr
;
9874 if (!hash_table
->strtabcount
)
9877 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9879 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9881 amt
= bed
->s
->sizeof_sym
* hash_table
->strtabcount
;
9882 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
9886 if (flinfo
->symshndxbuf
)
9888 amt
= sizeof (Elf_External_Sym_Shndx
);
9889 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
9890 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
9891 if (flinfo
->symshndxbuf
== NULL
)
9898 /* Now swap out the symbols. */
9899 for (i
= 0; i
< hash_table
->strtabcount
; i
++)
9901 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
9902 if (elfsym
->sym
.st_name
== (unsigned long) -1)
9903 elfsym
->sym
.st_name
= 0;
9906 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
9907 elfsym
->sym
.st_name
);
9909 /* Inform the linker of the addition of this symbol. */
9911 if (flinfo
->info
->callbacks
->ctf_new_symbol
)
9912 flinfo
->info
->callbacks
->ctf_new_symbol (elfsym
->dest_index
,
9915 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
9916 ((bfd_byte
*) symbuf
9917 + (elfsym
->dest_index
9918 * bed
->s
->sizeof_sym
)),
9919 (flinfo
->symshndxbuf
9920 + elfsym
->destshndx_index
));
9923 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
9924 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
9925 amt
= hash_table
->strtabcount
* bed
->s
->sizeof_sym
;
9926 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
9927 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
9929 hdr
->sh_size
+= amt
;
9937 free (hash_table
->strtab
);
9938 hash_table
->strtab
= NULL
;
9943 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9946 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
9948 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
9949 && sym
->st_shndx
< SHN_LORESERVE
)
9951 /* The gABI doesn't support dynamic symbols in output sections
9954 /* xgettext:c-format */
9955 (_("%pB: too many sections: %d (>= %d)"),
9956 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
9957 bfd_set_error (bfd_error_nonrepresentable_section
);
9963 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9964 allowing an unsatisfied unversioned symbol in the DSO to match a
9965 versioned symbol that would normally require an explicit version.
9966 We also handle the case that a DSO references a hidden symbol
9967 which may be satisfied by a versioned symbol in another DSO. */
9970 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
9971 const struct elf_backend_data
*bed
,
9972 struct elf_link_hash_entry
*h
)
9975 struct elf_link_loaded_list
*loaded
;
9977 if (!is_elf_hash_table (info
->hash
))
9980 /* Check indirect symbol. */
9981 while (h
->root
.type
== bfd_link_hash_indirect
)
9982 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9984 switch (h
->root
.type
)
9990 case bfd_link_hash_undefined
:
9991 case bfd_link_hash_undefweak
:
9992 abfd
= h
->root
.u
.undef
.abfd
;
9994 || (abfd
->flags
& DYNAMIC
) == 0
9995 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
9999 case bfd_link_hash_defined
:
10000 case bfd_link_hash_defweak
:
10001 abfd
= h
->root
.u
.def
.section
->owner
;
10004 case bfd_link_hash_common
:
10005 abfd
= h
->root
.u
.c
.p
->section
->owner
;
10008 BFD_ASSERT (abfd
!= NULL
);
10010 for (loaded
= elf_hash_table (info
)->dyn_loaded
;
10012 loaded
= loaded
->next
)
10015 Elf_Internal_Shdr
*hdr
;
10017 size_t extsymcount
;
10019 Elf_Internal_Shdr
*versymhdr
;
10020 Elf_Internal_Sym
*isym
;
10021 Elf_Internal_Sym
*isymend
;
10022 Elf_Internal_Sym
*isymbuf
;
10023 Elf_External_Versym
*ever
;
10024 Elf_External_Versym
*extversym
;
10026 input
= loaded
->abfd
;
10028 /* We check each DSO for a possible hidden versioned definition. */
10030 || elf_dynversym (input
) == 0)
10033 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
10035 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10036 if (elf_bad_symtab (input
))
10038 extsymcount
= symcount
;
10043 extsymcount
= symcount
- hdr
->sh_info
;
10044 extsymoff
= hdr
->sh_info
;
10047 if (extsymcount
== 0)
10050 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
10052 if (isymbuf
== NULL
)
10055 /* Read in any version definitions. */
10056 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
10057 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
10058 || (extversym
= (Elf_External_Versym
*)
10059 _bfd_malloc_and_read (input
, versymhdr
->sh_size
,
10060 versymhdr
->sh_size
)) == NULL
)
10066 ever
= extversym
+ extsymoff
;
10067 isymend
= isymbuf
+ extsymcount
;
10068 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
10071 Elf_Internal_Versym iver
;
10072 unsigned short version_index
;
10074 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
10075 || isym
->st_shndx
== SHN_UNDEF
)
10078 name
= bfd_elf_string_from_elf_section (input
,
10081 if (strcmp (name
, h
->root
.root
.string
) != 0)
10084 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
10086 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
10087 && !(h
->def_regular
10088 && h
->forced_local
))
10090 /* If we have a non-hidden versioned sym, then it should
10091 have provided a definition for the undefined sym unless
10092 it is defined in a non-shared object and forced local.
10097 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
10098 if (version_index
== 1 || version_index
== 2)
10100 /* This is the base or first version. We can use it. */
10114 /* Convert ELF common symbol TYPE. */
10117 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
10119 /* Commom symbol can only appear in relocatable link. */
10120 if (!bfd_link_relocatable (info
))
10122 switch (info
->elf_stt_common
)
10126 case elf_stt_common
:
10129 case no_elf_stt_common
:
10136 /* Add an external symbol to the symbol table. This is called from
10137 the hash table traversal routine. When generating a shared object,
10138 we go through the symbol table twice. The first time we output
10139 anything that might have been forced to local scope in a version
10140 script. The second time we output the symbols that are still
10144 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
10146 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
10147 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
10148 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
10150 Elf_Internal_Sym sym
;
10151 asection
*input_sec
;
10152 const struct elf_backend_data
*bed
;
10157 if (h
->root
.type
== bfd_link_hash_warning
)
10159 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10160 if (h
->root
.type
== bfd_link_hash_new
)
10164 /* Decide whether to output this symbol in this pass. */
10165 if (eoinfo
->localsyms
)
10167 if (!h
->forced_local
)
10172 if (h
->forced_local
)
10176 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10178 if (h
->root
.type
== bfd_link_hash_undefined
)
10180 /* If we have an undefined symbol reference here then it must have
10181 come from a shared library that is being linked in. (Undefined
10182 references in regular files have already been handled unless
10183 they are in unreferenced sections which are removed by garbage
10185 bfd_boolean ignore_undef
= FALSE
;
10187 /* Some symbols may be special in that the fact that they're
10188 undefined can be safely ignored - let backend determine that. */
10189 if (bed
->elf_backend_ignore_undef_symbol
)
10190 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
10192 /* If we are reporting errors for this situation then do so now. */
10194 && h
->ref_dynamic_nonweak
10195 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
10196 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
10197 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
10199 flinfo
->info
->callbacks
->undefined_symbol
10200 (flinfo
->info
, h
->root
.root
.string
,
10201 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
, NULL
, 0,
10202 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_DIAGNOSE
10203 && !flinfo
->info
->warn_unresolved_syms
);
10206 /* Strip a global symbol defined in a discarded section. */
10211 /* We should also warn if a forced local symbol is referenced from
10212 shared libraries. */
10213 if (bfd_link_executable (flinfo
->info
)
10218 && h
->ref_dynamic_nonweak
10219 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
10223 struct elf_link_hash_entry
*hi
= h
;
10225 /* Check indirect symbol. */
10226 while (hi
->root
.type
== bfd_link_hash_indirect
)
10227 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
10229 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
10230 /* xgettext:c-format */
10231 msg
= _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10232 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
10233 /* xgettext:c-format */
10234 msg
= _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10236 /* xgettext:c-format */
10237 msg
= _("%pB: local symbol `%s' in %pB is referenced by DSO");
10238 def_bfd
= flinfo
->output_bfd
;
10239 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
10240 def_bfd
= hi
->root
.u
.def
.section
->owner
;
10241 _bfd_error_handler (msg
, flinfo
->output_bfd
,
10242 h
->root
.root
.string
, def_bfd
);
10243 bfd_set_error (bfd_error_bad_value
);
10244 eoinfo
->failed
= TRUE
;
10248 /* We don't want to output symbols that have never been mentioned by
10249 a regular file, or that we have been told to strip. However, if
10250 h->indx is set to -2, the symbol is used by a reloc and we must
10255 else if ((h
->def_dynamic
10257 || h
->root
.type
== bfd_link_hash_new
)
10259 && !h
->ref_regular
)
10261 else if (flinfo
->info
->strip
== strip_all
)
10263 else if (flinfo
->info
->strip
== strip_some
10264 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
10265 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
10267 else if ((h
->root
.type
== bfd_link_hash_defined
10268 || h
->root
.type
== bfd_link_hash_defweak
)
10269 && ((flinfo
->info
->strip_discarded
10270 && discarded_section (h
->root
.u
.def
.section
))
10271 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
10272 && h
->root
.u
.def
.section
->owner
!= NULL
10273 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
10275 else if ((h
->root
.type
== bfd_link_hash_undefined
10276 || h
->root
.type
== bfd_link_hash_undefweak
)
10277 && h
->root
.u
.undef
.abfd
!= NULL
10278 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
10283 /* If we're stripping it, and it's not a dynamic symbol, there's
10284 nothing else to do. However, if it is a forced local symbol or
10285 an ifunc symbol we need to give the backend finish_dynamic_symbol
10286 function a chance to make it dynamic. */
10288 && h
->dynindx
== -1
10289 && type
!= STT_GNU_IFUNC
10290 && !h
->forced_local
)
10294 sym
.st_size
= h
->size
;
10295 sym
.st_other
= h
->other
;
10296 switch (h
->root
.type
)
10299 case bfd_link_hash_new
:
10300 case bfd_link_hash_warning
:
10304 case bfd_link_hash_undefined
:
10305 case bfd_link_hash_undefweak
:
10306 input_sec
= bfd_und_section_ptr
;
10307 sym
.st_shndx
= SHN_UNDEF
;
10310 case bfd_link_hash_defined
:
10311 case bfd_link_hash_defweak
:
10313 input_sec
= h
->root
.u
.def
.section
;
10314 if (input_sec
->output_section
!= NULL
)
10317 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
10318 input_sec
->output_section
);
10319 if (sym
.st_shndx
== SHN_BAD
)
10322 /* xgettext:c-format */
10323 (_("%pB: could not find output section %pA for input section %pA"),
10324 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
10325 bfd_set_error (bfd_error_nonrepresentable_section
);
10326 eoinfo
->failed
= TRUE
;
10330 /* ELF symbols in relocatable files are section relative,
10331 but in nonrelocatable files they are virtual
10333 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
10334 if (!bfd_link_relocatable (flinfo
->info
))
10336 sym
.st_value
+= input_sec
->output_section
->vma
;
10337 if (h
->type
== STT_TLS
)
10339 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
10340 if (tls_sec
!= NULL
)
10341 sym
.st_value
-= tls_sec
->vma
;
10347 BFD_ASSERT (input_sec
->owner
== NULL
10348 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
10349 sym
.st_shndx
= SHN_UNDEF
;
10350 input_sec
= bfd_und_section_ptr
;
10355 case bfd_link_hash_common
:
10356 input_sec
= h
->root
.u
.c
.p
->section
;
10357 sym
.st_shndx
= bed
->common_section_index (input_sec
);
10358 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
10361 case bfd_link_hash_indirect
:
10362 /* These symbols are created by symbol versioning. They point
10363 to the decorated version of the name. For example, if the
10364 symbol foo@@GNU_1.2 is the default, which should be used when
10365 foo is used with no version, then we add an indirect symbol
10366 foo which points to foo@@GNU_1.2. We ignore these symbols,
10367 since the indirected symbol is already in the hash table. */
10371 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
10372 switch (h
->root
.type
)
10374 case bfd_link_hash_common
:
10375 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10377 case bfd_link_hash_defined
:
10378 case bfd_link_hash_defweak
:
10379 if (bed
->common_definition (&sym
))
10380 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10384 case bfd_link_hash_undefined
:
10385 case bfd_link_hash_undefweak
:
10391 if (h
->forced_local
)
10393 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
10394 /* Turn off visibility on local symbol. */
10395 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
10397 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10398 else if (h
->unique_global
&& h
->def_regular
)
10399 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
10400 else if (h
->root
.type
== bfd_link_hash_undefweak
10401 || h
->root
.type
== bfd_link_hash_defweak
)
10402 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
10404 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
10405 sym
.st_target_internal
= h
->target_internal
;
10407 /* Give the processor backend a chance to tweak the symbol value,
10408 and also to finish up anything that needs to be done for this
10409 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10410 forced local syms when non-shared is due to a historical quirk.
10411 STT_GNU_IFUNC symbol must go through PLT. */
10412 if ((h
->type
== STT_GNU_IFUNC
10414 && !bfd_link_relocatable (flinfo
->info
))
10415 || ((h
->dynindx
!= -1
10416 || h
->forced_local
)
10417 && ((bfd_link_pic (flinfo
->info
)
10418 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
10419 || h
->root
.type
!= bfd_link_hash_undefweak
))
10420 || !h
->forced_local
)
10421 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
10423 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
10424 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
10426 eoinfo
->failed
= TRUE
;
10431 /* If we are marking the symbol as undefined, and there are no
10432 non-weak references to this symbol from a regular object, then
10433 mark the symbol as weak undefined; if there are non-weak
10434 references, mark the symbol as strong. We can't do this earlier,
10435 because it might not be marked as undefined until the
10436 finish_dynamic_symbol routine gets through with it. */
10437 if (sym
.st_shndx
== SHN_UNDEF
10439 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
10440 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
10443 type
= ELF_ST_TYPE (sym
.st_info
);
10445 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10446 if (type
== STT_GNU_IFUNC
)
10449 if (h
->ref_regular_nonweak
)
10450 bindtype
= STB_GLOBAL
;
10452 bindtype
= STB_WEAK
;
10453 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
10456 /* If this is a symbol defined in a dynamic library, don't use the
10457 symbol size from the dynamic library. Relinking an executable
10458 against a new library may introduce gratuitous changes in the
10459 executable's symbols if we keep the size. */
10460 if (sym
.st_shndx
== SHN_UNDEF
10465 /* If a non-weak symbol with non-default visibility is not defined
10466 locally, it is a fatal error. */
10467 if (!bfd_link_relocatable (flinfo
->info
)
10468 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
10469 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
10470 && h
->root
.type
== bfd_link_hash_undefined
10471 && !h
->def_regular
)
10475 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
10476 /* xgettext:c-format */
10477 msg
= _("%pB: protected symbol `%s' isn't defined");
10478 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
10479 /* xgettext:c-format */
10480 msg
= _("%pB: internal symbol `%s' isn't defined");
10482 /* xgettext:c-format */
10483 msg
= _("%pB: hidden symbol `%s' isn't defined");
10484 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
10485 bfd_set_error (bfd_error_bad_value
);
10486 eoinfo
->failed
= TRUE
;
10490 /* If this symbol should be put in the .dynsym section, then put it
10491 there now. We already know the symbol index. We also fill in
10492 the entry in the .hash section. */
10493 if (h
->dynindx
!= -1
10494 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
10495 && elf_hash_table (flinfo
->info
)->dynsym
!= NULL
10496 && !discarded_section (elf_hash_table (flinfo
->info
)->dynsym
))
10500 /* Since there is no version information in the dynamic string,
10501 if there is no version info in symbol version section, we will
10502 have a run-time problem if not linking executable, referenced
10503 by shared library, or not bound locally. */
10504 if (h
->verinfo
.verdef
== NULL
10505 && (!bfd_link_executable (flinfo
->info
)
10507 || !h
->def_regular
))
10509 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
10511 if (p
&& p
[1] != '\0')
10514 /* xgettext:c-format */
10515 (_("%pB: no symbol version section for versioned symbol `%s'"),
10516 flinfo
->output_bfd
, h
->root
.root
.string
);
10517 eoinfo
->failed
= TRUE
;
10522 sym
.st_name
= h
->dynstr_index
;
10523 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
10524 + h
->dynindx
* bed
->s
->sizeof_sym
);
10525 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
10527 eoinfo
->failed
= TRUE
;
10531 /* Inform the linker of the addition of this symbol. */
10533 if (flinfo
->info
->callbacks
->ctf_new_dynsym
)
10534 flinfo
->info
->callbacks
->ctf_new_dynsym (h
->dynindx
, &sym
);
10536 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
10538 if (flinfo
->hash_sec
!= NULL
)
10540 size_t hash_entry_size
;
10541 bfd_byte
*bucketpos
;
10543 size_t bucketcount
;
10546 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
10547 bucket
= h
->u
.elf_hash_value
% bucketcount
;
10550 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
10551 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
10552 + (bucket
+ 2) * hash_entry_size
);
10553 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
10554 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
10556 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
10557 ((bfd_byte
*) flinfo
->hash_sec
->contents
10558 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
10561 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
10563 Elf_Internal_Versym iversym
;
10564 Elf_External_Versym
*eversym
;
10566 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
10568 if (h
->verinfo
.verdef
== NULL
10569 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
10570 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
10571 iversym
.vs_vers
= 1;
10573 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
10577 if (h
->verinfo
.vertree
== NULL
)
10578 iversym
.vs_vers
= 1;
10580 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
10581 if (flinfo
->info
->create_default_symver
)
10585 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10586 defined locally. */
10587 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
10588 iversym
.vs_vers
|= VERSYM_HIDDEN
;
10590 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
10591 eversym
+= h
->dynindx
;
10592 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
10596 /* If the symbol is undefined, and we didn't output it to .dynsym,
10597 strip it from .symtab too. Obviously we can't do this for
10598 relocatable output or when needed for --emit-relocs. */
10599 else if (input_sec
== bfd_und_section_ptr
10601 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10602 && (h
->mark
!= 1 || ELF_ST_BIND (sym
.st_info
) != STB_GLOBAL
)
10603 && !bfd_link_relocatable (flinfo
->info
))
10606 /* Also strip others that we couldn't earlier due to dynamic symbol
10610 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
10613 /* Output a FILE symbol so that following locals are not associated
10614 with the wrong input file. We need one for forced local symbols
10615 if we've seen more than one FILE symbol or when we have exactly
10616 one FILE symbol but global symbols are present in a file other
10617 than the one with the FILE symbol. We also need one if linker
10618 defined symbols are present. In practice these conditions are
10619 always met, so just emit the FILE symbol unconditionally. */
10620 if (eoinfo
->localsyms
10621 && !eoinfo
->file_sym_done
10622 && eoinfo
->flinfo
->filesym_count
!= 0)
10624 Elf_Internal_Sym fsym
;
10626 memset (&fsym
, 0, sizeof (fsym
));
10627 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10628 fsym
.st_shndx
= SHN_ABS
;
10629 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
10630 bfd_und_section_ptr
, NULL
))
10633 eoinfo
->file_sym_done
= TRUE
;
10636 indx
= bfd_get_symcount (flinfo
->output_bfd
);
10637 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
10641 eoinfo
->failed
= TRUE
;
10646 else if (h
->indx
== -2)
10652 /* Return TRUE if special handling is done for relocs in SEC against
10653 symbols defined in discarded sections. */
10656 elf_section_ignore_discarded_relocs (asection
*sec
)
10658 const struct elf_backend_data
*bed
;
10660 switch (sec
->sec_info_type
)
10662 case SEC_INFO_TYPE_STABS
:
10663 case SEC_INFO_TYPE_EH_FRAME
:
10664 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10670 bed
= get_elf_backend_data (sec
->owner
);
10671 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
10672 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
10678 /* Return a mask saying how ld should treat relocations in SEC against
10679 symbols defined in discarded sections. If this function returns
10680 COMPLAIN set, ld will issue a warning message. If this function
10681 returns PRETEND set, and the discarded section was link-once and the
10682 same size as the kept link-once section, ld will pretend that the
10683 symbol was actually defined in the kept section. Otherwise ld will
10684 zero the reloc (at least that is the intent, but some cooperation by
10685 the target dependent code is needed, particularly for REL targets). */
10688 _bfd_elf_default_action_discarded (asection
*sec
)
10690 if (sec
->flags
& SEC_DEBUGGING
)
10693 if (strcmp (".eh_frame", sec
->name
) == 0)
10696 if (strcmp (".gcc_except_table", sec
->name
) == 0)
10699 return COMPLAIN
| PRETEND
;
10702 /* Find a match between a section and a member of a section group. */
10705 match_group_member (asection
*sec
, asection
*group
,
10706 struct bfd_link_info
*info
)
10708 asection
*first
= elf_next_in_group (group
);
10709 asection
*s
= first
;
10713 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
10716 s
= elf_next_in_group (s
);
10724 /* Check if the kept section of a discarded section SEC can be used
10725 to replace it. Return the replacement if it is OK. Otherwise return
10729 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
10733 kept
= sec
->kept_section
;
10736 if ((kept
->flags
& SEC_GROUP
) != 0)
10737 kept
= match_group_member (sec
, kept
, info
);
10740 if ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
10741 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
))
10745 /* Get the real kept section. */
10747 for (next
= kept
->kept_section
;
10749 next
= next
->kept_section
)
10753 sec
->kept_section
= kept
;
10758 /* Link an input file into the linker output file. This function
10759 handles all the sections and relocations of the input file at once.
10760 This is so that we only have to read the local symbols once, and
10761 don't have to keep them in memory. */
10764 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
10766 int (*relocate_section
)
10767 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
10768 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
10770 Elf_Internal_Shdr
*symtab_hdr
;
10771 size_t locsymcount
;
10773 Elf_Internal_Sym
*isymbuf
;
10774 Elf_Internal_Sym
*isym
;
10775 Elf_Internal_Sym
*isymend
;
10777 asection
**ppsection
;
10779 const struct elf_backend_data
*bed
;
10780 struct elf_link_hash_entry
**sym_hashes
;
10781 bfd_size_type address_size
;
10782 bfd_vma r_type_mask
;
10784 bfd_boolean have_file_sym
= FALSE
;
10786 output_bfd
= flinfo
->output_bfd
;
10787 bed
= get_elf_backend_data (output_bfd
);
10788 relocate_section
= bed
->elf_backend_relocate_section
;
10790 /* If this is a dynamic object, we don't want to do anything here:
10791 we don't want the local symbols, and we don't want the section
10793 if ((input_bfd
->flags
& DYNAMIC
) != 0)
10796 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
10797 if (elf_bad_symtab (input_bfd
))
10799 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10804 locsymcount
= symtab_hdr
->sh_info
;
10805 extsymoff
= symtab_hdr
->sh_info
;
10808 /* Enable GNU OSABI features in the output BFD that are used in the input
10810 if (bed
->elf_osabi
== ELFOSABI_NONE
10811 || bed
->elf_osabi
== ELFOSABI_GNU
10812 || bed
->elf_osabi
== ELFOSABI_FREEBSD
)
10813 elf_tdata (output_bfd
)->has_gnu_osabi
10814 |= (elf_tdata (input_bfd
)->has_gnu_osabi
10815 & (bfd_link_relocatable (flinfo
->info
)
10816 ? -1 : ~elf_gnu_osabi_retain
));
10818 /* Read the local symbols. */
10819 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10820 if (isymbuf
== NULL
&& locsymcount
!= 0)
10822 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
10823 flinfo
->internal_syms
,
10824 flinfo
->external_syms
,
10825 flinfo
->locsym_shndx
);
10826 if (isymbuf
== NULL
)
10830 /* Find local symbol sections and adjust values of symbols in
10831 SEC_MERGE sections. Write out those local symbols we know are
10832 going into the output file. */
10833 isymend
= isymbuf
+ locsymcount
;
10834 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
10836 isym
++, pindex
++, ppsection
++)
10840 Elf_Internal_Sym osym
;
10846 if (elf_bad_symtab (input_bfd
))
10848 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
10855 if (isym
->st_shndx
== SHN_UNDEF
)
10856 isec
= bfd_und_section_ptr
;
10857 else if (isym
->st_shndx
== SHN_ABS
)
10858 isec
= bfd_abs_section_ptr
;
10859 else if (isym
->st_shndx
== SHN_COMMON
)
10860 isec
= bfd_com_section_ptr
;
10863 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
10866 /* Don't attempt to output symbols with st_shnx in the
10867 reserved range other than SHN_ABS and SHN_COMMON. */
10868 isec
= bfd_und_section_ptr
;
10870 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
10871 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
10873 _bfd_merged_section_offset (output_bfd
, &isec
,
10874 elf_section_data (isec
)->sec_info
,
10880 /* Don't output the first, undefined, symbol. In fact, don't
10881 output any undefined local symbol. */
10882 if (isec
== bfd_und_section_ptr
)
10885 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
10887 /* We never output section symbols. Instead, we use the
10888 section symbol of the corresponding section in the output
10893 /* If we are stripping all symbols, we don't want to output this
10895 if (flinfo
->info
->strip
== strip_all
)
10898 /* If we are discarding all local symbols, we don't want to
10899 output this one. If we are generating a relocatable output
10900 file, then some of the local symbols may be required by
10901 relocs; we output them below as we discover that they are
10903 if (flinfo
->info
->discard
== discard_all
)
10906 /* If this symbol is defined in a section which we are
10907 discarding, we don't need to keep it. */
10908 if (isym
->st_shndx
!= SHN_UNDEF
10909 && isym
->st_shndx
< SHN_LORESERVE
10910 && isec
->output_section
== NULL
10911 && flinfo
->info
->non_contiguous_regions
10912 && flinfo
->info
->non_contiguous_regions_warnings
)
10914 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10915 "discards section `%s' from '%s'\n"),
10916 isec
->name
, bfd_get_filename (isec
->owner
));
10920 if (isym
->st_shndx
!= SHN_UNDEF
10921 && isym
->st_shndx
< SHN_LORESERVE
10922 && bfd_section_removed_from_list (output_bfd
,
10923 isec
->output_section
))
10926 /* Get the name of the symbol. */
10927 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
10932 /* See if we are discarding symbols with this name. */
10933 if ((flinfo
->info
->strip
== strip_some
10934 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
10936 || (((flinfo
->info
->discard
== discard_sec_merge
10937 && (isec
->flags
& SEC_MERGE
)
10938 && !bfd_link_relocatable (flinfo
->info
))
10939 || flinfo
->info
->discard
== discard_l
)
10940 && bfd_is_local_label_name (input_bfd
, name
)))
10943 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
10945 if (input_bfd
->lto_output
)
10946 /* -flto puts a temp file name here. This means builds
10947 are not reproducible. Discard the symbol. */
10949 have_file_sym
= TRUE
;
10950 flinfo
->filesym_count
+= 1;
10952 if (!have_file_sym
)
10954 /* In the absence of debug info, bfd_find_nearest_line uses
10955 FILE symbols to determine the source file for local
10956 function symbols. Provide a FILE symbol here if input
10957 files lack such, so that their symbols won't be
10958 associated with a previous input file. It's not the
10959 source file, but the best we can do. */
10960 const char *filename
;
10961 have_file_sym
= TRUE
;
10962 flinfo
->filesym_count
+= 1;
10963 memset (&osym
, 0, sizeof (osym
));
10964 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10965 osym
.st_shndx
= SHN_ABS
;
10966 if (input_bfd
->lto_output
)
10969 filename
= lbasename (bfd_get_filename (input_bfd
));
10970 if (!elf_link_output_symstrtab (flinfo
, filename
, &osym
,
10971 bfd_abs_section_ptr
, NULL
))
10977 /* Adjust the section index for the output file. */
10978 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10979 isec
->output_section
);
10980 if (osym
.st_shndx
== SHN_BAD
)
10983 /* ELF symbols in relocatable files are section relative, but
10984 in executable files they are virtual addresses. Note that
10985 this code assumes that all ELF sections have an associated
10986 BFD section with a reasonable value for output_offset; below
10987 we assume that they also have a reasonable value for
10988 output_section. Any special sections must be set up to meet
10989 these requirements. */
10990 osym
.st_value
+= isec
->output_offset
;
10991 if (!bfd_link_relocatable (flinfo
->info
))
10993 osym
.st_value
+= isec
->output_section
->vma
;
10994 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
10996 /* STT_TLS symbols are relative to PT_TLS segment base. */
10997 if (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
)
10998 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
11000 osym
.st_info
= ELF_ST_INFO (ELF_ST_BIND (osym
.st_info
),
11005 indx
= bfd_get_symcount (output_bfd
);
11006 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
11013 if (bed
->s
->arch_size
== 32)
11015 r_type_mask
= 0xff;
11021 r_type_mask
= 0xffffffff;
11026 /* Relocate the contents of each section. */
11027 sym_hashes
= elf_sym_hashes (input_bfd
);
11028 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
11030 bfd_byte
*contents
;
11032 if (! o
->linker_mark
)
11034 /* This section was omitted from the link. */
11038 if (!flinfo
->info
->resolve_section_groups
11039 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
11041 /* Deal with the group signature symbol. */
11042 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
11043 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
11044 asection
*osec
= o
->output_section
;
11046 BFD_ASSERT (bfd_link_relocatable (flinfo
->info
));
11047 if (symndx
>= locsymcount
11048 || (elf_bad_symtab (input_bfd
)
11049 && flinfo
->sections
[symndx
] == NULL
))
11051 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
11052 while (h
->root
.type
== bfd_link_hash_indirect
11053 || h
->root
.type
== bfd_link_hash_warning
)
11054 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11055 /* Arrange for symbol to be output. */
11057 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
11059 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
11061 /* We'll use the output section target_index. */
11062 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11063 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
11067 if (flinfo
->indices
[symndx
] == -1)
11069 /* Otherwise output the local symbol now. */
11070 Elf_Internal_Sym sym
= isymbuf
[symndx
];
11071 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11076 name
= bfd_elf_string_from_elf_section (input_bfd
,
11077 symtab_hdr
->sh_link
,
11082 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11084 if (sym
.st_shndx
== SHN_BAD
)
11087 sym
.st_value
+= o
->output_offset
;
11089 indx
= bfd_get_symcount (output_bfd
);
11090 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
11095 flinfo
->indices
[symndx
] = indx
;
11099 elf_section_data (osec
)->this_hdr
.sh_info
11100 = flinfo
->indices
[symndx
];
11104 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
11105 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
11108 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
11110 /* Section was created by _bfd_elf_link_create_dynamic_sections
11115 /* Get the contents of the section. They have been cached by a
11116 relaxation routine. Note that o is a section in an input
11117 file, so the contents field will not have been set by any of
11118 the routines which work on output files. */
11119 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
11121 contents
= elf_section_data (o
)->this_hdr
.contents
;
11122 if (bed
->caches_rawsize
11124 && o
->rawsize
< o
->size
)
11126 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
11127 contents
= flinfo
->contents
;
11132 contents
= flinfo
->contents
;
11133 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
11137 if ((o
->flags
& SEC_RELOC
) != 0)
11139 Elf_Internal_Rela
*internal_relocs
;
11140 Elf_Internal_Rela
*rel
, *relend
;
11141 int action_discarded
;
11144 /* Get the swapped relocs. */
11146 = _bfd_elf_link_read_relocs (input_bfd
, o
, flinfo
->external_relocs
,
11147 flinfo
->internal_relocs
, FALSE
);
11148 if (internal_relocs
== NULL
11149 && o
->reloc_count
> 0)
11152 /* We need to reverse-copy input .ctors/.dtors sections if
11153 they are placed in .init_array/.finit_array for output. */
11154 if (o
->size
> address_size
11155 && ((strncmp (o
->name
, ".ctors", 6) == 0
11156 && strcmp (o
->output_section
->name
,
11157 ".init_array") == 0)
11158 || (strncmp (o
->name
, ".dtors", 6) == 0
11159 && strcmp (o
->output_section
->name
,
11160 ".fini_array") == 0))
11161 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
11163 if (o
->size
* bed
->s
->int_rels_per_ext_rel
11164 != o
->reloc_count
* address_size
)
11167 /* xgettext:c-format */
11168 (_("error: %pB: size of section %pA is not "
11169 "multiple of address size"),
11171 bfd_set_error (bfd_error_bad_value
);
11174 o
->flags
|= SEC_ELF_REVERSE_COPY
;
11177 action_discarded
= -1;
11178 if (!elf_section_ignore_discarded_relocs (o
))
11179 action_discarded
= (*bed
->action_discarded
) (o
);
11181 /* Run through the relocs evaluating complex reloc symbols and
11182 looking for relocs against symbols from discarded sections
11183 or section symbols from removed link-once sections.
11184 Complain about relocs against discarded sections. Zero
11185 relocs against removed link-once sections. */
11187 rel
= internal_relocs
;
11188 relend
= rel
+ o
->reloc_count
;
11189 for ( ; rel
< relend
; rel
++)
11191 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
11192 unsigned int s_type
;
11193 asection
**ps
, *sec
;
11194 struct elf_link_hash_entry
*h
= NULL
;
11195 const char *sym_name
;
11197 if (r_symndx
== STN_UNDEF
)
11200 if (r_symndx
>= locsymcount
11201 || (elf_bad_symtab (input_bfd
)
11202 && flinfo
->sections
[r_symndx
] == NULL
))
11204 h
= sym_hashes
[r_symndx
- extsymoff
];
11206 /* Badly formatted input files can contain relocs that
11207 reference non-existant symbols. Check here so that
11208 we do not seg fault. */
11212 /* xgettext:c-format */
11213 (_("error: %pB contains a reloc (%#" PRIx64
") for section %pA "
11214 "that references a non-existent global symbol"),
11215 input_bfd
, (uint64_t) rel
->r_info
, o
);
11216 bfd_set_error (bfd_error_bad_value
);
11220 while (h
->root
.type
== bfd_link_hash_indirect
11221 || h
->root
.type
== bfd_link_hash_warning
)
11222 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11226 /* If a plugin symbol is referenced from a non-IR file,
11227 mark the symbol as undefined. Note that the
11228 linker may attach linker created dynamic sections
11229 to the plugin bfd. Symbols defined in linker
11230 created sections are not plugin symbols. */
11231 if ((h
->root
.non_ir_ref_regular
11232 || h
->root
.non_ir_ref_dynamic
)
11233 && (h
->root
.type
== bfd_link_hash_defined
11234 || h
->root
.type
== bfd_link_hash_defweak
)
11235 && (h
->root
.u
.def
.section
->flags
11236 & SEC_LINKER_CREATED
) == 0
11237 && h
->root
.u
.def
.section
->owner
!= NULL
11238 && (h
->root
.u
.def
.section
->owner
->flags
11239 & BFD_PLUGIN
) != 0)
11241 h
->root
.type
= bfd_link_hash_undefined
;
11242 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
11246 if (h
->root
.type
== bfd_link_hash_defined
11247 || h
->root
.type
== bfd_link_hash_defweak
)
11248 ps
= &h
->root
.u
.def
.section
;
11250 sym_name
= h
->root
.root
.string
;
11254 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
11256 s_type
= ELF_ST_TYPE (sym
->st_info
);
11257 ps
= &flinfo
->sections
[r_symndx
];
11258 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
11262 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
11263 && !bfd_link_relocatable (flinfo
->info
))
11266 bfd_vma dot
= (rel
->r_offset
11267 + o
->output_offset
+ o
->output_section
->vma
);
11269 printf ("Encountered a complex symbol!");
11270 printf (" (input_bfd %s, section %s, reloc %ld\n",
11271 bfd_get_filename (input_bfd
), o
->name
,
11272 (long) (rel
- internal_relocs
));
11273 printf (" symbol: idx %8.8lx, name %s\n",
11274 r_symndx
, sym_name
);
11275 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11276 (unsigned long) rel
->r_info
,
11277 (unsigned long) rel
->r_offset
);
11279 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
11280 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
11283 /* Symbol evaluated OK. Update to absolute value. */
11284 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
11289 if (action_discarded
!= -1 && ps
!= NULL
)
11291 /* Complain if the definition comes from a
11292 discarded section. */
11293 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
11295 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
11296 if (action_discarded
& COMPLAIN
)
11297 (*flinfo
->info
->callbacks
->einfo
)
11298 /* xgettext:c-format */
11299 (_("%X`%s' referenced in section `%pA' of %pB: "
11300 "defined in discarded section `%pA' of %pB\n"),
11301 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
11303 /* Try to do the best we can to support buggy old
11304 versions of gcc. Pretend that the symbol is
11305 really defined in the kept linkonce section.
11306 FIXME: This is quite broken. Modifying the
11307 symbol here means we will be changing all later
11308 uses of the symbol, not just in this section. */
11309 if (action_discarded
& PRETEND
)
11313 kept
= _bfd_elf_check_kept_section (sec
,
11325 /* Relocate the section by invoking a back end routine.
11327 The back end routine is responsible for adjusting the
11328 section contents as necessary, and (if using Rela relocs
11329 and generating a relocatable output file) adjusting the
11330 reloc addend as necessary.
11332 The back end routine does not have to worry about setting
11333 the reloc address or the reloc symbol index.
11335 The back end routine is given a pointer to the swapped in
11336 internal symbols, and can access the hash table entries
11337 for the external symbols via elf_sym_hashes (input_bfd).
11339 When generating relocatable output, the back end routine
11340 must handle STB_LOCAL/STT_SECTION symbols specially. The
11341 output symbol is going to be a section symbol
11342 corresponding to the output section, which will require
11343 the addend to be adjusted. */
11345 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
11346 input_bfd
, o
, contents
,
11354 || bfd_link_relocatable (flinfo
->info
)
11355 || flinfo
->info
->emitrelocations
)
11357 Elf_Internal_Rela
*irela
;
11358 Elf_Internal_Rela
*irelaend
, *irelamid
;
11359 bfd_vma last_offset
;
11360 struct elf_link_hash_entry
**rel_hash
;
11361 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
11362 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
11363 unsigned int next_erel
;
11364 bfd_boolean rela_normal
;
11365 struct bfd_elf_section_data
*esdi
, *esdo
;
11367 esdi
= elf_section_data (o
);
11368 esdo
= elf_section_data (o
->output_section
);
11369 rela_normal
= FALSE
;
11371 /* Adjust the reloc addresses and symbol indices. */
11373 irela
= internal_relocs
;
11374 irelaend
= irela
+ o
->reloc_count
;
11375 rel_hash
= esdo
->rel
.hashes
+ esdo
->rel
.count
;
11376 /* We start processing the REL relocs, if any. When we reach
11377 IRELAMID in the loop, we switch to the RELA relocs. */
11379 if (esdi
->rel
.hdr
!= NULL
)
11380 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
11381 * bed
->s
->int_rels_per_ext_rel
);
11382 rel_hash_list
= rel_hash
;
11383 rela_hash_list
= NULL
;
11384 last_offset
= o
->output_offset
;
11385 if (!bfd_link_relocatable (flinfo
->info
))
11386 last_offset
+= o
->output_section
->vma
;
11387 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
11389 unsigned long r_symndx
;
11391 Elf_Internal_Sym sym
;
11393 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
11399 if (irela
== irelamid
)
11401 rel_hash
= esdo
->rela
.hashes
+ esdo
->rela
.count
;
11402 rela_hash_list
= rel_hash
;
11403 rela_normal
= bed
->rela_normal
;
11406 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
11409 if (irela
->r_offset
>= (bfd_vma
) -2)
11411 /* This is a reloc for a deleted entry or somesuch.
11412 Turn it into an R_*_NONE reloc, at the same
11413 offset as the last reloc. elf_eh_frame.c and
11414 bfd_elf_discard_info rely on reloc offsets
11416 irela
->r_offset
= last_offset
;
11418 irela
->r_addend
= 0;
11422 irela
->r_offset
+= o
->output_offset
;
11424 /* Relocs in an executable have to be virtual addresses. */
11425 if (!bfd_link_relocatable (flinfo
->info
))
11426 irela
->r_offset
+= o
->output_section
->vma
;
11428 last_offset
= irela
->r_offset
;
11430 r_symndx
= irela
->r_info
>> r_sym_shift
;
11431 if (r_symndx
== STN_UNDEF
)
11434 if (r_symndx
>= locsymcount
11435 || (elf_bad_symtab (input_bfd
)
11436 && flinfo
->sections
[r_symndx
] == NULL
))
11438 struct elf_link_hash_entry
*rh
;
11439 unsigned long indx
;
11441 /* This is a reloc against a global symbol. We
11442 have not yet output all the local symbols, so
11443 we do not know the symbol index of any global
11444 symbol. We set the rel_hash entry for this
11445 reloc to point to the global hash table entry
11446 for this symbol. The symbol index is then
11447 set at the end of bfd_elf_final_link. */
11448 indx
= r_symndx
- extsymoff
;
11449 rh
= elf_sym_hashes (input_bfd
)[indx
];
11450 while (rh
->root
.type
== bfd_link_hash_indirect
11451 || rh
->root
.type
== bfd_link_hash_warning
)
11452 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
11454 /* Setting the index to -2 tells
11455 elf_link_output_extsym that this symbol is
11456 used by a reloc. */
11457 BFD_ASSERT (rh
->indx
< 0);
11464 /* This is a reloc against a local symbol. */
11467 sym
= isymbuf
[r_symndx
];
11468 sec
= flinfo
->sections
[r_symndx
];
11469 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
11471 /* I suppose the backend ought to fill in the
11472 section of any STT_SECTION symbol against a
11473 processor specific section. */
11474 r_symndx
= STN_UNDEF
;
11475 if (bfd_is_abs_section (sec
))
11477 else if (sec
== NULL
|| sec
->owner
== NULL
)
11479 bfd_set_error (bfd_error_bad_value
);
11484 asection
*osec
= sec
->output_section
;
11486 /* If we have discarded a section, the output
11487 section will be the absolute section. In
11488 case of discarded SEC_MERGE sections, use
11489 the kept section. relocate_section should
11490 have already handled discarded linkonce
11492 if (bfd_is_abs_section (osec
)
11493 && sec
->kept_section
!= NULL
11494 && sec
->kept_section
->output_section
!= NULL
)
11496 osec
= sec
->kept_section
->output_section
;
11497 irela
->r_addend
-= osec
->vma
;
11500 if (!bfd_is_abs_section (osec
))
11502 r_symndx
= osec
->target_index
;
11503 if (r_symndx
== STN_UNDEF
)
11505 irela
->r_addend
+= osec
->vma
;
11506 osec
= _bfd_nearby_section (output_bfd
, osec
,
11508 irela
->r_addend
-= osec
->vma
;
11509 r_symndx
= osec
->target_index
;
11514 /* Adjust the addend according to where the
11515 section winds up in the output section. */
11517 irela
->r_addend
+= sec
->output_offset
;
11521 if (flinfo
->indices
[r_symndx
] == -1)
11523 unsigned long shlink
;
11528 if (flinfo
->info
->strip
== strip_all
)
11530 /* You can't do ld -r -s. */
11531 bfd_set_error (bfd_error_invalid_operation
);
11535 /* This symbol was skipped earlier, but
11536 since it is needed by a reloc, we
11537 must output it now. */
11538 shlink
= symtab_hdr
->sh_link
;
11539 name
= (bfd_elf_string_from_elf_section
11540 (input_bfd
, shlink
, sym
.st_name
));
11544 osec
= sec
->output_section
;
11546 _bfd_elf_section_from_bfd_section (output_bfd
,
11548 if (sym
.st_shndx
== SHN_BAD
)
11551 sym
.st_value
+= sec
->output_offset
;
11552 if (!bfd_link_relocatable (flinfo
->info
))
11554 sym
.st_value
+= osec
->vma
;
11555 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
11557 struct elf_link_hash_table
*htab
11558 = elf_hash_table (flinfo
->info
);
11560 /* STT_TLS symbols are relative to PT_TLS
11562 if (htab
->tls_sec
!= NULL
)
11563 sym
.st_value
-= htab
->tls_sec
->vma
;
11566 = ELF_ST_INFO (ELF_ST_BIND (sym
.st_info
),
11571 indx
= bfd_get_symcount (output_bfd
);
11572 ret
= elf_link_output_symstrtab (flinfo
, name
,
11578 flinfo
->indices
[r_symndx
] = indx
;
11583 r_symndx
= flinfo
->indices
[r_symndx
];
11586 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
11587 | (irela
->r_info
& r_type_mask
));
11590 /* Swap out the relocs. */
11591 input_rel_hdr
= esdi
->rel
.hdr
;
11592 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
11594 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11599 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
11600 * bed
->s
->int_rels_per_ext_rel
);
11601 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
11604 input_rela_hdr
= esdi
->rela
.hdr
;
11605 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
11607 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11616 /* Write out the modified section contents. */
11617 if (bed
->elf_backend_write_section
11618 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
11621 /* Section written out. */
11623 else switch (o
->sec_info_type
)
11625 case SEC_INFO_TYPE_STABS
:
11626 if (! (_bfd_write_section_stabs
11628 &elf_hash_table (flinfo
->info
)->stab_info
,
11629 o
, &elf_section_data (o
)->sec_info
, contents
)))
11632 case SEC_INFO_TYPE_MERGE
:
11633 if (! _bfd_write_merged_section (output_bfd
, o
,
11634 elf_section_data (o
)->sec_info
))
11637 case SEC_INFO_TYPE_EH_FRAME
:
11639 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
11644 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
11646 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
11654 if (! (o
->flags
& SEC_EXCLUDE
))
11656 file_ptr offset
= (file_ptr
) o
->output_offset
;
11657 bfd_size_type todo
= o
->size
;
11659 offset
*= bfd_octets_per_byte (output_bfd
, o
);
11661 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
11663 /* Reverse-copy input section to output. */
11666 todo
-= address_size
;
11667 if (! bfd_set_section_contents (output_bfd
,
11675 offset
+= address_size
;
11679 else if (! bfd_set_section_contents (output_bfd
,
11693 /* Generate a reloc when linking an ELF file. This is a reloc
11694 requested by the linker, and does not come from any input file. This
11695 is used to build constructor and destructor tables when linking
11699 elf_reloc_link_order (bfd
*output_bfd
,
11700 struct bfd_link_info
*info
,
11701 asection
*output_section
,
11702 struct bfd_link_order
*link_order
)
11704 reloc_howto_type
*howto
;
11708 struct bfd_elf_section_reloc_data
*reldata
;
11709 struct elf_link_hash_entry
**rel_hash_ptr
;
11710 Elf_Internal_Shdr
*rel_hdr
;
11711 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
11712 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
11715 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
11717 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
11720 bfd_set_error (bfd_error_bad_value
);
11724 addend
= link_order
->u
.reloc
.p
->addend
;
11727 reldata
= &esdo
->rel
;
11728 else if (esdo
->rela
.hdr
)
11729 reldata
= &esdo
->rela
;
11736 /* Figure out the symbol index. */
11737 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
11738 if (link_order
->type
== bfd_section_reloc_link_order
)
11740 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
11741 BFD_ASSERT (indx
!= 0);
11742 *rel_hash_ptr
= NULL
;
11746 struct elf_link_hash_entry
*h
;
11748 /* Treat a reloc against a defined symbol as though it were
11749 actually against the section. */
11750 h
= ((struct elf_link_hash_entry
*)
11751 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
11752 link_order
->u
.reloc
.p
->u
.name
,
11753 FALSE
, FALSE
, TRUE
));
11755 && (h
->root
.type
== bfd_link_hash_defined
11756 || h
->root
.type
== bfd_link_hash_defweak
))
11760 section
= h
->root
.u
.def
.section
;
11761 indx
= section
->output_section
->target_index
;
11762 *rel_hash_ptr
= NULL
;
11763 /* It seems that we ought to add the symbol value to the
11764 addend here, but in practice it has already been added
11765 because it was passed to constructor_callback. */
11766 addend
+= section
->output_section
->vma
+ section
->output_offset
;
11768 else if (h
!= NULL
)
11770 /* Setting the index to -2 tells elf_link_output_extsym that
11771 this symbol is used by a reloc. */
11778 (*info
->callbacks
->unattached_reloc
)
11779 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
11784 /* If this is an inplace reloc, we must write the addend into the
11786 if (howto
->partial_inplace
&& addend
!= 0)
11788 bfd_size_type size
;
11789 bfd_reloc_status_type rstat
;
11792 const char *sym_name
;
11793 bfd_size_type octets
;
11795 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
11796 buf
= (bfd_byte
*) bfd_zmalloc (size
);
11797 if (buf
== NULL
&& size
!= 0)
11799 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
11806 case bfd_reloc_outofrange
:
11809 case bfd_reloc_overflow
:
11810 if (link_order
->type
== bfd_section_reloc_link_order
)
11811 sym_name
= bfd_section_name (link_order
->u
.reloc
.p
->u
.section
);
11813 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
11814 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
11815 howto
->name
, addend
, NULL
, NULL
,
11820 octets
= link_order
->offset
* bfd_octets_per_byte (output_bfd
,
11822 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
11829 /* The address of a reloc is relative to the section in a
11830 relocatable file, and is a virtual address in an executable
11832 offset
= link_order
->offset
;
11833 if (! bfd_link_relocatable (info
))
11834 offset
+= output_section
->vma
;
11836 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
11838 irel
[i
].r_offset
= offset
;
11839 irel
[i
].r_info
= 0;
11840 irel
[i
].r_addend
= 0;
11842 if (bed
->s
->arch_size
== 32)
11843 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
11845 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
11847 rel_hdr
= reldata
->hdr
;
11848 erel
= rel_hdr
->contents
;
11849 if (rel_hdr
->sh_type
== SHT_REL
)
11851 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
11852 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
11856 irel
[0].r_addend
= addend
;
11857 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
11858 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
11866 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11867 Returns TRUE upon success, FALSE otherwise. */
11870 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
11872 bfd_boolean ret
= FALSE
;
11874 const struct elf_backend_data
*bed
;
11876 enum bfd_architecture arch
;
11878 asymbol
**sympp
= NULL
;
11882 elf_symbol_type
*osymbuf
;
11885 implib_bfd
= info
->out_implib_bfd
;
11886 bed
= get_elf_backend_data (abfd
);
11888 if (!bfd_set_format (implib_bfd
, bfd_object
))
11891 /* Use flag from executable but make it a relocatable object. */
11892 flags
= bfd_get_file_flags (abfd
);
11893 flags
&= ~HAS_RELOC
;
11894 if (!bfd_set_start_address (implib_bfd
, 0)
11895 || !bfd_set_file_flags (implib_bfd
, flags
& ~EXEC_P
))
11898 /* Copy architecture of output file to import library file. */
11899 arch
= bfd_get_arch (abfd
);
11900 mach
= bfd_get_mach (abfd
);
11901 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
11902 && (abfd
->target_defaulted
11903 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
11906 /* Get symbol table size. */
11907 symsize
= bfd_get_symtab_upper_bound (abfd
);
11911 /* Read in the symbol table. */
11912 sympp
= (asymbol
**) bfd_malloc (symsize
);
11916 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
11920 /* Allow the BFD backend to copy any private header data it
11921 understands from the output BFD to the import library BFD. */
11922 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
11925 /* Filter symbols to appear in the import library. */
11926 if (bed
->elf_backend_filter_implib_symbols
)
11927 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
11930 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
11933 bfd_set_error (bfd_error_no_symbols
);
11934 _bfd_error_handler (_("%pB: no symbol found for import library"),
11940 /* Make symbols absolute. */
11941 amt
= symcount
* sizeof (*osymbuf
);
11942 osymbuf
= (elf_symbol_type
*) bfd_alloc (implib_bfd
, amt
);
11943 if (osymbuf
== NULL
)
11946 for (src_count
= 0; src_count
< symcount
; src_count
++)
11948 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
11949 sizeof (*osymbuf
));
11950 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
11951 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
11952 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
11953 osymbuf
[src_count
].internal_elf_sym
.st_value
=
11954 osymbuf
[src_count
].symbol
.value
;
11955 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
11958 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
11960 /* Allow the BFD backend to copy any private data it understands
11961 from the output BFD to the import library BFD. This is done last
11962 to permit the routine to look at the filtered symbol table. */
11963 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
11966 if (!bfd_close (implib_bfd
))
11977 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
11981 if (flinfo
->symstrtab
!= NULL
)
11982 _bfd_elf_strtab_free (flinfo
->symstrtab
);
11983 free (flinfo
->contents
);
11984 free (flinfo
->external_relocs
);
11985 free (flinfo
->internal_relocs
);
11986 free (flinfo
->external_syms
);
11987 free (flinfo
->locsym_shndx
);
11988 free (flinfo
->internal_syms
);
11989 free (flinfo
->indices
);
11990 free (flinfo
->sections
);
11991 if (flinfo
->symshndxbuf
!= (Elf_External_Sym_Shndx
*) -1)
11992 free (flinfo
->symshndxbuf
);
11993 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
11995 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11996 free (esdo
->rel
.hashes
);
11997 free (esdo
->rela
.hashes
);
12001 /* Do the final step of an ELF link. */
12004 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
12006 bfd_boolean dynamic
;
12007 bfd_boolean emit_relocs
;
12009 struct elf_final_link_info flinfo
;
12011 struct bfd_link_order
*p
;
12013 bfd_size_type max_contents_size
;
12014 bfd_size_type max_external_reloc_size
;
12015 bfd_size_type max_internal_reloc_count
;
12016 bfd_size_type max_sym_count
;
12017 bfd_size_type max_sym_shndx_count
;
12018 Elf_Internal_Sym elfsym
;
12020 Elf_Internal_Shdr
*symtab_hdr
;
12021 Elf_Internal_Shdr
*symtab_shndx_hdr
;
12022 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12023 struct elf_outext_info eoinfo
;
12024 bfd_boolean merged
;
12025 size_t relativecount
= 0;
12026 asection
*reldyn
= 0;
12028 asection
*attr_section
= NULL
;
12029 bfd_vma attr_size
= 0;
12030 const char *std_attrs_section
;
12031 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
12032 bfd_boolean sections_removed
;
12035 if (!is_elf_hash_table (htab
))
12038 if (bfd_link_pic (info
))
12039 abfd
->flags
|= DYNAMIC
;
12041 dynamic
= htab
->dynamic_sections_created
;
12042 dynobj
= htab
->dynobj
;
12044 emit_relocs
= (bfd_link_relocatable (info
)
12045 || info
->emitrelocations
);
12047 memset (&flinfo
, 0, sizeof (flinfo
));
12048 flinfo
.info
= info
;
12049 flinfo
.output_bfd
= abfd
;
12050 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
12051 if (flinfo
.symstrtab
== NULL
)
12056 flinfo
.hash_sec
= NULL
;
12057 flinfo
.symver_sec
= NULL
;
12061 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
12062 /* Note that dynsym_sec can be NULL (on VMS). */
12063 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
12064 /* Note that it is OK if symver_sec is NULL. */
12067 if (info
->unique_symbol
12068 && !bfd_hash_table_init (&flinfo
.local_hash_table
,
12069 local_hash_newfunc
,
12070 sizeof (struct local_hash_entry
)))
12073 /* The object attributes have been merged. Remove the input
12074 sections from the link, and set the contents of the output
12076 sections_removed
= FALSE
;
12077 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
12078 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12080 bfd_boolean remove_section
= FALSE
;
12082 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
12083 || strcmp (o
->name
, ".gnu.attributes") == 0)
12085 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12087 asection
*input_section
;
12089 if (p
->type
!= bfd_indirect_link_order
)
12091 input_section
= p
->u
.indirect
.section
;
12092 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12093 elf_link_input_bfd ignores this section. */
12094 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
12097 attr_size
= bfd_elf_obj_attr_size (abfd
);
12098 bfd_set_section_size (o
, attr_size
);
12099 /* Skip this section later on. */
12100 o
->map_head
.link_order
= NULL
;
12104 remove_section
= TRUE
;
12106 else if ((o
->flags
& SEC_GROUP
) != 0 && o
->size
== 0)
12108 /* Remove empty group section from linker output. */
12109 remove_section
= TRUE
;
12111 if (remove_section
)
12113 o
->flags
|= SEC_EXCLUDE
;
12114 bfd_section_list_remove (abfd
, o
);
12115 abfd
->section_count
--;
12116 sections_removed
= TRUE
;
12119 if (sections_removed
)
12120 _bfd_fix_excluded_sec_syms (abfd
, info
);
12122 /* Count up the number of relocations we will output for each output
12123 section, so that we know the sizes of the reloc sections. We
12124 also figure out some maximum sizes. */
12125 max_contents_size
= 0;
12126 max_external_reloc_size
= 0;
12127 max_internal_reloc_count
= 0;
12129 max_sym_shndx_count
= 0;
12131 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12133 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12134 o
->reloc_count
= 0;
12136 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12138 unsigned int reloc_count
= 0;
12139 unsigned int additional_reloc_count
= 0;
12140 struct bfd_elf_section_data
*esdi
= NULL
;
12142 if (p
->type
== bfd_section_reloc_link_order
12143 || p
->type
== bfd_symbol_reloc_link_order
)
12145 else if (p
->type
== bfd_indirect_link_order
)
12149 sec
= p
->u
.indirect
.section
;
12151 /* Mark all sections which are to be included in the
12152 link. This will normally be every section. We need
12153 to do this so that we can identify any sections which
12154 the linker has decided to not include. */
12155 sec
->linker_mark
= TRUE
;
12157 if (sec
->flags
& SEC_MERGE
)
12160 if (sec
->rawsize
> max_contents_size
)
12161 max_contents_size
= sec
->rawsize
;
12162 if (sec
->size
> max_contents_size
)
12163 max_contents_size
= sec
->size
;
12165 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
12166 && (sec
->owner
->flags
& DYNAMIC
) == 0)
12170 /* We are interested in just local symbols, not all
12172 if (elf_bad_symtab (sec
->owner
))
12173 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
12174 / bed
->s
->sizeof_sym
);
12176 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
12178 if (sym_count
> max_sym_count
)
12179 max_sym_count
= sym_count
;
12181 if (sym_count
> max_sym_shndx_count
12182 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
12183 max_sym_shndx_count
= sym_count
;
12185 if (esdo
->this_hdr
.sh_type
== SHT_REL
12186 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
12187 /* Some backends use reloc_count in relocation sections
12188 to count particular types of relocs. Of course,
12189 reloc sections themselves can't have relocations. */
12191 else if (emit_relocs
)
12193 reloc_count
= sec
->reloc_count
;
12194 if (bed
->elf_backend_count_additional_relocs
)
12197 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
12198 additional_reloc_count
+= c
;
12201 else if (bed
->elf_backend_count_relocs
)
12202 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
12204 esdi
= elf_section_data (sec
);
12206 if ((sec
->flags
& SEC_RELOC
) != 0)
12208 size_t ext_size
= 0;
12210 if (esdi
->rel
.hdr
!= NULL
)
12211 ext_size
= esdi
->rel
.hdr
->sh_size
;
12212 if (esdi
->rela
.hdr
!= NULL
)
12213 ext_size
+= esdi
->rela
.hdr
->sh_size
;
12215 if (ext_size
> max_external_reloc_size
)
12216 max_external_reloc_size
= ext_size
;
12217 if (sec
->reloc_count
> max_internal_reloc_count
)
12218 max_internal_reloc_count
= sec
->reloc_count
;
12223 if (reloc_count
== 0)
12226 reloc_count
+= additional_reloc_count
;
12227 o
->reloc_count
+= reloc_count
;
12229 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
12233 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
12234 esdo
->rel
.count
+= additional_reloc_count
;
12236 if (esdi
->rela
.hdr
)
12238 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
12239 esdo
->rela
.count
+= additional_reloc_count
;
12245 esdo
->rela
.count
+= reloc_count
;
12247 esdo
->rel
.count
+= reloc_count
;
12251 if (o
->reloc_count
> 0)
12252 o
->flags
|= SEC_RELOC
;
12255 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12256 set it (this is probably a bug) and if it is set
12257 assign_section_numbers will create a reloc section. */
12258 o
->flags
&=~ SEC_RELOC
;
12261 /* If the SEC_ALLOC flag is not set, force the section VMA to
12262 zero. This is done in elf_fake_sections as well, but forcing
12263 the VMA to 0 here will ensure that relocs against these
12264 sections are handled correctly. */
12265 if ((o
->flags
& SEC_ALLOC
) == 0
12266 && ! o
->user_set_vma
)
12270 if (! bfd_link_relocatable (info
) && merged
)
12271 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
12273 /* Figure out the file positions for everything but the symbol table
12274 and the relocs. We set symcount to force assign_section_numbers
12275 to create a symbol table. */
12276 abfd
->symcount
= info
->strip
!= strip_all
|| emit_relocs
;
12277 BFD_ASSERT (! abfd
->output_has_begun
);
12278 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
12281 /* Set sizes, and assign file positions for reloc sections. */
12282 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12284 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12285 if ((o
->flags
& SEC_RELOC
) != 0)
12288 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
12292 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
12296 /* _bfd_elf_compute_section_file_positions makes temporary use
12297 of target_index. Reset it. */
12298 o
->target_index
= 0;
12300 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12301 to count upwards while actually outputting the relocations. */
12302 esdo
->rel
.count
= 0;
12303 esdo
->rela
.count
= 0;
12305 if ((esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
12306 && !bfd_section_is_ctf (o
))
12308 /* Cache the section contents so that they can be compressed
12309 later. Use bfd_malloc since it will be freed by
12310 bfd_compress_section_contents. */
12311 unsigned char *contents
= esdo
->this_hdr
.contents
;
12312 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
12315 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
12316 if (contents
== NULL
)
12318 esdo
->this_hdr
.contents
= contents
;
12322 /* We have now assigned file positions for all the sections except .symtab,
12323 .strtab, and non-loaded reloc and compressed debugging sections. We start
12324 the .symtab section at the current file position, and write directly to it.
12325 We build the .strtab section in memory. */
12326 abfd
->symcount
= 0;
12327 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12328 /* sh_name is set in prep_headers. */
12329 symtab_hdr
->sh_type
= SHT_SYMTAB
;
12330 /* sh_flags, sh_addr and sh_size all start off zero. */
12331 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
12332 /* sh_link is set in assign_section_numbers. */
12333 /* sh_info is set below. */
12334 /* sh_offset is set just below. */
12335 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
12337 if (max_sym_count
< 20)
12338 max_sym_count
= 20;
12339 htab
->strtabsize
= max_sym_count
;
12340 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
12341 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
12342 if (htab
->strtab
== NULL
)
12344 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12346 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
12347 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
12349 if (info
->strip
!= strip_all
|| emit_relocs
)
12351 file_ptr off
= elf_next_file_pos (abfd
);
12353 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
12355 /* Note that at this point elf_next_file_pos (abfd) is
12356 incorrect. We do not yet know the size of the .symtab section.
12357 We correct next_file_pos below, after we do know the size. */
12359 /* Start writing out the symbol table. The first symbol is always a
12361 elfsym
.st_value
= 0;
12362 elfsym
.st_size
= 0;
12363 elfsym
.st_info
= 0;
12364 elfsym
.st_other
= 0;
12365 elfsym
.st_shndx
= SHN_UNDEF
;
12366 elfsym
.st_target_internal
= 0;
12367 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
12368 bfd_und_section_ptr
, NULL
) != 1)
12371 /* Output a symbol for each section if asked or they are used for
12372 relocs. These symbols usually have no names. We store the
12373 index of each one in the index field of the section, so that
12374 we can find it again when outputting relocs. */
12376 if (bfd_keep_unused_section_symbols (abfd
) || emit_relocs
)
12378 bfd_boolean name_local_sections
12379 = (bed
->elf_backend_name_local_section_symbols
12380 && bed
->elf_backend_name_local_section_symbols (abfd
));
12381 const char *name
= NULL
;
12383 elfsym
.st_size
= 0;
12384 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12385 elfsym
.st_other
= 0;
12386 elfsym
.st_value
= 0;
12387 elfsym
.st_target_internal
= 0;
12388 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12390 o
= bfd_section_from_elf_index (abfd
, i
);
12393 o
->target_index
= bfd_get_symcount (abfd
);
12394 elfsym
.st_shndx
= i
;
12395 if (!bfd_link_relocatable (info
))
12396 elfsym
.st_value
= o
->vma
;
12397 if (name_local_sections
)
12399 if (elf_link_output_symstrtab (&flinfo
, name
, &elfsym
, o
,
12407 /* On some targets like Irix 5 the symbol split between local and global
12408 ones recorded in the sh_info field needs to be done between section
12409 and all other symbols. */
12410 if (bed
->elf_backend_elfsym_local_is_section
12411 && bed
->elf_backend_elfsym_local_is_section (abfd
))
12412 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12414 /* Allocate some memory to hold information read in from the input
12416 if (max_contents_size
!= 0)
12418 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
12419 if (flinfo
.contents
== NULL
)
12423 if (max_external_reloc_size
!= 0)
12425 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
12426 if (flinfo
.external_relocs
== NULL
)
12430 if (max_internal_reloc_count
!= 0)
12432 amt
= max_internal_reloc_count
* sizeof (Elf_Internal_Rela
);
12433 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
12434 if (flinfo
.internal_relocs
== NULL
)
12438 if (max_sym_count
!= 0)
12440 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
12441 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
12442 if (flinfo
.external_syms
== NULL
)
12445 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
12446 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
12447 if (flinfo
.internal_syms
== NULL
)
12450 amt
= max_sym_count
* sizeof (long);
12451 flinfo
.indices
= (long int *) bfd_malloc (amt
);
12452 if (flinfo
.indices
== NULL
)
12455 amt
= max_sym_count
* sizeof (asection
*);
12456 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
12457 if (flinfo
.sections
== NULL
)
12461 if (max_sym_shndx_count
!= 0)
12463 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
12464 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
12465 if (flinfo
.locsym_shndx
== NULL
)
12471 bfd_vma base
, end
= 0; /* Both bytes. */
12474 for (sec
= htab
->tls_sec
;
12475 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
12478 bfd_size_type size
= sec
->size
;
12479 unsigned int opb
= bfd_octets_per_byte (abfd
, sec
);
12482 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
12484 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
12487 size
= ord
->offset
* opb
+ ord
->size
;
12489 end
= sec
->vma
+ size
/ opb
;
12491 base
= htab
->tls_sec
->vma
;
12492 /* Only align end of TLS section if static TLS doesn't have special
12493 alignment requirements. */
12494 if (bed
->static_tls_alignment
== 1)
12495 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
12496 htab
->tls_size
= end
- base
;
12499 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
12502 /* Since ELF permits relocations to be against local symbols, we
12503 must have the local symbols available when we do the relocations.
12504 Since we would rather only read the local symbols once, and we
12505 would rather not keep them in memory, we handle all the
12506 relocations for a single input file at the same time.
12508 Unfortunately, there is no way to know the total number of local
12509 symbols until we have seen all of them, and the local symbol
12510 indices precede the global symbol indices. This means that when
12511 we are generating relocatable output, and we see a reloc against
12512 a global symbol, we can not know the symbol index until we have
12513 finished examining all the local symbols to see which ones we are
12514 going to output. To deal with this, we keep the relocations in
12515 memory, and don't output them until the end of the link. This is
12516 an unfortunate waste of memory, but I don't see a good way around
12517 it. Fortunately, it only happens when performing a relocatable
12518 link, which is not the common case. FIXME: If keep_memory is set
12519 we could write the relocs out and then read them again; I don't
12520 know how bad the memory loss will be. */
12522 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12523 sub
->output_has_begun
= FALSE
;
12524 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12526 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12528 if (p
->type
== bfd_indirect_link_order
12529 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
12530 == bfd_target_elf_flavour
)
12531 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
12533 if (! sub
->output_has_begun
)
12535 if (! elf_link_input_bfd (&flinfo
, sub
))
12537 sub
->output_has_begun
= TRUE
;
12540 else if (p
->type
== bfd_section_reloc_link_order
12541 || p
->type
== bfd_symbol_reloc_link_order
)
12543 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
12548 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
12550 if (p
->type
== bfd_indirect_link_order
12551 && (bfd_get_flavour (sub
)
12552 == bfd_target_elf_flavour
)
12553 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
12554 != bed
->s
->elfclass
))
12556 const char *iclass
, *oclass
;
12558 switch (bed
->s
->elfclass
)
12560 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
12561 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
12562 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
12566 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
12568 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
12569 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
12570 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
12574 bfd_set_error (bfd_error_wrong_format
);
12576 /* xgettext:c-format */
12577 (_("%pB: file class %s incompatible with %s"),
12578 sub
, iclass
, oclass
);
12587 /* Free symbol buffer if needed. */
12588 if (!info
->reduce_memory_overheads
)
12590 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12591 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
12593 free (elf_tdata (sub
)->symbuf
);
12594 elf_tdata (sub
)->symbuf
= NULL
;
12600 /* Output any global symbols that got converted to local in a
12601 version script or due to symbol visibility. We do this in a
12602 separate step since ELF requires all local symbols to appear
12603 prior to any global symbols. FIXME: We should only do this if
12604 some global symbols were, in fact, converted to become local.
12605 FIXME: Will this work correctly with the Irix 5 linker? */
12606 eoinfo
.failed
= FALSE
;
12607 eoinfo
.flinfo
= &flinfo
;
12608 eoinfo
.localsyms
= TRUE
;
12609 eoinfo
.file_sym_done
= FALSE
;
12610 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12614 goto return_local_hash_table
;
12617 /* If backend needs to output some local symbols not present in the hash
12618 table, do it now. */
12619 if (bed
->elf_backend_output_arch_local_syms
12620 && (info
->strip
!= strip_all
|| emit_relocs
))
12622 typedef int (*out_sym_func
)
12623 (void *, const char *, Elf_Internal_Sym
*, asection
*,
12624 struct elf_link_hash_entry
*);
12626 if (! ((*bed
->elf_backend_output_arch_local_syms
)
12627 (abfd
, info
, &flinfo
,
12628 (out_sym_func
) elf_link_output_symstrtab
)))
12631 goto return_local_hash_table
;
12635 /* That wrote out all the local symbols. Finish up the symbol table
12636 with the global symbols. Even if we want to strip everything we
12637 can, we still need to deal with those global symbols that got
12638 converted to local in a version script. */
12640 /* The sh_info field records the index of the first non local symbol. */
12641 if (!symtab_hdr
->sh_info
)
12642 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12645 && htab
->dynsym
!= NULL
12646 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
12648 Elf_Internal_Sym sym
;
12649 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
12651 o
= htab
->dynsym
->output_section
;
12652 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
12654 /* Write out the section symbols for the output sections. */
12655 if (bfd_link_pic (info
)
12656 || htab
->is_relocatable_executable
)
12662 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12664 sym
.st_target_internal
= 0;
12666 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
12672 dynindx
= elf_section_data (s
)->dynindx
;
12675 indx
= elf_section_data (s
)->this_idx
;
12676 BFD_ASSERT (indx
> 0);
12677 sym
.st_shndx
= indx
;
12678 if (! check_dynsym (abfd
, &sym
))
12681 goto return_local_hash_table
;
12683 sym
.st_value
= s
->vma
;
12684 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
12686 /* Inform the linker of the addition of this symbol. */
12688 if (info
->callbacks
->ctf_new_dynsym
)
12689 info
->callbacks
->ctf_new_dynsym (dynindx
, &sym
);
12691 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12695 /* Write out the local dynsyms. */
12696 if (htab
->dynlocal
)
12698 struct elf_link_local_dynamic_entry
*e
;
12699 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
12704 /* Copy the internal symbol and turn off visibility.
12705 Note that we saved a word of storage and overwrote
12706 the original st_name with the dynstr_index. */
12708 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
12709 sym
.st_shndx
= SHN_UNDEF
;
12711 s
= bfd_section_from_elf_index (e
->input_bfd
,
12714 && s
->output_section
!= NULL
12715 && elf_section_data (s
->output_section
) != NULL
)
12718 elf_section_data (s
->output_section
)->this_idx
;
12719 if (! check_dynsym (abfd
, &sym
))
12722 goto return_local_hash_table
;
12724 sym
.st_value
= (s
->output_section
->vma
12726 + e
->isym
.st_value
);
12729 /* Inform the linker of the addition of this symbol. */
12731 if (info
->callbacks
->ctf_new_dynsym
)
12732 info
->callbacks
->ctf_new_dynsym (e
->dynindx
, &sym
);
12734 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
12735 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12740 /* We get the global symbols from the hash table. */
12741 eoinfo
.failed
= FALSE
;
12742 eoinfo
.localsyms
= FALSE
;
12743 eoinfo
.flinfo
= &flinfo
;
12744 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12748 goto return_local_hash_table
;
12751 /* If backend needs to output some symbols not present in the hash
12752 table, do it now. */
12753 if (bed
->elf_backend_output_arch_syms
12754 && (info
->strip
!= strip_all
|| emit_relocs
))
12756 typedef int (*out_sym_func
)
12757 (void *, const char *, Elf_Internal_Sym
*, asection
*,
12758 struct elf_link_hash_entry
*);
12760 if (! ((*bed
->elf_backend_output_arch_syms
)
12761 (abfd
, info
, &flinfo
,
12762 (out_sym_func
) elf_link_output_symstrtab
)))
12765 goto return_local_hash_table
;
12769 /* Finalize the .strtab section. */
12770 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
12772 /* Swap out the .strtab section. */
12773 if (!elf_link_swap_symbols_out (&flinfo
))
12776 goto return_local_hash_table
;
12779 /* Now we know the size of the symtab section. */
12780 if (bfd_get_symcount (abfd
) > 0)
12782 /* Finish up and write out the symbol string table (.strtab)
12784 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
12785 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
12787 if (elf_symtab_shndx_list (abfd
))
12789 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
12791 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
12793 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
12794 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
12795 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
12796 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
12797 symtab_shndx_hdr
->sh_size
= amt
;
12799 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
12802 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
12803 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
12806 goto return_local_hash_table
;
12811 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
12812 /* sh_name was set in prep_headers. */
12813 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
12814 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
12815 symstrtab_hdr
->sh_addr
= 0;
12816 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
12817 symstrtab_hdr
->sh_entsize
= 0;
12818 symstrtab_hdr
->sh_link
= 0;
12819 symstrtab_hdr
->sh_info
= 0;
12820 /* sh_offset is set just below. */
12821 symstrtab_hdr
->sh_addralign
= 1;
12823 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
12825 elf_next_file_pos (abfd
) = off
;
12827 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
12828 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
12831 goto return_local_hash_table
;
12835 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
12837 _bfd_error_handler (_("%pB: failed to generate import library"),
12838 info
->out_implib_bfd
);
12840 goto return_local_hash_table
;
12843 /* Adjust the relocs to have the correct symbol indices. */
12844 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12846 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12849 if ((o
->flags
& SEC_RELOC
) == 0)
12852 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
12853 if (esdo
->rel
.hdr
!= NULL
12854 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
, info
))
12857 goto return_local_hash_table
;
12859 if (esdo
->rela
.hdr
!= NULL
12860 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
, info
))
12863 goto return_local_hash_table
;
12866 /* Set the reloc_count field to 0 to prevent write_relocs from
12867 trying to swap the relocs out itself. */
12868 o
->reloc_count
= 0;
12871 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
12872 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
12874 /* If we are linking against a dynamic object, or generating a
12875 shared library, finish up the dynamic linking information. */
12878 bfd_byte
*dyncon
, *dynconend
;
12880 /* Fix up .dynamic entries. */
12881 o
= bfd_get_linker_section (dynobj
, ".dynamic");
12882 BFD_ASSERT (o
!= NULL
);
12884 dyncon
= o
->contents
;
12885 dynconend
= o
->contents
+ o
->size
;
12886 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12888 Elf_Internal_Dyn dyn
;
12891 bfd_size_type sh_size
;
12894 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12901 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
12903 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
12905 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
12906 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
12909 dyn
.d_un
.d_val
= relativecount
;
12916 name
= info
->init_function
;
12919 name
= info
->fini_function
;
12922 struct elf_link_hash_entry
*h
;
12924 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
12926 && (h
->root
.type
== bfd_link_hash_defined
12927 || h
->root
.type
== bfd_link_hash_defweak
))
12929 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
12930 o
= h
->root
.u
.def
.section
;
12931 if (o
->output_section
!= NULL
)
12932 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
12933 + o
->output_offset
);
12936 /* The symbol is imported from another shared
12937 library and does not apply to this one. */
12938 dyn
.d_un
.d_ptr
= 0;
12945 case DT_PREINIT_ARRAYSZ
:
12946 name
= ".preinit_array";
12948 case DT_INIT_ARRAYSZ
:
12949 name
= ".init_array";
12951 case DT_FINI_ARRAYSZ
:
12952 name
= ".fini_array";
12954 o
= bfd_get_section_by_name (abfd
, name
);
12958 (_("could not find section %s"), name
);
12963 (_("warning: %s section has zero size"), name
);
12964 dyn
.d_un
.d_val
= o
->size
;
12967 case DT_PREINIT_ARRAY
:
12968 name
= ".preinit_array";
12970 case DT_INIT_ARRAY
:
12971 name
= ".init_array";
12973 case DT_FINI_ARRAY
:
12974 name
= ".fini_array";
12976 o
= bfd_get_section_by_name (abfd
, name
);
12983 name
= ".gnu.hash";
12992 name
= ".gnu.version_d";
12995 name
= ".gnu.version_r";
12998 name
= ".gnu.version";
13000 o
= bfd_get_linker_section (dynobj
, name
);
13002 if (o
== NULL
|| bfd_is_abs_section (o
->output_section
))
13005 (_("could not find section %s"), name
);
13008 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
13011 (_("warning: section '%s' is being made into a note"), name
);
13012 bfd_set_error (bfd_error_nonrepresentable_section
);
13015 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
13022 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
13028 for (i
= 1; i
< elf_numsections (abfd
); i
++)
13030 Elf_Internal_Shdr
*hdr
;
13032 hdr
= elf_elfsections (abfd
)[i
];
13033 if (hdr
->sh_type
== type
13034 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
13036 sh_size
+= hdr
->sh_size
;
13038 || sh_addr
> hdr
->sh_addr
)
13039 sh_addr
= hdr
->sh_addr
;
13043 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
13045 unsigned int opb
= bfd_octets_per_byte (abfd
, o
);
13047 /* Don't count procedure linkage table relocs in the
13048 overall reloc count. */
13049 sh_size
-= htab
->srelplt
->size
;
13051 /* If the size is zero, make the address zero too.
13052 This is to avoid a glibc bug. If the backend
13053 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13054 zero, then we'll put DT_RELA at the end of
13055 DT_JMPREL. glibc will interpret the end of
13056 DT_RELA matching the end of DT_JMPREL as the
13057 case where DT_RELA includes DT_JMPREL, and for
13058 LD_BIND_NOW will decide that processing DT_RELA
13059 will process the PLT relocs too. Net result:
13060 No PLT relocs applied. */
13063 /* If .rela.plt is the first .rela section, exclude
13064 it from DT_RELA. */
13065 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
13066 + htab
->srelplt
->output_offset
) * opb
)
13067 sh_addr
+= htab
->srelplt
->size
;
13070 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
13071 dyn
.d_un
.d_val
= sh_size
;
13073 dyn
.d_un
.d_ptr
= sh_addr
;
13076 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13080 /* If we have created any dynamic sections, then output them. */
13081 if (dynobj
!= NULL
)
13083 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
13086 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13087 if (bfd_link_textrel_check (info
)
13088 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
)
13090 bfd_byte
*dyncon
, *dynconend
;
13092 dyncon
= o
->contents
;
13093 dynconend
= o
->contents
+ o
->size
;
13094 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13096 Elf_Internal_Dyn dyn
;
13098 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13100 if (dyn
.d_tag
== DT_TEXTREL
)
13102 if (info
->textrel_check
== textrel_check_error
)
13103 info
->callbacks
->einfo
13104 (_("%P%X: read-only segment has dynamic relocations\n"));
13105 else if (bfd_link_dll (info
))
13106 info
->callbacks
->einfo
13107 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13109 info
->callbacks
->einfo
13110 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13116 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
13118 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
13120 || o
->output_section
== bfd_abs_section_ptr
)
13122 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
13124 /* At this point, we are only interested in sections
13125 created by _bfd_elf_link_create_dynamic_sections. */
13128 if (htab
->stab_info
.stabstr
== o
)
13130 if (htab
->eh_info
.hdr_sec
== o
)
13132 if (strcmp (o
->name
, ".dynstr") != 0)
13134 bfd_size_type octets
= ((file_ptr
) o
->output_offset
13135 * bfd_octets_per_byte (abfd
, o
));
13136 if (!bfd_set_section_contents (abfd
, o
->output_section
,
13137 o
->contents
, octets
, o
->size
))
13142 /* The contents of the .dynstr section are actually in a
13146 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
13147 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
13148 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
13154 if (!info
->resolve_section_groups
)
13156 bfd_boolean failed
= FALSE
;
13158 BFD_ASSERT (bfd_link_relocatable (info
));
13159 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
13164 /* If we have optimized stabs strings, output them. */
13165 if (htab
->stab_info
.stabstr
!= NULL
)
13167 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
13171 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
13174 if (info
->callbacks
->emit_ctf
)
13175 info
->callbacks
->emit_ctf ();
13177 elf_final_link_free (abfd
, &flinfo
);
13181 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
13182 if (contents
== NULL
)
13184 /* Bail out and fail. */
13186 goto return_local_hash_table
;
13188 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
13189 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
13193 return_local_hash_table
:
13194 if (info
->unique_symbol
)
13195 bfd_hash_table_free (&flinfo
.local_hash_table
);
13199 elf_final_link_free (abfd
, &flinfo
);
13201 goto return_local_hash_table
;
13204 /* Initialize COOKIE for input bfd ABFD. */
13207 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
13208 struct bfd_link_info
*info
, bfd
*abfd
)
13210 Elf_Internal_Shdr
*symtab_hdr
;
13211 const struct elf_backend_data
*bed
;
13213 bed
= get_elf_backend_data (abfd
);
13214 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13216 cookie
->abfd
= abfd
;
13217 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
13218 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
13219 if (cookie
->bad_symtab
)
13221 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13222 cookie
->extsymoff
= 0;
13226 cookie
->locsymcount
= symtab_hdr
->sh_info
;
13227 cookie
->extsymoff
= symtab_hdr
->sh_info
;
13230 if (bed
->s
->arch_size
== 32)
13231 cookie
->r_sym_shift
= 8;
13233 cookie
->r_sym_shift
= 32;
13235 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
13236 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
13238 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
13239 cookie
->locsymcount
, 0,
13241 if (cookie
->locsyms
== NULL
)
13243 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
13246 if (info
->keep_memory
)
13247 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
13252 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13255 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
13257 Elf_Internal_Shdr
*symtab_hdr
;
13259 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13260 if (symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
13261 free (cookie
->locsyms
);
13264 /* Initialize the relocation information in COOKIE for input section SEC
13265 of input bfd ABFD. */
13268 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13269 struct bfd_link_info
*info
, bfd
*abfd
,
13272 if (sec
->reloc_count
== 0)
13274 cookie
->rels
= NULL
;
13275 cookie
->relend
= NULL
;
13279 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
13280 info
->keep_memory
);
13281 if (cookie
->rels
== NULL
)
13283 cookie
->rel
= cookie
->rels
;
13284 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
13286 cookie
->rel
= cookie
->rels
;
13290 /* Free the memory allocated by init_reloc_cookie_rels,
13294 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13297 if (elf_section_data (sec
)->relocs
!= cookie
->rels
)
13298 free (cookie
->rels
);
13301 /* Initialize the whole of COOKIE for input section SEC. */
13304 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13305 struct bfd_link_info
*info
,
13308 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
13310 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
13315 fini_reloc_cookie (cookie
, sec
->owner
);
13320 /* Free the memory allocated by init_reloc_cookie_for_section,
13324 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13327 fini_reloc_cookie_rels (cookie
, sec
);
13328 fini_reloc_cookie (cookie
, sec
->owner
);
13331 /* Garbage collect unused sections. */
13333 /* Default gc_mark_hook. */
13336 _bfd_elf_gc_mark_hook (asection
*sec
,
13337 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13338 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13339 struct elf_link_hash_entry
*h
,
13340 Elf_Internal_Sym
*sym
)
13344 switch (h
->root
.type
)
13346 case bfd_link_hash_defined
:
13347 case bfd_link_hash_defweak
:
13348 return h
->root
.u
.def
.section
;
13350 case bfd_link_hash_common
:
13351 return h
->root
.u
.c
.p
->section
;
13358 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
13363 /* Return the debug definition section. */
13366 elf_gc_mark_debug_section (asection
*sec ATTRIBUTE_UNUSED
,
13367 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13368 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13369 struct elf_link_hash_entry
*h
,
13370 Elf_Internal_Sym
*sym
)
13374 /* Return the global debug definition section. */
13375 if ((h
->root
.type
== bfd_link_hash_defined
13376 || h
->root
.type
== bfd_link_hash_defweak
)
13377 && (h
->root
.u
.def
.section
->flags
& SEC_DEBUGGING
) != 0)
13378 return h
->root
.u
.def
.section
;
13382 /* Return the local debug definition section. */
13383 asection
*isec
= bfd_section_from_elf_index (sec
->owner
,
13385 if ((isec
->flags
& SEC_DEBUGGING
) != 0)
13392 /* COOKIE->rel describes a relocation against section SEC, which is
13393 a section we've decided to keep. Return the section that contains
13394 the relocation symbol, or NULL if no section contains it. */
13397 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
13398 elf_gc_mark_hook_fn gc_mark_hook
,
13399 struct elf_reloc_cookie
*cookie
,
13400 bfd_boolean
*start_stop
)
13402 unsigned long r_symndx
;
13403 struct elf_link_hash_entry
*h
, *hw
;
13405 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
13406 if (r_symndx
== STN_UNDEF
)
13409 if (r_symndx
>= cookie
->locsymcount
13410 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13412 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
13415 info
->callbacks
->einfo (_("%F%P: corrupt input: %pB\n"),
13419 while (h
->root
.type
== bfd_link_hash_indirect
13420 || h
->root
.type
== bfd_link_hash_warning
)
13421 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13423 /* Keep all aliases of the symbol too. If an object symbol
13424 needs to be copied into .dynbss then all of its aliases
13425 should be present as dynamic symbols, not just the one used
13426 on the copy relocation. */
13428 while (hw
->is_weakalias
)
13434 if (start_stop
!= NULL
)
13436 /* To work around a glibc bug, mark XXX input sections
13437 when there is a reference to __start_XXX or __stop_XXX
13441 asection
*s
= h
->u2
.start_stop_section
;
13442 *start_stop
= !s
->gc_mark
;
13447 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
13450 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
13451 &cookie
->locsyms
[r_symndx
]);
13454 /* COOKIE->rel describes a relocation against section SEC, which is
13455 a section we've decided to keep. Mark the section that contains
13456 the relocation symbol. */
13459 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
13461 elf_gc_mark_hook_fn gc_mark_hook
,
13462 struct elf_reloc_cookie
*cookie
)
13465 bfd_boolean start_stop
= FALSE
;
13467 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
13468 while (rsec
!= NULL
)
13470 if (!rsec
->gc_mark
)
13472 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
13473 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
13475 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
13480 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
13485 /* The mark phase of garbage collection. For a given section, mark
13486 it and any sections in this section's group, and all the sections
13487 which define symbols to which it refers. */
13490 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
13492 elf_gc_mark_hook_fn gc_mark_hook
)
13495 asection
*group_sec
, *eh_frame
;
13499 /* Mark all the sections in the group. */
13500 group_sec
= elf_section_data (sec
)->next_in_group
;
13501 if (group_sec
&& !group_sec
->gc_mark
)
13502 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
13505 /* Look through the section relocs. */
13507 eh_frame
= elf_eh_frame_section (sec
->owner
);
13508 if ((sec
->flags
& SEC_RELOC
) != 0
13509 && sec
->reloc_count
> 0
13510 && sec
!= eh_frame
)
13512 struct elf_reloc_cookie cookie
;
13514 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
13518 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
13519 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
13524 fini_reloc_cookie_for_section (&cookie
, sec
);
13528 if (ret
&& eh_frame
&& elf_fde_list (sec
))
13530 struct elf_reloc_cookie cookie
;
13532 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
13536 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
13537 gc_mark_hook
, &cookie
))
13539 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
13543 eh_frame
= elf_section_eh_frame_entry (sec
);
13544 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
13545 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
13551 /* Scan and mark sections in a special or debug section group. */
13554 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
13556 /* Point to first section of section group. */
13558 /* Used to iterate the section group. */
13561 bfd_boolean is_special_grp
= TRUE
;
13562 bfd_boolean is_debug_grp
= TRUE
;
13564 /* First scan to see if group contains any section other than debug
13565 and special section. */
13566 ssec
= msec
= elf_next_in_group (grp
);
13569 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
13570 is_debug_grp
= FALSE
;
13572 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
13573 is_special_grp
= FALSE
;
13575 msec
= elf_next_in_group (msec
);
13577 while (msec
!= ssec
);
13579 /* If this is a pure debug section group or pure special section group,
13580 keep all sections in this group. */
13581 if (is_debug_grp
|| is_special_grp
)
13586 msec
= elf_next_in_group (msec
);
13588 while (msec
!= ssec
);
13592 /* Keep debug and special sections. */
13595 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
13596 elf_gc_mark_hook_fn mark_hook
)
13600 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13603 bfd_boolean some_kept
;
13604 bfd_boolean debug_frag_seen
;
13605 bfd_boolean has_kept_debug_info
;
13607 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13609 isec
= ibfd
->sections
;
13610 if (isec
== NULL
|| isec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13613 /* Ensure all linker created sections are kept,
13614 see if any other section is already marked,
13615 and note if we have any fragmented debug sections. */
13616 debug_frag_seen
= some_kept
= has_kept_debug_info
= FALSE
;
13617 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13619 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
13621 else if (isec
->gc_mark
13622 && (isec
->flags
& SEC_ALLOC
) != 0
13623 && elf_section_type (isec
) != SHT_NOTE
)
13627 /* Since all sections, except for backend specific ones,
13628 have been garbage collected, call mark_hook on this
13629 section if any of its linked-to sections is marked. */
13630 asection
*linked_to_sec
= elf_linked_to_section (isec
);
13631 for (; linked_to_sec
!= NULL
;
13632 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
13633 if (linked_to_sec
->gc_mark
)
13635 if (!_bfd_elf_gc_mark (info
, isec
, mark_hook
))
13641 if (!debug_frag_seen
13642 && (isec
->flags
& SEC_DEBUGGING
)
13643 && CONST_STRNEQ (isec
->name
, ".debug_line."))
13644 debug_frag_seen
= TRUE
;
13645 else if (strcmp (bfd_section_name (isec
),
13646 "__patchable_function_entries") == 0
13647 && elf_linked_to_section (isec
) == NULL
)
13648 info
->callbacks
->einfo (_("%F%P: %pB(%pA): error: "
13649 "need linked-to section "
13650 "for --gc-sections\n"),
13651 isec
->owner
, isec
);
13654 /* If no non-note alloc section in this file will be kept, then
13655 we can toss out the debug and special sections. */
13659 /* Keep debug and special sections like .comment when they are
13660 not part of a group. Also keep section groups that contain
13661 just debug sections or special sections. NB: Sections with
13662 linked-to section has been handled above. */
13663 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13665 if ((isec
->flags
& SEC_GROUP
) != 0)
13666 _bfd_elf_gc_mark_debug_special_section_group (isec
);
13667 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
13668 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
13669 && elf_next_in_group (isec
) == NULL
13670 && elf_linked_to_section (isec
) == NULL
)
13672 if (isec
->gc_mark
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
13673 has_kept_debug_info
= TRUE
;
13676 /* Look for CODE sections which are going to be discarded,
13677 and find and discard any fragmented debug sections which
13678 are associated with that code section. */
13679 if (debug_frag_seen
)
13680 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13681 if ((isec
->flags
& SEC_CODE
) != 0
13682 && isec
->gc_mark
== 0)
13687 ilen
= strlen (isec
->name
);
13689 /* Association is determined by the name of the debug
13690 section containing the name of the code section as
13691 a suffix. For example .debug_line.text.foo is a
13692 debug section associated with .text.foo. */
13693 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
13697 if (dsec
->gc_mark
== 0
13698 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
13701 dlen
= strlen (dsec
->name
);
13704 && strncmp (dsec
->name
+ (dlen
- ilen
),
13705 isec
->name
, ilen
) == 0)
13710 /* Mark debug sections referenced by kept debug sections. */
13711 if (has_kept_debug_info
)
13712 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13714 && (isec
->flags
& SEC_DEBUGGING
) != 0)
13715 if (!_bfd_elf_gc_mark (info
, isec
,
13716 elf_gc_mark_debug_section
))
13723 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
13726 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13728 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13732 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13733 || elf_object_id (sub
) != elf_hash_table_id (elf_hash_table (info
))
13734 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13737 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13740 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13742 /* When any section in a section group is kept, we keep all
13743 sections in the section group. If the first member of
13744 the section group is excluded, we will also exclude the
13746 if (o
->flags
& SEC_GROUP
)
13748 asection
*first
= elf_next_in_group (o
);
13749 o
->gc_mark
= first
->gc_mark
;
13755 /* Skip sweeping sections already excluded. */
13756 if (o
->flags
& SEC_EXCLUDE
)
13759 /* Since this is early in the link process, it is simple
13760 to remove a section from the output. */
13761 o
->flags
|= SEC_EXCLUDE
;
13763 if (info
->print_gc_sections
&& o
->size
!= 0)
13764 /* xgettext:c-format */
13765 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13773 /* Propagate collected vtable information. This is called through
13774 elf_link_hash_traverse. */
13777 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
13779 /* Those that are not vtables. */
13781 || h
->u2
.vtable
== NULL
13782 || h
->u2
.vtable
->parent
== NULL
)
13785 /* Those vtables that do not have parents, we cannot merge. */
13786 if (h
->u2
.vtable
->parent
== (struct elf_link_hash_entry
*) -1)
13789 /* If we've already been done, exit. */
13790 if (h
->u2
.vtable
->used
&& h
->u2
.vtable
->used
[-1])
13793 /* Make sure the parent's table is up to date. */
13794 elf_gc_propagate_vtable_entries_used (h
->u2
.vtable
->parent
, okp
);
13796 if (h
->u2
.vtable
->used
== NULL
)
13798 /* None of this table's entries were referenced. Re-use the
13800 h
->u2
.vtable
->used
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13801 h
->u2
.vtable
->size
= h
->u2
.vtable
->parent
->u2
.vtable
->size
;
13806 bfd_boolean
*cu
, *pu
;
13808 /* Or the parent's entries into ours. */
13809 cu
= h
->u2
.vtable
->used
;
13811 pu
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13814 const struct elf_backend_data
*bed
;
13815 unsigned int log_file_align
;
13817 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
13818 log_file_align
= bed
->s
->log_file_align
;
13819 n
= h
->u2
.vtable
->parent
->u2
.vtable
->size
>> log_file_align
;
13834 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
13837 bfd_vma hstart
, hend
;
13838 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
13839 const struct elf_backend_data
*bed
;
13840 unsigned int log_file_align
;
13842 /* Take care of both those symbols that do not describe vtables as
13843 well as those that are not loaded. */
13845 || h
->u2
.vtable
== NULL
13846 || h
->u2
.vtable
->parent
== NULL
)
13849 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
13850 || h
->root
.type
== bfd_link_hash_defweak
);
13852 sec
= h
->root
.u
.def
.section
;
13853 hstart
= h
->root
.u
.def
.value
;
13854 hend
= hstart
+ h
->size
;
13856 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
13858 return *(bfd_boolean
*) okp
= FALSE
;
13859 bed
= get_elf_backend_data (sec
->owner
);
13860 log_file_align
= bed
->s
->log_file_align
;
13862 relend
= relstart
+ sec
->reloc_count
;
13864 for (rel
= relstart
; rel
< relend
; ++rel
)
13865 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
13867 /* If the entry is in use, do nothing. */
13868 if (h
->u2
.vtable
->used
13869 && (rel
->r_offset
- hstart
) < h
->u2
.vtable
->size
)
13871 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
13872 if (h
->u2
.vtable
->used
[entry
])
13875 /* Otherwise, kill it. */
13876 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
13882 /* Mark sections containing dynamically referenced symbols. When
13883 building shared libraries, we must assume that any visible symbol is
13887 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
13889 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
13890 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
13892 if ((h
->root
.type
== bfd_link_hash_defined
13893 || h
->root
.type
== bfd_link_hash_defweak
)
13894 && ((h
->ref_dynamic
&& !h
->forced_local
)
13895 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
13896 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
13897 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
13898 && (!bfd_link_executable (info
)
13899 || info
->gc_keep_exported
13900 || info
->export_dynamic
13903 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
13904 && (h
->versioned
>= versioned
13905 || !bfd_hide_sym_by_version (info
->version_info
,
13906 h
->root
.root
.string
)))))
13907 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13912 /* Keep all sections containing symbols undefined on the command-line,
13913 and the section containing the entry symbol. */
13916 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
13918 struct bfd_sym_chain
*sym
;
13920 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
13922 struct elf_link_hash_entry
*h
;
13924 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
13925 FALSE
, FALSE
, FALSE
);
13928 && (h
->root
.type
== bfd_link_hash_defined
13929 || h
->root
.type
== bfd_link_hash_defweak
)
13930 && !bfd_is_const_section (h
->root
.u
.def
.section
))
13931 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13936 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
13937 struct bfd_link_info
*info
)
13939 bfd
*ibfd
= info
->input_bfds
;
13941 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13944 struct elf_reloc_cookie cookie
;
13946 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13948 sec
= ibfd
->sections
;
13949 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13952 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
13955 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
13957 if (CONST_STRNEQ (bfd_section_name (sec
), ".eh_frame_entry")
13958 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
13960 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
13961 fini_reloc_cookie_rels (&cookie
, sec
);
13968 /* Do mark and sweep of unused sections. */
13971 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
13973 bfd_boolean ok
= TRUE
;
13975 elf_gc_mark_hook_fn gc_mark_hook
;
13976 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13977 struct elf_link_hash_table
*htab
;
13979 if (!bed
->can_gc_sections
13980 || !is_elf_hash_table (info
->hash
))
13982 _bfd_error_handler(_("warning: gc-sections option ignored"));
13986 bed
->gc_keep (info
);
13987 htab
= elf_hash_table (info
);
13989 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13990 at the .eh_frame section if we can mark the FDEs individually. */
13991 for (sub
= info
->input_bfds
;
13992 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
13993 sub
= sub
->link
.next
)
13996 struct elf_reloc_cookie cookie
;
13998 sec
= sub
->sections
;
13999 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14001 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
14002 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
14004 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
14005 if (elf_section_data (sec
)->sec_info
14006 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
14007 elf_eh_frame_section (sub
) = sec
;
14008 fini_reloc_cookie_for_section (&cookie
, sec
);
14009 sec
= bfd_get_next_section_by_name (NULL
, sec
);
14013 /* Apply transitive closure to the vtable entry usage info. */
14014 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
14018 /* Kill the vtable relocations that were not used. */
14019 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &ok
);
14023 /* Mark dynamically referenced symbols. */
14024 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
14025 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
14027 /* Grovel through relocs to find out who stays ... */
14028 gc_mark_hook
= bed
->gc_mark_hook
;
14029 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
14033 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
14034 || elf_object_id (sub
) != elf_hash_table_id (htab
)
14035 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
14039 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14042 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14043 Also treat note sections as a root, if the section is not part
14044 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14045 well as FINI_ARRAY sections for ld -r. */
14046 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
14048 && (o
->flags
& SEC_EXCLUDE
) == 0
14049 && ((o
->flags
& SEC_KEEP
) != 0
14050 || (bfd_link_relocatable (info
)
14051 && ((elf_section_data (o
)->this_hdr
.sh_type
14052 == SHT_PREINIT_ARRAY
)
14053 || (elf_section_data (o
)->this_hdr
.sh_type
14055 || (elf_section_data (o
)->this_hdr
.sh_type
14056 == SHT_FINI_ARRAY
)))
14057 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
14058 && elf_next_in_group (o
) == NULL
14059 && elf_linked_to_section (o
) == NULL
)
14060 || ((elf_tdata (sub
)->has_gnu_osabi
& elf_gnu_osabi_retain
)
14061 && (elf_section_flags (o
) & SHF_GNU_RETAIN
))))
14063 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
14068 /* Allow the backend to mark additional target specific sections. */
14069 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
14071 /* ... and mark SEC_EXCLUDE for those that go. */
14072 return elf_gc_sweep (abfd
, info
);
14075 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14078 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
14080 struct elf_link_hash_entry
*h
,
14083 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
14084 struct elf_link_hash_entry
**search
, *child
;
14085 size_t extsymcount
;
14086 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14088 /* The sh_info field of the symtab header tells us where the
14089 external symbols start. We don't care about the local symbols at
14091 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
14092 if (!elf_bad_symtab (abfd
))
14093 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
14095 sym_hashes
= elf_sym_hashes (abfd
);
14096 sym_hashes_end
= sym_hashes
+ extsymcount
;
14098 /* Hunt down the child symbol, which is in this section at the same
14099 offset as the relocation. */
14100 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
14102 if ((child
= *search
) != NULL
14103 && (child
->root
.type
== bfd_link_hash_defined
14104 || child
->root
.type
== bfd_link_hash_defweak
)
14105 && child
->root
.u
.def
.section
== sec
14106 && child
->root
.u
.def
.value
== offset
)
14110 /* xgettext:c-format */
14111 _bfd_error_handler (_("%pB: %pA+%#" PRIx64
": no symbol found for INHERIT"),
14112 abfd
, sec
, (uint64_t) offset
);
14113 bfd_set_error (bfd_error_invalid_operation
);
14117 if (!child
->u2
.vtable
)
14119 child
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14120 bfd_zalloc (abfd
, sizeof (*child
->u2
.vtable
)));
14121 if (!child
->u2
.vtable
)
14126 /* This *should* only be the absolute section. It could potentially
14127 be that someone has defined a non-global vtable though, which
14128 would be bad. It isn't worth paging in the local symbols to be
14129 sure though; that case should simply be handled by the assembler. */
14131 child
->u2
.vtable
->parent
= (struct elf_link_hash_entry
*) -1;
14134 child
->u2
.vtable
->parent
= h
;
14139 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14142 bfd_elf_gc_record_vtentry (bfd
*abfd
, asection
*sec
,
14143 struct elf_link_hash_entry
*h
,
14146 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14147 unsigned int log_file_align
= bed
->s
->log_file_align
;
14151 /* xgettext:c-format */
14152 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14154 bfd_set_error (bfd_error_bad_value
);
14160 h
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14161 bfd_zalloc (abfd
, sizeof (*h
->u2
.vtable
)));
14166 if (addend
>= h
->u2
.vtable
->size
)
14168 size_t size
, bytes
, file_align
;
14169 bfd_boolean
*ptr
= h
->u2
.vtable
->used
;
14171 /* While the symbol is undefined, we have to be prepared to handle
14173 file_align
= 1 << log_file_align
;
14174 if (h
->root
.type
== bfd_link_hash_undefined
)
14175 size
= addend
+ file_align
;
14179 if (addend
>= size
)
14181 /* Oops! We've got a reference past the defined end of
14182 the table. This is probably a bug -- shall we warn? */
14183 size
= addend
+ file_align
;
14186 size
= (size
+ file_align
- 1) & -file_align
;
14188 /* Allocate one extra entry for use as a "done" flag for the
14189 consolidation pass. */
14190 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
14194 ptr
= (bfd_boolean
*) bfd_realloc (ptr
- 1, bytes
);
14200 oldbytes
= (((h
->u2
.vtable
->size
>> log_file_align
) + 1)
14201 * sizeof (bfd_boolean
));
14202 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
14206 ptr
= (bfd_boolean
*) bfd_zmalloc (bytes
);
14211 /* And arrange for that done flag to be at index -1. */
14212 h
->u2
.vtable
->used
= ptr
+ 1;
14213 h
->u2
.vtable
->size
= size
;
14216 h
->u2
.vtable
->used
[addend
>> log_file_align
] = TRUE
;
14221 /* Map an ELF section header flag to its corresponding string. */
14225 flagword flag_value
;
14226 } elf_flags_to_name_table
;
14228 static const elf_flags_to_name_table elf_flags_to_names
[] =
14230 { "SHF_WRITE", SHF_WRITE
},
14231 { "SHF_ALLOC", SHF_ALLOC
},
14232 { "SHF_EXECINSTR", SHF_EXECINSTR
},
14233 { "SHF_MERGE", SHF_MERGE
},
14234 { "SHF_STRINGS", SHF_STRINGS
},
14235 { "SHF_INFO_LINK", SHF_INFO_LINK
},
14236 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
14237 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
14238 { "SHF_GROUP", SHF_GROUP
},
14239 { "SHF_TLS", SHF_TLS
},
14240 { "SHF_MASKOS", SHF_MASKOS
},
14241 { "SHF_EXCLUDE", SHF_EXCLUDE
},
14244 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14246 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
14247 struct flag_info
*flaginfo
,
14250 const bfd_vma sh_flags
= elf_section_flags (section
);
14252 if (!flaginfo
->flags_initialized
)
14254 bfd
*obfd
= info
->output_bfd
;
14255 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14256 struct flag_info_list
*tf
= flaginfo
->flag_list
;
14258 int without_hex
= 0;
14260 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
14263 flagword (*lookup
) (char *);
14265 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
14266 if (lookup
!= NULL
)
14268 flagword hexval
= (*lookup
) ((char *) tf
->name
);
14272 if (tf
->with
== with_flags
)
14273 with_hex
|= hexval
;
14274 else if (tf
->with
== without_flags
)
14275 without_hex
|= hexval
;
14280 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
14282 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
14284 if (tf
->with
== with_flags
)
14285 with_hex
|= elf_flags_to_names
[i
].flag_value
;
14286 else if (tf
->with
== without_flags
)
14287 without_hex
|= elf_flags_to_names
[i
].flag_value
;
14294 info
->callbacks
->einfo
14295 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
14299 flaginfo
->flags_initialized
= TRUE
;
14300 flaginfo
->only_with_flags
|= with_hex
;
14301 flaginfo
->not_with_flags
|= without_hex
;
14304 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
14307 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
14313 struct alloc_got_off_arg
{
14315 struct bfd_link_info
*info
;
14318 /* We need a special top-level link routine to convert got reference counts
14319 to real got offsets. */
14322 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
14324 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
14325 bfd
*obfd
= gofarg
->info
->output_bfd
;
14326 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14328 if (h
->got
.refcount
> 0)
14330 h
->got
.offset
= gofarg
->gotoff
;
14331 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
14334 h
->got
.offset
= (bfd_vma
) -1;
14339 /* And an accompanying bit to work out final got entry offsets once
14340 we're done. Should be called from final_link. */
14343 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
14344 struct bfd_link_info
*info
)
14347 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14349 struct alloc_got_off_arg gofarg
;
14351 BFD_ASSERT (abfd
== info
->output_bfd
);
14353 if (! is_elf_hash_table (info
->hash
))
14356 /* The GOT offset is relative to the .got section, but the GOT header is
14357 put into the .got.plt section, if the backend uses it. */
14358 if (bed
->want_got_plt
)
14361 gotoff
= bed
->got_header_size
;
14363 /* Do the local .got entries first. */
14364 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
14366 bfd_signed_vma
*local_got
;
14367 size_t j
, locsymcount
;
14368 Elf_Internal_Shdr
*symtab_hdr
;
14370 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
14373 local_got
= elf_local_got_refcounts (i
);
14377 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
14378 if (elf_bad_symtab (i
))
14379 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
14381 locsymcount
= symtab_hdr
->sh_info
;
14383 for (j
= 0; j
< locsymcount
; ++j
)
14385 if (local_got
[j
] > 0)
14387 local_got
[j
] = gotoff
;
14388 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
14391 local_got
[j
] = (bfd_vma
) -1;
14395 /* Then the global .got entries. .plt refcounts are handled by
14396 adjust_dynamic_symbol */
14397 gofarg
.gotoff
= gotoff
;
14398 gofarg
.info
= info
;
14399 elf_link_hash_traverse (elf_hash_table (info
),
14400 elf_gc_allocate_got_offsets
,
14405 /* Many folk need no more in the way of final link than this, once
14406 got entry reference counting is enabled. */
14409 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
14411 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
14414 /* Invoke the regular ELF backend linker to do all the work. */
14415 return bfd_elf_final_link (abfd
, info
);
14419 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
14421 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
14423 if (rcookie
->bad_symtab
)
14424 rcookie
->rel
= rcookie
->rels
;
14426 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
14428 unsigned long r_symndx
;
14430 if (! rcookie
->bad_symtab
)
14431 if (rcookie
->rel
->r_offset
> offset
)
14433 if (rcookie
->rel
->r_offset
!= offset
)
14436 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
14437 if (r_symndx
== STN_UNDEF
)
14440 if (r_symndx
>= rcookie
->locsymcount
14441 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
14443 struct elf_link_hash_entry
*h
;
14445 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
14447 while (h
->root
.type
== bfd_link_hash_indirect
14448 || h
->root
.type
== bfd_link_hash_warning
)
14449 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
14451 if ((h
->root
.type
== bfd_link_hash_defined
14452 || h
->root
.type
== bfd_link_hash_defweak
)
14453 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
14454 || h
->root
.u
.def
.section
->kept_section
!= NULL
14455 || discarded_section (h
->root
.u
.def
.section
)))
14460 /* It's not a relocation against a global symbol,
14461 but it could be a relocation against a local
14462 symbol for a discarded section. */
14464 Elf_Internal_Sym
*isym
;
14466 /* Need to: get the symbol; get the section. */
14467 isym
= &rcookie
->locsyms
[r_symndx
];
14468 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
14470 && (isec
->kept_section
!= NULL
14471 || discarded_section (isec
)))
14479 /* Discard unneeded references to discarded sections.
14480 Returns -1 on error, 1 if any section's size was changed, 0 if
14481 nothing changed. This function assumes that the relocations are in
14482 sorted order, which is true for all known assemblers. */
14485 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
14487 struct elf_reloc_cookie cookie
;
14492 if (info
->traditional_format
14493 || !is_elf_hash_table (info
->hash
))
14496 o
= bfd_get_section_by_name (output_bfd
, ".stab");
14501 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14504 || i
->reloc_count
== 0
14505 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
14509 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14512 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14515 if (_bfd_discard_section_stabs (abfd
, i
,
14516 elf_section_data (i
)->sec_info
,
14517 bfd_elf_reloc_symbol_deleted_p
,
14521 fini_reloc_cookie_for_section (&cookie
, i
);
14526 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
14527 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
14531 int eh_changed
= 0;
14532 unsigned int eh_alignment
; /* Octets. */
14534 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14540 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14543 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14546 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
14547 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
14548 bfd_elf_reloc_symbol_deleted_p
,
14552 if (i
->size
!= i
->rawsize
)
14556 fini_reloc_cookie_for_section (&cookie
, i
);
14559 eh_alignment
= ((1 << o
->alignment_power
)
14560 * bfd_octets_per_byte (output_bfd
, o
));
14561 /* Skip over zero terminator, and prevent empty sections from
14562 adding alignment padding at the end. */
14563 for (i
= o
->map_tail
.s
; i
!= NULL
; i
= i
->map_tail
.s
)
14565 i
->flags
|= SEC_EXCLUDE
;
14566 else if (i
->size
> 4)
14568 /* The last non-empty eh_frame section doesn't need padding. */
14571 /* Any prior sections must pad the last FDE out to the output
14572 section alignment. Otherwise we might have zero padding
14573 between sections, which would be seen as a terminator. */
14574 for (; i
!= NULL
; i
= i
->map_tail
.s
)
14576 /* All but the last zero terminator should have been removed. */
14581 = (i
->size
+ eh_alignment
- 1) & -eh_alignment
;
14582 if (i
->size
!= size
)
14590 elf_link_hash_traverse (elf_hash_table (info
),
14591 _bfd_elf_adjust_eh_frame_global_symbol
, NULL
);
14594 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
14596 const struct elf_backend_data
*bed
;
14599 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14601 s
= abfd
->sections
;
14602 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14605 bed
= get_elf_backend_data (abfd
);
14607 if (bed
->elf_backend_discard_info
!= NULL
)
14609 if (!init_reloc_cookie (&cookie
, info
, abfd
))
14612 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
14615 fini_reloc_cookie (&cookie
, abfd
);
14619 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
14620 _bfd_elf_end_eh_frame_parsing (info
);
14622 if (info
->eh_frame_hdr_type
14623 && !bfd_link_relocatable (info
)
14624 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
14631 _bfd_elf_section_already_linked (bfd
*abfd
,
14633 struct bfd_link_info
*info
)
14636 const char *name
, *key
;
14637 struct bfd_section_already_linked
*l
;
14638 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
14640 if (sec
->output_section
== bfd_abs_section_ptr
)
14643 flags
= sec
->flags
;
14645 /* Return if it isn't a linkonce section. A comdat group section
14646 also has SEC_LINK_ONCE set. */
14647 if ((flags
& SEC_LINK_ONCE
) == 0)
14650 /* Don't put group member sections on our list of already linked
14651 sections. They are handled as a group via their group section. */
14652 if (elf_sec_group (sec
) != NULL
)
14655 /* For a SHT_GROUP section, use the group signature as the key. */
14657 if ((flags
& SEC_GROUP
) != 0
14658 && elf_next_in_group (sec
) != NULL
14659 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
14660 key
= elf_group_name (elf_next_in_group (sec
));
14663 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14664 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
14665 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
14668 /* Must be a user linkonce section that doesn't follow gcc's
14669 naming convention. In this case we won't be matching
14670 single member groups. */
14674 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
14676 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14678 /* We may have 2 different types of sections on the list: group
14679 sections with a signature of <key> (<key> is some string),
14680 and linkonce sections named .gnu.linkonce.<type>.<key>.
14681 Match like sections. LTO plugin sections are an exception.
14682 They are always named .gnu.linkonce.t.<key> and match either
14683 type of section. */
14684 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
14685 && ((flags
& SEC_GROUP
) != 0
14686 || strcmp (name
, l
->sec
->name
) == 0))
14687 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0
14688 || (sec
->owner
->flags
& BFD_PLUGIN
) != 0)
14690 /* The section has already been linked. See if we should
14691 issue a warning. */
14692 if (!_bfd_handle_already_linked (sec
, l
, info
))
14695 if (flags
& SEC_GROUP
)
14697 asection
*first
= elf_next_in_group (sec
);
14698 asection
*s
= first
;
14702 s
->output_section
= bfd_abs_section_ptr
;
14703 /* Record which group discards it. */
14704 s
->kept_section
= l
->sec
;
14705 s
= elf_next_in_group (s
);
14706 /* These lists are circular. */
14716 /* A single member comdat group section may be discarded by a
14717 linkonce section and vice versa. */
14718 if ((flags
& SEC_GROUP
) != 0)
14720 asection
*first
= elf_next_in_group (sec
);
14722 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
14723 /* Check this single member group against linkonce sections. */
14724 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14725 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14726 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
14728 first
->output_section
= bfd_abs_section_ptr
;
14729 first
->kept_section
= l
->sec
;
14730 sec
->output_section
= bfd_abs_section_ptr
;
14735 /* Check this linkonce section against single member groups. */
14736 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14737 if (l
->sec
->flags
& SEC_GROUP
)
14739 asection
*first
= elf_next_in_group (l
->sec
);
14742 && elf_next_in_group (first
) == first
14743 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
14745 sec
->output_section
= bfd_abs_section_ptr
;
14746 sec
->kept_section
= first
;
14751 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14752 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14753 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14754 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14755 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14756 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14757 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14758 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14759 The reverse order cannot happen as there is never a bfd with only the
14760 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14761 matter as here were are looking only for cross-bfd sections. */
14763 if ((flags
& SEC_GROUP
) == 0 && CONST_STRNEQ (name
, ".gnu.linkonce.r."))
14764 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14765 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14766 && CONST_STRNEQ (l
->sec
->name
, ".gnu.linkonce.t."))
14768 if (abfd
!= l
->sec
->owner
)
14769 sec
->output_section
= bfd_abs_section_ptr
;
14773 /* This is the first section with this name. Record it. */
14774 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
14775 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
14776 return sec
->output_section
== bfd_abs_section_ptr
;
14780 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
14782 return sym
->st_shndx
== SHN_COMMON
;
14786 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
14792 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
14794 return bfd_com_section_ptr
;
14798 _bfd_elf_default_got_elt_size (bfd
*abfd
,
14799 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
14800 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
14801 bfd
*ibfd ATTRIBUTE_UNUSED
,
14802 unsigned long symndx ATTRIBUTE_UNUSED
)
14804 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14805 return bed
->s
->arch_size
/ 8;
14808 /* Routines to support the creation of dynamic relocs. */
14810 /* Returns the name of the dynamic reloc section associated with SEC. */
14812 static const char *
14813 get_dynamic_reloc_section_name (bfd
* abfd
,
14815 bfd_boolean is_rela
)
14818 const char *old_name
= bfd_section_name (sec
);
14819 const char *prefix
= is_rela
? ".rela" : ".rel";
14821 if (old_name
== NULL
)
14824 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
14825 sprintf (name
, "%s%s", prefix
, old_name
);
14830 /* Returns the dynamic reloc section associated with SEC.
14831 If necessary compute the name of the dynamic reloc section based
14832 on SEC's name (looked up in ABFD's string table) and the setting
14836 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
14838 bfd_boolean is_rela
)
14840 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14842 if (reloc_sec
== NULL
)
14844 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14848 reloc_sec
= bfd_get_linker_section (abfd
, name
);
14850 if (reloc_sec
!= NULL
)
14851 elf_section_data (sec
)->sreloc
= reloc_sec
;
14858 /* Returns the dynamic reloc section associated with SEC. If the
14859 section does not exist it is created and attached to the DYNOBJ
14860 bfd and stored in the SRELOC field of SEC's elf_section_data
14863 ALIGNMENT is the alignment for the newly created section and
14864 IS_RELA defines whether the name should be .rela.<SEC's name>
14865 or .rel.<SEC's name>. The section name is looked up in the
14866 string table associated with ABFD. */
14869 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
14871 unsigned int alignment
,
14873 bfd_boolean is_rela
)
14875 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14877 if (reloc_sec
== NULL
)
14879 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14884 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
14886 if (reloc_sec
== NULL
)
14888 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
14889 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
14890 if ((sec
->flags
& SEC_ALLOC
) != 0)
14891 flags
|= SEC_ALLOC
| SEC_LOAD
;
14893 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
14894 if (reloc_sec
!= NULL
)
14896 /* _bfd_elf_get_sec_type_attr chooses a section type by
14897 name. Override as it may be wrong, eg. for a user
14898 section named "auto" we'll get ".relauto" which is
14899 seen to be a .rela section. */
14900 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
14901 if (!bfd_set_section_alignment (reloc_sec
, alignment
))
14906 elf_section_data (sec
)->sreloc
= reloc_sec
;
14912 /* Copy the ELF symbol type and other attributes for a linker script
14913 assignment from HSRC to HDEST. Generally this should be treated as
14914 if we found a strong non-dynamic definition for HDEST (except that
14915 ld ignores multiple definition errors). */
14917 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
14918 struct bfd_link_hash_entry
*hdest
,
14919 struct bfd_link_hash_entry
*hsrc
)
14921 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
14922 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
14923 Elf_Internal_Sym isym
;
14925 ehdest
->type
= ehsrc
->type
;
14926 ehdest
->target_internal
= ehsrc
->target_internal
;
14928 isym
.st_other
= ehsrc
->other
;
14929 elf_merge_st_other (abfd
, ehdest
, isym
.st_other
, NULL
, TRUE
, FALSE
);
14932 /* Append a RELA relocation REL to section S in BFD. */
14935 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14937 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14938 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
14939 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
14940 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
14943 /* Append a REL relocation REL to section S in BFD. */
14946 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14948 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14949 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
14950 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
14951 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);
14954 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14956 struct bfd_link_hash_entry
*
14957 bfd_elf_define_start_stop (struct bfd_link_info
*info
,
14958 const char *symbol
, asection
*sec
)
14960 struct elf_link_hash_entry
*h
;
14962 h
= elf_link_hash_lookup (elf_hash_table (info
), symbol
,
14963 FALSE
, FALSE
, TRUE
);
14964 /* NB: Common symbols will be turned into definition later. */
14966 && (h
->root
.type
== bfd_link_hash_undefined
14967 || h
->root
.type
== bfd_link_hash_undefweak
14968 || ((h
->ref_regular
|| h
->def_dynamic
)
14970 && h
->root
.type
!= bfd_link_hash_common
)))
14972 bfd_boolean was_dynamic
= h
->ref_dynamic
|| h
->def_dynamic
;
14973 h
->verinfo
.verdef
= NULL
;
14974 h
->root
.type
= bfd_link_hash_defined
;
14975 h
->root
.u
.def
.section
= sec
;
14976 h
->root
.u
.def
.value
= 0;
14977 h
->def_regular
= 1;
14978 h
->def_dynamic
= 0;
14980 h
->u2
.start_stop_section
= sec
;
14981 if (symbol
[0] == '.')
14983 /* .startof. and .sizeof. symbols are local. */
14984 const struct elf_backend_data
*bed
;
14985 bed
= get_elf_backend_data (info
->output_bfd
);
14986 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
14990 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
14991 h
->other
= ((h
->other
& ~ELF_ST_VISIBILITY (-1))
14992 | info
->start_stop_visibility
);
14994 bfd_elf_link_record_dynamic_symbol (info
, h
);
15001 /* Find dynamic relocs for H that apply to read-only sections. */
15004 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry
*h
)
15006 struct elf_dyn_relocs
*p
;
15008 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
15010 asection
*s
= p
->sec
->output_section
;
15012 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
15018 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15019 read-only sections. */
15022 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry
*h
, void *inf
)
15026 if (h
->root
.type
== bfd_link_hash_indirect
)
15029 sec
= _bfd_elf_readonly_dynrelocs (h
);
15032 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
15034 info
->flags
|= DF_TEXTREL
;
15035 /* xgettext:c-format */
15036 info
->callbacks
->minfo (_("%pB: dynamic relocation against `%pT' "
15037 "in read-only section `%pA'\n"),
15038 sec
->owner
, h
->root
.root
.string
, sec
);
15040 if (bfd_link_textrel_check (info
))
15041 /* xgettext:c-format */
15042 info
->callbacks
->einfo (_("%P: %pB: warning: relocation against `%s' "
15043 "in read-only section `%pA'\n"),
15044 sec
->owner
, h
->root
.root
.string
, sec
);
15046 /* Not an error, just cut short the traversal. */
15052 /* Add dynamic tags. */
15055 _bfd_elf_add_dynamic_tags (bfd
*output_bfd
, struct bfd_link_info
*info
,
15056 bfd_boolean need_dynamic_reloc
)
15058 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
15060 if (htab
->dynamic_sections_created
)
15062 /* Add some entries to the .dynamic section. We fill in the
15063 values later, in finish_dynamic_sections, but we must add
15064 the entries now so that we get the correct size for the
15065 .dynamic section. The DT_DEBUG entry is filled in by the
15066 dynamic linker and used by the debugger. */
15067 #define add_dynamic_entry(TAG, VAL) \
15068 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15070 const struct elf_backend_data
*bed
15071 = get_elf_backend_data (output_bfd
);
15073 if (bfd_link_executable (info
))
15075 if (!add_dynamic_entry (DT_DEBUG
, 0))
15079 if (htab
->dt_pltgot_required
|| htab
->splt
->size
!= 0)
15081 /* DT_PLTGOT is used by prelink even if there is no PLT
15083 if (!add_dynamic_entry (DT_PLTGOT
, 0))
15087 if (htab
->dt_jmprel_required
|| htab
->srelplt
->size
!= 0)
15089 if (!add_dynamic_entry (DT_PLTRELSZ
, 0)
15090 || !add_dynamic_entry (DT_PLTREL
,
15091 (bed
->rela_plts_and_copies_p
15092 ? DT_RELA
: DT_REL
))
15093 || !add_dynamic_entry (DT_JMPREL
, 0))
15097 if (htab
->tlsdesc_plt
15098 && (!add_dynamic_entry (DT_TLSDESC_PLT
, 0)
15099 || !add_dynamic_entry (DT_TLSDESC_GOT
, 0)))
15102 if (need_dynamic_reloc
)
15104 if (bed
->rela_plts_and_copies_p
)
15106 if (!add_dynamic_entry (DT_RELA
, 0)
15107 || !add_dynamic_entry (DT_RELASZ
, 0)
15108 || !add_dynamic_entry (DT_RELAENT
,
15109 bed
->s
->sizeof_rela
))
15114 if (!add_dynamic_entry (DT_REL
, 0)
15115 || !add_dynamic_entry (DT_RELSZ
, 0)
15116 || !add_dynamic_entry (DT_RELENT
,
15117 bed
->s
->sizeof_rel
))
15121 /* If any dynamic relocs apply to a read-only section,
15122 then we need a DT_TEXTREL entry. */
15123 if ((info
->flags
& DF_TEXTREL
) == 0)
15124 elf_link_hash_traverse (htab
, _bfd_elf_maybe_set_textrel
,
15127 if ((info
->flags
& DF_TEXTREL
) != 0)
15129 if (htab
->ifunc_resolvers
)
15130 info
->callbacks
->einfo
15131 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15132 "may result in a segfault at runtime; recompile with %s\n"),
15133 bfd_link_dll (info
) ? "-fPIC" : "-fPIE");
15135 if (!add_dynamic_entry (DT_TEXTREL
, 0))
15140 #undef add_dynamic_entry