1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2020 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"
35 /* This struct is used to pass information to routines called via
36 elf_link_hash_traverse which must return failure. */
38 struct elf_info_failed
40 struct bfd_link_info
*info
;
44 /* This structure is used to pass information to
45 _bfd_elf_link_find_version_dependencies. */
47 struct elf_find_verdep_info
49 /* General link information. */
50 struct bfd_link_info
*info
;
51 /* The number of dependencies. */
53 /* Whether we had a failure. */
57 static bfd_boolean _bfd_elf_fix_symbol_flags
58 (struct elf_link_hash_entry
*, struct elf_info_failed
*);
61 _bfd_elf_section_for_symbol (struct elf_reloc_cookie
*cookie
,
62 unsigned long r_symndx
,
65 if (r_symndx
>= cookie
->locsymcount
66 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
68 struct elf_link_hash_entry
*h
;
70 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
72 while (h
->root
.type
== bfd_link_hash_indirect
73 || h
->root
.type
== bfd_link_hash_warning
)
74 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
76 if ((h
->root
.type
== bfd_link_hash_defined
77 || h
->root
.type
== bfd_link_hash_defweak
)
78 && discarded_section (h
->root
.u
.def
.section
))
79 return h
->root
.u
.def
.section
;
85 /* It's not a relocation against a global symbol,
86 but it could be a relocation against a local
87 symbol for a discarded section. */
89 Elf_Internal_Sym
*isym
;
91 /* Need to: get the symbol; get the section. */
92 isym
= &cookie
->locsyms
[r_symndx
];
93 isec
= bfd_section_from_elf_index (cookie
->abfd
, isym
->st_shndx
);
95 && discard
? discarded_section (isec
) : 1)
101 /* Define a symbol in a dynamic linkage section. */
103 struct elf_link_hash_entry
*
104 _bfd_elf_define_linkage_sym (bfd
*abfd
,
105 struct bfd_link_info
*info
,
109 struct elf_link_hash_entry
*h
;
110 struct bfd_link_hash_entry
*bh
;
111 const struct elf_backend_data
*bed
;
113 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
116 /* Zap symbol defined in an as-needed lib that wasn't linked.
117 This is a symptom of a larger problem: Absolute symbols
118 defined in shared libraries can't be overridden, because we
119 lose the link to the bfd which is via the symbol section. */
120 h
->root
.type
= bfd_link_hash_new
;
126 bed
= get_elf_backend_data (abfd
);
127 if (!_bfd_generic_link_add_one_symbol (info
, abfd
, name
, BSF_GLOBAL
,
128 sec
, 0, NULL
, FALSE
, bed
->collect
,
131 h
= (struct elf_link_hash_entry
*) bh
;
132 BFD_ASSERT (h
!= NULL
);
135 h
->root
.linker_def
= 1;
136 h
->type
= STT_OBJECT
;
137 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
138 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
140 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
145 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
149 struct elf_link_hash_entry
*h
;
150 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
151 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
153 /* This function may be called more than once. */
154 if (htab
->sgot
!= NULL
)
157 flags
= bed
->dynamic_sec_flags
;
159 s
= bfd_make_section_anyway_with_flags (abfd
,
160 (bed
->rela_plts_and_copies_p
161 ? ".rela.got" : ".rel.got"),
162 (bed
->dynamic_sec_flags
165 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
169 s
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
171 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
175 if (bed
->want_got_plt
)
177 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
179 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
184 /* The first bit of the global offset table is the header. */
185 s
->size
+= bed
->got_header_size
;
187 if (bed
->want_got_sym
)
189 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
190 (or .got.plt) section. We don't do this in the linker script
191 because we don't want to define the symbol if we are not creating
192 a global offset table. */
193 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
194 "_GLOBAL_OFFSET_TABLE_");
195 elf_hash_table (info
)->hgot
= h
;
203 /* Create a strtab to hold the dynamic symbol names. */
205 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
207 struct elf_link_hash_table
*hash_table
;
209 hash_table
= elf_hash_table (info
);
210 if (hash_table
->dynobj
== NULL
)
212 /* We may not set dynobj, an input file holding linker created
213 dynamic sections to abfd, which may be a dynamic object with
214 its own dynamic sections. We need to find a normal input file
215 to hold linker created sections if possible. */
216 if ((abfd
->flags
& (DYNAMIC
| BFD_PLUGIN
)) != 0)
220 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link
.next
)
222 & (DYNAMIC
| BFD_LINKER_CREATED
| BFD_PLUGIN
)) == 0
223 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
224 && elf_object_id (ibfd
) == elf_hash_table_id (hash_table
)
225 && !((s
= ibfd
->sections
) != NULL
226 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
))
232 hash_table
->dynobj
= abfd
;
235 if (hash_table
->dynstr
== NULL
)
237 hash_table
->dynstr
= _bfd_elf_strtab_init ();
238 if (hash_table
->dynstr
== NULL
)
244 /* Create some sections which will be filled in with dynamic linking
245 information. ABFD is an input file which requires dynamic sections
246 to be created. The dynamic sections take up virtual memory space
247 when the final executable is run, so we need to create them before
248 addresses are assigned to the output sections. We work out the
249 actual contents and size of these sections later. */
252 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
256 const struct elf_backend_data
*bed
;
257 struct elf_link_hash_entry
*h
;
259 if (! is_elf_hash_table (info
->hash
))
262 if (elf_hash_table (info
)->dynamic_sections_created
)
265 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
268 abfd
= elf_hash_table (info
)->dynobj
;
269 bed
= get_elf_backend_data (abfd
);
271 flags
= bed
->dynamic_sec_flags
;
273 /* A dynamically linked executable has a .interp section, but a
274 shared library does not. */
275 if (bfd_link_executable (info
) && !info
->nointerp
)
277 s
= bfd_make_section_anyway_with_flags (abfd
, ".interp",
278 flags
| SEC_READONLY
);
283 /* Create sections to hold version informations. These are removed
284 if they are not needed. */
285 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_d",
286 flags
| SEC_READONLY
);
288 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
291 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version",
292 flags
| SEC_READONLY
);
294 || !bfd_set_section_alignment (s
, 1))
297 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_r",
298 flags
| SEC_READONLY
);
300 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
303 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynsym",
304 flags
| SEC_READONLY
);
306 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
308 elf_hash_table (info
)->dynsym
= s
;
310 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynstr",
311 flags
| SEC_READONLY
);
315 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynamic", flags
);
317 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
320 /* The special symbol _DYNAMIC is always set to the start of the
321 .dynamic section. We could set _DYNAMIC in a linker script, but we
322 only want to define it if we are, in fact, creating a .dynamic
323 section. We don't want to define it if there is no .dynamic
324 section, since on some ELF platforms the start up code examines it
325 to decide how to initialize the process. */
326 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC");
327 elf_hash_table (info
)->hdynamic
= h
;
333 s
= bfd_make_section_anyway_with_flags (abfd
, ".hash",
334 flags
| SEC_READONLY
);
336 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
338 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
341 if (info
->emit_gnu_hash
&& bed
->record_xhash_symbol
== NULL
)
343 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.hash",
344 flags
| SEC_READONLY
);
346 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
348 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
349 4 32-bit words followed by variable count of 64-bit words, then
350 variable count of 32-bit words. */
351 if (bed
->s
->arch_size
== 64)
352 elf_section_data (s
)->this_hdr
.sh_entsize
= 0;
354 elf_section_data (s
)->this_hdr
.sh_entsize
= 4;
357 /* Let the backend create the rest of the sections. This lets the
358 backend set the right flags. The backend will normally create
359 the .got and .plt sections. */
360 if (bed
->elf_backend_create_dynamic_sections
== NULL
361 || ! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
364 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
369 /* Create dynamic sections when linking against a dynamic object. */
372 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
374 flagword flags
, pltflags
;
375 struct elf_link_hash_entry
*h
;
377 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
378 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
380 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
381 .rel[a].bss sections. */
382 flags
= bed
->dynamic_sec_flags
;
385 if (bed
->plt_not_loaded
)
386 /* We do not clear SEC_ALLOC here because we still want the OS to
387 allocate space for the section; it's just that there's nothing
388 to read in from the object file. */
389 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
391 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
392 if (bed
->plt_readonly
)
393 pltflags
|= SEC_READONLY
;
395 s
= bfd_make_section_anyway_with_flags (abfd
, ".plt", pltflags
);
397 || !bfd_set_section_alignment (s
, bed
->plt_alignment
))
401 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
403 if (bed
->want_plt_sym
)
405 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
406 "_PROCEDURE_LINKAGE_TABLE_");
407 elf_hash_table (info
)->hplt
= h
;
412 s
= bfd_make_section_anyway_with_flags (abfd
,
413 (bed
->rela_plts_and_copies_p
414 ? ".rela.plt" : ".rel.plt"),
415 flags
| SEC_READONLY
);
417 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
421 if (! _bfd_elf_create_got_section (abfd
, info
))
424 if (bed
->want_dynbss
)
426 /* The .dynbss section is a place to put symbols which are defined
427 by dynamic objects, are referenced by regular objects, and are
428 not functions. We must allocate space for them in the process
429 image and use a R_*_COPY reloc to tell the dynamic linker to
430 initialize them at run time. The linker script puts the .dynbss
431 section into the .bss section of the final image. */
432 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynbss",
433 SEC_ALLOC
| SEC_LINKER_CREATED
);
438 if (bed
->want_dynrelro
)
440 /* Similarly, but for symbols that were originally in read-only
441 sections. This section doesn't really need to have contents,
442 but make it like other .data.rel.ro sections. */
443 s
= bfd_make_section_anyway_with_flags (abfd
, ".data.rel.ro",
450 /* The .rel[a].bss section holds copy relocs. This section is not
451 normally needed. We need to create it here, though, so that the
452 linker will map it to an output section. We can't just create it
453 only if we need it, because we will not know whether we need it
454 until we have seen all the input files, and the first time the
455 main linker code calls BFD after examining all the input files
456 (size_dynamic_sections) the input sections have already been
457 mapped to the output sections. If the section turns out not to
458 be needed, we can discard it later. We will never need this
459 section when generating a shared object, since they do not use
461 if (bfd_link_executable (info
))
463 s
= bfd_make_section_anyway_with_flags (abfd
,
464 (bed
->rela_plts_and_copies_p
465 ? ".rela.bss" : ".rel.bss"),
466 flags
| SEC_READONLY
);
468 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
472 if (bed
->want_dynrelro
)
474 s
= (bfd_make_section_anyway_with_flags
475 (abfd
, (bed
->rela_plts_and_copies_p
476 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
477 flags
| SEC_READONLY
));
479 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
481 htab
->sreldynrelro
= s
;
489 /* Record a new dynamic symbol. We record the dynamic symbols as we
490 read the input files, since we need to have a list of all of them
491 before we can determine the final sizes of the output sections.
492 Note that we may actually call this function even though we are not
493 going to output any dynamic symbols; in some cases we know that a
494 symbol should be in the dynamic symbol table, but only if there is
498 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
499 struct elf_link_hash_entry
*h
)
501 if (h
->dynindx
== -1)
503 struct elf_strtab_hash
*dynstr
;
508 /* XXX: The ABI draft says the linker must turn hidden and
509 internal symbols into STB_LOCAL symbols when producing the
510 DSO. However, if ld.so honors st_other in the dynamic table,
511 this would not be necessary. */
512 switch (ELF_ST_VISIBILITY (h
->other
))
516 if (h
->root
.type
!= bfd_link_hash_undefined
517 && h
->root
.type
!= bfd_link_hash_undefweak
)
520 if (!elf_hash_table (info
)->is_relocatable_executable
)
528 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
529 ++elf_hash_table (info
)->dynsymcount
;
531 dynstr
= elf_hash_table (info
)->dynstr
;
534 /* Create a strtab to hold the dynamic symbol names. */
535 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
540 /* We don't put any version information in the dynamic string
542 name
= h
->root
.root
.string
;
543 p
= strchr (name
, ELF_VER_CHR
);
545 /* We know that the p points into writable memory. In fact,
546 there are only a few symbols that have read-only names, being
547 those like _GLOBAL_OFFSET_TABLE_ that are created specially
548 by the backends. Most symbols will have names pointing into
549 an ELF string table read from a file, or to objalloc memory. */
552 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
557 if (indx
== (size_t) -1)
559 h
->dynstr_index
= indx
;
565 /* Mark a symbol dynamic. */
568 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
569 struct elf_link_hash_entry
*h
,
570 Elf_Internal_Sym
*sym
)
572 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
574 /* It may be called more than once on the same H. */
575 if(h
->dynamic
|| bfd_link_relocatable (info
))
578 if ((info
->dynamic_data
579 && (h
->type
== STT_OBJECT
580 || h
->type
== STT_COMMON
582 && (ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
583 || ELF_ST_TYPE (sym
->st_info
) == STT_COMMON
))))
586 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
589 /* NB: If a symbol is made dynamic by --dynamic-list, it has
591 h
->root
.non_ir_ref_dynamic
= 1;
595 /* Record an assignment to a symbol made by a linker script. We need
596 this in case some dynamic object refers to this symbol. */
599 bfd_elf_record_link_assignment (bfd
*output_bfd
,
600 struct bfd_link_info
*info
,
605 struct elf_link_hash_entry
*h
, *hv
;
606 struct elf_link_hash_table
*htab
;
607 const struct elf_backend_data
*bed
;
609 if (!is_elf_hash_table (info
->hash
))
612 htab
= elf_hash_table (info
);
613 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
617 if (h
->root
.type
== bfd_link_hash_warning
)
618 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
620 if (h
->versioned
== unknown
)
622 /* Set versioned if symbol version is unknown. */
623 char *version
= strrchr (name
, ELF_VER_CHR
);
626 if (version
> name
&& version
[-1] != ELF_VER_CHR
)
627 h
->versioned
= versioned_hidden
;
629 h
->versioned
= versioned
;
633 /* Symbols defined in a linker script but not referenced anywhere
634 else will have non_elf set. */
637 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
641 switch (h
->root
.type
)
643 case bfd_link_hash_defined
:
644 case bfd_link_hash_defweak
:
645 case bfd_link_hash_common
:
647 case bfd_link_hash_undefweak
:
648 case bfd_link_hash_undefined
:
649 /* Since we're defining the symbol, don't let it seem to have not
650 been defined. record_dynamic_symbol and size_dynamic_sections
651 may depend on this. */
652 h
->root
.type
= bfd_link_hash_new
;
653 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
654 bfd_link_repair_undef_list (&htab
->root
);
656 case bfd_link_hash_new
:
658 case bfd_link_hash_indirect
:
659 /* We had a versioned symbol in a dynamic library. We make the
660 the versioned symbol point to this one. */
661 bed
= get_elf_backend_data (output_bfd
);
663 while (hv
->root
.type
== bfd_link_hash_indirect
664 || hv
->root
.type
== bfd_link_hash_warning
)
665 hv
= (struct elf_link_hash_entry
*) hv
->root
.u
.i
.link
;
666 /* We don't need to update h->root.u since linker will set them
668 h
->root
.type
= bfd_link_hash_undefined
;
669 hv
->root
.type
= bfd_link_hash_indirect
;
670 hv
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
671 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hv
);
678 /* If this symbol is being provided by the linker script, and it is
679 currently defined by a dynamic object, but not by a regular
680 object, then mark it as undefined so that the generic linker will
681 force the correct value. */
685 h
->root
.type
= bfd_link_hash_undefined
;
687 /* If this symbol is currently defined by a dynamic object, but not
688 by a regular object, then clear out any version information because
689 the symbol will not be associated with the dynamic object any
691 if (h
->def_dynamic
&& !h
->def_regular
)
692 h
->verinfo
.verdef
= NULL
;
694 /* Make sure this symbol is not garbage collected. */
701 bed
= get_elf_backend_data (output_bfd
);
702 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
703 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
704 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
707 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
709 if (!bfd_link_relocatable (info
)
711 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
712 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
717 || bfd_link_dll (info
)
718 || elf_hash_table (info
)->is_relocatable_executable
)
722 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
725 /* If this is a weak defined symbol, and we know a corresponding
726 real symbol from the same dynamic object, make sure the real
727 symbol is also made into a dynamic symbol. */
730 struct elf_link_hash_entry
*def
= weakdef (h
);
732 if (def
->dynindx
== -1
733 && !bfd_elf_link_record_dynamic_symbol (info
, def
))
741 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
742 success, and 2 on a failure caused by attempting to record a symbol
743 in a discarded section, eg. a discarded link-once section symbol. */
746 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
751 struct elf_link_local_dynamic_entry
*entry
;
752 struct elf_link_hash_table
*eht
;
753 struct elf_strtab_hash
*dynstr
;
756 Elf_External_Sym_Shndx eshndx
;
757 char esym
[sizeof (Elf64_External_Sym
)];
759 if (! is_elf_hash_table (info
->hash
))
762 /* See if the entry exists already. */
763 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
764 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
767 amt
= sizeof (*entry
);
768 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
772 /* Go find the symbol, so that we can find it's name. */
773 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
774 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
776 bfd_release (input_bfd
, entry
);
780 if (entry
->isym
.st_shndx
!= SHN_UNDEF
781 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
785 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
786 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
788 /* We can still bfd_release here as nothing has done another
789 bfd_alloc. We can't do this later in this function. */
790 bfd_release (input_bfd
, entry
);
795 name
= (bfd_elf_string_from_elf_section
796 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
797 entry
->isym
.st_name
));
799 dynstr
= elf_hash_table (info
)->dynstr
;
802 /* Create a strtab to hold the dynamic symbol names. */
803 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
808 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
809 if (dynstr_index
== (size_t) -1)
811 entry
->isym
.st_name
= dynstr_index
;
813 eht
= elf_hash_table (info
);
815 entry
->next
= eht
->dynlocal
;
816 eht
->dynlocal
= entry
;
817 entry
->input_bfd
= input_bfd
;
818 entry
->input_indx
= input_indx
;
821 /* Whatever binding the symbol had before, it's now local. */
823 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
825 /* The dynindx will be set at the end of size_dynamic_sections. */
830 /* Return the dynindex of a local dynamic symbol. */
833 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
837 struct elf_link_local_dynamic_entry
*e
;
839 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
840 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
845 /* This function is used to renumber the dynamic symbols, if some of
846 them are removed because they are marked as local. This is called
847 via elf_link_hash_traverse. */
850 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
853 size_t *count
= (size_t *) data
;
858 if (h
->dynindx
!= -1)
859 h
->dynindx
= ++(*count
);
865 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
866 STB_LOCAL binding. */
869 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
872 size_t *count
= (size_t *) data
;
874 if (!h
->forced_local
)
877 if (h
->dynindx
!= -1)
878 h
->dynindx
= ++(*count
);
883 /* Return true if the dynamic symbol for a given section should be
884 omitted when creating a shared library. */
886 _bfd_elf_omit_section_dynsym_default (bfd
*output_bfd ATTRIBUTE_UNUSED
,
887 struct bfd_link_info
*info
,
890 struct elf_link_hash_table
*htab
;
893 switch (elf_section_data (p
)->this_hdr
.sh_type
)
897 /* If sh_type is yet undecided, assume it could be
898 SHT_PROGBITS/SHT_NOBITS. */
900 htab
= elf_hash_table (info
);
901 if (htab
->text_index_section
!= NULL
)
902 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
904 return (htab
->dynobj
!= NULL
905 && (ip
= bfd_get_linker_section (htab
->dynobj
, p
->name
)) != NULL
906 && ip
->output_section
== p
);
908 /* There shouldn't be section relative relocations
909 against any other section. */
916 _bfd_elf_omit_section_dynsym_all
917 (bfd
*output_bfd ATTRIBUTE_UNUSED
,
918 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
919 asection
*p ATTRIBUTE_UNUSED
)
924 /* Assign dynsym indices. In a shared library we generate a section
925 symbol for each output section, which come first. Next come symbols
926 which have been forced to local binding. Then all of the back-end
927 allocated local dynamic syms, followed by the rest of the global
928 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
929 (This prevents the early call before elf_backend_init_index_section
930 and strip_excluded_output_sections setting dynindx for sections
931 that are stripped.) */
934 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
935 struct bfd_link_info
*info
,
936 unsigned long *section_sym_count
)
938 unsigned long dynsymcount
= 0;
939 bfd_boolean do_sec
= section_sym_count
!= NULL
;
941 if (bfd_link_pic (info
)
942 || elf_hash_table (info
)->is_relocatable_executable
)
944 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
946 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
947 if ((p
->flags
& SEC_EXCLUDE
) == 0
948 && (p
->flags
& SEC_ALLOC
) != 0
949 && elf_hash_table (info
)->dynamic_relocs
950 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
954 elf_section_data (p
)->dynindx
= dynsymcount
;
957 elf_section_data (p
)->dynindx
= 0;
960 *section_sym_count
= dynsymcount
;
962 elf_link_hash_traverse (elf_hash_table (info
),
963 elf_link_renumber_local_hash_table_dynsyms
,
966 if (elf_hash_table (info
)->dynlocal
)
968 struct elf_link_local_dynamic_entry
*p
;
969 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
970 p
->dynindx
= ++dynsymcount
;
972 elf_hash_table (info
)->local_dynsymcount
= dynsymcount
;
974 elf_link_hash_traverse (elf_hash_table (info
),
975 elf_link_renumber_hash_table_dynsyms
,
978 /* There is an unused NULL entry at the head of the table which we
979 must account for in our count even if the table is empty since it
980 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
984 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
988 /* Merge st_other field. */
991 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
992 const Elf_Internal_Sym
*isym
, asection
*sec
,
993 bfd_boolean definition
, bfd_boolean dynamic
)
995 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
997 /* If st_other has a processor-specific meaning, specific
998 code might be needed here. */
999 if (bed
->elf_backend_merge_symbol_attribute
)
1000 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
1005 unsigned symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
1006 unsigned hvis
= ELF_ST_VISIBILITY (h
->other
);
1008 /* Keep the most constraining visibility. Leave the remainder
1009 of the st_other field to elf_backend_merge_symbol_attribute. */
1010 if (symvis
- 1 < hvis
- 1)
1011 h
->other
= symvis
| (h
->other
& ~ELF_ST_VISIBILITY (-1));
1014 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_DEFAULT
1015 && (sec
->flags
& SEC_READONLY
) == 0)
1016 h
->protected_def
= 1;
1019 /* This function is called when we want to merge a new symbol with an
1020 existing symbol. It handles the various cases which arise when we
1021 find a definition in a dynamic object, or when there is already a
1022 definition in a dynamic object. The new symbol is described by
1023 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1024 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1025 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1026 of an old common symbol. We set OVERRIDE if the old symbol is
1027 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1028 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1029 to change. By OK to change, we mean that we shouldn't warn if the
1030 type or size does change. */
1033 _bfd_elf_merge_symbol (bfd
*abfd
,
1034 struct bfd_link_info
*info
,
1036 Elf_Internal_Sym
*sym
,
1039 struct elf_link_hash_entry
**sym_hash
,
1041 bfd_boolean
*pold_weak
,
1042 unsigned int *pold_alignment
,
1044 bfd_boolean
*override
,
1045 bfd_boolean
*type_change_ok
,
1046 bfd_boolean
*size_change_ok
,
1047 bfd_boolean
*matched
)
1049 asection
*sec
, *oldsec
;
1050 struct elf_link_hash_entry
*h
;
1051 struct elf_link_hash_entry
*hi
;
1052 struct elf_link_hash_entry
*flip
;
1055 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
1056 bfd_boolean newweak
, oldweak
, newfunc
, oldfunc
;
1057 const struct elf_backend_data
*bed
;
1059 bfd_boolean default_sym
= *matched
;
1065 bind
= ELF_ST_BIND (sym
->st_info
);
1067 if (! bfd_is_und_section (sec
))
1068 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
1070 h
= ((struct elf_link_hash_entry
*)
1071 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
1076 bed
= get_elf_backend_data (abfd
);
1078 /* NEW_VERSION is the symbol version of the new symbol. */
1079 if (h
->versioned
!= unversioned
)
1081 /* Symbol version is unknown or versioned. */
1082 new_version
= strrchr (name
, ELF_VER_CHR
);
1085 if (h
->versioned
== unknown
)
1087 if (new_version
> name
&& new_version
[-1] != ELF_VER_CHR
)
1088 h
->versioned
= versioned_hidden
;
1090 h
->versioned
= versioned
;
1093 if (new_version
[0] == '\0')
1097 h
->versioned
= unversioned
;
1102 /* For merging, we only care about real symbols. But we need to make
1103 sure that indirect symbol dynamic flags are updated. */
1105 while (h
->root
.type
== bfd_link_hash_indirect
1106 || h
->root
.type
== bfd_link_hash_warning
)
1107 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1111 if (hi
== h
|| h
->root
.type
== bfd_link_hash_new
)
1115 /* OLD_HIDDEN is true if the existing symbol is only visible
1116 to the symbol with the same symbol version. NEW_HIDDEN is
1117 true if the new symbol is only visible to the symbol with
1118 the same symbol version. */
1119 bfd_boolean old_hidden
= h
->versioned
== versioned_hidden
;
1120 bfd_boolean new_hidden
= hi
->versioned
== versioned_hidden
;
1121 if (!old_hidden
&& !new_hidden
)
1122 /* The new symbol matches the existing symbol if both
1127 /* OLD_VERSION is the symbol version of the existing
1131 if (h
->versioned
>= versioned
)
1132 old_version
= strrchr (h
->root
.root
.string
,
1137 /* The new symbol matches the existing symbol if they
1138 have the same symbol version. */
1139 *matched
= (old_version
== new_version
1140 || (old_version
!= NULL
1141 && new_version
!= NULL
1142 && strcmp (old_version
, new_version
) == 0));
1147 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1152 switch (h
->root
.type
)
1157 case bfd_link_hash_undefined
:
1158 case bfd_link_hash_undefweak
:
1159 oldbfd
= h
->root
.u
.undef
.abfd
;
1162 case bfd_link_hash_defined
:
1163 case bfd_link_hash_defweak
:
1164 oldbfd
= h
->root
.u
.def
.section
->owner
;
1165 oldsec
= h
->root
.u
.def
.section
;
1168 case bfd_link_hash_common
:
1169 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1170 oldsec
= h
->root
.u
.c
.p
->section
;
1172 *pold_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1175 if (poldbfd
&& *poldbfd
== NULL
)
1178 /* Differentiate strong and weak symbols. */
1179 newweak
= bind
== STB_WEAK
;
1180 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1181 || h
->root
.type
== bfd_link_hash_undefweak
);
1183 *pold_weak
= oldweak
;
1185 /* We have to check it for every instance since the first few may be
1186 references and not all compilers emit symbol type for undefined
1188 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1190 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1191 respectively, is from a dynamic object. */
1193 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1195 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1196 syms and defined syms in dynamic libraries respectively.
1197 ref_dynamic on the other hand can be set for a symbol defined in
1198 a dynamic library, and def_dynamic may not be set; When the
1199 definition in a dynamic lib is overridden by a definition in the
1200 executable use of the symbol in the dynamic lib becomes a
1201 reference to the executable symbol. */
1204 if (bfd_is_und_section (sec
))
1206 if (bind
!= STB_WEAK
)
1208 h
->ref_dynamic_nonweak
= 1;
1209 hi
->ref_dynamic_nonweak
= 1;
1214 /* Update the existing symbol only if they match. */
1217 hi
->dynamic_def
= 1;
1221 /* If we just created the symbol, mark it as being an ELF symbol.
1222 Other than that, there is nothing to do--there is no merge issue
1223 with a newly defined symbol--so we just return. */
1225 if (h
->root
.type
== bfd_link_hash_new
)
1231 /* In cases involving weak versioned symbols, we may wind up trying
1232 to merge a symbol with itself. Catch that here, to avoid the
1233 confusion that results if we try to override a symbol with
1234 itself. The additional tests catch cases like
1235 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1236 dynamic object, which we do want to handle here. */
1238 && (newweak
|| oldweak
)
1239 && ((abfd
->flags
& DYNAMIC
) == 0
1240 || !h
->def_regular
))
1245 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1246 else if (oldsec
!= NULL
)
1248 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1249 indices used by MIPS ELF. */
1250 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1253 /* Handle a case where plugin_notice won't be called and thus won't
1254 set the non_ir_ref flags on the first pass over symbols. */
1256 && (oldbfd
->flags
& BFD_PLUGIN
) != (abfd
->flags
& BFD_PLUGIN
)
1257 && newdyn
!= olddyn
)
1259 h
->root
.non_ir_ref_dynamic
= TRUE
;
1260 hi
->root
.non_ir_ref_dynamic
= TRUE
;
1263 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1264 respectively, appear to be a definition rather than reference. */
1266 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1268 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1269 && h
->root
.type
!= bfd_link_hash_undefweak
1270 && h
->root
.type
!= bfd_link_hash_common
);
1272 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1273 respectively, appear to be a function. */
1275 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1276 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1278 oldfunc
= (h
->type
!= STT_NOTYPE
1279 && bed
->is_function_type (h
->type
));
1281 if (!(newfunc
&& oldfunc
)
1282 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1283 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1284 && h
->type
!= STT_NOTYPE
1285 && (newdef
|| bfd_is_com_section (sec
))
1286 && (olddef
|| h
->root
.type
== bfd_link_hash_common
))
1288 /* If creating a default indirect symbol ("foo" or "foo@") from
1289 a dynamic versioned definition ("foo@@") skip doing so if
1290 there is an existing regular definition with a different
1291 type. We don't want, for example, a "time" variable in the
1292 executable overriding a "time" function in a shared library. */
1300 /* When adding a symbol from a regular object file after we have
1301 created indirect symbols, undo the indirection and any
1308 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1309 h
->forced_local
= 0;
1313 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1315 h
->root
.type
= bfd_link_hash_undefined
;
1316 h
->root
.u
.undef
.abfd
= abfd
;
1320 h
->root
.type
= bfd_link_hash_new
;
1321 h
->root
.u
.undef
.abfd
= NULL
;
1327 /* Check TLS symbols. We don't check undefined symbols introduced
1328 by "ld -u" which have no type (and oldbfd NULL), and we don't
1329 check symbols from plugins because they also have no type. */
1331 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1332 && (abfd
->flags
& BFD_PLUGIN
) == 0
1333 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1334 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1337 bfd_boolean ntdef
, tdef
;
1338 asection
*ntsec
, *tsec
;
1340 if (h
->type
== STT_TLS
)
1361 /* xgettext:c-format */
1362 (_("%s: TLS definition in %pB section %pA "
1363 "mismatches non-TLS definition in %pB section %pA"),
1364 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
, ntsec
);
1365 else if (!tdef
&& !ntdef
)
1367 /* xgettext:c-format */
1368 (_("%s: TLS reference in %pB "
1369 "mismatches non-TLS reference in %pB"),
1370 h
->root
.root
.string
, tbfd
, ntbfd
);
1373 /* xgettext:c-format */
1374 (_("%s: TLS definition in %pB section %pA "
1375 "mismatches non-TLS reference in %pB"),
1376 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
);
1379 /* xgettext:c-format */
1380 (_("%s: TLS reference in %pB "
1381 "mismatches non-TLS definition in %pB section %pA"),
1382 h
->root
.root
.string
, tbfd
, ntbfd
, ntsec
);
1384 bfd_set_error (bfd_error_bad_value
);
1388 /* If the old symbol has non-default visibility, we ignore the new
1389 definition from a dynamic object. */
1391 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1392 && !bfd_is_und_section (sec
))
1395 /* Make sure this symbol is dynamic. */
1397 hi
->ref_dynamic
= 1;
1398 /* A protected symbol has external availability. Make sure it is
1399 recorded as dynamic.
1401 FIXME: Should we check type and size for protected symbol? */
1402 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1403 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1408 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1411 /* If the new symbol with non-default visibility comes from a
1412 relocatable file and the old definition comes from a dynamic
1413 object, we remove the old definition. */
1414 if (hi
->root
.type
== bfd_link_hash_indirect
)
1416 /* Handle the case where the old dynamic definition is
1417 default versioned. We need to copy the symbol info from
1418 the symbol with default version to the normal one if it
1419 was referenced before. */
1422 hi
->root
.type
= h
->root
.type
;
1423 h
->root
.type
= bfd_link_hash_indirect
;
1424 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1426 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1427 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1429 /* If the new symbol is hidden or internal, completely undo
1430 any dynamic link state. */
1431 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1432 h
->forced_local
= 0;
1439 /* FIXME: Should we check type and size for protected symbol? */
1449 /* If the old symbol was undefined before, then it will still be
1450 on the undefs list. If the new symbol is undefined or
1451 common, we can't make it bfd_link_hash_new here, because new
1452 undefined or common symbols will be added to the undefs list
1453 by _bfd_generic_link_add_one_symbol. Symbols may not be
1454 added twice to the undefs list. Also, if the new symbol is
1455 undefweak then we don't want to lose the strong undef. */
1456 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1458 h
->root
.type
= bfd_link_hash_undefined
;
1459 h
->root
.u
.undef
.abfd
= abfd
;
1463 h
->root
.type
= bfd_link_hash_new
;
1464 h
->root
.u
.undef
.abfd
= NULL
;
1467 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1469 /* If the new symbol is hidden or internal, completely undo
1470 any dynamic link state. */
1471 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1472 h
->forced_local
= 0;
1478 /* FIXME: Should we check type and size for protected symbol? */
1484 /* If a new weak symbol definition comes from a regular file and the
1485 old symbol comes from a dynamic library, we treat the new one as
1486 strong. Similarly, an old weak symbol definition from a regular
1487 file is treated as strong when the new symbol comes from a dynamic
1488 library. Further, an old weak symbol from a dynamic library is
1489 treated as strong if the new symbol is from a dynamic library.
1490 This reflects the way glibc's ld.so works.
1492 Also allow a weak symbol to override a linker script symbol
1493 defined by an early pass over the script. This is done so the
1494 linker knows the symbol is defined in an object file, for the
1495 DEFINED script function.
1497 Do this before setting *type_change_ok or *size_change_ok so that
1498 we warn properly when dynamic library symbols are overridden. */
1500 if (newdef
&& !newdyn
&& (olddyn
|| h
->root
.ldscript_def
))
1502 if (olddef
&& newdyn
)
1505 /* Allow changes between different types of function symbol. */
1506 if (newfunc
&& oldfunc
)
1507 *type_change_ok
= TRUE
;
1509 /* It's OK to change the type if either the existing symbol or the
1510 new symbol is weak. A type change is also OK if the old symbol
1511 is undefined and the new symbol is defined. */
1516 && h
->root
.type
== bfd_link_hash_undefined
))
1517 *type_change_ok
= TRUE
;
1519 /* It's OK to change the size if either the existing symbol or the
1520 new symbol is weak, or if the old symbol is undefined. */
1523 || h
->root
.type
== bfd_link_hash_undefined
)
1524 *size_change_ok
= TRUE
;
1526 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1527 symbol, respectively, appears to be a common symbol in a dynamic
1528 object. If a symbol appears in an uninitialized section, and is
1529 not weak, and is not a function, then it may be a common symbol
1530 which was resolved when the dynamic object was created. We want
1531 to treat such symbols specially, because they raise special
1532 considerations when setting the symbol size: if the symbol
1533 appears as a common symbol in a regular object, and the size in
1534 the regular object is larger, we must make sure that we use the
1535 larger size. This problematic case can always be avoided in C,
1536 but it must be handled correctly when using Fortran shared
1539 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1540 likewise for OLDDYNCOMMON and OLDDEF.
1542 Note that this test is just a heuristic, and that it is quite
1543 possible to have an uninitialized symbol in a shared object which
1544 is really a definition, rather than a common symbol. This could
1545 lead to some minor confusion when the symbol really is a common
1546 symbol in some regular object. However, I think it will be
1552 && (sec
->flags
& SEC_ALLOC
) != 0
1553 && (sec
->flags
& SEC_LOAD
) == 0
1556 newdyncommon
= TRUE
;
1558 newdyncommon
= FALSE
;
1562 && h
->root
.type
== bfd_link_hash_defined
1564 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1565 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1568 olddyncommon
= TRUE
;
1570 olddyncommon
= FALSE
;
1572 /* We now know everything about the old and new symbols. We ask the
1573 backend to check if we can merge them. */
1574 if (bed
->merge_symbol
!= NULL
)
1576 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1581 /* There are multiple definitions of a normal symbol. Skip the
1582 default symbol as well as definition from an IR object. */
1583 if (olddef
&& !olddyn
&& !oldweak
&& newdef
&& !newdyn
&& !newweak
1584 && !default_sym
&& h
->def_regular
1586 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1587 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1589 /* Handle a multiple definition. */
1590 (*info
->callbacks
->multiple_definition
) (info
, &h
->root
,
1591 abfd
, sec
, *pvalue
);
1596 /* If both the old and the new symbols look like common symbols in a
1597 dynamic object, set the size of the symbol to the larger of the
1602 && sym
->st_size
!= h
->size
)
1604 /* Since we think we have two common symbols, issue a multiple
1605 common warning if desired. Note that we only warn if the
1606 size is different. If the size is the same, we simply let
1607 the old symbol override the new one as normally happens with
1608 symbols defined in dynamic objects. */
1610 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1611 bfd_link_hash_common
, sym
->st_size
);
1612 if (sym
->st_size
> h
->size
)
1613 h
->size
= sym
->st_size
;
1615 *size_change_ok
= TRUE
;
1618 /* If we are looking at a dynamic object, and we have found a
1619 definition, we need to see if the symbol was already defined by
1620 some other object. If so, we want to use the existing
1621 definition, and we do not want to report a multiple symbol
1622 definition error; we do this by clobbering *PSEC to be
1623 bfd_und_section_ptr.
1625 We treat a common symbol as a definition if the symbol in the
1626 shared library is a function, since common symbols always
1627 represent variables; this can cause confusion in principle, but
1628 any such confusion would seem to indicate an erroneous program or
1629 shared library. We also permit a common symbol in a regular
1630 object to override a weak symbol in a shared object. */
1635 || (h
->root
.type
== bfd_link_hash_common
1636 && (newweak
|| newfunc
))))
1640 newdyncommon
= FALSE
;
1642 *psec
= sec
= bfd_und_section_ptr
;
1643 *size_change_ok
= TRUE
;
1645 /* If we get here when the old symbol is a common symbol, then
1646 we are explicitly letting it override a weak symbol or
1647 function in a dynamic object, and we don't want to warn about
1648 a type change. If the old symbol is a defined symbol, a type
1649 change warning may still be appropriate. */
1651 if (h
->root
.type
== bfd_link_hash_common
)
1652 *type_change_ok
= TRUE
;
1655 /* Handle the special case of an old common symbol merging with a
1656 new symbol which looks like a common symbol in a shared object.
1657 We change *PSEC and *PVALUE to make the new symbol look like a
1658 common symbol, and let _bfd_generic_link_add_one_symbol do the
1662 && h
->root
.type
== bfd_link_hash_common
)
1666 newdyncommon
= FALSE
;
1667 *pvalue
= sym
->st_size
;
1668 *psec
= sec
= bed
->common_section (oldsec
);
1669 *size_change_ok
= TRUE
;
1672 /* Skip weak definitions of symbols that are already defined. */
1673 if (newdef
&& olddef
&& newweak
)
1675 /* Don't skip new non-IR weak syms. */
1676 if (!(oldbfd
!= NULL
1677 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1678 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1684 /* Merge st_other. If the symbol already has a dynamic index,
1685 but visibility says it should not be visible, turn it into a
1687 elf_merge_st_other (abfd
, h
, sym
, sec
, newdef
, newdyn
);
1688 if (h
->dynindx
!= -1)
1689 switch (ELF_ST_VISIBILITY (h
->other
))
1693 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1698 /* If the old symbol is from a dynamic object, and the new symbol is
1699 a definition which is not from a dynamic object, then the new
1700 symbol overrides the old symbol. Symbols from regular files
1701 always take precedence over symbols from dynamic objects, even if
1702 they are defined after the dynamic object in the link.
1704 As above, we again permit a common symbol in a regular object to
1705 override a definition in a shared object if the shared object
1706 symbol is a function or is weak. */
1711 || (bfd_is_com_section (sec
)
1712 && (oldweak
|| oldfunc
)))
1717 /* Change the hash table entry to undefined, and let
1718 _bfd_generic_link_add_one_symbol do the right thing with the
1721 h
->root
.type
= bfd_link_hash_undefined
;
1722 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1723 *size_change_ok
= TRUE
;
1726 olddyncommon
= FALSE
;
1728 /* We again permit a type change when a common symbol may be
1729 overriding a function. */
1731 if (bfd_is_com_section (sec
))
1735 /* If a common symbol overrides a function, make sure
1736 that it isn't defined dynamically nor has type
1739 h
->type
= STT_NOTYPE
;
1741 *type_change_ok
= TRUE
;
1744 if (hi
->root
.type
== bfd_link_hash_indirect
)
1747 /* This union may have been set to be non-NULL when this symbol
1748 was seen in a dynamic object. We must force the union to be
1749 NULL, so that it is correct for a regular symbol. */
1750 h
->verinfo
.vertree
= NULL
;
1753 /* Handle the special case of a new common symbol merging with an
1754 old symbol that looks like it might be a common symbol defined in
1755 a shared object. Note that we have already handled the case in
1756 which a new common symbol should simply override the definition
1757 in the shared library. */
1760 && bfd_is_com_section (sec
)
1763 /* It would be best if we could set the hash table entry to a
1764 common symbol, but we don't know what to use for the section
1765 or the alignment. */
1766 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1767 bfd_link_hash_common
, sym
->st_size
);
1769 /* If the presumed common symbol in the dynamic object is
1770 larger, pretend that the new symbol has its size. */
1772 if (h
->size
> *pvalue
)
1775 /* We need to remember the alignment required by the symbol
1776 in the dynamic object. */
1777 BFD_ASSERT (pold_alignment
);
1778 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1781 olddyncommon
= FALSE
;
1783 h
->root
.type
= bfd_link_hash_undefined
;
1784 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1786 *size_change_ok
= TRUE
;
1787 *type_change_ok
= TRUE
;
1789 if (hi
->root
.type
== bfd_link_hash_indirect
)
1792 h
->verinfo
.vertree
= NULL
;
1797 /* Handle the case where we had a versioned symbol in a dynamic
1798 library and now find a definition in a normal object. In this
1799 case, we make the versioned symbol point to the normal one. */
1800 flip
->root
.type
= h
->root
.type
;
1801 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1802 h
->root
.type
= bfd_link_hash_indirect
;
1803 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1804 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1808 flip
->ref_dynamic
= 1;
1815 /* This function is called to create an indirect symbol from the
1816 default for the symbol with the default version if needed. The
1817 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1818 set DYNSYM if the new indirect symbol is dynamic. */
1821 _bfd_elf_add_default_symbol (bfd
*abfd
,
1822 struct bfd_link_info
*info
,
1823 struct elf_link_hash_entry
*h
,
1825 Elf_Internal_Sym
*sym
,
1829 bfd_boolean
*dynsym
)
1831 bfd_boolean type_change_ok
;
1832 bfd_boolean size_change_ok
;
1835 struct elf_link_hash_entry
*hi
;
1836 struct bfd_link_hash_entry
*bh
;
1837 const struct elf_backend_data
*bed
;
1838 bfd_boolean collect
;
1839 bfd_boolean dynamic
;
1840 bfd_boolean override
;
1842 size_t len
, shortlen
;
1844 bfd_boolean matched
;
1846 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1849 /* If this symbol has a version, and it is the default version, we
1850 create an indirect symbol from the default name to the fully
1851 decorated name. This will cause external references which do not
1852 specify a version to be bound to this version of the symbol. */
1853 p
= strchr (name
, ELF_VER_CHR
);
1854 if (h
->versioned
== unknown
)
1858 h
->versioned
= unversioned
;
1863 if (p
[1] != ELF_VER_CHR
)
1865 h
->versioned
= versioned_hidden
;
1869 h
->versioned
= versioned
;
1874 /* PR ld/19073: We may see an unversioned definition after the
1880 bed
= get_elf_backend_data (abfd
);
1881 collect
= bed
->collect
;
1882 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1884 shortlen
= p
- name
;
1885 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1886 if (shortname
== NULL
)
1888 memcpy (shortname
, name
, shortlen
);
1889 shortname
[shortlen
] = '\0';
1891 /* We are going to create a new symbol. Merge it with any existing
1892 symbol with this name. For the purposes of the merge, act as
1893 though we were defining the symbol we just defined, although we
1894 actually going to define an indirect symbol. */
1895 type_change_ok
= FALSE
;
1896 size_change_ok
= FALSE
;
1899 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1900 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1901 &type_change_ok
, &size_change_ok
, &matched
))
1907 if (hi
->def_regular
|| ELF_COMMON_DEF_P (hi
))
1909 /* If the undecorated symbol will have a version added by a
1910 script different to H, then don't indirect to/from the
1911 undecorated symbol. This isn't ideal because we may not yet
1912 have seen symbol versions, if given by a script on the
1913 command line rather than via --version-script. */
1914 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
1919 = bfd_find_version_for_sym (info
->version_info
,
1920 hi
->root
.root
.string
, &hide
);
1921 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
1923 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
1927 if (hi
->verinfo
.vertree
!= NULL
1928 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
1934 /* Add the default symbol if not performing a relocatable link. */
1935 if (! bfd_link_relocatable (info
))
1938 if (bh
->type
== bfd_link_hash_defined
1939 && bh
->u
.def
.section
->owner
!= NULL
1940 && (bh
->u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
1942 /* Mark the previous definition from IR object as
1943 undefined so that the generic linker will override
1945 bh
->type
= bfd_link_hash_undefined
;
1946 bh
->u
.undef
.abfd
= bh
->u
.def
.section
->owner
;
1948 if (! (_bfd_generic_link_add_one_symbol
1949 (info
, abfd
, shortname
, BSF_INDIRECT
,
1950 bfd_ind_section_ptr
,
1951 0, name
, FALSE
, collect
, &bh
)))
1953 hi
= (struct elf_link_hash_entry
*) bh
;
1958 /* In this case the symbol named SHORTNAME is overriding the
1959 indirect symbol we want to add. We were planning on making
1960 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1961 is the name without a version. NAME is the fully versioned
1962 name, and it is the default version.
1964 Overriding means that we already saw a definition for the
1965 symbol SHORTNAME in a regular object, and it is overriding
1966 the symbol defined in the dynamic object.
1968 When this happens, we actually want to change NAME, the
1969 symbol we just added, to refer to SHORTNAME. This will cause
1970 references to NAME in the shared object to become references
1971 to SHORTNAME in the regular object. This is what we expect
1972 when we override a function in a shared object: that the
1973 references in the shared object will be mapped to the
1974 definition in the regular object. */
1976 while (hi
->root
.type
== bfd_link_hash_indirect
1977 || hi
->root
.type
== bfd_link_hash_warning
)
1978 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1980 h
->root
.type
= bfd_link_hash_indirect
;
1981 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1985 hi
->ref_dynamic
= 1;
1989 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1994 /* Now set HI to H, so that the following code will set the
1995 other fields correctly. */
1999 /* Check if HI is a warning symbol. */
2000 if (hi
->root
.type
== bfd_link_hash_warning
)
2001 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2003 /* If there is a duplicate definition somewhere, then HI may not
2004 point to an indirect symbol. We will have reported an error to
2005 the user in that case. */
2007 if (hi
->root
.type
== bfd_link_hash_indirect
)
2009 struct elf_link_hash_entry
*ht
;
2011 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2012 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
2014 /* A reference to the SHORTNAME symbol from a dynamic library
2015 will be satisfied by the versioned symbol at runtime. In
2016 effect, we have a reference to the versioned symbol. */
2017 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2018 hi
->dynamic_def
|= ht
->dynamic_def
;
2020 /* See if the new flags lead us to realize that the symbol must
2026 if (! bfd_link_executable (info
)
2033 if (hi
->ref_regular
)
2039 /* We also need to define an indirection from the nondefault version
2043 len
= strlen (name
);
2044 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
2045 if (shortname
== NULL
)
2047 memcpy (shortname
, name
, shortlen
);
2048 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
2050 /* Once again, merge with any existing symbol. */
2051 type_change_ok
= FALSE
;
2052 size_change_ok
= FALSE
;
2054 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
2055 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
2056 &type_change_ok
, &size_change_ok
, &matched
))
2064 /* Here SHORTNAME is a versioned name, so we don't expect to see
2065 the type of override we do in the case above unless it is
2066 overridden by a versioned definition. */
2067 if (hi
->root
.type
!= bfd_link_hash_defined
2068 && hi
->root
.type
!= bfd_link_hash_defweak
)
2070 /* xgettext:c-format */
2071 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2077 if (! (_bfd_generic_link_add_one_symbol
2078 (info
, abfd
, shortname
, BSF_INDIRECT
,
2079 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
2081 hi
= (struct elf_link_hash_entry
*) bh
;
2083 /* If there is a duplicate definition somewhere, then HI may not
2084 point to an indirect symbol. We will have reported an error
2085 to the user in that case. */
2087 if (hi
->root
.type
== bfd_link_hash_indirect
)
2089 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
2090 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2091 hi
->dynamic_def
|= h
->dynamic_def
;
2093 /* See if the new flags lead us to realize that the symbol
2099 if (! bfd_link_executable (info
)
2105 if (hi
->ref_regular
)
2115 /* This routine is used to export all defined symbols into the dynamic
2116 symbol table. It is called via elf_link_hash_traverse. */
2119 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
2121 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2123 /* Ignore indirect symbols. These are added by the versioning code. */
2124 if (h
->root
.type
== bfd_link_hash_indirect
)
2127 /* Ignore this if we won't export it. */
2128 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2131 if (h
->dynindx
== -1
2132 && (h
->def_regular
|| h
->ref_regular
)
2133 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2134 h
->root
.root
.string
))
2136 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2146 /* Look through the symbols which are defined in other shared
2147 libraries and referenced here. Update the list of version
2148 dependencies. This will be put into the .gnu.version_r section.
2149 This function is called via elf_link_hash_traverse. */
2152 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2155 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2156 Elf_Internal_Verneed
*t
;
2157 Elf_Internal_Vernaux
*a
;
2160 /* We only care about symbols defined in shared objects with version
2165 || h
->verinfo
.verdef
== NULL
2166 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2167 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2170 /* See if we already know about this version. */
2171 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2175 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2178 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2179 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2185 /* This is a new version. Add it to tree we are building. */
2190 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2193 rinfo
->failed
= TRUE
;
2197 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2198 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2199 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2203 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2206 rinfo
->failed
= TRUE
;
2210 /* Note that we are copying a string pointer here, and testing it
2211 above. If bfd_elf_string_from_elf_section is ever changed to
2212 discard the string data when low in memory, this will have to be
2214 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2216 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2217 a
->vna_nextptr
= t
->vn_auxptr
;
2219 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2222 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2229 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2230 hidden. Set *T_P to NULL if there is no match. */
2233 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info
*info
,
2234 struct elf_link_hash_entry
*h
,
2235 const char *version_p
,
2236 struct bfd_elf_version_tree
**t_p
,
2239 struct bfd_elf_version_tree
*t
;
2241 /* Look for the version. If we find it, it is no longer weak. */
2242 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
2244 if (strcmp (t
->name
, version_p
) == 0)
2248 struct bfd_elf_version_expr
*d
;
2250 len
= version_p
- h
->root
.root
.string
;
2251 alc
= (char *) bfd_malloc (len
);
2254 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2255 alc
[len
- 1] = '\0';
2256 if (alc
[len
- 2] == ELF_VER_CHR
)
2257 alc
[len
- 2] = '\0';
2259 h
->verinfo
.vertree
= t
;
2263 if (t
->globals
.list
!= NULL
)
2264 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2266 /* See if there is anything to force this symbol to
2268 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2270 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2273 && ! info
->export_dynamic
)
2287 /* Return TRUE if the symbol H is hidden by version script. */
2290 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info
*info
,
2291 struct elf_link_hash_entry
*h
)
2294 bfd_boolean hide
= FALSE
;
2295 const struct elf_backend_data
*bed
2296 = get_elf_backend_data (info
->output_bfd
);
2298 /* Version script only hides symbols defined in regular objects. */
2299 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2302 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2303 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2305 struct bfd_elf_version_tree
*t
;
2308 if (*p
== ELF_VER_CHR
)
2312 && _bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
)
2316 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2321 /* If we don't have a version for this symbol, see if we can find
2323 if (h
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
2326 = bfd_find_version_for_sym (info
->version_info
,
2327 h
->root
.root
.string
, &hide
);
2328 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2330 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2338 /* Figure out appropriate versions for all the symbols. We may not
2339 have the version number script until we have read all of the input
2340 files, so until that point we don't know which symbols should be
2341 local. This function is called via elf_link_hash_traverse. */
2344 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2346 struct elf_info_failed
*sinfo
;
2347 struct bfd_link_info
*info
;
2348 const struct elf_backend_data
*bed
;
2349 struct elf_info_failed eif
;
2353 sinfo
= (struct elf_info_failed
*) data
;
2356 /* Fix the symbol flags. */
2359 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2362 sinfo
->failed
= TRUE
;
2366 bed
= get_elf_backend_data (info
->output_bfd
);
2368 /* We only need version numbers for symbols defined in regular
2370 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2372 /* Hide symbols defined in discarded input sections. */
2373 if ((h
->root
.type
== bfd_link_hash_defined
2374 || h
->root
.type
== bfd_link_hash_defweak
)
2375 && discarded_section (h
->root
.u
.def
.section
))
2376 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2381 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2382 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2384 struct bfd_elf_version_tree
*t
;
2387 if (*p
== ELF_VER_CHR
)
2390 /* If there is no version string, we can just return out. */
2394 if (!_bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
))
2396 sinfo
->failed
= TRUE
;
2401 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2403 /* If we are building an application, we need to create a
2404 version node for this version. */
2405 if (t
== NULL
&& bfd_link_executable (info
))
2407 struct bfd_elf_version_tree
**pp
;
2410 /* If we aren't going to export this symbol, we don't need
2411 to worry about it. */
2412 if (h
->dynindx
== -1)
2415 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2419 sinfo
->failed
= TRUE
;
2424 t
->name_indx
= (unsigned int) -1;
2428 /* Don't count anonymous version tag. */
2429 if (sinfo
->info
->version_info
!= NULL
2430 && sinfo
->info
->version_info
->vernum
== 0)
2432 for (pp
= &sinfo
->info
->version_info
;
2436 t
->vernum
= version_index
;
2440 h
->verinfo
.vertree
= t
;
2444 /* We could not find the version for a symbol when
2445 generating a shared archive. Return an error. */
2447 /* xgettext:c-format */
2448 (_("%pB: version node not found for symbol %s"),
2449 info
->output_bfd
, h
->root
.root
.string
);
2450 bfd_set_error (bfd_error_bad_value
);
2451 sinfo
->failed
= TRUE
;
2456 /* If we don't have a version for this symbol, see if we can find
2459 && h
->verinfo
.vertree
== NULL
2460 && sinfo
->info
->version_info
!= NULL
)
2463 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2464 h
->root
.root
.string
, &hide
);
2465 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2466 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2472 /* Read and swap the relocs from the section indicated by SHDR. This
2473 may be either a REL or a RELA section. The relocations are
2474 translated into RELA relocations and stored in INTERNAL_RELOCS,
2475 which should have already been allocated to contain enough space.
2476 The EXTERNAL_RELOCS are a buffer where the external form of the
2477 relocations should be stored.
2479 Returns FALSE if something goes wrong. */
2482 elf_link_read_relocs_from_section (bfd
*abfd
,
2484 Elf_Internal_Shdr
*shdr
,
2485 void *external_relocs
,
2486 Elf_Internal_Rela
*internal_relocs
)
2488 const struct elf_backend_data
*bed
;
2489 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2490 const bfd_byte
*erela
;
2491 const bfd_byte
*erelaend
;
2492 Elf_Internal_Rela
*irela
;
2493 Elf_Internal_Shdr
*symtab_hdr
;
2496 /* Position ourselves at the start of the section. */
2497 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2500 /* Read the relocations. */
2501 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2504 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2505 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2507 bed
= get_elf_backend_data (abfd
);
2509 /* Convert the external relocations to the internal format. */
2510 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2511 swap_in
= bed
->s
->swap_reloc_in
;
2512 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2513 swap_in
= bed
->s
->swap_reloca_in
;
2516 bfd_set_error (bfd_error_wrong_format
);
2520 erela
= (const bfd_byte
*) external_relocs
;
2521 /* Setting erelaend like this and comparing with <= handles case of
2522 a fuzzed object with sh_size not a multiple of sh_entsize. */
2523 erelaend
= erela
+ shdr
->sh_size
- shdr
->sh_entsize
;
2524 irela
= internal_relocs
;
2525 while (erela
<= erelaend
)
2529 (*swap_in
) (abfd
, erela
, irela
);
2530 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2531 if (bed
->s
->arch_size
== 64)
2535 if ((size_t) r_symndx
>= nsyms
)
2538 /* xgettext:c-format */
2539 (_("%pB: bad reloc symbol index (%#" PRIx64
" >= %#lx)"
2540 " for offset %#" PRIx64
" in section `%pA'"),
2541 abfd
, (uint64_t) r_symndx
, (unsigned long) nsyms
,
2542 (uint64_t) irela
->r_offset
, sec
);
2543 bfd_set_error (bfd_error_bad_value
);
2547 else if (r_symndx
!= STN_UNDEF
)
2550 /* xgettext:c-format */
2551 (_("%pB: non-zero symbol index (%#" PRIx64
")"
2552 " for offset %#" PRIx64
" in section `%pA'"
2553 " when the object file has no symbol table"),
2554 abfd
, (uint64_t) r_symndx
,
2555 (uint64_t) irela
->r_offset
, sec
);
2556 bfd_set_error (bfd_error_bad_value
);
2559 irela
+= bed
->s
->int_rels_per_ext_rel
;
2560 erela
+= shdr
->sh_entsize
;
2566 /* Read and swap the relocs for a section O. They may have been
2567 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2568 not NULL, they are used as buffers to read into. They are known to
2569 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2570 the return value is allocated using either malloc or bfd_alloc,
2571 according to the KEEP_MEMORY argument. If O has two relocation
2572 sections (both REL and RELA relocations), then the REL_HDR
2573 relocations will appear first in INTERNAL_RELOCS, followed by the
2574 RELA_HDR relocations. */
2577 _bfd_elf_link_read_relocs (bfd
*abfd
,
2579 void *external_relocs
,
2580 Elf_Internal_Rela
*internal_relocs
,
2581 bfd_boolean keep_memory
)
2583 void *alloc1
= NULL
;
2584 Elf_Internal_Rela
*alloc2
= NULL
;
2585 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2586 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2587 Elf_Internal_Rela
*internal_rela_relocs
;
2589 if (esdo
->relocs
!= NULL
)
2590 return esdo
->relocs
;
2592 if (o
->reloc_count
== 0)
2595 if (internal_relocs
== NULL
)
2599 size
= (bfd_size_type
) o
->reloc_count
* sizeof (Elf_Internal_Rela
);
2601 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2603 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2604 if (internal_relocs
== NULL
)
2608 if (external_relocs
== NULL
)
2610 bfd_size_type size
= 0;
2613 size
+= esdo
->rel
.hdr
->sh_size
;
2615 size
+= esdo
->rela
.hdr
->sh_size
;
2617 alloc1
= bfd_malloc (size
);
2620 external_relocs
= alloc1
;
2623 internal_rela_relocs
= internal_relocs
;
2626 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2630 external_relocs
= (((bfd_byte
*) external_relocs
)
2631 + esdo
->rel
.hdr
->sh_size
);
2632 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2633 * bed
->s
->int_rels_per_ext_rel
);
2637 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2639 internal_rela_relocs
)))
2642 /* Cache the results for next time, if we can. */
2644 esdo
->relocs
= internal_relocs
;
2648 /* Don't free alloc2, since if it was allocated we are passing it
2649 back (under the name of internal_relocs). */
2651 return internal_relocs
;
2658 bfd_release (abfd
, alloc2
);
2665 /* Compute the size of, and allocate space for, REL_HDR which is the
2666 section header for a section containing relocations for O. */
2669 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2670 struct bfd_elf_section_reloc_data
*reldata
)
2672 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2674 /* That allows us to calculate the size of the section. */
2675 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2677 /* The contents field must last into write_object_contents, so we
2678 allocate it with bfd_alloc rather than malloc. Also since we
2679 cannot be sure that the contents will actually be filled in,
2680 we zero the allocated space. */
2681 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2682 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2685 if (reldata
->hashes
== NULL
&& reldata
->count
)
2687 struct elf_link_hash_entry
**p
;
2689 p
= ((struct elf_link_hash_entry
**)
2690 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2694 reldata
->hashes
= p
;
2700 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2701 originated from the section given by INPUT_REL_HDR) to the
2705 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2706 asection
*input_section
,
2707 Elf_Internal_Shdr
*input_rel_hdr
,
2708 Elf_Internal_Rela
*internal_relocs
,
2709 struct elf_link_hash_entry
**rel_hash
2712 Elf_Internal_Rela
*irela
;
2713 Elf_Internal_Rela
*irelaend
;
2715 struct bfd_elf_section_reloc_data
*output_reldata
;
2716 asection
*output_section
;
2717 const struct elf_backend_data
*bed
;
2718 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2719 struct bfd_elf_section_data
*esdo
;
2721 output_section
= input_section
->output_section
;
2723 bed
= get_elf_backend_data (output_bfd
);
2724 esdo
= elf_section_data (output_section
);
2725 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2727 output_reldata
= &esdo
->rel
;
2728 swap_out
= bed
->s
->swap_reloc_out
;
2730 else if (esdo
->rela
.hdr
2731 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2733 output_reldata
= &esdo
->rela
;
2734 swap_out
= bed
->s
->swap_reloca_out
;
2739 /* xgettext:c-format */
2740 (_("%pB: relocation size mismatch in %pB section %pA"),
2741 output_bfd
, input_section
->owner
, input_section
);
2742 bfd_set_error (bfd_error_wrong_format
);
2746 erel
= output_reldata
->hdr
->contents
;
2747 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2748 irela
= internal_relocs
;
2749 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2750 * bed
->s
->int_rels_per_ext_rel
);
2751 while (irela
< irelaend
)
2753 (*swap_out
) (output_bfd
, irela
, erel
);
2754 irela
+= bed
->s
->int_rels_per_ext_rel
;
2755 erel
+= input_rel_hdr
->sh_entsize
;
2758 /* Bump the counter, so that we know where to add the next set of
2760 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2765 /* Make weak undefined symbols in PIE dynamic. */
2768 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2769 struct elf_link_hash_entry
*h
)
2771 if (bfd_link_pie (info
)
2773 && h
->root
.type
== bfd_link_hash_undefweak
)
2774 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2779 /* Fix up the flags for a symbol. This handles various cases which
2780 can only be fixed after all the input files are seen. This is
2781 currently called by both adjust_dynamic_symbol and
2782 assign_sym_version, which is unnecessary but perhaps more robust in
2783 the face of future changes. */
2786 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2787 struct elf_info_failed
*eif
)
2789 const struct elf_backend_data
*bed
;
2791 /* If this symbol was mentioned in a non-ELF file, try to set
2792 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2793 permit a non-ELF file to correctly refer to a symbol defined in
2794 an ELF dynamic object. */
2797 while (h
->root
.type
== bfd_link_hash_indirect
)
2798 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2800 if (h
->root
.type
!= bfd_link_hash_defined
2801 && h
->root
.type
!= bfd_link_hash_defweak
)
2804 h
->ref_regular_nonweak
= 1;
2808 if (h
->root
.u
.def
.section
->owner
!= NULL
2809 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2810 == bfd_target_elf_flavour
))
2813 h
->ref_regular_nonweak
= 1;
2819 if (h
->dynindx
== -1
2823 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2832 /* Unfortunately, NON_ELF is only correct if the symbol
2833 was first seen in a non-ELF file. Fortunately, if the symbol
2834 was first seen in an ELF file, we're probably OK unless the
2835 symbol was defined in a non-ELF file. Catch that case here.
2836 FIXME: We're still in trouble if the symbol was first seen in
2837 a dynamic object, and then later in a non-ELF regular object. */
2838 if ((h
->root
.type
== bfd_link_hash_defined
2839 || h
->root
.type
== bfd_link_hash_defweak
)
2841 && (h
->root
.u
.def
.section
->owner
!= NULL
2842 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2843 != bfd_target_elf_flavour
)
2844 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2845 && !h
->def_dynamic
)))
2849 /* Backend specific symbol fixup. */
2850 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2851 if (bed
->elf_backend_fixup_symbol
2852 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2855 /* If this is a final link, and the symbol was defined as a common
2856 symbol in a regular object file, and there was no definition in
2857 any dynamic object, then the linker will have allocated space for
2858 the symbol in a common section but the DEF_REGULAR
2859 flag will not have been set. */
2860 if (h
->root
.type
== bfd_link_hash_defined
2864 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
2867 /* Symbols defined in discarded sections shouldn't be dynamic. */
2868 if (h
->root
.type
== bfd_link_hash_undefined
&& h
->indx
== -3)
2869 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2871 /* If a weak undefined symbol has non-default visibility, we also
2872 hide it from the dynamic linker. */
2873 else if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2874 && h
->root
.type
== bfd_link_hash_undefweak
)
2875 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2877 /* A hidden versioned symbol in executable should be forced local if
2878 it is is locally defined, not referenced by shared library and not
2880 else if (bfd_link_executable (eif
->info
)
2881 && h
->versioned
== versioned_hidden
2882 && !eif
->info
->export_dynamic
2886 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2888 /* If -Bsymbolic was used (which means to bind references to global
2889 symbols to the definition within the shared object), and this
2890 symbol was defined in a regular object, then it actually doesn't
2891 need a PLT entry. Likewise, if the symbol has non-default
2892 visibility. If the symbol has hidden or internal visibility, we
2893 will force it local. */
2894 else if (h
->needs_plt
2895 && bfd_link_pic (eif
->info
)
2896 && is_elf_hash_table (eif
->info
->hash
)
2897 && (SYMBOLIC_BIND (eif
->info
, h
)
2898 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2901 bfd_boolean force_local
;
2903 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2904 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2905 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2908 /* If this is a weak defined symbol in a dynamic object, and we know
2909 the real definition in the dynamic object, copy interesting flags
2910 over to the real definition. */
2911 if (h
->is_weakalias
)
2913 struct elf_link_hash_entry
*def
= weakdef (h
);
2915 /* If the real definition is defined by a regular object file,
2916 don't do anything special. See the longer description in
2917 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2918 bfd_link_hash_defined as it was when put on the alias list
2919 then it must have originally been a versioned symbol (for
2920 which a non-versioned indirect symbol is created) and later
2921 a definition for the non-versioned symbol is found. In that
2922 case the indirection is flipped with the versioned symbol
2923 becoming an indirect pointing at the non-versioned symbol.
2924 Thus, not an alias any more. */
2925 if (def
->def_regular
2926 || def
->root
.type
!= bfd_link_hash_defined
)
2929 while ((h
= h
->u
.alias
) != def
)
2930 h
->is_weakalias
= 0;
2934 while (h
->root
.type
== bfd_link_hash_indirect
)
2935 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2936 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2937 || h
->root
.type
== bfd_link_hash_defweak
);
2938 BFD_ASSERT (def
->def_dynamic
);
2939 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, def
, h
);
2946 /* Make the backend pick a good value for a dynamic symbol. This is
2947 called via elf_link_hash_traverse, and also calls itself
2951 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2953 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2954 struct elf_link_hash_table
*htab
;
2955 const struct elf_backend_data
*bed
;
2957 if (! is_elf_hash_table (eif
->info
->hash
))
2960 /* Ignore indirect symbols. These are added by the versioning code. */
2961 if (h
->root
.type
== bfd_link_hash_indirect
)
2964 /* Fix the symbol flags. */
2965 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2968 htab
= elf_hash_table (eif
->info
);
2969 bed
= get_elf_backend_data (htab
->dynobj
);
2971 if (h
->root
.type
== bfd_link_hash_undefweak
)
2973 if (eif
->info
->dynamic_undefined_weak
== 0)
2974 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2975 else if (eif
->info
->dynamic_undefined_weak
> 0
2977 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2978 && !bfd_hide_sym_by_version (eif
->info
->version_info
,
2979 h
->root
.root
.string
))
2981 if (!bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2989 /* If this symbol does not require a PLT entry, and it is not
2990 defined by a dynamic object, or is not referenced by a regular
2991 object, ignore it. We do have to handle a weak defined symbol,
2992 even if no regular object refers to it, if we decided to add it
2993 to the dynamic symbol table. FIXME: Do we normally need to worry
2994 about symbols which are defined by one dynamic object and
2995 referenced by another one? */
2997 && h
->type
!= STT_GNU_IFUNC
3001 && (!h
->is_weakalias
|| weakdef (h
)->dynindx
== -1))))
3003 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
3007 /* If we've already adjusted this symbol, don't do it again. This
3008 can happen via a recursive call. */
3009 if (h
->dynamic_adjusted
)
3012 /* Don't look at this symbol again. Note that we must set this
3013 after checking the above conditions, because we may look at a
3014 symbol once, decide not to do anything, and then get called
3015 recursively later after REF_REGULAR is set below. */
3016 h
->dynamic_adjusted
= 1;
3018 /* If this is a weak definition, and we know a real definition, and
3019 the real symbol is not itself defined by a regular object file,
3020 then get a good value for the real definition. We handle the
3021 real symbol first, for the convenience of the backend routine.
3023 Note that there is a confusing case here. If the real definition
3024 is defined by a regular object file, we don't get the real symbol
3025 from the dynamic object, but we do get the weak symbol. If the
3026 processor backend uses a COPY reloc, then if some routine in the
3027 dynamic object changes the real symbol, we will not see that
3028 change in the corresponding weak symbol. This is the way other
3029 ELF linkers work as well, and seems to be a result of the shared
3032 I will clarify this issue. Most SVR4 shared libraries define the
3033 variable _timezone and define timezone as a weak synonym. The
3034 tzset call changes _timezone. If you write
3035 extern int timezone;
3037 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3038 you might expect that, since timezone is a synonym for _timezone,
3039 the same number will print both times. However, if the processor
3040 backend uses a COPY reloc, then actually timezone will be copied
3041 into your process image, and, since you define _timezone
3042 yourself, _timezone will not. Thus timezone and _timezone will
3043 wind up at different memory locations. The tzset call will set
3044 _timezone, leaving timezone unchanged. */
3046 if (h
->is_weakalias
)
3048 struct elf_link_hash_entry
*def
= weakdef (h
);
3050 /* If we get to this point, there is an implicit reference to
3051 the alias by a regular object file via the weak symbol H. */
3052 def
->ref_regular
= 1;
3054 /* Ensure that the backend adjust_dynamic_symbol function sees
3055 the strong alias before H by recursively calling ourselves. */
3056 if (!_bfd_elf_adjust_dynamic_symbol (def
, eif
))
3060 /* If a symbol has no type and no size and does not require a PLT
3061 entry, then we are probably about to do the wrong thing here: we
3062 are probably going to create a COPY reloc for an empty object.
3063 This case can arise when a shared object is built with assembly
3064 code, and the assembly code fails to set the symbol type. */
3066 && h
->type
== STT_NOTYPE
3069 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3070 h
->root
.root
.string
);
3072 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3081 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3085 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
3086 struct elf_link_hash_entry
*h
,
3089 unsigned int power_of_two
;
3091 asection
*sec
= h
->root
.u
.def
.section
;
3093 /* The section alignment of the definition is the maximum alignment
3094 requirement of symbols defined in the section. Since we don't
3095 know the symbol alignment requirement, we start with the
3096 maximum alignment and check low bits of the symbol address
3097 for the minimum alignment. */
3098 power_of_two
= bfd_section_alignment (sec
);
3099 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
3100 while ((h
->root
.u
.def
.value
& mask
) != 0)
3106 if (power_of_two
> bfd_section_alignment (dynbss
))
3108 /* Adjust the section alignment if needed. */
3109 if (!bfd_set_section_alignment (dynbss
, power_of_two
))
3113 /* We make sure that the symbol will be aligned properly. */
3114 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
3116 /* Define the symbol as being at this point in DYNBSS. */
3117 h
->root
.u
.def
.section
= dynbss
;
3118 h
->root
.u
.def
.value
= dynbss
->size
;
3120 /* Increment the size of DYNBSS to make room for the symbol. */
3121 dynbss
->size
+= h
->size
;
3123 /* No error if extern_protected_data is true. */
3124 if (h
->protected_def
3125 && (!info
->extern_protected_data
3126 || (info
->extern_protected_data
< 0
3127 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
3128 info
->callbacks
->einfo
3129 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3130 h
->root
.root
.string
);
3135 /* Adjust all external symbols pointing into SEC_MERGE sections
3136 to reflect the object merging within the sections. */
3139 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
3143 if ((h
->root
.type
== bfd_link_hash_defined
3144 || h
->root
.type
== bfd_link_hash_defweak
)
3145 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
3146 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
3148 bfd
*output_bfd
= (bfd
*) data
;
3150 h
->root
.u
.def
.value
=
3151 _bfd_merged_section_offset (output_bfd
,
3152 &h
->root
.u
.def
.section
,
3153 elf_section_data (sec
)->sec_info
,
3154 h
->root
.u
.def
.value
);
3160 /* Returns false if the symbol referred to by H should be considered
3161 to resolve local to the current module, and true if it should be
3162 considered to bind dynamically. */
3165 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
3166 struct bfd_link_info
*info
,
3167 bfd_boolean not_local_protected
)
3169 bfd_boolean binding_stays_local_p
;
3170 const struct elf_backend_data
*bed
;
3171 struct elf_link_hash_table
*hash_table
;
3176 while (h
->root
.type
== bfd_link_hash_indirect
3177 || h
->root
.type
== bfd_link_hash_warning
)
3178 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3180 /* If it was forced local, then clearly it's not dynamic. */
3181 if (h
->dynindx
== -1)
3183 if (h
->forced_local
)
3186 /* Identify the cases where name binding rules say that a
3187 visible symbol resolves locally. */
3188 binding_stays_local_p
= (bfd_link_executable (info
)
3189 || SYMBOLIC_BIND (info
, h
));
3191 switch (ELF_ST_VISIBILITY (h
->other
))
3198 hash_table
= elf_hash_table (info
);
3199 if (!is_elf_hash_table (hash_table
))
3202 bed
= get_elf_backend_data (hash_table
->dynobj
);
3204 /* Proper resolution for function pointer equality may require
3205 that these symbols perhaps be resolved dynamically, even though
3206 we should be resolving them to the current module. */
3207 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
3208 binding_stays_local_p
= TRUE
;
3215 /* If it isn't defined locally, then clearly it's dynamic. */
3216 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
3219 /* Otherwise, the symbol is dynamic if binding rules don't tell
3220 us that it remains local. */
3221 return !binding_stays_local_p
;
3224 /* Return true if the symbol referred to by H should be considered
3225 to resolve local to the current module, and false otherwise. Differs
3226 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3227 undefined symbols. The two functions are virtually identical except
3228 for the place where dynindx == -1 is tested. If that test is true,
3229 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3230 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3232 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3233 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3234 treatment of undefined weak symbols. For those that do not make
3235 undefined weak symbols dynamic, both functions may return false. */
3238 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3239 struct bfd_link_info
*info
,
3240 bfd_boolean local_protected
)
3242 const struct elf_backend_data
*bed
;
3243 struct elf_link_hash_table
*hash_table
;
3245 /* If it's a local sym, of course we resolve locally. */
3249 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3250 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3251 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3254 /* Forced local symbols resolve locally. */
3255 if (h
->forced_local
)
3258 /* Common symbols that become definitions don't get the DEF_REGULAR
3259 flag set, so test it first, and don't bail out. */
3260 if (ELF_COMMON_DEF_P (h
))
3262 /* If we don't have a definition in a regular file, then we can't
3263 resolve locally. The sym is either undefined or dynamic. */
3264 else if (!h
->def_regular
)
3267 /* Non-dynamic symbols resolve locally. */
3268 if (h
->dynindx
== -1)
3271 /* At this point, we know the symbol is defined and dynamic. In an
3272 executable it must resolve locally, likewise when building symbolic
3273 shared libraries. */
3274 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3277 /* Now deal with defined dynamic symbols in shared libraries. Ones
3278 with default visibility might not resolve locally. */
3279 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3282 hash_table
= elf_hash_table (info
);
3283 if (!is_elf_hash_table (hash_table
))
3286 bed
= get_elf_backend_data (hash_table
->dynobj
);
3288 /* If extern_protected_data is false, STV_PROTECTED non-function
3289 symbols are local. */
3290 if ((!info
->extern_protected_data
3291 || (info
->extern_protected_data
< 0
3292 && !bed
->extern_protected_data
))
3293 && !bed
->is_function_type (h
->type
))
3296 /* Function pointer equality tests may require that STV_PROTECTED
3297 symbols be treated as dynamic symbols. If the address of a
3298 function not defined in an executable is set to that function's
3299 plt entry in the executable, then the address of the function in
3300 a shared library must also be the plt entry in the executable. */
3301 return local_protected
;
3304 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3305 aligned. Returns the first TLS output section. */
3307 struct bfd_section
*
3308 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3310 struct bfd_section
*sec
, *tls
;
3311 unsigned int align
= 0;
3313 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3314 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3318 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3319 if (sec
->alignment_power
> align
)
3320 align
= sec
->alignment_power
;
3322 elf_hash_table (info
)->tls_sec
= tls
;
3324 /* Ensure the alignment of the first section (usually .tdata) is the largest
3325 alignment, so that the tls segment starts aligned. */
3327 tls
->alignment_power
= align
;
3332 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3334 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3335 Elf_Internal_Sym
*sym
)
3337 const struct elf_backend_data
*bed
;
3339 /* Local symbols do not count, but target specific ones might. */
3340 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3341 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3344 bed
= get_elf_backend_data (abfd
);
3345 /* Function symbols do not count. */
3346 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3349 /* If the section is undefined, then so is the symbol. */
3350 if (sym
->st_shndx
== SHN_UNDEF
)
3353 /* If the symbol is defined in the common section, then
3354 it is a common definition and so does not count. */
3355 if (bed
->common_definition (sym
))
3358 /* If the symbol is in a target specific section then we
3359 must rely upon the backend to tell us what it is. */
3360 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3361 /* FIXME - this function is not coded yet:
3363 return _bfd_is_global_symbol_definition (abfd, sym);
3365 Instead for now assume that the definition is not global,
3366 Even if this is wrong, at least the linker will behave
3367 in the same way that it used to do. */
3373 /* Search the symbol table of the archive element of the archive ABFD
3374 whose archive map contains a mention of SYMDEF, and determine if
3375 the symbol is defined in this element. */
3377 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3379 Elf_Internal_Shdr
* hdr
;
3383 Elf_Internal_Sym
*isymbuf
;
3384 Elf_Internal_Sym
*isym
;
3385 Elf_Internal_Sym
*isymend
;
3388 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
3392 if (! bfd_check_format (abfd
, bfd_object
))
3395 /* Select the appropriate symbol table. If we don't know if the
3396 object file is an IR object, give linker LTO plugin a chance to
3397 get the correct symbol table. */
3398 if (abfd
->plugin_format
== bfd_plugin_yes
3399 #if BFD_SUPPORTS_PLUGINS
3400 || (abfd
->plugin_format
== bfd_plugin_unknown
3401 && bfd_link_plugin_object_p (abfd
))
3405 /* Use the IR symbol table if the object has been claimed by
3407 abfd
= abfd
->plugin_dummy_bfd
;
3408 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3410 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3411 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3413 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3415 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3417 /* The sh_info field of the symtab header tells us where the
3418 external symbols start. We don't care about the local symbols. */
3419 if (elf_bad_symtab (abfd
))
3421 extsymcount
= symcount
;
3426 extsymcount
= symcount
- hdr
->sh_info
;
3427 extsymoff
= hdr
->sh_info
;
3430 if (extsymcount
== 0)
3433 /* Read in the symbol table. */
3434 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3436 if (isymbuf
== NULL
)
3439 /* Scan the symbol table looking for SYMDEF. */
3441 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3445 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3450 if (strcmp (name
, symdef
->name
) == 0)
3452 result
= is_global_data_symbol_definition (abfd
, isym
);
3462 /* Add an entry to the .dynamic table. */
3465 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3469 struct elf_link_hash_table
*hash_table
;
3470 const struct elf_backend_data
*bed
;
3472 bfd_size_type newsize
;
3473 bfd_byte
*newcontents
;
3474 Elf_Internal_Dyn dyn
;
3476 hash_table
= elf_hash_table (info
);
3477 if (! is_elf_hash_table (hash_table
))
3480 if (tag
== DT_RELA
|| tag
== DT_REL
)
3481 hash_table
->dynamic_relocs
= TRUE
;
3483 bed
= get_elf_backend_data (hash_table
->dynobj
);
3484 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3485 BFD_ASSERT (s
!= NULL
);
3487 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3488 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3489 if (newcontents
== NULL
)
3493 dyn
.d_un
.d_val
= val
;
3494 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3497 s
->contents
= newcontents
;
3502 /* Strip zero-sized dynamic sections. */
3505 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info
*info
)
3507 struct elf_link_hash_table
*hash_table
;
3508 const struct elf_backend_data
*bed
;
3509 asection
*s
, *sdynamic
, **pp
;
3510 asection
*rela_dyn
, *rel_dyn
;
3511 Elf_Internal_Dyn dyn
;
3512 bfd_byte
*extdyn
, *next
;
3513 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
3514 bfd_boolean strip_zero_sized
;
3515 bfd_boolean strip_zero_sized_plt
;
3517 if (bfd_link_relocatable (info
))
3520 hash_table
= elf_hash_table (info
);
3521 if (!is_elf_hash_table (hash_table
))
3524 if (!hash_table
->dynobj
)
3527 sdynamic
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3531 bed
= get_elf_backend_data (hash_table
->dynobj
);
3532 swap_dyn_in
= bed
->s
->swap_dyn_in
;
3534 strip_zero_sized
= FALSE
;
3535 strip_zero_sized_plt
= FALSE
;
3537 /* Strip zero-sized dynamic sections. */
3538 rela_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rela.dyn");
3539 rel_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rel.dyn");
3540 for (pp
= &info
->output_bfd
->sections
; (s
= *pp
) != NULL
;)
3544 || s
== hash_table
->srelplt
->output_section
3545 || s
== hash_table
->splt
->output_section
))
3548 info
->output_bfd
->section_count
--;
3549 strip_zero_sized
= TRUE
;
3554 else if (s
== hash_table
->splt
->output_section
)
3556 s
= hash_table
->splt
;
3557 strip_zero_sized_plt
= TRUE
;
3560 s
= hash_table
->srelplt
;
3561 s
->flags
|= SEC_EXCLUDE
;
3562 s
->output_section
= bfd_abs_section_ptr
;
3567 if (strip_zero_sized_plt
)
3568 for (extdyn
= sdynamic
->contents
;
3569 extdyn
< sdynamic
->contents
+ sdynamic
->size
;
3572 next
= extdyn
+ bed
->s
->sizeof_dyn
;
3573 swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3581 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3582 the procedure linkage table (the .plt section) has been
3584 memmove (extdyn
, next
,
3585 sdynamic
->size
- (next
- sdynamic
->contents
));
3590 if (strip_zero_sized
)
3592 /* Regenerate program headers. */
3593 elf_seg_map (info
->output_bfd
) = NULL
;
3594 return _bfd_elf_map_sections_to_segments (info
->output_bfd
, info
);
3600 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3601 1 if a DT_NEEDED tag already exists, and 0 on success. */
3604 bfd_elf_add_dt_needed_tag (bfd
*abfd
, struct bfd_link_info
*info
)
3606 struct elf_link_hash_table
*hash_table
;
3610 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3613 hash_table
= elf_hash_table (info
);
3614 soname
= elf_dt_name (abfd
);
3615 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3616 if (strindex
== (size_t) -1)
3619 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3622 const struct elf_backend_data
*bed
;
3625 bed
= get_elf_backend_data (hash_table
->dynobj
);
3626 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3628 for (extdyn
= sdyn
->contents
;
3629 extdyn
< sdyn
->contents
+ sdyn
->size
;
3630 extdyn
+= bed
->s
->sizeof_dyn
)
3632 Elf_Internal_Dyn dyn
;
3634 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3635 if (dyn
.d_tag
== DT_NEEDED
3636 && dyn
.d_un
.d_val
== strindex
)
3638 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3644 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3647 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3653 /* Return true if SONAME is on the needed list between NEEDED and STOP
3654 (or the end of list if STOP is NULL), and needed by a library that
3658 on_needed_list (const char *soname
,
3659 struct bfd_link_needed_list
*needed
,
3660 struct bfd_link_needed_list
*stop
)
3662 struct bfd_link_needed_list
*look
;
3663 for (look
= needed
; look
!= stop
; look
= look
->next
)
3664 if (strcmp (soname
, look
->name
) == 0
3665 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3666 /* If needed by a library that itself is not directly
3667 needed, recursively check whether that library is
3668 indirectly needed. Since we add DT_NEEDED entries to
3669 the end of the list, library dependencies appear after
3670 the library. Therefore search prior to the current
3671 LOOK, preventing possible infinite recursion. */
3672 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3678 /* Sort symbol by value, section, size, and type. */
3680 elf_sort_symbol (const void *arg1
, const void *arg2
)
3682 const struct elf_link_hash_entry
*h1
;
3683 const struct elf_link_hash_entry
*h2
;
3684 bfd_signed_vma vdiff
;
3689 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3690 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3691 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3693 return vdiff
> 0 ? 1 : -1;
3695 sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3699 /* Sort so that sized symbols are selected over zero size symbols. */
3700 vdiff
= h1
->size
- h2
->size
;
3702 return vdiff
> 0 ? 1 : -1;
3704 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3705 if (h1
->type
!= h2
->type
)
3706 return h1
->type
- h2
->type
;
3708 /* If symbols are properly sized and typed, and multiple strong
3709 aliases are not defined in a shared library by the user we
3710 shouldn't get here. Unfortunately linker script symbols like
3711 __bss_start sometimes match a user symbol defined at the start of
3712 .bss without proper size and type. We'd like to preference the
3713 user symbol over reserved system symbols. Sort on leading
3715 n1
= h1
->root
.root
.string
;
3716 n2
= h2
->root
.root
.string
;
3729 /* Final sort on name selects user symbols like '_u' over reserved
3730 system symbols like '_Z' and also will avoid qsort instability. */
3734 /* This function is used to adjust offsets into .dynstr for
3735 dynamic symbols. This is called via elf_link_hash_traverse. */
3738 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3740 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3742 if (h
->dynindx
!= -1)
3743 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3747 /* Assign string offsets in .dynstr, update all structures referencing
3751 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3753 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3754 struct elf_link_local_dynamic_entry
*entry
;
3755 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3756 bfd
*dynobj
= hash_table
->dynobj
;
3759 const struct elf_backend_data
*bed
;
3762 _bfd_elf_strtab_finalize (dynstr
);
3763 size
= _bfd_elf_strtab_size (dynstr
);
3765 bed
= get_elf_backend_data (dynobj
);
3766 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3767 BFD_ASSERT (sdyn
!= NULL
);
3769 /* Update all .dynamic entries referencing .dynstr strings. */
3770 for (extdyn
= sdyn
->contents
;
3771 extdyn
< sdyn
->contents
+ sdyn
->size
;
3772 extdyn
+= bed
->s
->sizeof_dyn
)
3774 Elf_Internal_Dyn dyn
;
3776 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3780 dyn
.d_un
.d_val
= size
;
3790 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3795 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3798 /* Now update local dynamic symbols. */
3799 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3800 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3801 entry
->isym
.st_name
);
3803 /* And the rest of dynamic symbols. */
3804 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3806 /* Adjust version definitions. */
3807 if (elf_tdata (output_bfd
)->cverdefs
)
3812 Elf_Internal_Verdef def
;
3813 Elf_Internal_Verdaux defaux
;
3815 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3819 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3821 p
+= sizeof (Elf_External_Verdef
);
3822 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3824 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3826 _bfd_elf_swap_verdaux_in (output_bfd
,
3827 (Elf_External_Verdaux
*) p
, &defaux
);
3828 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3830 _bfd_elf_swap_verdaux_out (output_bfd
,
3831 &defaux
, (Elf_External_Verdaux
*) p
);
3832 p
+= sizeof (Elf_External_Verdaux
);
3835 while (def
.vd_next
);
3838 /* Adjust version references. */
3839 if (elf_tdata (output_bfd
)->verref
)
3844 Elf_Internal_Verneed need
;
3845 Elf_Internal_Vernaux needaux
;
3847 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
3851 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3853 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3854 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3855 (Elf_External_Verneed
*) p
);
3856 p
+= sizeof (Elf_External_Verneed
);
3857 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3859 _bfd_elf_swap_vernaux_in (output_bfd
,
3860 (Elf_External_Vernaux
*) p
, &needaux
);
3861 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3863 _bfd_elf_swap_vernaux_out (output_bfd
,
3865 (Elf_External_Vernaux
*) p
);
3866 p
+= sizeof (Elf_External_Vernaux
);
3869 while (need
.vn_next
);
3875 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3876 The default is to only match when the INPUT and OUTPUT are exactly
3880 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3881 const bfd_target
*output
)
3883 return input
== output
;
3886 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3887 This version is used when different targets for the same architecture
3888 are virtually identical. */
3891 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3892 const bfd_target
*output
)
3894 const struct elf_backend_data
*obed
, *ibed
;
3896 if (input
== output
)
3899 ibed
= xvec_get_elf_backend_data (input
);
3900 obed
= xvec_get_elf_backend_data (output
);
3902 if (ibed
->arch
!= obed
->arch
)
3905 /* If both backends are using this function, deem them compatible. */
3906 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3909 /* Make a special call to the linker "notice" function to tell it that
3910 we are about to handle an as-needed lib, or have finished
3911 processing the lib. */
3914 _bfd_elf_notice_as_needed (bfd
*ibfd
,
3915 struct bfd_link_info
*info
,
3916 enum notice_asneeded_action act
)
3918 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
3921 /* Check relocations an ELF object file. */
3924 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
3926 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3927 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
3929 /* If this object is the same format as the output object, and it is
3930 not a shared library, then let the backend look through the
3933 This is required to build global offset table entries and to
3934 arrange for dynamic relocs. It is not required for the
3935 particular common case of linking non PIC code, even when linking
3936 against shared libraries, but unfortunately there is no way of
3937 knowing whether an object file has been compiled PIC or not.
3938 Looking through the relocs is not particularly time consuming.
3939 The problem is that we must either (1) keep the relocs in memory,
3940 which causes the linker to require additional runtime memory or
3941 (2) read the relocs twice from the input file, which wastes time.
3942 This would be a good case for using mmap.
3944 I have no idea how to handle linking PIC code into a file of a
3945 different format. It probably can't be done. */
3946 if ((abfd
->flags
& DYNAMIC
) == 0
3947 && is_elf_hash_table (htab
)
3948 && bed
->check_relocs
!= NULL
3949 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
3950 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
3954 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3956 Elf_Internal_Rela
*internal_relocs
;
3959 /* Don't check relocations in excluded sections. Don't do
3960 anything special with non-loaded, non-alloced sections.
3961 In particular, any relocs in such sections should not
3962 affect GOT and PLT reference counting (ie. we don't
3963 allow them to create GOT or PLT entries), there's no
3964 possibility or desire to optimize TLS relocs, and
3965 there's not much point in propagating relocs to shared
3966 libs that the dynamic linker won't relocate. */
3967 if ((o
->flags
& SEC_ALLOC
) == 0
3968 || (o
->flags
& SEC_RELOC
) == 0
3969 || (o
->flags
& SEC_EXCLUDE
) != 0
3970 || o
->reloc_count
== 0
3971 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
3972 && (o
->flags
& SEC_DEBUGGING
) != 0)
3973 || bfd_is_abs_section (o
->output_section
))
3976 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
3978 if (internal_relocs
== NULL
)
3981 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
3983 if (elf_section_data (o
)->relocs
!= internal_relocs
)
3984 free (internal_relocs
);
3994 /* Add symbols from an ELF object file to the linker hash table. */
3997 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3999 Elf_Internal_Ehdr
*ehdr
;
4000 Elf_Internal_Shdr
*hdr
;
4004 struct elf_link_hash_entry
**sym_hash
;
4005 bfd_boolean dynamic
;
4006 Elf_External_Versym
*extversym
= NULL
;
4007 Elf_External_Versym
*extversym_end
= NULL
;
4008 Elf_External_Versym
*ever
;
4009 struct elf_link_hash_entry
*weaks
;
4010 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
4011 size_t nondeflt_vers_cnt
= 0;
4012 Elf_Internal_Sym
*isymbuf
= NULL
;
4013 Elf_Internal_Sym
*isym
;
4014 Elf_Internal_Sym
*isymend
;
4015 const struct elf_backend_data
*bed
;
4016 bfd_boolean add_needed
;
4017 struct elf_link_hash_table
*htab
;
4018 void *alloc_mark
= NULL
;
4019 struct bfd_hash_entry
**old_table
= NULL
;
4020 unsigned int old_size
= 0;
4021 unsigned int old_count
= 0;
4022 void *old_tab
= NULL
;
4024 struct bfd_link_hash_entry
*old_undefs
= NULL
;
4025 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
4026 void *old_strtab
= NULL
;
4029 bfd_boolean just_syms
;
4031 htab
= elf_hash_table (info
);
4032 bed
= get_elf_backend_data (abfd
);
4034 if ((abfd
->flags
& DYNAMIC
) == 0)
4040 /* You can't use -r against a dynamic object. Also, there's no
4041 hope of using a dynamic object which does not exactly match
4042 the format of the output file. */
4043 if (bfd_link_relocatable (info
)
4044 || !is_elf_hash_table (htab
)
4045 || info
->output_bfd
->xvec
!= abfd
->xvec
)
4047 if (bfd_link_relocatable (info
))
4048 bfd_set_error (bfd_error_invalid_operation
);
4050 bfd_set_error (bfd_error_wrong_format
);
4055 ehdr
= elf_elfheader (abfd
);
4056 if (info
->warn_alternate_em
4057 && bed
->elf_machine_code
!= ehdr
->e_machine
4058 && ((bed
->elf_machine_alt1
!= 0
4059 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
4060 || (bed
->elf_machine_alt2
!= 0
4061 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
4063 /* xgettext:c-format */
4064 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4065 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
4067 /* As a GNU extension, any input sections which are named
4068 .gnu.warning.SYMBOL are treated as warning symbols for the given
4069 symbol. This differs from .gnu.warning sections, which generate
4070 warnings when they are included in an output file. */
4071 /* PR 12761: Also generate this warning when building shared libraries. */
4072 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4076 name
= bfd_section_name (s
);
4077 if (CONST_STRNEQ (name
, ".gnu.warning."))
4082 name
+= sizeof ".gnu.warning." - 1;
4084 /* If this is a shared object, then look up the symbol
4085 in the hash table. If it is there, and it is already
4086 been defined, then we will not be using the entry
4087 from this shared object, so we don't need to warn.
4088 FIXME: If we see the definition in a regular object
4089 later on, we will warn, but we shouldn't. The only
4090 fix is to keep track of what warnings we are supposed
4091 to emit, and then handle them all at the end of the
4095 struct elf_link_hash_entry
*h
;
4097 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
4099 /* FIXME: What about bfd_link_hash_common? */
4101 && (h
->root
.type
== bfd_link_hash_defined
4102 || h
->root
.type
== bfd_link_hash_defweak
))
4107 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
4111 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
4116 if (! (_bfd_generic_link_add_one_symbol
4117 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
4118 FALSE
, bed
->collect
, NULL
)))
4121 if (bfd_link_executable (info
))
4123 /* Clobber the section size so that the warning does
4124 not get copied into the output file. */
4127 /* Also set SEC_EXCLUDE, so that symbols defined in
4128 the warning section don't get copied to the output. */
4129 s
->flags
|= SEC_EXCLUDE
;
4134 just_syms
= ((s
= abfd
->sections
) != NULL
4135 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
4140 /* If we are creating a shared library, create all the dynamic
4141 sections immediately. We need to attach them to something,
4142 so we attach them to this BFD, provided it is the right
4143 format and is not from ld --just-symbols. Always create the
4144 dynamic sections for -E/--dynamic-list. FIXME: If there
4145 are no input BFD's of the same format as the output, we can't
4146 make a shared library. */
4148 && (bfd_link_pic (info
)
4149 || (!bfd_link_relocatable (info
)
4151 && (info
->export_dynamic
|| info
->dynamic
)))
4152 && is_elf_hash_table (htab
)
4153 && info
->output_bfd
->xvec
== abfd
->xvec
4154 && !htab
->dynamic_sections_created
)
4156 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
4160 else if (!is_elf_hash_table (htab
))
4164 const char *soname
= NULL
;
4166 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
4167 const Elf_Internal_Phdr
*phdr
;
4168 struct elf_link_loaded_list
*loaded_lib
;
4170 /* ld --just-symbols and dynamic objects don't mix very well.
4171 ld shouldn't allow it. */
4175 /* If this dynamic lib was specified on the command line with
4176 --as-needed in effect, then we don't want to add a DT_NEEDED
4177 tag unless the lib is actually used. Similary for libs brought
4178 in by another lib's DT_NEEDED. When --no-add-needed is used
4179 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4180 any dynamic library in DT_NEEDED tags in the dynamic lib at
4182 add_needed
= (elf_dyn_lib_class (abfd
)
4183 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
4184 | DYN_NO_NEEDED
)) == 0;
4186 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4191 unsigned int elfsec
;
4192 unsigned long shlink
;
4194 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
4201 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
4202 if (elfsec
== SHN_BAD
)
4203 goto error_free_dyn
;
4204 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
4206 for (extdyn
= dynbuf
;
4207 extdyn
<= dynbuf
+ s
->size
- bed
->s
->sizeof_dyn
;
4208 extdyn
+= bed
->s
->sizeof_dyn
)
4210 Elf_Internal_Dyn dyn
;
4212 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
4213 if (dyn
.d_tag
== DT_SONAME
)
4215 unsigned int tagv
= dyn
.d_un
.d_val
;
4216 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4218 goto error_free_dyn
;
4220 if (dyn
.d_tag
== DT_NEEDED
)
4222 struct bfd_link_needed_list
*n
, **pn
;
4224 unsigned int tagv
= dyn
.d_un
.d_val
;
4225 size_t amt
= sizeof (struct bfd_link_needed_list
);
4227 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4228 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4229 if (n
== NULL
|| fnm
== NULL
)
4230 goto error_free_dyn
;
4231 amt
= strlen (fnm
) + 1;
4232 anm
= (char *) bfd_alloc (abfd
, amt
);
4234 goto error_free_dyn
;
4235 memcpy (anm
, fnm
, amt
);
4239 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4243 if (dyn
.d_tag
== DT_RUNPATH
)
4245 struct bfd_link_needed_list
*n
, **pn
;
4247 unsigned int tagv
= dyn
.d_un
.d_val
;
4248 size_t amt
= sizeof (struct bfd_link_needed_list
);
4250 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4251 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4252 if (n
== NULL
|| fnm
== NULL
)
4253 goto error_free_dyn
;
4254 amt
= strlen (fnm
) + 1;
4255 anm
= (char *) bfd_alloc (abfd
, amt
);
4257 goto error_free_dyn
;
4258 memcpy (anm
, fnm
, amt
);
4262 for (pn
= & runpath
;
4268 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4269 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
4271 struct bfd_link_needed_list
*n
, **pn
;
4273 unsigned int tagv
= dyn
.d_un
.d_val
;
4274 size_t amt
= sizeof (struct bfd_link_needed_list
);
4276 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4277 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4278 if (n
== NULL
|| fnm
== NULL
)
4279 goto error_free_dyn
;
4280 amt
= strlen (fnm
) + 1;
4281 anm
= (char *) bfd_alloc (abfd
, amt
);
4283 goto error_free_dyn
;
4284 memcpy (anm
, fnm
, amt
);
4294 if (dyn
.d_tag
== DT_AUDIT
)
4296 unsigned int tagv
= dyn
.d_un
.d_val
;
4297 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4304 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4305 frees all more recently bfd_alloc'd blocks as well. */
4311 struct bfd_link_needed_list
**pn
;
4312 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4317 /* If we have a PT_GNU_RELRO program header, mark as read-only
4318 all sections contained fully therein. This makes relro
4319 shared library sections appear as they will at run-time. */
4320 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4321 while (phdr
-- > elf_tdata (abfd
)->phdr
)
4322 if (phdr
->p_type
== PT_GNU_RELRO
)
4324 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4326 unsigned int opb
= bfd_octets_per_byte (abfd
, s
);
4328 if ((s
->flags
& SEC_ALLOC
) != 0
4329 && s
->vma
* opb
>= phdr
->p_vaddr
4330 && s
->vma
* opb
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4331 s
->flags
|= SEC_READONLY
;
4336 /* We do not want to include any of the sections in a dynamic
4337 object in the output file. We hack by simply clobbering the
4338 list of sections in the BFD. This could be handled more
4339 cleanly by, say, a new section flag; the existing
4340 SEC_NEVER_LOAD flag is not the one we want, because that one
4341 still implies that the section takes up space in the output
4343 bfd_section_list_clear (abfd
);
4345 /* Find the name to use in a DT_NEEDED entry that refers to this
4346 object. If the object has a DT_SONAME entry, we use it.
4347 Otherwise, if the generic linker stuck something in
4348 elf_dt_name, we use that. Otherwise, we just use the file
4350 if (soname
== NULL
|| *soname
== '\0')
4352 soname
= elf_dt_name (abfd
);
4353 if (soname
== NULL
|| *soname
== '\0')
4354 soname
= bfd_get_filename (abfd
);
4357 /* Save the SONAME because sometimes the linker emulation code
4358 will need to know it. */
4359 elf_dt_name (abfd
) = soname
;
4361 /* If we have already included this dynamic object in the
4362 link, just ignore it. There is no reason to include a
4363 particular dynamic object more than once. */
4364 for (loaded_lib
= htab
->dyn_loaded
;
4366 loaded_lib
= loaded_lib
->next
)
4368 if (strcmp (elf_dt_name (loaded_lib
->abfd
), soname
) == 0)
4372 /* Create dynamic sections for backends that require that be done
4373 before setup_gnu_properties. */
4375 && !_bfd_elf_link_create_dynamic_sections (abfd
, info
))
4378 /* Save the DT_AUDIT entry for the linker emulation code. */
4379 elf_dt_audit (abfd
) = audit
;
4382 /* If this is a dynamic object, we always link against the .dynsym
4383 symbol table, not the .symtab symbol table. The dynamic linker
4384 will only see the .dynsym symbol table, so there is no reason to
4385 look at .symtab for a dynamic object. */
4387 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4388 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4390 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4392 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4394 /* The sh_info field of the symtab header tells us where the
4395 external symbols start. We don't care about the local symbols at
4397 if (elf_bad_symtab (abfd
))
4399 extsymcount
= symcount
;
4404 extsymcount
= symcount
- hdr
->sh_info
;
4405 extsymoff
= hdr
->sh_info
;
4408 sym_hash
= elf_sym_hashes (abfd
);
4409 if (extsymcount
!= 0)
4411 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4413 if (isymbuf
== NULL
)
4416 if (sym_hash
== NULL
)
4418 /* We store a pointer to the hash table entry for each
4420 size_t amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4421 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4422 if (sym_hash
== NULL
)
4423 goto error_free_sym
;
4424 elf_sym_hashes (abfd
) = sym_hash
;
4430 /* Read in any version definitions. */
4431 if (!_bfd_elf_slurp_version_tables (abfd
,
4432 info
->default_imported_symver
))
4433 goto error_free_sym
;
4435 /* Read in the symbol versions, but don't bother to convert them
4436 to internal format. */
4437 if (elf_dynversym (abfd
) != 0)
4439 Elf_Internal_Shdr
*versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4440 bfd_size_type amt
= versymhdr
->sh_size
;
4442 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0)
4443 goto error_free_sym
;
4444 extversym
= (Elf_External_Versym
*)
4445 _bfd_malloc_and_read (abfd
, amt
, amt
);
4446 if (extversym
== NULL
)
4447 goto error_free_sym
;
4448 extversym_end
= extversym
+ amt
/ sizeof (*extversym
);
4452 /* If we are loading an as-needed shared lib, save the symbol table
4453 state before we start adding symbols. If the lib turns out
4454 to be unneeded, restore the state. */
4455 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4460 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4462 struct bfd_hash_entry
*p
;
4463 struct elf_link_hash_entry
*h
;
4465 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4467 h
= (struct elf_link_hash_entry
*) p
;
4468 entsize
+= htab
->root
.table
.entsize
;
4469 if (h
->root
.type
== bfd_link_hash_warning
)
4470 entsize
+= htab
->root
.table
.entsize
;
4474 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4475 old_tab
= bfd_malloc (tabsize
+ entsize
);
4476 if (old_tab
== NULL
)
4477 goto error_free_vers
;
4479 /* Remember the current objalloc pointer, so that all mem for
4480 symbols added can later be reclaimed. */
4481 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4482 if (alloc_mark
== NULL
)
4483 goto error_free_vers
;
4485 /* Make a special call to the linker "notice" function to
4486 tell it that we are about to handle an as-needed lib. */
4487 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4488 goto error_free_vers
;
4490 /* Clone the symbol table. Remember some pointers into the
4491 symbol table, and dynamic symbol count. */
4492 old_ent
= (char *) old_tab
+ tabsize
;
4493 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4494 old_undefs
= htab
->root
.undefs
;
4495 old_undefs_tail
= htab
->root
.undefs_tail
;
4496 old_table
= htab
->root
.table
.table
;
4497 old_size
= htab
->root
.table
.size
;
4498 old_count
= htab
->root
.table
.count
;
4500 if (htab
->dynstr
!= NULL
)
4502 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4503 if (old_strtab
== NULL
)
4504 goto error_free_vers
;
4507 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4509 struct bfd_hash_entry
*p
;
4510 struct elf_link_hash_entry
*h
;
4512 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4514 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
4515 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4516 h
= (struct elf_link_hash_entry
*) p
;
4517 if (h
->root
.type
== bfd_link_hash_warning
)
4519 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
4520 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4527 if (extversym
== NULL
)
4529 else if (extversym
+ extsymoff
< extversym_end
)
4530 ever
= extversym
+ extsymoff
;
4533 /* xgettext:c-format */
4534 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4535 abfd
, (long) extsymoff
,
4536 (long) (extversym_end
- extversym
) / sizeof (* extversym
));
4537 bfd_set_error (bfd_error_bad_value
);
4538 goto error_free_vers
;
4541 if (!bfd_link_relocatable (info
)
4542 && abfd
->lto_slim_object
)
4545 (_("%pB: plugin needed to handle lto object"), abfd
);
4548 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
4550 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4554 asection
*sec
, *new_sec
;
4557 struct elf_link_hash_entry
*h
;
4558 struct elf_link_hash_entry
*hi
;
4559 bfd_boolean definition
;
4560 bfd_boolean size_change_ok
;
4561 bfd_boolean type_change_ok
;
4562 bfd_boolean new_weak
;
4563 bfd_boolean old_weak
;
4564 bfd_boolean override
;
4566 bfd_boolean discarded
;
4567 unsigned int old_alignment
;
4568 unsigned int shindex
;
4570 bfd_boolean matched
;
4574 flags
= BSF_NO_FLAGS
;
4576 value
= isym
->st_value
;
4577 common
= bed
->common_definition (isym
);
4578 if (common
&& info
->inhibit_common_definition
)
4580 /* Treat common symbol as undefined for --no-define-common. */
4581 isym
->st_shndx
= SHN_UNDEF
;
4586 bind
= ELF_ST_BIND (isym
->st_info
);
4590 /* This should be impossible, since ELF requires that all
4591 global symbols follow all local symbols, and that sh_info
4592 point to the first global symbol. Unfortunately, Irix 5
4594 if (elf_bad_symtab (abfd
))
4597 /* If we aren't prepared to handle locals within the globals
4598 then we'll likely segfault on a NULL symbol hash if the
4599 symbol is ever referenced in relocations. */
4600 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
4601 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, hdr
->sh_name
);
4602 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4603 " (>= sh_info of %lu)"),
4604 abfd
, name
, (long) (isym
- isymbuf
+ extsymoff
),
4607 /* Dynamic object relocations are not processed by ld, so
4608 ld won't run into the problem mentioned above. */
4611 bfd_set_error (bfd_error_bad_value
);
4612 goto error_free_vers
;
4615 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4623 case STB_GNU_UNIQUE
:
4624 flags
= BSF_GNU_UNIQUE
;
4628 /* Leave it up to the processor backend. */
4632 if (isym
->st_shndx
== SHN_UNDEF
)
4633 sec
= bfd_und_section_ptr
;
4634 else if (isym
->st_shndx
== SHN_ABS
)
4635 sec
= bfd_abs_section_ptr
;
4636 else if (isym
->st_shndx
== SHN_COMMON
)
4638 sec
= bfd_com_section_ptr
;
4639 /* What ELF calls the size we call the value. What ELF
4640 calls the value we call the alignment. */
4641 value
= isym
->st_size
;
4645 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4647 sec
= bfd_abs_section_ptr
;
4648 else if (discarded_section (sec
))
4650 /* Symbols from discarded section are undefined. We keep
4652 sec
= bfd_und_section_ptr
;
4654 isym
->st_shndx
= SHN_UNDEF
;
4656 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4660 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4663 goto error_free_vers
;
4665 if (isym
->st_shndx
== SHN_COMMON
4666 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4668 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4672 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4674 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4676 goto error_free_vers
;
4680 else if (isym
->st_shndx
== SHN_COMMON
4681 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4682 && !bfd_link_relocatable (info
))
4684 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4688 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4689 | SEC_LINKER_CREATED
);
4690 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4692 goto error_free_vers
;
4696 else if (bed
->elf_add_symbol_hook
)
4698 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4700 goto error_free_vers
;
4702 /* The hook function sets the name to NULL if this symbol
4703 should be skipped for some reason. */
4708 /* Sanity check that all possibilities were handled. */
4712 /* Silently discard TLS symbols from --just-syms. There's
4713 no way to combine a static TLS block with a new TLS block
4714 for this executable. */
4715 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4716 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4719 if (bfd_is_und_section (sec
)
4720 || bfd_is_com_section (sec
))
4725 size_change_ok
= FALSE
;
4726 type_change_ok
= bed
->type_change_ok
;
4733 if (is_elf_hash_table (htab
))
4735 Elf_Internal_Versym iver
;
4736 unsigned int vernum
= 0;
4741 if (info
->default_imported_symver
)
4742 /* Use the default symbol version created earlier. */
4743 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4747 else if (ever
>= extversym_end
)
4749 /* xgettext:c-format */
4750 _bfd_error_handler (_("%pB: not enough version information"),
4752 bfd_set_error (bfd_error_bad_value
);
4753 goto error_free_vers
;
4756 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4758 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4760 /* If this is a hidden symbol, or if it is not version
4761 1, we append the version name to the symbol name.
4762 However, we do not modify a non-hidden absolute symbol
4763 if it is not a function, because it might be the version
4764 symbol itself. FIXME: What if it isn't? */
4765 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4767 && (!bfd_is_abs_section (sec
)
4768 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4771 size_t namelen
, verlen
, newlen
;
4774 if (isym
->st_shndx
!= SHN_UNDEF
)
4776 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4778 else if (vernum
> 1)
4780 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4787 /* xgettext:c-format */
4788 (_("%pB: %s: invalid version %u (max %d)"),
4790 elf_tdata (abfd
)->cverdefs
);
4791 bfd_set_error (bfd_error_bad_value
);
4792 goto error_free_vers
;
4797 /* We cannot simply test for the number of
4798 entries in the VERNEED section since the
4799 numbers for the needed versions do not start
4801 Elf_Internal_Verneed
*t
;
4804 for (t
= elf_tdata (abfd
)->verref
;
4808 Elf_Internal_Vernaux
*a
;
4810 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4812 if (a
->vna_other
== vernum
)
4814 verstr
= a
->vna_nodename
;
4824 /* xgettext:c-format */
4825 (_("%pB: %s: invalid needed version %d"),
4826 abfd
, name
, vernum
);
4827 bfd_set_error (bfd_error_bad_value
);
4828 goto error_free_vers
;
4832 namelen
= strlen (name
);
4833 verlen
= strlen (verstr
);
4834 newlen
= namelen
+ verlen
+ 2;
4835 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4836 && isym
->st_shndx
!= SHN_UNDEF
)
4839 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4840 if (newname
== NULL
)
4841 goto error_free_vers
;
4842 memcpy (newname
, name
, namelen
);
4843 p
= newname
+ namelen
;
4845 /* If this is a defined non-hidden version symbol,
4846 we add another @ to the name. This indicates the
4847 default version of the symbol. */
4848 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4849 && isym
->st_shndx
!= SHN_UNDEF
)
4851 memcpy (p
, verstr
, verlen
+ 1);
4856 /* If this symbol has default visibility and the user has
4857 requested we not re-export it, then mark it as hidden. */
4858 if (!bfd_is_und_section (sec
)
4861 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4862 isym
->st_other
= (STV_HIDDEN
4863 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4865 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4866 sym_hash
, &old_bfd
, &old_weak
,
4867 &old_alignment
, &skip
, &override
,
4868 &type_change_ok
, &size_change_ok
,
4870 goto error_free_vers
;
4875 /* Override a definition only if the new symbol matches the
4877 if (override
&& matched
)
4881 while (h
->root
.type
== bfd_link_hash_indirect
4882 || h
->root
.type
== bfd_link_hash_warning
)
4883 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4885 if (elf_tdata (abfd
)->verdef
!= NULL
4888 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4891 if (! (_bfd_generic_link_add_one_symbol
4892 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4893 (struct bfd_link_hash_entry
**) sym_hash
)))
4894 goto error_free_vers
;
4897 /* We need to make sure that indirect symbol dynamic flags are
4900 while (h
->root
.type
== bfd_link_hash_indirect
4901 || h
->root
.type
== bfd_link_hash_warning
)
4902 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4904 /* Setting the index to -3 tells elf_link_output_extsym that
4905 this symbol is defined in a discarded section. */
4911 new_weak
= (flags
& BSF_WEAK
) != 0;
4915 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4916 && is_elf_hash_table (htab
)
4917 && h
->u
.alias
== NULL
)
4919 /* Keep a list of all weak defined non function symbols from
4920 a dynamic object, using the alias field. Later in this
4921 function we will set the alias field to the correct
4922 value. We only put non-function symbols from dynamic
4923 objects on this list, because that happens to be the only
4924 time we need to know the normal symbol corresponding to a
4925 weak symbol, and the information is time consuming to
4926 figure out. If the alias field is not already NULL,
4927 then this symbol was already defined by some previous
4928 dynamic object, and we will be using that previous
4929 definition anyhow. */
4935 /* Set the alignment of a common symbol. */
4936 if ((common
|| bfd_is_com_section (sec
))
4937 && h
->root
.type
== bfd_link_hash_common
)
4942 align
= bfd_log2 (isym
->st_value
);
4945 /* The new symbol is a common symbol in a shared object.
4946 We need to get the alignment from the section. */
4947 align
= new_sec
->alignment_power
;
4949 if (align
> old_alignment
)
4950 h
->root
.u
.c
.p
->alignment_power
= align
;
4952 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4955 if (is_elf_hash_table (htab
))
4957 /* Set a flag in the hash table entry indicating the type of
4958 reference or definition we just found. A dynamic symbol
4959 is one which is referenced or defined by both a regular
4960 object and a shared object. */
4961 bfd_boolean dynsym
= FALSE
;
4963 /* Plugin symbols aren't normal. Don't set def_regular or
4964 ref_regular for them, or make them dynamic. */
4965 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4972 if (bind
!= STB_WEAK
)
4973 h
->ref_regular_nonweak
= 1;
4985 /* If the indirect symbol has been forced local, don't
4986 make the real symbol dynamic. */
4987 if ((h
== hi
|| !hi
->forced_local
)
4988 && (bfd_link_dll (info
)
4998 hi
->ref_dynamic
= 1;
5003 hi
->def_dynamic
= 1;
5006 /* If the indirect symbol has been forced local, don't
5007 make the real symbol dynamic. */
5008 if ((h
== hi
|| !hi
->forced_local
)
5012 && weakdef (h
)->dynindx
!= -1)))
5016 /* Check to see if we need to add an indirect symbol for
5017 the default name. */
5019 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
5020 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
5021 sec
, value
, &old_bfd
, &dynsym
))
5022 goto error_free_vers
;
5024 /* Check the alignment when a common symbol is involved. This
5025 can change when a common symbol is overridden by a normal
5026 definition or a common symbol is ignored due to the old
5027 normal definition. We need to make sure the maximum
5028 alignment is maintained. */
5029 if ((old_alignment
|| common
)
5030 && h
->root
.type
!= bfd_link_hash_common
)
5032 unsigned int common_align
;
5033 unsigned int normal_align
;
5034 unsigned int symbol_align
;
5038 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
5039 || h
->root
.type
== bfd_link_hash_defweak
);
5041 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
5042 if (h
->root
.u
.def
.section
->owner
!= NULL
5043 && (h
->root
.u
.def
.section
->owner
->flags
5044 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
5046 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
5047 if (normal_align
> symbol_align
)
5048 normal_align
= symbol_align
;
5051 normal_align
= symbol_align
;
5055 common_align
= old_alignment
;
5056 common_bfd
= old_bfd
;
5061 common_align
= bfd_log2 (isym
->st_value
);
5063 normal_bfd
= old_bfd
;
5066 if (normal_align
< common_align
)
5068 /* PR binutils/2735 */
5069 if (normal_bfd
== NULL
)
5071 /* xgettext:c-format */
5072 (_("warning: alignment %u of common symbol `%s' in %pB is"
5073 " greater than the alignment (%u) of its section %pA"),
5074 1 << common_align
, name
, common_bfd
,
5075 1 << normal_align
, h
->root
.u
.def
.section
);
5078 /* xgettext:c-format */
5079 (_("warning: alignment %u of symbol `%s' in %pB"
5080 " is smaller than %u in %pB"),
5081 1 << normal_align
, name
, normal_bfd
,
5082 1 << common_align
, common_bfd
);
5086 /* Remember the symbol size if it isn't undefined. */
5087 if (isym
->st_size
!= 0
5088 && isym
->st_shndx
!= SHN_UNDEF
5089 && (definition
|| h
->size
== 0))
5092 && h
->size
!= isym
->st_size
5093 && ! size_change_ok
)
5095 /* xgettext:c-format */
5096 (_("warning: size of symbol `%s' changed"
5097 " from %" PRIu64
" in %pB to %" PRIu64
" in %pB"),
5098 name
, (uint64_t) h
->size
, old_bfd
,
5099 (uint64_t) isym
->st_size
, abfd
);
5101 h
->size
= isym
->st_size
;
5104 /* If this is a common symbol, then we always want H->SIZE
5105 to be the size of the common symbol. The code just above
5106 won't fix the size if a common symbol becomes larger. We
5107 don't warn about a size change here, because that is
5108 covered by --warn-common. Allow changes between different
5110 if (h
->root
.type
== bfd_link_hash_common
)
5111 h
->size
= h
->root
.u
.c
.size
;
5113 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
5114 && ((definition
&& !new_weak
)
5115 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
5116 || h
->type
== STT_NOTYPE
))
5118 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
5120 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5122 if (type
== STT_GNU_IFUNC
5123 && (abfd
->flags
& DYNAMIC
) != 0)
5126 if (h
->type
!= type
)
5128 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
5129 /* xgettext:c-format */
5131 (_("warning: type of symbol `%s' changed"
5132 " from %d to %d in %pB"),
5133 name
, h
->type
, type
, abfd
);
5139 /* Merge st_other field. */
5140 elf_merge_st_other (abfd
, h
, isym
, sec
, definition
, dynamic
);
5142 /* We don't want to make debug symbol dynamic. */
5144 && (sec
->flags
& SEC_DEBUGGING
)
5145 && !bfd_link_relocatable (info
))
5148 /* Nor should we make plugin symbols dynamic. */
5149 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5154 h
->target_internal
= isym
->st_target_internal
;
5155 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
5158 if (definition
&& !dynamic
)
5160 char *p
= strchr (name
, ELF_VER_CHR
);
5161 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
5163 /* Queue non-default versions so that .symver x, x@FOO
5164 aliases can be checked. */
5167 size_t amt
= ((isymend
- isym
+ 1)
5168 * sizeof (struct elf_link_hash_entry
*));
5170 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5172 goto error_free_vers
;
5174 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
5178 if (dynsym
&& h
->dynindx
== -1)
5180 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5181 goto error_free_vers
;
5183 && weakdef (h
)->dynindx
== -1)
5185 if (!bfd_elf_link_record_dynamic_symbol (info
, weakdef (h
)))
5186 goto error_free_vers
;
5189 else if (h
->dynindx
!= -1)
5190 /* If the symbol already has a dynamic index, but
5191 visibility says it should not be visible, turn it into
5193 switch (ELF_ST_VISIBILITY (h
->other
))
5197 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
5202 /* Don't add DT_NEEDED for references from the dummy bfd nor
5203 for unmatched symbol. */
5208 && h
->ref_regular_nonweak
5210 || (old_bfd
->flags
& BFD_PLUGIN
) == 0))
5211 || (h
->ref_dynamic_nonweak
5212 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5213 && !on_needed_list (elf_dt_name (abfd
),
5214 htab
->needed
, NULL
))))
5216 const char *soname
= elf_dt_name (abfd
);
5218 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
5219 h
->root
.root
.string
);
5221 /* A symbol from a library loaded via DT_NEEDED of some
5222 other library is referenced by a regular object.
5223 Add a DT_NEEDED entry for it. Issue an error if
5224 --no-add-needed is used and the reference was not
5227 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
5230 /* xgettext:c-format */
5231 (_("%pB: undefined reference to symbol '%s'"),
5233 bfd_set_error (bfd_error_missing_dso
);
5234 goto error_free_vers
;
5237 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
5238 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
5240 /* Create dynamic sections for backends that require
5241 that be done before setup_gnu_properties. */
5242 if (!_bfd_elf_link_create_dynamic_sections (abfd
, info
))
5249 if (info
->lto_plugin_active
5250 && !bfd_link_relocatable (info
)
5251 && (abfd
->flags
& BFD_PLUGIN
) == 0
5257 if (bed
->s
->arch_size
== 32)
5262 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5263 referenced in regular objects so that linker plugin will get
5264 the correct symbol resolution. */
5266 sym_hash
= elf_sym_hashes (abfd
);
5267 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5269 Elf_Internal_Rela
*internal_relocs
;
5270 Elf_Internal_Rela
*rel
, *relend
;
5272 /* Don't check relocations in excluded sections. */
5273 if ((s
->flags
& SEC_RELOC
) == 0
5274 || s
->reloc_count
== 0
5275 || (s
->flags
& SEC_EXCLUDE
) != 0
5276 || ((info
->strip
== strip_all
5277 || info
->strip
== strip_debugger
)
5278 && (s
->flags
& SEC_DEBUGGING
) != 0))
5281 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, s
, NULL
,
5284 if (internal_relocs
== NULL
)
5285 goto error_free_vers
;
5287 rel
= internal_relocs
;
5288 relend
= rel
+ s
->reloc_count
;
5289 for ( ; rel
< relend
; rel
++)
5291 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
5292 struct elf_link_hash_entry
*h
;
5294 /* Skip local symbols. */
5295 if (r_symndx
< extsymoff
)
5298 h
= sym_hash
[r_symndx
- extsymoff
];
5300 h
->root
.non_ir_ref_regular
= 1;
5303 if (elf_section_data (s
)->relocs
!= internal_relocs
)
5304 free (internal_relocs
);
5313 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
5317 /* Restore the symbol table. */
5318 old_ent
= (char *) old_tab
+ tabsize
;
5319 memset (elf_sym_hashes (abfd
), 0,
5320 extsymcount
* sizeof (struct elf_link_hash_entry
*));
5321 htab
->root
.table
.table
= old_table
;
5322 htab
->root
.table
.size
= old_size
;
5323 htab
->root
.table
.count
= old_count
;
5324 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
5325 htab
->root
.undefs
= old_undefs
;
5326 htab
->root
.undefs_tail
= old_undefs_tail
;
5327 if (htab
->dynstr
!= NULL
)
5328 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
5331 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
5333 struct bfd_hash_entry
*p
;
5334 struct elf_link_hash_entry
*h
;
5336 unsigned int alignment_power
;
5337 unsigned int non_ir_ref_dynamic
;
5339 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
5341 h
= (struct elf_link_hash_entry
*) p
;
5342 if (h
->root
.type
== bfd_link_hash_warning
)
5343 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5345 /* Preserve the maximum alignment and size for common
5346 symbols even if this dynamic lib isn't on DT_NEEDED
5347 since it can still be loaded at run time by another
5349 if (h
->root
.type
== bfd_link_hash_common
)
5351 size
= h
->root
.u
.c
.size
;
5352 alignment_power
= h
->root
.u
.c
.p
->alignment_power
;
5357 alignment_power
= 0;
5359 /* Preserve non_ir_ref_dynamic so that this symbol
5360 will be exported when the dynamic lib becomes needed
5361 in the second pass. */
5362 non_ir_ref_dynamic
= h
->root
.non_ir_ref_dynamic
;
5363 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
5364 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5365 h
= (struct elf_link_hash_entry
*) p
;
5366 if (h
->root
.type
== bfd_link_hash_warning
)
5368 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
5369 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5370 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5372 if (h
->root
.type
== bfd_link_hash_common
)
5374 if (size
> h
->root
.u
.c
.size
)
5375 h
->root
.u
.c
.size
= size
;
5376 if (alignment_power
> h
->root
.u
.c
.p
->alignment_power
)
5377 h
->root
.u
.c
.p
->alignment_power
= alignment_power
;
5379 h
->root
.non_ir_ref_dynamic
= non_ir_ref_dynamic
;
5383 /* Make a special call to the linker "notice" function to
5384 tell it that symbols added for crefs may need to be removed. */
5385 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
5386 goto error_free_vers
;
5389 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
5391 free (nondeflt_vers
);
5395 if (old_tab
!= NULL
)
5397 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
5398 goto error_free_vers
;
5403 /* Now that all the symbols from this input file are created, if
5404 not performing a relocatable link, handle .symver foo, foo@BAR
5405 such that any relocs against foo become foo@BAR. */
5406 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5410 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5412 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5413 char *shortname
, *p
;
5416 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5418 || (h
->root
.type
!= bfd_link_hash_defined
5419 && h
->root
.type
!= bfd_link_hash_defweak
))
5422 amt
= p
- h
->root
.root
.string
;
5423 shortname
= (char *) bfd_malloc (amt
+ 1);
5425 goto error_free_vers
;
5426 memcpy (shortname
, h
->root
.root
.string
, amt
);
5427 shortname
[amt
] = '\0';
5429 hi
= (struct elf_link_hash_entry
*)
5430 bfd_link_hash_lookup (&htab
->root
, shortname
,
5431 FALSE
, FALSE
, FALSE
);
5433 && hi
->root
.type
== h
->root
.type
5434 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5435 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5437 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
5438 hi
->root
.type
= bfd_link_hash_indirect
;
5439 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5440 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5441 sym_hash
= elf_sym_hashes (abfd
);
5443 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5444 if (sym_hash
[symidx
] == hi
)
5446 sym_hash
[symidx
] = h
;
5452 free (nondeflt_vers
);
5453 nondeflt_vers
= NULL
;
5456 /* Now set the alias field correctly for all the weak defined
5457 symbols we found. The only way to do this is to search all the
5458 symbols. Since we only need the information for non functions in
5459 dynamic objects, that's the only time we actually put anything on
5460 the list WEAKS. We need this information so that if a regular
5461 object refers to a symbol defined weakly in a dynamic object, the
5462 real symbol in the dynamic object is also put in the dynamic
5463 symbols; we also must arrange for both symbols to point to the
5464 same memory location. We could handle the general case of symbol
5465 aliasing, but a general symbol alias can only be generated in
5466 assembler code, handling it correctly would be very time
5467 consuming, and other ELF linkers don't handle general aliasing
5471 struct elf_link_hash_entry
**hpp
;
5472 struct elf_link_hash_entry
**hppend
;
5473 struct elf_link_hash_entry
**sorted_sym_hash
;
5474 struct elf_link_hash_entry
*h
;
5475 size_t sym_count
, amt
;
5477 /* Since we have to search the whole symbol list for each weak
5478 defined symbol, search time for N weak defined symbols will be
5479 O(N^2). Binary search will cut it down to O(NlogN). */
5480 amt
= extsymcount
* sizeof (*sorted_sym_hash
);
5481 sorted_sym_hash
= bfd_malloc (amt
);
5482 if (sorted_sym_hash
== NULL
)
5484 sym_hash
= sorted_sym_hash
;
5485 hpp
= elf_sym_hashes (abfd
);
5486 hppend
= hpp
+ extsymcount
;
5488 for (; hpp
< hppend
; hpp
++)
5492 && h
->root
.type
== bfd_link_hash_defined
5493 && !bed
->is_function_type (h
->type
))
5501 qsort (sorted_sym_hash
, sym_count
, sizeof (*sorted_sym_hash
),
5504 while (weaks
!= NULL
)
5506 struct elf_link_hash_entry
*hlook
;
5509 size_t i
, j
, idx
= 0;
5512 weaks
= hlook
->u
.alias
;
5513 hlook
->u
.alias
= NULL
;
5515 if (hlook
->root
.type
!= bfd_link_hash_defined
5516 && hlook
->root
.type
!= bfd_link_hash_defweak
)
5519 slook
= hlook
->root
.u
.def
.section
;
5520 vlook
= hlook
->root
.u
.def
.value
;
5526 bfd_signed_vma vdiff
;
5528 h
= sorted_sym_hash
[idx
];
5529 vdiff
= vlook
- h
->root
.u
.def
.value
;
5536 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5546 /* We didn't find a value/section match. */
5550 /* With multiple aliases, or when the weak symbol is already
5551 strongly defined, we have multiple matching symbols and
5552 the binary search above may land on any of them. Step
5553 one past the matching symbol(s). */
5556 h
= sorted_sym_hash
[idx
];
5557 if (h
->root
.u
.def
.section
!= slook
5558 || h
->root
.u
.def
.value
!= vlook
)
5562 /* Now look back over the aliases. Since we sorted by size
5563 as well as value and section, we'll choose the one with
5564 the largest size. */
5567 h
= sorted_sym_hash
[idx
];
5569 /* Stop if value or section doesn't match. */
5570 if (h
->root
.u
.def
.section
!= slook
5571 || h
->root
.u
.def
.value
!= vlook
)
5573 else if (h
!= hlook
)
5575 struct elf_link_hash_entry
*t
;
5578 hlook
->is_weakalias
= 1;
5580 if (t
->u
.alias
!= NULL
)
5581 while (t
->u
.alias
!= h
)
5585 /* If the weak definition is in the list of dynamic
5586 symbols, make sure the real definition is put
5588 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5590 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5593 free (sorted_sym_hash
);
5598 /* If the real definition is in the list of dynamic
5599 symbols, make sure the weak definition is put
5600 there as well. If we don't do this, then the
5601 dynamic loader might not merge the entries for the
5602 real definition and the weak definition. */
5603 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5605 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5606 goto err_free_sym_hash
;
5613 free (sorted_sym_hash
);
5616 if (bed
->check_directives
5617 && !(*bed
->check_directives
) (abfd
, info
))
5620 /* If this is a non-traditional link, try to optimize the handling
5621 of the .stab/.stabstr sections. */
5623 && ! info
->traditional_format
5624 && is_elf_hash_table (htab
)
5625 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5629 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5630 if (stabstr
!= NULL
)
5632 bfd_size_type string_offset
= 0;
5635 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5636 if (CONST_STRNEQ (stab
->name
, ".stab")
5637 && (!stab
->name
[5] ||
5638 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5639 && (stab
->flags
& SEC_MERGE
) == 0
5640 && !bfd_is_abs_section (stab
->output_section
))
5642 struct bfd_elf_section_data
*secdata
;
5644 secdata
= elf_section_data (stab
);
5645 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5646 stabstr
, &secdata
->sec_info
,
5649 if (secdata
->sec_info
)
5650 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5655 if (dynamic
&& add_needed
)
5657 /* Add this bfd to the loaded list. */
5658 struct elf_link_loaded_list
*n
;
5660 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5664 n
->next
= htab
->dyn_loaded
;
5665 htab
->dyn_loaded
= n
;
5667 if (dynamic
&& !add_needed
5668 && (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) != 0)
5669 elf_dyn_lib_class (abfd
) |= DYN_NO_NEEDED
;
5676 free (nondeflt_vers
);
5684 /* Return the linker hash table entry of a symbol that might be
5685 satisfied by an archive symbol. Return -1 on error. */
5687 struct elf_link_hash_entry
*
5688 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5689 struct bfd_link_info
*info
,
5692 struct elf_link_hash_entry
*h
;
5696 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, TRUE
);
5700 /* If this is a default version (the name contains @@), look up the
5701 symbol again with only one `@' as well as without the version.
5702 The effect is that references to the symbol with and without the
5703 version will be matched by the default symbol in the archive. */
5705 p
= strchr (name
, ELF_VER_CHR
);
5706 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5709 /* First check with only one `@'. */
5710 len
= strlen (name
);
5711 copy
= (char *) bfd_alloc (abfd
, len
);
5713 return (struct elf_link_hash_entry
*) -1;
5715 first
= p
- name
+ 1;
5716 memcpy (copy
, name
, first
);
5717 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5719 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, TRUE
);
5722 /* We also need to check references to the symbol without the
5724 copy
[first
- 1] = '\0';
5725 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
5726 FALSE
, FALSE
, TRUE
);
5729 bfd_release (abfd
, copy
);
5733 /* Add symbols from an ELF archive file to the linker hash table. We
5734 don't use _bfd_generic_link_add_archive_symbols because we need to
5735 handle versioned symbols.
5737 Fortunately, ELF archive handling is simpler than that done by
5738 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5739 oddities. In ELF, if we find a symbol in the archive map, and the
5740 symbol is currently undefined, we know that we must pull in that
5743 Unfortunately, we do have to make multiple passes over the symbol
5744 table until nothing further is resolved. */
5747 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5750 unsigned char *included
= NULL
;
5754 const struct elf_backend_data
*bed
;
5755 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
5756 (bfd
*, struct bfd_link_info
*, const char *);
5758 if (! bfd_has_map (abfd
))
5760 /* An empty archive is a special case. */
5761 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5763 bfd_set_error (bfd_error_no_armap
);
5767 /* Keep track of all symbols we know to be already defined, and all
5768 files we know to be already included. This is to speed up the
5769 second and subsequent passes. */
5770 c
= bfd_ardata (abfd
)->symdef_count
;
5773 amt
= c
* sizeof (*included
);
5774 included
= (unsigned char *) bfd_zmalloc (amt
);
5775 if (included
== NULL
)
5778 symdefs
= bfd_ardata (abfd
)->symdefs
;
5779 bed
= get_elf_backend_data (abfd
);
5780 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5793 symdefend
= symdef
+ c
;
5794 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5796 struct elf_link_hash_entry
*h
;
5798 struct bfd_link_hash_entry
*undefs_tail
;
5803 if (symdef
->file_offset
== last
)
5809 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5810 if (h
== (struct elf_link_hash_entry
*) -1)
5816 if (h
->root
.type
== bfd_link_hash_undefined
)
5818 /* If the archive element has already been loaded then one
5819 of the symbols defined by that element might have been
5820 made undefined due to being in a discarded section. */
5824 else if (h
->root
.type
== bfd_link_hash_common
)
5826 /* We currently have a common symbol. The archive map contains
5827 a reference to this symbol, so we may want to include it. We
5828 only want to include it however, if this archive element
5829 contains a definition of the symbol, not just another common
5832 Unfortunately some archivers (including GNU ar) will put
5833 declarations of common symbols into their archive maps, as
5834 well as real definitions, so we cannot just go by the archive
5835 map alone. Instead we must read in the element's symbol
5836 table and check that to see what kind of symbol definition
5838 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5843 if (h
->root
.type
!= bfd_link_hash_undefweak
)
5844 /* Symbol must be defined. Don't check it again. */
5849 /* We need to include this archive member. */
5850 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5851 if (element
== NULL
)
5854 if (! bfd_check_format (element
, bfd_object
))
5857 undefs_tail
= info
->hash
->undefs_tail
;
5859 if (!(*info
->callbacks
5860 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5862 if (!bfd_link_add_symbols (element
, info
))
5865 /* If there are any new undefined symbols, we need to make
5866 another pass through the archive in order to see whether
5867 they can be defined. FIXME: This isn't perfect, because
5868 common symbols wind up on undefs_tail and because an
5869 undefined symbol which is defined later on in this pass
5870 does not require another pass. This isn't a bug, but it
5871 does make the code less efficient than it could be. */
5872 if (undefs_tail
!= info
->hash
->undefs_tail
)
5875 /* Look backward to mark all symbols from this object file
5876 which we have already seen in this pass. */
5880 included
[mark
] = TRUE
;
5885 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5887 /* We mark subsequent symbols from this object file as we go
5888 on through the loop. */
5889 last
= symdef
->file_offset
;
5902 /* Given an ELF BFD, add symbols to the global hash table as
5906 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5908 switch (bfd_get_format (abfd
))
5911 return elf_link_add_object_symbols (abfd
, info
);
5913 return elf_link_add_archive_symbols (abfd
, info
);
5915 bfd_set_error (bfd_error_wrong_format
);
5920 struct hash_codes_info
5922 unsigned long *hashcodes
;
5926 /* This function will be called though elf_link_hash_traverse to store
5927 all hash value of the exported symbols in an array. */
5930 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5932 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
5937 /* Ignore indirect symbols. These are added by the versioning code. */
5938 if (h
->dynindx
== -1)
5941 name
= h
->root
.root
.string
;
5942 if (h
->versioned
>= versioned
)
5944 char *p
= strchr (name
, ELF_VER_CHR
);
5947 alc
= (char *) bfd_malloc (p
- name
+ 1);
5953 memcpy (alc
, name
, p
- name
);
5954 alc
[p
- name
] = '\0';
5959 /* Compute the hash value. */
5960 ha
= bfd_elf_hash (name
);
5962 /* Store the found hash value in the array given as the argument. */
5963 *(inf
->hashcodes
)++ = ha
;
5965 /* And store it in the struct so that we can put it in the hash table
5967 h
->u
.elf_hash_value
= ha
;
5973 struct collect_gnu_hash_codes
5976 const struct elf_backend_data
*bed
;
5977 unsigned long int nsyms
;
5978 unsigned long int maskbits
;
5979 unsigned long int *hashcodes
;
5980 unsigned long int *hashval
;
5981 unsigned long int *indx
;
5982 unsigned long int *counts
;
5986 long int min_dynindx
;
5987 unsigned long int bucketcount
;
5988 unsigned long int symindx
;
5989 long int local_indx
;
5990 long int shift1
, shift2
;
5991 unsigned long int mask
;
5995 /* This function will be called though elf_link_hash_traverse to store
5996 all hash value of the exported symbols in an array. */
5999 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6001 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6006 /* Ignore indirect symbols. These are added by the versioning code. */
6007 if (h
->dynindx
== -1)
6010 /* Ignore also local symbols and undefined symbols. */
6011 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6014 name
= h
->root
.root
.string
;
6015 if (h
->versioned
>= versioned
)
6017 char *p
= strchr (name
, ELF_VER_CHR
);
6020 alc
= (char *) bfd_malloc (p
- name
+ 1);
6026 memcpy (alc
, name
, p
- name
);
6027 alc
[p
- name
] = '\0';
6032 /* Compute the hash value. */
6033 ha
= bfd_elf_gnu_hash (name
);
6035 /* Store the found hash value in the array for compute_bucket_count,
6036 and also for .dynsym reordering purposes. */
6037 s
->hashcodes
[s
->nsyms
] = ha
;
6038 s
->hashval
[h
->dynindx
] = ha
;
6040 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
6041 s
->min_dynindx
= h
->dynindx
;
6047 /* This function will be called though elf_link_hash_traverse to do
6048 final dynamic symbol renumbering in case of .gnu.hash.
6049 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6050 to the translation table. */
6053 elf_gnu_hash_process_symidx (struct elf_link_hash_entry
*h
, void *data
)
6055 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6056 unsigned long int bucket
;
6057 unsigned long int val
;
6059 /* Ignore indirect symbols. */
6060 if (h
->dynindx
== -1)
6063 /* Ignore also local symbols and undefined symbols. */
6064 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6066 if (h
->dynindx
>= s
->min_dynindx
)
6068 if (s
->bed
->record_xhash_symbol
!= NULL
)
6070 (*s
->bed
->record_xhash_symbol
) (h
, 0);
6074 h
->dynindx
= s
->local_indx
++;
6079 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
6080 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
6081 & ((s
->maskbits
>> s
->shift1
) - 1);
6082 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
6084 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
6085 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
6086 if (s
->counts
[bucket
] == 1)
6087 /* Last element terminates the chain. */
6089 bfd_put_32 (s
->output_bfd
, val
,
6090 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
6091 --s
->counts
[bucket
];
6092 if (s
->bed
->record_xhash_symbol
!= NULL
)
6094 bfd_vma xlat_loc
= s
->xlat
+ (s
->indx
[bucket
]++ - s
->symindx
) * 4;
6096 (*s
->bed
->record_xhash_symbol
) (h
, xlat_loc
);
6099 h
->dynindx
= s
->indx
[bucket
]++;
6103 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6106 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
6108 return !(h
->forced_local
6109 || h
->root
.type
== bfd_link_hash_undefined
6110 || h
->root
.type
== bfd_link_hash_undefweak
6111 || ((h
->root
.type
== bfd_link_hash_defined
6112 || h
->root
.type
== bfd_link_hash_defweak
)
6113 && h
->root
.u
.def
.section
->output_section
== NULL
));
6116 /* Array used to determine the number of hash table buckets to use
6117 based on the number of symbols there are. If there are fewer than
6118 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6119 fewer than 37 we use 17 buckets, and so forth. We never use more
6120 than 32771 buckets. */
6122 static const size_t elf_buckets
[] =
6124 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6128 /* Compute bucket count for hashing table. We do not use a static set
6129 of possible tables sizes anymore. Instead we determine for all
6130 possible reasonable sizes of the table the outcome (i.e., the
6131 number of collisions etc) and choose the best solution. The
6132 weighting functions are not too simple to allow the table to grow
6133 without bounds. Instead one of the weighting factors is the size.
6134 Therefore the result is always a good payoff between few collisions
6135 (= short chain lengths) and table size. */
6137 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6138 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
6139 unsigned long int nsyms
,
6142 size_t best_size
= 0;
6143 unsigned long int i
;
6145 /* We have a problem here. The following code to optimize the table
6146 size requires an integer type with more the 32 bits. If
6147 BFD_HOST_U_64_BIT is set we know about such a type. */
6148 #ifdef BFD_HOST_U_64_BIT
6153 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
6154 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
6155 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
6156 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
6157 unsigned long int *counts
;
6159 unsigned int no_improvement_count
= 0;
6161 /* Possible optimization parameters: if we have NSYMS symbols we say
6162 that the hashing table must at least have NSYMS/4 and at most
6164 minsize
= nsyms
/ 4;
6167 best_size
= maxsize
= nsyms
* 2;
6172 if ((best_size
& 31) == 0)
6176 /* Create array where we count the collisions in. We must use bfd_malloc
6177 since the size could be large. */
6179 amt
*= sizeof (unsigned long int);
6180 counts
= (unsigned long int *) bfd_malloc (amt
);
6184 /* Compute the "optimal" size for the hash table. The criteria is a
6185 minimal chain length. The minor criteria is (of course) the size
6187 for (i
= minsize
; i
< maxsize
; ++i
)
6189 /* Walk through the array of hashcodes and count the collisions. */
6190 BFD_HOST_U_64_BIT max
;
6191 unsigned long int j
;
6192 unsigned long int fact
;
6194 if (gnu_hash
&& (i
& 31) == 0)
6197 memset (counts
, '\0', i
* sizeof (unsigned long int));
6199 /* Determine how often each hash bucket is used. */
6200 for (j
= 0; j
< nsyms
; ++j
)
6201 ++counts
[hashcodes
[j
] % i
];
6203 /* For the weight function we need some information about the
6204 pagesize on the target. This is information need not be 100%
6205 accurate. Since this information is not available (so far) we
6206 define it here to a reasonable default value. If it is crucial
6207 to have a better value some day simply define this value. */
6208 # ifndef BFD_TARGET_PAGESIZE
6209 # define BFD_TARGET_PAGESIZE (4096)
6212 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6214 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
6217 /* Variant 1: optimize for short chains. We add the squares
6218 of all the chain lengths (which favors many small chain
6219 over a few long chains). */
6220 for (j
= 0; j
< i
; ++j
)
6221 max
+= counts
[j
] * counts
[j
];
6223 /* This adds penalties for the overall size of the table. */
6224 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6227 /* Variant 2: Optimize a lot more for small table. Here we
6228 also add squares of the size but we also add penalties for
6229 empty slots (the +1 term). */
6230 for (j
= 0; j
< i
; ++j
)
6231 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
6233 /* The overall size of the table is considered, but not as
6234 strong as in variant 1, where it is squared. */
6235 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6239 /* Compare with current best results. */
6240 if (max
< best_chlen
)
6244 no_improvement_count
= 0;
6246 /* PR 11843: Avoid futile long searches for the best bucket size
6247 when there are a large number of symbols. */
6248 else if (++no_improvement_count
== 100)
6255 #endif /* defined (BFD_HOST_U_64_BIT) */
6257 /* This is the fallback solution if no 64bit type is available or if we
6258 are not supposed to spend much time on optimizations. We select the
6259 bucket count using a fixed set of numbers. */
6260 for (i
= 0; elf_buckets
[i
] != 0; i
++)
6262 best_size
= elf_buckets
[i
];
6263 if (nsyms
< elf_buckets
[i
+ 1])
6266 if (gnu_hash
&& best_size
< 2)
6273 /* Size any SHT_GROUP section for ld -r. */
6276 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
6281 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6282 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6283 && (s
= ibfd
->sections
) != NULL
6284 && s
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
6285 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
6290 /* Set a default stack segment size. The value in INFO wins. If it
6291 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6292 undefined it is initialized. */
6295 bfd_elf_stack_segment_size (bfd
*output_bfd
,
6296 struct bfd_link_info
*info
,
6297 const char *legacy_symbol
,
6298 bfd_vma default_size
)
6300 struct elf_link_hash_entry
*h
= NULL
;
6302 /* Look for legacy symbol. */
6304 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
6305 FALSE
, FALSE
, FALSE
);
6306 if (h
&& (h
->root
.type
== bfd_link_hash_defined
6307 || h
->root
.type
== bfd_link_hash_defweak
)
6309 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
6311 /* The symbol has no type if specified on the command line. */
6312 h
->type
= STT_OBJECT
;
6313 if (info
->stacksize
)
6314 /* xgettext:c-format */
6315 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6316 output_bfd
, legacy_symbol
);
6317 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
6318 /* xgettext:c-format */
6319 _bfd_error_handler (_("%pB: %s not absolute"),
6320 output_bfd
, legacy_symbol
);
6322 info
->stacksize
= h
->root
.u
.def
.value
;
6325 if (!info
->stacksize
)
6326 /* If the user didn't set a size, or explicitly inhibit the
6327 size, set it now. */
6328 info
->stacksize
= default_size
;
6330 /* Provide the legacy symbol, if it is referenced. */
6331 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
6332 || h
->root
.type
== bfd_link_hash_undefweak
))
6334 struct bfd_link_hash_entry
*bh
= NULL
;
6336 if (!(_bfd_generic_link_add_one_symbol
6337 (info
, output_bfd
, legacy_symbol
,
6338 BSF_GLOBAL
, bfd_abs_section_ptr
,
6339 info
->stacksize
>= 0 ? info
->stacksize
: 0,
6340 NULL
, FALSE
, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
6343 h
= (struct elf_link_hash_entry
*) bh
;
6345 h
->type
= STT_OBJECT
;
6351 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6353 struct elf_gc_sweep_symbol_info
6355 struct bfd_link_info
*info
;
6356 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
6361 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
6364 && (((h
->root
.type
== bfd_link_hash_defined
6365 || h
->root
.type
== bfd_link_hash_defweak
)
6366 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
6367 && h
->root
.u
.def
.section
->gc_mark
))
6368 || h
->root
.type
== bfd_link_hash_undefined
6369 || h
->root
.type
== bfd_link_hash_undefweak
))
6371 struct elf_gc_sweep_symbol_info
*inf
;
6373 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
6374 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
6377 h
->ref_regular_nonweak
= 0;
6383 /* Set up the sizes and contents of the ELF dynamic sections. This is
6384 called by the ELF linker emulation before_allocation routine. We
6385 must set the sizes of the sections before the linker sets the
6386 addresses of the various sections. */
6389 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
6392 const char *filter_shlib
,
6394 const char *depaudit
,
6395 const char * const *auxiliary_filters
,
6396 struct bfd_link_info
*info
,
6397 asection
**sinterpptr
)
6400 const struct elf_backend_data
*bed
;
6404 if (!is_elf_hash_table (info
->hash
))
6407 dynobj
= elf_hash_table (info
)->dynobj
;
6409 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6411 struct bfd_elf_version_tree
*verdefs
;
6412 struct elf_info_failed asvinfo
;
6413 struct bfd_elf_version_tree
*t
;
6414 struct bfd_elf_version_expr
*d
;
6418 /* If we are supposed to export all symbols into the dynamic symbol
6419 table (this is not the normal case), then do so. */
6420 if (info
->export_dynamic
6421 || (bfd_link_executable (info
) && info
->dynamic
))
6423 struct elf_info_failed eif
;
6427 elf_link_hash_traverse (elf_hash_table (info
),
6428 _bfd_elf_export_symbol
,
6436 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6438 if (soname_indx
== (size_t) -1
6439 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6443 soname_indx
= (size_t) -1;
6445 /* Make all global versions with definition. */
6446 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6447 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6448 if (!d
->symver
&& d
->literal
)
6450 const char *verstr
, *name
;
6451 size_t namelen
, verlen
, newlen
;
6452 char *newname
, *p
, leading_char
;
6453 struct elf_link_hash_entry
*newh
;
6455 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6457 namelen
= strlen (name
) + (leading_char
!= '\0');
6459 verlen
= strlen (verstr
);
6460 newlen
= namelen
+ verlen
+ 3;
6462 newname
= (char *) bfd_malloc (newlen
);
6463 if (newname
== NULL
)
6465 newname
[0] = leading_char
;
6466 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6468 /* Check the hidden versioned definition. */
6469 p
= newname
+ namelen
;
6471 memcpy (p
, verstr
, verlen
+ 1);
6472 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6473 newname
, FALSE
, FALSE
,
6476 || (newh
->root
.type
!= bfd_link_hash_defined
6477 && newh
->root
.type
!= bfd_link_hash_defweak
))
6479 /* Check the default versioned definition. */
6481 memcpy (p
, verstr
, verlen
+ 1);
6482 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6483 newname
, FALSE
, FALSE
,
6488 /* Mark this version if there is a definition and it is
6489 not defined in a shared object. */
6491 && !newh
->def_dynamic
6492 && (newh
->root
.type
== bfd_link_hash_defined
6493 || newh
->root
.type
== bfd_link_hash_defweak
))
6497 /* Attach all the symbols to their version information. */
6498 asvinfo
.info
= info
;
6499 asvinfo
.failed
= FALSE
;
6501 elf_link_hash_traverse (elf_hash_table (info
),
6502 _bfd_elf_link_assign_sym_version
,
6507 if (!info
->allow_undefined_version
)
6509 /* Check if all global versions have a definition. */
6510 bfd_boolean all_defined
= TRUE
;
6511 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6512 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6513 if (d
->literal
&& !d
->symver
&& !d
->script
)
6516 (_("%s: undefined version: %s"),
6517 d
->pattern
, t
->name
);
6518 all_defined
= FALSE
;
6523 bfd_set_error (bfd_error_bad_value
);
6528 /* Set up the version definition section. */
6529 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6530 BFD_ASSERT (s
!= NULL
);
6532 /* We may have created additional version definitions if we are
6533 just linking a regular application. */
6534 verdefs
= info
->version_info
;
6536 /* Skip anonymous version tag. */
6537 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6538 verdefs
= verdefs
->next
;
6540 if (verdefs
== NULL
&& !info
->create_default_symver
)
6541 s
->flags
|= SEC_EXCLUDE
;
6547 Elf_Internal_Verdef def
;
6548 Elf_Internal_Verdaux defaux
;
6549 struct bfd_link_hash_entry
*bh
;
6550 struct elf_link_hash_entry
*h
;
6556 /* Make space for the base version. */
6557 size
+= sizeof (Elf_External_Verdef
);
6558 size
+= sizeof (Elf_External_Verdaux
);
6561 /* Make space for the default version. */
6562 if (info
->create_default_symver
)
6564 size
+= sizeof (Elf_External_Verdef
);
6568 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6570 struct bfd_elf_version_deps
*n
;
6572 /* Don't emit base version twice. */
6576 size
+= sizeof (Elf_External_Verdef
);
6577 size
+= sizeof (Elf_External_Verdaux
);
6580 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6581 size
+= sizeof (Elf_External_Verdaux
);
6585 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6586 if (s
->contents
== NULL
&& s
->size
!= 0)
6589 /* Fill in the version definition section. */
6593 def
.vd_version
= VER_DEF_CURRENT
;
6594 def
.vd_flags
= VER_FLG_BASE
;
6597 if (info
->create_default_symver
)
6599 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6600 def
.vd_next
= sizeof (Elf_External_Verdef
);
6604 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6605 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6606 + sizeof (Elf_External_Verdaux
));
6609 if (soname_indx
!= (size_t) -1)
6611 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6613 def
.vd_hash
= bfd_elf_hash (soname
);
6614 defaux
.vda_name
= soname_indx
;
6621 name
= lbasename (bfd_get_filename (output_bfd
));
6622 def
.vd_hash
= bfd_elf_hash (name
);
6623 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6625 if (indx
== (size_t) -1)
6627 defaux
.vda_name
= indx
;
6629 defaux
.vda_next
= 0;
6631 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6632 (Elf_External_Verdef
*) p
);
6633 p
+= sizeof (Elf_External_Verdef
);
6634 if (info
->create_default_symver
)
6636 /* Add a symbol representing this version. */
6638 if (! (_bfd_generic_link_add_one_symbol
6639 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6641 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6643 h
= (struct elf_link_hash_entry
*) bh
;
6646 h
->type
= STT_OBJECT
;
6647 h
->verinfo
.vertree
= NULL
;
6649 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6652 /* Create a duplicate of the base version with the same
6653 aux block, but different flags. */
6656 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6658 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6659 + sizeof (Elf_External_Verdaux
));
6662 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6663 (Elf_External_Verdef
*) p
);
6664 p
+= sizeof (Elf_External_Verdef
);
6666 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6667 (Elf_External_Verdaux
*) p
);
6668 p
+= sizeof (Elf_External_Verdaux
);
6670 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6673 struct bfd_elf_version_deps
*n
;
6675 /* Don't emit the base version twice. */
6680 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6683 /* Add a symbol representing this version. */
6685 if (! (_bfd_generic_link_add_one_symbol
6686 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6688 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6690 h
= (struct elf_link_hash_entry
*) bh
;
6693 h
->type
= STT_OBJECT
;
6694 h
->verinfo
.vertree
= t
;
6696 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6699 def
.vd_version
= VER_DEF_CURRENT
;
6701 if (t
->globals
.list
== NULL
6702 && t
->locals
.list
== NULL
6704 def
.vd_flags
|= VER_FLG_WEAK
;
6705 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6706 def
.vd_cnt
= cdeps
+ 1;
6707 def
.vd_hash
= bfd_elf_hash (t
->name
);
6708 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6711 /* If a basever node is next, it *must* be the last node in
6712 the chain, otherwise Verdef construction breaks. */
6713 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6714 BFD_ASSERT (t
->next
->next
== NULL
);
6716 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6717 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6718 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6720 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6721 (Elf_External_Verdef
*) p
);
6722 p
+= sizeof (Elf_External_Verdef
);
6724 defaux
.vda_name
= h
->dynstr_index
;
6725 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6727 defaux
.vda_next
= 0;
6728 if (t
->deps
!= NULL
)
6729 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6730 t
->name_indx
= defaux
.vda_name
;
6732 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6733 (Elf_External_Verdaux
*) p
);
6734 p
+= sizeof (Elf_External_Verdaux
);
6736 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6738 if (n
->version_needed
== NULL
)
6740 /* This can happen if there was an error in the
6742 defaux
.vda_name
= 0;
6746 defaux
.vda_name
= n
->version_needed
->name_indx
;
6747 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6750 if (n
->next
== NULL
)
6751 defaux
.vda_next
= 0;
6753 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6755 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6756 (Elf_External_Verdaux
*) p
);
6757 p
+= sizeof (Elf_External_Verdaux
);
6761 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6765 bed
= get_elf_backend_data (output_bfd
);
6767 if (info
->gc_sections
&& bed
->can_gc_sections
)
6769 struct elf_gc_sweep_symbol_info sweep_info
;
6771 /* Remove the symbols that were in the swept sections from the
6772 dynamic symbol table. */
6773 sweep_info
.info
= info
;
6774 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
6775 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
6779 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6782 struct elf_find_verdep_info sinfo
;
6784 /* Work out the size of the version reference section. */
6786 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6787 BFD_ASSERT (s
!= NULL
);
6790 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6791 if (sinfo
.vers
== 0)
6793 sinfo
.failed
= FALSE
;
6795 elf_link_hash_traverse (elf_hash_table (info
),
6796 _bfd_elf_link_find_version_dependencies
,
6801 if (elf_tdata (output_bfd
)->verref
== NULL
)
6802 s
->flags
|= SEC_EXCLUDE
;
6805 Elf_Internal_Verneed
*vn
;
6810 /* Build the version dependency section. */
6813 for (vn
= elf_tdata (output_bfd
)->verref
;
6815 vn
= vn
->vn_nextref
)
6817 Elf_Internal_Vernaux
*a
;
6819 size
+= sizeof (Elf_External_Verneed
);
6821 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6822 size
+= sizeof (Elf_External_Vernaux
);
6826 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6827 if (s
->contents
== NULL
)
6831 for (vn
= elf_tdata (output_bfd
)->verref
;
6833 vn
= vn
->vn_nextref
)
6836 Elf_Internal_Vernaux
*a
;
6840 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6843 vn
->vn_version
= VER_NEED_CURRENT
;
6845 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6846 elf_dt_name (vn
->vn_bfd
) != NULL
6847 ? elf_dt_name (vn
->vn_bfd
)
6848 : lbasename (bfd_get_filename
6851 if (indx
== (size_t) -1)
6854 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
6855 if (vn
->vn_nextref
== NULL
)
6858 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
6859 + caux
* sizeof (Elf_External_Vernaux
));
6861 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
6862 (Elf_External_Verneed
*) p
);
6863 p
+= sizeof (Elf_External_Verneed
);
6865 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6867 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6868 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6869 a
->vna_nodename
, FALSE
);
6870 if (indx
== (size_t) -1)
6873 if (a
->vna_nextptr
== NULL
)
6876 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6878 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6879 (Elf_External_Vernaux
*) p
);
6880 p
+= sizeof (Elf_External_Vernaux
);
6884 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6888 /* Any syms created from now on start with -1 in
6889 got.refcount/offset and plt.refcount/offset. */
6890 elf_hash_table (info
)->init_got_refcount
6891 = elf_hash_table (info
)->init_got_offset
;
6892 elf_hash_table (info
)->init_plt_refcount
6893 = elf_hash_table (info
)->init_plt_offset
;
6895 if (bfd_link_relocatable (info
)
6896 && !_bfd_elf_size_group_sections (info
))
6899 /* The backend may have to create some sections regardless of whether
6900 we're dynamic or not. */
6901 if (bed
->elf_backend_always_size_sections
6902 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
6905 /* Determine any GNU_STACK segment requirements, after the backend
6906 has had a chance to set a default segment size. */
6907 if (info
->execstack
)
6908 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
6909 else if (info
->noexecstack
)
6910 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
6914 asection
*notesec
= NULL
;
6917 for (inputobj
= info
->input_bfds
;
6919 inputobj
= inputobj
->link
.next
)
6924 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
6926 s
= inputobj
->sections
;
6927 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
6930 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
6933 if (s
->flags
& SEC_CODE
)
6937 else if (bed
->default_execstack
)
6940 if (notesec
|| info
->stacksize
> 0)
6941 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
6942 if (notesec
&& exec
&& bfd_link_relocatable (info
)
6943 && notesec
->output_section
!= bfd_abs_section_ptr
)
6944 notesec
->output_section
->flags
|= SEC_CODE
;
6947 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6949 struct elf_info_failed eif
;
6950 struct elf_link_hash_entry
*h
;
6954 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
6955 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
6959 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
6961 info
->flags
|= DF_SYMBOLIC
;
6969 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
6971 if (indx
== (size_t) -1)
6974 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
6975 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
6979 if (filter_shlib
!= NULL
)
6983 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6984 filter_shlib
, TRUE
);
6985 if (indx
== (size_t) -1
6986 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
6990 if (auxiliary_filters
!= NULL
)
6992 const char * const *p
;
6994 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
6998 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7000 if (indx
== (size_t) -1
7001 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
7010 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
7012 if (indx
== (size_t) -1
7013 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
7017 if (depaudit
!= NULL
)
7021 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
7023 if (indx
== (size_t) -1
7024 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
7031 /* Find all symbols which were defined in a dynamic object and make
7032 the backend pick a reasonable value for them. */
7033 elf_link_hash_traverse (elf_hash_table (info
),
7034 _bfd_elf_adjust_dynamic_symbol
,
7039 /* Add some entries to the .dynamic section. We fill in some of the
7040 values later, in bfd_elf_final_link, but we must add the entries
7041 now so that we know the final size of the .dynamic section. */
7043 /* If there are initialization and/or finalization functions to
7044 call then add the corresponding DT_INIT/DT_FINI entries. */
7045 h
= (info
->init_function
7046 ? elf_link_hash_lookup (elf_hash_table (info
),
7047 info
->init_function
, FALSE
,
7054 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
7057 h
= (info
->fini_function
7058 ? elf_link_hash_lookup (elf_hash_table (info
),
7059 info
->fini_function
, FALSE
,
7066 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
7070 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
7071 if (s
!= NULL
&& s
->linker_has_input
)
7073 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7074 if (! bfd_link_executable (info
))
7079 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
7080 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
7081 && (o
= sub
->sections
) != NULL
7082 && o
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
)
7083 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7084 if (elf_section_data (o
)->this_hdr
.sh_type
7085 == SHT_PREINIT_ARRAY
)
7088 (_("%pB: .preinit_array section is not allowed in DSO"),
7093 bfd_set_error (bfd_error_nonrepresentable_section
);
7097 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
7098 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
7101 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
7102 if (s
!= NULL
&& s
->linker_has_input
)
7104 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
7105 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
7108 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
7109 if (s
!= NULL
&& s
->linker_has_input
)
7111 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
7112 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
7116 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
7117 /* If .dynstr is excluded from the link, we don't want any of
7118 these tags. Strictly, we should be checking each section
7119 individually; This quick check covers for the case where
7120 someone does a /DISCARD/ : { *(*) }. */
7121 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
7123 bfd_size_type strsize
;
7125 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7126 if ((info
->emit_hash
7127 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
7128 || (info
->emit_gnu_hash
7129 && (bed
->record_xhash_symbol
== NULL
7130 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0)))
7131 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
7132 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
7133 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
7134 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
7135 bed
->s
->sizeof_sym
))
7140 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
7143 /* The backend must work out the sizes of all the other dynamic
7146 && bed
->elf_backend_size_dynamic_sections
!= NULL
7147 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
7150 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7152 if (elf_tdata (output_bfd
)->cverdefs
)
7154 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
7156 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
7157 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
7161 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
7163 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
7166 else if (info
->flags
& DF_BIND_NOW
)
7168 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
7174 if (bfd_link_executable (info
))
7175 info
->flags_1
&= ~ (DF_1_INITFIRST
7178 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
7182 if (elf_tdata (output_bfd
)->cverrefs
)
7184 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
7186 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
7187 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
7191 if ((elf_tdata (output_bfd
)->cverrefs
== 0
7192 && elf_tdata (output_bfd
)->cverdefs
== 0)
7193 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, NULL
) <= 1)
7197 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7198 s
->flags
|= SEC_EXCLUDE
;
7204 /* Find the first non-excluded output section. We'll use its
7205 section symbol for some emitted relocs. */
7207 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
7210 asection
*found
= NULL
;
7212 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7213 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7214 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7217 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7220 elf_hash_table (info
)->text_index_section
= found
;
7223 /* Find two non-excluded output sections, one for code, one for data.
7224 We'll use their section symbols for some emitted relocs. */
7226 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
7229 asection
*found
= NULL
;
7231 /* Data first, since setting text_index_section changes
7232 _bfd_elf_omit_section_dynsym_default. */
7233 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7234 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7235 && !(s
->flags
& SEC_READONLY
)
7236 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7239 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7242 elf_hash_table (info
)->data_index_section
= found
;
7244 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7245 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7246 && (s
->flags
& SEC_READONLY
)
7247 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7252 elf_hash_table (info
)->text_index_section
= found
;
7255 #define GNU_HASH_SECTION_NAME(bed) \
7256 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7259 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
7261 const struct elf_backend_data
*bed
;
7262 unsigned long section_sym_count
;
7263 bfd_size_type dynsymcount
= 0;
7265 if (!is_elf_hash_table (info
->hash
))
7268 bed
= get_elf_backend_data (output_bfd
);
7269 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
7271 /* Assign dynsym indices. In a shared library we generate a section
7272 symbol for each output section, which come first. Next come all
7273 of the back-end allocated local dynamic syms, followed by the rest
7274 of the global symbols.
7276 This is usually not needed for static binaries, however backends
7277 can request to always do it, e.g. the MIPS backend uses dynamic
7278 symbol counts to lay out GOT, which will be produced in the
7279 presence of GOT relocations even in static binaries (holding fixed
7280 data in that case, to satisfy those relocations). */
7282 if (elf_hash_table (info
)->dynamic_sections_created
7283 || bed
->always_renumber_dynsyms
)
7284 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
7285 §ion_sym_count
);
7287 if (elf_hash_table (info
)->dynamic_sections_created
)
7291 unsigned int dtagcount
;
7293 dynobj
= elf_hash_table (info
)->dynobj
;
7295 /* Work out the size of the symbol version section. */
7296 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7297 BFD_ASSERT (s
!= NULL
);
7298 if ((s
->flags
& SEC_EXCLUDE
) == 0)
7300 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
7301 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7302 if (s
->contents
== NULL
)
7305 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
7309 /* Set the size of the .dynsym and .hash sections. We counted
7310 the number of dynamic symbols in elf_link_add_object_symbols.
7311 We will build the contents of .dynsym and .hash when we build
7312 the final symbol table, because until then we do not know the
7313 correct value to give the symbols. We built the .dynstr
7314 section as we went along in elf_link_add_object_symbols. */
7315 s
= elf_hash_table (info
)->dynsym
;
7316 BFD_ASSERT (s
!= NULL
);
7317 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
7319 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7320 if (s
->contents
== NULL
)
7323 /* The first entry in .dynsym is a dummy symbol. Clear all the
7324 section syms, in case we don't output them all. */
7325 ++section_sym_count
;
7326 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
7328 elf_hash_table (info
)->bucketcount
= 0;
7330 /* Compute the size of the hashing table. As a side effect this
7331 computes the hash values for all the names we export. */
7332 if (info
->emit_hash
)
7334 unsigned long int *hashcodes
;
7335 struct hash_codes_info hashinf
;
7337 unsigned long int nsyms
;
7339 size_t hash_entry_size
;
7341 /* Compute the hash values for all exported symbols. At the same
7342 time store the values in an array so that we could use them for
7344 amt
= dynsymcount
* sizeof (unsigned long int);
7345 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
7346 if (hashcodes
== NULL
)
7348 hashinf
.hashcodes
= hashcodes
;
7349 hashinf
.error
= FALSE
;
7351 /* Put all hash values in HASHCODES. */
7352 elf_link_hash_traverse (elf_hash_table (info
),
7353 elf_collect_hash_codes
, &hashinf
);
7360 nsyms
= hashinf
.hashcodes
- hashcodes
;
7362 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
7365 if (bucketcount
== 0 && nsyms
> 0)
7368 elf_hash_table (info
)->bucketcount
= bucketcount
;
7370 s
= bfd_get_linker_section (dynobj
, ".hash");
7371 BFD_ASSERT (s
!= NULL
);
7372 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
7373 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
7374 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7375 if (s
->contents
== NULL
)
7378 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
7379 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
7380 s
->contents
+ hash_entry_size
);
7383 if (info
->emit_gnu_hash
)
7386 unsigned char *contents
;
7387 struct collect_gnu_hash_codes cinfo
;
7391 memset (&cinfo
, 0, sizeof (cinfo
));
7393 /* Compute the hash values for all exported symbols. At the same
7394 time store the values in an array so that we could use them for
7396 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
7397 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
7398 if (cinfo
.hashcodes
== NULL
)
7401 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
7402 cinfo
.min_dynindx
= -1;
7403 cinfo
.output_bfd
= output_bfd
;
7406 /* Put all hash values in HASHCODES. */
7407 elf_link_hash_traverse (elf_hash_table (info
),
7408 elf_collect_gnu_hash_codes
, &cinfo
);
7411 free (cinfo
.hashcodes
);
7416 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
7418 if (bucketcount
== 0)
7420 free (cinfo
.hashcodes
);
7424 s
= bfd_get_linker_section (dynobj
, GNU_HASH_SECTION_NAME (bed
));
7425 BFD_ASSERT (s
!= NULL
);
7427 if (cinfo
.nsyms
== 0)
7429 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7430 BFD_ASSERT (cinfo
.min_dynindx
== -1);
7431 free (cinfo
.hashcodes
);
7432 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
7433 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7434 if (contents
== NULL
)
7436 s
->contents
= contents
;
7437 /* 1 empty bucket. */
7438 bfd_put_32 (output_bfd
, 1, contents
);
7439 /* SYMIDX above the special symbol 0. */
7440 bfd_put_32 (output_bfd
, 1, contents
+ 4);
7441 /* Just one word for bitmask. */
7442 bfd_put_32 (output_bfd
, 1, contents
+ 8);
7443 /* Only hash fn bloom filter. */
7444 bfd_put_32 (output_bfd
, 0, contents
+ 12);
7445 /* No hashes are valid - empty bitmask. */
7446 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
7447 /* No hashes in the only bucket. */
7448 bfd_put_32 (output_bfd
, 0,
7449 contents
+ 16 + bed
->s
->arch_size
/ 8);
7453 unsigned long int maskwords
, maskbitslog2
, x
;
7454 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
7458 while ((x
>>= 1) != 0)
7460 if (maskbitslog2
< 3)
7462 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
7463 maskbitslog2
= maskbitslog2
+ 3;
7465 maskbitslog2
= maskbitslog2
+ 2;
7466 if (bed
->s
->arch_size
== 64)
7468 if (maskbitslog2
== 5)
7474 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
7475 cinfo
.shift2
= maskbitslog2
;
7476 cinfo
.maskbits
= 1 << maskbitslog2
;
7477 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
7478 amt
= bucketcount
* sizeof (unsigned long int) * 2;
7479 amt
+= maskwords
* sizeof (bfd_vma
);
7480 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
7481 if (cinfo
.bitmask
== NULL
)
7483 free (cinfo
.hashcodes
);
7487 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
7488 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
7489 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
7490 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
7492 /* Determine how often each hash bucket is used. */
7493 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
7494 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
7495 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
7497 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
7498 if (cinfo
.counts
[i
] != 0)
7500 cinfo
.indx
[i
] = cnt
;
7501 cnt
+= cinfo
.counts
[i
];
7503 BFD_ASSERT (cnt
== dynsymcount
);
7504 cinfo
.bucketcount
= bucketcount
;
7505 cinfo
.local_indx
= cinfo
.min_dynindx
;
7507 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
7508 s
->size
+= cinfo
.maskbits
/ 8;
7509 if (bed
->record_xhash_symbol
!= NULL
)
7510 s
->size
+= cinfo
.nsyms
* 4;
7511 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7512 if (contents
== NULL
)
7514 free (cinfo
.bitmask
);
7515 free (cinfo
.hashcodes
);
7519 s
->contents
= contents
;
7520 bfd_put_32 (output_bfd
, bucketcount
, contents
);
7521 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
7522 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
7523 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
7524 contents
+= 16 + cinfo
.maskbits
/ 8;
7526 for (i
= 0; i
< bucketcount
; ++i
)
7528 if (cinfo
.counts
[i
] == 0)
7529 bfd_put_32 (output_bfd
, 0, contents
);
7531 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
7535 cinfo
.contents
= contents
;
7537 cinfo
.xlat
= contents
+ cinfo
.nsyms
* 4 - s
->contents
;
7538 /* Renumber dynamic symbols, if populating .gnu.hash section.
7539 If using .MIPS.xhash, populate the translation table. */
7540 elf_link_hash_traverse (elf_hash_table (info
),
7541 elf_gnu_hash_process_symidx
, &cinfo
);
7543 contents
= s
->contents
+ 16;
7544 for (i
= 0; i
< maskwords
; ++i
)
7546 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
7548 contents
+= bed
->s
->arch_size
/ 8;
7551 free (cinfo
.bitmask
);
7552 free (cinfo
.hashcodes
);
7556 s
= bfd_get_linker_section (dynobj
, ".dynstr");
7557 BFD_ASSERT (s
!= NULL
);
7559 elf_finalize_dynstr (output_bfd
, info
);
7561 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7563 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
7564 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
7571 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7574 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
7577 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
7578 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
7581 /* Finish SHF_MERGE section merging. */
7584 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
7589 if (!is_elf_hash_table (info
->hash
))
7592 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
7593 if ((ibfd
->flags
& DYNAMIC
) == 0
7594 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
7595 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
7596 == get_elf_backend_data (obfd
)->s
->elfclass
))
7597 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7598 if ((sec
->flags
& SEC_MERGE
) != 0
7599 && !bfd_is_abs_section (sec
->output_section
))
7601 struct bfd_elf_section_data
*secdata
;
7603 secdata
= elf_section_data (sec
);
7604 if (! _bfd_add_merge_section (obfd
,
7605 &elf_hash_table (info
)->merge_info
,
7606 sec
, &secdata
->sec_info
))
7608 else if (secdata
->sec_info
)
7609 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7612 if (elf_hash_table (info
)->merge_info
!= NULL
)
7613 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7614 merge_sections_remove_hook
);
7618 /* Create an entry in an ELF linker hash table. */
7620 struct bfd_hash_entry
*
7621 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7622 struct bfd_hash_table
*table
,
7625 /* Allocate the structure if it has not already been allocated by a
7629 entry
= (struct bfd_hash_entry
*)
7630 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7635 /* Call the allocation method of the superclass. */
7636 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7639 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7640 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7642 /* Set local fields. */
7645 ret
->got
= htab
->init_got_refcount
;
7646 ret
->plt
= htab
->init_plt_refcount
;
7647 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7648 - offsetof (struct elf_link_hash_entry
, size
)));
7649 /* Assume that we have been called by a non-ELF symbol reader.
7650 This flag is then reset by the code which reads an ELF input
7651 file. This ensures that a symbol created by a non-ELF symbol
7652 reader will have the flag set correctly. */
7659 /* Copy data from an indirect symbol to its direct symbol, hiding the
7660 old indirect symbol. Also used for copying flags to a weakdef. */
7663 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7664 struct elf_link_hash_entry
*dir
,
7665 struct elf_link_hash_entry
*ind
)
7667 struct elf_link_hash_table
*htab
;
7669 if (ind
->dyn_relocs
!= NULL
)
7671 if (dir
->dyn_relocs
!= NULL
)
7673 struct elf_dyn_relocs
**pp
;
7674 struct elf_dyn_relocs
*p
;
7676 /* Add reloc counts against the indirect sym to the direct sym
7677 list. Merge any entries against the same section. */
7678 for (pp
= &ind
->dyn_relocs
; (p
= *pp
) != NULL
; )
7680 struct elf_dyn_relocs
*q
;
7682 for (q
= dir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
7683 if (q
->sec
== p
->sec
)
7685 q
->pc_count
+= p
->pc_count
;
7686 q
->count
+= p
->count
;
7693 *pp
= dir
->dyn_relocs
;
7696 dir
->dyn_relocs
= ind
->dyn_relocs
;
7697 ind
->dyn_relocs
= NULL
;
7700 /* Copy down any references that we may have already seen to the
7701 symbol which just became indirect. */
7703 if (dir
->versioned
!= versioned_hidden
)
7704 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7705 dir
->ref_regular
|= ind
->ref_regular
;
7706 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7707 dir
->non_got_ref
|= ind
->non_got_ref
;
7708 dir
->needs_plt
|= ind
->needs_plt
;
7709 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7711 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7714 /* Copy over the global and procedure linkage table refcount entries.
7715 These may have been already set up by a check_relocs routine. */
7716 htab
= elf_hash_table (info
);
7717 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7719 if (dir
->got
.refcount
< 0)
7720 dir
->got
.refcount
= 0;
7721 dir
->got
.refcount
+= ind
->got
.refcount
;
7722 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7725 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7727 if (dir
->plt
.refcount
< 0)
7728 dir
->plt
.refcount
= 0;
7729 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7730 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7733 if (ind
->dynindx
!= -1)
7735 if (dir
->dynindx
!= -1)
7736 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7737 dir
->dynindx
= ind
->dynindx
;
7738 dir
->dynstr_index
= ind
->dynstr_index
;
7740 ind
->dynstr_index
= 0;
7745 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7746 struct elf_link_hash_entry
*h
,
7747 bfd_boolean force_local
)
7749 /* STT_GNU_IFUNC symbol must go through PLT. */
7750 if (h
->type
!= STT_GNU_IFUNC
)
7752 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7757 h
->forced_local
= 1;
7758 if (h
->dynindx
!= -1)
7760 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7763 h
->dynstr_index
= 0;
7768 /* Hide a symbol. */
7771 _bfd_elf_link_hide_symbol (bfd
*output_bfd
,
7772 struct bfd_link_info
*info
,
7773 struct bfd_link_hash_entry
*h
)
7775 if (is_elf_hash_table (info
->hash
))
7777 const struct elf_backend_data
*bed
7778 = get_elf_backend_data (output_bfd
);
7779 struct elf_link_hash_entry
*eh
7780 = (struct elf_link_hash_entry
*) h
;
7781 bed
->elf_backend_hide_symbol (info
, eh
, TRUE
);
7782 eh
->def_dynamic
= 0;
7783 eh
->ref_dynamic
= 0;
7784 eh
->dynamic_def
= 0;
7788 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7792 _bfd_elf_link_hash_table_init
7793 (struct elf_link_hash_table
*table
,
7795 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
7796 struct bfd_hash_table
*,
7798 unsigned int entsize
,
7799 enum elf_target_id target_id
)
7802 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
7804 table
->init_got_refcount
.refcount
= can_refcount
- 1;
7805 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
7806 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
7807 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
7808 /* The first dynamic symbol is a dummy. */
7809 table
->dynsymcount
= 1;
7811 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
7813 table
->root
.type
= bfd_link_elf_hash_table
;
7814 table
->hash_table_id
= target_id
;
7815 table
->target_os
= get_elf_backend_data (abfd
)->target_os
;
7820 /* Create an ELF linker hash table. */
7822 struct bfd_link_hash_table
*
7823 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7825 struct elf_link_hash_table
*ret
;
7826 size_t amt
= sizeof (struct elf_link_hash_table
);
7828 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7832 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7833 sizeof (struct elf_link_hash_entry
),
7839 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7844 /* Destroy an ELF linker hash table. */
7847 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7849 struct elf_link_hash_table
*htab
;
7851 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7852 if (htab
->dynstr
!= NULL
)
7853 _bfd_elf_strtab_free (htab
->dynstr
);
7854 _bfd_merge_sections_free (htab
->merge_info
);
7855 _bfd_generic_link_hash_table_free (obfd
);
7858 /* This is a hook for the ELF emulation code in the generic linker to
7859 tell the backend linker what file name to use for the DT_NEEDED
7860 entry for a dynamic object. */
7863 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7865 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7866 && bfd_get_format (abfd
) == bfd_object
)
7867 elf_dt_name (abfd
) = name
;
7871 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
7874 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7875 && bfd_get_format (abfd
) == bfd_object
)
7876 lib_class
= elf_dyn_lib_class (abfd
);
7883 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
7885 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7886 && bfd_get_format (abfd
) == bfd_object
)
7887 elf_dyn_lib_class (abfd
) = lib_class
;
7890 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7891 the linker ELF emulation code. */
7893 struct bfd_link_needed_list
*
7894 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7895 struct bfd_link_info
*info
)
7897 if (! is_elf_hash_table (info
->hash
))
7899 return elf_hash_table (info
)->needed
;
7902 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7903 hook for the linker ELF emulation code. */
7905 struct bfd_link_needed_list
*
7906 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7907 struct bfd_link_info
*info
)
7909 if (! is_elf_hash_table (info
->hash
))
7911 return elf_hash_table (info
)->runpath
;
7914 /* Get the name actually used for a dynamic object for a link. This
7915 is the SONAME entry if there is one. Otherwise, it is the string
7916 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7919 bfd_elf_get_dt_soname (bfd
*abfd
)
7921 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7922 && bfd_get_format (abfd
) == bfd_object
)
7923 return elf_dt_name (abfd
);
7927 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7928 the ELF linker emulation code. */
7931 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
7932 struct bfd_link_needed_list
**pneeded
)
7935 bfd_byte
*dynbuf
= NULL
;
7936 unsigned int elfsec
;
7937 unsigned long shlink
;
7938 bfd_byte
*extdyn
, *extdynend
;
7940 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
7944 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
7945 || bfd_get_format (abfd
) != bfd_object
)
7948 s
= bfd_get_section_by_name (abfd
, ".dynamic");
7949 if (s
== NULL
|| s
->size
== 0)
7952 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
7955 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
7956 if (elfsec
== SHN_BAD
)
7959 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
7961 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
7962 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
7965 extdynend
= extdyn
+ s
->size
;
7966 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
7968 Elf_Internal_Dyn dyn
;
7970 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
7972 if (dyn
.d_tag
== DT_NULL
)
7975 if (dyn
.d_tag
== DT_NEEDED
)
7978 struct bfd_link_needed_list
*l
;
7979 unsigned int tagv
= dyn
.d_un
.d_val
;
7982 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
7987 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
8007 struct elf_symbuf_symbol
8009 unsigned long st_name
; /* Symbol name, index in string tbl */
8010 unsigned char st_info
; /* Type and binding attributes */
8011 unsigned char st_other
; /* Visibilty, and target specific */
8014 struct elf_symbuf_head
8016 struct elf_symbuf_symbol
*ssym
;
8018 unsigned int st_shndx
;
8025 Elf_Internal_Sym
*isym
;
8026 struct elf_symbuf_symbol
*ssym
;
8032 /* Sort references to symbols by ascending section number. */
8035 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
8037 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
8038 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
8040 if (s1
->st_shndx
!= s2
->st_shndx
)
8041 return s1
->st_shndx
> s2
->st_shndx
? 1 : -1;
8042 /* Final sort by the address of the sym in the symbuf ensures
8045 return s1
> s2
? 1 : -1;
8050 elf_sym_name_compare (const void *arg1
, const void *arg2
)
8052 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
8053 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
8054 int ret
= strcmp (s1
->name
, s2
->name
);
8057 if (s1
->u
.p
!= s2
->u
.p
)
8058 return s1
->u
.p
> s2
->u
.p
? 1 : -1;
8062 static struct elf_symbuf_head
*
8063 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
8065 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
8066 struct elf_symbuf_symbol
*ssym
;
8067 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
8068 size_t i
, shndx_count
, total_size
, amt
;
8070 amt
= symcount
* sizeof (*indbuf
);
8071 indbuf
= (Elf_Internal_Sym
**) bfd_malloc (amt
);
8075 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
8076 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
8077 *ind
++ = &isymbuf
[i
];
8080 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
8081 elf_sort_elf_symbol
);
8084 if (indbufend
> indbuf
)
8085 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
8086 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
8089 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
8090 + (indbufend
- indbuf
) * sizeof (*ssym
));
8091 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
8092 if (ssymbuf
== NULL
)
8098 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
8099 ssymbuf
->ssym
= NULL
;
8100 ssymbuf
->count
= shndx_count
;
8101 ssymbuf
->st_shndx
= 0;
8102 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
8104 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
8107 ssymhead
->ssym
= ssym
;
8108 ssymhead
->count
= 0;
8109 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
8111 ssym
->st_name
= (*ind
)->st_name
;
8112 ssym
->st_info
= (*ind
)->st_info
;
8113 ssym
->st_other
= (*ind
)->st_other
;
8116 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
8117 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
8124 /* Check if 2 sections define the same set of local and global
8128 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
8129 struct bfd_link_info
*info
)
8132 const struct elf_backend_data
*bed1
, *bed2
;
8133 Elf_Internal_Shdr
*hdr1
, *hdr2
;
8134 size_t symcount1
, symcount2
;
8135 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
8136 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
8137 Elf_Internal_Sym
*isym
, *isymend
;
8138 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
8139 size_t count1
, count2
, i
;
8140 unsigned int shndx1
, shndx2
;
8146 /* Both sections have to be in ELF. */
8147 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
8148 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
8151 if (elf_section_type (sec1
) != elf_section_type (sec2
))
8154 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
8155 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
8156 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
8159 bed1
= get_elf_backend_data (bfd1
);
8160 bed2
= get_elf_backend_data (bfd2
);
8161 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
8162 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
8163 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
8164 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
8166 if (symcount1
== 0 || symcount2
== 0)
8172 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
8173 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
8175 if (ssymbuf1
== NULL
)
8177 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
8179 if (isymbuf1
== NULL
)
8182 if (!info
->reduce_memory_overheads
)
8184 ssymbuf1
= elf_create_symbuf (symcount1
, isymbuf1
);
8185 elf_tdata (bfd1
)->symbuf
= ssymbuf1
;
8189 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
8191 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
8193 if (isymbuf2
== NULL
)
8196 if (ssymbuf1
!= NULL
&& !info
->reduce_memory_overheads
)
8198 ssymbuf2
= elf_create_symbuf (symcount2
, isymbuf2
);
8199 elf_tdata (bfd2
)->symbuf
= ssymbuf2
;
8203 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
8205 /* Optimized faster version. */
8207 struct elf_symbol
*symp
;
8208 struct elf_symbuf_symbol
*ssym
, *ssymend
;
8211 hi
= ssymbuf1
->count
;
8216 mid
= (lo
+ hi
) / 2;
8217 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
8219 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
8223 count1
= ssymbuf1
[mid
].count
;
8230 hi
= ssymbuf2
->count
;
8235 mid
= (lo
+ hi
) / 2;
8236 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
8238 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
8242 count2
= ssymbuf2
[mid
].count
;
8248 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8252 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
8254 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
8255 if (symtable1
== NULL
|| symtable2
== NULL
)
8259 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
8260 ssym
< ssymend
; ssym
++, symp
++)
8262 symp
->u
.ssym
= ssym
;
8263 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
8269 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
8270 ssym
< ssymend
; ssym
++, symp
++)
8272 symp
->u
.ssym
= ssym
;
8273 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
8278 /* Sort symbol by name. */
8279 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8280 elf_sym_name_compare
);
8281 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8282 elf_sym_name_compare
);
8284 for (i
= 0; i
< count1
; i
++)
8285 /* Two symbols must have the same binding, type and name. */
8286 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
8287 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
8288 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8295 symtable1
= (struct elf_symbol
*)
8296 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
8297 symtable2
= (struct elf_symbol
*)
8298 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
8299 if (symtable1
== NULL
|| symtable2
== NULL
)
8302 /* Count definitions in the section. */
8304 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
8305 if (isym
->st_shndx
== shndx1
)
8306 symtable1
[count1
++].u
.isym
= isym
;
8309 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
8310 if (isym
->st_shndx
== shndx2
)
8311 symtable2
[count2
++].u
.isym
= isym
;
8313 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8316 for (i
= 0; i
< count1
; i
++)
8318 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
8319 symtable1
[i
].u
.isym
->st_name
);
8321 for (i
= 0; i
< count2
; i
++)
8323 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
8324 symtable2
[i
].u
.isym
->st_name
);
8326 /* Sort symbol by name. */
8327 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8328 elf_sym_name_compare
);
8329 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8330 elf_sym_name_compare
);
8332 for (i
= 0; i
< count1
; i
++)
8333 /* Two symbols must have the same binding, type and name. */
8334 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
8335 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
8336 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8350 /* Return TRUE if 2 section types are compatible. */
8353 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
8354 bfd
*bbfd
, const asection
*bsec
)
8358 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
8359 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8362 return elf_section_type (asec
) == elf_section_type (bsec
);
8365 /* Final phase of ELF linker. */
8367 /* A structure we use to avoid passing large numbers of arguments. */
8369 struct elf_final_link_info
8371 /* General link information. */
8372 struct bfd_link_info
*info
;
8375 /* Symbol string table. */
8376 struct elf_strtab_hash
*symstrtab
;
8377 /* .hash section. */
8379 /* symbol version section (.gnu.version). */
8380 asection
*symver_sec
;
8381 /* Buffer large enough to hold contents of any section. */
8383 /* Buffer large enough to hold external relocs of any section. */
8384 void *external_relocs
;
8385 /* Buffer large enough to hold internal relocs of any section. */
8386 Elf_Internal_Rela
*internal_relocs
;
8387 /* Buffer large enough to hold external local symbols of any input
8389 bfd_byte
*external_syms
;
8390 /* And a buffer for symbol section indices. */
8391 Elf_External_Sym_Shndx
*locsym_shndx
;
8392 /* Buffer large enough to hold internal local symbols of any input
8394 Elf_Internal_Sym
*internal_syms
;
8395 /* Array large enough to hold a symbol index for each local symbol
8396 of any input BFD. */
8398 /* Array large enough to hold a section pointer for each local
8399 symbol of any input BFD. */
8400 asection
**sections
;
8401 /* Buffer for SHT_SYMTAB_SHNDX section. */
8402 Elf_External_Sym_Shndx
*symshndxbuf
;
8403 /* Number of STT_FILE syms seen. */
8404 size_t filesym_count
;
8407 /* This struct is used to pass information to elf_link_output_extsym. */
8409 struct elf_outext_info
8412 bfd_boolean localsyms
;
8413 bfd_boolean file_sym_done
;
8414 struct elf_final_link_info
*flinfo
;
8418 /* Support for evaluating a complex relocation.
8420 Complex relocations are generalized, self-describing relocations. The
8421 implementation of them consists of two parts: complex symbols, and the
8422 relocations themselves.
8424 The relocations are use a reserved elf-wide relocation type code (R_RELC
8425 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8426 information (start bit, end bit, word width, etc) into the addend. This
8427 information is extracted from CGEN-generated operand tables within gas.
8429 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8430 internal) representing prefix-notation expressions, including but not
8431 limited to those sorts of expressions normally encoded as addends in the
8432 addend field. The symbol mangling format is:
8435 | <unary-operator> ':' <node>
8436 | <binary-operator> ':' <node> ':' <node>
8439 <literal> := 's' <digits=N> ':' <N character symbol name>
8440 | 'S' <digits=N> ':' <N character section name>
8444 <binary-operator> := as in C
8445 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8448 set_symbol_value (bfd
*bfd_with_globals
,
8449 Elf_Internal_Sym
*isymbuf
,
8454 struct elf_link_hash_entry
**sym_hashes
;
8455 struct elf_link_hash_entry
*h
;
8456 size_t extsymoff
= locsymcount
;
8458 if (symidx
< locsymcount
)
8460 Elf_Internal_Sym
*sym
;
8462 sym
= isymbuf
+ symidx
;
8463 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
8465 /* It is a local symbol: move it to the
8466 "absolute" section and give it a value. */
8467 sym
->st_shndx
= SHN_ABS
;
8468 sym
->st_value
= val
;
8471 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
8475 /* It is a global symbol: set its link type
8476 to "defined" and give it a value. */
8478 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
8479 h
= sym_hashes
[symidx
- extsymoff
];
8480 while (h
->root
.type
== bfd_link_hash_indirect
8481 || h
->root
.type
== bfd_link_hash_warning
)
8482 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8483 h
->root
.type
= bfd_link_hash_defined
;
8484 h
->root
.u
.def
.value
= val
;
8485 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
8489 resolve_symbol (const char *name
,
8491 struct elf_final_link_info
*flinfo
,
8493 Elf_Internal_Sym
*isymbuf
,
8496 Elf_Internal_Sym
*sym
;
8497 struct bfd_link_hash_entry
*global_entry
;
8498 const char *candidate
= NULL
;
8499 Elf_Internal_Shdr
*symtab_hdr
;
8502 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
8504 for (i
= 0; i
< locsymcount
; ++ i
)
8508 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
8511 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
8512 symtab_hdr
->sh_link
,
8515 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8516 name
, candidate
, (unsigned long) sym
->st_value
);
8518 if (candidate
&& strcmp (candidate
, name
) == 0)
8520 asection
*sec
= flinfo
->sections
[i
];
8522 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
8523 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
8525 printf ("Found symbol with value %8.8lx\n",
8526 (unsigned long) *result
);
8532 /* Hmm, haven't found it yet. perhaps it is a global. */
8533 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
8534 FALSE
, FALSE
, TRUE
);
8538 if (global_entry
->type
== bfd_link_hash_defined
8539 || global_entry
->type
== bfd_link_hash_defweak
)
8541 *result
= (global_entry
->u
.def
.value
8542 + global_entry
->u
.def
.section
->output_section
->vma
8543 + global_entry
->u
.def
.section
->output_offset
);
8545 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8546 global_entry
->root
.string
, (unsigned long) *result
);
8554 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8555 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8556 names like "foo.end" which is the end address of section "foo". */
8559 resolve_section (const char *name
,
8567 for (curr
= sections
; curr
; curr
= curr
->next
)
8568 if (strcmp (curr
->name
, name
) == 0)
8570 *result
= curr
->vma
;
8574 /* Hmm. still haven't found it. try pseudo-section names. */
8575 /* FIXME: This could be coded more efficiently... */
8576 for (curr
= sections
; curr
; curr
= curr
->next
)
8578 len
= strlen (curr
->name
);
8579 if (len
> strlen (name
))
8582 if (strncmp (curr
->name
, name
, len
) == 0)
8584 if (strncmp (".end", name
+ len
, 4) == 0)
8586 *result
= (curr
->vma
8587 + curr
->size
/ bfd_octets_per_byte (abfd
, curr
));
8591 /* Insert more pseudo-section names here, if you like. */
8599 undefined_reference (const char *reftype
, const char *name
)
8601 /* xgettext:c-format */
8602 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8607 eval_symbol (bfd_vma
*result
,
8610 struct elf_final_link_info
*flinfo
,
8612 Elf_Internal_Sym
*isymbuf
,
8621 const char *sym
= *symp
;
8623 bfd_boolean symbol_is_section
= FALSE
;
8628 if (len
< 1 || len
> sizeof (symbuf
))
8630 bfd_set_error (bfd_error_invalid_operation
);
8643 *result
= strtoul (sym
, (char **) symp
, 16);
8647 symbol_is_section
= TRUE
;
8651 symlen
= strtol (sym
, (char **) symp
, 10);
8652 sym
= *symp
+ 1; /* Skip the trailing ':'. */
8654 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
8656 bfd_set_error (bfd_error_invalid_operation
);
8660 memcpy (symbuf
, sym
, symlen
);
8661 symbuf
[symlen
] = '\0';
8662 *symp
= sym
+ symlen
;
8664 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8665 the symbol as a section, or vice-versa. so we're pretty liberal in our
8666 interpretation here; section means "try section first", not "must be a
8667 section", and likewise with symbol. */
8669 if (symbol_is_section
)
8671 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
8672 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8673 isymbuf
, locsymcount
))
8675 undefined_reference ("section", symbuf
);
8681 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8682 isymbuf
, locsymcount
)
8683 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8686 undefined_reference ("symbol", symbuf
);
8693 /* All that remains are operators. */
8695 #define UNARY_OP(op) \
8696 if (strncmp (sym, #op, strlen (#op)) == 0) \
8698 sym += strlen (#op); \
8702 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8703 isymbuf, locsymcount, signed_p)) \
8706 *result = op ((bfd_signed_vma) a); \
8712 #define BINARY_OP(op) \
8713 if (strncmp (sym, #op, strlen (#op)) == 0) \
8715 sym += strlen (#op); \
8719 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8720 isymbuf, locsymcount, signed_p)) \
8723 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8724 isymbuf, locsymcount, signed_p)) \
8727 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8757 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
8758 bfd_set_error (bfd_error_invalid_operation
);
8764 put_value (bfd_vma size
,
8765 unsigned long chunksz
,
8770 location
+= (size
- chunksz
);
8772 for (; size
; size
-= chunksz
, location
-= chunksz
)
8777 bfd_put_8 (input_bfd
, x
, location
);
8781 bfd_put_16 (input_bfd
, x
, location
);
8785 bfd_put_32 (input_bfd
, x
, location
);
8786 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8792 bfd_put_64 (input_bfd
, x
, location
);
8793 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8806 get_value (bfd_vma size
,
8807 unsigned long chunksz
,
8814 /* Sanity checks. */
8815 BFD_ASSERT (chunksz
<= sizeof (x
)
8818 && (size
% chunksz
) == 0
8819 && input_bfd
!= NULL
8820 && location
!= NULL
);
8822 if (chunksz
== sizeof (x
))
8824 BFD_ASSERT (size
== chunksz
);
8826 /* Make sure that we do not perform an undefined shift operation.
8827 We know that size == chunksz so there will only be one iteration
8828 of the loop below. */
8832 shift
= 8 * chunksz
;
8834 for (; size
; size
-= chunksz
, location
+= chunksz
)
8839 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
8842 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
8845 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
8849 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
8860 decode_complex_addend (unsigned long *start
, /* in bits */
8861 unsigned long *oplen
, /* in bits */
8862 unsigned long *len
, /* in bits */
8863 unsigned long *wordsz
, /* in bytes */
8864 unsigned long *chunksz
, /* in bytes */
8865 unsigned long *lsb0_p
,
8866 unsigned long *signed_p
,
8867 unsigned long *trunc_p
,
8868 unsigned long encoded
)
8870 * start
= encoded
& 0x3F;
8871 * len
= (encoded
>> 6) & 0x3F;
8872 * oplen
= (encoded
>> 12) & 0x3F;
8873 * wordsz
= (encoded
>> 18) & 0xF;
8874 * chunksz
= (encoded
>> 22) & 0xF;
8875 * lsb0_p
= (encoded
>> 27) & 1;
8876 * signed_p
= (encoded
>> 28) & 1;
8877 * trunc_p
= (encoded
>> 29) & 1;
8880 bfd_reloc_status_type
8881 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
8882 asection
*input_section
,
8884 Elf_Internal_Rela
*rel
,
8887 bfd_vma shift
, x
, mask
;
8888 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
8889 bfd_reloc_status_type r
;
8890 bfd_size_type octets
;
8892 /* Perform this reloc, since it is complex.
8893 (this is not to say that it necessarily refers to a complex
8894 symbol; merely that it is a self-describing CGEN based reloc.
8895 i.e. the addend has the complete reloc information (bit start, end,
8896 word size, etc) encoded within it.). */
8898 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
8899 &chunksz
, &lsb0_p
, &signed_p
,
8900 &trunc_p
, rel
->r_addend
);
8902 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
8905 shift
= (start
+ 1) - len
;
8907 shift
= (8 * wordsz
) - (start
+ len
);
8909 octets
= rel
->r_offset
* bfd_octets_per_byte (input_bfd
, input_section
);
8910 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ octets
);
8913 printf ("Doing complex reloc: "
8914 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8915 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8916 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8917 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
8918 oplen
, (unsigned long) x
, (unsigned long) mask
,
8919 (unsigned long) relocation
);
8924 /* Now do an overflow check. */
8925 r
= bfd_check_overflow ((signed_p
8926 ? complain_overflow_signed
8927 : complain_overflow_unsigned
),
8928 len
, 0, (8 * wordsz
),
8932 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
8935 printf (" relocation: %8.8lx\n"
8936 " shifted mask: %8.8lx\n"
8937 " shifted/masked reloc: %8.8lx\n"
8938 " result: %8.8lx\n",
8939 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
8940 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
8942 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ octets
);
8946 /* Functions to read r_offset from external (target order) reloc
8947 entry. Faster than bfd_getl32 et al, because we let the compiler
8948 know the value is aligned. */
8951 ext32l_r_offset (const void *p
)
8958 const union aligned32
*a
8959 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8961 uint32_t aval
= ( (uint32_t) a
->c
[0]
8962 | (uint32_t) a
->c
[1] << 8
8963 | (uint32_t) a
->c
[2] << 16
8964 | (uint32_t) a
->c
[3] << 24);
8969 ext32b_r_offset (const void *p
)
8976 const union aligned32
*a
8977 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8979 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
8980 | (uint32_t) a
->c
[1] << 16
8981 | (uint32_t) a
->c
[2] << 8
8982 | (uint32_t) a
->c
[3]);
8986 #ifdef BFD_HOST_64_BIT
8988 ext64l_r_offset (const void *p
)
8995 const union aligned64
*a
8996 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8998 uint64_t aval
= ( (uint64_t) a
->c
[0]
8999 | (uint64_t) a
->c
[1] << 8
9000 | (uint64_t) a
->c
[2] << 16
9001 | (uint64_t) a
->c
[3] << 24
9002 | (uint64_t) a
->c
[4] << 32
9003 | (uint64_t) a
->c
[5] << 40
9004 | (uint64_t) a
->c
[6] << 48
9005 | (uint64_t) a
->c
[7] << 56);
9010 ext64b_r_offset (const void *p
)
9017 const union aligned64
*a
9018 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9020 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
9021 | (uint64_t) a
->c
[1] << 48
9022 | (uint64_t) a
->c
[2] << 40
9023 | (uint64_t) a
->c
[3] << 32
9024 | (uint64_t) a
->c
[4] << 24
9025 | (uint64_t) a
->c
[5] << 16
9026 | (uint64_t) a
->c
[6] << 8
9027 | (uint64_t) a
->c
[7]);
9032 /* When performing a relocatable link, the input relocations are
9033 preserved. But, if they reference global symbols, the indices
9034 referenced must be updated. Update all the relocations found in
9038 elf_link_adjust_relocs (bfd
*abfd
,
9040 struct bfd_elf_section_reloc_data
*reldata
,
9042 struct bfd_link_info
*info
)
9045 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9047 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9048 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9049 bfd_vma r_type_mask
;
9051 unsigned int count
= reldata
->count
;
9052 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
9054 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
9056 swap_in
= bed
->s
->swap_reloc_in
;
9057 swap_out
= bed
->s
->swap_reloc_out
;
9059 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
9061 swap_in
= bed
->s
->swap_reloca_in
;
9062 swap_out
= bed
->s
->swap_reloca_out
;
9067 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
9070 if (bed
->s
->arch_size
== 32)
9077 r_type_mask
= 0xffffffff;
9081 erela
= reldata
->hdr
->contents
;
9082 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
9084 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
9087 if (*rel_hash
== NULL
)
9090 if ((*rel_hash
)->indx
== -2
9091 && info
->gc_sections
9092 && ! info
->gc_keep_exported
)
9094 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9095 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9097 (*rel_hash
)->root
.root
.string
);
9098 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9100 bfd_set_error (bfd_error_invalid_operation
);
9103 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
9105 (*swap_in
) (abfd
, erela
, irela
);
9106 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
9107 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
9108 | (irela
[j
].r_info
& r_type_mask
));
9109 (*swap_out
) (abfd
, irela
, erela
);
9112 if (bed
->elf_backend_update_relocs
)
9113 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
9115 if (sort
&& count
!= 0)
9117 bfd_vma (*ext_r_off
) (const void *);
9120 bfd_byte
*base
, *end
, *p
, *loc
;
9121 bfd_byte
*buf
= NULL
;
9123 if (bed
->s
->arch_size
== 32)
9125 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9126 ext_r_off
= ext32l_r_offset
;
9127 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9128 ext_r_off
= ext32b_r_offset
;
9134 #ifdef BFD_HOST_64_BIT
9135 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9136 ext_r_off
= ext64l_r_offset
;
9137 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9138 ext_r_off
= ext64b_r_offset
;
9144 /* Must use a stable sort here. A modified insertion sort,
9145 since the relocs are mostly sorted already. */
9146 elt_size
= reldata
->hdr
->sh_entsize
;
9147 base
= reldata
->hdr
->contents
;
9148 end
= base
+ count
* elt_size
;
9149 if (elt_size
> sizeof (Elf64_External_Rela
))
9152 /* Ensure the first element is lowest. This acts as a sentinel,
9153 speeding the main loop below. */
9154 r_off
= (*ext_r_off
) (base
);
9155 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
9157 bfd_vma r_off2
= (*ext_r_off
) (p
);
9166 /* Don't just swap *base and *loc as that changes the order
9167 of the original base[0] and base[1] if they happen to
9168 have the same r_offset. */
9169 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
9170 memcpy (onebuf
, loc
, elt_size
);
9171 memmove (base
+ elt_size
, base
, loc
- base
);
9172 memcpy (base
, onebuf
, elt_size
);
9175 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
9177 /* base to p is sorted, *p is next to insert. */
9178 r_off
= (*ext_r_off
) (p
);
9179 /* Search the sorted region for location to insert. */
9181 while (r_off
< (*ext_r_off
) (loc
))
9186 /* Chances are there is a run of relocs to insert here,
9187 from one of more input files. Files are not always
9188 linked in order due to the way elf_link_input_bfd is
9189 called. See pr17666. */
9190 size_t sortlen
= p
- loc
;
9191 bfd_vma r_off2
= (*ext_r_off
) (loc
);
9192 size_t runlen
= elt_size
;
9193 size_t buf_size
= 96 * 1024;
9194 while (p
+ runlen
< end
9195 && (sortlen
<= buf_size
9196 || runlen
+ elt_size
<= buf_size
)
9197 && r_off2
> (*ext_r_off
) (p
+ runlen
))
9201 buf
= bfd_malloc (buf_size
);
9205 if (runlen
< sortlen
)
9207 memcpy (buf
, p
, runlen
);
9208 memmove (loc
+ runlen
, loc
, sortlen
);
9209 memcpy (loc
, buf
, runlen
);
9213 memcpy (buf
, loc
, sortlen
);
9214 memmove (loc
, p
, runlen
);
9215 memcpy (loc
+ runlen
, buf
, sortlen
);
9217 p
+= runlen
- elt_size
;
9220 /* Hashes are no longer valid. */
9221 free (reldata
->hashes
);
9222 reldata
->hashes
= NULL
;
9228 struct elf_link_sort_rela
9234 enum elf_reloc_type_class type
;
9235 /* We use this as an array of size int_rels_per_ext_rel. */
9236 Elf_Internal_Rela rela
[1];
9239 /* qsort stability here and for cmp2 is only an issue if multiple
9240 dynamic relocations are emitted at the same address. But targets
9241 that apply a series of dynamic relocations each operating on the
9242 result of the prior relocation can't use -z combreloc as
9243 implemented anyway. Such schemes tend to be broken by sorting on
9244 symbol index. That leaves dynamic NONE relocs as the only other
9245 case where ld might emit multiple relocs at the same address, and
9246 those are only emitted due to target bugs. */
9249 elf_link_sort_cmp1 (const void *A
, const void *B
)
9251 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9252 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9253 int relativea
, relativeb
;
9255 relativea
= a
->type
== reloc_class_relative
;
9256 relativeb
= b
->type
== reloc_class_relative
;
9258 if (relativea
< relativeb
)
9260 if (relativea
> relativeb
)
9262 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
9264 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
9266 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9268 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9274 elf_link_sort_cmp2 (const void *A
, const void *B
)
9276 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9277 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9279 if (a
->type
< b
->type
)
9281 if (a
->type
> b
->type
)
9283 if (a
->u
.offset
< b
->u
.offset
)
9285 if (a
->u
.offset
> b
->u
.offset
)
9287 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9289 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9295 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
9297 asection
*dynamic_relocs
;
9300 bfd_size_type count
, size
;
9301 size_t i
, ret
, sort_elt
, ext_size
;
9302 bfd_byte
*sort
, *s_non_relative
, *p
;
9303 struct elf_link_sort_rela
*sq
;
9304 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9305 int i2e
= bed
->s
->int_rels_per_ext_rel
;
9306 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
9307 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9308 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9309 struct bfd_link_order
*lo
;
9311 bfd_boolean use_rela
;
9313 /* Find a dynamic reloc section. */
9314 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
9315 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
9316 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
9317 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9319 bfd_boolean use_rela_initialised
= FALSE
;
9321 /* This is just here to stop gcc from complaining.
9322 Its initialization checking code is not perfect. */
9325 /* Both sections are present. Examine the sizes
9326 of the indirect sections to help us choose. */
9327 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9328 if (lo
->type
== bfd_indirect_link_order
)
9330 asection
*o
= lo
->u
.indirect
.section
;
9332 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9334 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9335 /* Section size is divisible by both rel and rela sizes.
9336 It is of no help to us. */
9340 /* Section size is only divisible by rela. */
9341 if (use_rela_initialised
&& !use_rela
)
9343 _bfd_error_handler (_("%pB: unable to sort relocs - "
9344 "they are in more than one size"),
9346 bfd_set_error (bfd_error_invalid_operation
);
9352 use_rela_initialised
= TRUE
;
9356 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9358 /* Section size is only divisible by rel. */
9359 if (use_rela_initialised
&& use_rela
)
9361 _bfd_error_handler (_("%pB: unable to sort relocs - "
9362 "they are in more than one size"),
9364 bfd_set_error (bfd_error_invalid_operation
);
9370 use_rela_initialised
= TRUE
;
9375 /* The section size is not divisible by either -
9376 something is wrong. */
9377 _bfd_error_handler (_("%pB: unable to sort relocs - "
9378 "they are of an unknown size"), abfd
);
9379 bfd_set_error (bfd_error_invalid_operation
);
9384 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9385 if (lo
->type
== bfd_indirect_link_order
)
9387 asection
*o
= lo
->u
.indirect
.section
;
9389 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9391 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9392 /* Section size is divisible by both rel and rela sizes.
9393 It is of no help to us. */
9397 /* Section size is only divisible by rela. */
9398 if (use_rela_initialised
&& !use_rela
)
9400 _bfd_error_handler (_("%pB: unable to sort relocs - "
9401 "they are in more than one size"),
9403 bfd_set_error (bfd_error_invalid_operation
);
9409 use_rela_initialised
= TRUE
;
9413 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9415 /* Section size is only divisible by rel. */
9416 if (use_rela_initialised
&& use_rela
)
9418 _bfd_error_handler (_("%pB: unable to sort relocs - "
9419 "they are in more than one size"),
9421 bfd_set_error (bfd_error_invalid_operation
);
9427 use_rela_initialised
= TRUE
;
9432 /* The section size is not divisible by either -
9433 something is wrong. */
9434 _bfd_error_handler (_("%pB: unable to sort relocs - "
9435 "they are of an unknown size"), abfd
);
9436 bfd_set_error (bfd_error_invalid_operation
);
9441 if (! use_rela_initialised
)
9445 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
9447 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9454 dynamic_relocs
= rela_dyn
;
9455 ext_size
= bed
->s
->sizeof_rela
;
9456 swap_in
= bed
->s
->swap_reloca_in
;
9457 swap_out
= bed
->s
->swap_reloca_out
;
9461 dynamic_relocs
= rel_dyn
;
9462 ext_size
= bed
->s
->sizeof_rel
;
9463 swap_in
= bed
->s
->swap_reloc_in
;
9464 swap_out
= bed
->s
->swap_reloc_out
;
9468 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9469 if (lo
->type
== bfd_indirect_link_order
)
9470 size
+= lo
->u
.indirect
.section
->size
;
9472 if (size
!= dynamic_relocs
->size
)
9475 sort_elt
= (sizeof (struct elf_link_sort_rela
)
9476 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
9478 count
= dynamic_relocs
->size
/ ext_size
;
9481 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
9485 (*info
->callbacks
->warning
)
9486 (info
, _("not enough memory to sort relocations"), 0, abfd
, 0, 0);
9490 if (bed
->s
->arch_size
== 32)
9491 r_sym_mask
= ~(bfd_vma
) 0xff;
9493 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
9495 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9496 if (lo
->type
== bfd_indirect_link_order
)
9498 bfd_byte
*erel
, *erelend
;
9499 asection
*o
= lo
->u
.indirect
.section
;
9501 if (o
->contents
== NULL
&& o
->size
!= 0)
9503 /* This is a reloc section that is being handled as a normal
9504 section. See bfd_section_from_shdr. We can't combine
9505 relocs in this case. */
9510 erelend
= o
->contents
+ o
->size
;
9511 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
9513 while (erel
< erelend
)
9515 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9517 (*swap_in
) (abfd
, erel
, s
->rela
);
9518 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
9519 s
->u
.sym_mask
= r_sym_mask
;
9525 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
9527 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
9529 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9530 if (s
->type
!= reloc_class_relative
)
9536 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
9537 for (; i
< count
; i
++, p
+= sort_elt
)
9539 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
9540 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
9542 sp
->u
.offset
= sq
->rela
->r_offset
;
9545 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
9547 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
9548 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
9550 /* We have plt relocs in .rela.dyn. */
9551 sq
= (struct elf_link_sort_rela
*) sort
;
9552 for (i
= 0; i
< count
; i
++)
9553 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
9555 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
9557 struct bfd_link_order
**plo
;
9558 /* Put srelplt link_order last. This is so the output_offset
9559 set in the next loop is correct for DT_JMPREL. */
9560 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
9561 if ((*plo
)->type
== bfd_indirect_link_order
9562 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
9568 plo
= &(*plo
)->next
;
9571 dynamic_relocs
->map_tail
.link_order
= lo
;
9576 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9577 if (lo
->type
== bfd_indirect_link_order
)
9579 bfd_byte
*erel
, *erelend
;
9580 asection
*o
= lo
->u
.indirect
.section
;
9583 erelend
= o
->contents
+ o
->size
;
9584 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
9585 while (erel
< erelend
)
9587 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9588 (*swap_out
) (abfd
, s
->rela
, erel
);
9595 *psec
= dynamic_relocs
;
9599 /* Add a symbol to the output symbol string table. */
9602 elf_link_output_symstrtab (struct elf_final_link_info
*flinfo
,
9604 Elf_Internal_Sym
*elfsym
,
9605 asection
*input_sec
,
9606 struct elf_link_hash_entry
*h
)
9608 int (*output_symbol_hook
)
9609 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
9610 struct elf_link_hash_entry
*);
9611 struct elf_link_hash_table
*hash_table
;
9612 const struct elf_backend_data
*bed
;
9613 bfd_size_type strtabsize
;
9615 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9617 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9618 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
9619 if (output_symbol_hook
!= NULL
)
9621 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
9626 if (ELF_ST_TYPE (elfsym
->st_info
) == STT_GNU_IFUNC
)
9627 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_ifunc
;
9628 if (ELF_ST_BIND (elfsym
->st_info
) == STB_GNU_UNIQUE
)
9629 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_unique
;
9633 || (input_sec
->flags
& SEC_EXCLUDE
))
9634 elfsym
->st_name
= (unsigned long) -1;
9637 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9638 to get the final offset for st_name. */
9640 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
9642 if (elfsym
->st_name
== (unsigned long) -1)
9646 hash_table
= elf_hash_table (flinfo
->info
);
9647 strtabsize
= hash_table
->strtabsize
;
9648 if (strtabsize
<= hash_table
->strtabcount
)
9650 strtabsize
+= strtabsize
;
9651 hash_table
->strtabsize
= strtabsize
;
9652 strtabsize
*= sizeof (*hash_table
->strtab
);
9654 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
9656 if (hash_table
->strtab
== NULL
)
9659 hash_table
->strtab
[hash_table
->strtabcount
].sym
= *elfsym
;
9660 hash_table
->strtab
[hash_table
->strtabcount
].dest_index
9661 = hash_table
->strtabcount
;
9662 hash_table
->strtab
[hash_table
->strtabcount
].destshndx_index
9663 = flinfo
->symshndxbuf
? bfd_get_symcount (flinfo
->output_bfd
) : 0;
9665 flinfo
->output_bfd
->symcount
+= 1;
9666 hash_table
->strtabcount
+= 1;
9671 /* Swap symbols out to the symbol table and flush the output symbols to
9675 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
9677 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
9680 const struct elf_backend_data
*bed
;
9682 Elf_Internal_Shdr
*hdr
;
9686 if (!hash_table
->strtabcount
)
9689 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9691 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9693 amt
= bed
->s
->sizeof_sym
* hash_table
->strtabcount
;
9694 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
9698 if (flinfo
->symshndxbuf
)
9700 amt
= sizeof (Elf_External_Sym_Shndx
);
9701 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
9702 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
9703 if (flinfo
->symshndxbuf
== NULL
)
9710 for (i
= 0; i
< hash_table
->strtabcount
; i
++)
9712 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
9713 if (elfsym
->sym
.st_name
== (unsigned long) -1)
9714 elfsym
->sym
.st_name
= 0;
9717 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
9718 elfsym
->sym
.st_name
);
9719 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
9720 ((bfd_byte
*) symbuf
9721 + (elfsym
->dest_index
9722 * bed
->s
->sizeof_sym
)),
9723 (flinfo
->symshndxbuf
9724 + elfsym
->destshndx_index
));
9727 /* Allow the linker to examine the strtab and symtab now they are
9730 if (flinfo
->info
->callbacks
->examine_strtab
)
9731 flinfo
->info
->callbacks
->examine_strtab (hash_table
->strtab
,
9732 hash_table
->strtabcount
,
9735 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
9736 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
9737 amt
= hash_table
->strtabcount
* bed
->s
->sizeof_sym
;
9738 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
9739 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
9741 hdr
->sh_size
+= amt
;
9749 free (hash_table
->strtab
);
9750 hash_table
->strtab
= NULL
;
9755 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9758 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
9760 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
9761 && sym
->st_shndx
< SHN_LORESERVE
)
9763 /* The gABI doesn't support dynamic symbols in output sections
9766 /* xgettext:c-format */
9767 (_("%pB: too many sections: %d (>= %d)"),
9768 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
9769 bfd_set_error (bfd_error_nonrepresentable_section
);
9775 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9776 allowing an unsatisfied unversioned symbol in the DSO to match a
9777 versioned symbol that would normally require an explicit version.
9778 We also handle the case that a DSO references a hidden symbol
9779 which may be satisfied by a versioned symbol in another DSO. */
9782 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
9783 const struct elf_backend_data
*bed
,
9784 struct elf_link_hash_entry
*h
)
9787 struct elf_link_loaded_list
*loaded
;
9789 if (!is_elf_hash_table (info
->hash
))
9792 /* Check indirect symbol. */
9793 while (h
->root
.type
== bfd_link_hash_indirect
)
9794 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9796 switch (h
->root
.type
)
9802 case bfd_link_hash_undefined
:
9803 case bfd_link_hash_undefweak
:
9804 abfd
= h
->root
.u
.undef
.abfd
;
9806 || (abfd
->flags
& DYNAMIC
) == 0
9807 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
9811 case bfd_link_hash_defined
:
9812 case bfd_link_hash_defweak
:
9813 abfd
= h
->root
.u
.def
.section
->owner
;
9816 case bfd_link_hash_common
:
9817 abfd
= h
->root
.u
.c
.p
->section
->owner
;
9820 BFD_ASSERT (abfd
!= NULL
);
9822 for (loaded
= elf_hash_table (info
)->dyn_loaded
;
9824 loaded
= loaded
->next
)
9827 Elf_Internal_Shdr
*hdr
;
9831 Elf_Internal_Shdr
*versymhdr
;
9832 Elf_Internal_Sym
*isym
;
9833 Elf_Internal_Sym
*isymend
;
9834 Elf_Internal_Sym
*isymbuf
;
9835 Elf_External_Versym
*ever
;
9836 Elf_External_Versym
*extversym
;
9838 input
= loaded
->abfd
;
9840 /* We check each DSO for a possible hidden versioned definition. */
9842 || elf_dynversym (input
) == 0)
9845 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
9847 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9848 if (elf_bad_symtab (input
))
9850 extsymcount
= symcount
;
9855 extsymcount
= symcount
- hdr
->sh_info
;
9856 extsymoff
= hdr
->sh_info
;
9859 if (extsymcount
== 0)
9862 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
9864 if (isymbuf
== NULL
)
9867 /* Read in any version definitions. */
9868 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
9869 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
9870 || (extversym
= (Elf_External_Versym
*)
9871 _bfd_malloc_and_read (input
, versymhdr
->sh_size
,
9872 versymhdr
->sh_size
)) == NULL
)
9878 ever
= extversym
+ extsymoff
;
9879 isymend
= isymbuf
+ extsymcount
;
9880 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
9883 Elf_Internal_Versym iver
;
9884 unsigned short version_index
;
9886 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
9887 || isym
->st_shndx
== SHN_UNDEF
)
9890 name
= bfd_elf_string_from_elf_section (input
,
9893 if (strcmp (name
, h
->root
.root
.string
) != 0)
9896 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
9898 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
9900 && h
->forced_local
))
9902 /* If we have a non-hidden versioned sym, then it should
9903 have provided a definition for the undefined sym unless
9904 it is defined in a non-shared object and forced local.
9909 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
9910 if (version_index
== 1 || version_index
== 2)
9912 /* This is the base or first version. We can use it. */
9926 /* Convert ELF common symbol TYPE. */
9929 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
9931 /* Commom symbol can only appear in relocatable link. */
9932 if (!bfd_link_relocatable (info
))
9934 switch (info
->elf_stt_common
)
9938 case elf_stt_common
:
9941 case no_elf_stt_common
:
9948 /* Add an external symbol to the symbol table. This is called from
9949 the hash table traversal routine. When generating a shared object,
9950 we go through the symbol table twice. The first time we output
9951 anything that might have been forced to local scope in a version
9952 script. The second time we output the symbols that are still
9956 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
9958 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
9959 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
9960 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
9962 Elf_Internal_Sym sym
;
9963 asection
*input_sec
;
9964 const struct elf_backend_data
*bed
;
9969 if (h
->root
.type
== bfd_link_hash_warning
)
9971 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9972 if (h
->root
.type
== bfd_link_hash_new
)
9976 /* Decide whether to output this symbol in this pass. */
9977 if (eoinfo
->localsyms
)
9979 if (!h
->forced_local
)
9984 if (h
->forced_local
)
9988 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9990 if (h
->root
.type
== bfd_link_hash_undefined
)
9992 /* If we have an undefined symbol reference here then it must have
9993 come from a shared library that is being linked in. (Undefined
9994 references in regular files have already been handled unless
9995 they are in unreferenced sections which are removed by garbage
9997 bfd_boolean ignore_undef
= FALSE
;
9999 /* Some symbols may be special in that the fact that they're
10000 undefined can be safely ignored - let backend determine that. */
10001 if (bed
->elf_backend_ignore_undef_symbol
)
10002 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
10004 /* If we are reporting errors for this situation then do so now. */
10006 && h
->ref_dynamic_nonweak
10007 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
10008 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
10009 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
10011 flinfo
->info
->callbacks
->undefined_symbol
10012 (flinfo
->info
, h
->root
.root
.string
,
10013 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
, NULL
, 0,
10014 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_DIAGNOSE
10015 && !flinfo
->info
->warn_unresolved_syms
);
10018 /* Strip a global symbol defined in a discarded section. */
10023 /* We should also warn if a forced local symbol is referenced from
10024 shared libraries. */
10025 if (bfd_link_executable (flinfo
->info
)
10030 && h
->ref_dynamic_nonweak
10031 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
10035 struct elf_link_hash_entry
*hi
= h
;
10037 /* Check indirect symbol. */
10038 while (hi
->root
.type
== bfd_link_hash_indirect
)
10039 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
10041 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
10042 /* xgettext:c-format */
10043 msg
= _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10044 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
10045 /* xgettext:c-format */
10046 msg
= _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10048 /* xgettext:c-format */
10049 msg
= _("%pB: local symbol `%s' in %pB is referenced by DSO");
10050 def_bfd
= flinfo
->output_bfd
;
10051 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
10052 def_bfd
= hi
->root
.u
.def
.section
->owner
;
10053 _bfd_error_handler (msg
, flinfo
->output_bfd
,
10054 h
->root
.root
.string
, def_bfd
);
10055 bfd_set_error (bfd_error_bad_value
);
10056 eoinfo
->failed
= TRUE
;
10060 /* We don't want to output symbols that have never been mentioned by
10061 a regular file, or that we have been told to strip. However, if
10062 h->indx is set to -2, the symbol is used by a reloc and we must
10067 else if ((h
->def_dynamic
10069 || h
->root
.type
== bfd_link_hash_new
)
10071 && !h
->ref_regular
)
10073 else if (flinfo
->info
->strip
== strip_all
)
10075 else if (flinfo
->info
->strip
== strip_some
10076 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
10077 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
10079 else if ((h
->root
.type
== bfd_link_hash_defined
10080 || h
->root
.type
== bfd_link_hash_defweak
)
10081 && ((flinfo
->info
->strip_discarded
10082 && discarded_section (h
->root
.u
.def
.section
))
10083 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
10084 && h
->root
.u
.def
.section
->owner
!= NULL
10085 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
10087 else if ((h
->root
.type
== bfd_link_hash_undefined
10088 || h
->root
.type
== bfd_link_hash_undefweak
)
10089 && h
->root
.u
.undef
.abfd
!= NULL
10090 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
10095 /* If we're stripping it, and it's not a dynamic symbol, there's
10096 nothing else to do. However, if it is a forced local symbol or
10097 an ifunc symbol we need to give the backend finish_dynamic_symbol
10098 function a chance to make it dynamic. */
10100 && h
->dynindx
== -1
10101 && type
!= STT_GNU_IFUNC
10102 && !h
->forced_local
)
10106 sym
.st_size
= h
->size
;
10107 sym
.st_other
= h
->other
;
10108 switch (h
->root
.type
)
10111 case bfd_link_hash_new
:
10112 case bfd_link_hash_warning
:
10116 case bfd_link_hash_undefined
:
10117 case bfd_link_hash_undefweak
:
10118 input_sec
= bfd_und_section_ptr
;
10119 sym
.st_shndx
= SHN_UNDEF
;
10122 case bfd_link_hash_defined
:
10123 case bfd_link_hash_defweak
:
10125 input_sec
= h
->root
.u
.def
.section
;
10126 if (input_sec
->output_section
!= NULL
)
10129 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
10130 input_sec
->output_section
);
10131 if (sym
.st_shndx
== SHN_BAD
)
10134 /* xgettext:c-format */
10135 (_("%pB: could not find output section %pA for input section %pA"),
10136 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
10137 bfd_set_error (bfd_error_nonrepresentable_section
);
10138 eoinfo
->failed
= TRUE
;
10142 /* ELF symbols in relocatable files are section relative,
10143 but in nonrelocatable files they are virtual
10145 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
10146 if (!bfd_link_relocatable (flinfo
->info
))
10148 sym
.st_value
+= input_sec
->output_section
->vma
;
10149 if (h
->type
== STT_TLS
)
10151 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
10152 if (tls_sec
!= NULL
)
10153 sym
.st_value
-= tls_sec
->vma
;
10159 BFD_ASSERT (input_sec
->owner
== NULL
10160 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
10161 sym
.st_shndx
= SHN_UNDEF
;
10162 input_sec
= bfd_und_section_ptr
;
10167 case bfd_link_hash_common
:
10168 input_sec
= h
->root
.u
.c
.p
->section
;
10169 sym
.st_shndx
= bed
->common_section_index (input_sec
);
10170 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
10173 case bfd_link_hash_indirect
:
10174 /* These symbols are created by symbol versioning. They point
10175 to the decorated version of the name. For example, if the
10176 symbol foo@@GNU_1.2 is the default, which should be used when
10177 foo is used with no version, then we add an indirect symbol
10178 foo which points to foo@@GNU_1.2. We ignore these symbols,
10179 since the indirected symbol is already in the hash table. */
10183 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
10184 switch (h
->root
.type
)
10186 case bfd_link_hash_common
:
10187 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10189 case bfd_link_hash_defined
:
10190 case bfd_link_hash_defweak
:
10191 if (bed
->common_definition (&sym
))
10192 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10196 case bfd_link_hash_undefined
:
10197 case bfd_link_hash_undefweak
:
10203 if (h
->forced_local
)
10205 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
10206 /* Turn off visibility on local symbol. */
10207 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
10209 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10210 else if (h
->unique_global
&& h
->def_regular
)
10211 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
10212 else if (h
->root
.type
== bfd_link_hash_undefweak
10213 || h
->root
.type
== bfd_link_hash_defweak
)
10214 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
10216 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
10217 sym
.st_target_internal
= h
->target_internal
;
10219 /* Give the processor backend a chance to tweak the symbol value,
10220 and also to finish up anything that needs to be done for this
10221 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10222 forced local syms when non-shared is due to a historical quirk.
10223 STT_GNU_IFUNC symbol must go through PLT. */
10224 if ((h
->type
== STT_GNU_IFUNC
10226 && !bfd_link_relocatable (flinfo
->info
))
10227 || ((h
->dynindx
!= -1
10228 || h
->forced_local
)
10229 && ((bfd_link_pic (flinfo
->info
)
10230 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
10231 || h
->root
.type
!= bfd_link_hash_undefweak
))
10232 || !h
->forced_local
)
10233 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
10235 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
10236 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
10238 eoinfo
->failed
= TRUE
;
10243 /* If we are marking the symbol as undefined, and there are no
10244 non-weak references to this symbol from a regular object, then
10245 mark the symbol as weak undefined; if there are non-weak
10246 references, mark the symbol as strong. We can't do this earlier,
10247 because it might not be marked as undefined until the
10248 finish_dynamic_symbol routine gets through with it. */
10249 if (sym
.st_shndx
== SHN_UNDEF
10251 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
10252 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
10255 type
= ELF_ST_TYPE (sym
.st_info
);
10257 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10258 if (type
== STT_GNU_IFUNC
)
10261 if (h
->ref_regular_nonweak
)
10262 bindtype
= STB_GLOBAL
;
10264 bindtype
= STB_WEAK
;
10265 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
10268 /* If this is a symbol defined in a dynamic library, don't use the
10269 symbol size from the dynamic library. Relinking an executable
10270 against a new library may introduce gratuitous changes in the
10271 executable's symbols if we keep the size. */
10272 if (sym
.st_shndx
== SHN_UNDEF
10277 /* If a non-weak symbol with non-default visibility is not defined
10278 locally, it is a fatal error. */
10279 if (!bfd_link_relocatable (flinfo
->info
)
10280 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
10281 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
10282 && h
->root
.type
== bfd_link_hash_undefined
10283 && !h
->def_regular
)
10287 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
10288 /* xgettext:c-format */
10289 msg
= _("%pB: protected symbol `%s' isn't defined");
10290 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
10291 /* xgettext:c-format */
10292 msg
= _("%pB: internal symbol `%s' isn't defined");
10294 /* xgettext:c-format */
10295 msg
= _("%pB: hidden symbol `%s' isn't defined");
10296 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
10297 bfd_set_error (bfd_error_bad_value
);
10298 eoinfo
->failed
= TRUE
;
10302 /* If this symbol should be put in the .dynsym section, then put it
10303 there now. We already know the symbol index. We also fill in
10304 the entry in the .hash section. */
10305 if (h
->dynindx
!= -1
10306 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
10307 && elf_hash_table (flinfo
->info
)->dynsym
!= NULL
10308 && !discarded_section (elf_hash_table (flinfo
->info
)->dynsym
))
10312 /* Since there is no version information in the dynamic string,
10313 if there is no version info in symbol version section, we will
10314 have a run-time problem if not linking executable, referenced
10315 by shared library, or not bound locally. */
10316 if (h
->verinfo
.verdef
== NULL
10317 && (!bfd_link_executable (flinfo
->info
)
10319 || !h
->def_regular
))
10321 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
10323 if (p
&& p
[1] != '\0')
10326 /* xgettext:c-format */
10327 (_("%pB: no symbol version section for versioned symbol `%s'"),
10328 flinfo
->output_bfd
, h
->root
.root
.string
);
10329 eoinfo
->failed
= TRUE
;
10334 sym
.st_name
= h
->dynstr_index
;
10335 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
10336 + h
->dynindx
* bed
->s
->sizeof_sym
);
10337 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
10339 eoinfo
->failed
= TRUE
;
10342 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
10344 if (flinfo
->hash_sec
!= NULL
)
10346 size_t hash_entry_size
;
10347 bfd_byte
*bucketpos
;
10349 size_t bucketcount
;
10352 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
10353 bucket
= h
->u
.elf_hash_value
% bucketcount
;
10356 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
10357 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
10358 + (bucket
+ 2) * hash_entry_size
);
10359 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
10360 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
10362 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
10363 ((bfd_byte
*) flinfo
->hash_sec
->contents
10364 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
10367 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
10369 Elf_Internal_Versym iversym
;
10370 Elf_External_Versym
*eversym
;
10372 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
10374 if (h
->verinfo
.verdef
== NULL
10375 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
10376 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
10377 iversym
.vs_vers
= 0;
10379 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
10383 if (h
->verinfo
.vertree
== NULL
)
10384 iversym
.vs_vers
= 1;
10386 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
10387 if (flinfo
->info
->create_default_symver
)
10391 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10392 defined locally. */
10393 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
10394 iversym
.vs_vers
|= VERSYM_HIDDEN
;
10396 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
10397 eversym
+= h
->dynindx
;
10398 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
10402 /* If the symbol is undefined, and we didn't output it to .dynsym,
10403 strip it from .symtab too. Obviously we can't do this for
10404 relocatable output or when needed for --emit-relocs. */
10405 else if (input_sec
== bfd_und_section_ptr
10407 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10408 && (h
->mark
!= 1 || ELF_ST_BIND (sym
.st_info
) != STB_GLOBAL
)
10409 && !bfd_link_relocatable (flinfo
->info
))
10412 /* Also strip others that we couldn't earlier due to dynamic symbol
10416 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
10419 /* Output a FILE symbol so that following locals are not associated
10420 with the wrong input file. We need one for forced local symbols
10421 if we've seen more than one FILE symbol or when we have exactly
10422 one FILE symbol but global symbols are present in a file other
10423 than the one with the FILE symbol. We also need one if linker
10424 defined symbols are present. In practice these conditions are
10425 always met, so just emit the FILE symbol unconditionally. */
10426 if (eoinfo
->localsyms
10427 && !eoinfo
->file_sym_done
10428 && eoinfo
->flinfo
->filesym_count
!= 0)
10430 Elf_Internal_Sym fsym
;
10432 memset (&fsym
, 0, sizeof (fsym
));
10433 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10434 fsym
.st_shndx
= SHN_ABS
;
10435 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
10436 bfd_und_section_ptr
, NULL
))
10439 eoinfo
->file_sym_done
= TRUE
;
10442 indx
= bfd_get_symcount (flinfo
->output_bfd
);
10443 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
10447 eoinfo
->failed
= TRUE
;
10452 else if (h
->indx
== -2)
10458 /* Return TRUE if special handling is done for relocs in SEC against
10459 symbols defined in discarded sections. */
10462 elf_section_ignore_discarded_relocs (asection
*sec
)
10464 const struct elf_backend_data
*bed
;
10466 switch (sec
->sec_info_type
)
10468 case SEC_INFO_TYPE_STABS
:
10469 case SEC_INFO_TYPE_EH_FRAME
:
10470 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10476 bed
= get_elf_backend_data (sec
->owner
);
10477 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
10478 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
10484 /* Return a mask saying how ld should treat relocations in SEC against
10485 symbols defined in discarded sections. If this function returns
10486 COMPLAIN set, ld will issue a warning message. If this function
10487 returns PRETEND set, and the discarded section was link-once and the
10488 same size as the kept link-once section, ld will pretend that the
10489 symbol was actually defined in the kept section. Otherwise ld will
10490 zero the reloc (at least that is the intent, but some cooperation by
10491 the target dependent code is needed, particularly for REL targets). */
10494 _bfd_elf_default_action_discarded (asection
*sec
)
10496 if (sec
->flags
& SEC_DEBUGGING
)
10499 if (strcmp (".eh_frame", sec
->name
) == 0)
10502 if (strcmp (".gcc_except_table", sec
->name
) == 0)
10505 return COMPLAIN
| PRETEND
;
10508 /* Find a match between a section and a member of a section group. */
10511 match_group_member (asection
*sec
, asection
*group
,
10512 struct bfd_link_info
*info
)
10514 asection
*first
= elf_next_in_group (group
);
10515 asection
*s
= first
;
10519 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
10522 s
= elf_next_in_group (s
);
10530 /* Check if the kept section of a discarded section SEC can be used
10531 to replace it. Return the replacement if it is OK. Otherwise return
10535 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
10539 kept
= sec
->kept_section
;
10542 if ((kept
->flags
& SEC_GROUP
) != 0)
10543 kept
= match_group_member (sec
, kept
, info
);
10545 && ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
10546 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
)))
10548 sec
->kept_section
= kept
;
10553 /* Link an input file into the linker output file. This function
10554 handles all the sections and relocations of the input file at once.
10555 This is so that we only have to read the local symbols once, and
10556 don't have to keep them in memory. */
10559 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
10561 int (*relocate_section
)
10562 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
10563 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
10565 Elf_Internal_Shdr
*symtab_hdr
;
10566 size_t locsymcount
;
10568 Elf_Internal_Sym
*isymbuf
;
10569 Elf_Internal_Sym
*isym
;
10570 Elf_Internal_Sym
*isymend
;
10572 asection
**ppsection
;
10574 const struct elf_backend_data
*bed
;
10575 struct elf_link_hash_entry
**sym_hashes
;
10576 bfd_size_type address_size
;
10577 bfd_vma r_type_mask
;
10579 bfd_boolean have_file_sym
= FALSE
;
10581 output_bfd
= flinfo
->output_bfd
;
10582 bed
= get_elf_backend_data (output_bfd
);
10583 relocate_section
= bed
->elf_backend_relocate_section
;
10585 /* If this is a dynamic object, we don't want to do anything here:
10586 we don't want the local symbols, and we don't want the section
10588 if ((input_bfd
->flags
& DYNAMIC
) != 0)
10591 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
10592 if (elf_bad_symtab (input_bfd
))
10594 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10599 locsymcount
= symtab_hdr
->sh_info
;
10600 extsymoff
= symtab_hdr
->sh_info
;
10603 /* Read the local symbols. */
10604 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10605 if (isymbuf
== NULL
&& locsymcount
!= 0)
10607 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
10608 flinfo
->internal_syms
,
10609 flinfo
->external_syms
,
10610 flinfo
->locsym_shndx
);
10611 if (isymbuf
== NULL
)
10615 /* Find local symbol sections and adjust values of symbols in
10616 SEC_MERGE sections. Write out those local symbols we know are
10617 going into the output file. */
10618 isymend
= isymbuf
+ locsymcount
;
10619 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
10621 isym
++, pindex
++, ppsection
++)
10625 Elf_Internal_Sym osym
;
10631 if (elf_bad_symtab (input_bfd
))
10633 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
10640 if (isym
->st_shndx
== SHN_UNDEF
)
10641 isec
= bfd_und_section_ptr
;
10642 else if (isym
->st_shndx
== SHN_ABS
)
10643 isec
= bfd_abs_section_ptr
;
10644 else if (isym
->st_shndx
== SHN_COMMON
)
10645 isec
= bfd_com_section_ptr
;
10648 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
10651 /* Don't attempt to output symbols with st_shnx in the
10652 reserved range other than SHN_ABS and SHN_COMMON. */
10653 isec
= bfd_und_section_ptr
;
10655 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
10656 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
10658 _bfd_merged_section_offset (output_bfd
, &isec
,
10659 elf_section_data (isec
)->sec_info
,
10665 /* Don't output the first, undefined, symbol. In fact, don't
10666 output any undefined local symbol. */
10667 if (isec
== bfd_und_section_ptr
)
10670 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
10672 /* We never output section symbols. Instead, we use the
10673 section symbol of the corresponding section in the output
10678 /* If we are stripping all symbols, we don't want to output this
10680 if (flinfo
->info
->strip
== strip_all
)
10683 /* If we are discarding all local symbols, we don't want to
10684 output this one. If we are generating a relocatable output
10685 file, then some of the local symbols may be required by
10686 relocs; we output them below as we discover that they are
10688 if (flinfo
->info
->discard
== discard_all
)
10691 /* If this symbol is defined in a section which we are
10692 discarding, we don't need to keep it. */
10693 if (isym
->st_shndx
!= SHN_UNDEF
10694 && isym
->st_shndx
< SHN_LORESERVE
10695 && isec
->output_section
== NULL
10696 && flinfo
->info
->non_contiguous_regions
10697 && flinfo
->info
->non_contiguous_regions_warnings
)
10699 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10700 "discards section `%s' from '%s'\n"),
10701 isec
->name
, bfd_get_filename (isec
->owner
));
10705 if (isym
->st_shndx
!= SHN_UNDEF
10706 && isym
->st_shndx
< SHN_LORESERVE
10707 && bfd_section_removed_from_list (output_bfd
,
10708 isec
->output_section
))
10711 /* Get the name of the symbol. */
10712 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
10717 /* See if we are discarding symbols with this name. */
10718 if ((flinfo
->info
->strip
== strip_some
10719 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
10721 || (((flinfo
->info
->discard
== discard_sec_merge
10722 && (isec
->flags
& SEC_MERGE
)
10723 && !bfd_link_relocatable (flinfo
->info
))
10724 || flinfo
->info
->discard
== discard_l
)
10725 && bfd_is_local_label_name (input_bfd
, name
)))
10728 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
10730 if (input_bfd
->lto_output
)
10731 /* -flto puts a temp file name here. This means builds
10732 are not reproducible. Discard the symbol. */
10734 have_file_sym
= TRUE
;
10735 flinfo
->filesym_count
+= 1;
10737 if (!have_file_sym
)
10739 /* In the absence of debug info, bfd_find_nearest_line uses
10740 FILE symbols to determine the source file for local
10741 function symbols. Provide a FILE symbol here if input
10742 files lack such, so that their symbols won't be
10743 associated with a previous input file. It's not the
10744 source file, but the best we can do. */
10745 have_file_sym
= TRUE
;
10746 flinfo
->filesym_count
+= 1;
10747 memset (&osym
, 0, sizeof (osym
));
10748 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10749 osym
.st_shndx
= SHN_ABS
;
10750 if (!elf_link_output_symstrtab (flinfo
,
10751 (input_bfd
->lto_output
? NULL
10752 : bfd_get_filename (input_bfd
)),
10753 &osym
, bfd_abs_section_ptr
,
10760 /* Adjust the section index for the output file. */
10761 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10762 isec
->output_section
);
10763 if (osym
.st_shndx
== SHN_BAD
)
10766 /* ELF symbols in relocatable files are section relative, but
10767 in executable files they are virtual addresses. Note that
10768 this code assumes that all ELF sections have an associated
10769 BFD section with a reasonable value for output_offset; below
10770 we assume that they also have a reasonable value for
10771 output_section. Any special sections must be set up to meet
10772 these requirements. */
10773 osym
.st_value
+= isec
->output_offset
;
10774 if (!bfd_link_relocatable (flinfo
->info
))
10776 osym
.st_value
+= isec
->output_section
->vma
;
10777 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
10779 /* STT_TLS symbols are relative to PT_TLS segment base. */
10780 if (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
)
10781 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
10783 osym
.st_info
= ELF_ST_INFO (ELF_ST_BIND (osym
.st_info
),
10788 indx
= bfd_get_symcount (output_bfd
);
10789 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
10796 if (bed
->s
->arch_size
== 32)
10798 r_type_mask
= 0xff;
10804 r_type_mask
= 0xffffffff;
10809 /* Relocate the contents of each section. */
10810 sym_hashes
= elf_sym_hashes (input_bfd
);
10811 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
10813 bfd_byte
*contents
;
10815 if (! o
->linker_mark
)
10817 /* This section was omitted from the link. */
10821 if (!flinfo
->info
->resolve_section_groups
10822 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
10824 /* Deal with the group signature symbol. */
10825 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
10826 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
10827 asection
*osec
= o
->output_section
;
10829 BFD_ASSERT (bfd_link_relocatable (flinfo
->info
));
10830 if (symndx
>= locsymcount
10831 || (elf_bad_symtab (input_bfd
)
10832 && flinfo
->sections
[symndx
] == NULL
))
10834 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
10835 while (h
->root
.type
== bfd_link_hash_indirect
10836 || h
->root
.type
== bfd_link_hash_warning
)
10837 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10838 /* Arrange for symbol to be output. */
10840 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
10842 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
10844 /* We'll use the output section target_index. */
10845 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10846 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
10850 if (flinfo
->indices
[symndx
] == -1)
10852 /* Otherwise output the local symbol now. */
10853 Elf_Internal_Sym sym
= isymbuf
[symndx
];
10854 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10859 name
= bfd_elf_string_from_elf_section (input_bfd
,
10860 symtab_hdr
->sh_link
,
10865 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10867 if (sym
.st_shndx
== SHN_BAD
)
10870 sym
.st_value
+= o
->output_offset
;
10872 indx
= bfd_get_symcount (output_bfd
);
10873 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
10878 flinfo
->indices
[symndx
] = indx
;
10882 elf_section_data (osec
)->this_hdr
.sh_info
10883 = flinfo
->indices
[symndx
];
10887 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
10888 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
10891 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
10893 /* Section was created by _bfd_elf_link_create_dynamic_sections
10898 /* Get the contents of the section. They have been cached by a
10899 relaxation routine. Note that o is a section in an input
10900 file, so the contents field will not have been set by any of
10901 the routines which work on output files. */
10902 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
10904 contents
= elf_section_data (o
)->this_hdr
.contents
;
10905 if (bed
->caches_rawsize
10907 && o
->rawsize
< o
->size
)
10909 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
10910 contents
= flinfo
->contents
;
10915 contents
= flinfo
->contents
;
10916 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
10920 if ((o
->flags
& SEC_RELOC
) != 0)
10922 Elf_Internal_Rela
*internal_relocs
;
10923 Elf_Internal_Rela
*rel
, *relend
;
10924 int action_discarded
;
10927 /* Get the swapped relocs. */
10929 = _bfd_elf_link_read_relocs (input_bfd
, o
, flinfo
->external_relocs
,
10930 flinfo
->internal_relocs
, FALSE
);
10931 if (internal_relocs
== NULL
10932 && o
->reloc_count
> 0)
10935 /* We need to reverse-copy input .ctors/.dtors sections if
10936 they are placed in .init_array/.finit_array for output. */
10937 if (o
->size
> address_size
10938 && ((strncmp (o
->name
, ".ctors", 6) == 0
10939 && strcmp (o
->output_section
->name
,
10940 ".init_array") == 0)
10941 || (strncmp (o
->name
, ".dtors", 6) == 0
10942 && strcmp (o
->output_section
->name
,
10943 ".fini_array") == 0))
10944 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
10946 if (o
->size
* bed
->s
->int_rels_per_ext_rel
10947 != o
->reloc_count
* address_size
)
10950 /* xgettext:c-format */
10951 (_("error: %pB: size of section %pA is not "
10952 "multiple of address size"),
10954 bfd_set_error (bfd_error_bad_value
);
10957 o
->flags
|= SEC_ELF_REVERSE_COPY
;
10960 action_discarded
= -1;
10961 if (!elf_section_ignore_discarded_relocs (o
))
10962 action_discarded
= (*bed
->action_discarded
) (o
);
10964 /* Run through the relocs evaluating complex reloc symbols and
10965 looking for relocs against symbols from discarded sections
10966 or section symbols from removed link-once sections.
10967 Complain about relocs against discarded sections. Zero
10968 relocs against removed link-once sections. */
10970 rel
= internal_relocs
;
10971 relend
= rel
+ o
->reloc_count
;
10972 for ( ; rel
< relend
; rel
++)
10974 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
10975 unsigned int s_type
;
10976 asection
**ps
, *sec
;
10977 struct elf_link_hash_entry
*h
= NULL
;
10978 const char *sym_name
;
10980 if (r_symndx
== STN_UNDEF
)
10983 if (r_symndx
>= locsymcount
10984 || (elf_bad_symtab (input_bfd
)
10985 && flinfo
->sections
[r_symndx
] == NULL
))
10987 h
= sym_hashes
[r_symndx
- extsymoff
];
10989 /* Badly formatted input files can contain relocs that
10990 reference non-existant symbols. Check here so that
10991 we do not seg fault. */
10995 /* xgettext:c-format */
10996 (_("error: %pB contains a reloc (%#" PRIx64
") for section %pA "
10997 "that references a non-existent global symbol"),
10998 input_bfd
, (uint64_t) rel
->r_info
, o
);
10999 bfd_set_error (bfd_error_bad_value
);
11003 while (h
->root
.type
== bfd_link_hash_indirect
11004 || h
->root
.type
== bfd_link_hash_warning
)
11005 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11009 /* If a plugin symbol is referenced from a non-IR file,
11010 mark the symbol as undefined. Note that the
11011 linker may attach linker created dynamic sections
11012 to the plugin bfd. Symbols defined in linker
11013 created sections are not plugin symbols. */
11014 if ((h
->root
.non_ir_ref_regular
11015 || h
->root
.non_ir_ref_dynamic
)
11016 && (h
->root
.type
== bfd_link_hash_defined
11017 || h
->root
.type
== bfd_link_hash_defweak
)
11018 && (h
->root
.u
.def
.section
->flags
11019 & SEC_LINKER_CREATED
) == 0
11020 && h
->root
.u
.def
.section
->owner
!= NULL
11021 && (h
->root
.u
.def
.section
->owner
->flags
11022 & BFD_PLUGIN
) != 0)
11024 h
->root
.type
= bfd_link_hash_undefined
;
11025 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
11029 if (h
->root
.type
== bfd_link_hash_defined
11030 || h
->root
.type
== bfd_link_hash_defweak
)
11031 ps
= &h
->root
.u
.def
.section
;
11033 sym_name
= h
->root
.root
.string
;
11037 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
11039 s_type
= ELF_ST_TYPE (sym
->st_info
);
11040 ps
= &flinfo
->sections
[r_symndx
];
11041 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
11045 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
11046 && !bfd_link_relocatable (flinfo
->info
))
11049 bfd_vma dot
= (rel
->r_offset
11050 + o
->output_offset
+ o
->output_section
->vma
);
11052 printf ("Encountered a complex symbol!");
11053 printf (" (input_bfd %s, section %s, reloc %ld\n",
11054 bfd_get_filename (input_bfd
), o
->name
,
11055 (long) (rel
- internal_relocs
));
11056 printf (" symbol: idx %8.8lx, name %s\n",
11057 r_symndx
, sym_name
);
11058 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11059 (unsigned long) rel
->r_info
,
11060 (unsigned long) rel
->r_offset
);
11062 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
11063 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
11066 /* Symbol evaluated OK. Update to absolute value. */
11067 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
11072 if (action_discarded
!= -1 && ps
!= NULL
)
11074 /* Complain if the definition comes from a
11075 discarded section. */
11076 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
11078 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
11079 if (action_discarded
& COMPLAIN
)
11080 (*flinfo
->info
->callbacks
->einfo
)
11081 /* xgettext:c-format */
11082 (_("%X`%s' referenced in section `%pA' of %pB: "
11083 "defined in discarded section `%pA' of %pB\n"),
11084 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
11086 /* Try to do the best we can to support buggy old
11087 versions of gcc. Pretend that the symbol is
11088 really defined in the kept linkonce section.
11089 FIXME: This is quite broken. Modifying the
11090 symbol here means we will be changing all later
11091 uses of the symbol, not just in this section. */
11092 if (action_discarded
& PRETEND
)
11096 kept
= _bfd_elf_check_kept_section (sec
,
11108 /* Relocate the section by invoking a back end routine.
11110 The back end routine is responsible for adjusting the
11111 section contents as necessary, and (if using Rela relocs
11112 and generating a relocatable output file) adjusting the
11113 reloc addend as necessary.
11115 The back end routine does not have to worry about setting
11116 the reloc address or the reloc symbol index.
11118 The back end routine is given a pointer to the swapped in
11119 internal symbols, and can access the hash table entries
11120 for the external symbols via elf_sym_hashes (input_bfd).
11122 When generating relocatable output, the back end routine
11123 must handle STB_LOCAL/STT_SECTION symbols specially. The
11124 output symbol is going to be a section symbol
11125 corresponding to the output section, which will require
11126 the addend to be adjusted. */
11128 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
11129 input_bfd
, o
, contents
,
11137 || bfd_link_relocatable (flinfo
->info
)
11138 || flinfo
->info
->emitrelocations
)
11140 Elf_Internal_Rela
*irela
;
11141 Elf_Internal_Rela
*irelaend
, *irelamid
;
11142 bfd_vma last_offset
;
11143 struct elf_link_hash_entry
**rel_hash
;
11144 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
11145 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
11146 unsigned int next_erel
;
11147 bfd_boolean rela_normal
;
11148 struct bfd_elf_section_data
*esdi
, *esdo
;
11150 esdi
= elf_section_data (o
);
11151 esdo
= elf_section_data (o
->output_section
);
11152 rela_normal
= FALSE
;
11154 /* Adjust the reloc addresses and symbol indices. */
11156 irela
= internal_relocs
;
11157 irelaend
= irela
+ o
->reloc_count
;
11158 rel_hash
= esdo
->rel
.hashes
+ esdo
->rel
.count
;
11159 /* We start processing the REL relocs, if any. When we reach
11160 IRELAMID in the loop, we switch to the RELA relocs. */
11162 if (esdi
->rel
.hdr
!= NULL
)
11163 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
11164 * bed
->s
->int_rels_per_ext_rel
);
11165 rel_hash_list
= rel_hash
;
11166 rela_hash_list
= NULL
;
11167 last_offset
= o
->output_offset
;
11168 if (!bfd_link_relocatable (flinfo
->info
))
11169 last_offset
+= o
->output_section
->vma
;
11170 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
11172 unsigned long r_symndx
;
11174 Elf_Internal_Sym sym
;
11176 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
11182 if (irela
== irelamid
)
11184 rel_hash
= esdo
->rela
.hashes
+ esdo
->rela
.count
;
11185 rela_hash_list
= rel_hash
;
11186 rela_normal
= bed
->rela_normal
;
11189 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
11192 if (irela
->r_offset
>= (bfd_vma
) -2)
11194 /* This is a reloc for a deleted entry or somesuch.
11195 Turn it into an R_*_NONE reloc, at the same
11196 offset as the last reloc. elf_eh_frame.c and
11197 bfd_elf_discard_info rely on reloc offsets
11199 irela
->r_offset
= last_offset
;
11201 irela
->r_addend
= 0;
11205 irela
->r_offset
+= o
->output_offset
;
11207 /* Relocs in an executable have to be virtual addresses. */
11208 if (!bfd_link_relocatable (flinfo
->info
))
11209 irela
->r_offset
+= o
->output_section
->vma
;
11211 last_offset
= irela
->r_offset
;
11213 r_symndx
= irela
->r_info
>> r_sym_shift
;
11214 if (r_symndx
== STN_UNDEF
)
11217 if (r_symndx
>= locsymcount
11218 || (elf_bad_symtab (input_bfd
)
11219 && flinfo
->sections
[r_symndx
] == NULL
))
11221 struct elf_link_hash_entry
*rh
;
11222 unsigned long indx
;
11224 /* This is a reloc against a global symbol. We
11225 have not yet output all the local symbols, so
11226 we do not know the symbol index of any global
11227 symbol. We set the rel_hash entry for this
11228 reloc to point to the global hash table entry
11229 for this symbol. The symbol index is then
11230 set at the end of bfd_elf_final_link. */
11231 indx
= r_symndx
- extsymoff
;
11232 rh
= elf_sym_hashes (input_bfd
)[indx
];
11233 while (rh
->root
.type
== bfd_link_hash_indirect
11234 || rh
->root
.type
== bfd_link_hash_warning
)
11235 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
11237 /* Setting the index to -2 tells
11238 elf_link_output_extsym that this symbol is
11239 used by a reloc. */
11240 BFD_ASSERT (rh
->indx
< 0);
11247 /* This is a reloc against a local symbol. */
11250 sym
= isymbuf
[r_symndx
];
11251 sec
= flinfo
->sections
[r_symndx
];
11252 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
11254 /* I suppose the backend ought to fill in the
11255 section of any STT_SECTION symbol against a
11256 processor specific section. */
11257 r_symndx
= STN_UNDEF
;
11258 if (bfd_is_abs_section (sec
))
11260 else if (sec
== NULL
|| sec
->owner
== NULL
)
11262 bfd_set_error (bfd_error_bad_value
);
11267 asection
*osec
= sec
->output_section
;
11269 /* If we have discarded a section, the output
11270 section will be the absolute section. In
11271 case of discarded SEC_MERGE sections, use
11272 the kept section. relocate_section should
11273 have already handled discarded linkonce
11275 if (bfd_is_abs_section (osec
)
11276 && sec
->kept_section
!= NULL
11277 && sec
->kept_section
->output_section
!= NULL
)
11279 osec
= sec
->kept_section
->output_section
;
11280 irela
->r_addend
-= osec
->vma
;
11283 if (!bfd_is_abs_section (osec
))
11285 r_symndx
= osec
->target_index
;
11286 if (r_symndx
== STN_UNDEF
)
11288 irela
->r_addend
+= osec
->vma
;
11289 osec
= _bfd_nearby_section (output_bfd
, osec
,
11291 irela
->r_addend
-= osec
->vma
;
11292 r_symndx
= osec
->target_index
;
11297 /* Adjust the addend according to where the
11298 section winds up in the output section. */
11300 irela
->r_addend
+= sec
->output_offset
;
11304 if (flinfo
->indices
[r_symndx
] == -1)
11306 unsigned long shlink
;
11311 if (flinfo
->info
->strip
== strip_all
)
11313 /* You can't do ld -r -s. */
11314 bfd_set_error (bfd_error_invalid_operation
);
11318 /* This symbol was skipped earlier, but
11319 since it is needed by a reloc, we
11320 must output it now. */
11321 shlink
= symtab_hdr
->sh_link
;
11322 name
= (bfd_elf_string_from_elf_section
11323 (input_bfd
, shlink
, sym
.st_name
));
11327 osec
= sec
->output_section
;
11329 _bfd_elf_section_from_bfd_section (output_bfd
,
11331 if (sym
.st_shndx
== SHN_BAD
)
11334 sym
.st_value
+= sec
->output_offset
;
11335 if (!bfd_link_relocatable (flinfo
->info
))
11337 sym
.st_value
+= osec
->vma
;
11338 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
11340 struct elf_link_hash_table
*htab
11341 = elf_hash_table (flinfo
->info
);
11343 /* STT_TLS symbols are relative to PT_TLS
11345 if (htab
->tls_sec
!= NULL
)
11346 sym
.st_value
-= htab
->tls_sec
->vma
;
11349 = ELF_ST_INFO (ELF_ST_BIND (sym
.st_info
),
11354 indx
= bfd_get_symcount (output_bfd
);
11355 ret
= elf_link_output_symstrtab (flinfo
, name
,
11361 flinfo
->indices
[r_symndx
] = indx
;
11366 r_symndx
= flinfo
->indices
[r_symndx
];
11369 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
11370 | (irela
->r_info
& r_type_mask
));
11373 /* Swap out the relocs. */
11374 input_rel_hdr
= esdi
->rel
.hdr
;
11375 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
11377 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11382 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
11383 * bed
->s
->int_rels_per_ext_rel
);
11384 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
11387 input_rela_hdr
= esdi
->rela
.hdr
;
11388 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
11390 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11399 /* Write out the modified section contents. */
11400 if (bed
->elf_backend_write_section
11401 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
11404 /* Section written out. */
11406 else switch (o
->sec_info_type
)
11408 case SEC_INFO_TYPE_STABS
:
11409 if (! (_bfd_write_section_stabs
11411 &elf_hash_table (flinfo
->info
)->stab_info
,
11412 o
, &elf_section_data (o
)->sec_info
, contents
)))
11415 case SEC_INFO_TYPE_MERGE
:
11416 if (! _bfd_write_merged_section (output_bfd
, o
,
11417 elf_section_data (o
)->sec_info
))
11420 case SEC_INFO_TYPE_EH_FRAME
:
11422 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
11427 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
11429 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
11437 if (! (o
->flags
& SEC_EXCLUDE
))
11439 file_ptr offset
= (file_ptr
) o
->output_offset
;
11440 bfd_size_type todo
= o
->size
;
11442 offset
*= bfd_octets_per_byte (output_bfd
, o
);
11444 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
11446 /* Reverse-copy input section to output. */
11449 todo
-= address_size
;
11450 if (! bfd_set_section_contents (output_bfd
,
11458 offset
+= address_size
;
11462 else if (! bfd_set_section_contents (output_bfd
,
11476 /* Generate a reloc when linking an ELF file. This is a reloc
11477 requested by the linker, and does not come from any input file. This
11478 is used to build constructor and destructor tables when linking
11482 elf_reloc_link_order (bfd
*output_bfd
,
11483 struct bfd_link_info
*info
,
11484 asection
*output_section
,
11485 struct bfd_link_order
*link_order
)
11487 reloc_howto_type
*howto
;
11491 struct bfd_elf_section_reloc_data
*reldata
;
11492 struct elf_link_hash_entry
**rel_hash_ptr
;
11493 Elf_Internal_Shdr
*rel_hdr
;
11494 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
11495 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
11498 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
11500 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
11503 bfd_set_error (bfd_error_bad_value
);
11507 addend
= link_order
->u
.reloc
.p
->addend
;
11510 reldata
= &esdo
->rel
;
11511 else if (esdo
->rela
.hdr
)
11512 reldata
= &esdo
->rela
;
11519 /* Figure out the symbol index. */
11520 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
11521 if (link_order
->type
== bfd_section_reloc_link_order
)
11523 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
11524 BFD_ASSERT (indx
!= 0);
11525 *rel_hash_ptr
= NULL
;
11529 struct elf_link_hash_entry
*h
;
11531 /* Treat a reloc against a defined symbol as though it were
11532 actually against the section. */
11533 h
= ((struct elf_link_hash_entry
*)
11534 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
11535 link_order
->u
.reloc
.p
->u
.name
,
11536 FALSE
, FALSE
, TRUE
));
11538 && (h
->root
.type
== bfd_link_hash_defined
11539 || h
->root
.type
== bfd_link_hash_defweak
))
11543 section
= h
->root
.u
.def
.section
;
11544 indx
= section
->output_section
->target_index
;
11545 *rel_hash_ptr
= NULL
;
11546 /* It seems that we ought to add the symbol value to the
11547 addend here, but in practice it has already been added
11548 because it was passed to constructor_callback. */
11549 addend
+= section
->output_section
->vma
+ section
->output_offset
;
11551 else if (h
!= NULL
)
11553 /* Setting the index to -2 tells elf_link_output_extsym that
11554 this symbol is used by a reloc. */
11561 (*info
->callbacks
->unattached_reloc
)
11562 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
11567 /* If this is an inplace reloc, we must write the addend into the
11569 if (howto
->partial_inplace
&& addend
!= 0)
11571 bfd_size_type size
;
11572 bfd_reloc_status_type rstat
;
11575 const char *sym_name
;
11576 bfd_size_type octets
;
11578 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
11579 buf
= (bfd_byte
*) bfd_zmalloc (size
);
11580 if (buf
== NULL
&& size
!= 0)
11582 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
11589 case bfd_reloc_outofrange
:
11592 case bfd_reloc_overflow
:
11593 if (link_order
->type
== bfd_section_reloc_link_order
)
11594 sym_name
= bfd_section_name (link_order
->u
.reloc
.p
->u
.section
);
11596 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
11597 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
11598 howto
->name
, addend
, NULL
, NULL
,
11603 octets
= link_order
->offset
* bfd_octets_per_byte (output_bfd
,
11605 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
11612 /* The address of a reloc is relative to the section in a
11613 relocatable file, and is a virtual address in an executable
11615 offset
= link_order
->offset
;
11616 if (! bfd_link_relocatable (info
))
11617 offset
+= output_section
->vma
;
11619 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
11621 irel
[i
].r_offset
= offset
;
11622 irel
[i
].r_info
= 0;
11623 irel
[i
].r_addend
= 0;
11625 if (bed
->s
->arch_size
== 32)
11626 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
11628 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
11630 rel_hdr
= reldata
->hdr
;
11631 erel
= rel_hdr
->contents
;
11632 if (rel_hdr
->sh_type
== SHT_REL
)
11634 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
11635 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
11639 irel
[0].r_addend
= addend
;
11640 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
11641 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
11650 /* Compare two sections based on the locations of the sections they are
11651 linked to. Used by elf_fixup_link_order. */
11654 compare_link_order (const void *a
, const void *b
)
11656 const struct bfd_link_order
*alo
= *(const struct bfd_link_order
**) a
;
11657 const struct bfd_link_order
*blo
= *(const struct bfd_link_order
**) b
;
11658 asection
*asec
= elf_linked_to_section (alo
->u
.indirect
.section
);
11659 asection
*bsec
= elf_linked_to_section (blo
->u
.indirect
.section
);
11660 bfd_vma apos
= asec
->output_section
->lma
+ asec
->output_offset
;
11661 bfd_vma bpos
= bsec
->output_section
->lma
+ bsec
->output_offset
;
11668 /* The only way we should get matching LMAs is when the first of two
11669 sections has zero size. */
11670 if (asec
->size
< bsec
->size
)
11672 if (asec
->size
> bsec
->size
)
11675 /* If they are both zero size then they almost certainly have the same
11676 VMA and thus are not ordered with respect to each other. Test VMA
11677 anyway, and fall back to id to make the result reproducible across
11678 qsort implementations. */
11679 apos
= asec
->output_section
->vma
+ asec
->output_offset
;
11680 bpos
= bsec
->output_section
->vma
+ bsec
->output_offset
;
11686 return asec
->id
- bsec
->id
;
11690 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11691 order as their linked sections. Returns false if this could not be done
11692 because an output section includes both ordered and unordered
11693 sections. Ideally we'd do this in the linker proper. */
11696 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
11698 size_t seen_linkorder
;
11701 struct bfd_link_order
*p
;
11703 struct bfd_link_order
**sections
;
11704 asection
*other_sec
, *linkorder_sec
;
11705 bfd_vma offset
; /* Octets. */
11708 linkorder_sec
= NULL
;
11710 seen_linkorder
= 0;
11711 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11713 if (p
->type
== bfd_indirect_link_order
)
11715 asection
*s
= p
->u
.indirect
.section
;
11717 if ((s
->flags
& SEC_LINKER_CREATED
) == 0
11718 && bfd_get_flavour (sub
) == bfd_target_elf_flavour
11719 && elf_section_data (s
) != NULL
11720 && elf_linked_to_section (s
) != NULL
)
11734 if (seen_other
&& seen_linkorder
)
11736 if (other_sec
&& linkorder_sec
)
11738 /* xgettext:c-format */
11739 (_("%pA has both ordered [`%pA' in %pB] "
11740 "and unordered [`%pA' in %pB] sections"),
11741 o
, linkorder_sec
, linkorder_sec
->owner
,
11742 other_sec
, other_sec
->owner
);
11745 (_("%pA has both ordered and unordered sections"), o
);
11746 bfd_set_error (bfd_error_bad_value
);
11751 if (!seen_linkorder
)
11754 sections
= bfd_malloc (seen_linkorder
* sizeof (*sections
));
11755 if (sections
== NULL
)
11758 seen_linkorder
= 0;
11759 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11760 sections
[seen_linkorder
++] = p
;
11762 /* Sort the input sections in the order of their linked section. */
11763 qsort (sections
, seen_linkorder
, sizeof (*sections
), compare_link_order
);
11765 /* Change the offsets of the sections. */
11767 for (n
= 0; n
< seen_linkorder
; n
++)
11770 asection
*s
= sections
[n
]->u
.indirect
.section
;
11771 unsigned int opb
= bfd_octets_per_byte (abfd
, s
);
11773 mask
= ~(bfd_vma
) 0 << s
->alignment_power
* opb
;
11774 offset
= (offset
+ ~mask
) & mask
;
11775 sections
[n
]->offset
= s
->output_offset
= offset
/ opb
;
11776 offset
+= sections
[n
]->size
;
11783 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11784 Returns TRUE upon success, FALSE otherwise. */
11787 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
11789 bfd_boolean ret
= FALSE
;
11791 const struct elf_backend_data
*bed
;
11793 enum bfd_architecture arch
;
11795 asymbol
**sympp
= NULL
;
11799 elf_symbol_type
*osymbuf
;
11802 implib_bfd
= info
->out_implib_bfd
;
11803 bed
= get_elf_backend_data (abfd
);
11805 if (!bfd_set_format (implib_bfd
, bfd_object
))
11808 /* Use flag from executable but make it a relocatable object. */
11809 flags
= bfd_get_file_flags (abfd
);
11810 flags
&= ~HAS_RELOC
;
11811 if (!bfd_set_start_address (implib_bfd
, 0)
11812 || !bfd_set_file_flags (implib_bfd
, flags
& ~EXEC_P
))
11815 /* Copy architecture of output file to import library file. */
11816 arch
= bfd_get_arch (abfd
);
11817 mach
= bfd_get_mach (abfd
);
11818 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
11819 && (abfd
->target_defaulted
11820 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
11823 /* Get symbol table size. */
11824 symsize
= bfd_get_symtab_upper_bound (abfd
);
11828 /* Read in the symbol table. */
11829 sympp
= (asymbol
**) bfd_malloc (symsize
);
11833 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
11837 /* Allow the BFD backend to copy any private header data it
11838 understands from the output BFD to the import library BFD. */
11839 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
11842 /* Filter symbols to appear in the import library. */
11843 if (bed
->elf_backend_filter_implib_symbols
)
11844 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
11847 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
11850 bfd_set_error (bfd_error_no_symbols
);
11851 _bfd_error_handler (_("%pB: no symbol found for import library"),
11857 /* Make symbols absolute. */
11858 amt
= symcount
* sizeof (*osymbuf
);
11859 osymbuf
= (elf_symbol_type
*) bfd_alloc (implib_bfd
, amt
);
11860 if (osymbuf
== NULL
)
11863 for (src_count
= 0; src_count
< symcount
; src_count
++)
11865 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
11866 sizeof (*osymbuf
));
11867 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
11868 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
11869 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
11870 osymbuf
[src_count
].internal_elf_sym
.st_value
=
11871 osymbuf
[src_count
].symbol
.value
;
11872 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
11875 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
11877 /* Allow the BFD backend to copy any private data it understands
11878 from the output BFD to the import library BFD. This is done last
11879 to permit the routine to look at the filtered symbol table. */
11880 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
11883 if (!bfd_close (implib_bfd
))
11894 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
11898 if (flinfo
->symstrtab
!= NULL
)
11899 _bfd_elf_strtab_free (flinfo
->symstrtab
);
11900 free (flinfo
->contents
);
11901 free (flinfo
->external_relocs
);
11902 free (flinfo
->internal_relocs
);
11903 free (flinfo
->external_syms
);
11904 free (flinfo
->locsym_shndx
);
11905 free (flinfo
->internal_syms
);
11906 free (flinfo
->indices
);
11907 free (flinfo
->sections
);
11908 if (flinfo
->symshndxbuf
!= (Elf_External_Sym_Shndx
*) -1)
11909 free (flinfo
->symshndxbuf
);
11910 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
11912 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11913 free (esdo
->rel
.hashes
);
11914 free (esdo
->rela
.hashes
);
11918 /* Do the final step of an ELF link. */
11921 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
11923 bfd_boolean dynamic
;
11924 bfd_boolean emit_relocs
;
11926 struct elf_final_link_info flinfo
;
11928 struct bfd_link_order
*p
;
11930 bfd_size_type max_contents_size
;
11931 bfd_size_type max_external_reloc_size
;
11932 bfd_size_type max_internal_reloc_count
;
11933 bfd_size_type max_sym_count
;
11934 bfd_size_type max_sym_shndx_count
;
11935 Elf_Internal_Sym elfsym
;
11937 Elf_Internal_Shdr
*symtab_hdr
;
11938 Elf_Internal_Shdr
*symtab_shndx_hdr
;
11939 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11940 struct elf_outext_info eoinfo
;
11941 bfd_boolean merged
;
11942 size_t relativecount
= 0;
11943 asection
*reldyn
= 0;
11945 asection
*attr_section
= NULL
;
11946 bfd_vma attr_size
= 0;
11947 const char *std_attrs_section
;
11948 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
11949 bfd_boolean sections_removed
;
11951 if (!is_elf_hash_table (htab
))
11954 if (bfd_link_pic (info
))
11955 abfd
->flags
|= DYNAMIC
;
11957 dynamic
= htab
->dynamic_sections_created
;
11958 dynobj
= htab
->dynobj
;
11960 emit_relocs
= (bfd_link_relocatable (info
)
11961 || info
->emitrelocations
);
11963 flinfo
.info
= info
;
11964 flinfo
.output_bfd
= abfd
;
11965 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
11966 if (flinfo
.symstrtab
== NULL
)
11971 flinfo
.hash_sec
= NULL
;
11972 flinfo
.symver_sec
= NULL
;
11976 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
11977 /* Note that dynsym_sec can be NULL (on VMS). */
11978 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
11979 /* Note that it is OK if symver_sec is NULL. */
11982 flinfo
.contents
= NULL
;
11983 flinfo
.external_relocs
= NULL
;
11984 flinfo
.internal_relocs
= NULL
;
11985 flinfo
.external_syms
= NULL
;
11986 flinfo
.locsym_shndx
= NULL
;
11987 flinfo
.internal_syms
= NULL
;
11988 flinfo
.indices
= NULL
;
11989 flinfo
.sections
= NULL
;
11990 flinfo
.symshndxbuf
= NULL
;
11991 flinfo
.filesym_count
= 0;
11993 /* The object attributes have been merged. Remove the input
11994 sections from the link, and set the contents of the output
11996 sections_removed
= FALSE
;
11997 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
11998 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12000 bfd_boolean remove_section
= FALSE
;
12002 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
12003 || strcmp (o
->name
, ".gnu.attributes") == 0)
12005 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12007 asection
*input_section
;
12009 if (p
->type
!= bfd_indirect_link_order
)
12011 input_section
= p
->u
.indirect
.section
;
12012 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12013 elf_link_input_bfd ignores this section. */
12014 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
12017 attr_size
= bfd_elf_obj_attr_size (abfd
);
12018 bfd_set_section_size (o
, attr_size
);
12019 /* Skip this section later on. */
12020 o
->map_head
.link_order
= NULL
;
12024 remove_section
= TRUE
;
12026 else if ((o
->flags
& SEC_GROUP
) != 0 && o
->size
== 0)
12028 /* Remove empty group section from linker output. */
12029 remove_section
= TRUE
;
12031 if (remove_section
)
12033 o
->flags
|= SEC_EXCLUDE
;
12034 bfd_section_list_remove (abfd
, o
);
12035 abfd
->section_count
--;
12036 sections_removed
= TRUE
;
12039 if (sections_removed
)
12040 _bfd_fix_excluded_sec_syms (abfd
, info
);
12042 /* Count up the number of relocations we will output for each output
12043 section, so that we know the sizes of the reloc sections. We
12044 also figure out some maximum sizes. */
12045 max_contents_size
= 0;
12046 max_external_reloc_size
= 0;
12047 max_internal_reloc_count
= 0;
12049 max_sym_shndx_count
= 0;
12051 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12053 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12054 o
->reloc_count
= 0;
12056 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12058 unsigned int reloc_count
= 0;
12059 unsigned int additional_reloc_count
= 0;
12060 struct bfd_elf_section_data
*esdi
= NULL
;
12062 if (p
->type
== bfd_section_reloc_link_order
12063 || p
->type
== bfd_symbol_reloc_link_order
)
12065 else if (p
->type
== bfd_indirect_link_order
)
12069 sec
= p
->u
.indirect
.section
;
12071 /* Mark all sections which are to be included in the
12072 link. This will normally be every section. We need
12073 to do this so that we can identify any sections which
12074 the linker has decided to not include. */
12075 sec
->linker_mark
= TRUE
;
12077 if (sec
->flags
& SEC_MERGE
)
12080 if (sec
->rawsize
> max_contents_size
)
12081 max_contents_size
= sec
->rawsize
;
12082 if (sec
->size
> max_contents_size
)
12083 max_contents_size
= sec
->size
;
12085 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
12086 && (sec
->owner
->flags
& DYNAMIC
) == 0)
12090 /* We are interested in just local symbols, not all
12092 if (elf_bad_symtab (sec
->owner
))
12093 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
12094 / bed
->s
->sizeof_sym
);
12096 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
12098 if (sym_count
> max_sym_count
)
12099 max_sym_count
= sym_count
;
12101 if (sym_count
> max_sym_shndx_count
12102 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
12103 max_sym_shndx_count
= sym_count
;
12105 if (esdo
->this_hdr
.sh_type
== SHT_REL
12106 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
12107 /* Some backends use reloc_count in relocation sections
12108 to count particular types of relocs. Of course,
12109 reloc sections themselves can't have relocations. */
12111 else if (emit_relocs
)
12113 reloc_count
= sec
->reloc_count
;
12114 if (bed
->elf_backend_count_additional_relocs
)
12117 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
12118 additional_reloc_count
+= c
;
12121 else if (bed
->elf_backend_count_relocs
)
12122 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
12124 esdi
= elf_section_data (sec
);
12126 if ((sec
->flags
& SEC_RELOC
) != 0)
12128 size_t ext_size
= 0;
12130 if (esdi
->rel
.hdr
!= NULL
)
12131 ext_size
= esdi
->rel
.hdr
->sh_size
;
12132 if (esdi
->rela
.hdr
!= NULL
)
12133 ext_size
+= esdi
->rela
.hdr
->sh_size
;
12135 if (ext_size
> max_external_reloc_size
)
12136 max_external_reloc_size
= ext_size
;
12137 if (sec
->reloc_count
> max_internal_reloc_count
)
12138 max_internal_reloc_count
= sec
->reloc_count
;
12143 if (reloc_count
== 0)
12146 reloc_count
+= additional_reloc_count
;
12147 o
->reloc_count
+= reloc_count
;
12149 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
12153 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
12154 esdo
->rel
.count
+= additional_reloc_count
;
12156 if (esdi
->rela
.hdr
)
12158 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
12159 esdo
->rela
.count
+= additional_reloc_count
;
12165 esdo
->rela
.count
+= reloc_count
;
12167 esdo
->rel
.count
+= reloc_count
;
12171 if (o
->reloc_count
> 0)
12172 o
->flags
|= SEC_RELOC
;
12175 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12176 set it (this is probably a bug) and if it is set
12177 assign_section_numbers will create a reloc section. */
12178 o
->flags
&=~ SEC_RELOC
;
12181 /* If the SEC_ALLOC flag is not set, force the section VMA to
12182 zero. This is done in elf_fake_sections as well, but forcing
12183 the VMA to 0 here will ensure that relocs against these
12184 sections are handled correctly. */
12185 if ((o
->flags
& SEC_ALLOC
) == 0
12186 && ! o
->user_set_vma
)
12190 if (! bfd_link_relocatable (info
) && merged
)
12191 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
12193 /* Figure out the file positions for everything but the symbol table
12194 and the relocs. We set symcount to force assign_section_numbers
12195 to create a symbol table. */
12196 abfd
->symcount
= info
->strip
!= strip_all
|| emit_relocs
;
12197 BFD_ASSERT (! abfd
->output_has_begun
);
12198 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
12201 /* Set sizes, and assign file positions for reloc sections. */
12202 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12204 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12205 if ((o
->flags
& SEC_RELOC
) != 0)
12208 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
12212 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
12216 /* _bfd_elf_compute_section_file_positions makes temporary use
12217 of target_index. Reset it. */
12218 o
->target_index
= 0;
12220 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12221 to count upwards while actually outputting the relocations. */
12222 esdo
->rel
.count
= 0;
12223 esdo
->rela
.count
= 0;
12225 if ((esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
12226 && !bfd_section_is_ctf (o
))
12228 /* Cache the section contents so that they can be compressed
12229 later. Use bfd_malloc since it will be freed by
12230 bfd_compress_section_contents. */
12231 unsigned char *contents
= esdo
->this_hdr
.contents
;
12232 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
12235 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
12236 if (contents
== NULL
)
12238 esdo
->this_hdr
.contents
= contents
;
12242 /* We have now assigned file positions for all the sections except .symtab,
12243 .strtab, and non-loaded reloc and compressed debugging sections. We start
12244 the .symtab section at the current file position, and write directly to it.
12245 We build the .strtab section in memory. */
12246 abfd
->symcount
= 0;
12247 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12248 /* sh_name is set in prep_headers. */
12249 symtab_hdr
->sh_type
= SHT_SYMTAB
;
12250 /* sh_flags, sh_addr and sh_size all start off zero. */
12251 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
12252 /* sh_link is set in assign_section_numbers. */
12253 /* sh_info is set below. */
12254 /* sh_offset is set just below. */
12255 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
12257 if (max_sym_count
< 20)
12258 max_sym_count
= 20;
12259 htab
->strtabsize
= max_sym_count
;
12260 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
12261 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
12262 if (htab
->strtab
== NULL
)
12264 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12266 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
12267 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
12269 if (info
->strip
!= strip_all
|| emit_relocs
)
12271 file_ptr off
= elf_next_file_pos (abfd
);
12273 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
12275 /* Note that at this point elf_next_file_pos (abfd) is
12276 incorrect. We do not yet know the size of the .symtab section.
12277 We correct next_file_pos below, after we do know the size. */
12279 /* Start writing out the symbol table. The first symbol is always a
12281 elfsym
.st_value
= 0;
12282 elfsym
.st_size
= 0;
12283 elfsym
.st_info
= 0;
12284 elfsym
.st_other
= 0;
12285 elfsym
.st_shndx
= SHN_UNDEF
;
12286 elfsym
.st_target_internal
= 0;
12287 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
12288 bfd_und_section_ptr
, NULL
) != 1)
12291 /* Output a symbol for each section. We output these even if we are
12292 discarding local symbols, since they are used for relocs. These
12293 symbols have no names. We store the index of each one in the
12294 index field of the section, so that we can find it again when
12295 outputting relocs. */
12297 elfsym
.st_size
= 0;
12298 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12299 elfsym
.st_other
= 0;
12300 elfsym
.st_value
= 0;
12301 elfsym
.st_target_internal
= 0;
12302 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12304 o
= bfd_section_from_elf_index (abfd
, i
);
12307 o
->target_index
= bfd_get_symcount (abfd
);
12308 elfsym
.st_shndx
= i
;
12309 if (!bfd_link_relocatable (info
))
12310 elfsym
.st_value
= o
->vma
;
12311 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
, o
,
12318 /* Allocate some memory to hold information read in from the input
12320 if (max_contents_size
!= 0)
12322 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
12323 if (flinfo
.contents
== NULL
)
12327 if (max_external_reloc_size
!= 0)
12329 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
12330 if (flinfo
.external_relocs
== NULL
)
12334 if (max_internal_reloc_count
!= 0)
12336 amt
= max_internal_reloc_count
* sizeof (Elf_Internal_Rela
);
12337 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
12338 if (flinfo
.internal_relocs
== NULL
)
12342 if (max_sym_count
!= 0)
12344 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
12345 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
12346 if (flinfo
.external_syms
== NULL
)
12349 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
12350 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
12351 if (flinfo
.internal_syms
== NULL
)
12354 amt
= max_sym_count
* sizeof (long);
12355 flinfo
.indices
= (long int *) bfd_malloc (amt
);
12356 if (flinfo
.indices
== NULL
)
12359 amt
= max_sym_count
* sizeof (asection
*);
12360 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
12361 if (flinfo
.sections
== NULL
)
12365 if (max_sym_shndx_count
!= 0)
12367 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
12368 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
12369 if (flinfo
.locsym_shndx
== NULL
)
12375 bfd_vma base
, end
= 0; /* Both bytes. */
12378 for (sec
= htab
->tls_sec
;
12379 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
12382 bfd_size_type size
= sec
->size
;
12383 unsigned int opb
= bfd_octets_per_byte (abfd
, sec
);
12386 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
12388 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
12391 size
= ord
->offset
* opb
+ ord
->size
;
12393 end
= sec
->vma
+ size
/ opb
;
12395 base
= htab
->tls_sec
->vma
;
12396 /* Only align end of TLS section if static TLS doesn't have special
12397 alignment requirements. */
12398 if (bed
->static_tls_alignment
== 1)
12399 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
12400 htab
->tls_size
= end
- base
;
12403 /* Reorder SHF_LINK_ORDER sections. */
12404 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12406 if (!elf_fixup_link_order (abfd
, o
))
12410 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
12413 /* Since ELF permits relocations to be against local symbols, we
12414 must have the local symbols available when we do the relocations.
12415 Since we would rather only read the local symbols once, and we
12416 would rather not keep them in memory, we handle all the
12417 relocations for a single input file at the same time.
12419 Unfortunately, there is no way to know the total number of local
12420 symbols until we have seen all of them, and the local symbol
12421 indices precede the global symbol indices. This means that when
12422 we are generating relocatable output, and we see a reloc against
12423 a global symbol, we can not know the symbol index until we have
12424 finished examining all the local symbols to see which ones we are
12425 going to output. To deal with this, we keep the relocations in
12426 memory, and don't output them until the end of the link. This is
12427 an unfortunate waste of memory, but I don't see a good way around
12428 it. Fortunately, it only happens when performing a relocatable
12429 link, which is not the common case. FIXME: If keep_memory is set
12430 we could write the relocs out and then read them again; I don't
12431 know how bad the memory loss will be. */
12433 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12434 sub
->output_has_begun
= FALSE
;
12435 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12437 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12439 if (p
->type
== bfd_indirect_link_order
12440 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
12441 == bfd_target_elf_flavour
)
12442 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
12444 if (! sub
->output_has_begun
)
12446 if (! elf_link_input_bfd (&flinfo
, sub
))
12448 sub
->output_has_begun
= TRUE
;
12451 else if (p
->type
== bfd_section_reloc_link_order
12452 || p
->type
== bfd_symbol_reloc_link_order
)
12454 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
12459 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
12461 if (p
->type
== bfd_indirect_link_order
12462 && (bfd_get_flavour (sub
)
12463 == bfd_target_elf_flavour
)
12464 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
12465 != bed
->s
->elfclass
))
12467 const char *iclass
, *oclass
;
12469 switch (bed
->s
->elfclass
)
12471 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
12472 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
12473 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
12477 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
12479 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
12480 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
12481 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
12485 bfd_set_error (bfd_error_wrong_format
);
12487 /* xgettext:c-format */
12488 (_("%pB: file class %s incompatible with %s"),
12489 sub
, iclass
, oclass
);
12498 /* Free symbol buffer if needed. */
12499 if (!info
->reduce_memory_overheads
)
12501 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12502 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
12504 free (elf_tdata (sub
)->symbuf
);
12505 elf_tdata (sub
)->symbuf
= NULL
;
12509 /* Output any global symbols that got converted to local in a
12510 version script or due to symbol visibility. We do this in a
12511 separate step since ELF requires all local symbols to appear
12512 prior to any global symbols. FIXME: We should only do this if
12513 some global symbols were, in fact, converted to become local.
12514 FIXME: Will this work correctly with the Irix 5 linker? */
12515 eoinfo
.failed
= FALSE
;
12516 eoinfo
.flinfo
= &flinfo
;
12517 eoinfo
.localsyms
= TRUE
;
12518 eoinfo
.file_sym_done
= FALSE
;
12519 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12523 /* If backend needs to output some local symbols not present in the hash
12524 table, do it now. */
12525 if (bed
->elf_backend_output_arch_local_syms
12526 && (info
->strip
!= strip_all
|| emit_relocs
))
12528 typedef int (*out_sym_func
)
12529 (void *, const char *, Elf_Internal_Sym
*, asection
*,
12530 struct elf_link_hash_entry
*);
12532 if (! ((*bed
->elf_backend_output_arch_local_syms
)
12533 (abfd
, info
, &flinfo
,
12534 (out_sym_func
) elf_link_output_symstrtab
)))
12538 /* That wrote out all the local symbols. Finish up the symbol table
12539 with the global symbols. Even if we want to strip everything we
12540 can, we still need to deal with those global symbols that got
12541 converted to local in a version script. */
12543 /* The sh_info field records the index of the first non local symbol. */
12544 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12547 && htab
->dynsym
!= NULL
12548 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
12550 Elf_Internal_Sym sym
;
12551 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
12553 o
= htab
->dynsym
->output_section
;
12554 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
12556 /* Write out the section symbols for the output sections. */
12557 if (bfd_link_pic (info
)
12558 || htab
->is_relocatable_executable
)
12564 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12566 sym
.st_target_internal
= 0;
12568 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
12574 dynindx
= elf_section_data (s
)->dynindx
;
12577 indx
= elf_section_data (s
)->this_idx
;
12578 BFD_ASSERT (indx
> 0);
12579 sym
.st_shndx
= indx
;
12580 if (! check_dynsym (abfd
, &sym
))
12582 sym
.st_value
= s
->vma
;
12583 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
12584 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12588 /* Write out the local dynsyms. */
12589 if (htab
->dynlocal
)
12591 struct elf_link_local_dynamic_entry
*e
;
12592 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
12597 /* Copy the internal symbol and turn off visibility.
12598 Note that we saved a word of storage and overwrote
12599 the original st_name with the dynstr_index. */
12601 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
12603 s
= bfd_section_from_elf_index (e
->input_bfd
,
12608 elf_section_data (s
->output_section
)->this_idx
;
12609 if (! check_dynsym (abfd
, &sym
))
12611 sym
.st_value
= (s
->output_section
->vma
12613 + e
->isym
.st_value
);
12616 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
12617 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12622 /* We get the global symbols from the hash table. */
12623 eoinfo
.failed
= FALSE
;
12624 eoinfo
.localsyms
= FALSE
;
12625 eoinfo
.flinfo
= &flinfo
;
12626 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12630 /* If backend needs to output some symbols not present in the hash
12631 table, do it now. */
12632 if (bed
->elf_backend_output_arch_syms
12633 && (info
->strip
!= strip_all
|| emit_relocs
))
12635 typedef int (*out_sym_func
)
12636 (void *, const char *, Elf_Internal_Sym
*, asection
*,
12637 struct elf_link_hash_entry
*);
12639 if (! ((*bed
->elf_backend_output_arch_syms
)
12640 (abfd
, info
, &flinfo
,
12641 (out_sym_func
) elf_link_output_symstrtab
)))
12645 /* Finalize the .strtab section. */
12646 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
12648 /* Swap out the .strtab section. */
12649 if (!elf_link_swap_symbols_out (&flinfo
))
12652 /* Now we know the size of the symtab section. */
12653 if (bfd_get_symcount (abfd
) > 0)
12655 /* Finish up and write out the symbol string table (.strtab)
12657 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
12658 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
12660 if (elf_symtab_shndx_list (abfd
))
12662 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
12664 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
12666 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
12667 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
12668 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
12669 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
12670 symtab_shndx_hdr
->sh_size
= amt
;
12672 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
12675 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
12676 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
12681 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
12682 /* sh_name was set in prep_headers. */
12683 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
12684 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
12685 symstrtab_hdr
->sh_addr
= 0;
12686 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
12687 symstrtab_hdr
->sh_entsize
= 0;
12688 symstrtab_hdr
->sh_link
= 0;
12689 symstrtab_hdr
->sh_info
= 0;
12690 /* sh_offset is set just below. */
12691 symstrtab_hdr
->sh_addralign
= 1;
12693 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
12695 elf_next_file_pos (abfd
) = off
;
12697 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
12698 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
12702 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
12704 _bfd_error_handler (_("%pB: failed to generate import library"),
12705 info
->out_implib_bfd
);
12709 /* Adjust the relocs to have the correct symbol indices. */
12710 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12712 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12715 if ((o
->flags
& SEC_RELOC
) == 0)
12718 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
12719 if (esdo
->rel
.hdr
!= NULL
12720 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
, info
))
12722 if (esdo
->rela
.hdr
!= NULL
12723 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
, info
))
12726 /* Set the reloc_count field to 0 to prevent write_relocs from
12727 trying to swap the relocs out itself. */
12728 o
->reloc_count
= 0;
12731 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
12732 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
12734 /* If we are linking against a dynamic object, or generating a
12735 shared library, finish up the dynamic linking information. */
12738 bfd_byte
*dyncon
, *dynconend
;
12740 /* Fix up .dynamic entries. */
12741 o
= bfd_get_linker_section (dynobj
, ".dynamic");
12742 BFD_ASSERT (o
!= NULL
);
12744 dyncon
= o
->contents
;
12745 dynconend
= o
->contents
+ o
->size
;
12746 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12748 Elf_Internal_Dyn dyn
;
12751 bfd_size_type sh_size
;
12754 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12761 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
12763 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
12765 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
12766 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
12769 dyn
.d_un
.d_val
= relativecount
;
12776 name
= info
->init_function
;
12779 name
= info
->fini_function
;
12782 struct elf_link_hash_entry
*h
;
12784 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
12786 && (h
->root
.type
== bfd_link_hash_defined
12787 || h
->root
.type
== bfd_link_hash_defweak
))
12789 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
12790 o
= h
->root
.u
.def
.section
;
12791 if (o
->output_section
!= NULL
)
12792 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
12793 + o
->output_offset
);
12796 /* The symbol is imported from another shared
12797 library and does not apply to this one. */
12798 dyn
.d_un
.d_ptr
= 0;
12805 case DT_PREINIT_ARRAYSZ
:
12806 name
= ".preinit_array";
12808 case DT_INIT_ARRAYSZ
:
12809 name
= ".init_array";
12811 case DT_FINI_ARRAYSZ
:
12812 name
= ".fini_array";
12814 o
= bfd_get_section_by_name (abfd
, name
);
12818 (_("could not find section %s"), name
);
12823 (_("warning: %s section has zero size"), name
);
12824 dyn
.d_un
.d_val
= o
->size
;
12827 case DT_PREINIT_ARRAY
:
12828 name
= ".preinit_array";
12830 case DT_INIT_ARRAY
:
12831 name
= ".init_array";
12833 case DT_FINI_ARRAY
:
12834 name
= ".fini_array";
12836 o
= bfd_get_section_by_name (abfd
, name
);
12843 name
= ".gnu.hash";
12852 name
= ".gnu.version_d";
12855 name
= ".gnu.version_r";
12858 name
= ".gnu.version";
12860 o
= bfd_get_linker_section (dynobj
, name
);
12862 if (o
== NULL
|| bfd_is_abs_section (o
->output_section
))
12865 (_("could not find section %s"), name
);
12868 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
12871 (_("warning: section '%s' is being made into a note"), name
);
12872 bfd_set_error (bfd_error_nonrepresentable_section
);
12875 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
12882 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
12888 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12890 Elf_Internal_Shdr
*hdr
;
12892 hdr
= elf_elfsections (abfd
)[i
];
12893 if (hdr
->sh_type
== type
12894 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
12896 sh_size
+= hdr
->sh_size
;
12898 || sh_addr
> hdr
->sh_addr
)
12899 sh_addr
= hdr
->sh_addr
;
12903 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
12905 unsigned int opb
= bfd_octets_per_byte (abfd
, o
);
12907 /* Don't count procedure linkage table relocs in the
12908 overall reloc count. */
12909 sh_size
-= htab
->srelplt
->size
;
12911 /* If the size is zero, make the address zero too.
12912 This is to avoid a glibc bug. If the backend
12913 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12914 zero, then we'll put DT_RELA at the end of
12915 DT_JMPREL. glibc will interpret the end of
12916 DT_RELA matching the end of DT_JMPREL as the
12917 case where DT_RELA includes DT_JMPREL, and for
12918 LD_BIND_NOW will decide that processing DT_RELA
12919 will process the PLT relocs too. Net result:
12920 No PLT relocs applied. */
12923 /* If .rela.plt is the first .rela section, exclude
12924 it from DT_RELA. */
12925 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
12926 + htab
->srelplt
->output_offset
) * opb
)
12927 sh_addr
+= htab
->srelplt
->size
;
12930 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
12931 dyn
.d_un
.d_val
= sh_size
;
12933 dyn
.d_un
.d_ptr
= sh_addr
;
12936 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
12940 /* If we have created any dynamic sections, then output them. */
12941 if (dynobj
!= NULL
)
12943 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
12946 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12947 if (bfd_link_textrel_check (info
)
12948 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
)
12950 bfd_byte
*dyncon
, *dynconend
;
12952 dyncon
= o
->contents
;
12953 dynconend
= o
->contents
+ o
->size
;
12954 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12956 Elf_Internal_Dyn dyn
;
12958 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12960 if (dyn
.d_tag
== DT_TEXTREL
)
12962 if (info
->textrel_check
== textrel_check_error
)
12963 info
->callbacks
->einfo
12964 (_("%P%X: read-only segment has dynamic relocations\n"));
12965 else if (bfd_link_dll (info
))
12966 info
->callbacks
->einfo
12967 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
12969 info
->callbacks
->einfo
12970 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
12976 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
12978 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
12980 || o
->output_section
== bfd_abs_section_ptr
)
12982 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
12984 /* At this point, we are only interested in sections
12985 created by _bfd_elf_link_create_dynamic_sections. */
12988 if (htab
->stab_info
.stabstr
== o
)
12990 if (htab
->eh_info
.hdr_sec
== o
)
12992 if (strcmp (o
->name
, ".dynstr") != 0)
12994 bfd_size_type octets
= ((file_ptr
) o
->output_offset
12995 * bfd_octets_per_byte (abfd
, o
));
12996 if (!bfd_set_section_contents (abfd
, o
->output_section
,
12997 o
->contents
, octets
, o
->size
))
13002 /* The contents of the .dynstr section are actually in a
13006 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
13007 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
13008 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
13014 if (!info
->resolve_section_groups
)
13016 bfd_boolean failed
= FALSE
;
13018 BFD_ASSERT (bfd_link_relocatable (info
));
13019 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
13024 /* If we have optimized stabs strings, output them. */
13025 if (htab
->stab_info
.stabstr
!= NULL
)
13027 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
13031 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
13034 if (info
->callbacks
->emit_ctf
)
13035 info
->callbacks
->emit_ctf ();
13037 elf_final_link_free (abfd
, &flinfo
);
13041 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
13042 if (contents
== NULL
)
13043 return FALSE
; /* Bail out and fail. */
13044 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
13045 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
13052 elf_final_link_free (abfd
, &flinfo
);
13056 /* Initialize COOKIE for input bfd ABFD. */
13059 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
13060 struct bfd_link_info
*info
, bfd
*abfd
)
13062 Elf_Internal_Shdr
*symtab_hdr
;
13063 const struct elf_backend_data
*bed
;
13065 bed
= get_elf_backend_data (abfd
);
13066 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13068 cookie
->abfd
= abfd
;
13069 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
13070 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
13071 if (cookie
->bad_symtab
)
13073 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13074 cookie
->extsymoff
= 0;
13078 cookie
->locsymcount
= symtab_hdr
->sh_info
;
13079 cookie
->extsymoff
= symtab_hdr
->sh_info
;
13082 if (bed
->s
->arch_size
== 32)
13083 cookie
->r_sym_shift
= 8;
13085 cookie
->r_sym_shift
= 32;
13087 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
13088 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
13090 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
13091 cookie
->locsymcount
, 0,
13093 if (cookie
->locsyms
== NULL
)
13095 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
13098 if (info
->keep_memory
)
13099 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
13104 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13107 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
13109 Elf_Internal_Shdr
*symtab_hdr
;
13111 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13112 if (symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
13113 free (cookie
->locsyms
);
13116 /* Initialize the relocation information in COOKIE for input section SEC
13117 of input bfd ABFD. */
13120 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13121 struct bfd_link_info
*info
, bfd
*abfd
,
13124 if (sec
->reloc_count
== 0)
13126 cookie
->rels
= NULL
;
13127 cookie
->relend
= NULL
;
13131 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
13132 info
->keep_memory
);
13133 if (cookie
->rels
== NULL
)
13135 cookie
->rel
= cookie
->rels
;
13136 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
13138 cookie
->rel
= cookie
->rels
;
13142 /* Free the memory allocated by init_reloc_cookie_rels,
13146 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13149 if (elf_section_data (sec
)->relocs
!= cookie
->rels
)
13150 free (cookie
->rels
);
13153 /* Initialize the whole of COOKIE for input section SEC. */
13156 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13157 struct bfd_link_info
*info
,
13160 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
13162 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
13167 fini_reloc_cookie (cookie
, sec
->owner
);
13172 /* Free the memory allocated by init_reloc_cookie_for_section,
13176 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13179 fini_reloc_cookie_rels (cookie
, sec
);
13180 fini_reloc_cookie (cookie
, sec
->owner
);
13183 /* Garbage collect unused sections. */
13185 /* Default gc_mark_hook. */
13188 _bfd_elf_gc_mark_hook (asection
*sec
,
13189 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13190 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13191 struct elf_link_hash_entry
*h
,
13192 Elf_Internal_Sym
*sym
)
13196 switch (h
->root
.type
)
13198 case bfd_link_hash_defined
:
13199 case bfd_link_hash_defweak
:
13200 return h
->root
.u
.def
.section
;
13202 case bfd_link_hash_common
:
13203 return h
->root
.u
.c
.p
->section
;
13210 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
13215 /* Return the debug definition section. */
13218 elf_gc_mark_debug_section (asection
*sec ATTRIBUTE_UNUSED
,
13219 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13220 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13221 struct elf_link_hash_entry
*h
,
13222 Elf_Internal_Sym
*sym
)
13226 /* Return the global debug definition section. */
13227 if ((h
->root
.type
== bfd_link_hash_defined
13228 || h
->root
.type
== bfd_link_hash_defweak
)
13229 && (h
->root
.u
.def
.section
->flags
& SEC_DEBUGGING
) != 0)
13230 return h
->root
.u
.def
.section
;
13234 /* Return the local debug definition section. */
13235 asection
*isec
= bfd_section_from_elf_index (sec
->owner
,
13237 if ((isec
->flags
& SEC_DEBUGGING
) != 0)
13244 /* COOKIE->rel describes a relocation against section SEC, which is
13245 a section we've decided to keep. Return the section that contains
13246 the relocation symbol, or NULL if no section contains it. */
13249 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
13250 elf_gc_mark_hook_fn gc_mark_hook
,
13251 struct elf_reloc_cookie
*cookie
,
13252 bfd_boolean
*start_stop
)
13254 unsigned long r_symndx
;
13255 struct elf_link_hash_entry
*h
, *hw
;
13257 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
13258 if (r_symndx
== STN_UNDEF
)
13261 if (r_symndx
>= cookie
->locsymcount
13262 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13264 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
13267 info
->callbacks
->einfo (_("%F%P: corrupt input: %pB\n"),
13271 while (h
->root
.type
== bfd_link_hash_indirect
13272 || h
->root
.type
== bfd_link_hash_warning
)
13273 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13275 /* Keep all aliases of the symbol too. If an object symbol
13276 needs to be copied into .dynbss then all of its aliases
13277 should be present as dynamic symbols, not just the one used
13278 on the copy relocation. */
13280 while (hw
->is_weakalias
)
13286 if (start_stop
!= NULL
)
13288 /* To work around a glibc bug, mark XXX input sections
13289 when there is a reference to __start_XXX or __stop_XXX
13293 asection
*s
= h
->u2
.start_stop_section
;
13294 *start_stop
= !s
->gc_mark
;
13299 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
13302 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
13303 &cookie
->locsyms
[r_symndx
]);
13306 /* COOKIE->rel describes a relocation against section SEC, which is
13307 a section we've decided to keep. Mark the section that contains
13308 the relocation symbol. */
13311 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
13313 elf_gc_mark_hook_fn gc_mark_hook
,
13314 struct elf_reloc_cookie
*cookie
)
13317 bfd_boolean start_stop
= FALSE
;
13319 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
13320 while (rsec
!= NULL
)
13322 if (!rsec
->gc_mark
)
13324 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
13325 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
13327 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
13332 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
13337 /* The mark phase of garbage collection. For a given section, mark
13338 it and any sections in this section's group, and all the sections
13339 which define symbols to which it refers. */
13342 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
13344 elf_gc_mark_hook_fn gc_mark_hook
)
13347 asection
*group_sec
, *eh_frame
;
13351 /* Mark all the sections in the group. */
13352 group_sec
= elf_section_data (sec
)->next_in_group
;
13353 if (group_sec
&& !group_sec
->gc_mark
)
13354 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
13357 /* Look through the section relocs. */
13359 eh_frame
= elf_eh_frame_section (sec
->owner
);
13360 if ((sec
->flags
& SEC_RELOC
) != 0
13361 && sec
->reloc_count
> 0
13362 && sec
!= eh_frame
)
13364 struct elf_reloc_cookie cookie
;
13366 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
13370 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
13371 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
13376 fini_reloc_cookie_for_section (&cookie
, sec
);
13380 if (ret
&& eh_frame
&& elf_fde_list (sec
))
13382 struct elf_reloc_cookie cookie
;
13384 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
13388 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
13389 gc_mark_hook
, &cookie
))
13391 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
13395 eh_frame
= elf_section_eh_frame_entry (sec
);
13396 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
13397 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
13403 /* Scan and mark sections in a special or debug section group. */
13406 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
13408 /* Point to first section of section group. */
13410 /* Used to iterate the section group. */
13413 bfd_boolean is_special_grp
= TRUE
;
13414 bfd_boolean is_debug_grp
= TRUE
;
13416 /* First scan to see if group contains any section other than debug
13417 and special section. */
13418 ssec
= msec
= elf_next_in_group (grp
);
13421 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
13422 is_debug_grp
= FALSE
;
13424 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
13425 is_special_grp
= FALSE
;
13427 msec
= elf_next_in_group (msec
);
13429 while (msec
!= ssec
);
13431 /* If this is a pure debug section group or pure special section group,
13432 keep all sections in this group. */
13433 if (is_debug_grp
|| is_special_grp
)
13438 msec
= elf_next_in_group (msec
);
13440 while (msec
!= ssec
);
13444 /* Keep debug and special sections. */
13447 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
13448 elf_gc_mark_hook_fn mark_hook
)
13452 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13455 bfd_boolean some_kept
;
13456 bfd_boolean debug_frag_seen
;
13457 bfd_boolean has_kept_debug_info
;
13459 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13461 isec
= ibfd
->sections
;
13462 if (isec
== NULL
|| isec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13465 /* Ensure all linker created sections are kept,
13466 see if any other section is already marked,
13467 and note if we have any fragmented debug sections. */
13468 debug_frag_seen
= some_kept
= has_kept_debug_info
= FALSE
;
13469 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13471 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
13473 else if (isec
->gc_mark
13474 && (isec
->flags
& SEC_ALLOC
) != 0
13475 && elf_section_type (isec
) != SHT_NOTE
)
13479 /* Since all sections, except for backend specific ones,
13480 have been garbage collected, call mark_hook on this
13481 section if any of its linked-to sections is marked. */
13482 asection
*linked_to_sec
= elf_linked_to_section (isec
);
13483 for (; linked_to_sec
!= NULL
;
13484 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
13485 if (linked_to_sec
->gc_mark
)
13487 if (!_bfd_elf_gc_mark (info
, isec
, mark_hook
))
13493 if (!debug_frag_seen
13494 && (isec
->flags
& SEC_DEBUGGING
)
13495 && CONST_STRNEQ (isec
->name
, ".debug_line."))
13496 debug_frag_seen
= TRUE
;
13497 else if (strcmp (bfd_section_name (isec
),
13498 "__patchable_function_entries") == 0
13499 && elf_linked_to_section (isec
) == NULL
)
13500 info
->callbacks
->einfo (_("%F%P: %pB(%pA): error: "
13501 "need linked-to section "
13502 "for --gc-sections\n"),
13503 isec
->owner
, isec
);
13506 /* If no non-note alloc section in this file will be kept, then
13507 we can toss out the debug and special sections. */
13511 /* Keep debug and special sections like .comment when they are
13512 not part of a group. Also keep section groups that contain
13513 just debug sections or special sections. NB: Sections with
13514 linked-to section has been handled above. */
13515 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13517 if ((isec
->flags
& SEC_GROUP
) != 0)
13518 _bfd_elf_gc_mark_debug_special_section_group (isec
);
13519 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
13520 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
13521 && elf_next_in_group (isec
) == NULL
13522 && elf_linked_to_section (isec
) == NULL
)
13524 if (isec
->gc_mark
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
13525 has_kept_debug_info
= TRUE
;
13528 /* Look for CODE sections which are going to be discarded,
13529 and find and discard any fragmented debug sections which
13530 are associated with that code section. */
13531 if (debug_frag_seen
)
13532 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13533 if ((isec
->flags
& SEC_CODE
) != 0
13534 && isec
->gc_mark
== 0)
13539 ilen
= strlen (isec
->name
);
13541 /* Association is determined by the name of the debug
13542 section containing the name of the code section as
13543 a suffix. For example .debug_line.text.foo is a
13544 debug section associated with .text.foo. */
13545 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
13549 if (dsec
->gc_mark
== 0
13550 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
13553 dlen
= strlen (dsec
->name
);
13556 && strncmp (dsec
->name
+ (dlen
- ilen
),
13557 isec
->name
, ilen
) == 0)
13562 /* Mark debug sections referenced by kept debug sections. */
13563 if (has_kept_debug_info
)
13564 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13566 && (isec
->flags
& SEC_DEBUGGING
) != 0)
13567 if (!_bfd_elf_gc_mark (info
, isec
,
13568 elf_gc_mark_debug_section
))
13575 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
13578 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13580 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13584 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13585 || elf_object_id (sub
) != elf_hash_table_id (elf_hash_table (info
))
13586 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13589 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13592 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13594 /* When any section in a section group is kept, we keep all
13595 sections in the section group. If the first member of
13596 the section group is excluded, we will also exclude the
13598 if (o
->flags
& SEC_GROUP
)
13600 asection
*first
= elf_next_in_group (o
);
13601 o
->gc_mark
= first
->gc_mark
;
13607 /* Skip sweeping sections already excluded. */
13608 if (o
->flags
& SEC_EXCLUDE
)
13611 /* Since this is early in the link process, it is simple
13612 to remove a section from the output. */
13613 o
->flags
|= SEC_EXCLUDE
;
13615 if (info
->print_gc_sections
&& o
->size
!= 0)
13616 /* xgettext:c-format */
13617 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13625 /* Propagate collected vtable information. This is called through
13626 elf_link_hash_traverse. */
13629 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
13631 /* Those that are not vtables. */
13633 || h
->u2
.vtable
== NULL
13634 || h
->u2
.vtable
->parent
== NULL
)
13637 /* Those vtables that do not have parents, we cannot merge. */
13638 if (h
->u2
.vtable
->parent
== (struct elf_link_hash_entry
*) -1)
13641 /* If we've already been done, exit. */
13642 if (h
->u2
.vtable
->used
&& h
->u2
.vtable
->used
[-1])
13645 /* Make sure the parent's table is up to date. */
13646 elf_gc_propagate_vtable_entries_used (h
->u2
.vtable
->parent
, okp
);
13648 if (h
->u2
.vtable
->used
== NULL
)
13650 /* None of this table's entries were referenced. Re-use the
13652 h
->u2
.vtable
->used
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13653 h
->u2
.vtable
->size
= h
->u2
.vtable
->parent
->u2
.vtable
->size
;
13658 bfd_boolean
*cu
, *pu
;
13660 /* Or the parent's entries into ours. */
13661 cu
= h
->u2
.vtable
->used
;
13663 pu
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13666 const struct elf_backend_data
*bed
;
13667 unsigned int log_file_align
;
13669 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
13670 log_file_align
= bed
->s
->log_file_align
;
13671 n
= h
->u2
.vtable
->parent
->u2
.vtable
->size
>> log_file_align
;
13686 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
13689 bfd_vma hstart
, hend
;
13690 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
13691 const struct elf_backend_data
*bed
;
13692 unsigned int log_file_align
;
13694 /* Take care of both those symbols that do not describe vtables as
13695 well as those that are not loaded. */
13697 || h
->u2
.vtable
== NULL
13698 || h
->u2
.vtable
->parent
== NULL
)
13701 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
13702 || h
->root
.type
== bfd_link_hash_defweak
);
13704 sec
= h
->root
.u
.def
.section
;
13705 hstart
= h
->root
.u
.def
.value
;
13706 hend
= hstart
+ h
->size
;
13708 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
13710 return *(bfd_boolean
*) okp
= FALSE
;
13711 bed
= get_elf_backend_data (sec
->owner
);
13712 log_file_align
= bed
->s
->log_file_align
;
13714 relend
= relstart
+ sec
->reloc_count
;
13716 for (rel
= relstart
; rel
< relend
; ++rel
)
13717 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
13719 /* If the entry is in use, do nothing. */
13720 if (h
->u2
.vtable
->used
13721 && (rel
->r_offset
- hstart
) < h
->u2
.vtable
->size
)
13723 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
13724 if (h
->u2
.vtable
->used
[entry
])
13727 /* Otherwise, kill it. */
13728 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
13734 /* Mark sections containing dynamically referenced symbols. When
13735 building shared libraries, we must assume that any visible symbol is
13739 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
13741 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
13742 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
13744 if ((h
->root
.type
== bfd_link_hash_defined
13745 || h
->root
.type
== bfd_link_hash_defweak
)
13746 && ((h
->ref_dynamic
&& !h
->forced_local
)
13747 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
13748 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
13749 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
13750 && (!bfd_link_executable (info
)
13751 || info
->gc_keep_exported
13752 || info
->export_dynamic
13755 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
13756 && (h
->versioned
>= versioned
13757 || !bfd_hide_sym_by_version (info
->version_info
,
13758 h
->root
.root
.string
)))))
13759 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13764 /* Keep all sections containing symbols undefined on the command-line,
13765 and the section containing the entry symbol. */
13768 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
13770 struct bfd_sym_chain
*sym
;
13772 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
13774 struct elf_link_hash_entry
*h
;
13776 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
13777 FALSE
, FALSE
, FALSE
);
13780 && (h
->root
.type
== bfd_link_hash_defined
13781 || h
->root
.type
== bfd_link_hash_defweak
)
13782 && !bfd_is_const_section (h
->root
.u
.def
.section
))
13783 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13788 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
13789 struct bfd_link_info
*info
)
13791 bfd
*ibfd
= info
->input_bfds
;
13793 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13796 struct elf_reloc_cookie cookie
;
13798 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13800 sec
= ibfd
->sections
;
13801 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13804 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
13807 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
13809 if (CONST_STRNEQ (bfd_section_name (sec
), ".eh_frame_entry")
13810 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
13812 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
13813 fini_reloc_cookie_rels (&cookie
, sec
);
13820 /* Do mark and sweep of unused sections. */
13823 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
13825 bfd_boolean ok
= TRUE
;
13827 elf_gc_mark_hook_fn gc_mark_hook
;
13828 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13829 struct elf_link_hash_table
*htab
;
13831 if (!bed
->can_gc_sections
13832 || !is_elf_hash_table (info
->hash
))
13834 _bfd_error_handler(_("warning: gc-sections option ignored"));
13838 bed
->gc_keep (info
);
13839 htab
= elf_hash_table (info
);
13841 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13842 at the .eh_frame section if we can mark the FDEs individually. */
13843 for (sub
= info
->input_bfds
;
13844 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
13845 sub
= sub
->link
.next
)
13848 struct elf_reloc_cookie cookie
;
13850 sec
= sub
->sections
;
13851 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13853 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
13854 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
13856 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
13857 if (elf_section_data (sec
)->sec_info
13858 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
13859 elf_eh_frame_section (sub
) = sec
;
13860 fini_reloc_cookie_for_section (&cookie
, sec
);
13861 sec
= bfd_get_next_section_by_name (NULL
, sec
);
13865 /* Apply transitive closure to the vtable entry usage info. */
13866 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
13870 /* Kill the vtable relocations that were not used. */
13871 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &ok
);
13875 /* Mark dynamically referenced symbols. */
13876 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
13877 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
13879 /* Grovel through relocs to find out who stays ... */
13880 gc_mark_hook
= bed
->gc_mark_hook
;
13881 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13885 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13886 || elf_object_id (sub
) != elf_hash_table_id (htab
)
13887 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13891 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13894 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13895 Also treat note sections as a root, if the section is not part
13896 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13897 well as FINI_ARRAY sections for ld -r. */
13898 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13900 && (o
->flags
& SEC_EXCLUDE
) == 0
13901 && ((o
->flags
& SEC_KEEP
) != 0
13902 || (bfd_link_relocatable (info
)
13903 && ((elf_section_data (o
)->this_hdr
.sh_type
13904 == SHT_PREINIT_ARRAY
)
13905 || (elf_section_data (o
)->this_hdr
.sh_type
13907 || (elf_section_data (o
)->this_hdr
.sh_type
13908 == SHT_FINI_ARRAY
)))
13909 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
13910 && elf_next_in_group (o
) == NULL
)))
13912 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
13917 /* Allow the backend to mark additional target specific sections. */
13918 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
13920 /* ... and mark SEC_EXCLUDE for those that go. */
13921 return elf_gc_sweep (abfd
, info
);
13924 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13927 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
13929 struct elf_link_hash_entry
*h
,
13932 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
13933 struct elf_link_hash_entry
**search
, *child
;
13934 size_t extsymcount
;
13935 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13937 /* The sh_info field of the symtab header tells us where the
13938 external symbols start. We don't care about the local symbols at
13940 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
13941 if (!elf_bad_symtab (abfd
))
13942 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
13944 sym_hashes
= elf_sym_hashes (abfd
);
13945 sym_hashes_end
= sym_hashes
+ extsymcount
;
13947 /* Hunt down the child symbol, which is in this section at the same
13948 offset as the relocation. */
13949 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
13951 if ((child
= *search
) != NULL
13952 && (child
->root
.type
== bfd_link_hash_defined
13953 || child
->root
.type
== bfd_link_hash_defweak
)
13954 && child
->root
.u
.def
.section
== sec
13955 && child
->root
.u
.def
.value
== offset
)
13959 /* xgettext:c-format */
13960 _bfd_error_handler (_("%pB: %pA+%#" PRIx64
": no symbol found for INHERIT"),
13961 abfd
, sec
, (uint64_t) offset
);
13962 bfd_set_error (bfd_error_invalid_operation
);
13966 if (!child
->u2
.vtable
)
13968 child
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
13969 bfd_zalloc (abfd
, sizeof (*child
->u2
.vtable
)));
13970 if (!child
->u2
.vtable
)
13975 /* This *should* only be the absolute section. It could potentially
13976 be that someone has defined a non-global vtable though, which
13977 would be bad. It isn't worth paging in the local symbols to be
13978 sure though; that case should simply be handled by the assembler. */
13980 child
->u2
.vtable
->parent
= (struct elf_link_hash_entry
*) -1;
13983 child
->u2
.vtable
->parent
= h
;
13988 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13991 bfd_elf_gc_record_vtentry (bfd
*abfd
, asection
*sec
,
13992 struct elf_link_hash_entry
*h
,
13995 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13996 unsigned int log_file_align
= bed
->s
->log_file_align
;
14000 /* xgettext:c-format */
14001 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14003 bfd_set_error (bfd_error_bad_value
);
14009 h
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14010 bfd_zalloc (abfd
, sizeof (*h
->u2
.vtable
)));
14015 if (addend
>= h
->u2
.vtable
->size
)
14017 size_t size
, bytes
, file_align
;
14018 bfd_boolean
*ptr
= h
->u2
.vtable
->used
;
14020 /* While the symbol is undefined, we have to be prepared to handle
14022 file_align
= 1 << log_file_align
;
14023 if (h
->root
.type
== bfd_link_hash_undefined
)
14024 size
= addend
+ file_align
;
14028 if (addend
>= size
)
14030 /* Oops! We've got a reference past the defined end of
14031 the table. This is probably a bug -- shall we warn? */
14032 size
= addend
+ file_align
;
14035 size
= (size
+ file_align
- 1) & -file_align
;
14037 /* Allocate one extra entry for use as a "done" flag for the
14038 consolidation pass. */
14039 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
14043 ptr
= (bfd_boolean
*) bfd_realloc (ptr
- 1, bytes
);
14049 oldbytes
= (((h
->u2
.vtable
->size
>> log_file_align
) + 1)
14050 * sizeof (bfd_boolean
));
14051 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
14055 ptr
= (bfd_boolean
*) bfd_zmalloc (bytes
);
14060 /* And arrange for that done flag to be at index -1. */
14061 h
->u2
.vtable
->used
= ptr
+ 1;
14062 h
->u2
.vtable
->size
= size
;
14065 h
->u2
.vtable
->used
[addend
>> log_file_align
] = TRUE
;
14070 /* Map an ELF section header flag to its corresponding string. */
14074 flagword flag_value
;
14075 } elf_flags_to_name_table
;
14077 static elf_flags_to_name_table elf_flags_to_names
[] =
14079 { "SHF_WRITE", SHF_WRITE
},
14080 { "SHF_ALLOC", SHF_ALLOC
},
14081 { "SHF_EXECINSTR", SHF_EXECINSTR
},
14082 { "SHF_MERGE", SHF_MERGE
},
14083 { "SHF_STRINGS", SHF_STRINGS
},
14084 { "SHF_INFO_LINK", SHF_INFO_LINK
},
14085 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
14086 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
14087 { "SHF_GROUP", SHF_GROUP
},
14088 { "SHF_TLS", SHF_TLS
},
14089 { "SHF_MASKOS", SHF_MASKOS
},
14090 { "SHF_EXCLUDE", SHF_EXCLUDE
},
14093 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14095 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
14096 struct flag_info
*flaginfo
,
14099 const bfd_vma sh_flags
= elf_section_flags (section
);
14101 if (!flaginfo
->flags_initialized
)
14103 bfd
*obfd
= info
->output_bfd
;
14104 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14105 struct flag_info_list
*tf
= flaginfo
->flag_list
;
14107 int without_hex
= 0;
14109 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
14112 flagword (*lookup
) (char *);
14114 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
14115 if (lookup
!= NULL
)
14117 flagword hexval
= (*lookup
) ((char *) tf
->name
);
14121 if (tf
->with
== with_flags
)
14122 with_hex
|= hexval
;
14123 else if (tf
->with
== without_flags
)
14124 without_hex
|= hexval
;
14129 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
14131 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
14133 if (tf
->with
== with_flags
)
14134 with_hex
|= elf_flags_to_names
[i
].flag_value
;
14135 else if (tf
->with
== without_flags
)
14136 without_hex
|= elf_flags_to_names
[i
].flag_value
;
14143 info
->callbacks
->einfo
14144 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
14148 flaginfo
->flags_initialized
= TRUE
;
14149 flaginfo
->only_with_flags
|= with_hex
;
14150 flaginfo
->not_with_flags
|= without_hex
;
14153 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
14156 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
14162 struct alloc_got_off_arg
{
14164 struct bfd_link_info
*info
;
14167 /* We need a special top-level link routine to convert got reference counts
14168 to real got offsets. */
14171 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
14173 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
14174 bfd
*obfd
= gofarg
->info
->output_bfd
;
14175 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14177 if (h
->got
.refcount
> 0)
14179 h
->got
.offset
= gofarg
->gotoff
;
14180 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
14183 h
->got
.offset
= (bfd_vma
) -1;
14188 /* And an accompanying bit to work out final got entry offsets once
14189 we're done. Should be called from final_link. */
14192 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
14193 struct bfd_link_info
*info
)
14196 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14198 struct alloc_got_off_arg gofarg
;
14200 BFD_ASSERT (abfd
== info
->output_bfd
);
14202 if (! is_elf_hash_table (info
->hash
))
14205 /* The GOT offset is relative to the .got section, but the GOT header is
14206 put into the .got.plt section, if the backend uses it. */
14207 if (bed
->want_got_plt
)
14210 gotoff
= bed
->got_header_size
;
14212 /* Do the local .got entries first. */
14213 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
14215 bfd_signed_vma
*local_got
;
14216 size_t j
, locsymcount
;
14217 Elf_Internal_Shdr
*symtab_hdr
;
14219 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
14222 local_got
= elf_local_got_refcounts (i
);
14226 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
14227 if (elf_bad_symtab (i
))
14228 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
14230 locsymcount
= symtab_hdr
->sh_info
;
14232 for (j
= 0; j
< locsymcount
; ++j
)
14234 if (local_got
[j
] > 0)
14236 local_got
[j
] = gotoff
;
14237 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
14240 local_got
[j
] = (bfd_vma
) -1;
14244 /* Then the global .got entries. .plt refcounts are handled by
14245 adjust_dynamic_symbol */
14246 gofarg
.gotoff
= gotoff
;
14247 gofarg
.info
= info
;
14248 elf_link_hash_traverse (elf_hash_table (info
),
14249 elf_gc_allocate_got_offsets
,
14254 /* Many folk need no more in the way of final link than this, once
14255 got entry reference counting is enabled. */
14258 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
14260 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
14263 /* Invoke the regular ELF backend linker to do all the work. */
14264 return bfd_elf_final_link (abfd
, info
);
14268 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
14270 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
14272 if (rcookie
->bad_symtab
)
14273 rcookie
->rel
= rcookie
->rels
;
14275 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
14277 unsigned long r_symndx
;
14279 if (! rcookie
->bad_symtab
)
14280 if (rcookie
->rel
->r_offset
> offset
)
14282 if (rcookie
->rel
->r_offset
!= offset
)
14285 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
14286 if (r_symndx
== STN_UNDEF
)
14289 if (r_symndx
>= rcookie
->locsymcount
14290 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
14292 struct elf_link_hash_entry
*h
;
14294 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
14296 while (h
->root
.type
== bfd_link_hash_indirect
14297 || h
->root
.type
== bfd_link_hash_warning
)
14298 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
14300 if ((h
->root
.type
== bfd_link_hash_defined
14301 || h
->root
.type
== bfd_link_hash_defweak
)
14302 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
14303 || h
->root
.u
.def
.section
->kept_section
!= NULL
14304 || discarded_section (h
->root
.u
.def
.section
)))
14309 /* It's not a relocation against a global symbol,
14310 but it could be a relocation against a local
14311 symbol for a discarded section. */
14313 Elf_Internal_Sym
*isym
;
14315 /* Need to: get the symbol; get the section. */
14316 isym
= &rcookie
->locsyms
[r_symndx
];
14317 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
14319 && (isec
->kept_section
!= NULL
14320 || discarded_section (isec
)))
14328 /* Discard unneeded references to discarded sections.
14329 Returns -1 on error, 1 if any section's size was changed, 0 if
14330 nothing changed. This function assumes that the relocations are in
14331 sorted order, which is true for all known assemblers. */
14334 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
14336 struct elf_reloc_cookie cookie
;
14341 if (info
->traditional_format
14342 || !is_elf_hash_table (info
->hash
))
14345 o
= bfd_get_section_by_name (output_bfd
, ".stab");
14350 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14353 || i
->reloc_count
== 0
14354 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
14358 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14361 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14364 if (_bfd_discard_section_stabs (abfd
, i
,
14365 elf_section_data (i
)->sec_info
,
14366 bfd_elf_reloc_symbol_deleted_p
,
14370 fini_reloc_cookie_for_section (&cookie
, i
);
14375 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
14376 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
14380 int eh_changed
= 0;
14381 unsigned int eh_alignment
; /* Octets. */
14383 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14389 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14392 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14395 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
14396 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
14397 bfd_elf_reloc_symbol_deleted_p
,
14401 if (i
->size
!= i
->rawsize
)
14405 fini_reloc_cookie_for_section (&cookie
, i
);
14408 eh_alignment
= ((1 << o
->alignment_power
)
14409 * bfd_octets_per_byte (output_bfd
, o
));
14410 /* Skip over zero terminator, and prevent empty sections from
14411 adding alignment padding at the end. */
14412 for (i
= o
->map_tail
.s
; i
!= NULL
; i
= i
->map_tail
.s
)
14414 i
->flags
|= SEC_EXCLUDE
;
14415 else if (i
->size
> 4)
14417 /* The last non-empty eh_frame section doesn't need padding. */
14420 /* Any prior sections must pad the last FDE out to the output
14421 section alignment. Otherwise we might have zero padding
14422 between sections, which would be seen as a terminator. */
14423 for (; i
!= NULL
; i
= i
->map_tail
.s
)
14425 /* All but the last zero terminator should have been removed. */
14430 = (i
->size
+ eh_alignment
- 1) & -eh_alignment
;
14431 if (i
->size
!= size
)
14439 elf_link_hash_traverse (elf_hash_table (info
),
14440 _bfd_elf_adjust_eh_frame_global_symbol
, NULL
);
14443 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
14445 const struct elf_backend_data
*bed
;
14448 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14450 s
= abfd
->sections
;
14451 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14454 bed
= get_elf_backend_data (abfd
);
14456 if (bed
->elf_backend_discard_info
!= NULL
)
14458 if (!init_reloc_cookie (&cookie
, info
, abfd
))
14461 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
14464 fini_reloc_cookie (&cookie
, abfd
);
14468 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
14469 _bfd_elf_end_eh_frame_parsing (info
);
14471 if (info
->eh_frame_hdr_type
14472 && !bfd_link_relocatable (info
)
14473 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
14480 _bfd_elf_section_already_linked (bfd
*abfd
,
14482 struct bfd_link_info
*info
)
14485 const char *name
, *key
;
14486 struct bfd_section_already_linked
*l
;
14487 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
14489 if (sec
->output_section
== bfd_abs_section_ptr
)
14492 flags
= sec
->flags
;
14494 /* Return if it isn't a linkonce section. A comdat group section
14495 also has SEC_LINK_ONCE set. */
14496 if ((flags
& SEC_LINK_ONCE
) == 0)
14499 /* Don't put group member sections on our list of already linked
14500 sections. They are handled as a group via their group section. */
14501 if (elf_sec_group (sec
) != NULL
)
14504 /* For a SHT_GROUP section, use the group signature as the key. */
14506 if ((flags
& SEC_GROUP
) != 0
14507 && elf_next_in_group (sec
) != NULL
14508 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
14509 key
= elf_group_name (elf_next_in_group (sec
));
14512 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14513 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
14514 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
14517 /* Must be a user linkonce section that doesn't follow gcc's
14518 naming convention. In this case we won't be matching
14519 single member groups. */
14523 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
14525 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14527 /* We may have 2 different types of sections on the list: group
14528 sections with a signature of <key> (<key> is some string),
14529 and linkonce sections named .gnu.linkonce.<type>.<key>.
14530 Match like sections. LTO plugin sections are an exception.
14531 They are always named .gnu.linkonce.t.<key> and match either
14532 type of section. */
14533 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
14534 && ((flags
& SEC_GROUP
) != 0
14535 || strcmp (name
, l
->sec
->name
) == 0))
14536 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0
14537 || (sec
->owner
->flags
& BFD_PLUGIN
) != 0)
14539 /* The section has already been linked. See if we should
14540 issue a warning. */
14541 if (!_bfd_handle_already_linked (sec
, l
, info
))
14544 if (flags
& SEC_GROUP
)
14546 asection
*first
= elf_next_in_group (sec
);
14547 asection
*s
= first
;
14551 s
->output_section
= bfd_abs_section_ptr
;
14552 /* Record which group discards it. */
14553 s
->kept_section
= l
->sec
;
14554 s
= elf_next_in_group (s
);
14555 /* These lists are circular. */
14565 /* A single member comdat group section may be discarded by a
14566 linkonce section and vice versa. */
14567 if ((flags
& SEC_GROUP
) != 0)
14569 asection
*first
= elf_next_in_group (sec
);
14571 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
14572 /* Check this single member group against linkonce sections. */
14573 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14574 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14575 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
14577 first
->output_section
= bfd_abs_section_ptr
;
14578 first
->kept_section
= l
->sec
;
14579 sec
->output_section
= bfd_abs_section_ptr
;
14584 /* Check this linkonce section against single member groups. */
14585 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14586 if (l
->sec
->flags
& SEC_GROUP
)
14588 asection
*first
= elf_next_in_group (l
->sec
);
14591 && elf_next_in_group (first
) == first
14592 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
14594 sec
->output_section
= bfd_abs_section_ptr
;
14595 sec
->kept_section
= first
;
14600 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14601 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14602 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14603 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14604 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14605 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14606 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14607 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14608 The reverse order cannot happen as there is never a bfd with only the
14609 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14610 matter as here were are looking only for cross-bfd sections. */
14612 if ((flags
& SEC_GROUP
) == 0 && CONST_STRNEQ (name
, ".gnu.linkonce.r."))
14613 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14614 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14615 && CONST_STRNEQ (l
->sec
->name
, ".gnu.linkonce.t."))
14617 if (abfd
!= l
->sec
->owner
)
14618 sec
->output_section
= bfd_abs_section_ptr
;
14622 /* This is the first section with this name. Record it. */
14623 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
14624 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
14625 return sec
->output_section
== bfd_abs_section_ptr
;
14629 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
14631 return sym
->st_shndx
== SHN_COMMON
;
14635 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
14641 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
14643 return bfd_com_section_ptr
;
14647 _bfd_elf_default_got_elt_size (bfd
*abfd
,
14648 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
14649 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
14650 bfd
*ibfd ATTRIBUTE_UNUSED
,
14651 unsigned long symndx ATTRIBUTE_UNUSED
)
14653 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14654 return bed
->s
->arch_size
/ 8;
14657 /* Routines to support the creation of dynamic relocs. */
14659 /* Returns the name of the dynamic reloc section associated with SEC. */
14661 static const char *
14662 get_dynamic_reloc_section_name (bfd
* abfd
,
14664 bfd_boolean is_rela
)
14667 const char *old_name
= bfd_section_name (sec
);
14668 const char *prefix
= is_rela
? ".rela" : ".rel";
14670 if (old_name
== NULL
)
14673 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
14674 sprintf (name
, "%s%s", prefix
, old_name
);
14679 /* Returns the dynamic reloc section associated with SEC.
14680 If necessary compute the name of the dynamic reloc section based
14681 on SEC's name (looked up in ABFD's string table) and the setting
14685 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
14687 bfd_boolean is_rela
)
14689 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14691 if (reloc_sec
== NULL
)
14693 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14697 reloc_sec
= bfd_get_linker_section (abfd
, name
);
14699 if (reloc_sec
!= NULL
)
14700 elf_section_data (sec
)->sreloc
= reloc_sec
;
14707 /* Returns the dynamic reloc section associated with SEC. If the
14708 section does not exist it is created and attached to the DYNOBJ
14709 bfd and stored in the SRELOC field of SEC's elf_section_data
14712 ALIGNMENT is the alignment for the newly created section and
14713 IS_RELA defines whether the name should be .rela.<SEC's name>
14714 or .rel.<SEC's name>. The section name is looked up in the
14715 string table associated with ABFD. */
14718 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
14720 unsigned int alignment
,
14722 bfd_boolean is_rela
)
14724 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14726 if (reloc_sec
== NULL
)
14728 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14733 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
14735 if (reloc_sec
== NULL
)
14737 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
14738 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
14739 if ((sec
->flags
& SEC_ALLOC
) != 0)
14740 flags
|= SEC_ALLOC
| SEC_LOAD
;
14742 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
14743 if (reloc_sec
!= NULL
)
14745 /* _bfd_elf_get_sec_type_attr chooses a section type by
14746 name. Override as it may be wrong, eg. for a user
14747 section named "auto" we'll get ".relauto" which is
14748 seen to be a .rela section. */
14749 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
14750 if (!bfd_set_section_alignment (reloc_sec
, alignment
))
14755 elf_section_data (sec
)->sreloc
= reloc_sec
;
14761 /* Copy the ELF symbol type and other attributes for a linker script
14762 assignment from HSRC to HDEST. Generally this should be treated as
14763 if we found a strong non-dynamic definition for HDEST (except that
14764 ld ignores multiple definition errors). */
14766 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
14767 struct bfd_link_hash_entry
*hdest
,
14768 struct bfd_link_hash_entry
*hsrc
)
14770 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
14771 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
14772 Elf_Internal_Sym isym
;
14774 ehdest
->type
= ehsrc
->type
;
14775 ehdest
->target_internal
= ehsrc
->target_internal
;
14777 isym
.st_other
= ehsrc
->other
;
14778 elf_merge_st_other (abfd
, ehdest
, &isym
, NULL
, TRUE
, FALSE
);
14781 /* Append a RELA relocation REL to section S in BFD. */
14784 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14786 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14787 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
14788 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
14789 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
14792 /* Append a REL relocation REL to section S in BFD. */
14795 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14797 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14798 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
14799 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
14800 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);
14803 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14805 struct bfd_link_hash_entry
*
14806 bfd_elf_define_start_stop (struct bfd_link_info
*info
,
14807 const char *symbol
, asection
*sec
)
14809 struct elf_link_hash_entry
*h
;
14811 h
= elf_link_hash_lookup (elf_hash_table (info
), symbol
,
14812 FALSE
, FALSE
, TRUE
);
14813 /* NB: Common symbols will be turned into definition later. */
14815 && (h
->root
.type
== bfd_link_hash_undefined
14816 || h
->root
.type
== bfd_link_hash_undefweak
14817 || ((h
->ref_regular
|| h
->def_dynamic
)
14819 && h
->root
.type
!= bfd_link_hash_common
)))
14821 bfd_boolean was_dynamic
= h
->ref_dynamic
|| h
->def_dynamic
;
14822 h
->verinfo
.verdef
= NULL
;
14823 h
->root
.type
= bfd_link_hash_defined
;
14824 h
->root
.u
.def
.section
= sec
;
14825 h
->root
.u
.def
.value
= 0;
14826 h
->def_regular
= 1;
14827 h
->def_dynamic
= 0;
14829 h
->u2
.start_stop_section
= sec
;
14830 if (symbol
[0] == '.')
14832 /* .startof. and .sizeof. symbols are local. */
14833 const struct elf_backend_data
*bed
;
14834 bed
= get_elf_backend_data (info
->output_bfd
);
14835 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
14839 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
14840 h
->other
= ((h
->other
& ~ELF_ST_VISIBILITY (-1))
14841 | info
->start_stop_visibility
);
14843 bfd_elf_link_record_dynamic_symbol (info
, h
);
14850 /* Find dynamic relocs for H that apply to read-only sections. */
14853 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry
*h
)
14855 struct elf_dyn_relocs
*p
;
14857 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
14859 asection
*s
= p
->sec
->output_section
;
14861 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
14867 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
14868 read-only sections. */
14871 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry
*h
, void *inf
)
14875 if (h
->root
.type
== bfd_link_hash_indirect
)
14878 sec
= _bfd_elf_readonly_dynrelocs (h
);
14881 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
14883 info
->flags
|= DF_TEXTREL
;
14884 /* xgettext:c-format */
14885 info
->callbacks
->minfo (_("%pB: dynamic relocation against `%pT' "
14886 "in read-only section `%pA'\n"),
14887 sec
->owner
, h
->root
.root
.string
, sec
);
14889 if (bfd_link_textrel_check (info
))
14890 /* xgettext:c-format */
14891 info
->callbacks
->einfo (_("%P: %pB: warning: relocation against `%s' "
14892 "in read-only section `%pA'\n"),
14893 sec
->owner
, h
->root
.root
.string
, sec
);
14895 /* Not an error, just cut short the traversal. */
14901 /* Add dynamic tags. */
14904 _bfd_elf_add_dynamic_tags (bfd
*output_bfd
, struct bfd_link_info
*info
,
14905 bfd_boolean need_dynamic_reloc
)
14907 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
14909 if (htab
->dynamic_sections_created
)
14911 /* Add some entries to the .dynamic section. We fill in the
14912 values later, in finish_dynamic_sections, but we must add
14913 the entries now so that we get the correct size for the
14914 .dynamic section. The DT_DEBUG entry is filled in by the
14915 dynamic linker and used by the debugger. */
14916 #define add_dynamic_entry(TAG, VAL) \
14917 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
14919 const struct elf_backend_data
*bed
14920 = get_elf_backend_data (output_bfd
);
14922 if (bfd_link_executable (info
))
14924 if (!add_dynamic_entry (DT_DEBUG
, 0))
14928 if (htab
->dt_pltgot_required
|| htab
->splt
->size
!= 0)
14930 /* DT_PLTGOT is used by prelink even if there is no PLT
14932 if (!add_dynamic_entry (DT_PLTGOT
, 0))
14936 if (htab
->dt_jmprel_required
|| htab
->srelplt
->size
!= 0)
14938 if (!add_dynamic_entry (DT_PLTRELSZ
, 0)
14939 || !add_dynamic_entry (DT_PLTREL
,
14940 (bed
->rela_plts_and_copies_p
14941 ? DT_RELA
: DT_REL
))
14942 || !add_dynamic_entry (DT_JMPREL
, 0))
14946 if (htab
->tlsdesc_plt
14947 && (!add_dynamic_entry (DT_TLSDESC_PLT
, 0)
14948 || !add_dynamic_entry (DT_TLSDESC_GOT
, 0)))
14951 if (need_dynamic_reloc
)
14953 if (bed
->rela_plts_and_copies_p
)
14955 if (!add_dynamic_entry (DT_RELA
, 0)
14956 || !add_dynamic_entry (DT_RELASZ
, 0)
14957 || !add_dynamic_entry (DT_RELAENT
,
14958 bed
->s
->sizeof_rela
))
14963 if (!add_dynamic_entry (DT_REL
, 0)
14964 || !add_dynamic_entry (DT_RELSZ
, 0)
14965 || !add_dynamic_entry (DT_RELENT
,
14966 bed
->s
->sizeof_rel
))
14970 /* If any dynamic relocs apply to a read-only section,
14971 then we need a DT_TEXTREL entry. */
14972 if ((info
->flags
& DF_TEXTREL
) == 0)
14973 elf_link_hash_traverse (htab
, _bfd_elf_maybe_set_textrel
,
14976 if ((info
->flags
& DF_TEXTREL
) != 0)
14978 if (htab
->ifunc_resolvers
)
14979 info
->callbacks
->einfo
14980 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
14981 "may result in a segfault at runtime; recompile with %s\n"),
14982 bfd_link_dll (info
) ? "-fPIC" : "-fPIE");
14984 if (!add_dynamic_entry (DT_TEXTREL
, 0))
14989 #undef add_dynamic_entry