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
;
2649 /* Don't free alloc2, since if it was allocated we are passing it
2650 back (under the name of internal_relocs). */
2652 return internal_relocs
;
2660 bfd_release (abfd
, alloc2
);
2667 /* Compute the size of, and allocate space for, REL_HDR which is the
2668 section header for a section containing relocations for O. */
2671 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2672 struct bfd_elf_section_reloc_data
*reldata
)
2674 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2676 /* That allows us to calculate the size of the section. */
2677 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2679 /* The contents field must last into write_object_contents, so we
2680 allocate it with bfd_alloc rather than malloc. Also since we
2681 cannot be sure that the contents will actually be filled in,
2682 we zero the allocated space. */
2683 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2684 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2687 if (reldata
->hashes
== NULL
&& reldata
->count
)
2689 struct elf_link_hash_entry
**p
;
2691 p
= ((struct elf_link_hash_entry
**)
2692 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2696 reldata
->hashes
= p
;
2702 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2703 originated from the section given by INPUT_REL_HDR) to the
2707 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2708 asection
*input_section
,
2709 Elf_Internal_Shdr
*input_rel_hdr
,
2710 Elf_Internal_Rela
*internal_relocs
,
2711 struct elf_link_hash_entry
**rel_hash
2714 Elf_Internal_Rela
*irela
;
2715 Elf_Internal_Rela
*irelaend
;
2717 struct bfd_elf_section_reloc_data
*output_reldata
;
2718 asection
*output_section
;
2719 const struct elf_backend_data
*bed
;
2720 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2721 struct bfd_elf_section_data
*esdo
;
2723 output_section
= input_section
->output_section
;
2725 bed
= get_elf_backend_data (output_bfd
);
2726 esdo
= elf_section_data (output_section
);
2727 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2729 output_reldata
= &esdo
->rel
;
2730 swap_out
= bed
->s
->swap_reloc_out
;
2732 else if (esdo
->rela
.hdr
2733 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2735 output_reldata
= &esdo
->rela
;
2736 swap_out
= bed
->s
->swap_reloca_out
;
2741 /* xgettext:c-format */
2742 (_("%pB: relocation size mismatch in %pB section %pA"),
2743 output_bfd
, input_section
->owner
, input_section
);
2744 bfd_set_error (bfd_error_wrong_format
);
2748 erel
= output_reldata
->hdr
->contents
;
2749 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2750 irela
= internal_relocs
;
2751 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2752 * bed
->s
->int_rels_per_ext_rel
);
2753 while (irela
< irelaend
)
2755 (*swap_out
) (output_bfd
, irela
, erel
);
2756 irela
+= bed
->s
->int_rels_per_ext_rel
;
2757 erel
+= input_rel_hdr
->sh_entsize
;
2760 /* Bump the counter, so that we know where to add the next set of
2762 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2767 /* Make weak undefined symbols in PIE dynamic. */
2770 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2771 struct elf_link_hash_entry
*h
)
2773 if (bfd_link_pie (info
)
2775 && h
->root
.type
== bfd_link_hash_undefweak
)
2776 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2781 /* Fix up the flags for a symbol. This handles various cases which
2782 can only be fixed after all the input files are seen. This is
2783 currently called by both adjust_dynamic_symbol and
2784 assign_sym_version, which is unnecessary but perhaps more robust in
2785 the face of future changes. */
2788 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2789 struct elf_info_failed
*eif
)
2791 const struct elf_backend_data
*bed
;
2793 /* If this symbol was mentioned in a non-ELF file, try to set
2794 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2795 permit a non-ELF file to correctly refer to a symbol defined in
2796 an ELF dynamic object. */
2799 while (h
->root
.type
== bfd_link_hash_indirect
)
2800 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2802 if (h
->root
.type
!= bfd_link_hash_defined
2803 && h
->root
.type
!= bfd_link_hash_defweak
)
2806 h
->ref_regular_nonweak
= 1;
2810 if (h
->root
.u
.def
.section
->owner
!= NULL
2811 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2812 == bfd_target_elf_flavour
))
2815 h
->ref_regular_nonweak
= 1;
2821 if (h
->dynindx
== -1
2825 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2834 /* Unfortunately, NON_ELF is only correct if the symbol
2835 was first seen in a non-ELF file. Fortunately, if the symbol
2836 was first seen in an ELF file, we're probably OK unless the
2837 symbol was defined in a non-ELF file. Catch that case here.
2838 FIXME: We're still in trouble if the symbol was first seen in
2839 a dynamic object, and then later in a non-ELF regular object. */
2840 if ((h
->root
.type
== bfd_link_hash_defined
2841 || h
->root
.type
== bfd_link_hash_defweak
)
2843 && (h
->root
.u
.def
.section
->owner
!= NULL
2844 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2845 != bfd_target_elf_flavour
)
2846 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2847 && !h
->def_dynamic
)))
2851 /* Backend specific symbol fixup. */
2852 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2853 if (bed
->elf_backend_fixup_symbol
2854 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2857 /* If this is a final link, and the symbol was defined as a common
2858 symbol in a regular object file, and there was no definition in
2859 any dynamic object, then the linker will have allocated space for
2860 the symbol in a common section but the DEF_REGULAR
2861 flag will not have been set. */
2862 if (h
->root
.type
== bfd_link_hash_defined
2866 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
2869 /* Symbols defined in discarded sections shouldn't be dynamic. */
2870 if (h
->root
.type
== bfd_link_hash_undefined
&& h
->indx
== -3)
2871 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2873 /* If a weak undefined symbol has non-default visibility, we also
2874 hide it from the dynamic linker. */
2875 else if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2876 && h
->root
.type
== bfd_link_hash_undefweak
)
2877 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2879 /* A hidden versioned symbol in executable should be forced local if
2880 it is is locally defined, not referenced by shared library and not
2882 else if (bfd_link_executable (eif
->info
)
2883 && h
->versioned
== versioned_hidden
2884 && !eif
->info
->export_dynamic
2888 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2890 /* If -Bsymbolic was used (which means to bind references to global
2891 symbols to the definition within the shared object), and this
2892 symbol was defined in a regular object, then it actually doesn't
2893 need a PLT entry. Likewise, if the symbol has non-default
2894 visibility. If the symbol has hidden or internal visibility, we
2895 will force it local. */
2896 else if (h
->needs_plt
2897 && bfd_link_pic (eif
->info
)
2898 && is_elf_hash_table (eif
->info
->hash
)
2899 && (SYMBOLIC_BIND (eif
->info
, h
)
2900 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2903 bfd_boolean force_local
;
2905 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2906 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2907 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2910 /* If this is a weak defined symbol in a dynamic object, and we know
2911 the real definition in the dynamic object, copy interesting flags
2912 over to the real definition. */
2913 if (h
->is_weakalias
)
2915 struct elf_link_hash_entry
*def
= weakdef (h
);
2917 /* If the real definition is defined by a regular object file,
2918 don't do anything special. See the longer description in
2919 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2920 bfd_link_hash_defined as it was when put on the alias list
2921 then it must have originally been a versioned symbol (for
2922 which a non-versioned indirect symbol is created) and later
2923 a definition for the non-versioned symbol is found. In that
2924 case the indirection is flipped with the versioned symbol
2925 becoming an indirect pointing at the non-versioned symbol.
2926 Thus, not an alias any more. */
2927 if (def
->def_regular
2928 || def
->root
.type
!= bfd_link_hash_defined
)
2931 while ((h
= h
->u
.alias
) != def
)
2932 h
->is_weakalias
= 0;
2936 while (h
->root
.type
== bfd_link_hash_indirect
)
2937 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2938 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2939 || h
->root
.type
== bfd_link_hash_defweak
);
2940 BFD_ASSERT (def
->def_dynamic
);
2941 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, def
, h
);
2948 /* Make the backend pick a good value for a dynamic symbol. This is
2949 called via elf_link_hash_traverse, and also calls itself
2953 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2955 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2956 struct elf_link_hash_table
*htab
;
2957 const struct elf_backend_data
*bed
;
2959 if (! is_elf_hash_table (eif
->info
->hash
))
2962 /* Ignore indirect symbols. These are added by the versioning code. */
2963 if (h
->root
.type
== bfd_link_hash_indirect
)
2966 /* Fix the symbol flags. */
2967 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2970 htab
= elf_hash_table (eif
->info
);
2971 bed
= get_elf_backend_data (htab
->dynobj
);
2973 if (h
->root
.type
== bfd_link_hash_undefweak
)
2975 if (eif
->info
->dynamic_undefined_weak
== 0)
2976 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2977 else if (eif
->info
->dynamic_undefined_weak
> 0
2979 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2980 && !bfd_hide_sym_by_version (eif
->info
->version_info
,
2981 h
->root
.root
.string
))
2983 if (!bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2991 /* If this symbol does not require a PLT entry, and it is not
2992 defined by a dynamic object, or is not referenced by a regular
2993 object, ignore it. We do have to handle a weak defined symbol,
2994 even if no regular object refers to it, if we decided to add it
2995 to the dynamic symbol table. FIXME: Do we normally need to worry
2996 about symbols which are defined by one dynamic object and
2997 referenced by another one? */
2999 && h
->type
!= STT_GNU_IFUNC
3003 && (!h
->is_weakalias
|| weakdef (h
)->dynindx
== -1))))
3005 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
3009 /* If we've already adjusted this symbol, don't do it again. This
3010 can happen via a recursive call. */
3011 if (h
->dynamic_adjusted
)
3014 /* Don't look at this symbol again. Note that we must set this
3015 after checking the above conditions, because we may look at a
3016 symbol once, decide not to do anything, and then get called
3017 recursively later after REF_REGULAR is set below. */
3018 h
->dynamic_adjusted
= 1;
3020 /* If this is a weak definition, and we know a real definition, and
3021 the real symbol is not itself defined by a regular object file,
3022 then get a good value for the real definition. We handle the
3023 real symbol first, for the convenience of the backend routine.
3025 Note that there is a confusing case here. If the real definition
3026 is defined by a regular object file, we don't get the real symbol
3027 from the dynamic object, but we do get the weak symbol. If the
3028 processor backend uses a COPY reloc, then if some routine in the
3029 dynamic object changes the real symbol, we will not see that
3030 change in the corresponding weak symbol. This is the way other
3031 ELF linkers work as well, and seems to be a result of the shared
3034 I will clarify this issue. Most SVR4 shared libraries define the
3035 variable _timezone and define timezone as a weak synonym. The
3036 tzset call changes _timezone. If you write
3037 extern int timezone;
3039 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3040 you might expect that, since timezone is a synonym for _timezone,
3041 the same number will print both times. However, if the processor
3042 backend uses a COPY reloc, then actually timezone will be copied
3043 into your process image, and, since you define _timezone
3044 yourself, _timezone will not. Thus timezone and _timezone will
3045 wind up at different memory locations. The tzset call will set
3046 _timezone, leaving timezone unchanged. */
3048 if (h
->is_weakalias
)
3050 struct elf_link_hash_entry
*def
= weakdef (h
);
3052 /* If we get to this point, there is an implicit reference to
3053 the alias by a regular object file via the weak symbol H. */
3054 def
->ref_regular
= 1;
3056 /* Ensure that the backend adjust_dynamic_symbol function sees
3057 the strong alias before H by recursively calling ourselves. */
3058 if (!_bfd_elf_adjust_dynamic_symbol (def
, eif
))
3062 /* If a symbol has no type and no size and does not require a PLT
3063 entry, then we are probably about to do the wrong thing here: we
3064 are probably going to create a COPY reloc for an empty object.
3065 This case can arise when a shared object is built with assembly
3066 code, and the assembly code fails to set the symbol type. */
3068 && h
->type
== STT_NOTYPE
3071 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3072 h
->root
.root
.string
);
3074 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3083 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3087 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
3088 struct elf_link_hash_entry
*h
,
3091 unsigned int power_of_two
;
3093 asection
*sec
= h
->root
.u
.def
.section
;
3095 /* The section alignment of the definition is the maximum alignment
3096 requirement of symbols defined in the section. Since we don't
3097 know the symbol alignment requirement, we start with the
3098 maximum alignment and check low bits of the symbol address
3099 for the minimum alignment. */
3100 power_of_two
= bfd_section_alignment (sec
);
3101 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
3102 while ((h
->root
.u
.def
.value
& mask
) != 0)
3108 if (power_of_two
> bfd_section_alignment (dynbss
))
3110 /* Adjust the section alignment if needed. */
3111 if (!bfd_set_section_alignment (dynbss
, power_of_two
))
3115 /* We make sure that the symbol will be aligned properly. */
3116 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
3118 /* Define the symbol as being at this point in DYNBSS. */
3119 h
->root
.u
.def
.section
= dynbss
;
3120 h
->root
.u
.def
.value
= dynbss
->size
;
3122 /* Increment the size of DYNBSS to make room for the symbol. */
3123 dynbss
->size
+= h
->size
;
3125 /* No error if extern_protected_data is true. */
3126 if (h
->protected_def
3127 && (!info
->extern_protected_data
3128 || (info
->extern_protected_data
< 0
3129 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
3130 info
->callbacks
->einfo
3131 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3132 h
->root
.root
.string
);
3137 /* Adjust all external symbols pointing into SEC_MERGE sections
3138 to reflect the object merging within the sections. */
3141 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
3145 if ((h
->root
.type
== bfd_link_hash_defined
3146 || h
->root
.type
== bfd_link_hash_defweak
)
3147 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
3148 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
3150 bfd
*output_bfd
= (bfd
*) data
;
3152 h
->root
.u
.def
.value
=
3153 _bfd_merged_section_offset (output_bfd
,
3154 &h
->root
.u
.def
.section
,
3155 elf_section_data (sec
)->sec_info
,
3156 h
->root
.u
.def
.value
);
3162 /* Returns false if the symbol referred to by H should be considered
3163 to resolve local to the current module, and true if it should be
3164 considered to bind dynamically. */
3167 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
3168 struct bfd_link_info
*info
,
3169 bfd_boolean not_local_protected
)
3171 bfd_boolean binding_stays_local_p
;
3172 const struct elf_backend_data
*bed
;
3173 struct elf_link_hash_table
*hash_table
;
3178 while (h
->root
.type
== bfd_link_hash_indirect
3179 || h
->root
.type
== bfd_link_hash_warning
)
3180 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3182 /* If it was forced local, then clearly it's not dynamic. */
3183 if (h
->dynindx
== -1)
3185 if (h
->forced_local
)
3188 /* Identify the cases where name binding rules say that a
3189 visible symbol resolves locally. */
3190 binding_stays_local_p
= (bfd_link_executable (info
)
3191 || SYMBOLIC_BIND (info
, h
));
3193 switch (ELF_ST_VISIBILITY (h
->other
))
3200 hash_table
= elf_hash_table (info
);
3201 if (!is_elf_hash_table (hash_table
))
3204 bed
= get_elf_backend_data (hash_table
->dynobj
);
3206 /* Proper resolution for function pointer equality may require
3207 that these symbols perhaps be resolved dynamically, even though
3208 we should be resolving them to the current module. */
3209 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
3210 binding_stays_local_p
= TRUE
;
3217 /* If it isn't defined locally, then clearly it's dynamic. */
3218 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
3221 /* Otherwise, the symbol is dynamic if binding rules don't tell
3222 us that it remains local. */
3223 return !binding_stays_local_p
;
3226 /* Return true if the symbol referred to by H should be considered
3227 to resolve local to the current module, and false otherwise. Differs
3228 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3229 undefined symbols. The two functions are virtually identical except
3230 for the place where dynindx == -1 is tested. If that test is true,
3231 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3232 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3234 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3235 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3236 treatment of undefined weak symbols. For those that do not make
3237 undefined weak symbols dynamic, both functions may return false. */
3240 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3241 struct bfd_link_info
*info
,
3242 bfd_boolean local_protected
)
3244 const struct elf_backend_data
*bed
;
3245 struct elf_link_hash_table
*hash_table
;
3247 /* If it's a local sym, of course we resolve locally. */
3251 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3252 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3253 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3256 /* Forced local symbols resolve locally. */
3257 if (h
->forced_local
)
3260 /* Common symbols that become definitions don't get the DEF_REGULAR
3261 flag set, so test it first, and don't bail out. */
3262 if (ELF_COMMON_DEF_P (h
))
3264 /* If we don't have a definition in a regular file, then we can't
3265 resolve locally. The sym is either undefined or dynamic. */
3266 else if (!h
->def_regular
)
3269 /* Non-dynamic symbols resolve locally. */
3270 if (h
->dynindx
== -1)
3273 /* At this point, we know the symbol is defined and dynamic. In an
3274 executable it must resolve locally, likewise when building symbolic
3275 shared libraries. */
3276 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3279 /* Now deal with defined dynamic symbols in shared libraries. Ones
3280 with default visibility might not resolve locally. */
3281 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3284 hash_table
= elf_hash_table (info
);
3285 if (!is_elf_hash_table (hash_table
))
3288 bed
= get_elf_backend_data (hash_table
->dynobj
);
3290 /* If extern_protected_data is false, STV_PROTECTED non-function
3291 symbols are local. */
3292 if ((!info
->extern_protected_data
3293 || (info
->extern_protected_data
< 0
3294 && !bed
->extern_protected_data
))
3295 && !bed
->is_function_type (h
->type
))
3298 /* Function pointer equality tests may require that STV_PROTECTED
3299 symbols be treated as dynamic symbols. If the address of a
3300 function not defined in an executable is set to that function's
3301 plt entry in the executable, then the address of the function in
3302 a shared library must also be the plt entry in the executable. */
3303 return local_protected
;
3306 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3307 aligned. Returns the first TLS output section. */
3309 struct bfd_section
*
3310 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3312 struct bfd_section
*sec
, *tls
;
3313 unsigned int align
= 0;
3315 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3316 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3320 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3321 if (sec
->alignment_power
> align
)
3322 align
= sec
->alignment_power
;
3324 elf_hash_table (info
)->tls_sec
= tls
;
3326 /* Ensure the alignment of the first section (usually .tdata) is the largest
3327 alignment, so that the tls segment starts aligned. */
3329 tls
->alignment_power
= align
;
3334 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3336 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3337 Elf_Internal_Sym
*sym
)
3339 const struct elf_backend_data
*bed
;
3341 /* Local symbols do not count, but target specific ones might. */
3342 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3343 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3346 bed
= get_elf_backend_data (abfd
);
3347 /* Function symbols do not count. */
3348 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3351 /* If the section is undefined, then so is the symbol. */
3352 if (sym
->st_shndx
== SHN_UNDEF
)
3355 /* If the symbol is defined in the common section, then
3356 it is a common definition and so does not count. */
3357 if (bed
->common_definition (sym
))
3360 /* If the symbol is in a target specific section then we
3361 must rely upon the backend to tell us what it is. */
3362 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3363 /* FIXME - this function is not coded yet:
3365 return _bfd_is_global_symbol_definition (abfd, sym);
3367 Instead for now assume that the definition is not global,
3368 Even if this is wrong, at least the linker will behave
3369 in the same way that it used to do. */
3375 /* Search the symbol table of the archive element of the archive ABFD
3376 whose archive map contains a mention of SYMDEF, and determine if
3377 the symbol is defined in this element. */
3379 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3381 Elf_Internal_Shdr
* hdr
;
3385 Elf_Internal_Sym
*isymbuf
;
3386 Elf_Internal_Sym
*isym
;
3387 Elf_Internal_Sym
*isymend
;
3390 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
3394 if (! bfd_check_format (abfd
, bfd_object
))
3397 /* Select the appropriate symbol table. If we don't know if the
3398 object file is an IR object, give linker LTO plugin a chance to
3399 get the correct symbol table. */
3400 if (abfd
->plugin_format
== bfd_plugin_yes
3401 #if BFD_SUPPORTS_PLUGINS
3402 || (abfd
->plugin_format
== bfd_plugin_unknown
3403 && bfd_link_plugin_object_p (abfd
))
3407 /* Use the IR symbol table if the object has been claimed by
3409 abfd
= abfd
->plugin_dummy_bfd
;
3410 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3412 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3413 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3415 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3417 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3419 /* The sh_info field of the symtab header tells us where the
3420 external symbols start. We don't care about the local symbols. */
3421 if (elf_bad_symtab (abfd
))
3423 extsymcount
= symcount
;
3428 extsymcount
= symcount
- hdr
->sh_info
;
3429 extsymoff
= hdr
->sh_info
;
3432 if (extsymcount
== 0)
3435 /* Read in the symbol table. */
3436 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3438 if (isymbuf
== NULL
)
3441 /* Scan the symbol table looking for SYMDEF. */
3443 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3447 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3452 if (strcmp (name
, symdef
->name
) == 0)
3454 result
= is_global_data_symbol_definition (abfd
, isym
);
3464 /* Add an entry to the .dynamic table. */
3467 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3471 struct elf_link_hash_table
*hash_table
;
3472 const struct elf_backend_data
*bed
;
3474 bfd_size_type newsize
;
3475 bfd_byte
*newcontents
;
3476 Elf_Internal_Dyn dyn
;
3478 hash_table
= elf_hash_table (info
);
3479 if (! is_elf_hash_table (hash_table
))
3482 if (tag
== DT_RELA
|| tag
== DT_REL
)
3483 hash_table
->dynamic_relocs
= TRUE
;
3485 bed
= get_elf_backend_data (hash_table
->dynobj
);
3486 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3487 BFD_ASSERT (s
!= NULL
);
3489 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3490 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3491 if (newcontents
== NULL
)
3495 dyn
.d_un
.d_val
= val
;
3496 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3499 s
->contents
= newcontents
;
3504 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3505 1 if a DT_NEEDED tag already exists, and 0 on success. */
3508 bfd_elf_add_dt_needed_tag (bfd
*abfd
, struct bfd_link_info
*info
)
3510 struct elf_link_hash_table
*hash_table
;
3514 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3517 hash_table
= elf_hash_table (info
);
3518 soname
= elf_dt_name (abfd
);
3519 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3520 if (strindex
== (size_t) -1)
3523 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3526 const struct elf_backend_data
*bed
;
3529 bed
= get_elf_backend_data (hash_table
->dynobj
);
3530 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3532 for (extdyn
= sdyn
->contents
;
3533 extdyn
< sdyn
->contents
+ sdyn
->size
;
3534 extdyn
+= bed
->s
->sizeof_dyn
)
3536 Elf_Internal_Dyn dyn
;
3538 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3539 if (dyn
.d_tag
== DT_NEEDED
3540 && dyn
.d_un
.d_val
== strindex
)
3542 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3548 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3551 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3557 /* Return true if SONAME is on the needed list between NEEDED and STOP
3558 (or the end of list if STOP is NULL), and needed by a library that
3562 on_needed_list (const char *soname
,
3563 struct bfd_link_needed_list
*needed
,
3564 struct bfd_link_needed_list
*stop
)
3566 struct bfd_link_needed_list
*look
;
3567 for (look
= needed
; look
!= stop
; look
= look
->next
)
3568 if (strcmp (soname
, look
->name
) == 0
3569 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3570 /* If needed by a library that itself is not directly
3571 needed, recursively check whether that library is
3572 indirectly needed. Since we add DT_NEEDED entries to
3573 the end of the list, library dependencies appear after
3574 the library. Therefore search prior to the current
3575 LOOK, preventing possible infinite recursion. */
3576 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3582 /* Sort symbol by value, section, size, and type. */
3584 elf_sort_symbol (const void *arg1
, const void *arg2
)
3586 const struct elf_link_hash_entry
*h1
;
3587 const struct elf_link_hash_entry
*h2
;
3588 bfd_signed_vma vdiff
;
3593 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3594 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3595 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3597 return vdiff
> 0 ? 1 : -1;
3599 sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3603 /* Sort so that sized symbols are selected over zero size symbols. */
3604 vdiff
= h1
->size
- h2
->size
;
3606 return vdiff
> 0 ? 1 : -1;
3608 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3609 if (h1
->type
!= h2
->type
)
3610 return h1
->type
- h2
->type
;
3612 /* If symbols are properly sized and typed, and multiple strong
3613 aliases are not defined in a shared library by the user we
3614 shouldn't get here. Unfortunately linker script symbols like
3615 __bss_start sometimes match a user symbol defined at the start of
3616 .bss without proper size and type. We'd like to preference the
3617 user symbol over reserved system symbols. Sort on leading
3619 n1
= h1
->root
.root
.string
;
3620 n2
= h2
->root
.root
.string
;
3633 /* Final sort on name selects user symbols like '_u' over reserved
3634 system symbols like '_Z' and also will avoid qsort instability. */
3638 /* This function is used to adjust offsets into .dynstr for
3639 dynamic symbols. This is called via elf_link_hash_traverse. */
3642 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3644 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3646 if (h
->dynindx
!= -1)
3647 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3651 /* Assign string offsets in .dynstr, update all structures referencing
3655 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3657 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3658 struct elf_link_local_dynamic_entry
*entry
;
3659 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3660 bfd
*dynobj
= hash_table
->dynobj
;
3663 const struct elf_backend_data
*bed
;
3666 _bfd_elf_strtab_finalize (dynstr
);
3667 size
= _bfd_elf_strtab_size (dynstr
);
3669 bed
= get_elf_backend_data (dynobj
);
3670 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3671 BFD_ASSERT (sdyn
!= NULL
);
3673 /* Update all .dynamic entries referencing .dynstr strings. */
3674 for (extdyn
= sdyn
->contents
;
3675 extdyn
< sdyn
->contents
+ sdyn
->size
;
3676 extdyn
+= bed
->s
->sizeof_dyn
)
3678 Elf_Internal_Dyn dyn
;
3680 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3684 dyn
.d_un
.d_val
= size
;
3694 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3699 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3702 /* Now update local dynamic symbols. */
3703 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3704 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3705 entry
->isym
.st_name
);
3707 /* And the rest of dynamic symbols. */
3708 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3710 /* Adjust version definitions. */
3711 if (elf_tdata (output_bfd
)->cverdefs
)
3716 Elf_Internal_Verdef def
;
3717 Elf_Internal_Verdaux defaux
;
3719 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3723 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3725 p
+= sizeof (Elf_External_Verdef
);
3726 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3728 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3730 _bfd_elf_swap_verdaux_in (output_bfd
,
3731 (Elf_External_Verdaux
*) p
, &defaux
);
3732 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3734 _bfd_elf_swap_verdaux_out (output_bfd
,
3735 &defaux
, (Elf_External_Verdaux
*) p
);
3736 p
+= sizeof (Elf_External_Verdaux
);
3739 while (def
.vd_next
);
3742 /* Adjust version references. */
3743 if (elf_tdata (output_bfd
)->verref
)
3748 Elf_Internal_Verneed need
;
3749 Elf_Internal_Vernaux needaux
;
3751 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
3755 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3757 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3758 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3759 (Elf_External_Verneed
*) p
);
3760 p
+= sizeof (Elf_External_Verneed
);
3761 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3763 _bfd_elf_swap_vernaux_in (output_bfd
,
3764 (Elf_External_Vernaux
*) p
, &needaux
);
3765 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3767 _bfd_elf_swap_vernaux_out (output_bfd
,
3769 (Elf_External_Vernaux
*) p
);
3770 p
+= sizeof (Elf_External_Vernaux
);
3773 while (need
.vn_next
);
3779 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3780 The default is to only match when the INPUT and OUTPUT are exactly
3784 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3785 const bfd_target
*output
)
3787 return input
== output
;
3790 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3791 This version is used when different targets for the same architecture
3792 are virtually identical. */
3795 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3796 const bfd_target
*output
)
3798 const struct elf_backend_data
*obed
, *ibed
;
3800 if (input
== output
)
3803 ibed
= xvec_get_elf_backend_data (input
);
3804 obed
= xvec_get_elf_backend_data (output
);
3806 if (ibed
->arch
!= obed
->arch
)
3809 /* If both backends are using this function, deem them compatible. */
3810 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3813 /* Make a special call to the linker "notice" function to tell it that
3814 we are about to handle an as-needed lib, or have finished
3815 processing the lib. */
3818 _bfd_elf_notice_as_needed (bfd
*ibfd
,
3819 struct bfd_link_info
*info
,
3820 enum notice_asneeded_action act
)
3822 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
3825 /* Check relocations an ELF object file. */
3828 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
3830 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3831 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
3833 /* If this object is the same format as the output object, and it is
3834 not a shared library, then let the backend look through the
3837 This is required to build global offset table entries and to
3838 arrange for dynamic relocs. It is not required for the
3839 particular common case of linking non PIC code, even when linking
3840 against shared libraries, but unfortunately there is no way of
3841 knowing whether an object file has been compiled PIC or not.
3842 Looking through the relocs is not particularly time consuming.
3843 The problem is that we must either (1) keep the relocs in memory,
3844 which causes the linker to require additional runtime memory or
3845 (2) read the relocs twice from the input file, which wastes time.
3846 This would be a good case for using mmap.
3848 I have no idea how to handle linking PIC code into a file of a
3849 different format. It probably can't be done. */
3850 if ((abfd
->flags
& DYNAMIC
) == 0
3851 && is_elf_hash_table (htab
)
3852 && bed
->check_relocs
!= NULL
3853 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
3854 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
3858 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3860 Elf_Internal_Rela
*internal_relocs
;
3863 /* Don't check relocations in excluded sections. */
3864 if ((o
->flags
& SEC_RELOC
) == 0
3865 || (o
->flags
& SEC_EXCLUDE
) != 0
3866 || o
->reloc_count
== 0
3867 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
3868 && (o
->flags
& SEC_DEBUGGING
) != 0)
3869 || bfd_is_abs_section (o
->output_section
))
3872 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
3874 if (internal_relocs
== NULL
)
3877 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
3879 if (elf_section_data (o
)->relocs
!= internal_relocs
)
3880 free (internal_relocs
);
3890 /* Add symbols from an ELF object file to the linker hash table. */
3893 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3895 Elf_Internal_Ehdr
*ehdr
;
3896 Elf_Internal_Shdr
*hdr
;
3900 struct elf_link_hash_entry
**sym_hash
;
3901 bfd_boolean dynamic
;
3902 Elf_External_Versym
*extversym
= NULL
;
3903 Elf_External_Versym
*extversym_end
= NULL
;
3904 Elf_External_Versym
*ever
;
3905 struct elf_link_hash_entry
*weaks
;
3906 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3907 size_t nondeflt_vers_cnt
= 0;
3908 Elf_Internal_Sym
*isymbuf
= NULL
;
3909 Elf_Internal_Sym
*isym
;
3910 Elf_Internal_Sym
*isymend
;
3911 const struct elf_backend_data
*bed
;
3912 bfd_boolean add_needed
;
3913 struct elf_link_hash_table
*htab
;
3914 void *alloc_mark
= NULL
;
3915 struct bfd_hash_entry
**old_table
= NULL
;
3916 unsigned int old_size
= 0;
3917 unsigned int old_count
= 0;
3918 void *old_tab
= NULL
;
3920 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3921 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3922 void *old_strtab
= NULL
;
3925 bfd_boolean just_syms
;
3927 htab
= elf_hash_table (info
);
3928 bed
= get_elf_backend_data (abfd
);
3930 if ((abfd
->flags
& DYNAMIC
) == 0)
3936 /* You can't use -r against a dynamic object. Also, there's no
3937 hope of using a dynamic object which does not exactly match
3938 the format of the output file. */
3939 if (bfd_link_relocatable (info
)
3940 || !is_elf_hash_table (htab
)
3941 || info
->output_bfd
->xvec
!= abfd
->xvec
)
3943 if (bfd_link_relocatable (info
))
3944 bfd_set_error (bfd_error_invalid_operation
);
3946 bfd_set_error (bfd_error_wrong_format
);
3951 ehdr
= elf_elfheader (abfd
);
3952 if (info
->warn_alternate_em
3953 && bed
->elf_machine_code
!= ehdr
->e_machine
3954 && ((bed
->elf_machine_alt1
!= 0
3955 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
3956 || (bed
->elf_machine_alt2
!= 0
3957 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
3959 /* xgettext:c-format */
3960 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
3961 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
3963 /* As a GNU extension, any input sections which are named
3964 .gnu.warning.SYMBOL are treated as warning symbols for the given
3965 symbol. This differs from .gnu.warning sections, which generate
3966 warnings when they are included in an output file. */
3967 /* PR 12761: Also generate this warning when building shared libraries. */
3968 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3972 name
= bfd_section_name (s
);
3973 if (CONST_STRNEQ (name
, ".gnu.warning."))
3978 name
+= sizeof ".gnu.warning." - 1;
3980 /* If this is a shared object, then look up the symbol
3981 in the hash table. If it is there, and it is already
3982 been defined, then we will not be using the entry
3983 from this shared object, so we don't need to warn.
3984 FIXME: If we see the definition in a regular object
3985 later on, we will warn, but we shouldn't. The only
3986 fix is to keep track of what warnings we are supposed
3987 to emit, and then handle them all at the end of the
3991 struct elf_link_hash_entry
*h
;
3993 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3995 /* FIXME: What about bfd_link_hash_common? */
3997 && (h
->root
.type
== bfd_link_hash_defined
3998 || h
->root
.type
== bfd_link_hash_defweak
))
4003 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
4007 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
4012 if (! (_bfd_generic_link_add_one_symbol
4013 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
4014 FALSE
, bed
->collect
, NULL
)))
4017 if (bfd_link_executable (info
))
4019 /* Clobber the section size so that the warning does
4020 not get copied into the output file. */
4023 /* Also set SEC_EXCLUDE, so that symbols defined in
4024 the warning section don't get copied to the output. */
4025 s
->flags
|= SEC_EXCLUDE
;
4030 just_syms
= ((s
= abfd
->sections
) != NULL
4031 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
4036 /* If we are creating a shared library, create all the dynamic
4037 sections immediately. We need to attach them to something,
4038 so we attach them to this BFD, provided it is the right
4039 format and is not from ld --just-symbols. Always create the
4040 dynamic sections for -E/--dynamic-list. FIXME: If there
4041 are no input BFD's of the same format as the output, we can't
4042 make a shared library. */
4044 && (bfd_link_pic (info
)
4045 || (!bfd_link_relocatable (info
)
4047 && (info
->export_dynamic
|| info
->dynamic
)))
4048 && is_elf_hash_table (htab
)
4049 && info
->output_bfd
->xvec
== abfd
->xvec
4050 && !htab
->dynamic_sections_created
)
4052 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
4056 else if (!is_elf_hash_table (htab
))
4060 const char *soname
= NULL
;
4062 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
4063 const Elf_Internal_Phdr
*phdr
;
4064 struct elf_link_loaded_list
*loaded_lib
;
4066 /* ld --just-symbols and dynamic objects don't mix very well.
4067 ld shouldn't allow it. */
4071 /* If this dynamic lib was specified on the command line with
4072 --as-needed in effect, then we don't want to add a DT_NEEDED
4073 tag unless the lib is actually used. Similary for libs brought
4074 in by another lib's DT_NEEDED. When --no-add-needed is used
4075 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4076 any dynamic library in DT_NEEDED tags in the dynamic lib at
4078 add_needed
= (elf_dyn_lib_class (abfd
)
4079 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
4080 | DYN_NO_NEEDED
)) == 0;
4082 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4087 unsigned int elfsec
;
4088 unsigned long shlink
;
4090 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
4097 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
4098 if (elfsec
== SHN_BAD
)
4099 goto error_free_dyn
;
4100 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
4102 for (extdyn
= dynbuf
;
4103 extdyn
<= dynbuf
+ s
->size
- bed
->s
->sizeof_dyn
;
4104 extdyn
+= bed
->s
->sizeof_dyn
)
4106 Elf_Internal_Dyn dyn
;
4108 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
4109 if (dyn
.d_tag
== DT_SONAME
)
4111 unsigned int tagv
= dyn
.d_un
.d_val
;
4112 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4114 goto error_free_dyn
;
4116 if (dyn
.d_tag
== DT_NEEDED
)
4118 struct bfd_link_needed_list
*n
, **pn
;
4120 unsigned int tagv
= dyn
.d_un
.d_val
;
4121 size_t amt
= sizeof (struct bfd_link_needed_list
);
4123 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4124 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4125 if (n
== NULL
|| fnm
== NULL
)
4126 goto error_free_dyn
;
4127 amt
= strlen (fnm
) + 1;
4128 anm
= (char *) bfd_alloc (abfd
, amt
);
4130 goto error_free_dyn
;
4131 memcpy (anm
, fnm
, amt
);
4135 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4139 if (dyn
.d_tag
== DT_RUNPATH
)
4141 struct bfd_link_needed_list
*n
, **pn
;
4143 unsigned int tagv
= dyn
.d_un
.d_val
;
4144 size_t amt
= sizeof (struct bfd_link_needed_list
);
4146 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4147 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4148 if (n
== NULL
|| fnm
== NULL
)
4149 goto error_free_dyn
;
4150 amt
= strlen (fnm
) + 1;
4151 anm
= (char *) bfd_alloc (abfd
, amt
);
4153 goto error_free_dyn
;
4154 memcpy (anm
, fnm
, amt
);
4158 for (pn
= & runpath
;
4164 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4165 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
4167 struct bfd_link_needed_list
*n
, **pn
;
4169 unsigned int tagv
= dyn
.d_un
.d_val
;
4170 size_t amt
= sizeof (struct bfd_link_needed_list
);
4172 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4173 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4174 if (n
== NULL
|| fnm
== NULL
)
4175 goto error_free_dyn
;
4176 amt
= strlen (fnm
) + 1;
4177 anm
= (char *) bfd_alloc (abfd
, amt
);
4179 goto error_free_dyn
;
4180 memcpy (anm
, fnm
, amt
);
4190 if (dyn
.d_tag
== DT_AUDIT
)
4192 unsigned int tagv
= dyn
.d_un
.d_val
;
4193 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4200 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4201 frees all more recently bfd_alloc'd blocks as well. */
4207 struct bfd_link_needed_list
**pn
;
4208 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4213 /* If we have a PT_GNU_RELRO program header, mark as read-only
4214 all sections contained fully therein. This makes relro
4215 shared library sections appear as they will at run-time. */
4216 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4217 while (phdr
-- > elf_tdata (abfd
)->phdr
)
4218 if (phdr
->p_type
== PT_GNU_RELRO
)
4220 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4222 unsigned int opb
= bfd_octets_per_byte (abfd
, s
);
4224 if ((s
->flags
& SEC_ALLOC
) != 0
4225 && s
->vma
* opb
>= phdr
->p_vaddr
4226 && s
->vma
* opb
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4227 s
->flags
|= SEC_READONLY
;
4232 /* We do not want to include any of the sections in a dynamic
4233 object in the output file. We hack by simply clobbering the
4234 list of sections in the BFD. This could be handled more
4235 cleanly by, say, a new section flag; the existing
4236 SEC_NEVER_LOAD flag is not the one we want, because that one
4237 still implies that the section takes up space in the output
4239 bfd_section_list_clear (abfd
);
4241 /* Find the name to use in a DT_NEEDED entry that refers to this
4242 object. If the object has a DT_SONAME entry, we use it.
4243 Otherwise, if the generic linker stuck something in
4244 elf_dt_name, we use that. Otherwise, we just use the file
4246 if (soname
== NULL
|| *soname
== '\0')
4248 soname
= elf_dt_name (abfd
);
4249 if (soname
== NULL
|| *soname
== '\0')
4250 soname
= bfd_get_filename (abfd
);
4253 /* Save the SONAME because sometimes the linker emulation code
4254 will need to know it. */
4255 elf_dt_name (abfd
) = soname
;
4257 /* If we have already included this dynamic object in the
4258 link, just ignore it. There is no reason to include a
4259 particular dynamic object more than once. */
4260 for (loaded_lib
= htab
->dyn_loaded
;
4262 loaded_lib
= loaded_lib
->next
)
4264 if (strcmp (elf_dt_name (loaded_lib
->abfd
), soname
) == 0)
4268 /* Create dynamic sections for backends that require that be done
4269 before setup_gnu_properties. */
4271 && !_bfd_elf_link_create_dynamic_sections (abfd
, info
))
4274 /* Save the DT_AUDIT entry for the linker emulation code. */
4275 elf_dt_audit (abfd
) = audit
;
4278 /* If this is a dynamic object, we always link against the .dynsym
4279 symbol table, not the .symtab symbol table. The dynamic linker
4280 will only see the .dynsym symbol table, so there is no reason to
4281 look at .symtab for a dynamic object. */
4283 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4284 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4286 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4288 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4290 /* The sh_info field of the symtab header tells us where the
4291 external symbols start. We don't care about the local symbols at
4293 if (elf_bad_symtab (abfd
))
4295 extsymcount
= symcount
;
4300 extsymcount
= symcount
- hdr
->sh_info
;
4301 extsymoff
= hdr
->sh_info
;
4304 sym_hash
= elf_sym_hashes (abfd
);
4305 if (extsymcount
!= 0)
4307 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4309 if (isymbuf
== NULL
)
4312 if (sym_hash
== NULL
)
4314 /* We store a pointer to the hash table entry for each
4316 size_t amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4317 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4318 if (sym_hash
== NULL
)
4319 goto error_free_sym
;
4320 elf_sym_hashes (abfd
) = sym_hash
;
4326 /* Read in any version definitions. */
4327 if (!_bfd_elf_slurp_version_tables (abfd
,
4328 info
->default_imported_symver
))
4329 goto error_free_sym
;
4331 /* Read in the symbol versions, but don't bother to convert them
4332 to internal format. */
4333 if (elf_dynversym (abfd
) != 0)
4335 Elf_Internal_Shdr
*versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4336 bfd_size_type amt
= versymhdr
->sh_size
;
4338 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0)
4339 goto error_free_sym
;
4340 extversym
= (Elf_External_Versym
*)
4341 _bfd_malloc_and_read (abfd
, amt
, amt
);
4342 if (extversym
== NULL
)
4343 goto error_free_sym
;
4344 extversym_end
= extversym
+ amt
/ sizeof (*extversym
);
4348 /* If we are loading an as-needed shared lib, save the symbol table
4349 state before we start adding symbols. If the lib turns out
4350 to be unneeded, restore the state. */
4351 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4356 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4358 struct bfd_hash_entry
*p
;
4359 struct elf_link_hash_entry
*h
;
4361 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4363 h
= (struct elf_link_hash_entry
*) p
;
4364 entsize
+= htab
->root
.table
.entsize
;
4365 if (h
->root
.type
== bfd_link_hash_warning
)
4366 entsize
+= htab
->root
.table
.entsize
;
4370 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4371 old_tab
= bfd_malloc (tabsize
+ entsize
);
4372 if (old_tab
== NULL
)
4373 goto error_free_vers
;
4375 /* Remember the current objalloc pointer, so that all mem for
4376 symbols added can later be reclaimed. */
4377 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4378 if (alloc_mark
== NULL
)
4379 goto error_free_vers
;
4381 /* Make a special call to the linker "notice" function to
4382 tell it that we are about to handle an as-needed lib. */
4383 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4384 goto error_free_vers
;
4386 /* Clone the symbol table. Remember some pointers into the
4387 symbol table, and dynamic symbol count. */
4388 old_ent
= (char *) old_tab
+ tabsize
;
4389 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4390 old_undefs
= htab
->root
.undefs
;
4391 old_undefs_tail
= htab
->root
.undefs_tail
;
4392 old_table
= htab
->root
.table
.table
;
4393 old_size
= htab
->root
.table
.size
;
4394 old_count
= htab
->root
.table
.count
;
4396 if (htab
->dynstr
!= NULL
)
4398 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4399 if (old_strtab
== NULL
)
4400 goto error_free_vers
;
4403 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4405 struct bfd_hash_entry
*p
;
4406 struct elf_link_hash_entry
*h
;
4408 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4410 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
4411 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4412 h
= (struct elf_link_hash_entry
*) p
;
4413 if (h
->root
.type
== bfd_link_hash_warning
)
4415 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
4416 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4423 if (extversym
== NULL
)
4425 else if (extversym
+ extsymoff
< extversym_end
)
4426 ever
= extversym
+ extsymoff
;
4429 /* xgettext:c-format */
4430 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4431 abfd
, (long) extsymoff
,
4432 (long) (extversym_end
- extversym
) / sizeof (* extversym
));
4433 bfd_set_error (bfd_error_bad_value
);
4434 goto error_free_vers
;
4437 if (!bfd_link_relocatable (info
)
4438 && abfd
->lto_slim_object
)
4441 (_("%pB: plugin needed to handle lto object"), abfd
);
4444 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
4446 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4450 asection
*sec
, *new_sec
;
4453 struct elf_link_hash_entry
*h
;
4454 struct elf_link_hash_entry
*hi
;
4455 bfd_boolean definition
;
4456 bfd_boolean size_change_ok
;
4457 bfd_boolean type_change_ok
;
4458 bfd_boolean new_weak
;
4459 bfd_boolean old_weak
;
4460 bfd_boolean override
;
4462 bfd_boolean discarded
;
4463 unsigned int old_alignment
;
4464 unsigned int shindex
;
4466 bfd_boolean matched
;
4470 flags
= BSF_NO_FLAGS
;
4472 value
= isym
->st_value
;
4473 common
= bed
->common_definition (isym
);
4474 if (common
&& info
->inhibit_common_definition
)
4476 /* Treat common symbol as undefined for --no-define-common. */
4477 isym
->st_shndx
= SHN_UNDEF
;
4482 bind
= ELF_ST_BIND (isym
->st_info
);
4486 /* This should be impossible, since ELF requires that all
4487 global symbols follow all local symbols, and that sh_info
4488 point to the first global symbol. Unfortunately, Irix 5
4490 if (elf_bad_symtab (abfd
))
4493 /* If we aren't prepared to handle locals within the globals
4494 then we'll likely segfault on a NULL symbol hash if the
4495 symbol is ever referenced in relocations. */
4496 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
4497 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, hdr
->sh_name
);
4498 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4499 " (>= sh_info of %lu)"),
4500 abfd
, name
, (long) (isym
- isymbuf
+ extsymoff
),
4503 /* Dynamic object relocations are not processed by ld, so
4504 ld won't run into the problem mentioned above. */
4507 bfd_set_error (bfd_error_bad_value
);
4508 goto error_free_vers
;
4511 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4519 case STB_GNU_UNIQUE
:
4520 flags
= BSF_GNU_UNIQUE
;
4524 /* Leave it up to the processor backend. */
4528 if (isym
->st_shndx
== SHN_UNDEF
)
4529 sec
= bfd_und_section_ptr
;
4530 else if (isym
->st_shndx
== SHN_ABS
)
4531 sec
= bfd_abs_section_ptr
;
4532 else if (isym
->st_shndx
== SHN_COMMON
)
4534 sec
= bfd_com_section_ptr
;
4535 /* What ELF calls the size we call the value. What ELF
4536 calls the value we call the alignment. */
4537 value
= isym
->st_size
;
4541 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4543 sec
= bfd_abs_section_ptr
;
4544 else if (discarded_section (sec
))
4546 /* Symbols from discarded section are undefined. We keep
4548 sec
= bfd_und_section_ptr
;
4550 isym
->st_shndx
= SHN_UNDEF
;
4552 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4556 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4559 goto error_free_vers
;
4561 if (isym
->st_shndx
== SHN_COMMON
4562 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4564 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4568 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4570 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4572 goto error_free_vers
;
4576 else if (isym
->st_shndx
== SHN_COMMON
4577 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4578 && !bfd_link_relocatable (info
))
4580 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4584 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4585 | SEC_LINKER_CREATED
);
4586 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4588 goto error_free_vers
;
4592 else if (bed
->elf_add_symbol_hook
)
4594 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4596 goto error_free_vers
;
4598 /* The hook function sets the name to NULL if this symbol
4599 should be skipped for some reason. */
4604 /* Sanity check that all possibilities were handled. */
4608 /* Silently discard TLS symbols from --just-syms. There's
4609 no way to combine a static TLS block with a new TLS block
4610 for this executable. */
4611 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4612 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4615 if (bfd_is_und_section (sec
)
4616 || bfd_is_com_section (sec
))
4621 size_change_ok
= FALSE
;
4622 type_change_ok
= bed
->type_change_ok
;
4629 if (is_elf_hash_table (htab
))
4631 Elf_Internal_Versym iver
;
4632 unsigned int vernum
= 0;
4637 if (info
->default_imported_symver
)
4638 /* Use the default symbol version created earlier. */
4639 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4643 else if (ever
>= extversym_end
)
4645 /* xgettext:c-format */
4646 _bfd_error_handler (_("%pB: not enough version information"),
4648 bfd_set_error (bfd_error_bad_value
);
4649 goto error_free_vers
;
4652 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4654 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4656 /* If this is a hidden symbol, or if it is not version
4657 1, we append the version name to the symbol name.
4658 However, we do not modify a non-hidden absolute symbol
4659 if it is not a function, because it might be the version
4660 symbol itself. FIXME: What if it isn't? */
4661 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4663 && (!bfd_is_abs_section (sec
)
4664 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4667 size_t namelen
, verlen
, newlen
;
4670 if (isym
->st_shndx
!= SHN_UNDEF
)
4672 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4674 else if (vernum
> 1)
4676 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4683 /* xgettext:c-format */
4684 (_("%pB: %s: invalid version %u (max %d)"),
4686 elf_tdata (abfd
)->cverdefs
);
4687 bfd_set_error (bfd_error_bad_value
);
4688 goto error_free_vers
;
4693 /* We cannot simply test for the number of
4694 entries in the VERNEED section since the
4695 numbers for the needed versions do not start
4697 Elf_Internal_Verneed
*t
;
4700 for (t
= elf_tdata (abfd
)->verref
;
4704 Elf_Internal_Vernaux
*a
;
4706 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4708 if (a
->vna_other
== vernum
)
4710 verstr
= a
->vna_nodename
;
4720 /* xgettext:c-format */
4721 (_("%pB: %s: invalid needed version %d"),
4722 abfd
, name
, vernum
);
4723 bfd_set_error (bfd_error_bad_value
);
4724 goto error_free_vers
;
4728 namelen
= strlen (name
);
4729 verlen
= strlen (verstr
);
4730 newlen
= namelen
+ verlen
+ 2;
4731 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4732 && isym
->st_shndx
!= SHN_UNDEF
)
4735 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4736 if (newname
== NULL
)
4737 goto error_free_vers
;
4738 memcpy (newname
, name
, namelen
);
4739 p
= newname
+ namelen
;
4741 /* If this is a defined non-hidden version symbol,
4742 we add another @ to the name. This indicates the
4743 default version of the symbol. */
4744 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4745 && isym
->st_shndx
!= SHN_UNDEF
)
4747 memcpy (p
, verstr
, verlen
+ 1);
4752 /* If this symbol has default visibility and the user has
4753 requested we not re-export it, then mark it as hidden. */
4754 if (!bfd_is_und_section (sec
)
4757 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4758 isym
->st_other
= (STV_HIDDEN
4759 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4761 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4762 sym_hash
, &old_bfd
, &old_weak
,
4763 &old_alignment
, &skip
, &override
,
4764 &type_change_ok
, &size_change_ok
,
4766 goto error_free_vers
;
4771 /* Override a definition only if the new symbol matches the
4773 if (override
&& matched
)
4777 while (h
->root
.type
== bfd_link_hash_indirect
4778 || h
->root
.type
== bfd_link_hash_warning
)
4779 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4781 if (elf_tdata (abfd
)->verdef
!= NULL
4784 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4787 if (! (_bfd_generic_link_add_one_symbol
4788 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4789 (struct bfd_link_hash_entry
**) sym_hash
)))
4790 goto error_free_vers
;
4793 /* We need to make sure that indirect symbol dynamic flags are
4796 while (h
->root
.type
== bfd_link_hash_indirect
4797 || h
->root
.type
== bfd_link_hash_warning
)
4798 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4800 /* Setting the index to -3 tells elf_link_output_extsym that
4801 this symbol is defined in a discarded section. */
4807 new_weak
= (flags
& BSF_WEAK
) != 0;
4811 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4812 && is_elf_hash_table (htab
)
4813 && h
->u
.alias
== NULL
)
4815 /* Keep a list of all weak defined non function symbols from
4816 a dynamic object, using the alias field. Later in this
4817 function we will set the alias field to the correct
4818 value. We only put non-function symbols from dynamic
4819 objects on this list, because that happens to be the only
4820 time we need to know the normal symbol corresponding to a
4821 weak symbol, and the information is time consuming to
4822 figure out. If the alias field is not already NULL,
4823 then this symbol was already defined by some previous
4824 dynamic object, and we will be using that previous
4825 definition anyhow. */
4831 /* Set the alignment of a common symbol. */
4832 if ((common
|| bfd_is_com_section (sec
))
4833 && h
->root
.type
== bfd_link_hash_common
)
4838 align
= bfd_log2 (isym
->st_value
);
4841 /* The new symbol is a common symbol in a shared object.
4842 We need to get the alignment from the section. */
4843 align
= new_sec
->alignment_power
;
4845 if (align
> old_alignment
)
4846 h
->root
.u
.c
.p
->alignment_power
= align
;
4848 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4851 if (is_elf_hash_table (htab
))
4853 /* Set a flag in the hash table entry indicating the type of
4854 reference or definition we just found. A dynamic symbol
4855 is one which is referenced or defined by both a regular
4856 object and a shared object. */
4857 bfd_boolean dynsym
= FALSE
;
4859 /* Plugin symbols aren't normal. Don't set def_regular or
4860 ref_regular for them, or make them dynamic. */
4861 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4868 if (bind
!= STB_WEAK
)
4869 h
->ref_regular_nonweak
= 1;
4881 /* If the indirect symbol has been forced local, don't
4882 make the real symbol dynamic. */
4883 if ((h
== hi
|| !hi
->forced_local
)
4884 && (bfd_link_dll (info
)
4894 hi
->ref_dynamic
= 1;
4899 hi
->def_dynamic
= 1;
4902 /* If the indirect symbol has been forced local, don't
4903 make the real symbol dynamic. */
4904 if ((h
== hi
|| !hi
->forced_local
)
4908 && weakdef (h
)->dynindx
!= -1)))
4912 /* Check to see if we need to add an indirect symbol for
4913 the default name. */
4915 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
4916 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4917 sec
, value
, &old_bfd
, &dynsym
))
4918 goto error_free_vers
;
4920 /* Check the alignment when a common symbol is involved. This
4921 can change when a common symbol is overridden by a normal
4922 definition or a common symbol is ignored due to the old
4923 normal definition. We need to make sure the maximum
4924 alignment is maintained. */
4925 if ((old_alignment
|| common
)
4926 && h
->root
.type
!= bfd_link_hash_common
)
4928 unsigned int common_align
;
4929 unsigned int normal_align
;
4930 unsigned int symbol_align
;
4934 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
4935 || h
->root
.type
== bfd_link_hash_defweak
);
4937 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4938 if (h
->root
.u
.def
.section
->owner
!= NULL
4939 && (h
->root
.u
.def
.section
->owner
->flags
4940 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
4942 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4943 if (normal_align
> symbol_align
)
4944 normal_align
= symbol_align
;
4947 normal_align
= symbol_align
;
4951 common_align
= old_alignment
;
4952 common_bfd
= old_bfd
;
4957 common_align
= bfd_log2 (isym
->st_value
);
4959 normal_bfd
= old_bfd
;
4962 if (normal_align
< common_align
)
4964 /* PR binutils/2735 */
4965 if (normal_bfd
== NULL
)
4967 /* xgettext:c-format */
4968 (_("warning: alignment %u of common symbol `%s' in %pB is"
4969 " greater than the alignment (%u) of its section %pA"),
4970 1 << common_align
, name
, common_bfd
,
4971 1 << normal_align
, h
->root
.u
.def
.section
);
4974 /* xgettext:c-format */
4975 (_("warning: alignment %u of symbol `%s' in %pB"
4976 " is smaller than %u in %pB"),
4977 1 << normal_align
, name
, normal_bfd
,
4978 1 << common_align
, common_bfd
);
4982 /* Remember the symbol size if it isn't undefined. */
4983 if (isym
->st_size
!= 0
4984 && isym
->st_shndx
!= SHN_UNDEF
4985 && (definition
|| h
->size
== 0))
4988 && h
->size
!= isym
->st_size
4989 && ! size_change_ok
)
4991 /* xgettext:c-format */
4992 (_("warning: size of symbol `%s' changed"
4993 " from %" PRIu64
" in %pB to %" PRIu64
" in %pB"),
4994 name
, (uint64_t) h
->size
, old_bfd
,
4995 (uint64_t) isym
->st_size
, abfd
);
4997 h
->size
= isym
->st_size
;
5000 /* If this is a common symbol, then we always want H->SIZE
5001 to be the size of the common symbol. The code just above
5002 won't fix the size if a common symbol becomes larger. We
5003 don't warn about a size change here, because that is
5004 covered by --warn-common. Allow changes between different
5006 if (h
->root
.type
== bfd_link_hash_common
)
5007 h
->size
= h
->root
.u
.c
.size
;
5009 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
5010 && ((definition
&& !new_weak
)
5011 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
5012 || h
->type
== STT_NOTYPE
))
5014 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
5016 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5018 if (type
== STT_GNU_IFUNC
5019 && (abfd
->flags
& DYNAMIC
) != 0)
5022 if (h
->type
!= type
)
5024 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
5025 /* xgettext:c-format */
5027 (_("warning: type of symbol `%s' changed"
5028 " from %d to %d in %pB"),
5029 name
, h
->type
, type
, abfd
);
5035 /* Merge st_other field. */
5036 elf_merge_st_other (abfd
, h
, isym
, sec
, definition
, dynamic
);
5038 /* We don't want to make debug symbol dynamic. */
5040 && (sec
->flags
& SEC_DEBUGGING
)
5041 && !bfd_link_relocatable (info
))
5044 /* Nor should we make plugin symbols dynamic. */
5045 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5050 h
->target_internal
= isym
->st_target_internal
;
5051 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
5054 if (definition
&& !dynamic
)
5056 char *p
= strchr (name
, ELF_VER_CHR
);
5057 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
5059 /* Queue non-default versions so that .symver x, x@FOO
5060 aliases can be checked. */
5063 size_t amt
= ((isymend
- isym
+ 1)
5064 * sizeof (struct elf_link_hash_entry
*));
5066 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5068 goto error_free_vers
;
5070 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
5074 if (dynsym
&& h
->dynindx
== -1)
5076 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5077 goto error_free_vers
;
5079 && weakdef (h
)->dynindx
== -1)
5081 if (!bfd_elf_link_record_dynamic_symbol (info
, weakdef (h
)))
5082 goto error_free_vers
;
5085 else if (h
->dynindx
!= -1)
5086 /* If the symbol already has a dynamic index, but
5087 visibility says it should not be visible, turn it into
5089 switch (ELF_ST_VISIBILITY (h
->other
))
5093 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
5098 /* Don't add DT_NEEDED for references from the dummy bfd nor
5099 for unmatched symbol. */
5104 && h
->ref_regular_nonweak
5106 || (old_bfd
->flags
& BFD_PLUGIN
) == 0))
5107 || (h
->ref_dynamic_nonweak
5108 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5109 && !on_needed_list (elf_dt_name (abfd
),
5110 htab
->needed
, NULL
))))
5112 const char *soname
= elf_dt_name (abfd
);
5114 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
5115 h
->root
.root
.string
);
5117 /* A symbol from a library loaded via DT_NEEDED of some
5118 other library is referenced by a regular object.
5119 Add a DT_NEEDED entry for it. Issue an error if
5120 --no-add-needed is used and the reference was not
5123 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
5126 /* xgettext:c-format */
5127 (_("%pB: undefined reference to symbol '%s'"),
5129 bfd_set_error (bfd_error_missing_dso
);
5130 goto error_free_vers
;
5133 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
5134 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
5136 /* Create dynamic sections for backends that require
5137 that be done before setup_gnu_properties. */
5138 if (!_bfd_elf_link_create_dynamic_sections (abfd
, info
))
5145 if (info
->lto_plugin_active
5146 && !bfd_link_relocatable (info
)
5147 && (abfd
->flags
& BFD_PLUGIN
) == 0
5153 if (bed
->s
->arch_size
== 32)
5158 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5159 referenced in regular objects so that linker plugin will get
5160 the correct symbol resolution. */
5162 sym_hash
= elf_sym_hashes (abfd
);
5163 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5165 Elf_Internal_Rela
*internal_relocs
;
5166 Elf_Internal_Rela
*rel
, *relend
;
5168 /* Don't check relocations in excluded sections. */
5169 if ((s
->flags
& SEC_RELOC
) == 0
5170 || s
->reloc_count
== 0
5171 || (s
->flags
& SEC_EXCLUDE
) != 0
5172 || ((info
->strip
== strip_all
5173 || info
->strip
== strip_debugger
)
5174 && (s
->flags
& SEC_DEBUGGING
) != 0))
5177 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, s
, NULL
,
5180 if (internal_relocs
== NULL
)
5181 goto error_free_vers
;
5183 rel
= internal_relocs
;
5184 relend
= rel
+ s
->reloc_count
;
5185 for ( ; rel
< relend
; rel
++)
5187 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
5188 struct elf_link_hash_entry
*h
;
5190 /* Skip local symbols. */
5191 if (r_symndx
< extsymoff
)
5194 h
= sym_hash
[r_symndx
- extsymoff
];
5196 h
->root
.non_ir_ref_regular
= 1;
5199 if (elf_section_data (s
)->relocs
!= internal_relocs
)
5200 free (internal_relocs
);
5204 if (extversym
!= NULL
)
5210 if (isymbuf
!= NULL
)
5216 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
5220 /* Restore the symbol table. */
5221 old_ent
= (char *) old_tab
+ tabsize
;
5222 memset (elf_sym_hashes (abfd
), 0,
5223 extsymcount
* sizeof (struct elf_link_hash_entry
*));
5224 htab
->root
.table
.table
= old_table
;
5225 htab
->root
.table
.size
= old_size
;
5226 htab
->root
.table
.count
= old_count
;
5227 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
5228 htab
->root
.undefs
= old_undefs
;
5229 htab
->root
.undefs_tail
= old_undefs_tail
;
5230 if (htab
->dynstr
!= NULL
)
5231 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
5234 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
5236 struct bfd_hash_entry
*p
;
5237 struct elf_link_hash_entry
*h
;
5239 unsigned int alignment_power
;
5240 unsigned int non_ir_ref_dynamic
;
5242 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
5244 h
= (struct elf_link_hash_entry
*) p
;
5245 if (h
->root
.type
== bfd_link_hash_warning
)
5246 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5248 /* Preserve the maximum alignment and size for common
5249 symbols even if this dynamic lib isn't on DT_NEEDED
5250 since it can still be loaded at run time by another
5252 if (h
->root
.type
== bfd_link_hash_common
)
5254 size
= h
->root
.u
.c
.size
;
5255 alignment_power
= h
->root
.u
.c
.p
->alignment_power
;
5260 alignment_power
= 0;
5262 /* Preserve non_ir_ref_dynamic so that this symbol
5263 will be exported when the dynamic lib becomes needed
5264 in the second pass. */
5265 non_ir_ref_dynamic
= h
->root
.non_ir_ref_dynamic
;
5266 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
5267 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5268 h
= (struct elf_link_hash_entry
*) p
;
5269 if (h
->root
.type
== bfd_link_hash_warning
)
5271 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
5272 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5273 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5275 if (h
->root
.type
== bfd_link_hash_common
)
5277 if (size
> h
->root
.u
.c
.size
)
5278 h
->root
.u
.c
.size
= size
;
5279 if (alignment_power
> h
->root
.u
.c
.p
->alignment_power
)
5280 h
->root
.u
.c
.p
->alignment_power
= alignment_power
;
5282 h
->root
.non_ir_ref_dynamic
= non_ir_ref_dynamic
;
5286 /* Make a special call to the linker "notice" function to
5287 tell it that symbols added for crefs may need to be removed. */
5288 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
5289 goto error_free_vers
;
5292 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
5294 if (nondeflt_vers
!= NULL
)
5295 free (nondeflt_vers
);
5299 if (old_tab
!= NULL
)
5301 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
5302 goto error_free_vers
;
5307 /* Now that all the symbols from this input file are created, if
5308 not performing a relocatable link, handle .symver foo, foo@BAR
5309 such that any relocs against foo become foo@BAR. */
5310 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5314 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5316 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5317 char *shortname
, *p
;
5320 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5322 || (h
->root
.type
!= bfd_link_hash_defined
5323 && h
->root
.type
!= bfd_link_hash_defweak
))
5326 amt
= p
- h
->root
.root
.string
;
5327 shortname
= (char *) bfd_malloc (amt
+ 1);
5329 goto error_free_vers
;
5330 memcpy (shortname
, h
->root
.root
.string
, amt
);
5331 shortname
[amt
] = '\0';
5333 hi
= (struct elf_link_hash_entry
*)
5334 bfd_link_hash_lookup (&htab
->root
, shortname
,
5335 FALSE
, FALSE
, FALSE
);
5337 && hi
->root
.type
== h
->root
.type
5338 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5339 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5341 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
5342 hi
->root
.type
= bfd_link_hash_indirect
;
5343 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5344 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5345 sym_hash
= elf_sym_hashes (abfd
);
5347 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5348 if (sym_hash
[symidx
] == hi
)
5350 sym_hash
[symidx
] = h
;
5356 free (nondeflt_vers
);
5357 nondeflt_vers
= NULL
;
5360 /* Now set the alias field correctly for all the weak defined
5361 symbols we found. The only way to do this is to search all the
5362 symbols. Since we only need the information for non functions in
5363 dynamic objects, that's the only time we actually put anything on
5364 the list WEAKS. We need this information so that if a regular
5365 object refers to a symbol defined weakly in a dynamic object, the
5366 real symbol in the dynamic object is also put in the dynamic
5367 symbols; we also must arrange for both symbols to point to the
5368 same memory location. We could handle the general case of symbol
5369 aliasing, but a general symbol alias can only be generated in
5370 assembler code, handling it correctly would be very time
5371 consuming, and other ELF linkers don't handle general aliasing
5375 struct elf_link_hash_entry
**hpp
;
5376 struct elf_link_hash_entry
**hppend
;
5377 struct elf_link_hash_entry
**sorted_sym_hash
;
5378 struct elf_link_hash_entry
*h
;
5379 size_t sym_count
, amt
;
5381 /* Since we have to search the whole symbol list for each weak
5382 defined symbol, search time for N weak defined symbols will be
5383 O(N^2). Binary search will cut it down to O(NlogN). */
5384 amt
= extsymcount
* sizeof (*sorted_sym_hash
);
5385 sorted_sym_hash
= bfd_malloc (amt
);
5386 if (sorted_sym_hash
== NULL
)
5388 sym_hash
= sorted_sym_hash
;
5389 hpp
= elf_sym_hashes (abfd
);
5390 hppend
= hpp
+ extsymcount
;
5392 for (; hpp
< hppend
; hpp
++)
5396 && h
->root
.type
== bfd_link_hash_defined
5397 && !bed
->is_function_type (h
->type
))
5405 qsort (sorted_sym_hash
, sym_count
, sizeof (*sorted_sym_hash
),
5408 while (weaks
!= NULL
)
5410 struct elf_link_hash_entry
*hlook
;
5413 size_t i
, j
, idx
= 0;
5416 weaks
= hlook
->u
.alias
;
5417 hlook
->u
.alias
= NULL
;
5419 if (hlook
->root
.type
!= bfd_link_hash_defined
5420 && hlook
->root
.type
!= bfd_link_hash_defweak
)
5423 slook
= hlook
->root
.u
.def
.section
;
5424 vlook
= hlook
->root
.u
.def
.value
;
5430 bfd_signed_vma vdiff
;
5432 h
= sorted_sym_hash
[idx
];
5433 vdiff
= vlook
- h
->root
.u
.def
.value
;
5440 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5450 /* We didn't find a value/section match. */
5454 /* With multiple aliases, or when the weak symbol is already
5455 strongly defined, we have multiple matching symbols and
5456 the binary search above may land on any of them. Step
5457 one past the matching symbol(s). */
5460 h
= sorted_sym_hash
[idx
];
5461 if (h
->root
.u
.def
.section
!= slook
5462 || h
->root
.u
.def
.value
!= vlook
)
5466 /* Now look back over the aliases. Since we sorted by size
5467 as well as value and section, we'll choose the one with
5468 the largest size. */
5471 h
= sorted_sym_hash
[idx
];
5473 /* Stop if value or section doesn't match. */
5474 if (h
->root
.u
.def
.section
!= slook
5475 || h
->root
.u
.def
.value
!= vlook
)
5477 else if (h
!= hlook
)
5479 struct elf_link_hash_entry
*t
;
5482 hlook
->is_weakalias
= 1;
5484 if (t
->u
.alias
!= NULL
)
5485 while (t
->u
.alias
!= h
)
5489 /* If the weak definition is in the list of dynamic
5490 symbols, make sure the real definition is put
5492 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5494 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5497 free (sorted_sym_hash
);
5502 /* If the real definition is in the list of dynamic
5503 symbols, make sure the weak definition is put
5504 there as well. If we don't do this, then the
5505 dynamic loader might not merge the entries for the
5506 real definition and the weak definition. */
5507 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5509 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5510 goto err_free_sym_hash
;
5517 free (sorted_sym_hash
);
5520 if (bed
->check_directives
5521 && !(*bed
->check_directives
) (abfd
, info
))
5524 /* If this is a non-traditional link, try to optimize the handling
5525 of the .stab/.stabstr sections. */
5527 && ! info
->traditional_format
5528 && is_elf_hash_table (htab
)
5529 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5533 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5534 if (stabstr
!= NULL
)
5536 bfd_size_type string_offset
= 0;
5539 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5540 if (CONST_STRNEQ (stab
->name
, ".stab")
5541 && (!stab
->name
[5] ||
5542 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5543 && (stab
->flags
& SEC_MERGE
) == 0
5544 && !bfd_is_abs_section (stab
->output_section
))
5546 struct bfd_elf_section_data
*secdata
;
5548 secdata
= elf_section_data (stab
);
5549 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5550 stabstr
, &secdata
->sec_info
,
5553 if (secdata
->sec_info
)
5554 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5559 if (dynamic
&& add_needed
)
5561 /* Add this bfd to the loaded list. */
5562 struct elf_link_loaded_list
*n
;
5564 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5568 n
->next
= htab
->dyn_loaded
;
5569 htab
->dyn_loaded
= n
;
5571 if (dynamic
&& !add_needed
5572 && (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) != 0)
5573 elf_dyn_lib_class (abfd
) |= DYN_NO_NEEDED
;
5578 if (old_tab
!= NULL
)
5580 if (old_strtab
!= NULL
)
5582 if (nondeflt_vers
!= NULL
)
5583 free (nondeflt_vers
);
5584 if (extversym
!= NULL
)
5587 if (isymbuf
!= NULL
)
5593 /* Return the linker hash table entry of a symbol that might be
5594 satisfied by an archive symbol. Return -1 on error. */
5596 struct elf_link_hash_entry
*
5597 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5598 struct bfd_link_info
*info
,
5601 struct elf_link_hash_entry
*h
;
5605 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, TRUE
);
5609 /* If this is a default version (the name contains @@), look up the
5610 symbol again with only one `@' as well as without the version.
5611 The effect is that references to the symbol with and without the
5612 version will be matched by the default symbol in the archive. */
5614 p
= strchr (name
, ELF_VER_CHR
);
5615 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5618 /* First check with only one `@'. */
5619 len
= strlen (name
);
5620 copy
= (char *) bfd_alloc (abfd
, len
);
5622 return (struct elf_link_hash_entry
*) -1;
5624 first
= p
- name
+ 1;
5625 memcpy (copy
, name
, first
);
5626 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5628 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, TRUE
);
5631 /* We also need to check references to the symbol without the
5633 copy
[first
- 1] = '\0';
5634 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
5635 FALSE
, FALSE
, TRUE
);
5638 bfd_release (abfd
, copy
);
5642 /* Add symbols from an ELF archive file to the linker hash table. We
5643 don't use _bfd_generic_link_add_archive_symbols because we need to
5644 handle versioned symbols.
5646 Fortunately, ELF archive handling is simpler than that done by
5647 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5648 oddities. In ELF, if we find a symbol in the archive map, and the
5649 symbol is currently undefined, we know that we must pull in that
5652 Unfortunately, we do have to make multiple passes over the symbol
5653 table until nothing further is resolved. */
5656 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5659 unsigned char *included
= NULL
;
5663 const struct elf_backend_data
*bed
;
5664 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
5665 (bfd
*, struct bfd_link_info
*, const char *);
5667 if (! bfd_has_map (abfd
))
5669 /* An empty archive is a special case. */
5670 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5672 bfd_set_error (bfd_error_no_armap
);
5676 /* Keep track of all symbols we know to be already defined, and all
5677 files we know to be already included. This is to speed up the
5678 second and subsequent passes. */
5679 c
= bfd_ardata (abfd
)->symdef_count
;
5682 amt
= c
* sizeof (*included
);
5683 included
= (unsigned char *) bfd_zmalloc (amt
);
5684 if (included
== NULL
)
5687 symdefs
= bfd_ardata (abfd
)->symdefs
;
5688 bed
= get_elf_backend_data (abfd
);
5689 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5702 symdefend
= symdef
+ c
;
5703 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5705 struct elf_link_hash_entry
*h
;
5707 struct bfd_link_hash_entry
*undefs_tail
;
5712 if (symdef
->file_offset
== last
)
5718 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5719 if (h
== (struct elf_link_hash_entry
*) -1)
5725 if (h
->root
.type
== bfd_link_hash_common
)
5727 /* We currently have a common symbol. The archive map contains
5728 a reference to this symbol, so we may want to include it. We
5729 only want to include it however, if this archive element
5730 contains a definition of the symbol, not just another common
5733 Unfortunately some archivers (including GNU ar) will put
5734 declarations of common symbols into their archive maps, as
5735 well as real definitions, so we cannot just go by the archive
5736 map alone. Instead we must read in the element's symbol
5737 table and check that to see what kind of symbol definition
5739 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5742 else if (h
->root
.type
!= bfd_link_hash_undefined
)
5744 if (h
->root
.type
!= bfd_link_hash_undefweak
)
5745 /* Symbol must be defined. Don't check it again. */
5750 /* We need to include this archive member. */
5751 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5752 if (element
== NULL
)
5755 if (! bfd_check_format (element
, bfd_object
))
5758 undefs_tail
= info
->hash
->undefs_tail
;
5760 if (!(*info
->callbacks
5761 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5763 if (!bfd_link_add_symbols (element
, info
))
5766 /* If there are any new undefined symbols, we need to make
5767 another pass through the archive in order to see whether
5768 they can be defined. FIXME: This isn't perfect, because
5769 common symbols wind up on undefs_tail and because an
5770 undefined symbol which is defined later on in this pass
5771 does not require another pass. This isn't a bug, but it
5772 does make the code less efficient than it could be. */
5773 if (undefs_tail
!= info
->hash
->undefs_tail
)
5776 /* Look backward to mark all symbols from this object file
5777 which we have already seen in this pass. */
5781 included
[mark
] = TRUE
;
5786 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5788 /* We mark subsequent symbols from this object file as we go
5789 on through the loop. */
5790 last
= symdef
->file_offset
;
5800 if (included
!= NULL
)
5805 /* Given an ELF BFD, add symbols to the global hash table as
5809 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5811 switch (bfd_get_format (abfd
))
5814 return elf_link_add_object_symbols (abfd
, info
);
5816 return elf_link_add_archive_symbols (abfd
, info
);
5818 bfd_set_error (bfd_error_wrong_format
);
5823 struct hash_codes_info
5825 unsigned long *hashcodes
;
5829 /* This function will be called though elf_link_hash_traverse to store
5830 all hash value of the exported symbols in an array. */
5833 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5835 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
5840 /* Ignore indirect symbols. These are added by the versioning code. */
5841 if (h
->dynindx
== -1)
5844 name
= h
->root
.root
.string
;
5845 if (h
->versioned
>= versioned
)
5847 char *p
= strchr (name
, ELF_VER_CHR
);
5850 alc
= (char *) bfd_malloc (p
- name
+ 1);
5856 memcpy (alc
, name
, p
- name
);
5857 alc
[p
- name
] = '\0';
5862 /* Compute the hash value. */
5863 ha
= bfd_elf_hash (name
);
5865 /* Store the found hash value in the array given as the argument. */
5866 *(inf
->hashcodes
)++ = ha
;
5868 /* And store it in the struct so that we can put it in the hash table
5870 h
->u
.elf_hash_value
= ha
;
5878 struct collect_gnu_hash_codes
5881 const struct elf_backend_data
*bed
;
5882 unsigned long int nsyms
;
5883 unsigned long int maskbits
;
5884 unsigned long int *hashcodes
;
5885 unsigned long int *hashval
;
5886 unsigned long int *indx
;
5887 unsigned long int *counts
;
5891 long int min_dynindx
;
5892 unsigned long int bucketcount
;
5893 unsigned long int symindx
;
5894 long int local_indx
;
5895 long int shift1
, shift2
;
5896 unsigned long int mask
;
5900 /* This function will be called though elf_link_hash_traverse to store
5901 all hash value of the exported symbols in an array. */
5904 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5906 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5911 /* Ignore indirect symbols. These are added by the versioning code. */
5912 if (h
->dynindx
== -1)
5915 /* Ignore also local symbols and undefined symbols. */
5916 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5919 name
= h
->root
.root
.string
;
5920 if (h
->versioned
>= versioned
)
5922 char *p
= strchr (name
, ELF_VER_CHR
);
5925 alc
= (char *) bfd_malloc (p
- name
+ 1);
5931 memcpy (alc
, name
, p
- name
);
5932 alc
[p
- name
] = '\0';
5937 /* Compute the hash value. */
5938 ha
= bfd_elf_gnu_hash (name
);
5940 /* Store the found hash value in the array for compute_bucket_count,
5941 and also for .dynsym reordering purposes. */
5942 s
->hashcodes
[s
->nsyms
] = ha
;
5943 s
->hashval
[h
->dynindx
] = ha
;
5945 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5946 s
->min_dynindx
= h
->dynindx
;
5954 /* This function will be called though elf_link_hash_traverse to do
5955 final dynamic symbol renumbering in case of .gnu.hash.
5956 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
5957 to the translation table. */
5960 elf_gnu_hash_process_symidx (struct elf_link_hash_entry
*h
, void *data
)
5962 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5963 unsigned long int bucket
;
5964 unsigned long int val
;
5966 /* Ignore indirect symbols. */
5967 if (h
->dynindx
== -1)
5970 /* Ignore also local symbols and undefined symbols. */
5971 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5973 if (h
->dynindx
>= s
->min_dynindx
)
5975 if (s
->bed
->record_xhash_symbol
!= NULL
)
5977 (*s
->bed
->record_xhash_symbol
) (h
, 0);
5981 h
->dynindx
= s
->local_indx
++;
5986 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5987 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5988 & ((s
->maskbits
>> s
->shift1
) - 1);
5989 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5991 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5992 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5993 if (s
->counts
[bucket
] == 1)
5994 /* Last element terminates the chain. */
5996 bfd_put_32 (s
->output_bfd
, val
,
5997 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5998 --s
->counts
[bucket
];
5999 if (s
->bed
->record_xhash_symbol
!= NULL
)
6001 bfd_vma xlat_loc
= s
->xlat
+ (s
->indx
[bucket
]++ - s
->symindx
) * 4;
6003 (*s
->bed
->record_xhash_symbol
) (h
, xlat_loc
);
6006 h
->dynindx
= s
->indx
[bucket
]++;
6010 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6013 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
6015 return !(h
->forced_local
6016 || h
->root
.type
== bfd_link_hash_undefined
6017 || h
->root
.type
== bfd_link_hash_undefweak
6018 || ((h
->root
.type
== bfd_link_hash_defined
6019 || h
->root
.type
== bfd_link_hash_defweak
)
6020 && h
->root
.u
.def
.section
->output_section
== NULL
));
6023 /* Array used to determine the number of hash table buckets to use
6024 based on the number of symbols there are. If there are fewer than
6025 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6026 fewer than 37 we use 17 buckets, and so forth. We never use more
6027 than 32771 buckets. */
6029 static const size_t elf_buckets
[] =
6031 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6035 /* Compute bucket count for hashing table. We do not use a static set
6036 of possible tables sizes anymore. Instead we determine for all
6037 possible reasonable sizes of the table the outcome (i.e., the
6038 number of collisions etc) and choose the best solution. The
6039 weighting functions are not too simple to allow the table to grow
6040 without bounds. Instead one of the weighting factors is the size.
6041 Therefore the result is always a good payoff between few collisions
6042 (= short chain lengths) and table size. */
6044 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6045 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
6046 unsigned long int nsyms
,
6049 size_t best_size
= 0;
6050 unsigned long int i
;
6052 /* We have a problem here. The following code to optimize the table
6053 size requires an integer type with more the 32 bits. If
6054 BFD_HOST_U_64_BIT is set we know about such a type. */
6055 #ifdef BFD_HOST_U_64_BIT
6060 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
6061 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
6062 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
6063 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
6064 unsigned long int *counts
;
6066 unsigned int no_improvement_count
= 0;
6068 /* Possible optimization parameters: if we have NSYMS symbols we say
6069 that the hashing table must at least have NSYMS/4 and at most
6071 minsize
= nsyms
/ 4;
6074 best_size
= maxsize
= nsyms
* 2;
6079 if ((best_size
& 31) == 0)
6083 /* Create array where we count the collisions in. We must use bfd_malloc
6084 since the size could be large. */
6086 amt
*= sizeof (unsigned long int);
6087 counts
= (unsigned long int *) bfd_malloc (amt
);
6091 /* Compute the "optimal" size for the hash table. The criteria is a
6092 minimal chain length. The minor criteria is (of course) the size
6094 for (i
= minsize
; i
< maxsize
; ++i
)
6096 /* Walk through the array of hashcodes and count the collisions. */
6097 BFD_HOST_U_64_BIT max
;
6098 unsigned long int j
;
6099 unsigned long int fact
;
6101 if (gnu_hash
&& (i
& 31) == 0)
6104 memset (counts
, '\0', i
* sizeof (unsigned long int));
6106 /* Determine how often each hash bucket is used. */
6107 for (j
= 0; j
< nsyms
; ++j
)
6108 ++counts
[hashcodes
[j
] % i
];
6110 /* For the weight function we need some information about the
6111 pagesize on the target. This is information need not be 100%
6112 accurate. Since this information is not available (so far) we
6113 define it here to a reasonable default value. If it is crucial
6114 to have a better value some day simply define this value. */
6115 # ifndef BFD_TARGET_PAGESIZE
6116 # define BFD_TARGET_PAGESIZE (4096)
6119 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6121 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
6124 /* Variant 1: optimize for short chains. We add the squares
6125 of all the chain lengths (which favors many small chain
6126 over a few long chains). */
6127 for (j
= 0; j
< i
; ++j
)
6128 max
+= counts
[j
] * counts
[j
];
6130 /* This adds penalties for the overall size of the table. */
6131 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6134 /* Variant 2: Optimize a lot more for small table. Here we
6135 also add squares of the size but we also add penalties for
6136 empty slots (the +1 term). */
6137 for (j
= 0; j
< i
; ++j
)
6138 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
6140 /* The overall size of the table is considered, but not as
6141 strong as in variant 1, where it is squared. */
6142 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6146 /* Compare with current best results. */
6147 if (max
< best_chlen
)
6151 no_improvement_count
= 0;
6153 /* PR 11843: Avoid futile long searches for the best bucket size
6154 when there are a large number of symbols. */
6155 else if (++no_improvement_count
== 100)
6162 #endif /* defined (BFD_HOST_U_64_BIT) */
6164 /* This is the fallback solution if no 64bit type is available or if we
6165 are not supposed to spend much time on optimizations. We select the
6166 bucket count using a fixed set of numbers. */
6167 for (i
= 0; elf_buckets
[i
] != 0; i
++)
6169 best_size
= elf_buckets
[i
];
6170 if (nsyms
< elf_buckets
[i
+ 1])
6173 if (gnu_hash
&& best_size
< 2)
6180 /* Size any SHT_GROUP section for ld -r. */
6183 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
6188 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6189 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6190 && (s
= ibfd
->sections
) != NULL
6191 && s
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
6192 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
6197 /* Set a default stack segment size. The value in INFO wins. If it
6198 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6199 undefined it is initialized. */
6202 bfd_elf_stack_segment_size (bfd
*output_bfd
,
6203 struct bfd_link_info
*info
,
6204 const char *legacy_symbol
,
6205 bfd_vma default_size
)
6207 struct elf_link_hash_entry
*h
= NULL
;
6209 /* Look for legacy symbol. */
6211 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
6212 FALSE
, FALSE
, FALSE
);
6213 if (h
&& (h
->root
.type
== bfd_link_hash_defined
6214 || h
->root
.type
== bfd_link_hash_defweak
)
6216 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
6218 /* The symbol has no type if specified on the command line. */
6219 h
->type
= STT_OBJECT
;
6220 if (info
->stacksize
)
6221 /* xgettext:c-format */
6222 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6223 output_bfd
, legacy_symbol
);
6224 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
6225 /* xgettext:c-format */
6226 _bfd_error_handler (_("%pB: %s not absolute"),
6227 output_bfd
, legacy_symbol
);
6229 info
->stacksize
= h
->root
.u
.def
.value
;
6232 if (!info
->stacksize
)
6233 /* If the user didn't set a size, or explicitly inhibit the
6234 size, set it now. */
6235 info
->stacksize
= default_size
;
6237 /* Provide the legacy symbol, if it is referenced. */
6238 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
6239 || h
->root
.type
== bfd_link_hash_undefweak
))
6241 struct bfd_link_hash_entry
*bh
= NULL
;
6243 if (!(_bfd_generic_link_add_one_symbol
6244 (info
, output_bfd
, legacy_symbol
,
6245 BSF_GLOBAL
, bfd_abs_section_ptr
,
6246 info
->stacksize
>= 0 ? info
->stacksize
: 0,
6247 NULL
, FALSE
, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
6250 h
= (struct elf_link_hash_entry
*) bh
;
6252 h
->type
= STT_OBJECT
;
6258 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6260 struct elf_gc_sweep_symbol_info
6262 struct bfd_link_info
*info
;
6263 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
6268 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
6271 && (((h
->root
.type
== bfd_link_hash_defined
6272 || h
->root
.type
== bfd_link_hash_defweak
)
6273 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
6274 && h
->root
.u
.def
.section
->gc_mark
))
6275 || h
->root
.type
== bfd_link_hash_undefined
6276 || h
->root
.type
== bfd_link_hash_undefweak
))
6278 struct elf_gc_sweep_symbol_info
*inf
;
6280 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
6281 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
6284 h
->ref_regular_nonweak
= 0;
6290 /* Set up the sizes and contents of the ELF dynamic sections. This is
6291 called by the ELF linker emulation before_allocation routine. We
6292 must set the sizes of the sections before the linker sets the
6293 addresses of the various sections. */
6296 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
6299 const char *filter_shlib
,
6301 const char *depaudit
,
6302 const char * const *auxiliary_filters
,
6303 struct bfd_link_info
*info
,
6304 asection
**sinterpptr
)
6307 const struct elf_backend_data
*bed
;
6311 if (!is_elf_hash_table (info
->hash
))
6314 dynobj
= elf_hash_table (info
)->dynobj
;
6316 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6318 struct bfd_elf_version_tree
*verdefs
;
6319 struct elf_info_failed asvinfo
;
6320 struct bfd_elf_version_tree
*t
;
6321 struct bfd_elf_version_expr
*d
;
6325 /* If we are supposed to export all symbols into the dynamic symbol
6326 table (this is not the normal case), then do so. */
6327 if (info
->export_dynamic
6328 || (bfd_link_executable (info
) && info
->dynamic
))
6330 struct elf_info_failed eif
;
6334 elf_link_hash_traverse (elf_hash_table (info
),
6335 _bfd_elf_export_symbol
,
6343 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6345 if (soname_indx
== (size_t) -1
6346 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6350 soname_indx
= (size_t) -1;
6352 /* Make all global versions with definition. */
6353 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6354 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6355 if (!d
->symver
&& d
->literal
)
6357 const char *verstr
, *name
;
6358 size_t namelen
, verlen
, newlen
;
6359 char *newname
, *p
, leading_char
;
6360 struct elf_link_hash_entry
*newh
;
6362 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6364 namelen
= strlen (name
) + (leading_char
!= '\0');
6366 verlen
= strlen (verstr
);
6367 newlen
= namelen
+ verlen
+ 3;
6369 newname
= (char *) bfd_malloc (newlen
);
6370 if (newname
== NULL
)
6372 newname
[0] = leading_char
;
6373 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6375 /* Check the hidden versioned definition. */
6376 p
= newname
+ namelen
;
6378 memcpy (p
, verstr
, verlen
+ 1);
6379 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6380 newname
, FALSE
, FALSE
,
6383 || (newh
->root
.type
!= bfd_link_hash_defined
6384 && newh
->root
.type
!= bfd_link_hash_defweak
))
6386 /* Check the default versioned definition. */
6388 memcpy (p
, verstr
, verlen
+ 1);
6389 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6390 newname
, FALSE
, FALSE
,
6395 /* Mark this version if there is a definition and it is
6396 not defined in a shared object. */
6398 && !newh
->def_dynamic
6399 && (newh
->root
.type
== bfd_link_hash_defined
6400 || newh
->root
.type
== bfd_link_hash_defweak
))
6404 /* Attach all the symbols to their version information. */
6405 asvinfo
.info
= info
;
6406 asvinfo
.failed
= FALSE
;
6408 elf_link_hash_traverse (elf_hash_table (info
),
6409 _bfd_elf_link_assign_sym_version
,
6414 if (!info
->allow_undefined_version
)
6416 /* Check if all global versions have a definition. */
6417 bfd_boolean all_defined
= TRUE
;
6418 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6419 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6420 if (d
->literal
&& !d
->symver
&& !d
->script
)
6423 (_("%s: undefined version: %s"),
6424 d
->pattern
, t
->name
);
6425 all_defined
= FALSE
;
6430 bfd_set_error (bfd_error_bad_value
);
6435 /* Set up the version definition section. */
6436 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6437 BFD_ASSERT (s
!= NULL
);
6439 /* We may have created additional version definitions if we are
6440 just linking a regular application. */
6441 verdefs
= info
->version_info
;
6443 /* Skip anonymous version tag. */
6444 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6445 verdefs
= verdefs
->next
;
6447 if (verdefs
== NULL
&& !info
->create_default_symver
)
6448 s
->flags
|= SEC_EXCLUDE
;
6454 Elf_Internal_Verdef def
;
6455 Elf_Internal_Verdaux defaux
;
6456 struct bfd_link_hash_entry
*bh
;
6457 struct elf_link_hash_entry
*h
;
6463 /* Make space for the base version. */
6464 size
+= sizeof (Elf_External_Verdef
);
6465 size
+= sizeof (Elf_External_Verdaux
);
6468 /* Make space for the default version. */
6469 if (info
->create_default_symver
)
6471 size
+= sizeof (Elf_External_Verdef
);
6475 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6477 struct bfd_elf_version_deps
*n
;
6479 /* Don't emit base version twice. */
6483 size
+= sizeof (Elf_External_Verdef
);
6484 size
+= sizeof (Elf_External_Verdaux
);
6487 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6488 size
+= sizeof (Elf_External_Verdaux
);
6492 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6493 if (s
->contents
== NULL
&& s
->size
!= 0)
6496 /* Fill in the version definition section. */
6500 def
.vd_version
= VER_DEF_CURRENT
;
6501 def
.vd_flags
= VER_FLG_BASE
;
6504 if (info
->create_default_symver
)
6506 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6507 def
.vd_next
= sizeof (Elf_External_Verdef
);
6511 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6512 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6513 + sizeof (Elf_External_Verdaux
));
6516 if (soname_indx
!= (size_t) -1)
6518 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6520 def
.vd_hash
= bfd_elf_hash (soname
);
6521 defaux
.vda_name
= soname_indx
;
6528 name
= lbasename (output_bfd
->filename
);
6529 def
.vd_hash
= bfd_elf_hash (name
);
6530 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6532 if (indx
== (size_t) -1)
6534 defaux
.vda_name
= indx
;
6536 defaux
.vda_next
= 0;
6538 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6539 (Elf_External_Verdef
*) p
);
6540 p
+= sizeof (Elf_External_Verdef
);
6541 if (info
->create_default_symver
)
6543 /* Add a symbol representing this version. */
6545 if (! (_bfd_generic_link_add_one_symbol
6546 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6548 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6550 h
= (struct elf_link_hash_entry
*) bh
;
6553 h
->type
= STT_OBJECT
;
6554 h
->verinfo
.vertree
= NULL
;
6556 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6559 /* Create a duplicate of the base version with the same
6560 aux block, but different flags. */
6563 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6565 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6566 + sizeof (Elf_External_Verdaux
));
6569 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6570 (Elf_External_Verdef
*) p
);
6571 p
+= sizeof (Elf_External_Verdef
);
6573 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6574 (Elf_External_Verdaux
*) p
);
6575 p
+= sizeof (Elf_External_Verdaux
);
6577 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6580 struct bfd_elf_version_deps
*n
;
6582 /* Don't emit the base version twice. */
6587 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6590 /* Add a symbol representing this version. */
6592 if (! (_bfd_generic_link_add_one_symbol
6593 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6595 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6597 h
= (struct elf_link_hash_entry
*) bh
;
6600 h
->type
= STT_OBJECT
;
6601 h
->verinfo
.vertree
= t
;
6603 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6606 def
.vd_version
= VER_DEF_CURRENT
;
6608 if (t
->globals
.list
== NULL
6609 && t
->locals
.list
== NULL
6611 def
.vd_flags
|= VER_FLG_WEAK
;
6612 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6613 def
.vd_cnt
= cdeps
+ 1;
6614 def
.vd_hash
= bfd_elf_hash (t
->name
);
6615 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6618 /* If a basever node is next, it *must* be the last node in
6619 the chain, otherwise Verdef construction breaks. */
6620 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6621 BFD_ASSERT (t
->next
->next
== NULL
);
6623 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6624 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6625 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6627 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6628 (Elf_External_Verdef
*) p
);
6629 p
+= sizeof (Elf_External_Verdef
);
6631 defaux
.vda_name
= h
->dynstr_index
;
6632 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6634 defaux
.vda_next
= 0;
6635 if (t
->deps
!= NULL
)
6636 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6637 t
->name_indx
= defaux
.vda_name
;
6639 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6640 (Elf_External_Verdaux
*) p
);
6641 p
+= sizeof (Elf_External_Verdaux
);
6643 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6645 if (n
->version_needed
== NULL
)
6647 /* This can happen if there was an error in the
6649 defaux
.vda_name
= 0;
6653 defaux
.vda_name
= n
->version_needed
->name_indx
;
6654 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6657 if (n
->next
== NULL
)
6658 defaux
.vda_next
= 0;
6660 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6662 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6663 (Elf_External_Verdaux
*) p
);
6664 p
+= sizeof (Elf_External_Verdaux
);
6668 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6672 bed
= get_elf_backend_data (output_bfd
);
6674 if (info
->gc_sections
&& bed
->can_gc_sections
)
6676 struct elf_gc_sweep_symbol_info sweep_info
;
6678 /* Remove the symbols that were in the swept sections from the
6679 dynamic symbol table. */
6680 sweep_info
.info
= info
;
6681 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
6682 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
6686 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6689 struct elf_find_verdep_info sinfo
;
6691 /* Work out the size of the version reference section. */
6693 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6694 BFD_ASSERT (s
!= NULL
);
6697 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6698 if (sinfo
.vers
== 0)
6700 sinfo
.failed
= FALSE
;
6702 elf_link_hash_traverse (elf_hash_table (info
),
6703 _bfd_elf_link_find_version_dependencies
,
6708 if (elf_tdata (output_bfd
)->verref
== NULL
)
6709 s
->flags
|= SEC_EXCLUDE
;
6712 Elf_Internal_Verneed
*vn
;
6717 /* Build the version dependency section. */
6720 for (vn
= elf_tdata (output_bfd
)->verref
;
6722 vn
= vn
->vn_nextref
)
6724 Elf_Internal_Vernaux
*a
;
6726 size
+= sizeof (Elf_External_Verneed
);
6728 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6729 size
+= sizeof (Elf_External_Vernaux
);
6733 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6734 if (s
->contents
== NULL
)
6738 for (vn
= elf_tdata (output_bfd
)->verref
;
6740 vn
= vn
->vn_nextref
)
6743 Elf_Internal_Vernaux
*a
;
6747 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6750 vn
->vn_version
= VER_NEED_CURRENT
;
6752 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6753 elf_dt_name (vn
->vn_bfd
) != NULL
6754 ? elf_dt_name (vn
->vn_bfd
)
6755 : lbasename (vn
->vn_bfd
->filename
),
6757 if (indx
== (size_t) -1)
6760 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
6761 if (vn
->vn_nextref
== NULL
)
6764 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
6765 + caux
* sizeof (Elf_External_Vernaux
));
6767 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
6768 (Elf_External_Verneed
*) p
);
6769 p
+= sizeof (Elf_External_Verneed
);
6771 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6773 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6774 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6775 a
->vna_nodename
, FALSE
);
6776 if (indx
== (size_t) -1)
6779 if (a
->vna_nextptr
== NULL
)
6782 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6784 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6785 (Elf_External_Vernaux
*) p
);
6786 p
+= sizeof (Elf_External_Vernaux
);
6790 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6794 /* Any syms created from now on start with -1 in
6795 got.refcount/offset and plt.refcount/offset. */
6796 elf_hash_table (info
)->init_got_refcount
6797 = elf_hash_table (info
)->init_got_offset
;
6798 elf_hash_table (info
)->init_plt_refcount
6799 = elf_hash_table (info
)->init_plt_offset
;
6801 if (bfd_link_relocatable (info
)
6802 && !_bfd_elf_size_group_sections (info
))
6805 /* The backend may have to create some sections regardless of whether
6806 we're dynamic or not. */
6807 if (bed
->elf_backend_always_size_sections
6808 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
6811 /* Determine any GNU_STACK segment requirements, after the backend
6812 has had a chance to set a default segment size. */
6813 if (info
->execstack
)
6814 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
6815 else if (info
->noexecstack
)
6816 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
6820 asection
*notesec
= NULL
;
6823 for (inputobj
= info
->input_bfds
;
6825 inputobj
= inputobj
->link
.next
)
6830 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
6832 s
= inputobj
->sections
;
6833 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
6836 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
6839 if (s
->flags
& SEC_CODE
)
6843 else if (bed
->default_execstack
)
6846 if (notesec
|| info
->stacksize
> 0)
6847 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
6848 if (notesec
&& exec
&& bfd_link_relocatable (info
)
6849 && notesec
->output_section
!= bfd_abs_section_ptr
)
6850 notesec
->output_section
->flags
|= SEC_CODE
;
6853 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6855 struct elf_info_failed eif
;
6856 struct elf_link_hash_entry
*h
;
6860 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
6861 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
6865 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
6867 info
->flags
|= DF_SYMBOLIC
;
6875 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
6877 if (indx
== (size_t) -1)
6880 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
6881 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
6885 if (filter_shlib
!= NULL
)
6889 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6890 filter_shlib
, TRUE
);
6891 if (indx
== (size_t) -1
6892 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
6896 if (auxiliary_filters
!= NULL
)
6898 const char * const *p
;
6900 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
6904 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6906 if (indx
== (size_t) -1
6907 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
6916 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
6918 if (indx
== (size_t) -1
6919 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
6923 if (depaudit
!= NULL
)
6927 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
6929 if (indx
== (size_t) -1
6930 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
6937 /* Find all symbols which were defined in a dynamic object and make
6938 the backend pick a reasonable value for them. */
6939 elf_link_hash_traverse (elf_hash_table (info
),
6940 _bfd_elf_adjust_dynamic_symbol
,
6945 /* Add some entries to the .dynamic section. We fill in some of the
6946 values later, in bfd_elf_final_link, but we must add the entries
6947 now so that we know the final size of the .dynamic section. */
6949 /* If there are initialization and/or finalization functions to
6950 call then add the corresponding DT_INIT/DT_FINI entries. */
6951 h
= (info
->init_function
6952 ? elf_link_hash_lookup (elf_hash_table (info
),
6953 info
->init_function
, FALSE
,
6960 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
6963 h
= (info
->fini_function
6964 ? elf_link_hash_lookup (elf_hash_table (info
),
6965 info
->fini_function
, FALSE
,
6972 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
6976 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
6977 if (s
!= NULL
&& s
->linker_has_input
)
6979 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6980 if (! bfd_link_executable (info
))
6985 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
6986 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
6987 && (o
= sub
->sections
) != NULL
6988 && o
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
)
6989 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
6990 if (elf_section_data (o
)->this_hdr
.sh_type
6991 == SHT_PREINIT_ARRAY
)
6994 (_("%pB: .preinit_array section is not allowed in DSO"),
6999 bfd_set_error (bfd_error_nonrepresentable_section
);
7003 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
7004 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
7007 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
7008 if (s
!= NULL
&& s
->linker_has_input
)
7010 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
7011 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
7014 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
7015 if (s
!= NULL
&& s
->linker_has_input
)
7017 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
7018 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
7022 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
7023 /* If .dynstr is excluded from the link, we don't want any of
7024 these tags. Strictly, we should be checking each section
7025 individually; This quick check covers for the case where
7026 someone does a /DISCARD/ : { *(*) }. */
7027 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
7029 bfd_size_type strsize
;
7031 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7032 if ((info
->emit_hash
7033 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
7034 || (info
->emit_gnu_hash
7035 && (bed
->record_xhash_symbol
== NULL
7036 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0)))
7037 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
7038 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
7039 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
7040 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
7041 bed
->s
->sizeof_sym
))
7046 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
7049 /* The backend must work out the sizes of all the other dynamic
7052 && bed
->elf_backend_size_dynamic_sections
!= NULL
7053 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
7056 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7058 if (elf_tdata (output_bfd
)->cverdefs
)
7060 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
7062 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
7063 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
7067 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
7069 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
7072 else if (info
->flags
& DF_BIND_NOW
)
7074 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
7080 if (bfd_link_executable (info
))
7081 info
->flags_1
&= ~ (DF_1_INITFIRST
7084 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
7088 if (elf_tdata (output_bfd
)->cverrefs
)
7090 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
7092 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
7093 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
7097 if ((elf_tdata (output_bfd
)->cverrefs
== 0
7098 && elf_tdata (output_bfd
)->cverdefs
== 0)
7099 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, NULL
) <= 1)
7103 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7104 s
->flags
|= SEC_EXCLUDE
;
7110 /* Find the first non-excluded output section. We'll use its
7111 section symbol for some emitted relocs. */
7113 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
7116 asection
*found
= NULL
;
7118 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7119 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7120 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7123 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7126 elf_hash_table (info
)->text_index_section
= found
;
7129 /* Find two non-excluded output sections, one for code, one for data.
7130 We'll use their section symbols for some emitted relocs. */
7132 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
7135 asection
*found
= NULL
;
7137 /* Data first, since setting text_index_section changes
7138 _bfd_elf_omit_section_dynsym_default. */
7139 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7140 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7141 && !(s
->flags
& SEC_READONLY
)
7142 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7145 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7148 elf_hash_table (info
)->data_index_section
= found
;
7150 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7151 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7152 && (s
->flags
& SEC_READONLY
)
7153 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7158 elf_hash_table (info
)->text_index_section
= found
;
7161 #define GNU_HASH_SECTION_NAME(bed) \
7162 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7165 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
7167 const struct elf_backend_data
*bed
;
7168 unsigned long section_sym_count
;
7169 bfd_size_type dynsymcount
= 0;
7171 if (!is_elf_hash_table (info
->hash
))
7174 bed
= get_elf_backend_data (output_bfd
);
7175 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
7177 /* Assign dynsym indices. In a shared library we generate a section
7178 symbol for each output section, which come first. Next come all
7179 of the back-end allocated local dynamic syms, followed by the rest
7180 of the global symbols.
7182 This is usually not needed for static binaries, however backends
7183 can request to always do it, e.g. the MIPS backend uses dynamic
7184 symbol counts to lay out GOT, which will be produced in the
7185 presence of GOT relocations even in static binaries (holding fixed
7186 data in that case, to satisfy those relocations). */
7188 if (elf_hash_table (info
)->dynamic_sections_created
7189 || bed
->always_renumber_dynsyms
)
7190 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
7191 §ion_sym_count
);
7193 if (elf_hash_table (info
)->dynamic_sections_created
)
7197 unsigned int dtagcount
;
7199 dynobj
= elf_hash_table (info
)->dynobj
;
7201 /* Work out the size of the symbol version section. */
7202 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7203 BFD_ASSERT (s
!= NULL
);
7204 if ((s
->flags
& SEC_EXCLUDE
) == 0)
7206 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
7207 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7208 if (s
->contents
== NULL
)
7211 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
7215 /* Set the size of the .dynsym and .hash sections. We counted
7216 the number of dynamic symbols in elf_link_add_object_symbols.
7217 We will build the contents of .dynsym and .hash when we build
7218 the final symbol table, because until then we do not know the
7219 correct value to give the symbols. We built the .dynstr
7220 section as we went along in elf_link_add_object_symbols. */
7221 s
= elf_hash_table (info
)->dynsym
;
7222 BFD_ASSERT (s
!= NULL
);
7223 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
7225 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7226 if (s
->contents
== NULL
)
7229 /* The first entry in .dynsym is a dummy symbol. Clear all the
7230 section syms, in case we don't output them all. */
7231 ++section_sym_count
;
7232 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
7234 elf_hash_table (info
)->bucketcount
= 0;
7236 /* Compute the size of the hashing table. As a side effect this
7237 computes the hash values for all the names we export. */
7238 if (info
->emit_hash
)
7240 unsigned long int *hashcodes
;
7241 struct hash_codes_info hashinf
;
7243 unsigned long int nsyms
;
7245 size_t hash_entry_size
;
7247 /* Compute the hash values for all exported symbols. At the same
7248 time store the values in an array so that we could use them for
7250 amt
= dynsymcount
* sizeof (unsigned long int);
7251 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
7252 if (hashcodes
== NULL
)
7254 hashinf
.hashcodes
= hashcodes
;
7255 hashinf
.error
= FALSE
;
7257 /* Put all hash values in HASHCODES. */
7258 elf_link_hash_traverse (elf_hash_table (info
),
7259 elf_collect_hash_codes
, &hashinf
);
7266 nsyms
= hashinf
.hashcodes
- hashcodes
;
7268 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
7271 if (bucketcount
== 0 && nsyms
> 0)
7274 elf_hash_table (info
)->bucketcount
= bucketcount
;
7276 s
= bfd_get_linker_section (dynobj
, ".hash");
7277 BFD_ASSERT (s
!= NULL
);
7278 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
7279 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
7280 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7281 if (s
->contents
== NULL
)
7284 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
7285 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
7286 s
->contents
+ hash_entry_size
);
7289 if (info
->emit_gnu_hash
)
7292 unsigned char *contents
;
7293 struct collect_gnu_hash_codes cinfo
;
7297 memset (&cinfo
, 0, sizeof (cinfo
));
7299 /* Compute the hash values for all exported symbols. At the same
7300 time store the values in an array so that we could use them for
7302 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
7303 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
7304 if (cinfo
.hashcodes
== NULL
)
7307 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
7308 cinfo
.min_dynindx
= -1;
7309 cinfo
.output_bfd
= output_bfd
;
7312 /* Put all hash values in HASHCODES. */
7313 elf_link_hash_traverse (elf_hash_table (info
),
7314 elf_collect_gnu_hash_codes
, &cinfo
);
7317 free (cinfo
.hashcodes
);
7322 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
7324 if (bucketcount
== 0)
7326 free (cinfo
.hashcodes
);
7330 s
= bfd_get_linker_section (dynobj
, GNU_HASH_SECTION_NAME (bed
));
7331 BFD_ASSERT (s
!= NULL
);
7333 if (cinfo
.nsyms
== 0)
7335 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7336 BFD_ASSERT (cinfo
.min_dynindx
== -1);
7337 free (cinfo
.hashcodes
);
7338 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
7339 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7340 if (contents
== NULL
)
7342 s
->contents
= contents
;
7343 /* 1 empty bucket. */
7344 bfd_put_32 (output_bfd
, 1, contents
);
7345 /* SYMIDX above the special symbol 0. */
7346 bfd_put_32 (output_bfd
, 1, contents
+ 4);
7347 /* Just one word for bitmask. */
7348 bfd_put_32 (output_bfd
, 1, contents
+ 8);
7349 /* Only hash fn bloom filter. */
7350 bfd_put_32 (output_bfd
, 0, contents
+ 12);
7351 /* No hashes are valid - empty bitmask. */
7352 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
7353 /* No hashes in the only bucket. */
7354 bfd_put_32 (output_bfd
, 0,
7355 contents
+ 16 + bed
->s
->arch_size
/ 8);
7359 unsigned long int maskwords
, maskbitslog2
, x
;
7360 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
7364 while ((x
>>= 1) != 0)
7366 if (maskbitslog2
< 3)
7368 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
7369 maskbitslog2
= maskbitslog2
+ 3;
7371 maskbitslog2
= maskbitslog2
+ 2;
7372 if (bed
->s
->arch_size
== 64)
7374 if (maskbitslog2
== 5)
7380 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
7381 cinfo
.shift2
= maskbitslog2
;
7382 cinfo
.maskbits
= 1 << maskbitslog2
;
7383 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
7384 amt
= bucketcount
* sizeof (unsigned long int) * 2;
7385 amt
+= maskwords
* sizeof (bfd_vma
);
7386 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
7387 if (cinfo
.bitmask
== NULL
)
7389 free (cinfo
.hashcodes
);
7393 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
7394 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
7395 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
7396 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
7398 /* Determine how often each hash bucket is used. */
7399 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
7400 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
7401 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
7403 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
7404 if (cinfo
.counts
[i
] != 0)
7406 cinfo
.indx
[i
] = cnt
;
7407 cnt
+= cinfo
.counts
[i
];
7409 BFD_ASSERT (cnt
== dynsymcount
);
7410 cinfo
.bucketcount
= bucketcount
;
7411 cinfo
.local_indx
= cinfo
.min_dynindx
;
7413 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
7414 s
->size
+= cinfo
.maskbits
/ 8;
7415 if (bed
->record_xhash_symbol
!= NULL
)
7416 s
->size
+= cinfo
.nsyms
* 4;
7417 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7418 if (contents
== NULL
)
7420 free (cinfo
.bitmask
);
7421 free (cinfo
.hashcodes
);
7425 s
->contents
= contents
;
7426 bfd_put_32 (output_bfd
, bucketcount
, contents
);
7427 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
7428 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
7429 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
7430 contents
+= 16 + cinfo
.maskbits
/ 8;
7432 for (i
= 0; i
< bucketcount
; ++i
)
7434 if (cinfo
.counts
[i
] == 0)
7435 bfd_put_32 (output_bfd
, 0, contents
);
7437 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
7441 cinfo
.contents
= contents
;
7443 cinfo
.xlat
= contents
+ cinfo
.nsyms
* 4 - s
->contents
;
7444 /* Renumber dynamic symbols, if populating .gnu.hash section.
7445 If using .MIPS.xhash, populate the translation table. */
7446 elf_link_hash_traverse (elf_hash_table (info
),
7447 elf_gnu_hash_process_symidx
, &cinfo
);
7449 contents
= s
->contents
+ 16;
7450 for (i
= 0; i
< maskwords
; ++i
)
7452 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
7454 contents
+= bed
->s
->arch_size
/ 8;
7457 free (cinfo
.bitmask
);
7458 free (cinfo
.hashcodes
);
7462 s
= bfd_get_linker_section (dynobj
, ".dynstr");
7463 BFD_ASSERT (s
!= NULL
);
7465 elf_finalize_dynstr (output_bfd
, info
);
7467 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7469 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
7470 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
7477 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7480 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
7483 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
7484 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
7487 /* Finish SHF_MERGE section merging. */
7490 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
7495 if (!is_elf_hash_table (info
->hash
))
7498 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
7499 if ((ibfd
->flags
& DYNAMIC
) == 0
7500 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
7501 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
7502 == get_elf_backend_data (obfd
)->s
->elfclass
))
7503 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7504 if ((sec
->flags
& SEC_MERGE
) != 0
7505 && !bfd_is_abs_section (sec
->output_section
))
7507 struct bfd_elf_section_data
*secdata
;
7509 secdata
= elf_section_data (sec
);
7510 if (! _bfd_add_merge_section (obfd
,
7511 &elf_hash_table (info
)->merge_info
,
7512 sec
, &secdata
->sec_info
))
7514 else if (secdata
->sec_info
)
7515 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7518 if (elf_hash_table (info
)->merge_info
!= NULL
)
7519 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7520 merge_sections_remove_hook
);
7524 /* Create an entry in an ELF linker hash table. */
7526 struct bfd_hash_entry
*
7527 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7528 struct bfd_hash_table
*table
,
7531 /* Allocate the structure if it has not already been allocated by a
7535 entry
= (struct bfd_hash_entry
*)
7536 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7541 /* Call the allocation method of the superclass. */
7542 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7545 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7546 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7548 /* Set local fields. */
7551 ret
->got
= htab
->init_got_refcount
;
7552 ret
->plt
= htab
->init_plt_refcount
;
7553 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7554 - offsetof (struct elf_link_hash_entry
, size
)));
7555 /* Assume that we have been called by a non-ELF symbol reader.
7556 This flag is then reset by the code which reads an ELF input
7557 file. This ensures that a symbol created by a non-ELF symbol
7558 reader will have the flag set correctly. */
7565 /* Copy data from an indirect symbol to its direct symbol, hiding the
7566 old indirect symbol. Also used for copying flags to a weakdef. */
7569 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7570 struct elf_link_hash_entry
*dir
,
7571 struct elf_link_hash_entry
*ind
)
7573 struct elf_link_hash_table
*htab
;
7575 /* Copy down any references that we may have already seen to the
7576 symbol which just became indirect. */
7578 if (dir
->versioned
!= versioned_hidden
)
7579 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7580 dir
->ref_regular
|= ind
->ref_regular
;
7581 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7582 dir
->non_got_ref
|= ind
->non_got_ref
;
7583 dir
->needs_plt
|= ind
->needs_plt
;
7584 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7586 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7589 /* Copy over the global and procedure linkage table refcount entries.
7590 These may have been already set up by a check_relocs routine. */
7591 htab
= elf_hash_table (info
);
7592 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7594 if (dir
->got
.refcount
< 0)
7595 dir
->got
.refcount
= 0;
7596 dir
->got
.refcount
+= ind
->got
.refcount
;
7597 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7600 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7602 if (dir
->plt
.refcount
< 0)
7603 dir
->plt
.refcount
= 0;
7604 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7605 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7608 if (ind
->dynindx
!= -1)
7610 if (dir
->dynindx
!= -1)
7611 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7612 dir
->dynindx
= ind
->dynindx
;
7613 dir
->dynstr_index
= ind
->dynstr_index
;
7615 ind
->dynstr_index
= 0;
7620 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7621 struct elf_link_hash_entry
*h
,
7622 bfd_boolean force_local
)
7624 /* STT_GNU_IFUNC symbol must go through PLT. */
7625 if (h
->type
!= STT_GNU_IFUNC
)
7627 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7632 h
->forced_local
= 1;
7633 if (h
->dynindx
!= -1)
7635 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7638 h
->dynstr_index
= 0;
7643 /* Hide a symbol. */
7646 _bfd_elf_link_hide_symbol (bfd
*output_bfd
,
7647 struct bfd_link_info
*info
,
7648 struct bfd_link_hash_entry
*h
)
7650 if (is_elf_hash_table (info
->hash
))
7652 const struct elf_backend_data
*bed
7653 = get_elf_backend_data (output_bfd
);
7654 struct elf_link_hash_entry
*eh
7655 = (struct elf_link_hash_entry
*) h
;
7656 bed
->elf_backend_hide_symbol (info
, eh
, TRUE
);
7657 eh
->def_dynamic
= 0;
7658 eh
->ref_dynamic
= 0;
7659 eh
->dynamic_def
= 0;
7663 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7667 _bfd_elf_link_hash_table_init
7668 (struct elf_link_hash_table
*table
,
7670 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
7671 struct bfd_hash_table
*,
7673 unsigned int entsize
,
7674 enum elf_target_id target_id
)
7677 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
7679 table
->init_got_refcount
.refcount
= can_refcount
- 1;
7680 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
7681 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
7682 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
7683 /* The first dynamic symbol is a dummy. */
7684 table
->dynsymcount
= 1;
7686 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
7688 table
->root
.type
= bfd_link_elf_hash_table
;
7689 table
->hash_table_id
= target_id
;
7694 /* Create an ELF linker hash table. */
7696 struct bfd_link_hash_table
*
7697 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7699 struct elf_link_hash_table
*ret
;
7700 size_t amt
= sizeof (struct elf_link_hash_table
);
7702 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7706 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7707 sizeof (struct elf_link_hash_entry
),
7713 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7718 /* Destroy an ELF linker hash table. */
7721 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7723 struct elf_link_hash_table
*htab
;
7725 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7726 if (htab
->dynstr
!= NULL
)
7727 _bfd_elf_strtab_free (htab
->dynstr
);
7728 _bfd_merge_sections_free (htab
->merge_info
);
7729 _bfd_generic_link_hash_table_free (obfd
);
7732 /* This is a hook for the ELF emulation code in the generic linker to
7733 tell the backend linker what file name to use for the DT_NEEDED
7734 entry for a dynamic object. */
7737 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7739 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7740 && bfd_get_format (abfd
) == bfd_object
)
7741 elf_dt_name (abfd
) = name
;
7745 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
7748 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7749 && bfd_get_format (abfd
) == bfd_object
)
7750 lib_class
= elf_dyn_lib_class (abfd
);
7757 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
7759 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7760 && bfd_get_format (abfd
) == bfd_object
)
7761 elf_dyn_lib_class (abfd
) = lib_class
;
7764 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7765 the linker ELF emulation code. */
7767 struct bfd_link_needed_list
*
7768 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7769 struct bfd_link_info
*info
)
7771 if (! is_elf_hash_table (info
->hash
))
7773 return elf_hash_table (info
)->needed
;
7776 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7777 hook for the linker ELF emulation code. */
7779 struct bfd_link_needed_list
*
7780 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7781 struct bfd_link_info
*info
)
7783 if (! is_elf_hash_table (info
->hash
))
7785 return elf_hash_table (info
)->runpath
;
7788 /* Get the name actually used for a dynamic object for a link. This
7789 is the SONAME entry if there is one. Otherwise, it is the string
7790 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7793 bfd_elf_get_dt_soname (bfd
*abfd
)
7795 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7796 && bfd_get_format (abfd
) == bfd_object
)
7797 return elf_dt_name (abfd
);
7801 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7802 the ELF linker emulation code. */
7805 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
7806 struct bfd_link_needed_list
**pneeded
)
7809 bfd_byte
*dynbuf
= NULL
;
7810 unsigned int elfsec
;
7811 unsigned long shlink
;
7812 bfd_byte
*extdyn
, *extdynend
;
7814 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
7818 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
7819 || bfd_get_format (abfd
) != bfd_object
)
7822 s
= bfd_get_section_by_name (abfd
, ".dynamic");
7823 if (s
== NULL
|| s
->size
== 0)
7826 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
7829 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
7830 if (elfsec
== SHN_BAD
)
7833 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
7835 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
7836 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
7839 extdynend
= extdyn
+ s
->size
;
7840 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
7842 Elf_Internal_Dyn dyn
;
7844 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
7846 if (dyn
.d_tag
== DT_NULL
)
7849 if (dyn
.d_tag
== DT_NEEDED
)
7852 struct bfd_link_needed_list
*l
;
7853 unsigned int tagv
= dyn
.d_un
.d_val
;
7856 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
7861 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
7882 struct elf_symbuf_symbol
7884 unsigned long st_name
; /* Symbol name, index in string tbl */
7885 unsigned char st_info
; /* Type and binding attributes */
7886 unsigned char st_other
; /* Visibilty, and target specific */
7889 struct elf_symbuf_head
7891 struct elf_symbuf_symbol
*ssym
;
7893 unsigned int st_shndx
;
7900 Elf_Internal_Sym
*isym
;
7901 struct elf_symbuf_symbol
*ssym
;
7907 /* Sort references to symbols by ascending section number. */
7910 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
7912 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
7913 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
7915 if (s1
->st_shndx
!= s2
->st_shndx
)
7916 return s1
->st_shndx
> s2
->st_shndx
? 1 : -1;
7917 /* Final sort by the address of the sym in the symbuf ensures
7920 return s1
> s2
? 1 : -1;
7925 elf_sym_name_compare (const void *arg1
, const void *arg2
)
7927 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
7928 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
7929 int ret
= strcmp (s1
->name
, s2
->name
);
7932 if (s1
->u
.p
!= s2
->u
.p
)
7933 return s1
->u
.p
> s2
->u
.p
? 1 : -1;
7937 static struct elf_symbuf_head
*
7938 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
7940 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
7941 struct elf_symbuf_symbol
*ssym
;
7942 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
7943 size_t i
, shndx_count
, total_size
, amt
;
7945 amt
= symcount
* sizeof (*indbuf
);
7946 indbuf
= (Elf_Internal_Sym
**) bfd_malloc (amt
);
7950 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
7951 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
7952 *ind
++ = &isymbuf
[i
];
7955 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
7956 elf_sort_elf_symbol
);
7959 if (indbufend
> indbuf
)
7960 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
7961 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
7964 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
7965 + (indbufend
- indbuf
) * sizeof (*ssym
));
7966 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
7967 if (ssymbuf
== NULL
)
7973 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
7974 ssymbuf
->ssym
= NULL
;
7975 ssymbuf
->count
= shndx_count
;
7976 ssymbuf
->st_shndx
= 0;
7977 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
7979 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
7982 ssymhead
->ssym
= ssym
;
7983 ssymhead
->count
= 0;
7984 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
7986 ssym
->st_name
= (*ind
)->st_name
;
7987 ssym
->st_info
= (*ind
)->st_info
;
7988 ssym
->st_other
= (*ind
)->st_other
;
7991 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
7992 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
7999 /* Check if 2 sections define the same set of local and global
8003 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
8004 struct bfd_link_info
*info
)
8007 const struct elf_backend_data
*bed1
, *bed2
;
8008 Elf_Internal_Shdr
*hdr1
, *hdr2
;
8009 size_t symcount1
, symcount2
;
8010 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
8011 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
8012 Elf_Internal_Sym
*isym
, *isymend
;
8013 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
8014 size_t count1
, count2
, i
;
8015 unsigned int shndx1
, shndx2
;
8021 /* Both sections have to be in ELF. */
8022 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
8023 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
8026 if (elf_section_type (sec1
) != elf_section_type (sec2
))
8029 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
8030 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
8031 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
8034 bed1
= get_elf_backend_data (bfd1
);
8035 bed2
= get_elf_backend_data (bfd2
);
8036 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
8037 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
8038 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
8039 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
8041 if (symcount1
== 0 || symcount2
== 0)
8047 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
8048 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
8050 if (ssymbuf1
== NULL
)
8052 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
8054 if (isymbuf1
== NULL
)
8057 if (!info
->reduce_memory_overheads
)
8059 ssymbuf1
= elf_create_symbuf (symcount1
, isymbuf1
);
8060 elf_tdata (bfd1
)->symbuf
= ssymbuf1
;
8064 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
8066 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
8068 if (isymbuf2
== NULL
)
8071 if (ssymbuf1
!= NULL
&& !info
->reduce_memory_overheads
)
8073 ssymbuf2
= elf_create_symbuf (symcount2
, isymbuf2
);
8074 elf_tdata (bfd2
)->symbuf
= ssymbuf2
;
8078 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
8080 /* Optimized faster version. */
8082 struct elf_symbol
*symp
;
8083 struct elf_symbuf_symbol
*ssym
, *ssymend
;
8086 hi
= ssymbuf1
->count
;
8091 mid
= (lo
+ hi
) / 2;
8092 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
8094 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
8098 count1
= ssymbuf1
[mid
].count
;
8105 hi
= ssymbuf2
->count
;
8110 mid
= (lo
+ hi
) / 2;
8111 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
8113 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
8117 count2
= ssymbuf2
[mid
].count
;
8123 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8127 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
8129 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
8130 if (symtable1
== NULL
|| symtable2
== NULL
)
8134 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
8135 ssym
< ssymend
; ssym
++, symp
++)
8137 symp
->u
.ssym
= ssym
;
8138 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
8144 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
8145 ssym
< ssymend
; ssym
++, symp
++)
8147 symp
->u
.ssym
= ssym
;
8148 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
8153 /* Sort symbol by name. */
8154 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8155 elf_sym_name_compare
);
8156 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8157 elf_sym_name_compare
);
8159 for (i
= 0; i
< count1
; i
++)
8160 /* Two symbols must have the same binding, type and name. */
8161 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
8162 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
8163 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8170 symtable1
= (struct elf_symbol
*)
8171 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
8172 symtable2
= (struct elf_symbol
*)
8173 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
8174 if (symtable1
== NULL
|| symtable2
== NULL
)
8177 /* Count definitions in the section. */
8179 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
8180 if (isym
->st_shndx
== shndx1
)
8181 symtable1
[count1
++].u
.isym
= isym
;
8184 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
8185 if (isym
->st_shndx
== shndx2
)
8186 symtable2
[count2
++].u
.isym
= isym
;
8188 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8191 for (i
= 0; i
< count1
; i
++)
8193 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
8194 symtable1
[i
].u
.isym
->st_name
);
8196 for (i
= 0; i
< count2
; i
++)
8198 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
8199 symtable2
[i
].u
.isym
->st_name
);
8201 /* Sort symbol by name. */
8202 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8203 elf_sym_name_compare
);
8204 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8205 elf_sym_name_compare
);
8207 for (i
= 0; i
< count1
; i
++)
8208 /* Two symbols must have the same binding, type and name. */
8209 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
8210 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
8211 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8229 /* Return TRUE if 2 section types are compatible. */
8232 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
8233 bfd
*bbfd
, const asection
*bsec
)
8237 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
8238 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8241 return elf_section_type (asec
) == elf_section_type (bsec
);
8244 /* Final phase of ELF linker. */
8246 /* A structure we use to avoid passing large numbers of arguments. */
8248 struct elf_final_link_info
8250 /* General link information. */
8251 struct bfd_link_info
*info
;
8254 /* Symbol string table. */
8255 struct elf_strtab_hash
*symstrtab
;
8256 /* .hash section. */
8258 /* symbol version section (.gnu.version). */
8259 asection
*symver_sec
;
8260 /* Buffer large enough to hold contents of any section. */
8262 /* Buffer large enough to hold external relocs of any section. */
8263 void *external_relocs
;
8264 /* Buffer large enough to hold internal relocs of any section. */
8265 Elf_Internal_Rela
*internal_relocs
;
8266 /* Buffer large enough to hold external local symbols of any input
8268 bfd_byte
*external_syms
;
8269 /* And a buffer for symbol section indices. */
8270 Elf_External_Sym_Shndx
*locsym_shndx
;
8271 /* Buffer large enough to hold internal local symbols of any input
8273 Elf_Internal_Sym
*internal_syms
;
8274 /* Array large enough to hold a symbol index for each local symbol
8275 of any input BFD. */
8277 /* Array large enough to hold a section pointer for each local
8278 symbol of any input BFD. */
8279 asection
**sections
;
8280 /* Buffer for SHT_SYMTAB_SHNDX section. */
8281 Elf_External_Sym_Shndx
*symshndxbuf
;
8282 /* Number of STT_FILE syms seen. */
8283 size_t filesym_count
;
8286 /* This struct is used to pass information to elf_link_output_extsym. */
8288 struct elf_outext_info
8291 bfd_boolean localsyms
;
8292 bfd_boolean file_sym_done
;
8293 struct elf_final_link_info
*flinfo
;
8297 /* Support for evaluating a complex relocation.
8299 Complex relocations are generalized, self-describing relocations. The
8300 implementation of them consists of two parts: complex symbols, and the
8301 relocations themselves.
8303 The relocations are use a reserved elf-wide relocation type code (R_RELC
8304 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8305 information (start bit, end bit, word width, etc) into the addend. This
8306 information is extracted from CGEN-generated operand tables within gas.
8308 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8309 internal) representing prefix-notation expressions, including but not
8310 limited to those sorts of expressions normally encoded as addends in the
8311 addend field. The symbol mangling format is:
8314 | <unary-operator> ':' <node>
8315 | <binary-operator> ':' <node> ':' <node>
8318 <literal> := 's' <digits=N> ':' <N character symbol name>
8319 | 'S' <digits=N> ':' <N character section name>
8323 <binary-operator> := as in C
8324 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8327 set_symbol_value (bfd
*bfd_with_globals
,
8328 Elf_Internal_Sym
*isymbuf
,
8333 struct elf_link_hash_entry
**sym_hashes
;
8334 struct elf_link_hash_entry
*h
;
8335 size_t extsymoff
= locsymcount
;
8337 if (symidx
< locsymcount
)
8339 Elf_Internal_Sym
*sym
;
8341 sym
= isymbuf
+ symidx
;
8342 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
8344 /* It is a local symbol: move it to the
8345 "absolute" section and give it a value. */
8346 sym
->st_shndx
= SHN_ABS
;
8347 sym
->st_value
= val
;
8350 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
8354 /* It is a global symbol: set its link type
8355 to "defined" and give it a value. */
8357 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
8358 h
= sym_hashes
[symidx
- extsymoff
];
8359 while (h
->root
.type
== bfd_link_hash_indirect
8360 || h
->root
.type
== bfd_link_hash_warning
)
8361 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8362 h
->root
.type
= bfd_link_hash_defined
;
8363 h
->root
.u
.def
.value
= val
;
8364 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
8368 resolve_symbol (const char *name
,
8370 struct elf_final_link_info
*flinfo
,
8372 Elf_Internal_Sym
*isymbuf
,
8375 Elf_Internal_Sym
*sym
;
8376 struct bfd_link_hash_entry
*global_entry
;
8377 const char *candidate
= NULL
;
8378 Elf_Internal_Shdr
*symtab_hdr
;
8381 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
8383 for (i
= 0; i
< locsymcount
; ++ i
)
8387 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
8390 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
8391 symtab_hdr
->sh_link
,
8394 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8395 name
, candidate
, (unsigned long) sym
->st_value
);
8397 if (candidate
&& strcmp (candidate
, name
) == 0)
8399 asection
*sec
= flinfo
->sections
[i
];
8401 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
8402 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
8404 printf ("Found symbol with value %8.8lx\n",
8405 (unsigned long) *result
);
8411 /* Hmm, haven't found it yet. perhaps it is a global. */
8412 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
8413 FALSE
, FALSE
, TRUE
);
8417 if (global_entry
->type
== bfd_link_hash_defined
8418 || global_entry
->type
== bfd_link_hash_defweak
)
8420 *result
= (global_entry
->u
.def
.value
8421 + global_entry
->u
.def
.section
->output_section
->vma
8422 + global_entry
->u
.def
.section
->output_offset
);
8424 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8425 global_entry
->root
.string
, (unsigned long) *result
);
8433 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8434 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8435 names like "foo.end" which is the end address of section "foo". */
8438 resolve_section (const char *name
,
8446 for (curr
= sections
; curr
; curr
= curr
->next
)
8447 if (strcmp (curr
->name
, name
) == 0)
8449 *result
= curr
->vma
;
8453 /* Hmm. still haven't found it. try pseudo-section names. */
8454 /* FIXME: This could be coded more efficiently... */
8455 for (curr
= sections
; curr
; curr
= curr
->next
)
8457 len
= strlen (curr
->name
);
8458 if (len
> strlen (name
))
8461 if (strncmp (curr
->name
, name
, len
) == 0)
8463 if (strncmp (".end", name
+ len
, 4) == 0)
8465 *result
= (curr
->vma
8466 + curr
->size
/ bfd_octets_per_byte (abfd
, curr
));
8470 /* Insert more pseudo-section names here, if you like. */
8478 undefined_reference (const char *reftype
, const char *name
)
8480 /* xgettext:c-format */
8481 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8486 eval_symbol (bfd_vma
*result
,
8489 struct elf_final_link_info
*flinfo
,
8491 Elf_Internal_Sym
*isymbuf
,
8500 const char *sym
= *symp
;
8502 bfd_boolean symbol_is_section
= FALSE
;
8507 if (len
< 1 || len
> sizeof (symbuf
))
8509 bfd_set_error (bfd_error_invalid_operation
);
8522 *result
= strtoul (sym
, (char **) symp
, 16);
8526 symbol_is_section
= TRUE
;
8530 symlen
= strtol (sym
, (char **) symp
, 10);
8531 sym
= *symp
+ 1; /* Skip the trailing ':'. */
8533 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
8535 bfd_set_error (bfd_error_invalid_operation
);
8539 memcpy (symbuf
, sym
, symlen
);
8540 symbuf
[symlen
] = '\0';
8541 *symp
= sym
+ symlen
;
8543 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8544 the symbol as a section, or vice-versa. so we're pretty liberal in our
8545 interpretation here; section means "try section first", not "must be a
8546 section", and likewise with symbol. */
8548 if (symbol_is_section
)
8550 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
8551 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8552 isymbuf
, locsymcount
))
8554 undefined_reference ("section", symbuf
);
8560 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8561 isymbuf
, locsymcount
)
8562 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8565 undefined_reference ("symbol", symbuf
);
8572 /* All that remains are operators. */
8574 #define UNARY_OP(op) \
8575 if (strncmp (sym, #op, strlen (#op)) == 0) \
8577 sym += strlen (#op); \
8581 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8582 isymbuf, locsymcount, signed_p)) \
8585 *result = op ((bfd_signed_vma) a); \
8591 #define BINARY_OP(op) \
8592 if (strncmp (sym, #op, strlen (#op)) == 0) \
8594 sym += strlen (#op); \
8598 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8599 isymbuf, locsymcount, signed_p)) \
8602 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8603 isymbuf, locsymcount, signed_p)) \
8606 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8636 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
8637 bfd_set_error (bfd_error_invalid_operation
);
8643 put_value (bfd_vma size
,
8644 unsigned long chunksz
,
8649 location
+= (size
- chunksz
);
8651 for (; size
; size
-= chunksz
, location
-= chunksz
)
8656 bfd_put_8 (input_bfd
, x
, location
);
8660 bfd_put_16 (input_bfd
, x
, location
);
8664 bfd_put_32 (input_bfd
, x
, location
);
8665 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8671 bfd_put_64 (input_bfd
, x
, location
);
8672 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8685 get_value (bfd_vma size
,
8686 unsigned long chunksz
,
8693 /* Sanity checks. */
8694 BFD_ASSERT (chunksz
<= sizeof (x
)
8697 && (size
% chunksz
) == 0
8698 && input_bfd
!= NULL
8699 && location
!= NULL
);
8701 if (chunksz
== sizeof (x
))
8703 BFD_ASSERT (size
== chunksz
);
8705 /* Make sure that we do not perform an undefined shift operation.
8706 We know that size == chunksz so there will only be one iteration
8707 of the loop below. */
8711 shift
= 8 * chunksz
;
8713 for (; size
; size
-= chunksz
, location
+= chunksz
)
8718 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
8721 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
8724 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
8728 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
8739 decode_complex_addend (unsigned long *start
, /* in bits */
8740 unsigned long *oplen
, /* in bits */
8741 unsigned long *len
, /* in bits */
8742 unsigned long *wordsz
, /* in bytes */
8743 unsigned long *chunksz
, /* in bytes */
8744 unsigned long *lsb0_p
,
8745 unsigned long *signed_p
,
8746 unsigned long *trunc_p
,
8747 unsigned long encoded
)
8749 * start
= encoded
& 0x3F;
8750 * len
= (encoded
>> 6) & 0x3F;
8751 * oplen
= (encoded
>> 12) & 0x3F;
8752 * wordsz
= (encoded
>> 18) & 0xF;
8753 * chunksz
= (encoded
>> 22) & 0xF;
8754 * lsb0_p
= (encoded
>> 27) & 1;
8755 * signed_p
= (encoded
>> 28) & 1;
8756 * trunc_p
= (encoded
>> 29) & 1;
8759 bfd_reloc_status_type
8760 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
8761 asection
*input_section
,
8763 Elf_Internal_Rela
*rel
,
8766 bfd_vma shift
, x
, mask
;
8767 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
8768 bfd_reloc_status_type r
;
8769 bfd_size_type octets
;
8771 /* Perform this reloc, since it is complex.
8772 (this is not to say that it necessarily refers to a complex
8773 symbol; merely that it is a self-describing CGEN based reloc.
8774 i.e. the addend has the complete reloc information (bit start, end,
8775 word size, etc) encoded within it.). */
8777 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
8778 &chunksz
, &lsb0_p
, &signed_p
,
8779 &trunc_p
, rel
->r_addend
);
8781 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
8784 shift
= (start
+ 1) - len
;
8786 shift
= (8 * wordsz
) - (start
+ len
);
8788 octets
= rel
->r_offset
* bfd_octets_per_byte (input_bfd
, input_section
);
8789 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ octets
);
8792 printf ("Doing complex reloc: "
8793 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8794 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8795 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8796 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
8797 oplen
, (unsigned long) x
, (unsigned long) mask
,
8798 (unsigned long) relocation
);
8803 /* Now do an overflow check. */
8804 r
= bfd_check_overflow ((signed_p
8805 ? complain_overflow_signed
8806 : complain_overflow_unsigned
),
8807 len
, 0, (8 * wordsz
),
8811 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
8814 printf (" relocation: %8.8lx\n"
8815 " shifted mask: %8.8lx\n"
8816 " shifted/masked reloc: %8.8lx\n"
8817 " result: %8.8lx\n",
8818 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
8819 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
8821 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ octets
);
8825 /* Functions to read r_offset from external (target order) reloc
8826 entry. Faster than bfd_getl32 et al, because we let the compiler
8827 know the value is aligned. */
8830 ext32l_r_offset (const void *p
)
8837 const union aligned32
*a
8838 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8840 uint32_t aval
= ( (uint32_t) a
->c
[0]
8841 | (uint32_t) a
->c
[1] << 8
8842 | (uint32_t) a
->c
[2] << 16
8843 | (uint32_t) a
->c
[3] << 24);
8848 ext32b_r_offset (const void *p
)
8855 const union aligned32
*a
8856 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8858 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
8859 | (uint32_t) a
->c
[1] << 16
8860 | (uint32_t) a
->c
[2] << 8
8861 | (uint32_t) a
->c
[3]);
8865 #ifdef BFD_HOST_64_BIT
8867 ext64l_r_offset (const void *p
)
8874 const union aligned64
*a
8875 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8877 uint64_t aval
= ( (uint64_t) a
->c
[0]
8878 | (uint64_t) a
->c
[1] << 8
8879 | (uint64_t) a
->c
[2] << 16
8880 | (uint64_t) a
->c
[3] << 24
8881 | (uint64_t) a
->c
[4] << 32
8882 | (uint64_t) a
->c
[5] << 40
8883 | (uint64_t) a
->c
[6] << 48
8884 | (uint64_t) a
->c
[7] << 56);
8889 ext64b_r_offset (const void *p
)
8896 const union aligned64
*a
8897 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8899 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
8900 | (uint64_t) a
->c
[1] << 48
8901 | (uint64_t) a
->c
[2] << 40
8902 | (uint64_t) a
->c
[3] << 32
8903 | (uint64_t) a
->c
[4] << 24
8904 | (uint64_t) a
->c
[5] << 16
8905 | (uint64_t) a
->c
[6] << 8
8906 | (uint64_t) a
->c
[7]);
8911 /* When performing a relocatable link, the input relocations are
8912 preserved. But, if they reference global symbols, the indices
8913 referenced must be updated. Update all the relocations found in
8917 elf_link_adjust_relocs (bfd
*abfd
,
8919 struct bfd_elf_section_reloc_data
*reldata
,
8921 struct bfd_link_info
*info
)
8924 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8926 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8927 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8928 bfd_vma r_type_mask
;
8930 unsigned int count
= reldata
->count
;
8931 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
8933 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
8935 swap_in
= bed
->s
->swap_reloc_in
;
8936 swap_out
= bed
->s
->swap_reloc_out
;
8938 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
8940 swap_in
= bed
->s
->swap_reloca_in
;
8941 swap_out
= bed
->s
->swap_reloca_out
;
8946 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
8949 if (bed
->s
->arch_size
== 32)
8956 r_type_mask
= 0xffffffff;
8960 erela
= reldata
->hdr
->contents
;
8961 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
8963 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
8966 if (*rel_hash
== NULL
)
8969 if ((*rel_hash
)->indx
== -2
8970 && info
->gc_sections
8971 && ! info
->gc_keep_exported
)
8973 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8974 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
8976 (*rel_hash
)->root
.root
.string
);
8977 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
8979 bfd_set_error (bfd_error_invalid_operation
);
8982 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
8984 (*swap_in
) (abfd
, erela
, irela
);
8985 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
8986 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
8987 | (irela
[j
].r_info
& r_type_mask
));
8988 (*swap_out
) (abfd
, irela
, erela
);
8991 if (bed
->elf_backend_update_relocs
)
8992 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
8994 if (sort
&& count
!= 0)
8996 bfd_vma (*ext_r_off
) (const void *);
8999 bfd_byte
*base
, *end
, *p
, *loc
;
9000 bfd_byte
*buf
= NULL
;
9002 if (bed
->s
->arch_size
== 32)
9004 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9005 ext_r_off
= ext32l_r_offset
;
9006 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9007 ext_r_off
= ext32b_r_offset
;
9013 #ifdef BFD_HOST_64_BIT
9014 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9015 ext_r_off
= ext64l_r_offset
;
9016 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9017 ext_r_off
= ext64b_r_offset
;
9023 /* Must use a stable sort here. A modified insertion sort,
9024 since the relocs are mostly sorted already. */
9025 elt_size
= reldata
->hdr
->sh_entsize
;
9026 base
= reldata
->hdr
->contents
;
9027 end
= base
+ count
* elt_size
;
9028 if (elt_size
> sizeof (Elf64_External_Rela
))
9031 /* Ensure the first element is lowest. This acts as a sentinel,
9032 speeding the main loop below. */
9033 r_off
= (*ext_r_off
) (base
);
9034 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
9036 bfd_vma r_off2
= (*ext_r_off
) (p
);
9045 /* Don't just swap *base and *loc as that changes the order
9046 of the original base[0] and base[1] if they happen to
9047 have the same r_offset. */
9048 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
9049 memcpy (onebuf
, loc
, elt_size
);
9050 memmove (base
+ elt_size
, base
, loc
- base
);
9051 memcpy (base
, onebuf
, elt_size
);
9054 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
9056 /* base to p is sorted, *p is next to insert. */
9057 r_off
= (*ext_r_off
) (p
);
9058 /* Search the sorted region for location to insert. */
9060 while (r_off
< (*ext_r_off
) (loc
))
9065 /* Chances are there is a run of relocs to insert here,
9066 from one of more input files. Files are not always
9067 linked in order due to the way elf_link_input_bfd is
9068 called. See pr17666. */
9069 size_t sortlen
= p
- loc
;
9070 bfd_vma r_off2
= (*ext_r_off
) (loc
);
9071 size_t runlen
= elt_size
;
9072 size_t buf_size
= 96 * 1024;
9073 while (p
+ runlen
< end
9074 && (sortlen
<= buf_size
9075 || runlen
+ elt_size
<= buf_size
)
9076 && r_off2
> (*ext_r_off
) (p
+ runlen
))
9080 buf
= bfd_malloc (buf_size
);
9084 if (runlen
< sortlen
)
9086 memcpy (buf
, p
, runlen
);
9087 memmove (loc
+ runlen
, loc
, sortlen
);
9088 memcpy (loc
, buf
, runlen
);
9092 memcpy (buf
, loc
, sortlen
);
9093 memmove (loc
, p
, runlen
);
9094 memcpy (loc
+ runlen
, buf
, sortlen
);
9096 p
+= runlen
- elt_size
;
9099 /* Hashes are no longer valid. */
9100 free (reldata
->hashes
);
9101 reldata
->hashes
= NULL
;
9107 struct elf_link_sort_rela
9113 enum elf_reloc_type_class type
;
9114 /* We use this as an array of size int_rels_per_ext_rel. */
9115 Elf_Internal_Rela rela
[1];
9118 /* qsort stability here and for cmp2 is only an issue if multiple
9119 dynamic relocations are emitted at the same address. But targets
9120 that apply a series of dynamic relocations each operating on the
9121 result of the prior relocation can't use -z combreloc as
9122 implemented anyway. Such schemes tend to be broken by sorting on
9123 symbol index. That leaves dynamic NONE relocs as the only other
9124 case where ld might emit multiple relocs at the same address, and
9125 those are only emitted due to target bugs. */
9128 elf_link_sort_cmp1 (const void *A
, const void *B
)
9130 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9131 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9132 int relativea
, relativeb
;
9134 relativea
= a
->type
== reloc_class_relative
;
9135 relativeb
= b
->type
== reloc_class_relative
;
9137 if (relativea
< relativeb
)
9139 if (relativea
> relativeb
)
9141 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
9143 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
9145 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9147 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9153 elf_link_sort_cmp2 (const void *A
, const void *B
)
9155 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9156 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9158 if (a
->type
< b
->type
)
9160 if (a
->type
> b
->type
)
9162 if (a
->u
.offset
< b
->u
.offset
)
9164 if (a
->u
.offset
> b
->u
.offset
)
9166 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9168 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9174 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
9176 asection
*dynamic_relocs
;
9179 bfd_size_type count
, size
;
9180 size_t i
, ret
, sort_elt
, ext_size
;
9181 bfd_byte
*sort
, *s_non_relative
, *p
;
9182 struct elf_link_sort_rela
*sq
;
9183 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9184 int i2e
= bed
->s
->int_rels_per_ext_rel
;
9185 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
9186 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9187 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9188 struct bfd_link_order
*lo
;
9190 bfd_boolean use_rela
;
9192 /* Find a dynamic reloc section. */
9193 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
9194 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
9195 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
9196 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9198 bfd_boolean use_rela_initialised
= FALSE
;
9200 /* This is just here to stop gcc from complaining.
9201 Its initialization checking code is not perfect. */
9204 /* Both sections are present. Examine the sizes
9205 of the indirect sections to help us choose. */
9206 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9207 if (lo
->type
== bfd_indirect_link_order
)
9209 asection
*o
= lo
->u
.indirect
.section
;
9211 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9213 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9214 /* Section size is divisible by both rel and rela sizes.
9215 It is of no help to us. */
9219 /* Section size is only divisible by rela. */
9220 if (use_rela_initialised
&& !use_rela
)
9222 _bfd_error_handler (_("%pB: unable to sort relocs - "
9223 "they are in more than one size"),
9225 bfd_set_error (bfd_error_invalid_operation
);
9231 use_rela_initialised
= TRUE
;
9235 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9237 /* Section size is only divisible by rel. */
9238 if (use_rela_initialised
&& use_rela
)
9240 _bfd_error_handler (_("%pB: unable to sort relocs - "
9241 "they are in more than one size"),
9243 bfd_set_error (bfd_error_invalid_operation
);
9249 use_rela_initialised
= TRUE
;
9254 /* The section size is not divisible by either -
9255 something is wrong. */
9256 _bfd_error_handler (_("%pB: unable to sort relocs - "
9257 "they are of an unknown size"), abfd
);
9258 bfd_set_error (bfd_error_invalid_operation
);
9263 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9264 if (lo
->type
== bfd_indirect_link_order
)
9266 asection
*o
= lo
->u
.indirect
.section
;
9268 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9270 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9271 /* Section size is divisible by both rel and rela sizes.
9272 It is of no help to us. */
9276 /* Section size is only divisible by rela. */
9277 if (use_rela_initialised
&& !use_rela
)
9279 _bfd_error_handler (_("%pB: unable to sort relocs - "
9280 "they are in more than one size"),
9282 bfd_set_error (bfd_error_invalid_operation
);
9288 use_rela_initialised
= TRUE
;
9292 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9294 /* Section size is only divisible by rel. */
9295 if (use_rela_initialised
&& use_rela
)
9297 _bfd_error_handler (_("%pB: unable to sort relocs - "
9298 "they are in more than one size"),
9300 bfd_set_error (bfd_error_invalid_operation
);
9306 use_rela_initialised
= TRUE
;
9311 /* The section size is not divisible by either -
9312 something is wrong. */
9313 _bfd_error_handler (_("%pB: unable to sort relocs - "
9314 "they are of an unknown size"), abfd
);
9315 bfd_set_error (bfd_error_invalid_operation
);
9320 if (! use_rela_initialised
)
9324 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
9326 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9333 dynamic_relocs
= rela_dyn
;
9334 ext_size
= bed
->s
->sizeof_rela
;
9335 swap_in
= bed
->s
->swap_reloca_in
;
9336 swap_out
= bed
->s
->swap_reloca_out
;
9340 dynamic_relocs
= rel_dyn
;
9341 ext_size
= bed
->s
->sizeof_rel
;
9342 swap_in
= bed
->s
->swap_reloc_in
;
9343 swap_out
= bed
->s
->swap_reloc_out
;
9347 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9348 if (lo
->type
== bfd_indirect_link_order
)
9349 size
+= lo
->u
.indirect
.section
->size
;
9351 if (size
!= dynamic_relocs
->size
)
9354 sort_elt
= (sizeof (struct elf_link_sort_rela
)
9355 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
9357 count
= dynamic_relocs
->size
/ ext_size
;
9360 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
9364 (*info
->callbacks
->warning
)
9365 (info
, _("not enough memory to sort relocations"), 0, abfd
, 0, 0);
9369 if (bed
->s
->arch_size
== 32)
9370 r_sym_mask
= ~(bfd_vma
) 0xff;
9372 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
9374 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9375 if (lo
->type
== bfd_indirect_link_order
)
9377 bfd_byte
*erel
, *erelend
;
9378 asection
*o
= lo
->u
.indirect
.section
;
9380 if (o
->contents
== NULL
&& o
->size
!= 0)
9382 /* This is a reloc section that is being handled as a normal
9383 section. See bfd_section_from_shdr. We can't combine
9384 relocs in this case. */
9389 erelend
= o
->contents
+ o
->size
;
9390 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
9392 while (erel
< erelend
)
9394 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9396 (*swap_in
) (abfd
, erel
, s
->rela
);
9397 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
9398 s
->u
.sym_mask
= r_sym_mask
;
9404 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
9406 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
9408 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9409 if (s
->type
!= reloc_class_relative
)
9415 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
9416 for (; i
< count
; i
++, p
+= sort_elt
)
9418 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
9419 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
9421 sp
->u
.offset
= sq
->rela
->r_offset
;
9424 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
9426 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
9427 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
9429 /* We have plt relocs in .rela.dyn. */
9430 sq
= (struct elf_link_sort_rela
*) sort
;
9431 for (i
= 0; i
< count
; i
++)
9432 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
9434 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
9436 struct bfd_link_order
**plo
;
9437 /* Put srelplt link_order last. This is so the output_offset
9438 set in the next loop is correct for DT_JMPREL. */
9439 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
9440 if ((*plo
)->type
== bfd_indirect_link_order
9441 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
9447 plo
= &(*plo
)->next
;
9450 dynamic_relocs
->map_tail
.link_order
= lo
;
9455 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9456 if (lo
->type
== bfd_indirect_link_order
)
9458 bfd_byte
*erel
, *erelend
;
9459 asection
*o
= lo
->u
.indirect
.section
;
9462 erelend
= o
->contents
+ o
->size
;
9463 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
9464 while (erel
< erelend
)
9466 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9467 (*swap_out
) (abfd
, s
->rela
, erel
);
9474 *psec
= dynamic_relocs
;
9478 /* Add a symbol to the output symbol string table. */
9481 elf_link_output_symstrtab (struct elf_final_link_info
*flinfo
,
9483 Elf_Internal_Sym
*elfsym
,
9484 asection
*input_sec
,
9485 struct elf_link_hash_entry
*h
)
9487 int (*output_symbol_hook
)
9488 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
9489 struct elf_link_hash_entry
*);
9490 struct elf_link_hash_table
*hash_table
;
9491 const struct elf_backend_data
*bed
;
9492 bfd_size_type strtabsize
;
9494 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9496 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9497 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
9498 if (output_symbol_hook
!= NULL
)
9500 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
9505 if (ELF_ST_TYPE (elfsym
->st_info
) == STT_GNU_IFUNC
)
9506 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_ifunc
;
9507 if (ELF_ST_BIND (elfsym
->st_info
) == STB_GNU_UNIQUE
)
9508 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_unique
;
9512 || (input_sec
->flags
& SEC_EXCLUDE
))
9513 elfsym
->st_name
= (unsigned long) -1;
9516 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9517 to get the final offset for st_name. */
9519 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
9521 if (elfsym
->st_name
== (unsigned long) -1)
9525 hash_table
= elf_hash_table (flinfo
->info
);
9526 strtabsize
= hash_table
->strtabsize
;
9527 if (strtabsize
<= hash_table
->strtabcount
)
9529 strtabsize
+= strtabsize
;
9530 hash_table
->strtabsize
= strtabsize
;
9531 strtabsize
*= sizeof (*hash_table
->strtab
);
9533 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
9535 if (hash_table
->strtab
== NULL
)
9538 hash_table
->strtab
[hash_table
->strtabcount
].sym
= *elfsym
;
9539 hash_table
->strtab
[hash_table
->strtabcount
].dest_index
9540 = hash_table
->strtabcount
;
9541 hash_table
->strtab
[hash_table
->strtabcount
].destshndx_index
9542 = flinfo
->symshndxbuf
? bfd_get_symcount (flinfo
->output_bfd
) : 0;
9544 flinfo
->output_bfd
->symcount
+= 1;
9545 hash_table
->strtabcount
+= 1;
9550 /* Swap symbols out to the symbol table and flush the output symbols to
9554 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
9556 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
9559 const struct elf_backend_data
*bed
;
9561 Elf_Internal_Shdr
*hdr
;
9565 if (!hash_table
->strtabcount
)
9568 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9570 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9572 amt
= bed
->s
->sizeof_sym
* hash_table
->strtabcount
;
9573 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
9577 if (flinfo
->symshndxbuf
)
9579 amt
= sizeof (Elf_External_Sym_Shndx
);
9580 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
9581 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
9582 if (flinfo
->symshndxbuf
== NULL
)
9589 for (i
= 0; i
< hash_table
->strtabcount
; i
++)
9591 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
9592 if (elfsym
->sym
.st_name
== (unsigned long) -1)
9593 elfsym
->sym
.st_name
= 0;
9596 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
9597 elfsym
->sym
.st_name
);
9598 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
9599 ((bfd_byte
*) symbuf
9600 + (elfsym
->dest_index
9601 * bed
->s
->sizeof_sym
)),
9602 (flinfo
->symshndxbuf
9603 + elfsym
->destshndx_index
));
9606 /* Allow the linker to examine the strtab and symtab now they are
9609 if (flinfo
->info
->callbacks
->examine_strtab
)
9610 flinfo
->info
->callbacks
->examine_strtab (hash_table
->strtab
,
9611 hash_table
->strtabcount
,
9614 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
9615 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
9616 amt
= hash_table
->strtabcount
* bed
->s
->sizeof_sym
;
9617 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
9618 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
9620 hdr
->sh_size
+= amt
;
9628 free (hash_table
->strtab
);
9629 hash_table
->strtab
= NULL
;
9634 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9637 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
9639 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
9640 && sym
->st_shndx
< SHN_LORESERVE
)
9642 /* The gABI doesn't support dynamic symbols in output sections
9645 /* xgettext:c-format */
9646 (_("%pB: too many sections: %d (>= %d)"),
9647 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
9648 bfd_set_error (bfd_error_nonrepresentable_section
);
9654 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9655 allowing an unsatisfied unversioned symbol in the DSO to match a
9656 versioned symbol that would normally require an explicit version.
9657 We also handle the case that a DSO references a hidden symbol
9658 which may be satisfied by a versioned symbol in another DSO. */
9661 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
9662 const struct elf_backend_data
*bed
,
9663 struct elf_link_hash_entry
*h
)
9666 struct elf_link_loaded_list
*loaded
;
9668 if (!is_elf_hash_table (info
->hash
))
9671 /* Check indirect symbol. */
9672 while (h
->root
.type
== bfd_link_hash_indirect
)
9673 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9675 switch (h
->root
.type
)
9681 case bfd_link_hash_undefined
:
9682 case bfd_link_hash_undefweak
:
9683 abfd
= h
->root
.u
.undef
.abfd
;
9685 || (abfd
->flags
& DYNAMIC
) == 0
9686 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
9690 case bfd_link_hash_defined
:
9691 case bfd_link_hash_defweak
:
9692 abfd
= h
->root
.u
.def
.section
->owner
;
9695 case bfd_link_hash_common
:
9696 abfd
= h
->root
.u
.c
.p
->section
->owner
;
9699 BFD_ASSERT (abfd
!= NULL
);
9701 for (loaded
= elf_hash_table (info
)->dyn_loaded
;
9703 loaded
= loaded
->next
)
9706 Elf_Internal_Shdr
*hdr
;
9710 Elf_Internal_Shdr
*versymhdr
;
9711 Elf_Internal_Sym
*isym
;
9712 Elf_Internal_Sym
*isymend
;
9713 Elf_Internal_Sym
*isymbuf
;
9714 Elf_External_Versym
*ever
;
9715 Elf_External_Versym
*extversym
;
9717 input
= loaded
->abfd
;
9719 /* We check each DSO for a possible hidden versioned definition. */
9721 || elf_dynversym (input
) == 0)
9724 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
9726 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9727 if (elf_bad_symtab (input
))
9729 extsymcount
= symcount
;
9734 extsymcount
= symcount
- hdr
->sh_info
;
9735 extsymoff
= hdr
->sh_info
;
9738 if (extsymcount
== 0)
9741 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
9743 if (isymbuf
== NULL
)
9746 /* Read in any version definitions. */
9747 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
9748 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
9749 || (extversym
= (Elf_External_Versym
*)
9750 _bfd_malloc_and_read (input
, versymhdr
->sh_size
,
9751 versymhdr
->sh_size
)) == NULL
)
9757 ever
= extversym
+ extsymoff
;
9758 isymend
= isymbuf
+ extsymcount
;
9759 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
9762 Elf_Internal_Versym iver
;
9763 unsigned short version_index
;
9765 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
9766 || isym
->st_shndx
== SHN_UNDEF
)
9769 name
= bfd_elf_string_from_elf_section (input
,
9772 if (strcmp (name
, h
->root
.root
.string
) != 0)
9775 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
9777 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
9779 && h
->forced_local
))
9781 /* If we have a non-hidden versioned sym, then it should
9782 have provided a definition for the undefined sym unless
9783 it is defined in a non-shared object and forced local.
9788 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
9789 if (version_index
== 1 || version_index
== 2)
9791 /* This is the base or first version. We can use it. */
9805 /* Convert ELF common symbol TYPE. */
9808 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
9810 /* Commom symbol can only appear in relocatable link. */
9811 if (!bfd_link_relocatable (info
))
9813 switch (info
->elf_stt_common
)
9817 case elf_stt_common
:
9820 case no_elf_stt_common
:
9827 /* Add an external symbol to the symbol table. This is called from
9828 the hash table traversal routine. When generating a shared object,
9829 we go through the symbol table twice. The first time we output
9830 anything that might have been forced to local scope in a version
9831 script. The second time we output the symbols that are still
9835 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
9837 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
9838 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
9839 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
9841 Elf_Internal_Sym sym
;
9842 asection
*input_sec
;
9843 const struct elf_backend_data
*bed
;
9848 if (h
->root
.type
== bfd_link_hash_warning
)
9850 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9851 if (h
->root
.type
== bfd_link_hash_new
)
9855 /* Decide whether to output this symbol in this pass. */
9856 if (eoinfo
->localsyms
)
9858 if (!h
->forced_local
)
9863 if (h
->forced_local
)
9867 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9869 if (h
->root
.type
== bfd_link_hash_undefined
)
9871 /* If we have an undefined symbol reference here then it must have
9872 come from a shared library that is being linked in. (Undefined
9873 references in regular files have already been handled unless
9874 they are in unreferenced sections which are removed by garbage
9876 bfd_boolean ignore_undef
= FALSE
;
9878 /* Some symbols may be special in that the fact that they're
9879 undefined can be safely ignored - let backend determine that. */
9880 if (bed
->elf_backend_ignore_undef_symbol
)
9881 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
9883 /* If we are reporting errors for this situation then do so now. */
9885 && h
->ref_dynamic_nonweak
9886 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
9887 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
9888 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
9889 (*flinfo
->info
->callbacks
->undefined_symbol
)
9890 (flinfo
->info
, h
->root
.root
.string
,
9891 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
,
9893 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
);
9895 /* Strip a global symbol defined in a discarded section. */
9900 /* We should also warn if a forced local symbol is referenced from
9901 shared libraries. */
9902 if (bfd_link_executable (flinfo
->info
)
9907 && h
->ref_dynamic_nonweak
9908 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
9912 struct elf_link_hash_entry
*hi
= h
;
9914 /* Check indirect symbol. */
9915 while (hi
->root
.type
== bfd_link_hash_indirect
)
9916 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
9918 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
9919 /* xgettext:c-format */
9920 msg
= _("%pB: internal symbol `%s' in %pB is referenced by DSO");
9921 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
9922 /* xgettext:c-format */
9923 msg
= _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
9925 /* xgettext:c-format */
9926 msg
= _("%pB: local symbol `%s' in %pB is referenced by DSO");
9927 def_bfd
= flinfo
->output_bfd
;
9928 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
9929 def_bfd
= hi
->root
.u
.def
.section
->owner
;
9930 _bfd_error_handler (msg
, flinfo
->output_bfd
,
9931 h
->root
.root
.string
, def_bfd
);
9932 bfd_set_error (bfd_error_bad_value
);
9933 eoinfo
->failed
= TRUE
;
9937 /* We don't want to output symbols that have never been mentioned by
9938 a regular file, or that we have been told to strip. However, if
9939 h->indx is set to -2, the symbol is used by a reloc and we must
9944 else if ((h
->def_dynamic
9946 || h
->root
.type
== bfd_link_hash_new
)
9950 else if (flinfo
->info
->strip
== strip_all
)
9952 else if (flinfo
->info
->strip
== strip_some
9953 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
9954 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
9956 else if ((h
->root
.type
== bfd_link_hash_defined
9957 || h
->root
.type
== bfd_link_hash_defweak
)
9958 && ((flinfo
->info
->strip_discarded
9959 && discarded_section (h
->root
.u
.def
.section
))
9960 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
9961 && h
->root
.u
.def
.section
->owner
!= NULL
9962 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
9964 else if ((h
->root
.type
== bfd_link_hash_undefined
9965 || h
->root
.type
== bfd_link_hash_undefweak
)
9966 && h
->root
.u
.undef
.abfd
!= NULL
9967 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
9972 /* If we're stripping it, and it's not a dynamic symbol, there's
9973 nothing else to do. However, if it is a forced local symbol or
9974 an ifunc symbol we need to give the backend finish_dynamic_symbol
9975 function a chance to make it dynamic. */
9978 && type
!= STT_GNU_IFUNC
9979 && !h
->forced_local
)
9983 sym
.st_size
= h
->size
;
9984 sym
.st_other
= h
->other
;
9985 switch (h
->root
.type
)
9988 case bfd_link_hash_new
:
9989 case bfd_link_hash_warning
:
9993 case bfd_link_hash_undefined
:
9994 case bfd_link_hash_undefweak
:
9995 input_sec
= bfd_und_section_ptr
;
9996 sym
.st_shndx
= SHN_UNDEF
;
9999 case bfd_link_hash_defined
:
10000 case bfd_link_hash_defweak
:
10002 input_sec
= h
->root
.u
.def
.section
;
10003 if (input_sec
->output_section
!= NULL
)
10006 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
10007 input_sec
->output_section
);
10008 if (sym
.st_shndx
== SHN_BAD
)
10011 /* xgettext:c-format */
10012 (_("%pB: could not find output section %pA for input section %pA"),
10013 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
10014 bfd_set_error (bfd_error_nonrepresentable_section
);
10015 eoinfo
->failed
= TRUE
;
10019 /* ELF symbols in relocatable files are section relative,
10020 but in nonrelocatable files they are virtual
10022 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
10023 if (!bfd_link_relocatable (flinfo
->info
))
10025 sym
.st_value
+= input_sec
->output_section
->vma
;
10026 if (h
->type
== STT_TLS
)
10028 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
10029 if (tls_sec
!= NULL
)
10030 sym
.st_value
-= tls_sec
->vma
;
10036 BFD_ASSERT (input_sec
->owner
== NULL
10037 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
10038 sym
.st_shndx
= SHN_UNDEF
;
10039 input_sec
= bfd_und_section_ptr
;
10044 case bfd_link_hash_common
:
10045 input_sec
= h
->root
.u
.c
.p
->section
;
10046 sym
.st_shndx
= bed
->common_section_index (input_sec
);
10047 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
10050 case bfd_link_hash_indirect
:
10051 /* These symbols are created by symbol versioning. They point
10052 to the decorated version of the name. For example, if the
10053 symbol foo@@GNU_1.2 is the default, which should be used when
10054 foo is used with no version, then we add an indirect symbol
10055 foo which points to foo@@GNU_1.2. We ignore these symbols,
10056 since the indirected symbol is already in the hash table. */
10060 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
10061 switch (h
->root
.type
)
10063 case bfd_link_hash_common
:
10064 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10066 case bfd_link_hash_defined
:
10067 case bfd_link_hash_defweak
:
10068 if (bed
->common_definition (&sym
))
10069 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10073 case bfd_link_hash_undefined
:
10074 case bfd_link_hash_undefweak
:
10080 if (h
->forced_local
)
10082 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
10083 /* Turn off visibility on local symbol. */
10084 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
10086 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10087 else if (h
->unique_global
&& h
->def_regular
)
10088 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
10089 else if (h
->root
.type
== bfd_link_hash_undefweak
10090 || h
->root
.type
== bfd_link_hash_defweak
)
10091 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
10093 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
10094 sym
.st_target_internal
= h
->target_internal
;
10096 /* Give the processor backend a chance to tweak the symbol value,
10097 and also to finish up anything that needs to be done for this
10098 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10099 forced local syms when non-shared is due to a historical quirk.
10100 STT_GNU_IFUNC symbol must go through PLT. */
10101 if ((h
->type
== STT_GNU_IFUNC
10103 && !bfd_link_relocatable (flinfo
->info
))
10104 || ((h
->dynindx
!= -1
10105 || h
->forced_local
)
10106 && ((bfd_link_pic (flinfo
->info
)
10107 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
10108 || h
->root
.type
!= bfd_link_hash_undefweak
))
10109 || !h
->forced_local
)
10110 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
10112 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
10113 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
10115 eoinfo
->failed
= TRUE
;
10120 /* If we are marking the symbol as undefined, and there are no
10121 non-weak references to this symbol from a regular object, then
10122 mark the symbol as weak undefined; if there are non-weak
10123 references, mark the symbol as strong. We can't do this earlier,
10124 because it might not be marked as undefined until the
10125 finish_dynamic_symbol routine gets through with it. */
10126 if (sym
.st_shndx
== SHN_UNDEF
10128 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
10129 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
10132 type
= ELF_ST_TYPE (sym
.st_info
);
10134 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10135 if (type
== STT_GNU_IFUNC
)
10138 if (h
->ref_regular_nonweak
)
10139 bindtype
= STB_GLOBAL
;
10141 bindtype
= STB_WEAK
;
10142 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
10145 /* If this is a symbol defined in a dynamic library, don't use the
10146 symbol size from the dynamic library. Relinking an executable
10147 against a new library may introduce gratuitous changes in the
10148 executable's symbols if we keep the size. */
10149 if (sym
.st_shndx
== SHN_UNDEF
10154 /* If a non-weak symbol with non-default visibility is not defined
10155 locally, it is a fatal error. */
10156 if (!bfd_link_relocatable (flinfo
->info
)
10157 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
10158 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
10159 && h
->root
.type
== bfd_link_hash_undefined
10160 && !h
->def_regular
)
10164 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
10165 /* xgettext:c-format */
10166 msg
= _("%pB: protected symbol `%s' isn't defined");
10167 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
10168 /* xgettext:c-format */
10169 msg
= _("%pB: internal symbol `%s' isn't defined");
10171 /* xgettext:c-format */
10172 msg
= _("%pB: hidden symbol `%s' isn't defined");
10173 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
10174 bfd_set_error (bfd_error_bad_value
);
10175 eoinfo
->failed
= TRUE
;
10179 /* If this symbol should be put in the .dynsym section, then put it
10180 there now. We already know the symbol index. We also fill in
10181 the entry in the .hash section. */
10182 if (h
->dynindx
!= -1
10183 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
10184 && elf_hash_table (flinfo
->info
)->dynsym
!= NULL
10185 && !discarded_section (elf_hash_table (flinfo
->info
)->dynsym
))
10189 /* Since there is no version information in the dynamic string,
10190 if there is no version info in symbol version section, we will
10191 have a run-time problem if not linking executable, referenced
10192 by shared library, or not bound locally. */
10193 if (h
->verinfo
.verdef
== NULL
10194 && (!bfd_link_executable (flinfo
->info
)
10196 || !h
->def_regular
))
10198 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
10200 if (p
&& p
[1] != '\0')
10203 /* xgettext:c-format */
10204 (_("%pB: no symbol version section for versioned symbol `%s'"),
10205 flinfo
->output_bfd
, h
->root
.root
.string
);
10206 eoinfo
->failed
= TRUE
;
10211 sym
.st_name
= h
->dynstr_index
;
10212 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
10213 + h
->dynindx
* bed
->s
->sizeof_sym
);
10214 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
10216 eoinfo
->failed
= TRUE
;
10219 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
10221 if (flinfo
->hash_sec
!= NULL
)
10223 size_t hash_entry_size
;
10224 bfd_byte
*bucketpos
;
10226 size_t bucketcount
;
10229 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
10230 bucket
= h
->u
.elf_hash_value
% bucketcount
;
10233 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
10234 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
10235 + (bucket
+ 2) * hash_entry_size
);
10236 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
10237 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
10239 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
10240 ((bfd_byte
*) flinfo
->hash_sec
->contents
10241 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
10244 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
10246 Elf_Internal_Versym iversym
;
10247 Elf_External_Versym
*eversym
;
10249 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
10251 if (h
->verinfo
.verdef
== NULL
10252 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
10253 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
10254 iversym
.vs_vers
= 0;
10256 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
10260 if (h
->verinfo
.vertree
== NULL
)
10261 iversym
.vs_vers
= 1;
10263 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
10264 if (flinfo
->info
->create_default_symver
)
10268 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10269 defined locally. */
10270 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
10271 iversym
.vs_vers
|= VERSYM_HIDDEN
;
10273 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
10274 eversym
+= h
->dynindx
;
10275 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
10279 /* If the symbol is undefined, and we didn't output it to .dynsym,
10280 strip it from .symtab too. Obviously we can't do this for
10281 relocatable output or when needed for --emit-relocs. */
10282 else if (input_sec
== bfd_und_section_ptr
10284 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10285 && (h
->mark
!= 1 || ELF_ST_BIND (sym
.st_info
) != STB_GLOBAL
)
10286 && !bfd_link_relocatable (flinfo
->info
))
10289 /* Also strip others that we couldn't earlier due to dynamic symbol
10293 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
10296 /* Output a FILE symbol so that following locals are not associated
10297 with the wrong input file. We need one for forced local symbols
10298 if we've seen more than one FILE symbol or when we have exactly
10299 one FILE symbol but global symbols are present in a file other
10300 than the one with the FILE symbol. We also need one if linker
10301 defined symbols are present. In practice these conditions are
10302 always met, so just emit the FILE symbol unconditionally. */
10303 if (eoinfo
->localsyms
10304 && !eoinfo
->file_sym_done
10305 && eoinfo
->flinfo
->filesym_count
!= 0)
10307 Elf_Internal_Sym fsym
;
10309 memset (&fsym
, 0, sizeof (fsym
));
10310 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10311 fsym
.st_shndx
= SHN_ABS
;
10312 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
10313 bfd_und_section_ptr
, NULL
))
10316 eoinfo
->file_sym_done
= TRUE
;
10319 indx
= bfd_get_symcount (flinfo
->output_bfd
);
10320 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
10324 eoinfo
->failed
= TRUE
;
10329 else if (h
->indx
== -2)
10335 /* Return TRUE if special handling is done for relocs in SEC against
10336 symbols defined in discarded sections. */
10339 elf_section_ignore_discarded_relocs (asection
*sec
)
10341 const struct elf_backend_data
*bed
;
10343 switch (sec
->sec_info_type
)
10345 case SEC_INFO_TYPE_STABS
:
10346 case SEC_INFO_TYPE_EH_FRAME
:
10347 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10353 bed
= get_elf_backend_data (sec
->owner
);
10354 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
10355 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
10361 /* Return a mask saying how ld should treat relocations in SEC against
10362 symbols defined in discarded sections. If this function returns
10363 COMPLAIN set, ld will issue a warning message. If this function
10364 returns PRETEND set, and the discarded section was link-once and the
10365 same size as the kept link-once section, ld will pretend that the
10366 symbol was actually defined in the kept section. Otherwise ld will
10367 zero the reloc (at least that is the intent, but some cooperation by
10368 the target dependent code is needed, particularly for REL targets). */
10371 _bfd_elf_default_action_discarded (asection
*sec
)
10373 if (sec
->flags
& SEC_DEBUGGING
)
10376 if (strcmp (".eh_frame", sec
->name
) == 0)
10379 if (strcmp (".gcc_except_table", sec
->name
) == 0)
10382 return COMPLAIN
| PRETEND
;
10385 /* Find a match between a section and a member of a section group. */
10388 match_group_member (asection
*sec
, asection
*group
,
10389 struct bfd_link_info
*info
)
10391 asection
*first
= elf_next_in_group (group
);
10392 asection
*s
= first
;
10396 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
10399 s
= elf_next_in_group (s
);
10407 /* Check if the kept section of a discarded section SEC can be used
10408 to replace it. Return the replacement if it is OK. Otherwise return
10412 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
10416 kept
= sec
->kept_section
;
10419 if ((kept
->flags
& SEC_GROUP
) != 0)
10420 kept
= match_group_member (sec
, kept
, info
);
10422 && ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
10423 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
)))
10425 sec
->kept_section
= kept
;
10430 /* Link an input file into the linker output file. This function
10431 handles all the sections and relocations of the input file at once.
10432 This is so that we only have to read the local symbols once, and
10433 don't have to keep them in memory. */
10436 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
10438 int (*relocate_section
)
10439 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
10440 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
10442 Elf_Internal_Shdr
*symtab_hdr
;
10443 size_t locsymcount
;
10445 Elf_Internal_Sym
*isymbuf
;
10446 Elf_Internal_Sym
*isym
;
10447 Elf_Internal_Sym
*isymend
;
10449 asection
**ppsection
;
10451 const struct elf_backend_data
*bed
;
10452 struct elf_link_hash_entry
**sym_hashes
;
10453 bfd_size_type address_size
;
10454 bfd_vma r_type_mask
;
10456 bfd_boolean have_file_sym
= FALSE
;
10458 output_bfd
= flinfo
->output_bfd
;
10459 bed
= get_elf_backend_data (output_bfd
);
10460 relocate_section
= bed
->elf_backend_relocate_section
;
10462 /* If this is a dynamic object, we don't want to do anything here:
10463 we don't want the local symbols, and we don't want the section
10465 if ((input_bfd
->flags
& DYNAMIC
) != 0)
10468 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
10469 if (elf_bad_symtab (input_bfd
))
10471 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10476 locsymcount
= symtab_hdr
->sh_info
;
10477 extsymoff
= symtab_hdr
->sh_info
;
10480 /* Read the local symbols. */
10481 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10482 if (isymbuf
== NULL
&& locsymcount
!= 0)
10484 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
10485 flinfo
->internal_syms
,
10486 flinfo
->external_syms
,
10487 flinfo
->locsym_shndx
);
10488 if (isymbuf
== NULL
)
10492 /* Find local symbol sections and adjust values of symbols in
10493 SEC_MERGE sections. Write out those local symbols we know are
10494 going into the output file. */
10495 isymend
= isymbuf
+ locsymcount
;
10496 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
10498 isym
++, pindex
++, ppsection
++)
10502 Elf_Internal_Sym osym
;
10508 if (elf_bad_symtab (input_bfd
))
10510 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
10517 if (isym
->st_shndx
== SHN_UNDEF
)
10518 isec
= bfd_und_section_ptr
;
10519 else if (isym
->st_shndx
== SHN_ABS
)
10520 isec
= bfd_abs_section_ptr
;
10521 else if (isym
->st_shndx
== SHN_COMMON
)
10522 isec
= bfd_com_section_ptr
;
10525 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
10528 /* Don't attempt to output symbols with st_shnx in the
10529 reserved range other than SHN_ABS and SHN_COMMON. */
10530 isec
= bfd_und_section_ptr
;
10532 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
10533 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
10535 _bfd_merged_section_offset (output_bfd
, &isec
,
10536 elf_section_data (isec
)->sec_info
,
10542 /* Don't output the first, undefined, symbol. In fact, don't
10543 output any undefined local symbol. */
10544 if (isec
== bfd_und_section_ptr
)
10547 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
10549 /* We never output section symbols. Instead, we use the
10550 section symbol of the corresponding section in the output
10555 /* If we are stripping all symbols, we don't want to output this
10557 if (flinfo
->info
->strip
== strip_all
)
10560 /* If we are discarding all local symbols, we don't want to
10561 output this one. If we are generating a relocatable output
10562 file, then some of the local symbols may be required by
10563 relocs; we output them below as we discover that they are
10565 if (flinfo
->info
->discard
== discard_all
)
10568 /* If this symbol is defined in a section which we are
10569 discarding, we don't need to keep it. */
10570 if (isym
->st_shndx
!= SHN_UNDEF
10571 && isym
->st_shndx
< SHN_LORESERVE
10572 && isec
->output_section
== NULL
10573 && flinfo
->info
->non_contiguous_regions
10574 && flinfo
->info
->non_contiguous_regions_warnings
)
10576 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10577 "discards section `%s' from '%s'\n"),
10578 isec
->name
, isec
->owner
->filename
);
10582 if (isym
->st_shndx
!= SHN_UNDEF
10583 && isym
->st_shndx
< SHN_LORESERVE
10584 && bfd_section_removed_from_list (output_bfd
,
10585 isec
->output_section
))
10588 /* Get the name of the symbol. */
10589 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
10594 /* See if we are discarding symbols with this name. */
10595 if ((flinfo
->info
->strip
== strip_some
10596 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
10598 || (((flinfo
->info
->discard
== discard_sec_merge
10599 && (isec
->flags
& SEC_MERGE
)
10600 && !bfd_link_relocatable (flinfo
->info
))
10601 || flinfo
->info
->discard
== discard_l
)
10602 && bfd_is_local_label_name (input_bfd
, name
)))
10605 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
10607 if (input_bfd
->lto_output
)
10608 /* -flto puts a temp file name here. This means builds
10609 are not reproducible. Discard the symbol. */
10611 have_file_sym
= TRUE
;
10612 flinfo
->filesym_count
+= 1;
10614 if (!have_file_sym
)
10616 /* In the absence of debug info, bfd_find_nearest_line uses
10617 FILE symbols to determine the source file for local
10618 function symbols. Provide a FILE symbol here if input
10619 files lack such, so that their symbols won't be
10620 associated with a previous input file. It's not the
10621 source file, but the best we can do. */
10622 have_file_sym
= TRUE
;
10623 flinfo
->filesym_count
+= 1;
10624 memset (&osym
, 0, sizeof (osym
));
10625 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10626 osym
.st_shndx
= SHN_ABS
;
10627 if (!elf_link_output_symstrtab (flinfo
,
10628 (input_bfd
->lto_output
? NULL
10629 : input_bfd
->filename
),
10630 &osym
, bfd_abs_section_ptr
,
10637 /* Adjust the section index for the output file. */
10638 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10639 isec
->output_section
);
10640 if (osym
.st_shndx
== SHN_BAD
)
10643 /* ELF symbols in relocatable files are section relative, but
10644 in executable files they are virtual addresses. Note that
10645 this code assumes that all ELF sections have an associated
10646 BFD section with a reasonable value for output_offset; below
10647 we assume that they also have a reasonable value for
10648 output_section. Any special sections must be set up to meet
10649 these requirements. */
10650 osym
.st_value
+= isec
->output_offset
;
10651 if (!bfd_link_relocatable (flinfo
->info
))
10653 osym
.st_value
+= isec
->output_section
->vma
;
10654 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
10656 /* STT_TLS symbols are relative to PT_TLS segment base. */
10657 if (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
)
10658 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
10660 osym
.st_info
= ELF_ST_INFO (ELF_ST_BIND (osym
.st_info
),
10665 indx
= bfd_get_symcount (output_bfd
);
10666 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
10673 if (bed
->s
->arch_size
== 32)
10675 r_type_mask
= 0xff;
10681 r_type_mask
= 0xffffffff;
10686 /* Relocate the contents of each section. */
10687 sym_hashes
= elf_sym_hashes (input_bfd
);
10688 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
10690 bfd_byte
*contents
;
10692 if (! o
->linker_mark
)
10694 /* This section was omitted from the link. */
10698 if (!flinfo
->info
->resolve_section_groups
10699 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
10701 /* Deal with the group signature symbol. */
10702 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
10703 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
10704 asection
*osec
= o
->output_section
;
10706 BFD_ASSERT (bfd_link_relocatable (flinfo
->info
));
10707 if (symndx
>= locsymcount
10708 || (elf_bad_symtab (input_bfd
)
10709 && flinfo
->sections
[symndx
] == NULL
))
10711 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
10712 while (h
->root
.type
== bfd_link_hash_indirect
10713 || h
->root
.type
== bfd_link_hash_warning
)
10714 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10715 /* Arrange for symbol to be output. */
10717 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
10719 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
10721 /* We'll use the output section target_index. */
10722 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10723 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
10727 if (flinfo
->indices
[symndx
] == -1)
10729 /* Otherwise output the local symbol now. */
10730 Elf_Internal_Sym sym
= isymbuf
[symndx
];
10731 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10736 name
= bfd_elf_string_from_elf_section (input_bfd
,
10737 symtab_hdr
->sh_link
,
10742 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10744 if (sym
.st_shndx
== SHN_BAD
)
10747 sym
.st_value
+= o
->output_offset
;
10749 indx
= bfd_get_symcount (output_bfd
);
10750 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
10755 flinfo
->indices
[symndx
] = indx
;
10759 elf_section_data (osec
)->this_hdr
.sh_info
10760 = flinfo
->indices
[symndx
];
10764 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
10765 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
10768 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
10770 /* Section was created by _bfd_elf_link_create_dynamic_sections
10775 /* Get the contents of the section. They have been cached by a
10776 relaxation routine. Note that o is a section in an input
10777 file, so the contents field will not have been set by any of
10778 the routines which work on output files. */
10779 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
10781 contents
= elf_section_data (o
)->this_hdr
.contents
;
10782 if (bed
->caches_rawsize
10784 && o
->rawsize
< o
->size
)
10786 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
10787 contents
= flinfo
->contents
;
10792 contents
= flinfo
->contents
;
10793 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
10797 if ((o
->flags
& SEC_RELOC
) != 0)
10799 Elf_Internal_Rela
*internal_relocs
;
10800 Elf_Internal_Rela
*rel
, *relend
;
10801 int action_discarded
;
10804 /* Get the swapped relocs. */
10806 = _bfd_elf_link_read_relocs (input_bfd
, o
, flinfo
->external_relocs
,
10807 flinfo
->internal_relocs
, FALSE
);
10808 if (internal_relocs
== NULL
10809 && o
->reloc_count
> 0)
10812 /* We need to reverse-copy input .ctors/.dtors sections if
10813 they are placed in .init_array/.finit_array for output. */
10814 if (o
->size
> address_size
10815 && ((strncmp (o
->name
, ".ctors", 6) == 0
10816 && strcmp (o
->output_section
->name
,
10817 ".init_array") == 0)
10818 || (strncmp (o
->name
, ".dtors", 6) == 0
10819 && strcmp (o
->output_section
->name
,
10820 ".fini_array") == 0))
10821 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
10823 if (o
->size
* bed
->s
->int_rels_per_ext_rel
10824 != o
->reloc_count
* address_size
)
10827 /* xgettext:c-format */
10828 (_("error: %pB: size of section %pA is not "
10829 "multiple of address size"),
10831 bfd_set_error (bfd_error_bad_value
);
10834 o
->flags
|= SEC_ELF_REVERSE_COPY
;
10837 action_discarded
= -1;
10838 if (!elf_section_ignore_discarded_relocs (o
))
10839 action_discarded
= (*bed
->action_discarded
) (o
);
10841 /* Run through the relocs evaluating complex reloc symbols and
10842 looking for relocs against symbols from discarded sections
10843 or section symbols from removed link-once sections.
10844 Complain about relocs against discarded sections. Zero
10845 relocs against removed link-once sections. */
10847 rel
= internal_relocs
;
10848 relend
= rel
+ o
->reloc_count
;
10849 for ( ; rel
< relend
; rel
++)
10851 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
10852 unsigned int s_type
;
10853 asection
**ps
, *sec
;
10854 struct elf_link_hash_entry
*h
= NULL
;
10855 const char *sym_name
;
10857 if (r_symndx
== STN_UNDEF
)
10860 if (r_symndx
>= locsymcount
10861 || (elf_bad_symtab (input_bfd
)
10862 && flinfo
->sections
[r_symndx
] == NULL
))
10864 h
= sym_hashes
[r_symndx
- extsymoff
];
10866 /* Badly formatted input files can contain relocs that
10867 reference non-existant symbols. Check here so that
10868 we do not seg fault. */
10872 /* xgettext:c-format */
10873 (_("error: %pB contains a reloc (%#" PRIx64
") for section %pA "
10874 "that references a non-existent global symbol"),
10875 input_bfd
, (uint64_t) rel
->r_info
, o
);
10876 bfd_set_error (bfd_error_bad_value
);
10880 while (h
->root
.type
== bfd_link_hash_indirect
10881 || h
->root
.type
== bfd_link_hash_warning
)
10882 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10886 /* If a plugin symbol is referenced from a non-IR file,
10887 mark the symbol as undefined. Note that the
10888 linker may attach linker created dynamic sections
10889 to the plugin bfd. Symbols defined in linker
10890 created sections are not plugin symbols. */
10891 if ((h
->root
.non_ir_ref_regular
10892 || h
->root
.non_ir_ref_dynamic
)
10893 && (h
->root
.type
== bfd_link_hash_defined
10894 || h
->root
.type
== bfd_link_hash_defweak
)
10895 && (h
->root
.u
.def
.section
->flags
10896 & SEC_LINKER_CREATED
) == 0
10897 && h
->root
.u
.def
.section
->owner
!= NULL
10898 && (h
->root
.u
.def
.section
->owner
->flags
10899 & BFD_PLUGIN
) != 0)
10901 h
->root
.type
= bfd_link_hash_undefined
;
10902 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
10906 if (h
->root
.type
== bfd_link_hash_defined
10907 || h
->root
.type
== bfd_link_hash_defweak
)
10908 ps
= &h
->root
.u
.def
.section
;
10910 sym_name
= h
->root
.root
.string
;
10914 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
10916 s_type
= ELF_ST_TYPE (sym
->st_info
);
10917 ps
= &flinfo
->sections
[r_symndx
];
10918 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
10922 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
10923 && !bfd_link_relocatable (flinfo
->info
))
10926 bfd_vma dot
= (rel
->r_offset
10927 + o
->output_offset
+ o
->output_section
->vma
);
10929 printf ("Encountered a complex symbol!");
10930 printf (" (input_bfd %s, section %s, reloc %ld\n",
10931 input_bfd
->filename
, o
->name
,
10932 (long) (rel
- internal_relocs
));
10933 printf (" symbol: idx %8.8lx, name %s\n",
10934 r_symndx
, sym_name
);
10935 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10936 (unsigned long) rel
->r_info
,
10937 (unsigned long) rel
->r_offset
);
10939 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
10940 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
10943 /* Symbol evaluated OK. Update to absolute value. */
10944 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
10949 if (action_discarded
!= -1 && ps
!= NULL
)
10951 /* Complain if the definition comes from a
10952 discarded section. */
10953 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
10955 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
10956 if (action_discarded
& COMPLAIN
)
10957 (*flinfo
->info
->callbacks
->einfo
)
10958 /* xgettext:c-format */
10959 (_("%X`%s' referenced in section `%pA' of %pB: "
10960 "defined in discarded section `%pA' of %pB\n"),
10961 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
10963 /* Try to do the best we can to support buggy old
10964 versions of gcc. Pretend that the symbol is
10965 really defined in the kept linkonce section.
10966 FIXME: This is quite broken. Modifying the
10967 symbol here means we will be changing all later
10968 uses of the symbol, not just in this section. */
10969 if (action_discarded
& PRETEND
)
10973 kept
= _bfd_elf_check_kept_section (sec
,
10985 /* Relocate the section by invoking a back end routine.
10987 The back end routine is responsible for adjusting the
10988 section contents as necessary, and (if using Rela relocs
10989 and generating a relocatable output file) adjusting the
10990 reloc addend as necessary.
10992 The back end routine does not have to worry about setting
10993 the reloc address or the reloc symbol index.
10995 The back end routine is given a pointer to the swapped in
10996 internal symbols, and can access the hash table entries
10997 for the external symbols via elf_sym_hashes (input_bfd).
10999 When generating relocatable output, the back end routine
11000 must handle STB_LOCAL/STT_SECTION symbols specially. The
11001 output symbol is going to be a section symbol
11002 corresponding to the output section, which will require
11003 the addend to be adjusted. */
11005 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
11006 input_bfd
, o
, contents
,
11014 || bfd_link_relocatable (flinfo
->info
)
11015 || flinfo
->info
->emitrelocations
)
11017 Elf_Internal_Rela
*irela
;
11018 Elf_Internal_Rela
*irelaend
, *irelamid
;
11019 bfd_vma last_offset
;
11020 struct elf_link_hash_entry
**rel_hash
;
11021 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
11022 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
11023 unsigned int next_erel
;
11024 bfd_boolean rela_normal
;
11025 struct bfd_elf_section_data
*esdi
, *esdo
;
11027 esdi
= elf_section_data (o
);
11028 esdo
= elf_section_data (o
->output_section
);
11029 rela_normal
= FALSE
;
11031 /* Adjust the reloc addresses and symbol indices. */
11033 irela
= internal_relocs
;
11034 irelaend
= irela
+ o
->reloc_count
;
11035 rel_hash
= esdo
->rel
.hashes
+ esdo
->rel
.count
;
11036 /* We start processing the REL relocs, if any. When we reach
11037 IRELAMID in the loop, we switch to the RELA relocs. */
11039 if (esdi
->rel
.hdr
!= NULL
)
11040 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
11041 * bed
->s
->int_rels_per_ext_rel
);
11042 rel_hash_list
= rel_hash
;
11043 rela_hash_list
= NULL
;
11044 last_offset
= o
->output_offset
;
11045 if (!bfd_link_relocatable (flinfo
->info
))
11046 last_offset
+= o
->output_section
->vma
;
11047 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
11049 unsigned long r_symndx
;
11051 Elf_Internal_Sym sym
;
11053 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
11059 if (irela
== irelamid
)
11061 rel_hash
= esdo
->rela
.hashes
+ esdo
->rela
.count
;
11062 rela_hash_list
= rel_hash
;
11063 rela_normal
= bed
->rela_normal
;
11066 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
11069 if (irela
->r_offset
>= (bfd_vma
) -2)
11071 /* This is a reloc for a deleted entry or somesuch.
11072 Turn it into an R_*_NONE reloc, at the same
11073 offset as the last reloc. elf_eh_frame.c and
11074 bfd_elf_discard_info rely on reloc offsets
11076 irela
->r_offset
= last_offset
;
11078 irela
->r_addend
= 0;
11082 irela
->r_offset
+= o
->output_offset
;
11084 /* Relocs in an executable have to be virtual addresses. */
11085 if (!bfd_link_relocatable (flinfo
->info
))
11086 irela
->r_offset
+= o
->output_section
->vma
;
11088 last_offset
= irela
->r_offset
;
11090 r_symndx
= irela
->r_info
>> r_sym_shift
;
11091 if (r_symndx
== STN_UNDEF
)
11094 if (r_symndx
>= locsymcount
11095 || (elf_bad_symtab (input_bfd
)
11096 && flinfo
->sections
[r_symndx
] == NULL
))
11098 struct elf_link_hash_entry
*rh
;
11099 unsigned long indx
;
11101 /* This is a reloc against a global symbol. We
11102 have not yet output all the local symbols, so
11103 we do not know the symbol index of any global
11104 symbol. We set the rel_hash entry for this
11105 reloc to point to the global hash table entry
11106 for this symbol. The symbol index is then
11107 set at the end of bfd_elf_final_link. */
11108 indx
= r_symndx
- extsymoff
;
11109 rh
= elf_sym_hashes (input_bfd
)[indx
];
11110 while (rh
->root
.type
== bfd_link_hash_indirect
11111 || rh
->root
.type
== bfd_link_hash_warning
)
11112 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
11114 /* Setting the index to -2 tells
11115 elf_link_output_extsym that this symbol is
11116 used by a reloc. */
11117 BFD_ASSERT (rh
->indx
< 0);
11124 /* This is a reloc against a local symbol. */
11127 sym
= isymbuf
[r_symndx
];
11128 sec
= flinfo
->sections
[r_symndx
];
11129 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
11131 /* I suppose the backend ought to fill in the
11132 section of any STT_SECTION symbol against a
11133 processor specific section. */
11134 r_symndx
= STN_UNDEF
;
11135 if (bfd_is_abs_section (sec
))
11137 else if (sec
== NULL
|| sec
->owner
== NULL
)
11139 bfd_set_error (bfd_error_bad_value
);
11144 asection
*osec
= sec
->output_section
;
11146 /* If we have discarded a section, the output
11147 section will be the absolute section. In
11148 case of discarded SEC_MERGE sections, use
11149 the kept section. relocate_section should
11150 have already handled discarded linkonce
11152 if (bfd_is_abs_section (osec
)
11153 && sec
->kept_section
!= NULL
11154 && sec
->kept_section
->output_section
!= NULL
)
11156 osec
= sec
->kept_section
->output_section
;
11157 irela
->r_addend
-= osec
->vma
;
11160 if (!bfd_is_abs_section (osec
))
11162 r_symndx
= osec
->target_index
;
11163 if (r_symndx
== STN_UNDEF
)
11165 irela
->r_addend
+= osec
->vma
;
11166 osec
= _bfd_nearby_section (output_bfd
, osec
,
11168 irela
->r_addend
-= osec
->vma
;
11169 r_symndx
= osec
->target_index
;
11174 /* Adjust the addend according to where the
11175 section winds up in the output section. */
11177 irela
->r_addend
+= sec
->output_offset
;
11181 if (flinfo
->indices
[r_symndx
] == -1)
11183 unsigned long shlink
;
11188 if (flinfo
->info
->strip
== strip_all
)
11190 /* You can't do ld -r -s. */
11191 bfd_set_error (bfd_error_invalid_operation
);
11195 /* This symbol was skipped earlier, but
11196 since it is needed by a reloc, we
11197 must output it now. */
11198 shlink
= symtab_hdr
->sh_link
;
11199 name
= (bfd_elf_string_from_elf_section
11200 (input_bfd
, shlink
, sym
.st_name
));
11204 osec
= sec
->output_section
;
11206 _bfd_elf_section_from_bfd_section (output_bfd
,
11208 if (sym
.st_shndx
== SHN_BAD
)
11211 sym
.st_value
+= sec
->output_offset
;
11212 if (!bfd_link_relocatable (flinfo
->info
))
11214 sym
.st_value
+= osec
->vma
;
11215 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
11217 struct elf_link_hash_table
*htab
11218 = elf_hash_table (flinfo
->info
);
11220 /* STT_TLS symbols are relative to PT_TLS
11222 if (htab
->tls_sec
!= NULL
)
11223 sym
.st_value
-= htab
->tls_sec
->vma
;
11226 = ELF_ST_INFO (ELF_ST_BIND (sym
.st_info
),
11231 indx
= bfd_get_symcount (output_bfd
);
11232 ret
= elf_link_output_symstrtab (flinfo
, name
,
11238 flinfo
->indices
[r_symndx
] = indx
;
11243 r_symndx
= flinfo
->indices
[r_symndx
];
11246 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
11247 | (irela
->r_info
& r_type_mask
));
11250 /* Swap out the relocs. */
11251 input_rel_hdr
= esdi
->rel
.hdr
;
11252 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
11254 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11259 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
11260 * bed
->s
->int_rels_per_ext_rel
);
11261 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
11264 input_rela_hdr
= esdi
->rela
.hdr
;
11265 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
11267 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11276 /* Write out the modified section contents. */
11277 if (bed
->elf_backend_write_section
11278 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
11281 /* Section written out. */
11283 else switch (o
->sec_info_type
)
11285 case SEC_INFO_TYPE_STABS
:
11286 if (! (_bfd_write_section_stabs
11288 &elf_hash_table (flinfo
->info
)->stab_info
,
11289 o
, &elf_section_data (o
)->sec_info
, contents
)))
11292 case SEC_INFO_TYPE_MERGE
:
11293 if (! _bfd_write_merged_section (output_bfd
, o
,
11294 elf_section_data (o
)->sec_info
))
11297 case SEC_INFO_TYPE_EH_FRAME
:
11299 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
11304 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
11306 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
11314 if (! (o
->flags
& SEC_EXCLUDE
))
11316 file_ptr offset
= (file_ptr
) o
->output_offset
;
11317 bfd_size_type todo
= o
->size
;
11319 offset
*= bfd_octets_per_byte (output_bfd
, o
);
11321 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
11323 /* Reverse-copy input section to output. */
11326 todo
-= address_size
;
11327 if (! bfd_set_section_contents (output_bfd
,
11335 offset
+= address_size
;
11339 else if (! bfd_set_section_contents (output_bfd
,
11353 /* Generate a reloc when linking an ELF file. This is a reloc
11354 requested by the linker, and does not come from any input file. This
11355 is used to build constructor and destructor tables when linking
11359 elf_reloc_link_order (bfd
*output_bfd
,
11360 struct bfd_link_info
*info
,
11361 asection
*output_section
,
11362 struct bfd_link_order
*link_order
)
11364 reloc_howto_type
*howto
;
11368 struct bfd_elf_section_reloc_data
*reldata
;
11369 struct elf_link_hash_entry
**rel_hash_ptr
;
11370 Elf_Internal_Shdr
*rel_hdr
;
11371 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
11372 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
11375 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
11377 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
11380 bfd_set_error (bfd_error_bad_value
);
11384 addend
= link_order
->u
.reloc
.p
->addend
;
11387 reldata
= &esdo
->rel
;
11388 else if (esdo
->rela
.hdr
)
11389 reldata
= &esdo
->rela
;
11396 /* Figure out the symbol index. */
11397 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
11398 if (link_order
->type
== bfd_section_reloc_link_order
)
11400 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
11401 BFD_ASSERT (indx
!= 0);
11402 *rel_hash_ptr
= NULL
;
11406 struct elf_link_hash_entry
*h
;
11408 /* Treat a reloc against a defined symbol as though it were
11409 actually against the section. */
11410 h
= ((struct elf_link_hash_entry
*)
11411 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
11412 link_order
->u
.reloc
.p
->u
.name
,
11413 FALSE
, FALSE
, TRUE
));
11415 && (h
->root
.type
== bfd_link_hash_defined
11416 || h
->root
.type
== bfd_link_hash_defweak
))
11420 section
= h
->root
.u
.def
.section
;
11421 indx
= section
->output_section
->target_index
;
11422 *rel_hash_ptr
= NULL
;
11423 /* It seems that we ought to add the symbol value to the
11424 addend here, but in practice it has already been added
11425 because it was passed to constructor_callback. */
11426 addend
+= section
->output_section
->vma
+ section
->output_offset
;
11428 else if (h
!= NULL
)
11430 /* Setting the index to -2 tells elf_link_output_extsym that
11431 this symbol is used by a reloc. */
11438 (*info
->callbacks
->unattached_reloc
)
11439 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
11444 /* If this is an inplace reloc, we must write the addend into the
11446 if (howto
->partial_inplace
&& addend
!= 0)
11448 bfd_size_type size
;
11449 bfd_reloc_status_type rstat
;
11452 const char *sym_name
;
11453 bfd_size_type octets
;
11455 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
11456 buf
= (bfd_byte
*) bfd_zmalloc (size
);
11457 if (buf
== NULL
&& size
!= 0)
11459 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
11466 case bfd_reloc_outofrange
:
11469 case bfd_reloc_overflow
:
11470 if (link_order
->type
== bfd_section_reloc_link_order
)
11471 sym_name
= bfd_section_name (link_order
->u
.reloc
.p
->u
.section
);
11473 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
11474 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
11475 howto
->name
, addend
, NULL
, NULL
,
11480 octets
= link_order
->offset
* bfd_octets_per_byte (output_bfd
,
11482 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
11489 /* The address of a reloc is relative to the section in a
11490 relocatable file, and is a virtual address in an executable
11492 offset
= link_order
->offset
;
11493 if (! bfd_link_relocatable (info
))
11494 offset
+= output_section
->vma
;
11496 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
11498 irel
[i
].r_offset
= offset
;
11499 irel
[i
].r_info
= 0;
11500 irel
[i
].r_addend
= 0;
11502 if (bed
->s
->arch_size
== 32)
11503 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
11505 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
11507 rel_hdr
= reldata
->hdr
;
11508 erel
= rel_hdr
->contents
;
11509 if (rel_hdr
->sh_type
== SHT_REL
)
11511 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
11512 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
11516 irel
[0].r_addend
= addend
;
11517 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
11518 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
11527 /* Compare two sections based on the locations of the sections they are
11528 linked to. Used by elf_fixup_link_order. */
11531 compare_link_order (const void *a
, const void *b
)
11533 const struct bfd_link_order
*alo
= *(const struct bfd_link_order
**) a
;
11534 const struct bfd_link_order
*blo
= *(const struct bfd_link_order
**) b
;
11535 asection
*asec
= elf_linked_to_section (alo
->u
.indirect
.section
);
11536 asection
*bsec
= elf_linked_to_section (blo
->u
.indirect
.section
);
11537 bfd_vma apos
= asec
->output_section
->lma
+ asec
->output_offset
;
11538 bfd_vma bpos
= bsec
->output_section
->lma
+ bsec
->output_offset
;
11545 /* The only way we should get matching LMAs is when the first of two
11546 sections has zero size. */
11547 if (asec
->size
< bsec
->size
)
11549 if (asec
->size
> bsec
->size
)
11552 /* If they are both zero size then they almost certainly have the same
11553 VMA and thus are not ordered with respect to each other. Test VMA
11554 anyway, and fall back to id to make the result reproducible across
11555 qsort implementations. */
11556 apos
= asec
->output_section
->vma
+ asec
->output_offset
;
11557 bpos
= bsec
->output_section
->vma
+ bsec
->output_offset
;
11563 return asec
->id
- bsec
->id
;
11567 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11568 order as their linked sections. Returns false if this could not be done
11569 because an output section includes both ordered and unordered
11570 sections. Ideally we'd do this in the linker proper. */
11573 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
11575 size_t seen_linkorder
;
11578 struct bfd_link_order
*p
;
11580 struct bfd_link_order
**sections
;
11581 asection
*other_sec
, *linkorder_sec
;
11582 bfd_vma offset
; /* Octets. */
11585 linkorder_sec
= NULL
;
11587 seen_linkorder
= 0;
11588 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11590 if (p
->type
== bfd_indirect_link_order
)
11592 asection
*s
= p
->u
.indirect
.section
;
11594 if ((s
->flags
& SEC_LINKER_CREATED
) == 0
11595 && bfd_get_flavour (sub
) == bfd_target_elf_flavour
11596 && elf_section_data (s
) != NULL
11597 && elf_linked_to_section (s
) != NULL
)
11611 if (seen_other
&& seen_linkorder
)
11613 if (other_sec
&& linkorder_sec
)
11615 /* xgettext:c-format */
11616 (_("%pA has both ordered [`%pA' in %pB] "
11617 "and unordered [`%pA' in %pB] sections"),
11618 o
, linkorder_sec
, linkorder_sec
->owner
,
11619 other_sec
, other_sec
->owner
);
11622 (_("%pA has both ordered and unordered sections"), o
);
11623 bfd_set_error (bfd_error_bad_value
);
11628 if (!seen_linkorder
)
11631 sections
= bfd_malloc (seen_linkorder
* sizeof (*sections
));
11632 if (sections
== NULL
)
11635 seen_linkorder
= 0;
11636 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11637 sections
[seen_linkorder
++] = p
;
11639 /* Sort the input sections in the order of their linked section. */
11640 qsort (sections
, seen_linkorder
, sizeof (*sections
), compare_link_order
);
11642 /* Change the offsets of the sections. */
11644 for (n
= 0; n
< seen_linkorder
; n
++)
11647 asection
*s
= sections
[n
]->u
.indirect
.section
;
11648 unsigned int opb
= bfd_octets_per_byte (abfd
, s
);
11650 mask
= ~(bfd_vma
) 0 << s
->alignment_power
* opb
;
11651 offset
= (offset
+ ~mask
) & mask
;
11652 sections
[n
]->offset
= s
->output_offset
= offset
/ opb
;
11653 offset
+= sections
[n
]->size
;
11660 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11661 Returns TRUE upon success, FALSE otherwise. */
11664 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
11666 bfd_boolean ret
= FALSE
;
11668 const struct elf_backend_data
*bed
;
11670 enum bfd_architecture arch
;
11672 asymbol
**sympp
= NULL
;
11676 elf_symbol_type
*osymbuf
;
11679 implib_bfd
= info
->out_implib_bfd
;
11680 bed
= get_elf_backend_data (abfd
);
11682 if (!bfd_set_format (implib_bfd
, bfd_object
))
11685 /* Use flag from executable but make it a relocatable object. */
11686 flags
= bfd_get_file_flags (abfd
);
11687 flags
&= ~HAS_RELOC
;
11688 if (!bfd_set_start_address (implib_bfd
, 0)
11689 || !bfd_set_file_flags (implib_bfd
, flags
& ~EXEC_P
))
11692 /* Copy architecture of output file to import library file. */
11693 arch
= bfd_get_arch (abfd
);
11694 mach
= bfd_get_mach (abfd
);
11695 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
11696 && (abfd
->target_defaulted
11697 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
11700 /* Get symbol table size. */
11701 symsize
= bfd_get_symtab_upper_bound (abfd
);
11705 /* Read in the symbol table. */
11706 sympp
= (asymbol
**) bfd_malloc (symsize
);
11710 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
11714 /* Allow the BFD backend to copy any private header data it
11715 understands from the output BFD to the import library BFD. */
11716 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
11719 /* Filter symbols to appear in the import library. */
11720 if (bed
->elf_backend_filter_implib_symbols
)
11721 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
11724 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
11727 bfd_set_error (bfd_error_no_symbols
);
11728 _bfd_error_handler (_("%pB: no symbol found for import library"),
11734 /* Make symbols absolute. */
11735 amt
= symcount
* sizeof (*osymbuf
);
11736 osymbuf
= (elf_symbol_type
*) bfd_alloc (implib_bfd
, amt
);
11737 if (osymbuf
== NULL
)
11740 for (src_count
= 0; src_count
< symcount
; src_count
++)
11742 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
11743 sizeof (*osymbuf
));
11744 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
11745 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
11746 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
11747 osymbuf
[src_count
].internal_elf_sym
.st_value
=
11748 osymbuf
[src_count
].symbol
.value
;
11749 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
11752 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
11754 /* Allow the BFD backend to copy any private data it understands
11755 from the output BFD to the import library BFD. This is done last
11756 to permit the routine to look at the filtered symbol table. */
11757 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
11760 if (!bfd_close (implib_bfd
))
11771 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
11775 if (flinfo
->symstrtab
!= NULL
)
11776 _bfd_elf_strtab_free (flinfo
->symstrtab
);
11777 if (flinfo
->contents
!= NULL
)
11778 free (flinfo
->contents
);
11779 if (flinfo
->external_relocs
!= NULL
)
11780 free (flinfo
->external_relocs
);
11781 if (flinfo
->internal_relocs
!= NULL
)
11782 free (flinfo
->internal_relocs
);
11783 if (flinfo
->external_syms
!= NULL
)
11784 free (flinfo
->external_syms
);
11785 if (flinfo
->locsym_shndx
!= NULL
)
11786 free (flinfo
->locsym_shndx
);
11787 if (flinfo
->internal_syms
!= NULL
)
11788 free (flinfo
->internal_syms
);
11789 if (flinfo
->indices
!= NULL
)
11790 free (flinfo
->indices
);
11791 if (flinfo
->sections
!= NULL
)
11792 free (flinfo
->sections
);
11793 if (flinfo
->symshndxbuf
!= NULL
11794 && flinfo
->symshndxbuf
!= (Elf_External_Sym_Shndx
*) -1)
11795 free (flinfo
->symshndxbuf
);
11796 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
11798 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11799 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rel
.hashes
!= NULL
)
11800 free (esdo
->rel
.hashes
);
11801 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rela
.hashes
!= NULL
)
11802 free (esdo
->rela
.hashes
);
11806 /* Do the final step of an ELF link. */
11809 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
11811 bfd_boolean dynamic
;
11812 bfd_boolean emit_relocs
;
11814 struct elf_final_link_info flinfo
;
11816 struct bfd_link_order
*p
;
11818 bfd_size_type max_contents_size
;
11819 bfd_size_type max_external_reloc_size
;
11820 bfd_size_type max_internal_reloc_count
;
11821 bfd_size_type max_sym_count
;
11822 bfd_size_type max_sym_shndx_count
;
11823 Elf_Internal_Sym elfsym
;
11825 Elf_Internal_Shdr
*symtab_hdr
;
11826 Elf_Internal_Shdr
*symtab_shndx_hdr
;
11827 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11828 struct elf_outext_info eoinfo
;
11829 bfd_boolean merged
;
11830 size_t relativecount
= 0;
11831 asection
*reldyn
= 0;
11833 asection
*attr_section
= NULL
;
11834 bfd_vma attr_size
= 0;
11835 const char *std_attrs_section
;
11836 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
11837 bfd_boolean sections_removed
;
11839 if (!is_elf_hash_table (htab
))
11842 if (bfd_link_pic (info
))
11843 abfd
->flags
|= DYNAMIC
;
11845 dynamic
= htab
->dynamic_sections_created
;
11846 dynobj
= htab
->dynobj
;
11848 emit_relocs
= (bfd_link_relocatable (info
)
11849 || info
->emitrelocations
);
11851 flinfo
.info
= info
;
11852 flinfo
.output_bfd
= abfd
;
11853 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
11854 if (flinfo
.symstrtab
== NULL
)
11859 flinfo
.hash_sec
= NULL
;
11860 flinfo
.symver_sec
= NULL
;
11864 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
11865 /* Note that dynsym_sec can be NULL (on VMS). */
11866 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
11867 /* Note that it is OK if symver_sec is NULL. */
11870 flinfo
.contents
= NULL
;
11871 flinfo
.external_relocs
= NULL
;
11872 flinfo
.internal_relocs
= NULL
;
11873 flinfo
.external_syms
= NULL
;
11874 flinfo
.locsym_shndx
= NULL
;
11875 flinfo
.internal_syms
= NULL
;
11876 flinfo
.indices
= NULL
;
11877 flinfo
.sections
= NULL
;
11878 flinfo
.symshndxbuf
= NULL
;
11879 flinfo
.filesym_count
= 0;
11881 /* The object attributes have been merged. Remove the input
11882 sections from the link, and set the contents of the output
11884 sections_removed
= FALSE
;
11885 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
11886 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11888 bfd_boolean remove_section
= FALSE
;
11890 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
11891 || strcmp (o
->name
, ".gnu.attributes") == 0)
11893 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11895 asection
*input_section
;
11897 if (p
->type
!= bfd_indirect_link_order
)
11899 input_section
= p
->u
.indirect
.section
;
11900 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11901 elf_link_input_bfd ignores this section. */
11902 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
11905 attr_size
= bfd_elf_obj_attr_size (abfd
);
11906 bfd_set_section_size (o
, attr_size
);
11907 /* Skip this section later on. */
11908 o
->map_head
.link_order
= NULL
;
11912 remove_section
= TRUE
;
11914 else if ((o
->flags
& SEC_GROUP
) != 0 && o
->size
== 0)
11916 /* Remove empty group section from linker output. */
11917 remove_section
= TRUE
;
11919 if (remove_section
)
11921 o
->flags
|= SEC_EXCLUDE
;
11922 bfd_section_list_remove (abfd
, o
);
11923 abfd
->section_count
--;
11924 sections_removed
= TRUE
;
11927 if (sections_removed
)
11928 _bfd_fix_excluded_sec_syms (abfd
, info
);
11930 /* Count up the number of relocations we will output for each output
11931 section, so that we know the sizes of the reloc sections. We
11932 also figure out some maximum sizes. */
11933 max_contents_size
= 0;
11934 max_external_reloc_size
= 0;
11935 max_internal_reloc_count
= 0;
11937 max_sym_shndx_count
= 0;
11939 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11941 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11942 o
->reloc_count
= 0;
11944 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11946 unsigned int reloc_count
= 0;
11947 unsigned int additional_reloc_count
= 0;
11948 struct bfd_elf_section_data
*esdi
= NULL
;
11950 if (p
->type
== bfd_section_reloc_link_order
11951 || p
->type
== bfd_symbol_reloc_link_order
)
11953 else if (p
->type
== bfd_indirect_link_order
)
11957 sec
= p
->u
.indirect
.section
;
11959 /* Mark all sections which are to be included in the
11960 link. This will normally be every section. We need
11961 to do this so that we can identify any sections which
11962 the linker has decided to not include. */
11963 sec
->linker_mark
= TRUE
;
11965 if (sec
->flags
& SEC_MERGE
)
11968 if (sec
->rawsize
> max_contents_size
)
11969 max_contents_size
= sec
->rawsize
;
11970 if (sec
->size
> max_contents_size
)
11971 max_contents_size
= sec
->size
;
11973 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
11974 && (sec
->owner
->flags
& DYNAMIC
) == 0)
11978 /* We are interested in just local symbols, not all
11980 if (elf_bad_symtab (sec
->owner
))
11981 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
11982 / bed
->s
->sizeof_sym
);
11984 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
11986 if (sym_count
> max_sym_count
)
11987 max_sym_count
= sym_count
;
11989 if (sym_count
> max_sym_shndx_count
11990 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
11991 max_sym_shndx_count
= sym_count
;
11993 if (esdo
->this_hdr
.sh_type
== SHT_REL
11994 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
11995 /* Some backends use reloc_count in relocation sections
11996 to count particular types of relocs. Of course,
11997 reloc sections themselves can't have relocations. */
11999 else if (emit_relocs
)
12001 reloc_count
= sec
->reloc_count
;
12002 if (bed
->elf_backend_count_additional_relocs
)
12005 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
12006 additional_reloc_count
+= c
;
12009 else if (bed
->elf_backend_count_relocs
)
12010 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
12012 esdi
= elf_section_data (sec
);
12014 if ((sec
->flags
& SEC_RELOC
) != 0)
12016 size_t ext_size
= 0;
12018 if (esdi
->rel
.hdr
!= NULL
)
12019 ext_size
= esdi
->rel
.hdr
->sh_size
;
12020 if (esdi
->rela
.hdr
!= NULL
)
12021 ext_size
+= esdi
->rela
.hdr
->sh_size
;
12023 if (ext_size
> max_external_reloc_size
)
12024 max_external_reloc_size
= ext_size
;
12025 if (sec
->reloc_count
> max_internal_reloc_count
)
12026 max_internal_reloc_count
= sec
->reloc_count
;
12031 if (reloc_count
== 0)
12034 reloc_count
+= additional_reloc_count
;
12035 o
->reloc_count
+= reloc_count
;
12037 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
12041 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
12042 esdo
->rel
.count
+= additional_reloc_count
;
12044 if (esdi
->rela
.hdr
)
12046 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
12047 esdo
->rela
.count
+= additional_reloc_count
;
12053 esdo
->rela
.count
+= reloc_count
;
12055 esdo
->rel
.count
+= reloc_count
;
12059 if (o
->reloc_count
> 0)
12060 o
->flags
|= SEC_RELOC
;
12063 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12064 set it (this is probably a bug) and if it is set
12065 assign_section_numbers will create a reloc section. */
12066 o
->flags
&=~ SEC_RELOC
;
12069 /* If the SEC_ALLOC flag is not set, force the section VMA to
12070 zero. This is done in elf_fake_sections as well, but forcing
12071 the VMA to 0 here will ensure that relocs against these
12072 sections are handled correctly. */
12073 if ((o
->flags
& SEC_ALLOC
) == 0
12074 && ! o
->user_set_vma
)
12078 if (! bfd_link_relocatable (info
) && merged
)
12079 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
12081 /* Figure out the file positions for everything but the symbol table
12082 and the relocs. We set symcount to force assign_section_numbers
12083 to create a symbol table. */
12084 abfd
->symcount
= info
->strip
!= strip_all
|| emit_relocs
;
12085 BFD_ASSERT (! abfd
->output_has_begun
);
12086 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
12089 /* Set sizes, and assign file positions for reloc sections. */
12090 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12092 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12093 if ((o
->flags
& SEC_RELOC
) != 0)
12096 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
12100 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
12104 /* _bfd_elf_compute_section_file_positions makes temporary use
12105 of target_index. Reset it. */
12106 o
->target_index
= 0;
12108 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12109 to count upwards while actually outputting the relocations. */
12110 esdo
->rel
.count
= 0;
12111 esdo
->rela
.count
= 0;
12113 if ((esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
12114 && !bfd_section_is_ctf (o
))
12116 /* Cache the section contents so that they can be compressed
12117 later. Use bfd_malloc since it will be freed by
12118 bfd_compress_section_contents. */
12119 unsigned char *contents
= esdo
->this_hdr
.contents
;
12120 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
12123 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
12124 if (contents
== NULL
)
12126 esdo
->this_hdr
.contents
= contents
;
12130 /* We have now assigned file positions for all the sections except .symtab,
12131 .strtab, and non-loaded reloc and compressed debugging sections. We start
12132 the .symtab section at the current file position, and write directly to it.
12133 We build the .strtab section in memory. */
12134 abfd
->symcount
= 0;
12135 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12136 /* sh_name is set in prep_headers. */
12137 symtab_hdr
->sh_type
= SHT_SYMTAB
;
12138 /* sh_flags, sh_addr and sh_size all start off zero. */
12139 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
12140 /* sh_link is set in assign_section_numbers. */
12141 /* sh_info is set below. */
12142 /* sh_offset is set just below. */
12143 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
12145 if (max_sym_count
< 20)
12146 max_sym_count
= 20;
12147 htab
->strtabsize
= max_sym_count
;
12148 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
12149 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
12150 if (htab
->strtab
== NULL
)
12152 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12154 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
12155 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
12157 if (info
->strip
!= strip_all
|| emit_relocs
)
12159 file_ptr off
= elf_next_file_pos (abfd
);
12161 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
12163 /* Note that at this point elf_next_file_pos (abfd) is
12164 incorrect. We do not yet know the size of the .symtab section.
12165 We correct next_file_pos below, after we do know the size. */
12167 /* Start writing out the symbol table. The first symbol is always a
12169 elfsym
.st_value
= 0;
12170 elfsym
.st_size
= 0;
12171 elfsym
.st_info
= 0;
12172 elfsym
.st_other
= 0;
12173 elfsym
.st_shndx
= SHN_UNDEF
;
12174 elfsym
.st_target_internal
= 0;
12175 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
12176 bfd_und_section_ptr
, NULL
) != 1)
12179 /* Output a symbol for each section. We output these even if we are
12180 discarding local symbols, since they are used for relocs. These
12181 symbols have no names. We store the index of each one in the
12182 index field of the section, so that we can find it again when
12183 outputting relocs. */
12185 elfsym
.st_size
= 0;
12186 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12187 elfsym
.st_other
= 0;
12188 elfsym
.st_value
= 0;
12189 elfsym
.st_target_internal
= 0;
12190 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12192 o
= bfd_section_from_elf_index (abfd
, i
);
12195 o
->target_index
= bfd_get_symcount (abfd
);
12196 elfsym
.st_shndx
= i
;
12197 if (!bfd_link_relocatable (info
))
12198 elfsym
.st_value
= o
->vma
;
12199 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
, o
,
12206 /* Allocate some memory to hold information read in from the input
12208 if (max_contents_size
!= 0)
12210 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
12211 if (flinfo
.contents
== NULL
)
12215 if (max_external_reloc_size
!= 0)
12217 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
12218 if (flinfo
.external_relocs
== NULL
)
12222 if (max_internal_reloc_count
!= 0)
12224 amt
= max_internal_reloc_count
* sizeof (Elf_Internal_Rela
);
12225 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
12226 if (flinfo
.internal_relocs
== NULL
)
12230 if (max_sym_count
!= 0)
12232 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
12233 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
12234 if (flinfo
.external_syms
== NULL
)
12237 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
12238 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
12239 if (flinfo
.internal_syms
== NULL
)
12242 amt
= max_sym_count
* sizeof (long);
12243 flinfo
.indices
= (long int *) bfd_malloc (amt
);
12244 if (flinfo
.indices
== NULL
)
12247 amt
= max_sym_count
* sizeof (asection
*);
12248 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
12249 if (flinfo
.sections
== NULL
)
12253 if (max_sym_shndx_count
!= 0)
12255 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
12256 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
12257 if (flinfo
.locsym_shndx
== NULL
)
12263 bfd_vma base
, end
= 0; /* Both bytes. */
12266 for (sec
= htab
->tls_sec
;
12267 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
12270 bfd_size_type size
= sec
->size
;
12271 unsigned int opb
= bfd_octets_per_byte (abfd
, sec
);
12274 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
12276 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
12279 size
= ord
->offset
* opb
+ ord
->size
;
12281 end
= sec
->vma
+ size
/ opb
;
12283 base
= htab
->tls_sec
->vma
;
12284 /* Only align end of TLS section if static TLS doesn't have special
12285 alignment requirements. */
12286 if (bed
->static_tls_alignment
== 1)
12287 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
12288 htab
->tls_size
= end
- base
;
12291 /* Reorder SHF_LINK_ORDER sections. */
12292 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12294 if (!elf_fixup_link_order (abfd
, o
))
12298 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
12301 /* Since ELF permits relocations to be against local symbols, we
12302 must have the local symbols available when we do the relocations.
12303 Since we would rather only read the local symbols once, and we
12304 would rather not keep them in memory, we handle all the
12305 relocations for a single input file at the same time.
12307 Unfortunately, there is no way to know the total number of local
12308 symbols until we have seen all of them, and the local symbol
12309 indices precede the global symbol indices. This means that when
12310 we are generating relocatable output, and we see a reloc against
12311 a global symbol, we can not know the symbol index until we have
12312 finished examining all the local symbols to see which ones we are
12313 going to output. To deal with this, we keep the relocations in
12314 memory, and don't output them until the end of the link. This is
12315 an unfortunate waste of memory, but I don't see a good way around
12316 it. Fortunately, it only happens when performing a relocatable
12317 link, which is not the common case. FIXME: If keep_memory is set
12318 we could write the relocs out and then read them again; I don't
12319 know how bad the memory loss will be. */
12321 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12322 sub
->output_has_begun
= FALSE
;
12323 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12325 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12327 if (p
->type
== bfd_indirect_link_order
12328 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
12329 == bfd_target_elf_flavour
)
12330 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
12332 if (! sub
->output_has_begun
)
12334 if (! elf_link_input_bfd (&flinfo
, sub
))
12336 sub
->output_has_begun
= TRUE
;
12339 else if (p
->type
== bfd_section_reloc_link_order
12340 || p
->type
== bfd_symbol_reloc_link_order
)
12342 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
12347 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
12349 if (p
->type
== bfd_indirect_link_order
12350 && (bfd_get_flavour (sub
)
12351 == bfd_target_elf_flavour
)
12352 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
12353 != bed
->s
->elfclass
))
12355 const char *iclass
, *oclass
;
12357 switch (bed
->s
->elfclass
)
12359 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
12360 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
12361 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
12365 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
12367 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
12368 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
12369 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
12373 bfd_set_error (bfd_error_wrong_format
);
12375 /* xgettext:c-format */
12376 (_("%pB: file class %s incompatible with %s"),
12377 sub
, iclass
, oclass
);
12386 /* Free symbol buffer if needed. */
12387 if (!info
->reduce_memory_overheads
)
12389 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12390 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
12391 && elf_tdata (sub
)->symbuf
)
12393 free (elf_tdata (sub
)->symbuf
);
12394 elf_tdata (sub
)->symbuf
= NULL
;
12398 /* Output any global symbols that got converted to local in a
12399 version script or due to symbol visibility. We do this in a
12400 separate step since ELF requires all local symbols to appear
12401 prior to any global symbols. FIXME: We should only do this if
12402 some global symbols were, in fact, converted to become local.
12403 FIXME: Will this work correctly with the Irix 5 linker? */
12404 eoinfo
.failed
= FALSE
;
12405 eoinfo
.flinfo
= &flinfo
;
12406 eoinfo
.localsyms
= TRUE
;
12407 eoinfo
.file_sym_done
= FALSE
;
12408 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12412 /* If backend needs to output some local symbols not present in the hash
12413 table, do it now. */
12414 if (bed
->elf_backend_output_arch_local_syms
12415 && (info
->strip
!= strip_all
|| emit_relocs
))
12417 typedef int (*out_sym_func
)
12418 (void *, const char *, Elf_Internal_Sym
*, asection
*,
12419 struct elf_link_hash_entry
*);
12421 if (! ((*bed
->elf_backend_output_arch_local_syms
)
12422 (abfd
, info
, &flinfo
,
12423 (out_sym_func
) elf_link_output_symstrtab
)))
12427 /* That wrote out all the local symbols. Finish up the symbol table
12428 with the global symbols. Even if we want to strip everything we
12429 can, we still need to deal with those global symbols that got
12430 converted to local in a version script. */
12432 /* The sh_info field records the index of the first non local symbol. */
12433 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12436 && htab
->dynsym
!= NULL
12437 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
12439 Elf_Internal_Sym sym
;
12440 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
12442 o
= htab
->dynsym
->output_section
;
12443 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
12445 /* Write out the section symbols for the output sections. */
12446 if (bfd_link_pic (info
)
12447 || htab
->is_relocatable_executable
)
12453 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12455 sym
.st_target_internal
= 0;
12457 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
12463 dynindx
= elf_section_data (s
)->dynindx
;
12466 indx
= elf_section_data (s
)->this_idx
;
12467 BFD_ASSERT (indx
> 0);
12468 sym
.st_shndx
= indx
;
12469 if (! check_dynsym (abfd
, &sym
))
12471 sym
.st_value
= s
->vma
;
12472 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
12473 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12477 /* Write out the local dynsyms. */
12478 if (htab
->dynlocal
)
12480 struct elf_link_local_dynamic_entry
*e
;
12481 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
12486 /* Copy the internal symbol and turn off visibility.
12487 Note that we saved a word of storage and overwrote
12488 the original st_name with the dynstr_index. */
12490 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
12492 s
= bfd_section_from_elf_index (e
->input_bfd
,
12497 elf_section_data (s
->output_section
)->this_idx
;
12498 if (! check_dynsym (abfd
, &sym
))
12500 sym
.st_value
= (s
->output_section
->vma
12502 + e
->isym
.st_value
);
12505 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
12506 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12511 /* We get the global symbols from the hash table. */
12512 eoinfo
.failed
= FALSE
;
12513 eoinfo
.localsyms
= FALSE
;
12514 eoinfo
.flinfo
= &flinfo
;
12515 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12519 /* If backend needs to output some symbols not present in the hash
12520 table, do it now. */
12521 if (bed
->elf_backend_output_arch_syms
12522 && (info
->strip
!= strip_all
|| emit_relocs
))
12524 typedef int (*out_sym_func
)
12525 (void *, const char *, Elf_Internal_Sym
*, asection
*,
12526 struct elf_link_hash_entry
*);
12528 if (! ((*bed
->elf_backend_output_arch_syms
)
12529 (abfd
, info
, &flinfo
,
12530 (out_sym_func
) elf_link_output_symstrtab
)))
12534 /* Finalize the .strtab section. */
12535 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
12537 /* Swap out the .strtab section. */
12538 if (!elf_link_swap_symbols_out (&flinfo
))
12541 /* Now we know the size of the symtab section. */
12542 if (bfd_get_symcount (abfd
) > 0)
12544 /* Finish up and write out the symbol string table (.strtab)
12546 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
12547 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
12549 if (elf_symtab_shndx_list (abfd
))
12551 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
12553 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
12555 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
12556 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
12557 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
12558 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
12559 symtab_shndx_hdr
->sh_size
= amt
;
12561 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
12564 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
12565 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
12570 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
12571 /* sh_name was set in prep_headers. */
12572 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
12573 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
12574 symstrtab_hdr
->sh_addr
= 0;
12575 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
12576 symstrtab_hdr
->sh_entsize
= 0;
12577 symstrtab_hdr
->sh_link
= 0;
12578 symstrtab_hdr
->sh_info
= 0;
12579 /* sh_offset is set just below. */
12580 symstrtab_hdr
->sh_addralign
= 1;
12582 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
12584 elf_next_file_pos (abfd
) = off
;
12586 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
12587 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
12591 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
12593 _bfd_error_handler (_("%pB: failed to generate import library"),
12594 info
->out_implib_bfd
);
12598 /* Adjust the relocs to have the correct symbol indices. */
12599 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12601 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12604 if ((o
->flags
& SEC_RELOC
) == 0)
12607 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
12608 if (esdo
->rel
.hdr
!= NULL
12609 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
, info
))
12611 if (esdo
->rela
.hdr
!= NULL
12612 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
, info
))
12615 /* Set the reloc_count field to 0 to prevent write_relocs from
12616 trying to swap the relocs out itself. */
12617 o
->reloc_count
= 0;
12620 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
12621 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
12623 /* If we are linking against a dynamic object, or generating a
12624 shared library, finish up the dynamic linking information. */
12627 bfd_byte
*dyncon
, *dynconend
;
12629 /* Fix up .dynamic entries. */
12630 o
= bfd_get_linker_section (dynobj
, ".dynamic");
12631 BFD_ASSERT (o
!= NULL
);
12633 dyncon
= o
->contents
;
12634 dynconend
= o
->contents
+ o
->size
;
12635 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12637 Elf_Internal_Dyn dyn
;
12640 bfd_size_type sh_size
;
12643 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12650 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
12652 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
12654 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
12655 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
12658 dyn
.d_un
.d_val
= relativecount
;
12665 name
= info
->init_function
;
12668 name
= info
->fini_function
;
12671 struct elf_link_hash_entry
*h
;
12673 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
12675 && (h
->root
.type
== bfd_link_hash_defined
12676 || h
->root
.type
== bfd_link_hash_defweak
))
12678 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
12679 o
= h
->root
.u
.def
.section
;
12680 if (o
->output_section
!= NULL
)
12681 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
12682 + o
->output_offset
);
12685 /* The symbol is imported from another shared
12686 library and does not apply to this one. */
12687 dyn
.d_un
.d_ptr
= 0;
12694 case DT_PREINIT_ARRAYSZ
:
12695 name
= ".preinit_array";
12697 case DT_INIT_ARRAYSZ
:
12698 name
= ".init_array";
12700 case DT_FINI_ARRAYSZ
:
12701 name
= ".fini_array";
12703 o
= bfd_get_section_by_name (abfd
, name
);
12707 (_("could not find section %s"), name
);
12712 (_("warning: %s section has zero size"), name
);
12713 dyn
.d_un
.d_val
= o
->size
;
12716 case DT_PREINIT_ARRAY
:
12717 name
= ".preinit_array";
12719 case DT_INIT_ARRAY
:
12720 name
= ".init_array";
12722 case DT_FINI_ARRAY
:
12723 name
= ".fini_array";
12725 o
= bfd_get_section_by_name (abfd
, name
);
12732 name
= ".gnu.hash";
12741 name
= ".gnu.version_d";
12744 name
= ".gnu.version_r";
12747 name
= ".gnu.version";
12749 o
= bfd_get_linker_section (dynobj
, name
);
12751 if (o
== NULL
|| bfd_is_abs_section (o
->output_section
))
12754 (_("could not find section %s"), name
);
12757 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
12760 (_("warning: section '%s' is being made into a note"), name
);
12761 bfd_set_error (bfd_error_nonrepresentable_section
);
12764 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
12771 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
12777 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12779 Elf_Internal_Shdr
*hdr
;
12781 hdr
= elf_elfsections (abfd
)[i
];
12782 if (hdr
->sh_type
== type
12783 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
12785 sh_size
+= hdr
->sh_size
;
12787 || sh_addr
> hdr
->sh_addr
)
12788 sh_addr
= hdr
->sh_addr
;
12792 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
12794 unsigned int opb
= bfd_octets_per_byte (abfd
, o
);
12796 /* Don't count procedure linkage table relocs in the
12797 overall reloc count. */
12798 sh_size
-= htab
->srelplt
->size
;
12800 /* If the size is zero, make the address zero too.
12801 This is to avoid a glibc bug. If the backend
12802 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12803 zero, then we'll put DT_RELA at the end of
12804 DT_JMPREL. glibc will interpret the end of
12805 DT_RELA matching the end of DT_JMPREL as the
12806 case where DT_RELA includes DT_JMPREL, and for
12807 LD_BIND_NOW will decide that processing DT_RELA
12808 will process the PLT relocs too. Net result:
12809 No PLT relocs applied. */
12812 /* If .rela.plt is the first .rela section, exclude
12813 it from DT_RELA. */
12814 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
12815 + htab
->srelplt
->output_offset
) * opb
)
12816 sh_addr
+= htab
->srelplt
->size
;
12819 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
12820 dyn
.d_un
.d_val
= sh_size
;
12822 dyn
.d_un
.d_ptr
= sh_addr
;
12825 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
12829 /* If we have created any dynamic sections, then output them. */
12830 if (dynobj
!= NULL
)
12832 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
12835 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12836 if (((info
->warn_shared_textrel
&& bfd_link_pic (info
))
12837 || info
->error_textrel
)
12838 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
)
12840 bfd_byte
*dyncon
, *dynconend
;
12842 dyncon
= o
->contents
;
12843 dynconend
= o
->contents
+ o
->size
;
12844 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12846 Elf_Internal_Dyn dyn
;
12848 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12850 if (dyn
.d_tag
== DT_TEXTREL
)
12852 if (info
->error_textrel
)
12853 info
->callbacks
->einfo
12854 (_("%P%X: read-only segment has dynamic relocations\n"));
12856 info
->callbacks
->einfo
12857 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
12863 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
12865 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
12867 || o
->output_section
== bfd_abs_section_ptr
)
12869 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
12871 /* At this point, we are only interested in sections
12872 created by _bfd_elf_link_create_dynamic_sections. */
12875 if (htab
->stab_info
.stabstr
== o
)
12877 if (htab
->eh_info
.hdr_sec
== o
)
12879 if (strcmp (o
->name
, ".dynstr") != 0)
12881 bfd_size_type octets
= ((file_ptr
) o
->output_offset
12882 * bfd_octets_per_byte (abfd
, o
));
12883 if (!bfd_set_section_contents (abfd
, o
->output_section
,
12884 o
->contents
, octets
, o
->size
))
12889 /* The contents of the .dynstr section are actually in a
12893 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
12894 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
12895 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
12901 if (!info
->resolve_section_groups
)
12903 bfd_boolean failed
= FALSE
;
12905 BFD_ASSERT (bfd_link_relocatable (info
));
12906 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
12911 /* If we have optimized stabs strings, output them. */
12912 if (htab
->stab_info
.stabstr
!= NULL
)
12914 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
12918 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
12921 if (info
->callbacks
->emit_ctf
)
12922 info
->callbacks
->emit_ctf ();
12924 elf_final_link_free (abfd
, &flinfo
);
12928 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
12929 if (contents
== NULL
)
12930 return FALSE
; /* Bail out and fail. */
12931 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
12932 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
12939 elf_final_link_free (abfd
, &flinfo
);
12943 /* Initialize COOKIE for input bfd ABFD. */
12946 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
12947 struct bfd_link_info
*info
, bfd
*abfd
)
12949 Elf_Internal_Shdr
*symtab_hdr
;
12950 const struct elf_backend_data
*bed
;
12952 bed
= get_elf_backend_data (abfd
);
12953 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12955 cookie
->abfd
= abfd
;
12956 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
12957 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
12958 if (cookie
->bad_symtab
)
12960 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
12961 cookie
->extsymoff
= 0;
12965 cookie
->locsymcount
= symtab_hdr
->sh_info
;
12966 cookie
->extsymoff
= symtab_hdr
->sh_info
;
12969 if (bed
->s
->arch_size
== 32)
12970 cookie
->r_sym_shift
= 8;
12972 cookie
->r_sym_shift
= 32;
12974 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
12975 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
12977 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
12978 cookie
->locsymcount
, 0,
12980 if (cookie
->locsyms
== NULL
)
12982 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
12985 if (info
->keep_memory
)
12986 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
12991 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12994 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
12996 Elf_Internal_Shdr
*symtab_hdr
;
12998 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12999 if (cookie
->locsyms
!= NULL
13000 && symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
13001 free (cookie
->locsyms
);
13004 /* Initialize the relocation information in COOKIE for input section SEC
13005 of input bfd ABFD. */
13008 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13009 struct bfd_link_info
*info
, bfd
*abfd
,
13012 if (sec
->reloc_count
== 0)
13014 cookie
->rels
= NULL
;
13015 cookie
->relend
= NULL
;
13019 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
13020 info
->keep_memory
);
13021 if (cookie
->rels
== NULL
)
13023 cookie
->rel
= cookie
->rels
;
13024 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
13026 cookie
->rel
= cookie
->rels
;
13030 /* Free the memory allocated by init_reloc_cookie_rels,
13034 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13037 if (cookie
->rels
&& elf_section_data (sec
)->relocs
!= cookie
->rels
)
13038 free (cookie
->rels
);
13041 /* Initialize the whole of COOKIE for input section SEC. */
13044 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13045 struct bfd_link_info
*info
,
13048 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
13050 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
13055 fini_reloc_cookie (cookie
, sec
->owner
);
13060 /* Free the memory allocated by init_reloc_cookie_for_section,
13064 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13067 fini_reloc_cookie_rels (cookie
, sec
);
13068 fini_reloc_cookie (cookie
, sec
->owner
);
13071 /* Garbage collect unused sections. */
13073 /* Default gc_mark_hook. */
13076 _bfd_elf_gc_mark_hook (asection
*sec
,
13077 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13078 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13079 struct elf_link_hash_entry
*h
,
13080 Elf_Internal_Sym
*sym
)
13084 switch (h
->root
.type
)
13086 case bfd_link_hash_defined
:
13087 case bfd_link_hash_defweak
:
13088 return h
->root
.u
.def
.section
;
13090 case bfd_link_hash_common
:
13091 return h
->root
.u
.c
.p
->section
;
13098 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
13103 /* Return the debug definition section. */
13106 elf_gc_mark_debug_section (asection
*sec ATTRIBUTE_UNUSED
,
13107 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13108 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13109 struct elf_link_hash_entry
*h
,
13110 Elf_Internal_Sym
*sym
)
13114 /* Return the global debug definition section. */
13115 if ((h
->root
.type
== bfd_link_hash_defined
13116 || h
->root
.type
== bfd_link_hash_defweak
)
13117 && (h
->root
.u
.def
.section
->flags
& SEC_DEBUGGING
) != 0)
13118 return h
->root
.u
.def
.section
;
13122 /* Return the local debug definition section. */
13123 asection
*isec
= bfd_section_from_elf_index (sec
->owner
,
13125 if ((isec
->flags
& SEC_DEBUGGING
) != 0)
13132 /* COOKIE->rel describes a relocation against section SEC, which is
13133 a section we've decided to keep. Return the section that contains
13134 the relocation symbol, or NULL if no section contains it. */
13137 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
13138 elf_gc_mark_hook_fn gc_mark_hook
,
13139 struct elf_reloc_cookie
*cookie
,
13140 bfd_boolean
*start_stop
)
13142 unsigned long r_symndx
;
13143 struct elf_link_hash_entry
*h
, *hw
;
13145 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
13146 if (r_symndx
== STN_UNDEF
)
13149 if (r_symndx
>= cookie
->locsymcount
13150 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13152 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
13155 info
->callbacks
->einfo (_("%F%P: corrupt input: %pB\n"),
13159 while (h
->root
.type
== bfd_link_hash_indirect
13160 || h
->root
.type
== bfd_link_hash_warning
)
13161 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13163 /* Keep all aliases of the symbol too. If an object symbol
13164 needs to be copied into .dynbss then all of its aliases
13165 should be present as dynamic symbols, not just the one used
13166 on the copy relocation. */
13168 while (hw
->is_weakalias
)
13174 if (start_stop
!= NULL
)
13176 /* To work around a glibc bug, mark XXX input sections
13177 when there is a reference to __start_XXX or __stop_XXX
13181 asection
*s
= h
->u2
.start_stop_section
;
13182 *start_stop
= !s
->gc_mark
;
13187 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
13190 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
13191 &cookie
->locsyms
[r_symndx
]);
13194 /* COOKIE->rel describes a relocation against section SEC, which is
13195 a section we've decided to keep. Mark the section that contains
13196 the relocation symbol. */
13199 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
13201 elf_gc_mark_hook_fn gc_mark_hook
,
13202 struct elf_reloc_cookie
*cookie
)
13205 bfd_boolean start_stop
= FALSE
;
13207 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
13208 while (rsec
!= NULL
)
13210 if (!rsec
->gc_mark
)
13212 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
13213 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
13215 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
13220 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
13225 /* The mark phase of garbage collection. For a given section, mark
13226 it and any sections in this section's group, and all the sections
13227 which define symbols to which it refers. */
13230 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
13232 elf_gc_mark_hook_fn gc_mark_hook
)
13235 asection
*group_sec
, *eh_frame
;
13239 /* Mark all the sections in the group. */
13240 group_sec
= elf_section_data (sec
)->next_in_group
;
13241 if (group_sec
&& !group_sec
->gc_mark
)
13242 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
13245 /* Look through the section relocs. */
13247 eh_frame
= elf_eh_frame_section (sec
->owner
);
13248 if ((sec
->flags
& SEC_RELOC
) != 0
13249 && sec
->reloc_count
> 0
13250 && sec
!= eh_frame
)
13252 struct elf_reloc_cookie cookie
;
13254 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
13258 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
13259 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
13264 fini_reloc_cookie_for_section (&cookie
, sec
);
13268 if (ret
&& eh_frame
&& elf_fde_list (sec
))
13270 struct elf_reloc_cookie cookie
;
13272 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
13276 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
13277 gc_mark_hook
, &cookie
))
13279 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
13283 eh_frame
= elf_section_eh_frame_entry (sec
);
13284 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
13285 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
13291 /* Scan and mark sections in a special or debug section group. */
13294 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
13296 /* Point to first section of section group. */
13298 /* Used to iterate the section group. */
13301 bfd_boolean is_special_grp
= TRUE
;
13302 bfd_boolean is_debug_grp
= TRUE
;
13304 /* First scan to see if group contains any section other than debug
13305 and special section. */
13306 ssec
= msec
= elf_next_in_group (grp
);
13309 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
13310 is_debug_grp
= FALSE
;
13312 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
13313 is_special_grp
= FALSE
;
13315 msec
= elf_next_in_group (msec
);
13317 while (msec
!= ssec
);
13319 /* If this is a pure debug section group or pure special section group,
13320 keep all sections in this group. */
13321 if (is_debug_grp
|| is_special_grp
)
13326 msec
= elf_next_in_group (msec
);
13328 while (msec
!= ssec
);
13332 /* Keep debug and special sections. */
13335 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
13336 elf_gc_mark_hook_fn mark_hook
)
13340 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13343 bfd_boolean some_kept
;
13344 bfd_boolean debug_frag_seen
;
13345 bfd_boolean has_kept_debug_info
;
13347 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13349 isec
= ibfd
->sections
;
13350 if (isec
== NULL
|| isec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13353 /* Ensure all linker created sections are kept,
13354 see if any other section is already marked,
13355 and note if we have any fragmented debug sections. */
13356 debug_frag_seen
= some_kept
= has_kept_debug_info
= FALSE
;
13357 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13359 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
13361 else if (isec
->gc_mark
13362 && (isec
->flags
& SEC_ALLOC
) != 0
13363 && elf_section_type (isec
) != SHT_NOTE
)
13367 /* Since all sections, except for backend specific ones,
13368 have been garbage collected, call mark_hook on this
13369 section if any of its linked-to sections is marked. */
13370 asection
*linked_to_sec
= elf_linked_to_section (isec
);
13371 for (; linked_to_sec
!= NULL
;
13372 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
13373 if (linked_to_sec
->gc_mark
)
13375 if (!_bfd_elf_gc_mark (info
, isec
, mark_hook
))
13381 if (!debug_frag_seen
13382 && (isec
->flags
& SEC_DEBUGGING
)
13383 && CONST_STRNEQ (isec
->name
, ".debug_line."))
13384 debug_frag_seen
= TRUE
;
13385 else if (strcmp (bfd_section_name (isec
),
13386 "__patchable_function_entries") == 0
13387 && elf_linked_to_section (isec
) == NULL
)
13388 info
->callbacks
->einfo (_("%F%P: %pB(%pA): error: "
13389 "need linked-to section "
13390 "for --gc-sections\n"),
13391 isec
->owner
, isec
);
13394 /* If no non-note alloc section in this file will be kept, then
13395 we can toss out the debug and special sections. */
13399 /* Keep debug and special sections like .comment when they are
13400 not part of a group. Also keep section groups that contain
13401 just debug sections or special sections. NB: Sections with
13402 linked-to section has been handled above. */
13403 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13405 if ((isec
->flags
& SEC_GROUP
) != 0)
13406 _bfd_elf_gc_mark_debug_special_section_group (isec
);
13407 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
13408 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
13409 && elf_next_in_group (isec
) == NULL
13410 && elf_linked_to_section (isec
) == NULL
)
13412 if (isec
->gc_mark
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
13413 has_kept_debug_info
= TRUE
;
13416 /* Look for CODE sections which are going to be discarded,
13417 and find and discard any fragmented debug sections which
13418 are associated with that code section. */
13419 if (debug_frag_seen
)
13420 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13421 if ((isec
->flags
& SEC_CODE
) != 0
13422 && isec
->gc_mark
== 0)
13427 ilen
= strlen (isec
->name
);
13429 /* Association is determined by the name of the debug
13430 section containing the name of the code section as
13431 a suffix. For example .debug_line.text.foo is a
13432 debug section associated with .text.foo. */
13433 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
13437 if (dsec
->gc_mark
== 0
13438 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
13441 dlen
= strlen (dsec
->name
);
13444 && strncmp (dsec
->name
+ (dlen
- ilen
),
13445 isec
->name
, ilen
) == 0)
13450 /* Mark debug sections referenced by kept debug sections. */
13451 if (has_kept_debug_info
)
13452 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13454 && (isec
->flags
& SEC_DEBUGGING
) != 0)
13455 if (!_bfd_elf_gc_mark (info
, isec
,
13456 elf_gc_mark_debug_section
))
13463 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
13466 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13468 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13472 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13473 || elf_object_id (sub
) != elf_hash_table_id (elf_hash_table (info
))
13474 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13477 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13480 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13482 /* When any section in a section group is kept, we keep all
13483 sections in the section group. If the first member of
13484 the section group is excluded, we will also exclude the
13486 if (o
->flags
& SEC_GROUP
)
13488 asection
*first
= elf_next_in_group (o
);
13489 o
->gc_mark
= first
->gc_mark
;
13495 /* Skip sweeping sections already excluded. */
13496 if (o
->flags
& SEC_EXCLUDE
)
13499 /* Since this is early in the link process, it is simple
13500 to remove a section from the output. */
13501 o
->flags
|= SEC_EXCLUDE
;
13503 if (info
->print_gc_sections
&& o
->size
!= 0)
13504 /* xgettext:c-format */
13505 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13513 /* Propagate collected vtable information. This is called through
13514 elf_link_hash_traverse. */
13517 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
13519 /* Those that are not vtables. */
13521 || h
->u2
.vtable
== NULL
13522 || h
->u2
.vtable
->parent
== NULL
)
13525 /* Those vtables that do not have parents, we cannot merge. */
13526 if (h
->u2
.vtable
->parent
== (struct elf_link_hash_entry
*) -1)
13529 /* If we've already been done, exit. */
13530 if (h
->u2
.vtable
->used
&& h
->u2
.vtable
->used
[-1])
13533 /* Make sure the parent's table is up to date. */
13534 elf_gc_propagate_vtable_entries_used (h
->u2
.vtable
->parent
, okp
);
13536 if (h
->u2
.vtable
->used
== NULL
)
13538 /* None of this table's entries were referenced. Re-use the
13540 h
->u2
.vtable
->used
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13541 h
->u2
.vtable
->size
= h
->u2
.vtable
->parent
->u2
.vtable
->size
;
13546 bfd_boolean
*cu
, *pu
;
13548 /* Or the parent's entries into ours. */
13549 cu
= h
->u2
.vtable
->used
;
13551 pu
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13554 const struct elf_backend_data
*bed
;
13555 unsigned int log_file_align
;
13557 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
13558 log_file_align
= bed
->s
->log_file_align
;
13559 n
= h
->u2
.vtable
->parent
->u2
.vtable
->size
>> log_file_align
;
13574 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
13577 bfd_vma hstart
, hend
;
13578 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
13579 const struct elf_backend_data
*bed
;
13580 unsigned int log_file_align
;
13582 /* Take care of both those symbols that do not describe vtables as
13583 well as those that are not loaded. */
13585 || h
->u2
.vtable
== NULL
13586 || h
->u2
.vtable
->parent
== NULL
)
13589 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
13590 || h
->root
.type
== bfd_link_hash_defweak
);
13592 sec
= h
->root
.u
.def
.section
;
13593 hstart
= h
->root
.u
.def
.value
;
13594 hend
= hstart
+ h
->size
;
13596 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
13598 return *(bfd_boolean
*) okp
= FALSE
;
13599 bed
= get_elf_backend_data (sec
->owner
);
13600 log_file_align
= bed
->s
->log_file_align
;
13602 relend
= relstart
+ sec
->reloc_count
;
13604 for (rel
= relstart
; rel
< relend
; ++rel
)
13605 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
13607 /* If the entry is in use, do nothing. */
13608 if (h
->u2
.vtable
->used
13609 && (rel
->r_offset
- hstart
) < h
->u2
.vtable
->size
)
13611 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
13612 if (h
->u2
.vtable
->used
[entry
])
13615 /* Otherwise, kill it. */
13616 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
13622 /* Mark sections containing dynamically referenced symbols. When
13623 building shared libraries, we must assume that any visible symbol is
13627 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
13629 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
13630 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
13632 if ((h
->root
.type
== bfd_link_hash_defined
13633 || h
->root
.type
== bfd_link_hash_defweak
)
13634 && ((h
->ref_dynamic
&& !h
->forced_local
)
13635 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
13636 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
13637 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
13638 && (!bfd_link_executable (info
)
13639 || info
->gc_keep_exported
13640 || info
->export_dynamic
13643 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
13644 && (h
->versioned
>= versioned
13645 || !bfd_hide_sym_by_version (info
->version_info
,
13646 h
->root
.root
.string
)))))
13647 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13652 /* Keep all sections containing symbols undefined on the command-line,
13653 and the section containing the entry symbol. */
13656 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
13658 struct bfd_sym_chain
*sym
;
13660 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
13662 struct elf_link_hash_entry
*h
;
13664 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
13665 FALSE
, FALSE
, FALSE
);
13668 && (h
->root
.type
== bfd_link_hash_defined
13669 || h
->root
.type
== bfd_link_hash_defweak
)
13670 && !bfd_is_abs_section (h
->root
.u
.def
.section
)
13671 && !bfd_is_und_section (h
->root
.u
.def
.section
))
13672 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13677 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
13678 struct bfd_link_info
*info
)
13680 bfd
*ibfd
= info
->input_bfds
;
13682 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13685 struct elf_reloc_cookie cookie
;
13687 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13689 sec
= ibfd
->sections
;
13690 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13693 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
13696 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
13698 if (CONST_STRNEQ (bfd_section_name (sec
), ".eh_frame_entry")
13699 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
13701 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
13702 fini_reloc_cookie_rels (&cookie
, sec
);
13709 /* Do mark and sweep of unused sections. */
13712 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
13714 bfd_boolean ok
= TRUE
;
13716 elf_gc_mark_hook_fn gc_mark_hook
;
13717 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13718 struct elf_link_hash_table
*htab
;
13720 if (!bed
->can_gc_sections
13721 || !is_elf_hash_table (info
->hash
))
13723 _bfd_error_handler(_("warning: gc-sections option ignored"));
13727 bed
->gc_keep (info
);
13728 htab
= elf_hash_table (info
);
13730 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13731 at the .eh_frame section if we can mark the FDEs individually. */
13732 for (sub
= info
->input_bfds
;
13733 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
13734 sub
= sub
->link
.next
)
13737 struct elf_reloc_cookie cookie
;
13739 sec
= sub
->sections
;
13740 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13742 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
13743 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
13745 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
13746 if (elf_section_data (sec
)->sec_info
13747 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
13748 elf_eh_frame_section (sub
) = sec
;
13749 fini_reloc_cookie_for_section (&cookie
, sec
);
13750 sec
= bfd_get_next_section_by_name (NULL
, sec
);
13754 /* Apply transitive closure to the vtable entry usage info. */
13755 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
13759 /* Kill the vtable relocations that were not used. */
13760 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &ok
);
13764 /* Mark dynamically referenced symbols. */
13765 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
13766 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
13768 /* Grovel through relocs to find out who stays ... */
13769 gc_mark_hook
= bed
->gc_mark_hook
;
13770 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13774 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13775 || elf_object_id (sub
) != elf_hash_table_id (htab
)
13776 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13780 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13783 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13784 Also treat note sections as a root, if the section is not part
13785 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13786 well as FINI_ARRAY sections for ld -r. */
13787 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13789 && (o
->flags
& SEC_EXCLUDE
) == 0
13790 && ((o
->flags
& SEC_KEEP
) != 0
13791 || (bfd_link_relocatable (info
)
13792 && ((elf_section_data (o
)->this_hdr
.sh_type
13793 == SHT_PREINIT_ARRAY
)
13794 || (elf_section_data (o
)->this_hdr
.sh_type
13796 || (elf_section_data (o
)->this_hdr
.sh_type
13797 == SHT_FINI_ARRAY
)))
13798 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
13799 && elf_next_in_group (o
) == NULL
)))
13801 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
13806 /* Allow the backend to mark additional target specific sections. */
13807 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
13809 /* ... and mark SEC_EXCLUDE for those that go. */
13810 return elf_gc_sweep (abfd
, info
);
13813 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13816 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
13818 struct elf_link_hash_entry
*h
,
13821 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
13822 struct elf_link_hash_entry
**search
, *child
;
13823 size_t extsymcount
;
13824 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13826 /* The sh_info field of the symtab header tells us where the
13827 external symbols start. We don't care about the local symbols at
13829 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
13830 if (!elf_bad_symtab (abfd
))
13831 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
13833 sym_hashes
= elf_sym_hashes (abfd
);
13834 sym_hashes_end
= sym_hashes
+ extsymcount
;
13836 /* Hunt down the child symbol, which is in this section at the same
13837 offset as the relocation. */
13838 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
13840 if ((child
= *search
) != NULL
13841 && (child
->root
.type
== bfd_link_hash_defined
13842 || child
->root
.type
== bfd_link_hash_defweak
)
13843 && child
->root
.u
.def
.section
== sec
13844 && child
->root
.u
.def
.value
== offset
)
13848 /* xgettext:c-format */
13849 _bfd_error_handler (_("%pB: %pA+%#" PRIx64
": no symbol found for INHERIT"),
13850 abfd
, sec
, (uint64_t) offset
);
13851 bfd_set_error (bfd_error_invalid_operation
);
13855 if (!child
->u2
.vtable
)
13857 child
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
13858 bfd_zalloc (abfd
, sizeof (*child
->u2
.vtable
)));
13859 if (!child
->u2
.vtable
)
13864 /* This *should* only be the absolute section. It could potentially
13865 be that someone has defined a non-global vtable though, which
13866 would be bad. It isn't worth paging in the local symbols to be
13867 sure though; that case should simply be handled by the assembler. */
13869 child
->u2
.vtable
->parent
= (struct elf_link_hash_entry
*) -1;
13872 child
->u2
.vtable
->parent
= h
;
13877 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13880 bfd_elf_gc_record_vtentry (bfd
*abfd
, asection
*sec
,
13881 struct elf_link_hash_entry
*h
,
13884 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13885 unsigned int log_file_align
= bed
->s
->log_file_align
;
13889 /* xgettext:c-format */
13890 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
13892 bfd_set_error (bfd_error_bad_value
);
13898 h
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
13899 bfd_zalloc (abfd
, sizeof (*h
->u2
.vtable
)));
13904 if (addend
>= h
->u2
.vtable
->size
)
13906 size_t size
, bytes
, file_align
;
13907 bfd_boolean
*ptr
= h
->u2
.vtable
->used
;
13909 /* While the symbol is undefined, we have to be prepared to handle
13911 file_align
= 1 << log_file_align
;
13912 if (h
->root
.type
== bfd_link_hash_undefined
)
13913 size
= addend
+ file_align
;
13917 if (addend
>= size
)
13919 /* Oops! We've got a reference past the defined end of
13920 the table. This is probably a bug -- shall we warn? */
13921 size
= addend
+ file_align
;
13924 size
= (size
+ file_align
- 1) & -file_align
;
13926 /* Allocate one extra entry for use as a "done" flag for the
13927 consolidation pass. */
13928 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
13932 ptr
= (bfd_boolean
*) bfd_realloc (ptr
- 1, bytes
);
13938 oldbytes
= (((h
->u2
.vtable
->size
>> log_file_align
) + 1)
13939 * sizeof (bfd_boolean
));
13940 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
13944 ptr
= (bfd_boolean
*) bfd_zmalloc (bytes
);
13949 /* And arrange for that done flag to be at index -1. */
13950 h
->u2
.vtable
->used
= ptr
+ 1;
13951 h
->u2
.vtable
->size
= size
;
13954 h
->u2
.vtable
->used
[addend
>> log_file_align
] = TRUE
;
13959 /* Map an ELF section header flag to its corresponding string. */
13963 flagword flag_value
;
13964 } elf_flags_to_name_table
;
13966 static elf_flags_to_name_table elf_flags_to_names
[] =
13968 { "SHF_WRITE", SHF_WRITE
},
13969 { "SHF_ALLOC", SHF_ALLOC
},
13970 { "SHF_EXECINSTR", SHF_EXECINSTR
},
13971 { "SHF_MERGE", SHF_MERGE
},
13972 { "SHF_STRINGS", SHF_STRINGS
},
13973 { "SHF_INFO_LINK", SHF_INFO_LINK
},
13974 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
13975 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
13976 { "SHF_GROUP", SHF_GROUP
},
13977 { "SHF_TLS", SHF_TLS
},
13978 { "SHF_MASKOS", SHF_MASKOS
},
13979 { "SHF_EXCLUDE", SHF_EXCLUDE
},
13982 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13984 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
13985 struct flag_info
*flaginfo
,
13988 const bfd_vma sh_flags
= elf_section_flags (section
);
13990 if (!flaginfo
->flags_initialized
)
13992 bfd
*obfd
= info
->output_bfd
;
13993 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13994 struct flag_info_list
*tf
= flaginfo
->flag_list
;
13996 int without_hex
= 0;
13998 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
14001 flagword (*lookup
) (char *);
14003 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
14004 if (lookup
!= NULL
)
14006 flagword hexval
= (*lookup
) ((char *) tf
->name
);
14010 if (tf
->with
== with_flags
)
14011 with_hex
|= hexval
;
14012 else if (tf
->with
== without_flags
)
14013 without_hex
|= hexval
;
14018 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
14020 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
14022 if (tf
->with
== with_flags
)
14023 with_hex
|= elf_flags_to_names
[i
].flag_value
;
14024 else if (tf
->with
== without_flags
)
14025 without_hex
|= elf_flags_to_names
[i
].flag_value
;
14032 info
->callbacks
->einfo
14033 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
14037 flaginfo
->flags_initialized
= TRUE
;
14038 flaginfo
->only_with_flags
|= with_hex
;
14039 flaginfo
->not_with_flags
|= without_hex
;
14042 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
14045 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
14051 struct alloc_got_off_arg
{
14053 struct bfd_link_info
*info
;
14056 /* We need a special top-level link routine to convert got reference counts
14057 to real got offsets. */
14060 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
14062 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
14063 bfd
*obfd
= gofarg
->info
->output_bfd
;
14064 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14066 if (h
->got
.refcount
> 0)
14068 h
->got
.offset
= gofarg
->gotoff
;
14069 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
14072 h
->got
.offset
= (bfd_vma
) -1;
14077 /* And an accompanying bit to work out final got entry offsets once
14078 we're done. Should be called from final_link. */
14081 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
14082 struct bfd_link_info
*info
)
14085 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14087 struct alloc_got_off_arg gofarg
;
14089 BFD_ASSERT (abfd
== info
->output_bfd
);
14091 if (! is_elf_hash_table (info
->hash
))
14094 /* The GOT offset is relative to the .got section, but the GOT header is
14095 put into the .got.plt section, if the backend uses it. */
14096 if (bed
->want_got_plt
)
14099 gotoff
= bed
->got_header_size
;
14101 /* Do the local .got entries first. */
14102 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
14104 bfd_signed_vma
*local_got
;
14105 size_t j
, locsymcount
;
14106 Elf_Internal_Shdr
*symtab_hdr
;
14108 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
14111 local_got
= elf_local_got_refcounts (i
);
14115 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
14116 if (elf_bad_symtab (i
))
14117 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
14119 locsymcount
= symtab_hdr
->sh_info
;
14121 for (j
= 0; j
< locsymcount
; ++j
)
14123 if (local_got
[j
] > 0)
14125 local_got
[j
] = gotoff
;
14126 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
14129 local_got
[j
] = (bfd_vma
) -1;
14133 /* Then the global .got entries. .plt refcounts are handled by
14134 adjust_dynamic_symbol */
14135 gofarg
.gotoff
= gotoff
;
14136 gofarg
.info
= info
;
14137 elf_link_hash_traverse (elf_hash_table (info
),
14138 elf_gc_allocate_got_offsets
,
14143 /* Many folk need no more in the way of final link than this, once
14144 got entry reference counting is enabled. */
14147 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
14149 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
14152 /* Invoke the regular ELF backend linker to do all the work. */
14153 return bfd_elf_final_link (abfd
, info
);
14157 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
14159 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
14161 if (rcookie
->bad_symtab
)
14162 rcookie
->rel
= rcookie
->rels
;
14164 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
14166 unsigned long r_symndx
;
14168 if (! rcookie
->bad_symtab
)
14169 if (rcookie
->rel
->r_offset
> offset
)
14171 if (rcookie
->rel
->r_offset
!= offset
)
14174 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
14175 if (r_symndx
== STN_UNDEF
)
14178 if (r_symndx
>= rcookie
->locsymcount
14179 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
14181 struct elf_link_hash_entry
*h
;
14183 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
14185 while (h
->root
.type
== bfd_link_hash_indirect
14186 || h
->root
.type
== bfd_link_hash_warning
)
14187 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
14189 if ((h
->root
.type
== bfd_link_hash_defined
14190 || h
->root
.type
== bfd_link_hash_defweak
)
14191 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
14192 || h
->root
.u
.def
.section
->kept_section
!= NULL
14193 || discarded_section (h
->root
.u
.def
.section
)))
14198 /* It's not a relocation against a global symbol,
14199 but it could be a relocation against a local
14200 symbol for a discarded section. */
14202 Elf_Internal_Sym
*isym
;
14204 /* Need to: get the symbol; get the section. */
14205 isym
= &rcookie
->locsyms
[r_symndx
];
14206 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
14208 && (isec
->kept_section
!= NULL
14209 || discarded_section (isec
)))
14217 /* Discard unneeded references to discarded sections.
14218 Returns -1 on error, 1 if any section's size was changed, 0 if
14219 nothing changed. This function assumes that the relocations are in
14220 sorted order, which is true for all known assemblers. */
14223 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
14225 struct elf_reloc_cookie cookie
;
14230 if (info
->traditional_format
14231 || !is_elf_hash_table (info
->hash
))
14234 o
= bfd_get_section_by_name (output_bfd
, ".stab");
14239 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14242 || i
->reloc_count
== 0
14243 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
14247 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14250 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14253 if (_bfd_discard_section_stabs (abfd
, i
,
14254 elf_section_data (i
)->sec_info
,
14255 bfd_elf_reloc_symbol_deleted_p
,
14259 fini_reloc_cookie_for_section (&cookie
, i
);
14264 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
14265 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
14269 int eh_changed
= 0;
14270 unsigned int eh_alignment
; /* Octets. */
14272 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14278 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14281 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14284 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
14285 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
14286 bfd_elf_reloc_symbol_deleted_p
,
14290 if (i
->size
!= i
->rawsize
)
14294 fini_reloc_cookie_for_section (&cookie
, i
);
14297 eh_alignment
= ((1 << o
->alignment_power
)
14298 * bfd_octets_per_byte (output_bfd
, o
));
14299 /* Skip over zero terminator, and prevent empty sections from
14300 adding alignment padding at the end. */
14301 for (i
= o
->map_tail
.s
; i
!= NULL
; i
= i
->map_tail
.s
)
14303 i
->flags
|= SEC_EXCLUDE
;
14304 else if (i
->size
> 4)
14306 /* The last non-empty eh_frame section doesn't need padding. */
14309 /* Any prior sections must pad the last FDE out to the output
14310 section alignment. Otherwise we might have zero padding
14311 between sections, which would be seen as a terminator. */
14312 for (; i
!= NULL
; i
= i
->map_tail
.s
)
14314 /* All but the last zero terminator should have been removed. */
14319 = (i
->size
+ eh_alignment
- 1) & -eh_alignment
;
14320 if (i
->size
!= size
)
14328 elf_link_hash_traverse (elf_hash_table (info
),
14329 _bfd_elf_adjust_eh_frame_global_symbol
, NULL
);
14332 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
14334 const struct elf_backend_data
*bed
;
14337 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14339 s
= abfd
->sections
;
14340 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14343 bed
= get_elf_backend_data (abfd
);
14345 if (bed
->elf_backend_discard_info
!= NULL
)
14347 if (!init_reloc_cookie (&cookie
, info
, abfd
))
14350 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
14353 fini_reloc_cookie (&cookie
, abfd
);
14357 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
14358 _bfd_elf_end_eh_frame_parsing (info
);
14360 if (info
->eh_frame_hdr_type
14361 && !bfd_link_relocatable (info
)
14362 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
14369 _bfd_elf_section_already_linked (bfd
*abfd
,
14371 struct bfd_link_info
*info
)
14374 const char *name
, *key
;
14375 struct bfd_section_already_linked
*l
;
14376 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
14378 if (sec
->output_section
== bfd_abs_section_ptr
)
14381 flags
= sec
->flags
;
14383 /* Return if it isn't a linkonce section. A comdat group section
14384 also has SEC_LINK_ONCE set. */
14385 if ((flags
& SEC_LINK_ONCE
) == 0)
14388 /* Don't put group member sections on our list of already linked
14389 sections. They are handled as a group via their group section. */
14390 if (elf_sec_group (sec
) != NULL
)
14393 /* For a SHT_GROUP section, use the group signature as the key. */
14395 if ((flags
& SEC_GROUP
) != 0
14396 && elf_next_in_group (sec
) != NULL
14397 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
14398 key
= elf_group_name (elf_next_in_group (sec
));
14401 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14402 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
14403 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
14406 /* Must be a user linkonce section that doesn't follow gcc's
14407 naming convention. In this case we won't be matching
14408 single member groups. */
14412 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
14414 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14416 /* We may have 2 different types of sections on the list: group
14417 sections with a signature of <key> (<key> is some string),
14418 and linkonce sections named .gnu.linkonce.<type>.<key>.
14419 Match like sections. LTO plugin sections are an exception.
14420 They are always named .gnu.linkonce.t.<key> and match either
14421 type of section. */
14422 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
14423 && ((flags
& SEC_GROUP
) != 0
14424 || strcmp (name
, l
->sec
->name
) == 0))
14425 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0)
14427 /* The section has already been linked. See if we should
14428 issue a warning. */
14429 if (!_bfd_handle_already_linked (sec
, l
, info
))
14432 if (flags
& SEC_GROUP
)
14434 asection
*first
= elf_next_in_group (sec
);
14435 asection
*s
= first
;
14439 s
->output_section
= bfd_abs_section_ptr
;
14440 /* Record which group discards it. */
14441 s
->kept_section
= l
->sec
;
14442 s
= elf_next_in_group (s
);
14443 /* These lists are circular. */
14453 /* A single member comdat group section may be discarded by a
14454 linkonce section and vice versa. */
14455 if ((flags
& SEC_GROUP
) != 0)
14457 asection
*first
= elf_next_in_group (sec
);
14459 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
14460 /* Check this single member group against linkonce sections. */
14461 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14462 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14463 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
14465 first
->output_section
= bfd_abs_section_ptr
;
14466 first
->kept_section
= l
->sec
;
14467 sec
->output_section
= bfd_abs_section_ptr
;
14472 /* Check this linkonce section against single member groups. */
14473 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14474 if (l
->sec
->flags
& SEC_GROUP
)
14476 asection
*first
= elf_next_in_group (l
->sec
);
14479 && elf_next_in_group (first
) == first
14480 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
14482 sec
->output_section
= bfd_abs_section_ptr
;
14483 sec
->kept_section
= first
;
14488 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14489 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14490 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14491 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14492 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14493 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14494 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14495 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14496 The reverse order cannot happen as there is never a bfd with only the
14497 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14498 matter as here were are looking only for cross-bfd sections. */
14500 if ((flags
& SEC_GROUP
) == 0 && CONST_STRNEQ (name
, ".gnu.linkonce.r."))
14501 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14502 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14503 && CONST_STRNEQ (l
->sec
->name
, ".gnu.linkonce.t."))
14505 if (abfd
!= l
->sec
->owner
)
14506 sec
->output_section
= bfd_abs_section_ptr
;
14510 /* This is the first section with this name. Record it. */
14511 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
14512 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
14513 return sec
->output_section
== bfd_abs_section_ptr
;
14517 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
14519 return sym
->st_shndx
== SHN_COMMON
;
14523 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
14529 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
14531 return bfd_com_section_ptr
;
14535 _bfd_elf_default_got_elt_size (bfd
*abfd
,
14536 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
14537 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
14538 bfd
*ibfd ATTRIBUTE_UNUSED
,
14539 unsigned long symndx ATTRIBUTE_UNUSED
)
14541 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14542 return bed
->s
->arch_size
/ 8;
14545 /* Routines to support the creation of dynamic relocs. */
14547 /* Returns the name of the dynamic reloc section associated with SEC. */
14549 static const char *
14550 get_dynamic_reloc_section_name (bfd
* abfd
,
14552 bfd_boolean is_rela
)
14555 const char *old_name
= bfd_section_name (sec
);
14556 const char *prefix
= is_rela
? ".rela" : ".rel";
14558 if (old_name
== NULL
)
14561 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
14562 sprintf (name
, "%s%s", prefix
, old_name
);
14567 /* Returns the dynamic reloc section associated with SEC.
14568 If necessary compute the name of the dynamic reloc section based
14569 on SEC's name (looked up in ABFD's string table) and the setting
14573 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
14575 bfd_boolean is_rela
)
14577 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14579 if (reloc_sec
== NULL
)
14581 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14585 reloc_sec
= bfd_get_linker_section (abfd
, name
);
14587 if (reloc_sec
!= NULL
)
14588 elf_section_data (sec
)->sreloc
= reloc_sec
;
14595 /* Returns the dynamic reloc section associated with SEC. If the
14596 section does not exist it is created and attached to the DYNOBJ
14597 bfd and stored in the SRELOC field of SEC's elf_section_data
14600 ALIGNMENT is the alignment for the newly created section and
14601 IS_RELA defines whether the name should be .rela.<SEC's name>
14602 or .rel.<SEC's name>. The section name is looked up in the
14603 string table associated with ABFD. */
14606 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
14608 unsigned int alignment
,
14610 bfd_boolean is_rela
)
14612 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14614 if (reloc_sec
== NULL
)
14616 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14621 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
14623 if (reloc_sec
== NULL
)
14625 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
14626 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
14627 if ((sec
->flags
& SEC_ALLOC
) != 0)
14628 flags
|= SEC_ALLOC
| SEC_LOAD
;
14630 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
14631 if (reloc_sec
!= NULL
)
14633 /* _bfd_elf_get_sec_type_attr chooses a section type by
14634 name. Override as it may be wrong, eg. for a user
14635 section named "auto" we'll get ".relauto" which is
14636 seen to be a .rela section. */
14637 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
14638 if (!bfd_set_section_alignment (reloc_sec
, alignment
))
14643 elf_section_data (sec
)->sreloc
= reloc_sec
;
14649 /* Copy the ELF symbol type and other attributes for a linker script
14650 assignment from HSRC to HDEST. Generally this should be treated as
14651 if we found a strong non-dynamic definition for HDEST (except that
14652 ld ignores multiple definition errors). */
14654 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
14655 struct bfd_link_hash_entry
*hdest
,
14656 struct bfd_link_hash_entry
*hsrc
)
14658 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
14659 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
14660 Elf_Internal_Sym isym
;
14662 ehdest
->type
= ehsrc
->type
;
14663 ehdest
->target_internal
= ehsrc
->target_internal
;
14665 isym
.st_other
= ehsrc
->other
;
14666 elf_merge_st_other (abfd
, ehdest
, &isym
, NULL
, TRUE
, FALSE
);
14669 /* Append a RELA relocation REL to section S in BFD. */
14672 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14674 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14675 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
14676 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
14677 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
14680 /* Append a REL relocation REL to section S in BFD. */
14683 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14685 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14686 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
14687 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
14688 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);
14691 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14693 struct bfd_link_hash_entry
*
14694 bfd_elf_define_start_stop (struct bfd_link_info
*info
,
14695 const char *symbol
, asection
*sec
)
14697 struct elf_link_hash_entry
*h
;
14699 h
= elf_link_hash_lookup (elf_hash_table (info
), symbol
,
14700 FALSE
, FALSE
, TRUE
);
14702 && (h
->root
.type
== bfd_link_hash_undefined
14703 || h
->root
.type
== bfd_link_hash_undefweak
14704 || ((h
->ref_regular
|| h
->def_dynamic
) && !h
->def_regular
)))
14706 bfd_boolean was_dynamic
= h
->ref_dynamic
|| h
->def_dynamic
;
14707 h
->root
.type
= bfd_link_hash_defined
;
14708 h
->root
.u
.def
.section
= sec
;
14709 h
->root
.u
.def
.value
= 0;
14710 h
->def_regular
= 1;
14711 h
->def_dynamic
= 0;
14713 h
->u2
.start_stop_section
= sec
;
14714 if (symbol
[0] == '.')
14716 /* .startof. and .sizeof. symbols are local. */
14717 const struct elf_backend_data
*bed
;
14718 bed
= get_elf_backend_data (info
->output_bfd
);
14719 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
14723 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
14724 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED
;
14726 bfd_elf_link_record_dynamic_symbol (info
, h
);