1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2019 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
)
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
)
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 is the largest alignment,
3327 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 if DO_IT is true,
3505 otherwise just check whether one already exists. Returns -1 on error,
3506 1 if a DT_NEEDED tag already exists, and 0 on success. */
3509 elf_add_dt_needed_tag (bfd
*abfd
,
3510 struct bfd_link_info
*info
,
3514 struct elf_link_hash_table
*hash_table
;
3517 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3520 hash_table
= elf_hash_table (info
);
3521 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3522 if (strindex
== (size_t) -1)
3525 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3528 const struct elf_backend_data
*bed
;
3531 bed
= get_elf_backend_data (hash_table
->dynobj
);
3532 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3534 for (extdyn
= sdyn
->contents
;
3535 extdyn
< sdyn
->contents
+ sdyn
->size
;
3536 extdyn
+= bed
->s
->sizeof_dyn
)
3538 Elf_Internal_Dyn dyn
;
3540 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3541 if (dyn
.d_tag
== DT_NEEDED
3542 && dyn
.d_un
.d_val
== strindex
)
3544 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3552 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3555 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3559 /* We were just checking for existence of the tag. */
3560 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3565 /* Return true if SONAME is on the needed list between NEEDED and STOP
3566 (or the end of list if STOP is NULL), and needed by a library that
3570 on_needed_list (const char *soname
,
3571 struct bfd_link_needed_list
*needed
,
3572 struct bfd_link_needed_list
*stop
)
3574 struct bfd_link_needed_list
*look
;
3575 for (look
= needed
; look
!= stop
; look
= look
->next
)
3576 if (strcmp (soname
, look
->name
) == 0
3577 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3578 /* If needed by a library that itself is not directly
3579 needed, recursively check whether that library is
3580 indirectly needed. Since we add DT_NEEDED entries to
3581 the end of the list, library dependencies appear after
3582 the library. Therefore search prior to the current
3583 LOOK, preventing possible infinite recursion. */
3584 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3590 /* Sort symbol by value, section, size, and type. */
3592 elf_sort_symbol (const void *arg1
, const void *arg2
)
3594 const struct elf_link_hash_entry
*h1
;
3595 const struct elf_link_hash_entry
*h2
;
3596 bfd_signed_vma vdiff
;
3601 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3602 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3603 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3605 return vdiff
> 0 ? 1 : -1;
3607 sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3611 /* Sort so that sized symbols are selected over zero size symbols. */
3612 vdiff
= h1
->size
- h2
->size
;
3614 return vdiff
> 0 ? 1 : -1;
3616 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3617 if (h1
->type
!= h2
->type
)
3618 return h1
->type
- h2
->type
;
3620 /* If symbols are properly sized and typed, and multiple strong
3621 aliases are not defined in a shared library by the user we
3622 shouldn't get here. Unfortunately linker script symbols like
3623 __bss_start sometimes match a user symbol defined at the start of
3624 .bss without proper size and type. We'd like to preference the
3625 user symbol over reserved system symbols. Sort on leading
3627 n1
= h1
->root
.root
.string
;
3628 n2
= h2
->root
.root
.string
;
3641 /* Final sort on name selects user symbols like '_u' over reserved
3642 system symbols like '_Z' and also will avoid qsort instability. */
3646 /* This function is used to adjust offsets into .dynstr for
3647 dynamic symbols. This is called via elf_link_hash_traverse. */
3650 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3652 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3654 if (h
->dynindx
!= -1)
3655 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3659 /* Assign string offsets in .dynstr, update all structures referencing
3663 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3665 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3666 struct elf_link_local_dynamic_entry
*entry
;
3667 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3668 bfd
*dynobj
= hash_table
->dynobj
;
3671 const struct elf_backend_data
*bed
;
3674 _bfd_elf_strtab_finalize (dynstr
);
3675 size
= _bfd_elf_strtab_size (dynstr
);
3677 bed
= get_elf_backend_data (dynobj
);
3678 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3679 BFD_ASSERT (sdyn
!= NULL
);
3681 /* Update all .dynamic entries referencing .dynstr strings. */
3682 for (extdyn
= sdyn
->contents
;
3683 extdyn
< sdyn
->contents
+ sdyn
->size
;
3684 extdyn
+= bed
->s
->sizeof_dyn
)
3686 Elf_Internal_Dyn dyn
;
3688 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3692 dyn
.d_un
.d_val
= size
;
3702 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3707 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3710 /* Now update local dynamic symbols. */
3711 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3712 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3713 entry
->isym
.st_name
);
3715 /* And the rest of dynamic symbols. */
3716 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3718 /* Adjust version definitions. */
3719 if (elf_tdata (output_bfd
)->cverdefs
)
3724 Elf_Internal_Verdef def
;
3725 Elf_Internal_Verdaux defaux
;
3727 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3731 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3733 p
+= sizeof (Elf_External_Verdef
);
3734 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3736 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3738 _bfd_elf_swap_verdaux_in (output_bfd
,
3739 (Elf_External_Verdaux
*) p
, &defaux
);
3740 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3742 _bfd_elf_swap_verdaux_out (output_bfd
,
3743 &defaux
, (Elf_External_Verdaux
*) p
);
3744 p
+= sizeof (Elf_External_Verdaux
);
3747 while (def
.vd_next
);
3750 /* Adjust version references. */
3751 if (elf_tdata (output_bfd
)->verref
)
3756 Elf_Internal_Verneed need
;
3757 Elf_Internal_Vernaux needaux
;
3759 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
3763 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3765 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3766 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3767 (Elf_External_Verneed
*) p
);
3768 p
+= sizeof (Elf_External_Verneed
);
3769 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3771 _bfd_elf_swap_vernaux_in (output_bfd
,
3772 (Elf_External_Vernaux
*) p
, &needaux
);
3773 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3775 _bfd_elf_swap_vernaux_out (output_bfd
,
3777 (Elf_External_Vernaux
*) p
);
3778 p
+= sizeof (Elf_External_Vernaux
);
3781 while (need
.vn_next
);
3787 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3788 The default is to only match when the INPUT and OUTPUT are exactly
3792 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3793 const bfd_target
*output
)
3795 return input
== output
;
3798 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3799 This version is used when different targets for the same architecture
3800 are virtually identical. */
3803 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3804 const bfd_target
*output
)
3806 const struct elf_backend_data
*obed
, *ibed
;
3808 if (input
== output
)
3811 ibed
= xvec_get_elf_backend_data (input
);
3812 obed
= xvec_get_elf_backend_data (output
);
3814 if (ibed
->arch
!= obed
->arch
)
3817 /* If both backends are using this function, deem them compatible. */
3818 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3821 /* Make a special call to the linker "notice" function to tell it that
3822 we are about to handle an as-needed lib, or have finished
3823 processing the lib. */
3826 _bfd_elf_notice_as_needed (bfd
*ibfd
,
3827 struct bfd_link_info
*info
,
3828 enum notice_asneeded_action act
)
3830 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
3833 /* Check relocations an ELF object file. */
3836 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
3838 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3839 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
3841 /* If this object is the same format as the output object, and it is
3842 not a shared library, then let the backend look through the
3845 This is required to build global offset table entries and to
3846 arrange for dynamic relocs. It is not required for the
3847 particular common case of linking non PIC code, even when linking
3848 against shared libraries, but unfortunately there is no way of
3849 knowing whether an object file has been compiled PIC or not.
3850 Looking through the relocs is not particularly time consuming.
3851 The problem is that we must either (1) keep the relocs in memory,
3852 which causes the linker to require additional runtime memory or
3853 (2) read the relocs twice from the input file, which wastes time.
3854 This would be a good case for using mmap.
3856 I have no idea how to handle linking PIC code into a file of a
3857 different format. It probably can't be done. */
3858 if ((abfd
->flags
& DYNAMIC
) == 0
3859 && is_elf_hash_table (htab
)
3860 && bed
->check_relocs
!= NULL
3861 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
3862 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
3866 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3868 Elf_Internal_Rela
*internal_relocs
;
3871 /* Don't check relocations in excluded sections. */
3872 if ((o
->flags
& SEC_RELOC
) == 0
3873 || (o
->flags
& SEC_EXCLUDE
) != 0
3874 || o
->reloc_count
== 0
3875 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
3876 && (o
->flags
& SEC_DEBUGGING
) != 0)
3877 || bfd_is_abs_section (o
->output_section
))
3880 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
3882 if (internal_relocs
== NULL
)
3885 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
3887 if (elf_section_data (o
)->relocs
!= internal_relocs
)
3888 free (internal_relocs
);
3898 /* Add symbols from an ELF object file to the linker hash table. */
3901 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3903 Elf_Internal_Ehdr
*ehdr
;
3904 Elf_Internal_Shdr
*hdr
;
3908 struct elf_link_hash_entry
**sym_hash
;
3909 bfd_boolean dynamic
;
3910 Elf_External_Versym
*extversym
= NULL
;
3911 Elf_External_Versym
*extversym_end
= NULL
;
3912 Elf_External_Versym
*ever
;
3913 struct elf_link_hash_entry
*weaks
;
3914 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3915 size_t nondeflt_vers_cnt
= 0;
3916 Elf_Internal_Sym
*isymbuf
= NULL
;
3917 Elf_Internal_Sym
*isym
;
3918 Elf_Internal_Sym
*isymend
;
3919 const struct elf_backend_data
*bed
;
3920 bfd_boolean add_needed
;
3921 struct elf_link_hash_table
*htab
;
3923 void *alloc_mark
= NULL
;
3924 struct bfd_hash_entry
**old_table
= NULL
;
3925 unsigned int old_size
= 0;
3926 unsigned int old_count
= 0;
3927 void *old_tab
= NULL
;
3929 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3930 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3931 void *old_strtab
= NULL
;
3934 bfd_boolean just_syms
;
3936 htab
= elf_hash_table (info
);
3937 bed
= get_elf_backend_data (abfd
);
3939 if ((abfd
->flags
& DYNAMIC
) == 0)
3945 /* You can't use -r against a dynamic object. Also, there's no
3946 hope of using a dynamic object which does not exactly match
3947 the format of the output file. */
3948 if (bfd_link_relocatable (info
)
3949 || !is_elf_hash_table (htab
)
3950 || info
->output_bfd
->xvec
!= abfd
->xvec
)
3952 if (bfd_link_relocatable (info
))
3953 bfd_set_error (bfd_error_invalid_operation
);
3955 bfd_set_error (bfd_error_wrong_format
);
3960 ehdr
= elf_elfheader (abfd
);
3961 if (info
->warn_alternate_em
3962 && bed
->elf_machine_code
!= ehdr
->e_machine
3963 && ((bed
->elf_machine_alt1
!= 0
3964 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
3965 || (bed
->elf_machine_alt2
!= 0
3966 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
3968 /* xgettext:c-format */
3969 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
3970 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
3972 /* As a GNU extension, any input sections which are named
3973 .gnu.warning.SYMBOL are treated as warning symbols for the given
3974 symbol. This differs from .gnu.warning sections, which generate
3975 warnings when they are included in an output file. */
3976 /* PR 12761: Also generate this warning when building shared libraries. */
3977 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3981 name
= bfd_section_name (s
);
3982 if (CONST_STRNEQ (name
, ".gnu.warning."))
3987 name
+= sizeof ".gnu.warning." - 1;
3989 /* If this is a shared object, then look up the symbol
3990 in the hash table. If it is there, and it is already
3991 been defined, then we will not be using the entry
3992 from this shared object, so we don't need to warn.
3993 FIXME: If we see the definition in a regular object
3994 later on, we will warn, but we shouldn't. The only
3995 fix is to keep track of what warnings we are supposed
3996 to emit, and then handle them all at the end of the
4000 struct elf_link_hash_entry
*h
;
4002 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
4004 /* FIXME: What about bfd_link_hash_common? */
4006 && (h
->root
.type
== bfd_link_hash_defined
4007 || h
->root
.type
== bfd_link_hash_defweak
))
4012 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
4016 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
4021 if (! (_bfd_generic_link_add_one_symbol
4022 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
4023 FALSE
, bed
->collect
, NULL
)))
4026 if (bfd_link_executable (info
))
4028 /* Clobber the section size so that the warning does
4029 not get copied into the output file. */
4032 /* Also set SEC_EXCLUDE, so that symbols defined in
4033 the warning section don't get copied to the output. */
4034 s
->flags
|= SEC_EXCLUDE
;
4039 just_syms
= ((s
= abfd
->sections
) != NULL
4040 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
4045 /* If we are creating a shared library, create all the dynamic
4046 sections immediately. We need to attach them to something,
4047 so we attach them to this BFD, provided it is the right
4048 format and is not from ld --just-symbols. Always create the
4049 dynamic sections for -E/--dynamic-list. FIXME: If there
4050 are no input BFD's of the same format as the output, we can't
4051 make a shared library. */
4053 && (bfd_link_pic (info
)
4054 || (!bfd_link_relocatable (info
)
4056 && (info
->export_dynamic
|| info
->dynamic
)))
4057 && is_elf_hash_table (htab
)
4058 && info
->output_bfd
->xvec
== abfd
->xvec
4059 && !htab
->dynamic_sections_created
)
4061 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
4065 else if (!is_elf_hash_table (htab
))
4069 const char *soname
= NULL
;
4071 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
4072 const Elf_Internal_Phdr
*phdr
;
4075 /* ld --just-symbols and dynamic objects don't mix very well.
4076 ld shouldn't allow it. */
4080 /* If this dynamic lib was specified on the command line with
4081 --as-needed in effect, then we don't want to add a DT_NEEDED
4082 tag unless the lib is actually used. Similary for libs brought
4083 in by another lib's DT_NEEDED. When --no-add-needed is used
4084 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4085 any dynamic library in DT_NEEDED tags in the dynamic lib at
4087 add_needed
= (elf_dyn_lib_class (abfd
)
4088 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
4089 | DYN_NO_NEEDED
)) == 0;
4091 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4096 unsigned int elfsec
;
4097 unsigned long shlink
;
4099 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
4106 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
4107 if (elfsec
== SHN_BAD
)
4108 goto error_free_dyn
;
4109 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
4111 for (extdyn
= dynbuf
;
4112 extdyn
<= dynbuf
+ s
->size
- bed
->s
->sizeof_dyn
;
4113 extdyn
+= bed
->s
->sizeof_dyn
)
4115 Elf_Internal_Dyn dyn
;
4117 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
4118 if (dyn
.d_tag
== DT_SONAME
)
4120 unsigned int tagv
= dyn
.d_un
.d_val
;
4121 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4123 goto error_free_dyn
;
4125 if (dyn
.d_tag
== DT_NEEDED
)
4127 struct bfd_link_needed_list
*n
, **pn
;
4129 unsigned int tagv
= dyn
.d_un
.d_val
;
4131 amt
= sizeof (struct bfd_link_needed_list
);
4132 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4133 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4134 if (n
== NULL
|| fnm
== NULL
)
4135 goto error_free_dyn
;
4136 amt
= strlen (fnm
) + 1;
4137 anm
= (char *) bfd_alloc (abfd
, amt
);
4139 goto error_free_dyn
;
4140 memcpy (anm
, fnm
, amt
);
4144 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4148 if (dyn
.d_tag
== DT_RUNPATH
)
4150 struct bfd_link_needed_list
*n
, **pn
;
4152 unsigned int tagv
= dyn
.d_un
.d_val
;
4154 amt
= sizeof (struct bfd_link_needed_list
);
4155 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4156 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4157 if (n
== NULL
|| fnm
== NULL
)
4158 goto error_free_dyn
;
4159 amt
= strlen (fnm
) + 1;
4160 anm
= (char *) bfd_alloc (abfd
, amt
);
4162 goto error_free_dyn
;
4163 memcpy (anm
, fnm
, amt
);
4167 for (pn
= & runpath
;
4173 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4174 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
4176 struct bfd_link_needed_list
*n
, **pn
;
4178 unsigned int tagv
= dyn
.d_un
.d_val
;
4180 amt
= sizeof (struct bfd_link_needed_list
);
4181 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4182 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4183 if (n
== NULL
|| fnm
== NULL
)
4184 goto error_free_dyn
;
4185 amt
= strlen (fnm
) + 1;
4186 anm
= (char *) bfd_alloc (abfd
, amt
);
4188 goto error_free_dyn
;
4189 memcpy (anm
, fnm
, amt
);
4199 if (dyn
.d_tag
== DT_AUDIT
)
4201 unsigned int tagv
= dyn
.d_un
.d_val
;
4202 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4209 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4210 frees all more recently bfd_alloc'd blocks as well. */
4216 struct bfd_link_needed_list
**pn
;
4217 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4222 /* If we have a PT_GNU_RELRO program header, mark as read-only
4223 all sections contained fully therein. This makes relro
4224 shared library sections appear as they will at run-time. */
4225 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4226 while (phdr
-- > elf_tdata (abfd
)->phdr
)
4227 if (phdr
->p_type
== PT_GNU_RELRO
)
4229 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4230 if ((s
->flags
& SEC_ALLOC
) != 0
4231 && s
->vma
>= phdr
->p_vaddr
4232 && s
->vma
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4233 s
->flags
|= SEC_READONLY
;
4237 /* We do not want to include any of the sections in a dynamic
4238 object in the output file. We hack by simply clobbering the
4239 list of sections in the BFD. This could be handled more
4240 cleanly by, say, a new section flag; the existing
4241 SEC_NEVER_LOAD flag is not the one we want, because that one
4242 still implies that the section takes up space in the output
4244 bfd_section_list_clear (abfd
);
4246 /* Find the name to use in a DT_NEEDED entry that refers to this
4247 object. If the object has a DT_SONAME entry, we use it.
4248 Otherwise, if the generic linker stuck something in
4249 elf_dt_name, we use that. Otherwise, we just use the file
4251 if (soname
== NULL
|| *soname
== '\0')
4253 soname
= elf_dt_name (abfd
);
4254 if (soname
== NULL
|| *soname
== '\0')
4255 soname
= bfd_get_filename (abfd
);
4258 /* Save the SONAME because sometimes the linker emulation code
4259 will need to know it. */
4260 elf_dt_name (abfd
) = soname
;
4262 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4266 /* If we have already included this dynamic object in the
4267 link, just ignore it. There is no reason to include a
4268 particular dynamic object more than once. */
4272 /* Save the DT_AUDIT entry for the linker emulation code. */
4273 elf_dt_audit (abfd
) = audit
;
4276 /* If this is a dynamic object, we always link against the .dynsym
4277 symbol table, not the .symtab symbol table. The dynamic linker
4278 will only see the .dynsym symbol table, so there is no reason to
4279 look at .symtab for a dynamic object. */
4281 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4282 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4284 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4286 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4288 /* The sh_info field of the symtab header tells us where the
4289 external symbols start. We don't care about the local symbols at
4291 if (elf_bad_symtab (abfd
))
4293 extsymcount
= symcount
;
4298 extsymcount
= symcount
- hdr
->sh_info
;
4299 extsymoff
= hdr
->sh_info
;
4302 sym_hash
= elf_sym_hashes (abfd
);
4303 if (extsymcount
!= 0)
4305 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4307 if (isymbuf
== NULL
)
4310 if (sym_hash
== NULL
)
4312 /* We store a pointer to the hash table entry for each
4315 amt
*= sizeof (struct elf_link_hash_entry
*);
4316 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4317 if (sym_hash
== NULL
)
4318 goto error_free_sym
;
4319 elf_sym_hashes (abfd
) = sym_hash
;
4325 /* Read in any version definitions. */
4326 if (!_bfd_elf_slurp_version_tables (abfd
,
4327 info
->default_imported_symver
))
4328 goto error_free_sym
;
4330 /* Read in the symbol versions, but don't bother to convert them
4331 to internal format. */
4332 if (elf_dynversym (abfd
) != 0)
4334 Elf_Internal_Shdr
*versymhdr
;
4336 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4337 amt
= versymhdr
->sh_size
;
4338 extversym
= (Elf_External_Versym
*) bfd_malloc (amt
);
4339 if (extversym
== NULL
)
4340 goto error_free_sym
;
4341 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
4342 || bfd_bread (extversym
, amt
, abfd
) != amt
)
4343 goto error_free_vers
;
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
;
4395 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4396 if (old_strtab
== NULL
)
4397 goto error_free_vers
;
4399 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4401 struct bfd_hash_entry
*p
;
4402 struct elf_link_hash_entry
*h
;
4404 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4406 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
4407 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4408 h
= (struct elf_link_hash_entry
*) p
;
4409 if (h
->root
.type
== bfd_link_hash_warning
)
4411 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
4412 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4419 if (extversym
== NULL
)
4421 else if (extversym
+ extsymoff
< extversym_end
)
4422 ever
= extversym
+ extsymoff
;
4425 /* xgettext:c-format */
4426 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4427 abfd
, (long) extsymoff
,
4428 (long) (extversym_end
- extversym
) / sizeof (* extversym
));
4429 bfd_set_error (bfd_error_bad_value
);
4430 goto error_free_vers
;
4433 if (!bfd_link_relocatable (info
)
4434 && abfd
->lto_slim_object
)
4437 (_("%pB: plugin needed to handle lto object"), abfd
);
4440 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
4442 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4446 asection
*sec
, *new_sec
;
4449 struct elf_link_hash_entry
*h
;
4450 struct elf_link_hash_entry
*hi
;
4451 bfd_boolean definition
;
4452 bfd_boolean size_change_ok
;
4453 bfd_boolean type_change_ok
;
4454 bfd_boolean new_weak
;
4455 bfd_boolean old_weak
;
4456 bfd_boolean override
;
4458 bfd_boolean discarded
;
4459 unsigned int old_alignment
;
4460 unsigned int shindex
;
4462 bfd_boolean matched
;
4466 flags
= BSF_NO_FLAGS
;
4468 value
= isym
->st_value
;
4469 common
= bed
->common_definition (isym
);
4470 if (common
&& info
->inhibit_common_definition
)
4472 /* Treat common symbol as undefined for --no-define-common. */
4473 isym
->st_shndx
= SHN_UNDEF
;
4478 bind
= ELF_ST_BIND (isym
->st_info
);
4482 /* This should be impossible, since ELF requires that all
4483 global symbols follow all local symbols, and that sh_info
4484 point to the first global symbol. Unfortunately, Irix 5
4486 if (elf_bad_symtab (abfd
))
4489 /* If we aren't prepared to handle locals within the globals
4490 then we'll likely segfault on a NULL symbol hash if the
4491 symbol is ever referenced in relocations. */
4492 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
4493 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, hdr
->sh_name
);
4494 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4495 " (>= sh_info of %lu)"),
4496 abfd
, name
, (long) (isym
- isymbuf
+ extsymoff
),
4499 /* Dynamic object relocations are not processed by ld, so
4500 ld won't run into the problem mentioned above. */
4503 bfd_set_error (bfd_error_bad_value
);
4504 goto error_free_vers
;
4507 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4515 case STB_GNU_UNIQUE
:
4516 flags
= BSF_GNU_UNIQUE
;
4520 /* Leave it up to the processor backend. */
4524 if (isym
->st_shndx
== SHN_UNDEF
)
4525 sec
= bfd_und_section_ptr
;
4526 else if (isym
->st_shndx
== SHN_ABS
)
4527 sec
= bfd_abs_section_ptr
;
4528 else if (isym
->st_shndx
== SHN_COMMON
)
4530 sec
= bfd_com_section_ptr
;
4531 /* What ELF calls the size we call the value. What ELF
4532 calls the value we call the alignment. */
4533 value
= isym
->st_size
;
4537 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4539 sec
= bfd_abs_section_ptr
;
4540 else if (discarded_section (sec
))
4542 /* Symbols from discarded section are undefined. We keep
4544 sec
= bfd_und_section_ptr
;
4546 isym
->st_shndx
= SHN_UNDEF
;
4548 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4552 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4555 goto error_free_vers
;
4557 if (isym
->st_shndx
== SHN_COMMON
4558 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4560 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4564 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4566 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4568 goto error_free_vers
;
4572 else if (isym
->st_shndx
== SHN_COMMON
4573 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4574 && !bfd_link_relocatable (info
))
4576 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4580 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4581 | SEC_LINKER_CREATED
);
4582 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4584 goto error_free_vers
;
4588 else if (bed
->elf_add_symbol_hook
)
4590 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4592 goto error_free_vers
;
4594 /* The hook function sets the name to NULL if this symbol
4595 should be skipped for some reason. */
4600 /* Sanity check that all possibilities were handled. */
4604 /* Silently discard TLS symbols from --just-syms. There's
4605 no way to combine a static TLS block with a new TLS block
4606 for this executable. */
4607 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4608 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4611 if (bfd_is_und_section (sec
)
4612 || bfd_is_com_section (sec
))
4617 size_change_ok
= FALSE
;
4618 type_change_ok
= bed
->type_change_ok
;
4625 if (is_elf_hash_table (htab
))
4627 Elf_Internal_Versym iver
;
4628 unsigned int vernum
= 0;
4633 if (info
->default_imported_symver
)
4634 /* Use the default symbol version created earlier. */
4635 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4639 else if (ever
>= extversym_end
)
4641 /* xgettext:c-format */
4642 _bfd_error_handler (_("%pB: not enough version information"),
4644 bfd_set_error (bfd_error_bad_value
);
4645 goto error_free_vers
;
4648 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4650 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4652 /* If this is a hidden symbol, or if it is not version
4653 1, we append the version name to the symbol name.
4654 However, we do not modify a non-hidden absolute symbol
4655 if it is not a function, because it might be the version
4656 symbol itself. FIXME: What if it isn't? */
4657 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4659 && (!bfd_is_abs_section (sec
)
4660 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4663 size_t namelen
, verlen
, newlen
;
4666 if (isym
->st_shndx
!= SHN_UNDEF
)
4668 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4670 else if (vernum
> 1)
4672 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4679 /* xgettext:c-format */
4680 (_("%pB: %s: invalid version %u (max %d)"),
4682 elf_tdata (abfd
)->cverdefs
);
4683 bfd_set_error (bfd_error_bad_value
);
4684 goto error_free_vers
;
4689 /* We cannot simply test for the number of
4690 entries in the VERNEED section since the
4691 numbers for the needed versions do not start
4693 Elf_Internal_Verneed
*t
;
4696 for (t
= elf_tdata (abfd
)->verref
;
4700 Elf_Internal_Vernaux
*a
;
4702 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4704 if (a
->vna_other
== vernum
)
4706 verstr
= a
->vna_nodename
;
4716 /* xgettext:c-format */
4717 (_("%pB: %s: invalid needed version %d"),
4718 abfd
, name
, vernum
);
4719 bfd_set_error (bfd_error_bad_value
);
4720 goto error_free_vers
;
4724 namelen
= strlen (name
);
4725 verlen
= strlen (verstr
);
4726 newlen
= namelen
+ verlen
+ 2;
4727 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4728 && isym
->st_shndx
!= SHN_UNDEF
)
4731 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4732 if (newname
== NULL
)
4733 goto error_free_vers
;
4734 memcpy (newname
, name
, namelen
);
4735 p
= newname
+ namelen
;
4737 /* If this is a defined non-hidden version symbol,
4738 we add another @ to the name. This indicates the
4739 default version of the symbol. */
4740 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4741 && isym
->st_shndx
!= SHN_UNDEF
)
4743 memcpy (p
, verstr
, verlen
+ 1);
4748 /* If this symbol has default visibility and the user has
4749 requested we not re-export it, then mark it as hidden. */
4750 if (!bfd_is_und_section (sec
)
4753 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4754 isym
->st_other
= (STV_HIDDEN
4755 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4757 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4758 sym_hash
, &old_bfd
, &old_weak
,
4759 &old_alignment
, &skip
, &override
,
4760 &type_change_ok
, &size_change_ok
,
4762 goto error_free_vers
;
4767 /* Override a definition only if the new symbol matches the
4769 if (override
&& matched
)
4773 while (h
->root
.type
== bfd_link_hash_indirect
4774 || h
->root
.type
== bfd_link_hash_warning
)
4775 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4777 if (elf_tdata (abfd
)->verdef
!= NULL
4780 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4783 if (! (_bfd_generic_link_add_one_symbol
4784 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4785 (struct bfd_link_hash_entry
**) sym_hash
)))
4786 goto error_free_vers
;
4789 /* We need to make sure that indirect symbol dynamic flags are
4792 while (h
->root
.type
== bfd_link_hash_indirect
4793 || h
->root
.type
== bfd_link_hash_warning
)
4794 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4796 /* Setting the index to -3 tells elf_link_output_extsym that
4797 this symbol is defined in a discarded section. */
4803 new_weak
= (flags
& BSF_WEAK
) != 0;
4807 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4808 && is_elf_hash_table (htab
)
4809 && h
->u
.alias
== NULL
)
4811 /* Keep a list of all weak defined non function symbols from
4812 a dynamic object, using the alias field. Later in this
4813 function we will set the alias field to the correct
4814 value. We only put non-function symbols from dynamic
4815 objects on this list, because that happens to be the only
4816 time we need to know the normal symbol corresponding to a
4817 weak symbol, and the information is time consuming to
4818 figure out. If the alias field is not already NULL,
4819 then this symbol was already defined by some previous
4820 dynamic object, and we will be using that previous
4821 definition anyhow. */
4827 /* Set the alignment of a common symbol. */
4828 if ((common
|| bfd_is_com_section (sec
))
4829 && h
->root
.type
== bfd_link_hash_common
)
4834 align
= bfd_log2 (isym
->st_value
);
4837 /* The new symbol is a common symbol in a shared object.
4838 We need to get the alignment from the section. */
4839 align
= new_sec
->alignment_power
;
4841 if (align
> old_alignment
)
4842 h
->root
.u
.c
.p
->alignment_power
= align
;
4844 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4847 if (is_elf_hash_table (htab
))
4849 /* Set a flag in the hash table entry indicating the type of
4850 reference or definition we just found. A dynamic symbol
4851 is one which is referenced or defined by both a regular
4852 object and a shared object. */
4853 bfd_boolean dynsym
= FALSE
;
4855 /* Plugin symbols aren't normal. Don't set def_regular or
4856 ref_regular for them, or make them dynamic. */
4857 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4864 if (bind
!= STB_WEAK
)
4865 h
->ref_regular_nonweak
= 1;
4877 /* If the indirect symbol has been forced local, don't
4878 make the real symbol dynamic. */
4879 if ((h
== hi
|| !hi
->forced_local
)
4880 && (bfd_link_dll (info
)
4890 hi
->ref_dynamic
= 1;
4895 hi
->def_dynamic
= 1;
4898 /* If the indirect symbol has been forced local, don't
4899 make the real symbol dynamic. */
4900 if ((h
== hi
|| !hi
->forced_local
)
4904 && weakdef (h
)->dynindx
!= -1)))
4908 /* Check to see if we need to add an indirect symbol for
4909 the default name. */
4911 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
4912 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4913 sec
, value
, &old_bfd
, &dynsym
))
4914 goto error_free_vers
;
4916 /* Check the alignment when a common symbol is involved. This
4917 can change when a common symbol is overridden by a normal
4918 definition or a common symbol is ignored due to the old
4919 normal definition. We need to make sure the maximum
4920 alignment is maintained. */
4921 if ((old_alignment
|| common
)
4922 && h
->root
.type
!= bfd_link_hash_common
)
4924 unsigned int common_align
;
4925 unsigned int normal_align
;
4926 unsigned int symbol_align
;
4930 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
4931 || h
->root
.type
== bfd_link_hash_defweak
);
4933 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4934 if (h
->root
.u
.def
.section
->owner
!= NULL
4935 && (h
->root
.u
.def
.section
->owner
->flags
4936 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
4938 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4939 if (normal_align
> symbol_align
)
4940 normal_align
= symbol_align
;
4943 normal_align
= symbol_align
;
4947 common_align
= old_alignment
;
4948 common_bfd
= old_bfd
;
4953 common_align
= bfd_log2 (isym
->st_value
);
4955 normal_bfd
= old_bfd
;
4958 if (normal_align
< common_align
)
4960 /* PR binutils/2735 */
4961 if (normal_bfd
== NULL
)
4963 /* xgettext:c-format */
4964 (_("warning: alignment %u of common symbol `%s' in %pB is"
4965 " greater than the alignment (%u) of its section %pA"),
4966 1 << common_align
, name
, common_bfd
,
4967 1 << normal_align
, h
->root
.u
.def
.section
);
4970 /* xgettext:c-format */
4971 (_("warning: alignment %u of symbol `%s' in %pB"
4972 " is smaller than %u in %pB"),
4973 1 << normal_align
, name
, normal_bfd
,
4974 1 << common_align
, common_bfd
);
4978 /* Remember the symbol size if it isn't undefined. */
4979 if (isym
->st_size
!= 0
4980 && isym
->st_shndx
!= SHN_UNDEF
4981 && (definition
|| h
->size
== 0))
4984 && h
->size
!= isym
->st_size
4985 && ! size_change_ok
)
4987 /* xgettext:c-format */
4988 (_("warning: size of symbol `%s' changed"
4989 " from %" PRIu64
" in %pB to %" PRIu64
" in %pB"),
4990 name
, (uint64_t) h
->size
, old_bfd
,
4991 (uint64_t) isym
->st_size
, abfd
);
4993 h
->size
= isym
->st_size
;
4996 /* If this is a common symbol, then we always want H->SIZE
4997 to be the size of the common symbol. The code just above
4998 won't fix the size if a common symbol becomes larger. We
4999 don't warn about a size change here, because that is
5000 covered by --warn-common. Allow changes between different
5002 if (h
->root
.type
== bfd_link_hash_common
)
5003 h
->size
= h
->root
.u
.c
.size
;
5005 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
5006 && ((definition
&& !new_weak
)
5007 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
5008 || h
->type
== STT_NOTYPE
))
5010 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
5012 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5014 if (type
== STT_GNU_IFUNC
5015 && (abfd
->flags
& DYNAMIC
) != 0)
5018 if (h
->type
!= type
)
5020 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
5021 /* xgettext:c-format */
5023 (_("warning: type of symbol `%s' changed"
5024 " from %d to %d in %pB"),
5025 name
, h
->type
, type
, abfd
);
5031 /* Merge st_other field. */
5032 elf_merge_st_other (abfd
, h
, isym
, sec
, definition
, dynamic
);
5034 /* We don't want to make debug symbol dynamic. */
5036 && (sec
->flags
& SEC_DEBUGGING
)
5037 && !bfd_link_relocatable (info
))
5040 /* Nor should we make plugin symbols dynamic. */
5041 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5046 h
->target_internal
= isym
->st_target_internal
;
5047 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
5050 if (definition
&& !dynamic
)
5052 char *p
= strchr (name
, ELF_VER_CHR
);
5053 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
5055 /* Queue non-default versions so that .symver x, x@FOO
5056 aliases can be checked. */
5059 amt
= ((isymend
- isym
+ 1)
5060 * sizeof (struct elf_link_hash_entry
*));
5062 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5064 goto error_free_vers
;
5066 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
5070 if (dynsym
&& h
->dynindx
== -1)
5072 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5073 goto error_free_vers
;
5075 && weakdef (h
)->dynindx
== -1)
5077 if (!bfd_elf_link_record_dynamic_symbol (info
, weakdef (h
)))
5078 goto error_free_vers
;
5081 else if (h
->dynindx
!= -1)
5082 /* If the symbol already has a dynamic index, but
5083 visibility says it should not be visible, turn it into
5085 switch (ELF_ST_VISIBILITY (h
->other
))
5089 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
5094 /* Don't add DT_NEEDED for references from the dummy bfd nor
5095 for unmatched symbol. */
5100 && h
->ref_regular_nonweak
5102 || (old_bfd
->flags
& BFD_PLUGIN
) == 0))
5103 || (h
->ref_dynamic_nonweak
5104 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5105 && !on_needed_list (elf_dt_name (abfd
),
5106 htab
->needed
, NULL
))))
5109 const char *soname
= elf_dt_name (abfd
);
5111 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
5112 h
->root
.root
.string
);
5114 /* A symbol from a library loaded via DT_NEEDED of some
5115 other library is referenced by a regular object.
5116 Add a DT_NEEDED entry for it. Issue an error if
5117 --no-add-needed is used and the reference was not
5120 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
5123 /* xgettext:c-format */
5124 (_("%pB: undefined reference to symbol '%s'"),
5126 bfd_set_error (bfd_error_missing_dso
);
5127 goto error_free_vers
;
5130 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
5131 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
5134 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
5136 goto error_free_vers
;
5138 BFD_ASSERT (ret
== 0);
5143 if (info
->lto_plugin_active
5144 && !bfd_link_relocatable (info
)
5145 && (abfd
->flags
& BFD_PLUGIN
) == 0
5151 if (bed
->s
->arch_size
== 32)
5156 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5157 referenced in regular objects so that linker plugin will get
5158 the correct symbol resolution. */
5160 sym_hash
= elf_sym_hashes (abfd
);
5161 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5163 Elf_Internal_Rela
*internal_relocs
;
5164 Elf_Internal_Rela
*rel
, *relend
;
5166 /* Don't check relocations in excluded sections. */
5167 if ((s
->flags
& SEC_RELOC
) == 0
5168 || s
->reloc_count
== 0
5169 || (s
->flags
& SEC_EXCLUDE
) != 0
5170 || ((info
->strip
== strip_all
5171 || info
->strip
== strip_debugger
)
5172 && (s
->flags
& SEC_DEBUGGING
) != 0))
5175 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, s
, NULL
,
5178 if (internal_relocs
== NULL
)
5179 goto error_free_vers
;
5181 rel
= internal_relocs
;
5182 relend
= rel
+ s
->reloc_count
;
5183 for ( ; rel
< relend
; rel
++)
5185 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
5186 struct elf_link_hash_entry
*h
;
5188 /* Skip local symbols. */
5189 if (r_symndx
< extsymoff
)
5192 h
= sym_hash
[r_symndx
- extsymoff
];
5194 h
->root
.non_ir_ref_regular
= 1;
5197 if (elf_section_data (s
)->relocs
!= internal_relocs
)
5198 free (internal_relocs
);
5202 if (extversym
!= NULL
)
5208 if (isymbuf
!= NULL
)
5214 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
5218 /* Restore the symbol table. */
5219 old_ent
= (char *) old_tab
+ tabsize
;
5220 memset (elf_sym_hashes (abfd
), 0,
5221 extsymcount
* sizeof (struct elf_link_hash_entry
*));
5222 htab
->root
.table
.table
= old_table
;
5223 htab
->root
.table
.size
= old_size
;
5224 htab
->root
.table
.count
= old_count
;
5225 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
5226 htab
->root
.undefs
= old_undefs
;
5227 htab
->root
.undefs_tail
= old_undefs_tail
;
5228 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
5231 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
5233 struct bfd_hash_entry
*p
;
5234 struct elf_link_hash_entry
*h
;
5236 unsigned int alignment_power
;
5237 unsigned int non_ir_ref_dynamic
;
5239 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
5241 h
= (struct elf_link_hash_entry
*) p
;
5242 if (h
->root
.type
== bfd_link_hash_warning
)
5243 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5245 /* Preserve the maximum alignment and size for common
5246 symbols even if this dynamic lib isn't on DT_NEEDED
5247 since it can still be loaded at run time by another
5249 if (h
->root
.type
== bfd_link_hash_common
)
5251 size
= h
->root
.u
.c
.size
;
5252 alignment_power
= h
->root
.u
.c
.p
->alignment_power
;
5257 alignment_power
= 0;
5259 /* Preserve non_ir_ref_dynamic so that this symbol
5260 will be exported when the dynamic lib becomes needed
5261 in the second pass. */
5262 non_ir_ref_dynamic
= h
->root
.non_ir_ref_dynamic
;
5263 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
5264 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5265 h
= (struct elf_link_hash_entry
*) p
;
5266 if (h
->root
.type
== bfd_link_hash_warning
)
5268 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
5269 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5270 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5272 if (h
->root
.type
== bfd_link_hash_common
)
5274 if (size
> h
->root
.u
.c
.size
)
5275 h
->root
.u
.c
.size
= size
;
5276 if (alignment_power
> h
->root
.u
.c
.p
->alignment_power
)
5277 h
->root
.u
.c
.p
->alignment_power
= alignment_power
;
5279 h
->root
.non_ir_ref_dynamic
= non_ir_ref_dynamic
;
5283 /* Make a special call to the linker "notice" function to
5284 tell it that symbols added for crefs may need to be removed. */
5285 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
5286 goto error_free_vers
;
5289 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
5291 if (nondeflt_vers
!= NULL
)
5292 free (nondeflt_vers
);
5296 if (old_tab
!= NULL
)
5298 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
5299 goto error_free_vers
;
5304 /* Now that all the symbols from this input file are created, if
5305 not performing a relocatable link, handle .symver foo, foo@BAR
5306 such that any relocs against foo become foo@BAR. */
5307 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5311 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5313 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5314 char *shortname
, *p
;
5316 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5318 || (h
->root
.type
!= bfd_link_hash_defined
5319 && h
->root
.type
!= bfd_link_hash_defweak
))
5322 amt
= p
- h
->root
.root
.string
;
5323 shortname
= (char *) bfd_malloc (amt
+ 1);
5325 goto error_free_vers
;
5326 memcpy (shortname
, h
->root
.root
.string
, amt
);
5327 shortname
[amt
] = '\0';
5329 hi
= (struct elf_link_hash_entry
*)
5330 bfd_link_hash_lookup (&htab
->root
, shortname
,
5331 FALSE
, FALSE
, FALSE
);
5333 && hi
->root
.type
== h
->root
.type
5334 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5335 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5337 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
5338 hi
->root
.type
= bfd_link_hash_indirect
;
5339 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5340 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5341 sym_hash
= elf_sym_hashes (abfd
);
5343 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5344 if (sym_hash
[symidx
] == hi
)
5346 sym_hash
[symidx
] = h
;
5352 free (nondeflt_vers
);
5353 nondeflt_vers
= NULL
;
5356 /* Now set the alias field correctly for all the weak defined
5357 symbols we found. The only way to do this is to search all the
5358 symbols. Since we only need the information for non functions in
5359 dynamic objects, that's the only time we actually put anything on
5360 the list WEAKS. We need this information so that if a regular
5361 object refers to a symbol defined weakly in a dynamic object, the
5362 real symbol in the dynamic object is also put in the dynamic
5363 symbols; we also must arrange for both symbols to point to the
5364 same memory location. We could handle the general case of symbol
5365 aliasing, but a general symbol alias can only be generated in
5366 assembler code, handling it correctly would be very time
5367 consuming, and other ELF linkers don't handle general aliasing
5371 struct elf_link_hash_entry
**hpp
;
5372 struct elf_link_hash_entry
**hppend
;
5373 struct elf_link_hash_entry
**sorted_sym_hash
;
5374 struct elf_link_hash_entry
*h
;
5377 /* Since we have to search the whole symbol list for each weak
5378 defined symbol, search time for N weak defined symbols will be
5379 O(N^2). Binary search will cut it down to O(NlogN). */
5381 amt
*= sizeof (*sorted_sym_hash
);
5382 sorted_sym_hash
= bfd_malloc (amt
);
5383 if (sorted_sym_hash
== NULL
)
5385 sym_hash
= sorted_sym_hash
;
5386 hpp
= elf_sym_hashes (abfd
);
5387 hppend
= hpp
+ extsymcount
;
5389 for (; hpp
< hppend
; hpp
++)
5393 && h
->root
.type
== bfd_link_hash_defined
5394 && !bed
->is_function_type (h
->type
))
5402 qsort (sorted_sym_hash
, sym_count
, sizeof (*sorted_sym_hash
),
5405 while (weaks
!= NULL
)
5407 struct elf_link_hash_entry
*hlook
;
5410 size_t i
, j
, idx
= 0;
5413 weaks
= hlook
->u
.alias
;
5414 hlook
->u
.alias
= NULL
;
5416 if (hlook
->root
.type
!= bfd_link_hash_defined
5417 && hlook
->root
.type
!= bfd_link_hash_defweak
)
5420 slook
= hlook
->root
.u
.def
.section
;
5421 vlook
= hlook
->root
.u
.def
.value
;
5427 bfd_signed_vma vdiff
;
5429 h
= sorted_sym_hash
[idx
];
5430 vdiff
= vlook
- h
->root
.u
.def
.value
;
5437 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5447 /* We didn't find a value/section match. */
5451 /* With multiple aliases, or when the weak symbol is already
5452 strongly defined, we have multiple matching symbols and
5453 the binary search above may land on any of them. Step
5454 one past the matching symbol(s). */
5457 h
= sorted_sym_hash
[idx
];
5458 if (h
->root
.u
.def
.section
!= slook
5459 || h
->root
.u
.def
.value
!= vlook
)
5463 /* Now look back over the aliases. Since we sorted by size
5464 as well as value and section, we'll choose the one with
5465 the largest size. */
5468 h
= sorted_sym_hash
[idx
];
5470 /* Stop if value or section doesn't match. */
5471 if (h
->root
.u
.def
.section
!= slook
5472 || h
->root
.u
.def
.value
!= vlook
)
5474 else if (h
!= hlook
)
5476 struct elf_link_hash_entry
*t
;
5479 hlook
->is_weakalias
= 1;
5481 if (t
->u
.alias
!= NULL
)
5482 while (t
->u
.alias
!= h
)
5486 /* If the weak definition is in the list of dynamic
5487 symbols, make sure the real definition is put
5489 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5491 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5494 free (sorted_sym_hash
);
5499 /* If the real definition is in the list of dynamic
5500 symbols, make sure the weak definition is put
5501 there as well. If we don't do this, then the
5502 dynamic loader might not merge the entries for the
5503 real definition and the weak definition. */
5504 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5506 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5507 goto err_free_sym_hash
;
5514 free (sorted_sym_hash
);
5517 if (bed
->check_directives
5518 && !(*bed
->check_directives
) (abfd
, info
))
5521 /* If this is a non-traditional link, try to optimize the handling
5522 of the .stab/.stabstr sections. */
5524 && ! info
->traditional_format
5525 && is_elf_hash_table (htab
)
5526 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5530 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5531 if (stabstr
!= NULL
)
5533 bfd_size_type string_offset
= 0;
5536 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5537 if (CONST_STRNEQ (stab
->name
, ".stab")
5538 && (!stab
->name
[5] ||
5539 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5540 && (stab
->flags
& SEC_MERGE
) == 0
5541 && !bfd_is_abs_section (stab
->output_section
))
5543 struct bfd_elf_section_data
*secdata
;
5545 secdata
= elf_section_data (stab
);
5546 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5547 stabstr
, &secdata
->sec_info
,
5550 if (secdata
->sec_info
)
5551 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5556 if (is_elf_hash_table (htab
) && add_needed
)
5558 /* Add this bfd to the loaded list. */
5559 struct elf_link_loaded_list
*n
;
5561 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5565 n
->next
= htab
->loaded
;
5572 if (old_tab
!= NULL
)
5574 if (old_strtab
!= NULL
)
5576 if (nondeflt_vers
!= NULL
)
5577 free (nondeflt_vers
);
5578 if (extversym
!= NULL
)
5581 if (isymbuf
!= NULL
)
5587 /* Return the linker hash table entry of a symbol that might be
5588 satisfied by an archive symbol. Return -1 on error. */
5590 struct elf_link_hash_entry
*
5591 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5592 struct bfd_link_info
*info
,
5595 struct elf_link_hash_entry
*h
;
5599 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, TRUE
);
5603 /* If this is a default version (the name contains @@), look up the
5604 symbol again with only one `@' as well as without the version.
5605 The effect is that references to the symbol with and without the
5606 version will be matched by the default symbol in the archive. */
5608 p
= strchr (name
, ELF_VER_CHR
);
5609 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5612 /* First check with only one `@'. */
5613 len
= strlen (name
);
5614 copy
= (char *) bfd_alloc (abfd
, len
);
5616 return (struct elf_link_hash_entry
*) -1;
5618 first
= p
- name
+ 1;
5619 memcpy (copy
, name
, first
);
5620 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5622 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, TRUE
);
5625 /* We also need to check references to the symbol without the
5627 copy
[first
- 1] = '\0';
5628 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
5629 FALSE
, FALSE
, TRUE
);
5632 bfd_release (abfd
, copy
);
5636 /* Add symbols from an ELF archive file to the linker hash table. We
5637 don't use _bfd_generic_link_add_archive_symbols because we need to
5638 handle versioned symbols.
5640 Fortunately, ELF archive handling is simpler than that done by
5641 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5642 oddities. In ELF, if we find a symbol in the archive map, and the
5643 symbol is currently undefined, we know that we must pull in that
5646 Unfortunately, we do have to make multiple passes over the symbol
5647 table until nothing further is resolved. */
5650 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5653 unsigned char *included
= NULL
;
5657 const struct elf_backend_data
*bed
;
5658 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
5659 (bfd
*, struct bfd_link_info
*, const char *);
5661 if (! bfd_has_map (abfd
))
5663 /* An empty archive is a special case. */
5664 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5666 bfd_set_error (bfd_error_no_armap
);
5670 /* Keep track of all symbols we know to be already defined, and all
5671 files we know to be already included. This is to speed up the
5672 second and subsequent passes. */
5673 c
= bfd_ardata (abfd
)->symdef_count
;
5677 amt
*= sizeof (*included
);
5678 included
= (unsigned char *) bfd_zmalloc (amt
);
5679 if (included
== NULL
)
5682 symdefs
= bfd_ardata (abfd
)->symdefs
;
5683 bed
= get_elf_backend_data (abfd
);
5684 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5697 symdefend
= symdef
+ c
;
5698 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5700 struct elf_link_hash_entry
*h
;
5702 struct bfd_link_hash_entry
*undefs_tail
;
5707 if (symdef
->file_offset
== last
)
5713 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5714 if (h
== (struct elf_link_hash_entry
*) -1)
5720 if (h
->root
.type
== bfd_link_hash_common
)
5722 /* We currently have a common symbol. The archive map contains
5723 a reference to this symbol, so we may want to include it. We
5724 only want to include it however, if this archive element
5725 contains a definition of the symbol, not just another common
5728 Unfortunately some archivers (including GNU ar) will put
5729 declarations of common symbols into their archive maps, as
5730 well as real definitions, so we cannot just go by the archive
5731 map alone. Instead we must read in the element's symbol
5732 table and check that to see what kind of symbol definition
5734 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5737 else if (h
->root
.type
!= bfd_link_hash_undefined
)
5739 if (h
->root
.type
!= bfd_link_hash_undefweak
)
5740 /* Symbol must be defined. Don't check it again. */
5745 /* We need to include this archive member. */
5746 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5747 if (element
== NULL
)
5750 if (! bfd_check_format (element
, bfd_object
))
5753 undefs_tail
= info
->hash
->undefs_tail
;
5755 if (!(*info
->callbacks
5756 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5758 if (!bfd_link_add_symbols (element
, info
))
5761 /* If there are any new undefined symbols, we need to make
5762 another pass through the archive in order to see whether
5763 they can be defined. FIXME: This isn't perfect, because
5764 common symbols wind up on undefs_tail and because an
5765 undefined symbol which is defined later on in this pass
5766 does not require another pass. This isn't a bug, but it
5767 does make the code less efficient than it could be. */
5768 if (undefs_tail
!= info
->hash
->undefs_tail
)
5771 /* Look backward to mark all symbols from this object file
5772 which we have already seen in this pass. */
5776 included
[mark
] = TRUE
;
5781 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5783 /* We mark subsequent symbols from this object file as we go
5784 on through the loop. */
5785 last
= symdef
->file_offset
;
5795 if (included
!= NULL
)
5800 /* Given an ELF BFD, add symbols to the global hash table as
5804 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5806 switch (bfd_get_format (abfd
))
5809 return elf_link_add_object_symbols (abfd
, info
);
5811 return elf_link_add_archive_symbols (abfd
, info
);
5813 bfd_set_error (bfd_error_wrong_format
);
5818 struct hash_codes_info
5820 unsigned long *hashcodes
;
5824 /* This function will be called though elf_link_hash_traverse to store
5825 all hash value of the exported symbols in an array. */
5828 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5830 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
5835 /* Ignore indirect symbols. These are added by the versioning code. */
5836 if (h
->dynindx
== -1)
5839 name
= h
->root
.root
.string
;
5840 if (h
->versioned
>= versioned
)
5842 char *p
= strchr (name
, ELF_VER_CHR
);
5845 alc
= (char *) bfd_malloc (p
- name
+ 1);
5851 memcpy (alc
, name
, p
- name
);
5852 alc
[p
- name
] = '\0';
5857 /* Compute the hash value. */
5858 ha
= bfd_elf_hash (name
);
5860 /* Store the found hash value in the array given as the argument. */
5861 *(inf
->hashcodes
)++ = ha
;
5863 /* And store it in the struct so that we can put it in the hash table
5865 h
->u
.elf_hash_value
= ha
;
5873 struct collect_gnu_hash_codes
5876 const struct elf_backend_data
*bed
;
5877 unsigned long int nsyms
;
5878 unsigned long int maskbits
;
5879 unsigned long int *hashcodes
;
5880 unsigned long int *hashval
;
5881 unsigned long int *indx
;
5882 unsigned long int *counts
;
5886 long int min_dynindx
;
5887 unsigned long int bucketcount
;
5888 unsigned long int symindx
;
5889 long int local_indx
;
5890 long int shift1
, shift2
;
5891 unsigned long int mask
;
5895 /* This function will be called though elf_link_hash_traverse to store
5896 all hash value of the exported symbols in an array. */
5899 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5901 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5906 /* Ignore indirect symbols. These are added by the versioning code. */
5907 if (h
->dynindx
== -1)
5910 /* Ignore also local symbols and undefined symbols. */
5911 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5914 name
= h
->root
.root
.string
;
5915 if (h
->versioned
>= versioned
)
5917 char *p
= strchr (name
, ELF_VER_CHR
);
5920 alc
= (char *) bfd_malloc (p
- name
+ 1);
5926 memcpy (alc
, name
, p
- name
);
5927 alc
[p
- name
] = '\0';
5932 /* Compute the hash value. */
5933 ha
= bfd_elf_gnu_hash (name
);
5935 /* Store the found hash value in the array for compute_bucket_count,
5936 and also for .dynsym reordering purposes. */
5937 s
->hashcodes
[s
->nsyms
] = ha
;
5938 s
->hashval
[h
->dynindx
] = ha
;
5940 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5941 s
->min_dynindx
= h
->dynindx
;
5949 /* This function will be called though elf_link_hash_traverse to do
5950 final dynamic symbol renumbering in case of .gnu.hash.
5951 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
5952 to the translation table. */
5955 elf_gnu_hash_process_symidx (struct elf_link_hash_entry
*h
, void *data
)
5957 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5958 unsigned long int bucket
;
5959 unsigned long int val
;
5961 /* Ignore indirect symbols. */
5962 if (h
->dynindx
== -1)
5965 /* Ignore also local symbols and undefined symbols. */
5966 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5968 if (h
->dynindx
>= s
->min_dynindx
)
5970 if (s
->bed
->record_xhash_symbol
!= NULL
)
5972 (*s
->bed
->record_xhash_symbol
) (h
, 0);
5976 h
->dynindx
= s
->local_indx
++;
5981 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5982 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5983 & ((s
->maskbits
>> s
->shift1
) - 1);
5984 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5986 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5987 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5988 if (s
->counts
[bucket
] == 1)
5989 /* Last element terminates the chain. */
5991 bfd_put_32 (s
->output_bfd
, val
,
5992 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5993 --s
->counts
[bucket
];
5994 if (s
->bed
->record_xhash_symbol
!= NULL
)
5996 bfd_vma xlat_loc
= s
->xlat
+ (s
->indx
[bucket
]++ - s
->symindx
) * 4;
5998 (*s
->bed
->record_xhash_symbol
) (h
, xlat_loc
);
6001 h
->dynindx
= s
->indx
[bucket
]++;
6005 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6008 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
6010 return !(h
->forced_local
6011 || h
->root
.type
== bfd_link_hash_undefined
6012 || h
->root
.type
== bfd_link_hash_undefweak
6013 || ((h
->root
.type
== bfd_link_hash_defined
6014 || h
->root
.type
== bfd_link_hash_defweak
)
6015 && h
->root
.u
.def
.section
->output_section
== NULL
));
6018 /* Array used to determine the number of hash table buckets to use
6019 based on the number of symbols there are. If there are fewer than
6020 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6021 fewer than 37 we use 17 buckets, and so forth. We never use more
6022 than 32771 buckets. */
6024 static const size_t elf_buckets
[] =
6026 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6030 /* Compute bucket count for hashing table. We do not use a static set
6031 of possible tables sizes anymore. Instead we determine for all
6032 possible reasonable sizes of the table the outcome (i.e., the
6033 number of collisions etc) and choose the best solution. The
6034 weighting functions are not too simple to allow the table to grow
6035 without bounds. Instead one of the weighting factors is the size.
6036 Therefore the result is always a good payoff between few collisions
6037 (= short chain lengths) and table size. */
6039 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6040 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
6041 unsigned long int nsyms
,
6044 size_t best_size
= 0;
6045 unsigned long int i
;
6047 /* We have a problem here. The following code to optimize the table
6048 size requires an integer type with more the 32 bits. If
6049 BFD_HOST_U_64_BIT is set we know about such a type. */
6050 #ifdef BFD_HOST_U_64_BIT
6055 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
6056 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
6057 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
6058 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
6059 unsigned long int *counts
;
6061 unsigned int no_improvement_count
= 0;
6063 /* Possible optimization parameters: if we have NSYMS symbols we say
6064 that the hashing table must at least have NSYMS/4 and at most
6066 minsize
= nsyms
/ 4;
6069 best_size
= maxsize
= nsyms
* 2;
6074 if ((best_size
& 31) == 0)
6078 /* Create array where we count the collisions in. We must use bfd_malloc
6079 since the size could be large. */
6081 amt
*= sizeof (unsigned long int);
6082 counts
= (unsigned long int *) bfd_malloc (amt
);
6086 /* Compute the "optimal" size for the hash table. The criteria is a
6087 minimal chain length. The minor criteria is (of course) the size
6089 for (i
= minsize
; i
< maxsize
; ++i
)
6091 /* Walk through the array of hashcodes and count the collisions. */
6092 BFD_HOST_U_64_BIT max
;
6093 unsigned long int j
;
6094 unsigned long int fact
;
6096 if (gnu_hash
&& (i
& 31) == 0)
6099 memset (counts
, '\0', i
* sizeof (unsigned long int));
6101 /* Determine how often each hash bucket is used. */
6102 for (j
= 0; j
< nsyms
; ++j
)
6103 ++counts
[hashcodes
[j
] % i
];
6105 /* For the weight function we need some information about the
6106 pagesize on the target. This is information need not be 100%
6107 accurate. Since this information is not available (so far) we
6108 define it here to a reasonable default value. If it is crucial
6109 to have a better value some day simply define this value. */
6110 # ifndef BFD_TARGET_PAGESIZE
6111 # define BFD_TARGET_PAGESIZE (4096)
6114 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6116 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
6119 /* Variant 1: optimize for short chains. We add the squares
6120 of all the chain lengths (which favors many small chain
6121 over a few long chains). */
6122 for (j
= 0; j
< i
; ++j
)
6123 max
+= counts
[j
] * counts
[j
];
6125 /* This adds penalties for the overall size of the table. */
6126 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6129 /* Variant 2: Optimize a lot more for small table. Here we
6130 also add squares of the size but we also add penalties for
6131 empty slots (the +1 term). */
6132 for (j
= 0; j
< i
; ++j
)
6133 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
6135 /* The overall size of the table is considered, but not as
6136 strong as in variant 1, where it is squared. */
6137 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6141 /* Compare with current best results. */
6142 if (max
< best_chlen
)
6146 no_improvement_count
= 0;
6148 /* PR 11843: Avoid futile long searches for the best bucket size
6149 when there are a large number of symbols. */
6150 else if (++no_improvement_count
== 100)
6157 #endif /* defined (BFD_HOST_U_64_BIT) */
6159 /* This is the fallback solution if no 64bit type is available or if we
6160 are not supposed to spend much time on optimizations. We select the
6161 bucket count using a fixed set of numbers. */
6162 for (i
= 0; elf_buckets
[i
] != 0; i
++)
6164 best_size
= elf_buckets
[i
];
6165 if (nsyms
< elf_buckets
[i
+ 1])
6168 if (gnu_hash
&& best_size
< 2)
6175 /* Size any SHT_GROUP section for ld -r. */
6178 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
6183 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6184 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6185 && (s
= ibfd
->sections
) != NULL
6186 && s
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
6187 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
6192 /* Set a default stack segment size. The value in INFO wins. If it
6193 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6194 undefined it is initialized. */
6197 bfd_elf_stack_segment_size (bfd
*output_bfd
,
6198 struct bfd_link_info
*info
,
6199 const char *legacy_symbol
,
6200 bfd_vma default_size
)
6202 struct elf_link_hash_entry
*h
= NULL
;
6204 /* Look for legacy symbol. */
6206 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
6207 FALSE
, FALSE
, FALSE
);
6208 if (h
&& (h
->root
.type
== bfd_link_hash_defined
6209 || h
->root
.type
== bfd_link_hash_defweak
)
6211 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
6213 /* The symbol has no type if specified on the command line. */
6214 h
->type
= STT_OBJECT
;
6215 if (info
->stacksize
)
6216 /* xgettext:c-format */
6217 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6218 output_bfd
, legacy_symbol
);
6219 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
6220 /* xgettext:c-format */
6221 _bfd_error_handler (_("%pB: %s not absolute"),
6222 output_bfd
, legacy_symbol
);
6224 info
->stacksize
= h
->root
.u
.def
.value
;
6227 if (!info
->stacksize
)
6228 /* If the user didn't set a size, or explicitly inhibit the
6229 size, set it now. */
6230 info
->stacksize
= default_size
;
6232 /* Provide the legacy symbol, if it is referenced. */
6233 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
6234 || h
->root
.type
== bfd_link_hash_undefweak
))
6236 struct bfd_link_hash_entry
*bh
= NULL
;
6238 if (!(_bfd_generic_link_add_one_symbol
6239 (info
, output_bfd
, legacy_symbol
,
6240 BSF_GLOBAL
, bfd_abs_section_ptr
,
6241 info
->stacksize
>= 0 ? info
->stacksize
: 0,
6242 NULL
, FALSE
, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
6245 h
= (struct elf_link_hash_entry
*) bh
;
6247 h
->type
= STT_OBJECT
;
6253 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6255 struct elf_gc_sweep_symbol_info
6257 struct bfd_link_info
*info
;
6258 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
6263 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
6266 && (((h
->root
.type
== bfd_link_hash_defined
6267 || h
->root
.type
== bfd_link_hash_defweak
)
6268 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
6269 && h
->root
.u
.def
.section
->gc_mark
))
6270 || h
->root
.type
== bfd_link_hash_undefined
6271 || h
->root
.type
== bfd_link_hash_undefweak
))
6273 struct elf_gc_sweep_symbol_info
*inf
;
6275 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
6276 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
6279 h
->ref_regular_nonweak
= 0;
6285 /* Set up the sizes and contents of the ELF dynamic sections. This is
6286 called by the ELF linker emulation before_allocation routine. We
6287 must set the sizes of the sections before the linker sets the
6288 addresses of the various sections. */
6291 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
6294 const char *filter_shlib
,
6296 const char *depaudit
,
6297 const char * const *auxiliary_filters
,
6298 struct bfd_link_info
*info
,
6299 asection
**sinterpptr
)
6302 const struct elf_backend_data
*bed
;
6306 if (!is_elf_hash_table (info
->hash
))
6309 dynobj
= elf_hash_table (info
)->dynobj
;
6311 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6313 struct bfd_elf_version_tree
*verdefs
;
6314 struct elf_info_failed asvinfo
;
6315 struct bfd_elf_version_tree
*t
;
6316 struct bfd_elf_version_expr
*d
;
6320 /* If we are supposed to export all symbols into the dynamic symbol
6321 table (this is not the normal case), then do so. */
6322 if (info
->export_dynamic
6323 || (bfd_link_executable (info
) && info
->dynamic
))
6325 struct elf_info_failed eif
;
6329 elf_link_hash_traverse (elf_hash_table (info
),
6330 _bfd_elf_export_symbol
,
6338 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6340 if (soname_indx
== (size_t) -1
6341 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6345 soname_indx
= (size_t) -1;
6347 /* Make all global versions with definition. */
6348 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6349 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6350 if (!d
->symver
&& d
->literal
)
6352 const char *verstr
, *name
;
6353 size_t namelen
, verlen
, newlen
;
6354 char *newname
, *p
, leading_char
;
6355 struct elf_link_hash_entry
*newh
;
6357 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6359 namelen
= strlen (name
) + (leading_char
!= '\0');
6361 verlen
= strlen (verstr
);
6362 newlen
= namelen
+ verlen
+ 3;
6364 newname
= (char *) bfd_malloc (newlen
);
6365 if (newname
== NULL
)
6367 newname
[0] = leading_char
;
6368 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6370 /* Check the hidden versioned definition. */
6371 p
= newname
+ namelen
;
6373 memcpy (p
, verstr
, verlen
+ 1);
6374 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6375 newname
, FALSE
, FALSE
,
6378 || (newh
->root
.type
!= bfd_link_hash_defined
6379 && newh
->root
.type
!= bfd_link_hash_defweak
))
6381 /* Check the default versioned definition. */
6383 memcpy (p
, verstr
, verlen
+ 1);
6384 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6385 newname
, FALSE
, FALSE
,
6390 /* Mark this version if there is a definition and it is
6391 not defined in a shared object. */
6393 && !newh
->def_dynamic
6394 && (newh
->root
.type
== bfd_link_hash_defined
6395 || newh
->root
.type
== bfd_link_hash_defweak
))
6399 /* Attach all the symbols to their version information. */
6400 asvinfo
.info
= info
;
6401 asvinfo
.failed
= FALSE
;
6403 elf_link_hash_traverse (elf_hash_table (info
),
6404 _bfd_elf_link_assign_sym_version
,
6409 if (!info
->allow_undefined_version
)
6411 /* Check if all global versions have a definition. */
6412 bfd_boolean all_defined
= TRUE
;
6413 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6414 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6415 if (d
->literal
&& !d
->symver
&& !d
->script
)
6418 (_("%s: undefined version: %s"),
6419 d
->pattern
, t
->name
);
6420 all_defined
= FALSE
;
6425 bfd_set_error (bfd_error_bad_value
);
6430 /* Set up the version definition section. */
6431 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6432 BFD_ASSERT (s
!= NULL
);
6434 /* We may have created additional version definitions if we are
6435 just linking a regular application. */
6436 verdefs
= info
->version_info
;
6438 /* Skip anonymous version tag. */
6439 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6440 verdefs
= verdefs
->next
;
6442 if (verdefs
== NULL
&& !info
->create_default_symver
)
6443 s
->flags
|= SEC_EXCLUDE
;
6449 Elf_Internal_Verdef def
;
6450 Elf_Internal_Verdaux defaux
;
6451 struct bfd_link_hash_entry
*bh
;
6452 struct elf_link_hash_entry
*h
;
6458 /* Make space for the base version. */
6459 size
+= sizeof (Elf_External_Verdef
);
6460 size
+= sizeof (Elf_External_Verdaux
);
6463 /* Make space for the default version. */
6464 if (info
->create_default_symver
)
6466 size
+= sizeof (Elf_External_Verdef
);
6470 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6472 struct bfd_elf_version_deps
*n
;
6474 /* Don't emit base version twice. */
6478 size
+= sizeof (Elf_External_Verdef
);
6479 size
+= sizeof (Elf_External_Verdaux
);
6482 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6483 size
+= sizeof (Elf_External_Verdaux
);
6487 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6488 if (s
->contents
== NULL
&& s
->size
!= 0)
6491 /* Fill in the version definition section. */
6495 def
.vd_version
= VER_DEF_CURRENT
;
6496 def
.vd_flags
= VER_FLG_BASE
;
6499 if (info
->create_default_symver
)
6501 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6502 def
.vd_next
= sizeof (Elf_External_Verdef
);
6506 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6507 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6508 + sizeof (Elf_External_Verdaux
));
6511 if (soname_indx
!= (size_t) -1)
6513 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6515 def
.vd_hash
= bfd_elf_hash (soname
);
6516 defaux
.vda_name
= soname_indx
;
6523 name
= lbasename (output_bfd
->filename
);
6524 def
.vd_hash
= bfd_elf_hash (name
);
6525 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6527 if (indx
== (size_t) -1)
6529 defaux
.vda_name
= indx
;
6531 defaux
.vda_next
= 0;
6533 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6534 (Elf_External_Verdef
*) p
);
6535 p
+= sizeof (Elf_External_Verdef
);
6536 if (info
->create_default_symver
)
6538 /* Add a symbol representing this version. */
6540 if (! (_bfd_generic_link_add_one_symbol
6541 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6543 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6545 h
= (struct elf_link_hash_entry
*) bh
;
6548 h
->type
= STT_OBJECT
;
6549 h
->verinfo
.vertree
= NULL
;
6551 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6554 /* Create a duplicate of the base version with the same
6555 aux block, but different flags. */
6558 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6560 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6561 + sizeof (Elf_External_Verdaux
));
6564 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6565 (Elf_External_Verdef
*) p
);
6566 p
+= sizeof (Elf_External_Verdef
);
6568 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6569 (Elf_External_Verdaux
*) p
);
6570 p
+= sizeof (Elf_External_Verdaux
);
6572 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6575 struct bfd_elf_version_deps
*n
;
6577 /* Don't emit the base version twice. */
6582 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6585 /* Add a symbol representing this version. */
6587 if (! (_bfd_generic_link_add_one_symbol
6588 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6590 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6592 h
= (struct elf_link_hash_entry
*) bh
;
6595 h
->type
= STT_OBJECT
;
6596 h
->verinfo
.vertree
= t
;
6598 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6601 def
.vd_version
= VER_DEF_CURRENT
;
6603 if (t
->globals
.list
== NULL
6604 && t
->locals
.list
== NULL
6606 def
.vd_flags
|= VER_FLG_WEAK
;
6607 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6608 def
.vd_cnt
= cdeps
+ 1;
6609 def
.vd_hash
= bfd_elf_hash (t
->name
);
6610 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6613 /* If a basever node is next, it *must* be the last node in
6614 the chain, otherwise Verdef construction breaks. */
6615 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6616 BFD_ASSERT (t
->next
->next
== NULL
);
6618 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6619 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6620 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6622 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6623 (Elf_External_Verdef
*) p
);
6624 p
+= sizeof (Elf_External_Verdef
);
6626 defaux
.vda_name
= h
->dynstr_index
;
6627 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6629 defaux
.vda_next
= 0;
6630 if (t
->deps
!= NULL
)
6631 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6632 t
->name_indx
= defaux
.vda_name
;
6634 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6635 (Elf_External_Verdaux
*) p
);
6636 p
+= sizeof (Elf_External_Verdaux
);
6638 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6640 if (n
->version_needed
== NULL
)
6642 /* This can happen if there was an error in the
6644 defaux
.vda_name
= 0;
6648 defaux
.vda_name
= n
->version_needed
->name_indx
;
6649 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6652 if (n
->next
== NULL
)
6653 defaux
.vda_next
= 0;
6655 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6657 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6658 (Elf_External_Verdaux
*) p
);
6659 p
+= sizeof (Elf_External_Verdaux
);
6663 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6667 bed
= get_elf_backend_data (output_bfd
);
6669 if (info
->gc_sections
&& bed
->can_gc_sections
)
6671 struct elf_gc_sweep_symbol_info sweep_info
;
6673 /* Remove the symbols that were in the swept sections from the
6674 dynamic symbol table. */
6675 sweep_info
.info
= info
;
6676 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
6677 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
6681 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6684 struct elf_find_verdep_info sinfo
;
6686 /* Work out the size of the version reference section. */
6688 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6689 BFD_ASSERT (s
!= NULL
);
6692 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6693 if (sinfo
.vers
== 0)
6695 sinfo
.failed
= FALSE
;
6697 elf_link_hash_traverse (elf_hash_table (info
),
6698 _bfd_elf_link_find_version_dependencies
,
6703 if (elf_tdata (output_bfd
)->verref
== NULL
)
6704 s
->flags
|= SEC_EXCLUDE
;
6707 Elf_Internal_Verneed
*vn
;
6712 /* Build the version dependency section. */
6715 for (vn
= elf_tdata (output_bfd
)->verref
;
6717 vn
= vn
->vn_nextref
)
6719 Elf_Internal_Vernaux
*a
;
6721 size
+= sizeof (Elf_External_Verneed
);
6723 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6724 size
+= sizeof (Elf_External_Vernaux
);
6728 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6729 if (s
->contents
== NULL
)
6733 for (vn
= elf_tdata (output_bfd
)->verref
;
6735 vn
= vn
->vn_nextref
)
6738 Elf_Internal_Vernaux
*a
;
6742 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6745 vn
->vn_version
= VER_NEED_CURRENT
;
6747 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6748 elf_dt_name (vn
->vn_bfd
) != NULL
6749 ? elf_dt_name (vn
->vn_bfd
)
6750 : lbasename (vn
->vn_bfd
->filename
),
6752 if (indx
== (size_t) -1)
6755 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
6756 if (vn
->vn_nextref
== NULL
)
6759 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
6760 + caux
* sizeof (Elf_External_Vernaux
));
6762 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
6763 (Elf_External_Verneed
*) p
);
6764 p
+= sizeof (Elf_External_Verneed
);
6766 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6768 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6769 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6770 a
->vna_nodename
, FALSE
);
6771 if (indx
== (size_t) -1)
6774 if (a
->vna_nextptr
== NULL
)
6777 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6779 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6780 (Elf_External_Vernaux
*) p
);
6781 p
+= sizeof (Elf_External_Vernaux
);
6785 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6789 /* Any syms created from now on start with -1 in
6790 got.refcount/offset and plt.refcount/offset. */
6791 elf_hash_table (info
)->init_got_refcount
6792 = elf_hash_table (info
)->init_got_offset
;
6793 elf_hash_table (info
)->init_plt_refcount
6794 = elf_hash_table (info
)->init_plt_offset
;
6796 if (bfd_link_relocatable (info
)
6797 && !_bfd_elf_size_group_sections (info
))
6800 /* The backend may have to create some sections regardless of whether
6801 we're dynamic or not. */
6802 if (bed
->elf_backend_always_size_sections
6803 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
6806 /* Determine any GNU_STACK segment requirements, after the backend
6807 has had a chance to set a default segment size. */
6808 if (info
->execstack
)
6809 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
6810 else if (info
->noexecstack
)
6811 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
6815 asection
*notesec
= NULL
;
6818 for (inputobj
= info
->input_bfds
;
6820 inputobj
= inputobj
->link
.next
)
6825 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
6827 s
= inputobj
->sections
;
6828 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
6831 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
6834 if (s
->flags
& SEC_CODE
)
6838 else if (bed
->default_execstack
)
6841 if (notesec
|| info
->stacksize
> 0)
6842 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
6843 if (notesec
&& exec
&& bfd_link_relocatable (info
)
6844 && notesec
->output_section
!= bfd_abs_section_ptr
)
6845 notesec
->output_section
->flags
|= SEC_CODE
;
6848 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6850 struct elf_info_failed eif
;
6851 struct elf_link_hash_entry
*h
;
6855 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
6856 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
6860 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
6862 info
->flags
|= DF_SYMBOLIC
;
6870 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
6872 if (indx
== (size_t) -1)
6875 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
6876 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
6880 if (filter_shlib
!= NULL
)
6884 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6885 filter_shlib
, TRUE
);
6886 if (indx
== (size_t) -1
6887 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
6891 if (auxiliary_filters
!= NULL
)
6893 const char * const *p
;
6895 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
6899 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6901 if (indx
== (size_t) -1
6902 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
6911 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
6913 if (indx
== (size_t) -1
6914 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
6918 if (depaudit
!= NULL
)
6922 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
6924 if (indx
== (size_t) -1
6925 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
6932 /* Find all symbols which were defined in a dynamic object and make
6933 the backend pick a reasonable value for them. */
6934 elf_link_hash_traverse (elf_hash_table (info
),
6935 _bfd_elf_adjust_dynamic_symbol
,
6940 /* Add some entries to the .dynamic section. We fill in some of the
6941 values later, in bfd_elf_final_link, but we must add the entries
6942 now so that we know the final size of the .dynamic section. */
6944 /* If there are initialization and/or finalization functions to
6945 call then add the corresponding DT_INIT/DT_FINI entries. */
6946 h
= (info
->init_function
6947 ? elf_link_hash_lookup (elf_hash_table (info
),
6948 info
->init_function
, FALSE
,
6955 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
6958 h
= (info
->fini_function
6959 ? elf_link_hash_lookup (elf_hash_table (info
),
6960 info
->fini_function
, FALSE
,
6967 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
6971 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
6972 if (s
!= NULL
&& s
->linker_has_input
)
6974 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6975 if (! bfd_link_executable (info
))
6980 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
6981 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
6982 && (o
= sub
->sections
) != NULL
6983 && o
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
)
6984 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
6985 if (elf_section_data (o
)->this_hdr
.sh_type
6986 == SHT_PREINIT_ARRAY
)
6989 (_("%pB: .preinit_array section is not allowed in DSO"),
6994 bfd_set_error (bfd_error_nonrepresentable_section
);
6998 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
6999 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
7002 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
7003 if (s
!= NULL
&& s
->linker_has_input
)
7005 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
7006 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
7009 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
7010 if (s
!= NULL
&& s
->linker_has_input
)
7012 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
7013 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
7017 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
7018 /* If .dynstr is excluded from the link, we don't want any of
7019 these tags. Strictly, we should be checking each section
7020 individually; This quick check covers for the case where
7021 someone does a /DISCARD/ : { *(*) }. */
7022 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
7024 bfd_size_type strsize
;
7026 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7027 if ((info
->emit_hash
7028 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
7029 || (info
->emit_gnu_hash
7030 && (bed
->record_xhash_symbol
== NULL
7031 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0)))
7032 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
7033 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
7034 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
7035 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
7036 bed
->s
->sizeof_sym
))
7041 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
7044 /* The backend must work out the sizes of all the other dynamic
7047 && bed
->elf_backend_size_dynamic_sections
!= NULL
7048 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
7051 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7053 if (elf_tdata (output_bfd
)->cverdefs
)
7055 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
7057 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
7058 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
7062 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
7064 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
7067 else if (info
->flags
& DF_BIND_NOW
)
7069 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
7075 if (bfd_link_executable (info
))
7076 info
->flags_1
&= ~ (DF_1_INITFIRST
7079 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
7083 if (elf_tdata (output_bfd
)->cverrefs
)
7085 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
7087 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
7088 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
7092 if ((elf_tdata (output_bfd
)->cverrefs
== 0
7093 && elf_tdata (output_bfd
)->cverdefs
== 0)
7094 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, NULL
) <= 1)
7098 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7099 s
->flags
|= SEC_EXCLUDE
;
7105 /* Find the first non-excluded output section. We'll use its
7106 section symbol for some emitted relocs. */
7108 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
7111 asection
*found
= NULL
;
7113 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7114 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7115 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7118 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7121 elf_hash_table (info
)->text_index_section
= found
;
7124 /* Find two non-excluded output sections, one for code, one for data.
7125 We'll use their section symbols for some emitted relocs. */
7127 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
7130 asection
*found
= NULL
;
7132 /* Data first, since setting text_index_section changes
7133 _bfd_elf_omit_section_dynsym_default. */
7134 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7135 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7136 && !(s
->flags
& SEC_READONLY
)
7137 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7140 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7143 elf_hash_table (info
)->data_index_section
= found
;
7145 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7146 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7147 && (s
->flags
& SEC_READONLY
)
7148 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7153 elf_hash_table (info
)->text_index_section
= found
;
7156 #define GNU_HASH_SECTION_NAME(bed) \
7157 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7160 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
7162 const struct elf_backend_data
*bed
;
7163 unsigned long section_sym_count
;
7164 bfd_size_type dynsymcount
= 0;
7166 if (!is_elf_hash_table (info
->hash
))
7169 bed
= get_elf_backend_data (output_bfd
);
7170 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
7172 /* Assign dynsym indices. In a shared library we generate a section
7173 symbol for each output section, which come first. Next come all
7174 of the back-end allocated local dynamic syms, followed by the rest
7175 of the global symbols.
7177 This is usually not needed for static binaries, however backends
7178 can request to always do it, e.g. the MIPS backend uses dynamic
7179 symbol counts to lay out GOT, which will be produced in the
7180 presence of GOT relocations even in static binaries (holding fixed
7181 data in that case, to satisfy those relocations). */
7183 if (elf_hash_table (info
)->dynamic_sections_created
7184 || bed
->always_renumber_dynsyms
)
7185 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
7186 §ion_sym_count
);
7188 if (elf_hash_table (info
)->dynamic_sections_created
)
7192 unsigned int dtagcount
;
7194 dynobj
= elf_hash_table (info
)->dynobj
;
7196 /* Work out the size of the symbol version section. */
7197 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7198 BFD_ASSERT (s
!= NULL
);
7199 if ((s
->flags
& SEC_EXCLUDE
) == 0)
7201 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
7202 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7203 if (s
->contents
== NULL
)
7206 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
7210 /* Set the size of the .dynsym and .hash sections. We counted
7211 the number of dynamic symbols in elf_link_add_object_symbols.
7212 We will build the contents of .dynsym and .hash when we build
7213 the final symbol table, because until then we do not know the
7214 correct value to give the symbols. We built the .dynstr
7215 section as we went along in elf_link_add_object_symbols. */
7216 s
= elf_hash_table (info
)->dynsym
;
7217 BFD_ASSERT (s
!= NULL
);
7218 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
7220 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7221 if (s
->contents
== NULL
)
7224 /* The first entry in .dynsym is a dummy symbol. Clear all the
7225 section syms, in case we don't output them all. */
7226 ++section_sym_count
;
7227 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
7229 elf_hash_table (info
)->bucketcount
= 0;
7231 /* Compute the size of the hashing table. As a side effect this
7232 computes the hash values for all the names we export. */
7233 if (info
->emit_hash
)
7235 unsigned long int *hashcodes
;
7236 struct hash_codes_info hashinf
;
7238 unsigned long int nsyms
;
7240 size_t hash_entry_size
;
7242 /* Compute the hash values for all exported symbols. At the same
7243 time store the values in an array so that we could use them for
7245 amt
= dynsymcount
* sizeof (unsigned long int);
7246 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
7247 if (hashcodes
== NULL
)
7249 hashinf
.hashcodes
= hashcodes
;
7250 hashinf
.error
= FALSE
;
7252 /* Put all hash values in HASHCODES. */
7253 elf_link_hash_traverse (elf_hash_table (info
),
7254 elf_collect_hash_codes
, &hashinf
);
7261 nsyms
= hashinf
.hashcodes
- hashcodes
;
7263 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
7266 if (bucketcount
== 0 && nsyms
> 0)
7269 elf_hash_table (info
)->bucketcount
= bucketcount
;
7271 s
= bfd_get_linker_section (dynobj
, ".hash");
7272 BFD_ASSERT (s
!= NULL
);
7273 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
7274 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
7275 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7276 if (s
->contents
== NULL
)
7279 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
7280 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
7281 s
->contents
+ hash_entry_size
);
7284 if (info
->emit_gnu_hash
)
7287 unsigned char *contents
;
7288 struct collect_gnu_hash_codes cinfo
;
7292 memset (&cinfo
, 0, sizeof (cinfo
));
7294 /* Compute the hash values for all exported symbols. At the same
7295 time store the values in an array so that we could use them for
7297 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
7298 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
7299 if (cinfo
.hashcodes
== NULL
)
7302 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
7303 cinfo
.min_dynindx
= -1;
7304 cinfo
.output_bfd
= output_bfd
;
7307 /* Put all hash values in HASHCODES. */
7308 elf_link_hash_traverse (elf_hash_table (info
),
7309 elf_collect_gnu_hash_codes
, &cinfo
);
7312 free (cinfo
.hashcodes
);
7317 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
7319 if (bucketcount
== 0)
7321 free (cinfo
.hashcodes
);
7325 s
= bfd_get_linker_section (dynobj
, GNU_HASH_SECTION_NAME (bed
));
7326 BFD_ASSERT (s
!= NULL
);
7328 if (cinfo
.nsyms
== 0)
7330 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7331 BFD_ASSERT (cinfo
.min_dynindx
== -1);
7332 free (cinfo
.hashcodes
);
7333 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
7334 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7335 if (contents
== NULL
)
7337 s
->contents
= contents
;
7338 /* 1 empty bucket. */
7339 bfd_put_32 (output_bfd
, 1, contents
);
7340 /* SYMIDX above the special symbol 0. */
7341 bfd_put_32 (output_bfd
, 1, contents
+ 4);
7342 /* Just one word for bitmask. */
7343 bfd_put_32 (output_bfd
, 1, contents
+ 8);
7344 /* Only hash fn bloom filter. */
7345 bfd_put_32 (output_bfd
, 0, contents
+ 12);
7346 /* No hashes are valid - empty bitmask. */
7347 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
7348 /* No hashes in the only bucket. */
7349 bfd_put_32 (output_bfd
, 0,
7350 contents
+ 16 + bed
->s
->arch_size
/ 8);
7354 unsigned long int maskwords
, maskbitslog2
, x
;
7355 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
7359 while ((x
>>= 1) != 0)
7361 if (maskbitslog2
< 3)
7363 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
7364 maskbitslog2
= maskbitslog2
+ 3;
7366 maskbitslog2
= maskbitslog2
+ 2;
7367 if (bed
->s
->arch_size
== 64)
7369 if (maskbitslog2
== 5)
7375 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
7376 cinfo
.shift2
= maskbitslog2
;
7377 cinfo
.maskbits
= 1 << maskbitslog2
;
7378 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
7379 amt
= bucketcount
* sizeof (unsigned long int) * 2;
7380 amt
+= maskwords
* sizeof (bfd_vma
);
7381 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
7382 if (cinfo
.bitmask
== NULL
)
7384 free (cinfo
.hashcodes
);
7388 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
7389 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
7390 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
7391 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
7393 /* Determine how often each hash bucket is used. */
7394 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
7395 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
7396 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
7398 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
7399 if (cinfo
.counts
[i
] != 0)
7401 cinfo
.indx
[i
] = cnt
;
7402 cnt
+= cinfo
.counts
[i
];
7404 BFD_ASSERT (cnt
== dynsymcount
);
7405 cinfo
.bucketcount
= bucketcount
;
7406 cinfo
.local_indx
= cinfo
.min_dynindx
;
7408 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
7409 s
->size
+= cinfo
.maskbits
/ 8;
7410 if (bed
->record_xhash_symbol
!= NULL
)
7411 s
->size
+= cinfo
.nsyms
* 4;
7412 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7413 if (contents
== NULL
)
7415 free (cinfo
.bitmask
);
7416 free (cinfo
.hashcodes
);
7420 s
->contents
= contents
;
7421 bfd_put_32 (output_bfd
, bucketcount
, contents
);
7422 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
7423 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
7424 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
7425 contents
+= 16 + cinfo
.maskbits
/ 8;
7427 for (i
= 0; i
< bucketcount
; ++i
)
7429 if (cinfo
.counts
[i
] == 0)
7430 bfd_put_32 (output_bfd
, 0, contents
);
7432 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
7436 cinfo
.contents
= contents
;
7438 cinfo
.xlat
= contents
+ cinfo
.nsyms
* 4 - s
->contents
;
7439 /* Renumber dynamic symbols, if populating .gnu.hash section.
7440 If using .MIPS.xhash, populate the translation table. */
7441 elf_link_hash_traverse (elf_hash_table (info
),
7442 elf_gnu_hash_process_symidx
, &cinfo
);
7444 contents
= s
->contents
+ 16;
7445 for (i
= 0; i
< maskwords
; ++i
)
7447 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
7449 contents
+= bed
->s
->arch_size
/ 8;
7452 free (cinfo
.bitmask
);
7453 free (cinfo
.hashcodes
);
7457 s
= bfd_get_linker_section (dynobj
, ".dynstr");
7458 BFD_ASSERT (s
!= NULL
);
7460 elf_finalize_dynstr (output_bfd
, info
);
7462 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7464 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
7465 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
7472 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7475 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
7478 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
7479 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
7482 /* Finish SHF_MERGE section merging. */
7485 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
7490 if (!is_elf_hash_table (info
->hash
))
7493 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
7494 if ((ibfd
->flags
& DYNAMIC
) == 0
7495 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
7496 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
7497 == get_elf_backend_data (obfd
)->s
->elfclass
))
7498 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7499 if ((sec
->flags
& SEC_MERGE
) != 0
7500 && !bfd_is_abs_section (sec
->output_section
))
7502 struct bfd_elf_section_data
*secdata
;
7504 secdata
= elf_section_data (sec
);
7505 if (! _bfd_add_merge_section (obfd
,
7506 &elf_hash_table (info
)->merge_info
,
7507 sec
, &secdata
->sec_info
))
7509 else if (secdata
->sec_info
)
7510 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7513 if (elf_hash_table (info
)->merge_info
!= NULL
)
7514 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7515 merge_sections_remove_hook
);
7519 /* Create an entry in an ELF linker hash table. */
7521 struct bfd_hash_entry
*
7522 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7523 struct bfd_hash_table
*table
,
7526 /* Allocate the structure if it has not already been allocated by a
7530 entry
= (struct bfd_hash_entry
*)
7531 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7536 /* Call the allocation method of the superclass. */
7537 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7540 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7541 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7543 /* Set local fields. */
7546 ret
->got
= htab
->init_got_refcount
;
7547 ret
->plt
= htab
->init_plt_refcount
;
7548 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7549 - offsetof (struct elf_link_hash_entry
, size
)));
7550 /* Assume that we have been called by a non-ELF symbol reader.
7551 This flag is then reset by the code which reads an ELF input
7552 file. This ensures that a symbol created by a non-ELF symbol
7553 reader will have the flag set correctly. */
7560 /* Copy data from an indirect symbol to its direct symbol, hiding the
7561 old indirect symbol. Also used for copying flags to a weakdef. */
7564 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7565 struct elf_link_hash_entry
*dir
,
7566 struct elf_link_hash_entry
*ind
)
7568 struct elf_link_hash_table
*htab
;
7570 /* Copy down any references that we may have already seen to the
7571 symbol which just became indirect. */
7573 if (dir
->versioned
!= versioned_hidden
)
7574 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7575 dir
->ref_regular
|= ind
->ref_regular
;
7576 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7577 dir
->non_got_ref
|= ind
->non_got_ref
;
7578 dir
->needs_plt
|= ind
->needs_plt
;
7579 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7581 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7584 /* Copy over the global and procedure linkage table refcount entries.
7585 These may have been already set up by a check_relocs routine. */
7586 htab
= elf_hash_table (info
);
7587 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7589 if (dir
->got
.refcount
< 0)
7590 dir
->got
.refcount
= 0;
7591 dir
->got
.refcount
+= ind
->got
.refcount
;
7592 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7595 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7597 if (dir
->plt
.refcount
< 0)
7598 dir
->plt
.refcount
= 0;
7599 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7600 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7603 if (ind
->dynindx
!= -1)
7605 if (dir
->dynindx
!= -1)
7606 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7607 dir
->dynindx
= ind
->dynindx
;
7608 dir
->dynstr_index
= ind
->dynstr_index
;
7610 ind
->dynstr_index
= 0;
7615 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7616 struct elf_link_hash_entry
*h
,
7617 bfd_boolean force_local
)
7619 /* STT_GNU_IFUNC symbol must go through PLT. */
7620 if (h
->type
!= STT_GNU_IFUNC
)
7622 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7627 h
->forced_local
= 1;
7628 if (h
->dynindx
!= -1)
7630 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7633 h
->dynstr_index
= 0;
7638 /* Hide a symbol. */
7641 _bfd_elf_link_hide_symbol (bfd
*output_bfd
,
7642 struct bfd_link_info
*info
,
7643 struct bfd_link_hash_entry
*h
)
7645 if (is_elf_hash_table (info
->hash
))
7647 const struct elf_backend_data
*bed
7648 = get_elf_backend_data (output_bfd
);
7649 struct elf_link_hash_entry
*eh
7650 = (struct elf_link_hash_entry
*) h
;
7651 bed
->elf_backend_hide_symbol (info
, eh
, TRUE
);
7652 eh
->def_dynamic
= 0;
7653 eh
->ref_dynamic
= 0;
7654 eh
->dynamic_def
= 0;
7658 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7662 _bfd_elf_link_hash_table_init
7663 (struct elf_link_hash_table
*table
,
7665 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
7666 struct bfd_hash_table
*,
7668 unsigned int entsize
,
7669 enum elf_target_id target_id
)
7672 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
7674 table
->init_got_refcount
.refcount
= can_refcount
- 1;
7675 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
7676 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
7677 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
7678 /* The first dynamic symbol is a dummy. */
7679 table
->dynsymcount
= 1;
7681 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
7683 table
->root
.type
= bfd_link_elf_hash_table
;
7684 table
->hash_table_id
= target_id
;
7689 /* Create an ELF linker hash table. */
7691 struct bfd_link_hash_table
*
7692 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7694 struct elf_link_hash_table
*ret
;
7695 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
7697 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7701 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7702 sizeof (struct elf_link_hash_entry
),
7708 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7713 /* Destroy an ELF linker hash table. */
7716 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7718 struct elf_link_hash_table
*htab
;
7720 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7721 if (htab
->dynstr
!= NULL
)
7722 _bfd_elf_strtab_free (htab
->dynstr
);
7723 _bfd_merge_sections_free (htab
->merge_info
);
7724 _bfd_generic_link_hash_table_free (obfd
);
7727 /* This is a hook for the ELF emulation code in the generic linker to
7728 tell the backend linker what file name to use for the DT_NEEDED
7729 entry for a dynamic object. */
7732 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7734 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7735 && bfd_get_format (abfd
) == bfd_object
)
7736 elf_dt_name (abfd
) = name
;
7740 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
7743 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7744 && bfd_get_format (abfd
) == bfd_object
)
7745 lib_class
= elf_dyn_lib_class (abfd
);
7752 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
7754 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7755 && bfd_get_format (abfd
) == bfd_object
)
7756 elf_dyn_lib_class (abfd
) = lib_class
;
7759 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7760 the linker ELF emulation code. */
7762 struct bfd_link_needed_list
*
7763 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7764 struct bfd_link_info
*info
)
7766 if (! is_elf_hash_table (info
->hash
))
7768 return elf_hash_table (info
)->needed
;
7771 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7772 hook for the linker ELF emulation code. */
7774 struct bfd_link_needed_list
*
7775 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7776 struct bfd_link_info
*info
)
7778 if (! is_elf_hash_table (info
->hash
))
7780 return elf_hash_table (info
)->runpath
;
7783 /* Get the name actually used for a dynamic object for a link. This
7784 is the SONAME entry if there is one. Otherwise, it is the string
7785 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7788 bfd_elf_get_dt_soname (bfd
*abfd
)
7790 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7791 && bfd_get_format (abfd
) == bfd_object
)
7792 return elf_dt_name (abfd
);
7796 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7797 the ELF linker emulation code. */
7800 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
7801 struct bfd_link_needed_list
**pneeded
)
7804 bfd_byte
*dynbuf
= NULL
;
7805 unsigned int elfsec
;
7806 unsigned long shlink
;
7807 bfd_byte
*extdyn
, *extdynend
;
7809 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
7813 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
7814 || bfd_get_format (abfd
) != bfd_object
)
7817 s
= bfd_get_section_by_name (abfd
, ".dynamic");
7818 if (s
== NULL
|| s
->size
== 0)
7821 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
7824 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
7825 if (elfsec
== SHN_BAD
)
7828 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
7830 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
7831 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
7834 extdynend
= extdyn
+ s
->size
;
7835 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
7837 Elf_Internal_Dyn dyn
;
7839 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
7841 if (dyn
.d_tag
== DT_NULL
)
7844 if (dyn
.d_tag
== DT_NEEDED
)
7847 struct bfd_link_needed_list
*l
;
7848 unsigned int tagv
= dyn
.d_un
.d_val
;
7851 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
7856 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
7877 struct elf_symbuf_symbol
7879 unsigned long st_name
; /* Symbol name, index in string tbl */
7880 unsigned char st_info
; /* Type and binding attributes */
7881 unsigned char st_other
; /* Visibilty, and target specific */
7884 struct elf_symbuf_head
7886 struct elf_symbuf_symbol
*ssym
;
7888 unsigned int st_shndx
;
7895 Elf_Internal_Sym
*isym
;
7896 struct elf_symbuf_symbol
*ssym
;
7902 /* Sort references to symbols by ascending section number. */
7905 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
7907 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
7908 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
7910 if (s1
->st_shndx
!= s2
->st_shndx
)
7911 return s1
->st_shndx
> s2
->st_shndx
? 1 : -1;
7912 /* Final sort by the address of the sym in the symbuf ensures
7915 return s1
> s2
? 1 : -1;
7920 elf_sym_name_compare (const void *arg1
, const void *arg2
)
7922 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
7923 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
7924 int ret
= strcmp (s1
->name
, s2
->name
);
7927 if (s1
->u
.p
!= s2
->u
.p
)
7928 return s1
->u
.p
> s2
->u
.p
? 1 : -1;
7932 static struct elf_symbuf_head
*
7933 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
7935 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
7936 struct elf_symbuf_symbol
*ssym
;
7937 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
7938 size_t i
, shndx_count
, total_size
;
7940 indbuf
= (Elf_Internal_Sym
**) bfd_malloc2 (symcount
, sizeof (*indbuf
));
7944 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
7945 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
7946 *ind
++ = &isymbuf
[i
];
7949 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
7950 elf_sort_elf_symbol
);
7953 if (indbufend
> indbuf
)
7954 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
7955 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
7958 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
7959 + (indbufend
- indbuf
) * sizeof (*ssym
));
7960 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
7961 if (ssymbuf
== NULL
)
7967 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
7968 ssymbuf
->ssym
= NULL
;
7969 ssymbuf
->count
= shndx_count
;
7970 ssymbuf
->st_shndx
= 0;
7971 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
7973 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
7976 ssymhead
->ssym
= ssym
;
7977 ssymhead
->count
= 0;
7978 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
7980 ssym
->st_name
= (*ind
)->st_name
;
7981 ssym
->st_info
= (*ind
)->st_info
;
7982 ssym
->st_other
= (*ind
)->st_other
;
7985 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
7986 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
7993 /* Check if 2 sections define the same set of local and global
7997 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
7998 struct bfd_link_info
*info
)
8001 const struct elf_backend_data
*bed1
, *bed2
;
8002 Elf_Internal_Shdr
*hdr1
, *hdr2
;
8003 size_t symcount1
, symcount2
;
8004 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
8005 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
8006 Elf_Internal_Sym
*isym
, *isymend
;
8007 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
8008 size_t count1
, count2
, i
;
8009 unsigned int shndx1
, shndx2
;
8015 /* Both sections have to be in ELF. */
8016 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
8017 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
8020 if (elf_section_type (sec1
) != elf_section_type (sec2
))
8023 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
8024 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
8025 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
8028 bed1
= get_elf_backend_data (bfd1
);
8029 bed2
= get_elf_backend_data (bfd2
);
8030 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
8031 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
8032 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
8033 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
8035 if (symcount1
== 0 || symcount2
== 0)
8041 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
8042 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
8044 if (ssymbuf1
== NULL
)
8046 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
8048 if (isymbuf1
== NULL
)
8051 if (!info
->reduce_memory_overheads
)
8053 ssymbuf1
= elf_create_symbuf (symcount1
, isymbuf1
);
8054 elf_tdata (bfd1
)->symbuf
= ssymbuf1
;
8058 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
8060 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
8062 if (isymbuf2
== NULL
)
8065 if (ssymbuf1
!= NULL
&& !info
->reduce_memory_overheads
)
8067 ssymbuf2
= elf_create_symbuf (symcount2
, isymbuf2
);
8068 elf_tdata (bfd2
)->symbuf
= ssymbuf2
;
8072 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
8074 /* Optimized faster version. */
8076 struct elf_symbol
*symp
;
8077 struct elf_symbuf_symbol
*ssym
, *ssymend
;
8080 hi
= ssymbuf1
->count
;
8085 mid
= (lo
+ hi
) / 2;
8086 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
8088 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
8092 count1
= ssymbuf1
[mid
].count
;
8099 hi
= ssymbuf2
->count
;
8104 mid
= (lo
+ hi
) / 2;
8105 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
8107 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
8111 count2
= ssymbuf2
[mid
].count
;
8117 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8121 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
8123 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
8124 if (symtable1
== NULL
|| symtable2
== NULL
)
8128 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
8129 ssym
< ssymend
; ssym
++, symp
++)
8131 symp
->u
.ssym
= ssym
;
8132 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
8138 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
8139 ssym
< ssymend
; ssym
++, symp
++)
8141 symp
->u
.ssym
= ssym
;
8142 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
8147 /* Sort symbol by name. */
8148 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8149 elf_sym_name_compare
);
8150 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8151 elf_sym_name_compare
);
8153 for (i
= 0; i
< count1
; i
++)
8154 /* Two symbols must have the same binding, type and name. */
8155 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
8156 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
8157 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8164 symtable1
= (struct elf_symbol
*)
8165 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
8166 symtable2
= (struct elf_symbol
*)
8167 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
8168 if (symtable1
== NULL
|| symtable2
== NULL
)
8171 /* Count definitions in the section. */
8173 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
8174 if (isym
->st_shndx
== shndx1
)
8175 symtable1
[count1
++].u
.isym
= isym
;
8178 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
8179 if (isym
->st_shndx
== shndx2
)
8180 symtable2
[count2
++].u
.isym
= isym
;
8182 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8185 for (i
= 0; i
< count1
; i
++)
8187 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
8188 symtable1
[i
].u
.isym
->st_name
);
8190 for (i
= 0; i
< count2
; i
++)
8192 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
8193 symtable2
[i
].u
.isym
->st_name
);
8195 /* Sort symbol by name. */
8196 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8197 elf_sym_name_compare
);
8198 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8199 elf_sym_name_compare
);
8201 for (i
= 0; i
< count1
; i
++)
8202 /* Two symbols must have the same binding, type and name. */
8203 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
8204 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
8205 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8223 /* Return TRUE if 2 section types are compatible. */
8226 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
8227 bfd
*bbfd
, const asection
*bsec
)
8231 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
8232 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8235 return elf_section_type (asec
) == elf_section_type (bsec
);
8238 /* Final phase of ELF linker. */
8240 /* A structure we use to avoid passing large numbers of arguments. */
8242 struct elf_final_link_info
8244 /* General link information. */
8245 struct bfd_link_info
*info
;
8248 /* Symbol string table. */
8249 struct elf_strtab_hash
*symstrtab
;
8250 /* .hash section. */
8252 /* symbol version section (.gnu.version). */
8253 asection
*symver_sec
;
8254 /* Buffer large enough to hold contents of any section. */
8256 /* Buffer large enough to hold external relocs of any section. */
8257 void *external_relocs
;
8258 /* Buffer large enough to hold internal relocs of any section. */
8259 Elf_Internal_Rela
*internal_relocs
;
8260 /* Buffer large enough to hold external local symbols of any input
8262 bfd_byte
*external_syms
;
8263 /* And a buffer for symbol section indices. */
8264 Elf_External_Sym_Shndx
*locsym_shndx
;
8265 /* Buffer large enough to hold internal local symbols of any input
8267 Elf_Internal_Sym
*internal_syms
;
8268 /* Array large enough to hold a symbol index for each local symbol
8269 of any input BFD. */
8271 /* Array large enough to hold a section pointer for each local
8272 symbol of any input BFD. */
8273 asection
**sections
;
8274 /* Buffer for SHT_SYMTAB_SHNDX section. */
8275 Elf_External_Sym_Shndx
*symshndxbuf
;
8276 /* Number of STT_FILE syms seen. */
8277 size_t filesym_count
;
8280 /* This struct is used to pass information to elf_link_output_extsym. */
8282 struct elf_outext_info
8285 bfd_boolean localsyms
;
8286 bfd_boolean file_sym_done
;
8287 struct elf_final_link_info
*flinfo
;
8291 /* Support for evaluating a complex relocation.
8293 Complex relocations are generalized, self-describing relocations. The
8294 implementation of them consists of two parts: complex symbols, and the
8295 relocations themselves.
8297 The relocations are use a reserved elf-wide relocation type code (R_RELC
8298 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8299 information (start bit, end bit, word width, etc) into the addend. This
8300 information is extracted from CGEN-generated operand tables within gas.
8302 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8303 internal) representing prefix-notation expressions, including but not
8304 limited to those sorts of expressions normally encoded as addends in the
8305 addend field. The symbol mangling format is:
8308 | <unary-operator> ':' <node>
8309 | <binary-operator> ':' <node> ':' <node>
8312 <literal> := 's' <digits=N> ':' <N character symbol name>
8313 | 'S' <digits=N> ':' <N character section name>
8317 <binary-operator> := as in C
8318 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8321 set_symbol_value (bfd
*bfd_with_globals
,
8322 Elf_Internal_Sym
*isymbuf
,
8327 struct elf_link_hash_entry
**sym_hashes
;
8328 struct elf_link_hash_entry
*h
;
8329 size_t extsymoff
= locsymcount
;
8331 if (symidx
< locsymcount
)
8333 Elf_Internal_Sym
*sym
;
8335 sym
= isymbuf
+ symidx
;
8336 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
8338 /* It is a local symbol: move it to the
8339 "absolute" section and give it a value. */
8340 sym
->st_shndx
= SHN_ABS
;
8341 sym
->st_value
= val
;
8344 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
8348 /* It is a global symbol: set its link type
8349 to "defined" and give it a value. */
8351 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
8352 h
= sym_hashes
[symidx
- extsymoff
];
8353 while (h
->root
.type
== bfd_link_hash_indirect
8354 || h
->root
.type
== bfd_link_hash_warning
)
8355 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8356 h
->root
.type
= bfd_link_hash_defined
;
8357 h
->root
.u
.def
.value
= val
;
8358 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
8362 resolve_symbol (const char *name
,
8364 struct elf_final_link_info
*flinfo
,
8366 Elf_Internal_Sym
*isymbuf
,
8369 Elf_Internal_Sym
*sym
;
8370 struct bfd_link_hash_entry
*global_entry
;
8371 const char *candidate
= NULL
;
8372 Elf_Internal_Shdr
*symtab_hdr
;
8375 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
8377 for (i
= 0; i
< locsymcount
; ++ i
)
8381 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
8384 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
8385 symtab_hdr
->sh_link
,
8388 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8389 name
, candidate
, (unsigned long) sym
->st_value
);
8391 if (candidate
&& strcmp (candidate
, name
) == 0)
8393 asection
*sec
= flinfo
->sections
[i
];
8395 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
8396 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
8398 printf ("Found symbol with value %8.8lx\n",
8399 (unsigned long) *result
);
8405 /* Hmm, haven't found it yet. perhaps it is a global. */
8406 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
8407 FALSE
, FALSE
, TRUE
);
8411 if (global_entry
->type
== bfd_link_hash_defined
8412 || global_entry
->type
== bfd_link_hash_defweak
)
8414 *result
= (global_entry
->u
.def
.value
8415 + global_entry
->u
.def
.section
->output_section
->vma
8416 + global_entry
->u
.def
.section
->output_offset
);
8418 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8419 global_entry
->root
.string
, (unsigned long) *result
);
8427 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8428 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8429 names like "foo.end" which is the end address of section "foo". */
8432 resolve_section (const char *name
,
8440 for (curr
= sections
; curr
; curr
= curr
->next
)
8441 if (strcmp (curr
->name
, name
) == 0)
8443 *result
= curr
->vma
;
8447 /* Hmm. still haven't found it. try pseudo-section names. */
8448 /* FIXME: This could be coded more efficiently... */
8449 for (curr
= sections
; curr
; curr
= curr
->next
)
8451 len
= strlen (curr
->name
);
8452 if (len
> strlen (name
))
8455 if (strncmp (curr
->name
, name
, len
) == 0)
8457 if (strncmp (".end", name
+ len
, 4) == 0)
8459 *result
= (curr
->vma
8460 + curr
->size
/ bfd_octets_per_byte (abfd
, NULL
));
8464 /* Insert more pseudo-section names here, if you like. */
8472 undefined_reference (const char *reftype
, const char *name
)
8474 /* xgettext:c-format */
8475 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8480 eval_symbol (bfd_vma
*result
,
8483 struct elf_final_link_info
*flinfo
,
8485 Elf_Internal_Sym
*isymbuf
,
8494 const char *sym
= *symp
;
8496 bfd_boolean symbol_is_section
= FALSE
;
8501 if (len
< 1 || len
> sizeof (symbuf
))
8503 bfd_set_error (bfd_error_invalid_operation
);
8516 *result
= strtoul (sym
, (char **) symp
, 16);
8520 symbol_is_section
= TRUE
;
8524 symlen
= strtol (sym
, (char **) symp
, 10);
8525 sym
= *symp
+ 1; /* Skip the trailing ':'. */
8527 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
8529 bfd_set_error (bfd_error_invalid_operation
);
8533 memcpy (symbuf
, sym
, symlen
);
8534 symbuf
[symlen
] = '\0';
8535 *symp
= sym
+ symlen
;
8537 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8538 the symbol as a section, or vice-versa. so we're pretty liberal in our
8539 interpretation here; section means "try section first", not "must be a
8540 section", and likewise with symbol. */
8542 if (symbol_is_section
)
8544 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
8545 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8546 isymbuf
, locsymcount
))
8548 undefined_reference ("section", symbuf
);
8554 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8555 isymbuf
, locsymcount
)
8556 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8559 undefined_reference ("symbol", symbuf
);
8566 /* All that remains are operators. */
8568 #define UNARY_OP(op) \
8569 if (strncmp (sym, #op, strlen (#op)) == 0) \
8571 sym += strlen (#op); \
8575 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8576 isymbuf, locsymcount, signed_p)) \
8579 *result = op ((bfd_signed_vma) a); \
8585 #define BINARY_OP(op) \
8586 if (strncmp (sym, #op, strlen (#op)) == 0) \
8588 sym += strlen (#op); \
8592 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8593 isymbuf, locsymcount, signed_p)) \
8596 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8597 isymbuf, locsymcount, signed_p)) \
8600 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8630 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
8631 bfd_set_error (bfd_error_invalid_operation
);
8637 put_value (bfd_vma size
,
8638 unsigned long chunksz
,
8643 location
+= (size
- chunksz
);
8645 for (; size
; size
-= chunksz
, location
-= chunksz
)
8650 bfd_put_8 (input_bfd
, x
, location
);
8654 bfd_put_16 (input_bfd
, x
, location
);
8658 bfd_put_32 (input_bfd
, x
, location
);
8659 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8665 bfd_put_64 (input_bfd
, x
, location
);
8666 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8679 get_value (bfd_vma size
,
8680 unsigned long chunksz
,
8687 /* Sanity checks. */
8688 BFD_ASSERT (chunksz
<= sizeof (x
)
8691 && (size
% chunksz
) == 0
8692 && input_bfd
!= NULL
8693 && location
!= NULL
);
8695 if (chunksz
== sizeof (x
))
8697 BFD_ASSERT (size
== chunksz
);
8699 /* Make sure that we do not perform an undefined shift operation.
8700 We know that size == chunksz so there will only be one iteration
8701 of the loop below. */
8705 shift
= 8 * chunksz
;
8707 for (; size
; size
-= chunksz
, location
+= chunksz
)
8712 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
8715 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
8718 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
8722 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
8733 decode_complex_addend (unsigned long *start
, /* in bits */
8734 unsigned long *oplen
, /* in bits */
8735 unsigned long *len
, /* in bits */
8736 unsigned long *wordsz
, /* in bytes */
8737 unsigned long *chunksz
, /* in bytes */
8738 unsigned long *lsb0_p
,
8739 unsigned long *signed_p
,
8740 unsigned long *trunc_p
,
8741 unsigned long encoded
)
8743 * start
= encoded
& 0x3F;
8744 * len
= (encoded
>> 6) & 0x3F;
8745 * oplen
= (encoded
>> 12) & 0x3F;
8746 * wordsz
= (encoded
>> 18) & 0xF;
8747 * chunksz
= (encoded
>> 22) & 0xF;
8748 * lsb0_p
= (encoded
>> 27) & 1;
8749 * signed_p
= (encoded
>> 28) & 1;
8750 * trunc_p
= (encoded
>> 29) & 1;
8753 bfd_reloc_status_type
8754 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
8755 asection
*input_section ATTRIBUTE_UNUSED
,
8757 Elf_Internal_Rela
*rel
,
8760 bfd_vma shift
, x
, mask
;
8761 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
8762 bfd_reloc_status_type r
;
8764 /* Perform this reloc, since it is complex.
8765 (this is not to say that it necessarily refers to a complex
8766 symbol; merely that it is a self-describing CGEN based reloc.
8767 i.e. the addend has the complete reloc information (bit start, end,
8768 word size, etc) encoded within it.). */
8770 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
8771 &chunksz
, &lsb0_p
, &signed_p
,
8772 &trunc_p
, rel
->r_addend
);
8774 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
8777 shift
= (start
+ 1) - len
;
8779 shift
= (8 * wordsz
) - (start
+ len
);
8781 x
= get_value (wordsz
, chunksz
, input_bfd
,
8783 + rel
->r_offset
* bfd_octets_per_byte (input_bfd
, NULL
));
8786 printf ("Doing complex reloc: "
8787 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8788 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8789 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8790 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
8791 oplen
, (unsigned long) x
, (unsigned long) mask
,
8792 (unsigned long) relocation
);
8797 /* Now do an overflow check. */
8798 r
= bfd_check_overflow ((signed_p
8799 ? complain_overflow_signed
8800 : complain_overflow_unsigned
),
8801 len
, 0, (8 * wordsz
),
8805 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
8808 printf (" relocation: %8.8lx\n"
8809 " shifted mask: %8.8lx\n"
8810 " shifted/masked reloc: %8.8lx\n"
8811 " result: %8.8lx\n",
8812 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
8813 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
8815 put_value (wordsz
, chunksz
, input_bfd
, x
,
8816 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
, NULL
));
8820 /* Functions to read r_offset from external (target order) reloc
8821 entry. Faster than bfd_getl32 et al, because we let the compiler
8822 know the value is aligned. */
8825 ext32l_r_offset (const void *p
)
8832 const union aligned32
*a
8833 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8835 uint32_t aval
= ( (uint32_t) a
->c
[0]
8836 | (uint32_t) a
->c
[1] << 8
8837 | (uint32_t) a
->c
[2] << 16
8838 | (uint32_t) a
->c
[3] << 24);
8843 ext32b_r_offset (const void *p
)
8850 const union aligned32
*a
8851 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8853 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
8854 | (uint32_t) a
->c
[1] << 16
8855 | (uint32_t) a
->c
[2] << 8
8856 | (uint32_t) a
->c
[3]);
8860 #ifdef BFD_HOST_64_BIT
8862 ext64l_r_offset (const void *p
)
8869 const union aligned64
*a
8870 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8872 uint64_t aval
= ( (uint64_t) a
->c
[0]
8873 | (uint64_t) a
->c
[1] << 8
8874 | (uint64_t) a
->c
[2] << 16
8875 | (uint64_t) a
->c
[3] << 24
8876 | (uint64_t) a
->c
[4] << 32
8877 | (uint64_t) a
->c
[5] << 40
8878 | (uint64_t) a
->c
[6] << 48
8879 | (uint64_t) a
->c
[7] << 56);
8884 ext64b_r_offset (const void *p
)
8891 const union aligned64
*a
8892 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8894 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
8895 | (uint64_t) a
->c
[1] << 48
8896 | (uint64_t) a
->c
[2] << 40
8897 | (uint64_t) a
->c
[3] << 32
8898 | (uint64_t) a
->c
[4] << 24
8899 | (uint64_t) a
->c
[5] << 16
8900 | (uint64_t) a
->c
[6] << 8
8901 | (uint64_t) a
->c
[7]);
8906 /* When performing a relocatable link, the input relocations are
8907 preserved. But, if they reference global symbols, the indices
8908 referenced must be updated. Update all the relocations found in
8912 elf_link_adjust_relocs (bfd
*abfd
,
8914 struct bfd_elf_section_reloc_data
*reldata
,
8916 struct bfd_link_info
*info
)
8919 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8921 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8922 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8923 bfd_vma r_type_mask
;
8925 unsigned int count
= reldata
->count
;
8926 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
8928 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
8930 swap_in
= bed
->s
->swap_reloc_in
;
8931 swap_out
= bed
->s
->swap_reloc_out
;
8933 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
8935 swap_in
= bed
->s
->swap_reloca_in
;
8936 swap_out
= bed
->s
->swap_reloca_out
;
8941 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
8944 if (bed
->s
->arch_size
== 32)
8951 r_type_mask
= 0xffffffff;
8955 erela
= reldata
->hdr
->contents
;
8956 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
8958 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
8961 if (*rel_hash
== NULL
)
8964 if ((*rel_hash
)->indx
== -2
8965 && info
->gc_sections
8966 && ! info
->gc_keep_exported
)
8968 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8969 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
8971 (*rel_hash
)->root
.root
.string
);
8972 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
8974 bfd_set_error (bfd_error_invalid_operation
);
8977 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
8979 (*swap_in
) (abfd
, erela
, irela
);
8980 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
8981 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
8982 | (irela
[j
].r_info
& r_type_mask
));
8983 (*swap_out
) (abfd
, irela
, erela
);
8986 if (bed
->elf_backend_update_relocs
)
8987 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
8989 if (sort
&& count
!= 0)
8991 bfd_vma (*ext_r_off
) (const void *);
8994 bfd_byte
*base
, *end
, *p
, *loc
;
8995 bfd_byte
*buf
= NULL
;
8997 if (bed
->s
->arch_size
== 32)
8999 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9000 ext_r_off
= ext32l_r_offset
;
9001 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9002 ext_r_off
= ext32b_r_offset
;
9008 #ifdef BFD_HOST_64_BIT
9009 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9010 ext_r_off
= ext64l_r_offset
;
9011 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9012 ext_r_off
= ext64b_r_offset
;
9018 /* Must use a stable sort here. A modified insertion sort,
9019 since the relocs are mostly sorted already. */
9020 elt_size
= reldata
->hdr
->sh_entsize
;
9021 base
= reldata
->hdr
->contents
;
9022 end
= base
+ count
* elt_size
;
9023 if (elt_size
> sizeof (Elf64_External_Rela
))
9026 /* Ensure the first element is lowest. This acts as a sentinel,
9027 speeding the main loop below. */
9028 r_off
= (*ext_r_off
) (base
);
9029 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
9031 bfd_vma r_off2
= (*ext_r_off
) (p
);
9040 /* Don't just swap *base and *loc as that changes the order
9041 of the original base[0] and base[1] if they happen to
9042 have the same r_offset. */
9043 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
9044 memcpy (onebuf
, loc
, elt_size
);
9045 memmove (base
+ elt_size
, base
, loc
- base
);
9046 memcpy (base
, onebuf
, elt_size
);
9049 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
9051 /* base to p is sorted, *p is next to insert. */
9052 r_off
= (*ext_r_off
) (p
);
9053 /* Search the sorted region for location to insert. */
9055 while (r_off
< (*ext_r_off
) (loc
))
9060 /* Chances are there is a run of relocs to insert here,
9061 from one of more input files. Files are not always
9062 linked in order due to the way elf_link_input_bfd is
9063 called. See pr17666. */
9064 size_t sortlen
= p
- loc
;
9065 bfd_vma r_off2
= (*ext_r_off
) (loc
);
9066 size_t runlen
= elt_size
;
9067 size_t buf_size
= 96 * 1024;
9068 while (p
+ runlen
< end
9069 && (sortlen
<= buf_size
9070 || runlen
+ elt_size
<= buf_size
)
9071 && r_off2
> (*ext_r_off
) (p
+ runlen
))
9075 buf
= bfd_malloc (buf_size
);
9079 if (runlen
< sortlen
)
9081 memcpy (buf
, p
, runlen
);
9082 memmove (loc
+ runlen
, loc
, sortlen
);
9083 memcpy (loc
, buf
, runlen
);
9087 memcpy (buf
, loc
, sortlen
);
9088 memmove (loc
, p
, runlen
);
9089 memcpy (loc
+ runlen
, buf
, sortlen
);
9091 p
+= runlen
- elt_size
;
9094 /* Hashes are no longer valid. */
9095 free (reldata
->hashes
);
9096 reldata
->hashes
= NULL
;
9102 struct elf_link_sort_rela
9108 enum elf_reloc_type_class type
;
9109 /* We use this as an array of size int_rels_per_ext_rel. */
9110 Elf_Internal_Rela rela
[1];
9113 /* qsort stability here and for cmp2 is only an issue if multiple
9114 dynamic relocations are emitted at the same address. But targets
9115 that apply a series of dynamic relocations each operating on the
9116 result of the prior relocation can't use -z combreloc as
9117 implemented anyway. Such schemes tend to be broken by sorting on
9118 symbol index. That leaves dynamic NONE relocs as the only other
9119 case where ld might emit multiple relocs at the same address, and
9120 those are only emitted due to target bugs. */
9123 elf_link_sort_cmp1 (const void *A
, const void *B
)
9125 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9126 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9127 int relativea
, relativeb
;
9129 relativea
= a
->type
== reloc_class_relative
;
9130 relativeb
= b
->type
== reloc_class_relative
;
9132 if (relativea
< relativeb
)
9134 if (relativea
> relativeb
)
9136 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
9138 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
9140 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9142 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9148 elf_link_sort_cmp2 (const void *A
, const void *B
)
9150 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9151 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9153 if (a
->type
< b
->type
)
9155 if (a
->type
> b
->type
)
9157 if (a
->u
.offset
< b
->u
.offset
)
9159 if (a
->u
.offset
> b
->u
.offset
)
9161 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9163 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9169 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
9171 asection
*dynamic_relocs
;
9174 bfd_size_type count
, size
;
9175 size_t i
, ret
, sort_elt
, ext_size
;
9176 bfd_byte
*sort
, *s_non_relative
, *p
;
9177 struct elf_link_sort_rela
*sq
;
9178 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9179 int i2e
= bed
->s
->int_rels_per_ext_rel
;
9180 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
9181 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9182 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9183 struct bfd_link_order
*lo
;
9185 bfd_boolean use_rela
;
9187 /* Find a dynamic reloc section. */
9188 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
9189 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
9190 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
9191 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9193 bfd_boolean use_rela_initialised
= FALSE
;
9195 /* This is just here to stop gcc from complaining.
9196 Its initialization checking code is not perfect. */
9199 /* Both sections are present. Examine the sizes
9200 of the indirect sections to help us choose. */
9201 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9202 if (lo
->type
== bfd_indirect_link_order
)
9204 asection
*o
= lo
->u
.indirect
.section
;
9206 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9208 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9209 /* Section size is divisible by both rel and rela sizes.
9210 It is of no help to us. */
9214 /* Section size is only divisible by rela. */
9215 if (use_rela_initialised
&& !use_rela
)
9217 _bfd_error_handler (_("%pB: unable to sort relocs - "
9218 "they are in more than one size"),
9220 bfd_set_error (bfd_error_invalid_operation
);
9226 use_rela_initialised
= TRUE
;
9230 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9232 /* Section size is only divisible by rel. */
9233 if (use_rela_initialised
&& use_rela
)
9235 _bfd_error_handler (_("%pB: unable to sort relocs - "
9236 "they are in more than one size"),
9238 bfd_set_error (bfd_error_invalid_operation
);
9244 use_rela_initialised
= TRUE
;
9249 /* The section size is not divisible by either -
9250 something is wrong. */
9251 _bfd_error_handler (_("%pB: unable to sort relocs - "
9252 "they are of an unknown size"), abfd
);
9253 bfd_set_error (bfd_error_invalid_operation
);
9258 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9259 if (lo
->type
== bfd_indirect_link_order
)
9261 asection
*o
= lo
->u
.indirect
.section
;
9263 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9265 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9266 /* Section size is divisible by both rel and rela sizes.
9267 It is of no help to us. */
9271 /* Section size is only divisible by rela. */
9272 if (use_rela_initialised
&& !use_rela
)
9274 _bfd_error_handler (_("%pB: unable to sort relocs - "
9275 "they are in more than one size"),
9277 bfd_set_error (bfd_error_invalid_operation
);
9283 use_rela_initialised
= TRUE
;
9287 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9289 /* Section size is only divisible by rel. */
9290 if (use_rela_initialised
&& use_rela
)
9292 _bfd_error_handler (_("%pB: unable to sort relocs - "
9293 "they are in more than one size"),
9295 bfd_set_error (bfd_error_invalid_operation
);
9301 use_rela_initialised
= TRUE
;
9306 /* The section size is not divisible by either -
9307 something is wrong. */
9308 _bfd_error_handler (_("%pB: unable to sort relocs - "
9309 "they are of an unknown size"), abfd
);
9310 bfd_set_error (bfd_error_invalid_operation
);
9315 if (! use_rela_initialised
)
9319 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
9321 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9328 dynamic_relocs
= rela_dyn
;
9329 ext_size
= bed
->s
->sizeof_rela
;
9330 swap_in
= bed
->s
->swap_reloca_in
;
9331 swap_out
= bed
->s
->swap_reloca_out
;
9335 dynamic_relocs
= rel_dyn
;
9336 ext_size
= bed
->s
->sizeof_rel
;
9337 swap_in
= bed
->s
->swap_reloc_in
;
9338 swap_out
= bed
->s
->swap_reloc_out
;
9342 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9343 if (lo
->type
== bfd_indirect_link_order
)
9344 size
+= lo
->u
.indirect
.section
->size
;
9346 if (size
!= dynamic_relocs
->size
)
9349 sort_elt
= (sizeof (struct elf_link_sort_rela
)
9350 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
9352 count
= dynamic_relocs
->size
/ ext_size
;
9355 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
9359 (*info
->callbacks
->warning
)
9360 (info
, _("not enough memory to sort relocations"), 0, abfd
, 0, 0);
9364 if (bed
->s
->arch_size
== 32)
9365 r_sym_mask
= ~(bfd_vma
) 0xff;
9367 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
9369 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9370 if (lo
->type
== bfd_indirect_link_order
)
9372 bfd_byte
*erel
, *erelend
;
9373 asection
*o
= lo
->u
.indirect
.section
;
9375 if (o
->contents
== NULL
&& o
->size
!= 0)
9377 /* This is a reloc section that is being handled as a normal
9378 section. See bfd_section_from_shdr. We can't combine
9379 relocs in this case. */
9384 erelend
= o
->contents
+ o
->size
;
9385 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
9387 while (erel
< erelend
)
9389 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9391 (*swap_in
) (abfd
, erel
, s
->rela
);
9392 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
9393 s
->u
.sym_mask
= r_sym_mask
;
9399 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
9401 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
9403 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9404 if (s
->type
!= reloc_class_relative
)
9410 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
9411 for (; i
< count
; i
++, p
+= sort_elt
)
9413 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
9414 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
9416 sp
->u
.offset
= sq
->rela
->r_offset
;
9419 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
9421 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
9422 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
9424 /* We have plt relocs in .rela.dyn. */
9425 sq
= (struct elf_link_sort_rela
*) sort
;
9426 for (i
= 0; i
< count
; i
++)
9427 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
9429 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
9431 struct bfd_link_order
**plo
;
9432 /* Put srelplt link_order last. This is so the output_offset
9433 set in the next loop is correct for DT_JMPREL. */
9434 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
9435 if ((*plo
)->type
== bfd_indirect_link_order
9436 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
9442 plo
= &(*plo
)->next
;
9445 dynamic_relocs
->map_tail
.link_order
= lo
;
9450 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9451 if (lo
->type
== bfd_indirect_link_order
)
9453 bfd_byte
*erel
, *erelend
;
9454 asection
*o
= lo
->u
.indirect
.section
;
9457 erelend
= o
->contents
+ o
->size
;
9458 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
9459 while (erel
< erelend
)
9461 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9462 (*swap_out
) (abfd
, s
->rela
, erel
);
9469 *psec
= dynamic_relocs
;
9473 /* Add a symbol to the output symbol string table. */
9476 elf_link_output_symstrtab (struct elf_final_link_info
*flinfo
,
9478 Elf_Internal_Sym
*elfsym
,
9479 asection
*input_sec
,
9480 struct elf_link_hash_entry
*h
)
9482 int (*output_symbol_hook
)
9483 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
9484 struct elf_link_hash_entry
*);
9485 struct elf_link_hash_table
*hash_table
;
9486 const struct elf_backend_data
*bed
;
9487 bfd_size_type strtabsize
;
9489 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9491 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9492 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
9493 if (output_symbol_hook
!= NULL
)
9495 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
9500 if (ELF_ST_TYPE (elfsym
->st_info
) == STT_GNU_IFUNC
)
9501 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_ifunc
;
9502 if (ELF_ST_BIND (elfsym
->st_info
) == STB_GNU_UNIQUE
)
9503 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_unique
;
9507 || (input_sec
->flags
& SEC_EXCLUDE
))
9508 elfsym
->st_name
= (unsigned long) -1;
9511 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9512 to get the final offset for st_name. */
9514 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
9516 if (elfsym
->st_name
== (unsigned long) -1)
9520 hash_table
= elf_hash_table (flinfo
->info
);
9521 strtabsize
= hash_table
->strtabsize
;
9522 if (strtabsize
<= hash_table
->strtabcount
)
9524 strtabsize
+= strtabsize
;
9525 hash_table
->strtabsize
= strtabsize
;
9526 strtabsize
*= sizeof (*hash_table
->strtab
);
9528 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
9530 if (hash_table
->strtab
== NULL
)
9533 hash_table
->strtab
[hash_table
->strtabcount
].sym
= *elfsym
;
9534 hash_table
->strtab
[hash_table
->strtabcount
].dest_index
9535 = hash_table
->strtabcount
;
9536 hash_table
->strtab
[hash_table
->strtabcount
].destshndx_index
9537 = flinfo
->symshndxbuf
? bfd_get_symcount (flinfo
->output_bfd
) : 0;
9539 flinfo
->output_bfd
->symcount
+= 1;
9540 hash_table
->strtabcount
+= 1;
9545 /* Swap symbols out to the symbol table and flush the output symbols to
9549 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
9551 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
9554 const struct elf_backend_data
*bed
;
9556 Elf_Internal_Shdr
*hdr
;
9560 if (!hash_table
->strtabcount
)
9563 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9565 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9567 amt
= bed
->s
->sizeof_sym
* hash_table
->strtabcount
;
9568 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
9572 if (flinfo
->symshndxbuf
)
9574 amt
= sizeof (Elf_External_Sym_Shndx
);
9575 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
9576 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
9577 if (flinfo
->symshndxbuf
== NULL
)
9584 for (i
= 0; i
< hash_table
->strtabcount
; i
++)
9586 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
9587 if (elfsym
->sym
.st_name
== (unsigned long) -1)
9588 elfsym
->sym
.st_name
= 0;
9591 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
9592 elfsym
->sym
.st_name
);
9593 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
9594 ((bfd_byte
*) symbuf
9595 + (elfsym
->dest_index
9596 * bed
->s
->sizeof_sym
)),
9597 (flinfo
->symshndxbuf
9598 + elfsym
->destshndx_index
));
9601 /* Allow the linker to examine the strtab and symtab now they are
9604 if (flinfo
->info
->callbacks
->examine_strtab
)
9605 flinfo
->info
->callbacks
->examine_strtab (hash_table
->strtab
,
9606 hash_table
->strtabcount
,
9609 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
9610 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
9611 amt
= hash_table
->strtabcount
* bed
->s
->sizeof_sym
;
9612 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
9613 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
9615 hdr
->sh_size
+= amt
;
9623 free (hash_table
->strtab
);
9624 hash_table
->strtab
= NULL
;
9629 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9632 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
9634 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
9635 && sym
->st_shndx
< SHN_LORESERVE
)
9637 /* The gABI doesn't support dynamic symbols in output sections
9640 /* xgettext:c-format */
9641 (_("%pB: too many sections: %d (>= %d)"),
9642 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
9643 bfd_set_error (bfd_error_nonrepresentable_section
);
9649 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9650 allowing an unsatisfied unversioned symbol in the DSO to match a
9651 versioned symbol that would normally require an explicit version.
9652 We also handle the case that a DSO references a hidden symbol
9653 which may be satisfied by a versioned symbol in another DSO. */
9656 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
9657 const struct elf_backend_data
*bed
,
9658 struct elf_link_hash_entry
*h
)
9661 struct elf_link_loaded_list
*loaded
;
9663 if (!is_elf_hash_table (info
->hash
))
9666 /* Check indirect symbol. */
9667 while (h
->root
.type
== bfd_link_hash_indirect
)
9668 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9670 switch (h
->root
.type
)
9676 case bfd_link_hash_undefined
:
9677 case bfd_link_hash_undefweak
:
9678 abfd
= h
->root
.u
.undef
.abfd
;
9680 || (abfd
->flags
& DYNAMIC
) == 0
9681 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
9685 case bfd_link_hash_defined
:
9686 case bfd_link_hash_defweak
:
9687 abfd
= h
->root
.u
.def
.section
->owner
;
9690 case bfd_link_hash_common
:
9691 abfd
= h
->root
.u
.c
.p
->section
->owner
;
9694 BFD_ASSERT (abfd
!= NULL
);
9696 for (loaded
= elf_hash_table (info
)->loaded
;
9698 loaded
= loaded
->next
)
9701 Elf_Internal_Shdr
*hdr
;
9705 Elf_Internal_Shdr
*versymhdr
;
9706 Elf_Internal_Sym
*isym
;
9707 Elf_Internal_Sym
*isymend
;
9708 Elf_Internal_Sym
*isymbuf
;
9709 Elf_External_Versym
*ever
;
9710 Elf_External_Versym
*extversym
;
9712 input
= loaded
->abfd
;
9714 /* We check each DSO for a possible hidden versioned definition. */
9716 || (input
->flags
& DYNAMIC
) == 0
9717 || elf_dynversym (input
) == 0)
9720 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
9722 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9723 if (elf_bad_symtab (input
))
9725 extsymcount
= symcount
;
9730 extsymcount
= symcount
- hdr
->sh_info
;
9731 extsymoff
= hdr
->sh_info
;
9734 if (extsymcount
== 0)
9737 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
9739 if (isymbuf
== NULL
)
9742 /* Read in any version definitions. */
9743 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
9744 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
9745 if (extversym
== NULL
)
9748 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
9749 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
9750 != versymhdr
->sh_size
))
9758 ever
= extversym
+ extsymoff
;
9759 isymend
= isymbuf
+ extsymcount
;
9760 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
9763 Elf_Internal_Versym iver
;
9764 unsigned short version_index
;
9766 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
9767 || isym
->st_shndx
== SHN_UNDEF
)
9770 name
= bfd_elf_string_from_elf_section (input
,
9773 if (strcmp (name
, h
->root
.root
.string
) != 0)
9776 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
9778 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
9780 && h
->forced_local
))
9782 /* If we have a non-hidden versioned sym, then it should
9783 have provided a definition for the undefined sym unless
9784 it is defined in a non-shared object and forced local.
9789 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
9790 if (version_index
== 1 || version_index
== 2)
9792 /* This is the base or first version. We can use it. */
9806 /* Convert ELF common symbol TYPE. */
9809 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
9811 /* Commom symbol can only appear in relocatable link. */
9812 if (!bfd_link_relocatable (info
))
9814 switch (info
->elf_stt_common
)
9818 case elf_stt_common
:
9821 case no_elf_stt_common
:
9828 /* Add an external symbol to the symbol table. This is called from
9829 the hash table traversal routine. When generating a shared object,
9830 we go through the symbol table twice. The first time we output
9831 anything that might have been forced to local scope in a version
9832 script. The second time we output the symbols that are still
9836 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
9838 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
9839 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
9840 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
9842 Elf_Internal_Sym sym
;
9843 asection
*input_sec
;
9844 const struct elf_backend_data
*bed
;
9849 if (h
->root
.type
== bfd_link_hash_warning
)
9851 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9852 if (h
->root
.type
== bfd_link_hash_new
)
9856 /* Decide whether to output this symbol in this pass. */
9857 if (eoinfo
->localsyms
)
9859 if (!h
->forced_local
)
9864 if (h
->forced_local
)
9868 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9870 if (h
->root
.type
== bfd_link_hash_undefined
)
9872 /* If we have an undefined symbol reference here then it must have
9873 come from a shared library that is being linked in. (Undefined
9874 references in regular files have already been handled unless
9875 they are in unreferenced sections which are removed by garbage
9877 bfd_boolean ignore_undef
= FALSE
;
9879 /* Some symbols may be special in that the fact that they're
9880 undefined can be safely ignored - let backend determine that. */
9881 if (bed
->elf_backend_ignore_undef_symbol
)
9882 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
9884 /* If we are reporting errors for this situation then do so now. */
9886 && h
->ref_dynamic_nonweak
9887 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
9888 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
9889 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
9890 (*flinfo
->info
->callbacks
->undefined_symbol
)
9891 (flinfo
->info
, h
->root
.root
.string
,
9892 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
,
9894 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
);
9896 /* Strip a global symbol defined in a discarded section. */
9901 /* We should also warn if a forced local symbol is referenced from
9902 shared libraries. */
9903 if (bfd_link_executable (flinfo
->info
)
9908 && h
->ref_dynamic_nonweak
9909 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
9913 struct elf_link_hash_entry
*hi
= h
;
9915 /* Check indirect symbol. */
9916 while (hi
->root
.type
== bfd_link_hash_indirect
)
9917 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
9919 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
9920 /* xgettext:c-format */
9921 msg
= _("%pB: internal symbol `%s' in %pB is referenced by DSO");
9922 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
9923 /* xgettext:c-format */
9924 msg
= _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
9926 /* xgettext:c-format */
9927 msg
= _("%pB: local symbol `%s' in %pB is referenced by DSO");
9928 def_bfd
= flinfo
->output_bfd
;
9929 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
9930 def_bfd
= hi
->root
.u
.def
.section
->owner
;
9931 _bfd_error_handler (msg
, flinfo
->output_bfd
,
9932 h
->root
.root
.string
, def_bfd
);
9933 bfd_set_error (bfd_error_bad_value
);
9934 eoinfo
->failed
= TRUE
;
9938 /* We don't want to output symbols that have never been mentioned by
9939 a regular file, or that we have been told to strip. However, if
9940 h->indx is set to -2, the symbol is used by a reloc and we must
9945 else if ((h
->def_dynamic
9947 || h
->root
.type
== bfd_link_hash_new
)
9951 else if (flinfo
->info
->strip
== strip_all
)
9953 else if (flinfo
->info
->strip
== strip_some
9954 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
9955 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
9957 else if ((h
->root
.type
== bfd_link_hash_defined
9958 || h
->root
.type
== bfd_link_hash_defweak
)
9959 && ((flinfo
->info
->strip_discarded
9960 && discarded_section (h
->root
.u
.def
.section
))
9961 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
9962 && h
->root
.u
.def
.section
->owner
!= NULL
9963 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
9965 else if ((h
->root
.type
== bfd_link_hash_undefined
9966 || h
->root
.type
== bfd_link_hash_undefweak
)
9967 && h
->root
.u
.undef
.abfd
!= NULL
9968 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
9973 /* If we're stripping it, and it's not a dynamic symbol, there's
9974 nothing else to do. However, if it is a forced local symbol or
9975 an ifunc symbol we need to give the backend finish_dynamic_symbol
9976 function a chance to make it dynamic. */
9979 && type
!= STT_GNU_IFUNC
9980 && !h
->forced_local
)
9984 sym
.st_size
= h
->size
;
9985 sym
.st_other
= h
->other
;
9986 switch (h
->root
.type
)
9989 case bfd_link_hash_new
:
9990 case bfd_link_hash_warning
:
9994 case bfd_link_hash_undefined
:
9995 case bfd_link_hash_undefweak
:
9996 input_sec
= bfd_und_section_ptr
;
9997 sym
.st_shndx
= SHN_UNDEF
;
10000 case bfd_link_hash_defined
:
10001 case bfd_link_hash_defweak
:
10003 input_sec
= h
->root
.u
.def
.section
;
10004 if (input_sec
->output_section
!= NULL
)
10007 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
10008 input_sec
->output_section
);
10009 if (sym
.st_shndx
== SHN_BAD
)
10012 /* xgettext:c-format */
10013 (_("%pB: could not find output section %pA for input section %pA"),
10014 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
10015 bfd_set_error (bfd_error_nonrepresentable_section
);
10016 eoinfo
->failed
= TRUE
;
10020 /* ELF symbols in relocatable files are section relative,
10021 but in nonrelocatable files they are virtual
10023 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
10024 if (!bfd_link_relocatable (flinfo
->info
))
10026 sym
.st_value
+= input_sec
->output_section
->vma
;
10027 if (h
->type
== STT_TLS
)
10029 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
10030 if (tls_sec
!= NULL
)
10031 sym
.st_value
-= tls_sec
->vma
;
10037 BFD_ASSERT (input_sec
->owner
== NULL
10038 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
10039 sym
.st_shndx
= SHN_UNDEF
;
10040 input_sec
= bfd_und_section_ptr
;
10045 case bfd_link_hash_common
:
10046 input_sec
= h
->root
.u
.c
.p
->section
;
10047 sym
.st_shndx
= bed
->common_section_index (input_sec
);
10048 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
10051 case bfd_link_hash_indirect
:
10052 /* These symbols are created by symbol versioning. They point
10053 to the decorated version of the name. For example, if the
10054 symbol foo@@GNU_1.2 is the default, which should be used when
10055 foo is used with no version, then we add an indirect symbol
10056 foo which points to foo@@GNU_1.2. We ignore these symbols,
10057 since the indirected symbol is already in the hash table. */
10061 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
10062 switch (h
->root
.type
)
10064 case bfd_link_hash_common
:
10065 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10067 case bfd_link_hash_defined
:
10068 case bfd_link_hash_defweak
:
10069 if (bed
->common_definition (&sym
))
10070 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10074 case bfd_link_hash_undefined
:
10075 case bfd_link_hash_undefweak
:
10081 if (h
->forced_local
)
10083 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
10084 /* Turn off visibility on local symbol. */
10085 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
10087 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10088 else if (h
->unique_global
&& h
->def_regular
)
10089 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
10090 else if (h
->root
.type
== bfd_link_hash_undefweak
10091 || h
->root
.type
== bfd_link_hash_defweak
)
10092 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
10094 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
10095 sym
.st_target_internal
= h
->target_internal
;
10097 /* Give the processor backend a chance to tweak the symbol value,
10098 and also to finish up anything that needs to be done for this
10099 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10100 forced local syms when non-shared is due to a historical quirk.
10101 STT_GNU_IFUNC symbol must go through PLT. */
10102 if ((h
->type
== STT_GNU_IFUNC
10104 && !bfd_link_relocatable (flinfo
->info
))
10105 || ((h
->dynindx
!= -1
10106 || h
->forced_local
)
10107 && ((bfd_link_pic (flinfo
->info
)
10108 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
10109 || h
->root
.type
!= bfd_link_hash_undefweak
))
10110 || !h
->forced_local
)
10111 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
10113 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
10114 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
10116 eoinfo
->failed
= TRUE
;
10121 /* If we are marking the symbol as undefined, and there are no
10122 non-weak references to this symbol from a regular object, then
10123 mark the symbol as weak undefined; if there are non-weak
10124 references, mark the symbol as strong. We can't do this earlier,
10125 because it might not be marked as undefined until the
10126 finish_dynamic_symbol routine gets through with it. */
10127 if (sym
.st_shndx
== SHN_UNDEF
10129 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
10130 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
10133 type
= ELF_ST_TYPE (sym
.st_info
);
10135 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10136 if (type
== STT_GNU_IFUNC
)
10139 if (h
->ref_regular_nonweak
)
10140 bindtype
= STB_GLOBAL
;
10142 bindtype
= STB_WEAK
;
10143 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
10146 /* If this is a symbol defined in a dynamic library, don't use the
10147 symbol size from the dynamic library. Relinking an executable
10148 against a new library may introduce gratuitous changes in the
10149 executable's symbols if we keep the size. */
10150 if (sym
.st_shndx
== SHN_UNDEF
10155 /* If a non-weak symbol with non-default visibility is not defined
10156 locally, it is a fatal error. */
10157 if (!bfd_link_relocatable (flinfo
->info
)
10158 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
10159 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
10160 && h
->root
.type
== bfd_link_hash_undefined
10161 && !h
->def_regular
)
10165 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
10166 /* xgettext:c-format */
10167 msg
= _("%pB: protected symbol `%s' isn't defined");
10168 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
10169 /* xgettext:c-format */
10170 msg
= _("%pB: internal symbol `%s' isn't defined");
10172 /* xgettext:c-format */
10173 msg
= _("%pB: hidden symbol `%s' isn't defined");
10174 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
10175 bfd_set_error (bfd_error_bad_value
);
10176 eoinfo
->failed
= TRUE
;
10180 /* If this symbol should be put in the .dynsym section, then put it
10181 there now. We already know the symbol index. We also fill in
10182 the entry in the .hash section. */
10183 if (h
->dynindx
!= -1
10184 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
10185 && elf_hash_table (flinfo
->info
)->dynsym
!= NULL
10186 && !discarded_section (elf_hash_table (flinfo
->info
)->dynsym
))
10190 /* Since there is no version information in the dynamic string,
10191 if there is no version info in symbol version section, we will
10192 have a run-time problem if not linking executable, referenced
10193 by shared library, or not bound locally. */
10194 if (h
->verinfo
.verdef
== NULL
10195 && (!bfd_link_executable (flinfo
->info
)
10197 || !h
->def_regular
))
10199 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
10201 if (p
&& p
[1] != '\0')
10204 /* xgettext:c-format */
10205 (_("%pB: no symbol version section for versioned symbol `%s'"),
10206 flinfo
->output_bfd
, h
->root
.root
.string
);
10207 eoinfo
->failed
= TRUE
;
10212 sym
.st_name
= h
->dynstr_index
;
10213 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
10214 + h
->dynindx
* bed
->s
->sizeof_sym
);
10215 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
10217 eoinfo
->failed
= TRUE
;
10220 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
10222 if (flinfo
->hash_sec
!= NULL
)
10224 size_t hash_entry_size
;
10225 bfd_byte
*bucketpos
;
10227 size_t bucketcount
;
10230 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
10231 bucket
= h
->u
.elf_hash_value
% bucketcount
;
10234 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
10235 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
10236 + (bucket
+ 2) * hash_entry_size
);
10237 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
10238 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
10240 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
10241 ((bfd_byte
*) flinfo
->hash_sec
->contents
10242 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
10245 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
10247 Elf_Internal_Versym iversym
;
10248 Elf_External_Versym
*eversym
;
10250 if (!h
->def_regular
)
10252 if (h
->verinfo
.verdef
== NULL
10253 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
10254 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
10255 iversym
.vs_vers
= 0;
10257 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
10261 if (h
->verinfo
.vertree
== NULL
)
10262 iversym
.vs_vers
= 1;
10264 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
10265 if (flinfo
->info
->create_default_symver
)
10269 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10270 defined locally. */
10271 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
10272 iversym
.vs_vers
|= VERSYM_HIDDEN
;
10274 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
10275 eversym
+= h
->dynindx
;
10276 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
10280 /* If the symbol is undefined, and we didn't output it to .dynsym,
10281 strip it from .symtab too. Obviously we can't do this for
10282 relocatable output or when needed for --emit-relocs. */
10283 else if (input_sec
== bfd_und_section_ptr
10285 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10286 && (h
->mark
!= 1 || ELF_ST_BIND (sym
.st_info
) != STB_GLOBAL
)
10287 && !bfd_link_relocatable (flinfo
->info
))
10290 /* Also strip others that we couldn't earlier due to dynamic symbol
10294 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
10297 /* Output a FILE symbol so that following locals are not associated
10298 with the wrong input file. We need one for forced local symbols
10299 if we've seen more than one FILE symbol or when we have exactly
10300 one FILE symbol but global symbols are present in a file other
10301 than the one with the FILE symbol. We also need one if linker
10302 defined symbols are present. In practice these conditions are
10303 always met, so just emit the FILE symbol unconditionally. */
10304 if (eoinfo
->localsyms
10305 && !eoinfo
->file_sym_done
10306 && eoinfo
->flinfo
->filesym_count
!= 0)
10308 Elf_Internal_Sym fsym
;
10310 memset (&fsym
, 0, sizeof (fsym
));
10311 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10312 fsym
.st_shndx
= SHN_ABS
;
10313 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
10314 bfd_und_section_ptr
, NULL
))
10317 eoinfo
->file_sym_done
= TRUE
;
10320 indx
= bfd_get_symcount (flinfo
->output_bfd
);
10321 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
10325 eoinfo
->failed
= TRUE
;
10330 else if (h
->indx
== -2)
10336 /* Return TRUE if special handling is done for relocs in SEC against
10337 symbols defined in discarded sections. */
10340 elf_section_ignore_discarded_relocs (asection
*sec
)
10342 const struct elf_backend_data
*bed
;
10344 switch (sec
->sec_info_type
)
10346 case SEC_INFO_TYPE_STABS
:
10347 case SEC_INFO_TYPE_EH_FRAME
:
10348 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10354 bed
= get_elf_backend_data (sec
->owner
);
10355 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
10356 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
10362 /* Return a mask saying how ld should treat relocations in SEC against
10363 symbols defined in discarded sections. If this function returns
10364 COMPLAIN set, ld will issue a warning message. If this function
10365 returns PRETEND set, and the discarded section was link-once and the
10366 same size as the kept link-once section, ld will pretend that the
10367 symbol was actually defined in the kept section. Otherwise ld will
10368 zero the reloc (at least that is the intent, but some cooperation by
10369 the target dependent code is needed, particularly for REL targets). */
10372 _bfd_elf_default_action_discarded (asection
*sec
)
10374 if (sec
->flags
& SEC_DEBUGGING
)
10377 if (strcmp (".eh_frame", sec
->name
) == 0)
10380 if (strcmp (".gcc_except_table", sec
->name
) == 0)
10383 return COMPLAIN
| PRETEND
;
10386 /* Find a match between a section and a member of a section group. */
10389 match_group_member (asection
*sec
, asection
*group
,
10390 struct bfd_link_info
*info
)
10392 asection
*first
= elf_next_in_group (group
);
10393 asection
*s
= first
;
10397 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
10400 s
= elf_next_in_group (s
);
10408 /* Check if the kept section of a discarded section SEC can be used
10409 to replace it. Return the replacement if it is OK. Otherwise return
10413 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
10417 kept
= sec
->kept_section
;
10420 if ((kept
->flags
& SEC_GROUP
) != 0)
10421 kept
= match_group_member (sec
, kept
, info
);
10423 && ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
10424 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
)))
10426 sec
->kept_section
= kept
;
10431 /* Link an input file into the linker output file. This function
10432 handles all the sections and relocations of the input file at once.
10433 This is so that we only have to read the local symbols once, and
10434 don't have to keep them in memory. */
10437 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
10439 int (*relocate_section
)
10440 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
10441 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
10443 Elf_Internal_Shdr
*symtab_hdr
;
10444 size_t locsymcount
;
10446 Elf_Internal_Sym
*isymbuf
;
10447 Elf_Internal_Sym
*isym
;
10448 Elf_Internal_Sym
*isymend
;
10450 asection
**ppsection
;
10452 const struct elf_backend_data
*bed
;
10453 struct elf_link_hash_entry
**sym_hashes
;
10454 bfd_size_type address_size
;
10455 bfd_vma r_type_mask
;
10457 bfd_boolean have_file_sym
= FALSE
;
10459 output_bfd
= flinfo
->output_bfd
;
10460 bed
= get_elf_backend_data (output_bfd
);
10461 relocate_section
= bed
->elf_backend_relocate_section
;
10463 /* If this is a dynamic object, we don't want to do anything here:
10464 we don't want the local symbols, and we don't want the section
10466 if ((input_bfd
->flags
& DYNAMIC
) != 0)
10469 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
10470 if (elf_bad_symtab (input_bfd
))
10472 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10477 locsymcount
= symtab_hdr
->sh_info
;
10478 extsymoff
= symtab_hdr
->sh_info
;
10481 /* Read the local symbols. */
10482 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10483 if (isymbuf
== NULL
&& locsymcount
!= 0)
10485 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
10486 flinfo
->internal_syms
,
10487 flinfo
->external_syms
,
10488 flinfo
->locsym_shndx
);
10489 if (isymbuf
== NULL
)
10493 /* Find local symbol sections and adjust values of symbols in
10494 SEC_MERGE sections. Write out those local symbols we know are
10495 going into the output file. */
10496 isymend
= isymbuf
+ locsymcount
;
10497 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
10499 isym
++, pindex
++, ppsection
++)
10503 Elf_Internal_Sym osym
;
10509 if (elf_bad_symtab (input_bfd
))
10511 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
10518 if (isym
->st_shndx
== SHN_UNDEF
)
10519 isec
= bfd_und_section_ptr
;
10520 else if (isym
->st_shndx
== SHN_ABS
)
10521 isec
= bfd_abs_section_ptr
;
10522 else if (isym
->st_shndx
== SHN_COMMON
)
10523 isec
= bfd_com_section_ptr
;
10526 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
10529 /* Don't attempt to output symbols with st_shnx in the
10530 reserved range other than SHN_ABS and SHN_COMMON. */
10531 isec
= bfd_und_section_ptr
;
10533 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
10534 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
10536 _bfd_merged_section_offset (output_bfd
, &isec
,
10537 elf_section_data (isec
)->sec_info
,
10543 /* Don't output the first, undefined, symbol. In fact, don't
10544 output any undefined local symbol. */
10545 if (isec
== bfd_und_section_ptr
)
10548 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
10550 /* We never output section symbols. Instead, we use the
10551 section symbol of the corresponding section in the output
10556 /* If we are stripping all symbols, we don't want to output this
10558 if (flinfo
->info
->strip
== strip_all
)
10561 /* If we are discarding all local symbols, we don't want to
10562 output this one. If we are generating a relocatable output
10563 file, then some of the local symbols may be required by
10564 relocs; we output them below as we discover that they are
10566 if (flinfo
->info
->discard
== discard_all
)
10569 /* If this symbol is defined in a section which we are
10570 discarding, we don't need to keep it. */
10571 if (isym
->st_shndx
!= SHN_UNDEF
10572 && isym
->st_shndx
< SHN_LORESERVE
10573 && bfd_section_removed_from_list (output_bfd
,
10574 isec
->output_section
))
10577 /* Get the name of the symbol. */
10578 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
10583 /* See if we are discarding symbols with this name. */
10584 if ((flinfo
->info
->strip
== strip_some
10585 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
10587 || (((flinfo
->info
->discard
== discard_sec_merge
10588 && (isec
->flags
& SEC_MERGE
)
10589 && !bfd_link_relocatable (flinfo
->info
))
10590 || flinfo
->info
->discard
== discard_l
)
10591 && bfd_is_local_label_name (input_bfd
, name
)))
10594 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
10596 if (input_bfd
->lto_output
)
10597 /* -flto puts a temp file name here. This means builds
10598 are not reproducible. Discard the symbol. */
10600 have_file_sym
= TRUE
;
10601 flinfo
->filesym_count
+= 1;
10603 if (!have_file_sym
)
10605 /* In the absence of debug info, bfd_find_nearest_line uses
10606 FILE symbols to determine the source file for local
10607 function symbols. Provide a FILE symbol here if input
10608 files lack such, so that their symbols won't be
10609 associated with a previous input file. It's not the
10610 source file, but the best we can do. */
10611 have_file_sym
= TRUE
;
10612 flinfo
->filesym_count
+= 1;
10613 memset (&osym
, 0, sizeof (osym
));
10614 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10615 osym
.st_shndx
= SHN_ABS
;
10616 if (!elf_link_output_symstrtab (flinfo
,
10617 (input_bfd
->lto_output
? NULL
10618 : input_bfd
->filename
),
10619 &osym
, bfd_abs_section_ptr
,
10626 /* Adjust the section index for the output file. */
10627 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10628 isec
->output_section
);
10629 if (osym
.st_shndx
== SHN_BAD
)
10632 /* ELF symbols in relocatable files are section relative, but
10633 in executable files they are virtual addresses. Note that
10634 this code assumes that all ELF sections have an associated
10635 BFD section with a reasonable value for output_offset; below
10636 we assume that they also have a reasonable value for
10637 output_section. Any special sections must be set up to meet
10638 these requirements. */
10639 osym
.st_value
+= isec
->output_offset
;
10640 if (!bfd_link_relocatable (flinfo
->info
))
10642 osym
.st_value
+= isec
->output_section
->vma
;
10643 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
10645 /* STT_TLS symbols are relative to PT_TLS segment base. */
10646 if (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
)
10647 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
10649 osym
.st_info
= ELF_ST_INFO (ELF_ST_BIND (osym
.st_info
),
10654 indx
= bfd_get_symcount (output_bfd
);
10655 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
10662 if (bed
->s
->arch_size
== 32)
10664 r_type_mask
= 0xff;
10670 r_type_mask
= 0xffffffff;
10675 /* Relocate the contents of each section. */
10676 sym_hashes
= elf_sym_hashes (input_bfd
);
10677 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
10679 bfd_byte
*contents
;
10681 if (! o
->linker_mark
)
10683 /* This section was omitted from the link. */
10687 if (!flinfo
->info
->resolve_section_groups
10688 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
10690 /* Deal with the group signature symbol. */
10691 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
10692 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
10693 asection
*osec
= o
->output_section
;
10695 BFD_ASSERT (bfd_link_relocatable (flinfo
->info
));
10696 if (symndx
>= locsymcount
10697 || (elf_bad_symtab (input_bfd
)
10698 && flinfo
->sections
[symndx
] == NULL
))
10700 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
10701 while (h
->root
.type
== bfd_link_hash_indirect
10702 || h
->root
.type
== bfd_link_hash_warning
)
10703 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10704 /* Arrange for symbol to be output. */
10706 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
10708 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
10710 /* We'll use the output section target_index. */
10711 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10712 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
10716 if (flinfo
->indices
[symndx
] == -1)
10718 /* Otherwise output the local symbol now. */
10719 Elf_Internal_Sym sym
= isymbuf
[symndx
];
10720 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10725 name
= bfd_elf_string_from_elf_section (input_bfd
,
10726 symtab_hdr
->sh_link
,
10731 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10733 if (sym
.st_shndx
== SHN_BAD
)
10736 sym
.st_value
+= o
->output_offset
;
10738 indx
= bfd_get_symcount (output_bfd
);
10739 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
10744 flinfo
->indices
[symndx
] = indx
;
10748 elf_section_data (osec
)->this_hdr
.sh_info
10749 = flinfo
->indices
[symndx
];
10753 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
10754 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
10757 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
10759 /* Section was created by _bfd_elf_link_create_dynamic_sections
10764 /* Get the contents of the section. They have been cached by a
10765 relaxation routine. Note that o is a section in an input
10766 file, so the contents field will not have been set by any of
10767 the routines which work on output files. */
10768 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
10770 contents
= elf_section_data (o
)->this_hdr
.contents
;
10771 if (bed
->caches_rawsize
10773 && o
->rawsize
< o
->size
)
10775 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
10776 contents
= flinfo
->contents
;
10781 contents
= flinfo
->contents
;
10782 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
10786 if ((o
->flags
& SEC_RELOC
) != 0)
10788 Elf_Internal_Rela
*internal_relocs
;
10789 Elf_Internal_Rela
*rel
, *relend
;
10790 int action_discarded
;
10793 /* Get the swapped relocs. */
10795 = _bfd_elf_link_read_relocs (input_bfd
, o
, flinfo
->external_relocs
,
10796 flinfo
->internal_relocs
, FALSE
);
10797 if (internal_relocs
== NULL
10798 && o
->reloc_count
> 0)
10801 /* We need to reverse-copy input .ctors/.dtors sections if
10802 they are placed in .init_array/.finit_array for output. */
10803 if (o
->size
> address_size
10804 && ((strncmp (o
->name
, ".ctors", 6) == 0
10805 && strcmp (o
->output_section
->name
,
10806 ".init_array") == 0)
10807 || (strncmp (o
->name
, ".dtors", 6) == 0
10808 && strcmp (o
->output_section
->name
,
10809 ".fini_array") == 0))
10810 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
10812 if (o
->size
* bed
->s
->int_rels_per_ext_rel
10813 != o
->reloc_count
* address_size
)
10816 /* xgettext:c-format */
10817 (_("error: %pB: size of section %pA is not "
10818 "multiple of address size"),
10820 bfd_set_error (bfd_error_bad_value
);
10823 o
->flags
|= SEC_ELF_REVERSE_COPY
;
10826 action_discarded
= -1;
10827 if (!elf_section_ignore_discarded_relocs (o
))
10828 action_discarded
= (*bed
->action_discarded
) (o
);
10830 /* Run through the relocs evaluating complex reloc symbols and
10831 looking for relocs against symbols from discarded sections
10832 or section symbols from removed link-once sections.
10833 Complain about relocs against discarded sections. Zero
10834 relocs against removed link-once sections. */
10836 rel
= internal_relocs
;
10837 relend
= rel
+ o
->reloc_count
;
10838 for ( ; rel
< relend
; rel
++)
10840 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
10841 unsigned int s_type
;
10842 asection
**ps
, *sec
;
10843 struct elf_link_hash_entry
*h
= NULL
;
10844 const char *sym_name
;
10846 if (r_symndx
== STN_UNDEF
)
10849 if (r_symndx
>= locsymcount
10850 || (elf_bad_symtab (input_bfd
)
10851 && flinfo
->sections
[r_symndx
] == NULL
))
10853 h
= sym_hashes
[r_symndx
- extsymoff
];
10855 /* Badly formatted input files can contain relocs that
10856 reference non-existant symbols. Check here so that
10857 we do not seg fault. */
10861 /* xgettext:c-format */
10862 (_("error: %pB contains a reloc (%#" PRIx64
") for section %pA "
10863 "that references a non-existent global symbol"),
10864 input_bfd
, (uint64_t) rel
->r_info
, o
);
10865 bfd_set_error (bfd_error_bad_value
);
10869 while (h
->root
.type
== bfd_link_hash_indirect
10870 || h
->root
.type
== bfd_link_hash_warning
)
10871 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10875 /* If a plugin symbol is referenced from a non-IR file,
10876 mark the symbol as undefined. Note that the
10877 linker may attach linker created dynamic sections
10878 to the plugin bfd. Symbols defined in linker
10879 created sections are not plugin symbols. */
10880 if ((h
->root
.non_ir_ref_regular
10881 || h
->root
.non_ir_ref_dynamic
)
10882 && (h
->root
.type
== bfd_link_hash_defined
10883 || h
->root
.type
== bfd_link_hash_defweak
)
10884 && (h
->root
.u
.def
.section
->flags
10885 & SEC_LINKER_CREATED
) == 0
10886 && h
->root
.u
.def
.section
->owner
!= NULL
10887 && (h
->root
.u
.def
.section
->owner
->flags
10888 & BFD_PLUGIN
) != 0)
10890 h
->root
.type
= bfd_link_hash_undefined
;
10891 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
10895 if (h
->root
.type
== bfd_link_hash_defined
10896 || h
->root
.type
== bfd_link_hash_defweak
)
10897 ps
= &h
->root
.u
.def
.section
;
10899 sym_name
= h
->root
.root
.string
;
10903 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
10905 s_type
= ELF_ST_TYPE (sym
->st_info
);
10906 ps
= &flinfo
->sections
[r_symndx
];
10907 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
10911 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
10912 && !bfd_link_relocatable (flinfo
->info
))
10915 bfd_vma dot
= (rel
->r_offset
10916 + o
->output_offset
+ o
->output_section
->vma
);
10918 printf ("Encountered a complex symbol!");
10919 printf (" (input_bfd %s, section %s, reloc %ld\n",
10920 input_bfd
->filename
, o
->name
,
10921 (long) (rel
- internal_relocs
));
10922 printf (" symbol: idx %8.8lx, name %s\n",
10923 r_symndx
, sym_name
);
10924 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10925 (unsigned long) rel
->r_info
,
10926 (unsigned long) rel
->r_offset
);
10928 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
10929 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
10932 /* Symbol evaluated OK. Update to absolute value. */
10933 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
10938 if (action_discarded
!= -1 && ps
!= NULL
)
10940 /* Complain if the definition comes from a
10941 discarded section. */
10942 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
10944 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
10945 if (action_discarded
& COMPLAIN
)
10946 (*flinfo
->info
->callbacks
->einfo
)
10947 /* xgettext:c-format */
10948 (_("%X`%s' referenced in section `%pA' of %pB: "
10949 "defined in discarded section `%pA' of %pB\n"),
10950 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
10952 /* Try to do the best we can to support buggy old
10953 versions of gcc. Pretend that the symbol is
10954 really defined in the kept linkonce section.
10955 FIXME: This is quite broken. Modifying the
10956 symbol here means we will be changing all later
10957 uses of the symbol, not just in this section. */
10958 if (action_discarded
& PRETEND
)
10962 kept
= _bfd_elf_check_kept_section (sec
,
10974 /* Relocate the section by invoking a back end routine.
10976 The back end routine is responsible for adjusting the
10977 section contents as necessary, and (if using Rela relocs
10978 and generating a relocatable output file) adjusting the
10979 reloc addend as necessary.
10981 The back end routine does not have to worry about setting
10982 the reloc address or the reloc symbol index.
10984 The back end routine is given a pointer to the swapped in
10985 internal symbols, and can access the hash table entries
10986 for the external symbols via elf_sym_hashes (input_bfd).
10988 When generating relocatable output, the back end routine
10989 must handle STB_LOCAL/STT_SECTION symbols specially. The
10990 output symbol is going to be a section symbol
10991 corresponding to the output section, which will require
10992 the addend to be adjusted. */
10994 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
10995 input_bfd
, o
, contents
,
11003 || bfd_link_relocatable (flinfo
->info
)
11004 || flinfo
->info
->emitrelocations
)
11006 Elf_Internal_Rela
*irela
;
11007 Elf_Internal_Rela
*irelaend
, *irelamid
;
11008 bfd_vma last_offset
;
11009 struct elf_link_hash_entry
**rel_hash
;
11010 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
11011 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
11012 unsigned int next_erel
;
11013 bfd_boolean rela_normal
;
11014 struct bfd_elf_section_data
*esdi
, *esdo
;
11016 esdi
= elf_section_data (o
);
11017 esdo
= elf_section_data (o
->output_section
);
11018 rela_normal
= FALSE
;
11020 /* Adjust the reloc addresses and symbol indices. */
11022 irela
= internal_relocs
;
11023 irelaend
= irela
+ o
->reloc_count
;
11024 rel_hash
= esdo
->rel
.hashes
+ esdo
->rel
.count
;
11025 /* We start processing the REL relocs, if any. When we reach
11026 IRELAMID in the loop, we switch to the RELA relocs. */
11028 if (esdi
->rel
.hdr
!= NULL
)
11029 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
11030 * bed
->s
->int_rels_per_ext_rel
);
11031 rel_hash_list
= rel_hash
;
11032 rela_hash_list
= NULL
;
11033 last_offset
= o
->output_offset
;
11034 if (!bfd_link_relocatable (flinfo
->info
))
11035 last_offset
+= o
->output_section
->vma
;
11036 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
11038 unsigned long r_symndx
;
11040 Elf_Internal_Sym sym
;
11042 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
11048 if (irela
== irelamid
)
11050 rel_hash
= esdo
->rela
.hashes
+ esdo
->rela
.count
;
11051 rela_hash_list
= rel_hash
;
11052 rela_normal
= bed
->rela_normal
;
11055 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
11058 if (irela
->r_offset
>= (bfd_vma
) -2)
11060 /* This is a reloc for a deleted entry or somesuch.
11061 Turn it into an R_*_NONE reloc, at the same
11062 offset as the last reloc. elf_eh_frame.c and
11063 bfd_elf_discard_info rely on reloc offsets
11065 irela
->r_offset
= last_offset
;
11067 irela
->r_addend
= 0;
11071 irela
->r_offset
+= o
->output_offset
;
11073 /* Relocs in an executable have to be virtual addresses. */
11074 if (!bfd_link_relocatable (flinfo
->info
))
11075 irela
->r_offset
+= o
->output_section
->vma
;
11077 last_offset
= irela
->r_offset
;
11079 r_symndx
= irela
->r_info
>> r_sym_shift
;
11080 if (r_symndx
== STN_UNDEF
)
11083 if (r_symndx
>= locsymcount
11084 || (elf_bad_symtab (input_bfd
)
11085 && flinfo
->sections
[r_symndx
] == NULL
))
11087 struct elf_link_hash_entry
*rh
;
11088 unsigned long indx
;
11090 /* This is a reloc against a global symbol. We
11091 have not yet output all the local symbols, so
11092 we do not know the symbol index of any global
11093 symbol. We set the rel_hash entry for this
11094 reloc to point to the global hash table entry
11095 for this symbol. The symbol index is then
11096 set at the end of bfd_elf_final_link. */
11097 indx
= r_symndx
- extsymoff
;
11098 rh
= elf_sym_hashes (input_bfd
)[indx
];
11099 while (rh
->root
.type
== bfd_link_hash_indirect
11100 || rh
->root
.type
== bfd_link_hash_warning
)
11101 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
11103 /* Setting the index to -2 tells
11104 elf_link_output_extsym that this symbol is
11105 used by a reloc. */
11106 BFD_ASSERT (rh
->indx
< 0);
11113 /* This is a reloc against a local symbol. */
11116 sym
= isymbuf
[r_symndx
];
11117 sec
= flinfo
->sections
[r_symndx
];
11118 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
11120 /* I suppose the backend ought to fill in the
11121 section of any STT_SECTION symbol against a
11122 processor specific section. */
11123 r_symndx
= STN_UNDEF
;
11124 if (bfd_is_abs_section (sec
))
11126 else if (sec
== NULL
|| sec
->owner
== NULL
)
11128 bfd_set_error (bfd_error_bad_value
);
11133 asection
*osec
= sec
->output_section
;
11135 /* If we have discarded a section, the output
11136 section will be the absolute section. In
11137 case of discarded SEC_MERGE sections, use
11138 the kept section. relocate_section should
11139 have already handled discarded linkonce
11141 if (bfd_is_abs_section (osec
)
11142 && sec
->kept_section
!= NULL
11143 && sec
->kept_section
->output_section
!= NULL
)
11145 osec
= sec
->kept_section
->output_section
;
11146 irela
->r_addend
-= osec
->vma
;
11149 if (!bfd_is_abs_section (osec
))
11151 r_symndx
= osec
->target_index
;
11152 if (r_symndx
== STN_UNDEF
)
11154 irela
->r_addend
+= osec
->vma
;
11155 osec
= _bfd_nearby_section (output_bfd
, osec
,
11157 irela
->r_addend
-= osec
->vma
;
11158 r_symndx
= osec
->target_index
;
11163 /* Adjust the addend according to where the
11164 section winds up in the output section. */
11166 irela
->r_addend
+= sec
->output_offset
;
11170 if (flinfo
->indices
[r_symndx
] == -1)
11172 unsigned long shlink
;
11177 if (flinfo
->info
->strip
== strip_all
)
11179 /* You can't do ld -r -s. */
11180 bfd_set_error (bfd_error_invalid_operation
);
11184 /* This symbol was skipped earlier, but
11185 since it is needed by a reloc, we
11186 must output it now. */
11187 shlink
= symtab_hdr
->sh_link
;
11188 name
= (bfd_elf_string_from_elf_section
11189 (input_bfd
, shlink
, sym
.st_name
));
11193 osec
= sec
->output_section
;
11195 _bfd_elf_section_from_bfd_section (output_bfd
,
11197 if (sym
.st_shndx
== SHN_BAD
)
11200 sym
.st_value
+= sec
->output_offset
;
11201 if (!bfd_link_relocatable (flinfo
->info
))
11203 sym
.st_value
+= osec
->vma
;
11204 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
11206 struct elf_link_hash_table
*htab
11207 = elf_hash_table (flinfo
->info
);
11209 /* STT_TLS symbols are relative to PT_TLS
11211 if (htab
->tls_sec
!= NULL
)
11212 sym
.st_value
-= htab
->tls_sec
->vma
;
11215 = ELF_ST_INFO (ELF_ST_BIND (sym
.st_info
),
11220 indx
= bfd_get_symcount (output_bfd
);
11221 ret
= elf_link_output_symstrtab (flinfo
, name
,
11227 flinfo
->indices
[r_symndx
] = indx
;
11232 r_symndx
= flinfo
->indices
[r_symndx
];
11235 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
11236 | (irela
->r_info
& r_type_mask
));
11239 /* Swap out the relocs. */
11240 input_rel_hdr
= esdi
->rel
.hdr
;
11241 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
11243 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11248 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
11249 * bed
->s
->int_rels_per_ext_rel
);
11250 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
11253 input_rela_hdr
= esdi
->rela
.hdr
;
11254 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
11256 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11265 /* Write out the modified section contents. */
11266 if (bed
->elf_backend_write_section
11267 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
11270 /* Section written out. */
11272 else switch (o
->sec_info_type
)
11274 case SEC_INFO_TYPE_STABS
:
11275 if (! (_bfd_write_section_stabs
11277 &elf_hash_table (flinfo
->info
)->stab_info
,
11278 o
, &elf_section_data (o
)->sec_info
, contents
)))
11281 case SEC_INFO_TYPE_MERGE
:
11282 if (! _bfd_write_merged_section (output_bfd
, o
,
11283 elf_section_data (o
)->sec_info
))
11286 case SEC_INFO_TYPE_EH_FRAME
:
11288 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
11293 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
11295 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
11303 if (! (o
->flags
& SEC_EXCLUDE
))
11305 file_ptr offset
= (file_ptr
) o
->output_offset
;
11306 bfd_size_type todo
= o
->size
;
11308 offset
*= bfd_octets_per_byte (output_bfd
, NULL
);
11310 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
11312 /* Reverse-copy input section to output. */
11315 todo
-= address_size
;
11316 if (! bfd_set_section_contents (output_bfd
,
11324 offset
+= address_size
;
11328 else if (! bfd_set_section_contents (output_bfd
,
11342 /* Generate a reloc when linking an ELF file. This is a reloc
11343 requested by the linker, and does not come from any input file. This
11344 is used to build constructor and destructor tables when linking
11348 elf_reloc_link_order (bfd
*output_bfd
,
11349 struct bfd_link_info
*info
,
11350 asection
*output_section
,
11351 struct bfd_link_order
*link_order
)
11353 reloc_howto_type
*howto
;
11357 struct bfd_elf_section_reloc_data
*reldata
;
11358 struct elf_link_hash_entry
**rel_hash_ptr
;
11359 Elf_Internal_Shdr
*rel_hdr
;
11360 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
11361 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
11364 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
11366 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
11369 bfd_set_error (bfd_error_bad_value
);
11373 addend
= link_order
->u
.reloc
.p
->addend
;
11376 reldata
= &esdo
->rel
;
11377 else if (esdo
->rela
.hdr
)
11378 reldata
= &esdo
->rela
;
11385 /* Figure out the symbol index. */
11386 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
11387 if (link_order
->type
== bfd_section_reloc_link_order
)
11389 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
11390 BFD_ASSERT (indx
!= 0);
11391 *rel_hash_ptr
= NULL
;
11395 struct elf_link_hash_entry
*h
;
11397 /* Treat a reloc against a defined symbol as though it were
11398 actually against the section. */
11399 h
= ((struct elf_link_hash_entry
*)
11400 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
11401 link_order
->u
.reloc
.p
->u
.name
,
11402 FALSE
, FALSE
, TRUE
));
11404 && (h
->root
.type
== bfd_link_hash_defined
11405 || h
->root
.type
== bfd_link_hash_defweak
))
11409 section
= h
->root
.u
.def
.section
;
11410 indx
= section
->output_section
->target_index
;
11411 *rel_hash_ptr
= NULL
;
11412 /* It seems that we ought to add the symbol value to the
11413 addend here, but in practice it has already been added
11414 because it was passed to constructor_callback. */
11415 addend
+= section
->output_section
->vma
+ section
->output_offset
;
11417 else if (h
!= NULL
)
11419 /* Setting the index to -2 tells elf_link_output_extsym that
11420 this symbol is used by a reloc. */
11427 (*info
->callbacks
->unattached_reloc
)
11428 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
11433 /* If this is an inplace reloc, we must write the addend into the
11435 if (howto
->partial_inplace
&& addend
!= 0)
11437 bfd_size_type size
;
11438 bfd_reloc_status_type rstat
;
11441 const char *sym_name
;
11443 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
11444 buf
= (bfd_byte
*) bfd_zmalloc (size
);
11445 if (buf
== NULL
&& size
!= 0)
11447 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
11454 case bfd_reloc_outofrange
:
11457 case bfd_reloc_overflow
:
11458 if (link_order
->type
== bfd_section_reloc_link_order
)
11459 sym_name
= bfd_section_name (link_order
->u
.reloc
.p
->u
.section
);
11461 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
11462 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
11463 howto
->name
, addend
, NULL
, NULL
,
11468 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
11470 * bfd_octets_per_byte (output_bfd
, NULL
),
11477 /* The address of a reloc is relative to the section in a
11478 relocatable file, and is a virtual address in an executable
11480 offset
= link_order
->offset
;
11481 if (! bfd_link_relocatable (info
))
11482 offset
+= output_section
->vma
;
11484 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
11486 irel
[i
].r_offset
= offset
;
11487 irel
[i
].r_info
= 0;
11488 irel
[i
].r_addend
= 0;
11490 if (bed
->s
->arch_size
== 32)
11491 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
11493 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
11495 rel_hdr
= reldata
->hdr
;
11496 erel
= rel_hdr
->contents
;
11497 if (rel_hdr
->sh_type
== SHT_REL
)
11499 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
11500 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
11504 irel
[0].r_addend
= addend
;
11505 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
11506 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
11515 /* Compare two sections based on the locations of the sections they are
11516 linked to. Used by elf_fixup_link_order. */
11519 compare_link_order (const void *a
, const void *b
)
11521 const struct bfd_link_order
*alo
= *(const struct bfd_link_order
**) a
;
11522 const struct bfd_link_order
*blo
= *(const struct bfd_link_order
**) b
;
11523 asection
*asec
= elf_linked_to_section (alo
->u
.indirect
.section
);
11524 asection
*bsec
= elf_linked_to_section (blo
->u
.indirect
.section
);
11525 bfd_vma apos
= asec
->output_section
->lma
+ asec
->output_offset
;
11526 bfd_vma bpos
= bsec
->output_section
->lma
+ bsec
->output_offset
;
11533 /* The only way we should get matching LMAs is when the first of two
11534 sections has zero size. */
11535 if (asec
->size
< bsec
->size
)
11537 if (asec
->size
> bsec
->size
)
11540 /* If they are both zero size then they almost certainly have the same
11541 VMA and thus are not ordered with respect to each other. Test VMA
11542 anyway, and fall back to id to make the result reproducible across
11543 qsort implementations. */
11544 apos
= asec
->output_section
->vma
+ asec
->output_offset
;
11545 bpos
= bsec
->output_section
->vma
+ bsec
->output_offset
;
11551 return asec
->id
- bsec
->id
;
11555 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11556 order as their linked sections. Returns false if this could not be done
11557 because an output section includes both ordered and unordered
11558 sections. Ideally we'd do this in the linker proper. */
11561 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
11563 size_t seen_linkorder
;
11566 struct bfd_link_order
*p
;
11568 struct bfd_link_order
**sections
;
11569 asection
*s
, *other_sec
, *linkorder_sec
;
11573 linkorder_sec
= NULL
;
11575 seen_linkorder
= 0;
11576 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11578 if (p
->type
== bfd_indirect_link_order
)
11580 s
= p
->u
.indirect
.section
;
11582 if ((s
->flags
& SEC_LINKER_CREATED
) == 0
11583 && bfd_get_flavour (sub
) == bfd_target_elf_flavour
11584 && elf_section_data (s
) != NULL
11585 && elf_linked_to_section (s
) != NULL
)
11599 if (seen_other
&& seen_linkorder
)
11601 if (other_sec
&& linkorder_sec
)
11603 /* xgettext:c-format */
11604 (_("%pA has both ordered [`%pA' in %pB] "
11605 "and unordered [`%pA' in %pB] sections"),
11606 o
, linkorder_sec
, linkorder_sec
->owner
,
11607 other_sec
, other_sec
->owner
);
11610 (_("%pA has both ordered and unordered sections"), o
);
11611 bfd_set_error (bfd_error_bad_value
);
11616 if (!seen_linkorder
)
11619 sections
= bfd_malloc (seen_linkorder
* sizeof (*sections
));
11620 if (sections
== NULL
)
11623 seen_linkorder
= 0;
11624 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11625 sections
[seen_linkorder
++] = p
;
11627 /* Sort the input sections in the order of their linked section. */
11628 qsort (sections
, seen_linkorder
, sizeof (*sections
), compare_link_order
);
11630 /* Change the offsets of the sections. */
11632 for (n
= 0; n
< seen_linkorder
; n
++)
11635 s
= sections
[n
]->u
.indirect
.section
;
11636 mask
= ~(bfd_vma
) 0 << s
->alignment_power
;
11637 offset
= (offset
+ ~mask
) & mask
;
11638 s
->output_offset
= offset
/ bfd_octets_per_byte (abfd
, NULL
);
11639 sections
[n
]->offset
= offset
;
11640 offset
+= sections
[n
]->size
;
11647 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11648 Returns TRUE upon success, FALSE otherwise. */
11651 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
11653 bfd_boolean ret
= FALSE
;
11655 const struct elf_backend_data
*bed
;
11657 enum bfd_architecture arch
;
11659 asymbol
**sympp
= NULL
;
11663 elf_symbol_type
*osymbuf
;
11665 implib_bfd
= info
->out_implib_bfd
;
11666 bed
= get_elf_backend_data (abfd
);
11668 if (!bfd_set_format (implib_bfd
, bfd_object
))
11671 /* Use flag from executable but make it a relocatable object. */
11672 flags
= bfd_get_file_flags (abfd
);
11673 flags
&= ~HAS_RELOC
;
11674 if (!bfd_set_start_address (implib_bfd
, 0)
11675 || !bfd_set_file_flags (implib_bfd
, flags
& ~EXEC_P
))
11678 /* Copy architecture of output file to import library file. */
11679 arch
= bfd_get_arch (abfd
);
11680 mach
= bfd_get_mach (abfd
);
11681 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
11682 && (abfd
->target_defaulted
11683 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
11686 /* Get symbol table size. */
11687 symsize
= bfd_get_symtab_upper_bound (abfd
);
11691 /* Read in the symbol table. */
11692 sympp
= (asymbol
**) bfd_malloc (symsize
);
11696 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
11700 /* Allow the BFD backend to copy any private header data it
11701 understands from the output BFD to the import library BFD. */
11702 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
11705 /* Filter symbols to appear in the import library. */
11706 if (bed
->elf_backend_filter_implib_symbols
)
11707 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
11710 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
11713 bfd_set_error (bfd_error_no_symbols
);
11714 _bfd_error_handler (_("%pB: no symbol found for import library"),
11720 /* Make symbols absolute. */
11721 osymbuf
= (elf_symbol_type
*) bfd_alloc2 (implib_bfd
, symcount
,
11722 sizeof (*osymbuf
));
11723 if (osymbuf
== NULL
)
11726 for (src_count
= 0; src_count
< symcount
; src_count
++)
11728 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
11729 sizeof (*osymbuf
));
11730 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
11731 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
11732 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
11733 osymbuf
[src_count
].internal_elf_sym
.st_value
=
11734 osymbuf
[src_count
].symbol
.value
;
11735 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
11738 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
11740 /* Allow the BFD backend to copy any private data it understands
11741 from the output BFD to the import library BFD. This is done last
11742 to permit the routine to look at the filtered symbol table. */
11743 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
11746 if (!bfd_close (implib_bfd
))
11757 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
11761 if (flinfo
->symstrtab
!= NULL
)
11762 _bfd_elf_strtab_free (flinfo
->symstrtab
);
11763 if (flinfo
->contents
!= NULL
)
11764 free (flinfo
->contents
);
11765 if (flinfo
->external_relocs
!= NULL
)
11766 free (flinfo
->external_relocs
);
11767 if (flinfo
->internal_relocs
!= NULL
)
11768 free (flinfo
->internal_relocs
);
11769 if (flinfo
->external_syms
!= NULL
)
11770 free (flinfo
->external_syms
);
11771 if (flinfo
->locsym_shndx
!= NULL
)
11772 free (flinfo
->locsym_shndx
);
11773 if (flinfo
->internal_syms
!= NULL
)
11774 free (flinfo
->internal_syms
);
11775 if (flinfo
->indices
!= NULL
)
11776 free (flinfo
->indices
);
11777 if (flinfo
->sections
!= NULL
)
11778 free (flinfo
->sections
);
11779 if (flinfo
->symshndxbuf
!= NULL
11780 && flinfo
->symshndxbuf
!= (Elf_External_Sym_Shndx
*) -1)
11781 free (flinfo
->symshndxbuf
);
11782 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
11784 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11785 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rel
.hashes
!= NULL
)
11786 free (esdo
->rel
.hashes
);
11787 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rela
.hashes
!= NULL
)
11788 free (esdo
->rela
.hashes
);
11792 /* Do the final step of an ELF link. */
11795 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
11797 bfd_boolean dynamic
;
11798 bfd_boolean emit_relocs
;
11800 struct elf_final_link_info flinfo
;
11802 struct bfd_link_order
*p
;
11804 bfd_size_type max_contents_size
;
11805 bfd_size_type max_external_reloc_size
;
11806 bfd_size_type max_internal_reloc_count
;
11807 bfd_size_type max_sym_count
;
11808 bfd_size_type max_sym_shndx_count
;
11809 Elf_Internal_Sym elfsym
;
11811 Elf_Internal_Shdr
*symtab_hdr
;
11812 Elf_Internal_Shdr
*symtab_shndx_hdr
;
11813 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11814 struct elf_outext_info eoinfo
;
11815 bfd_boolean merged
;
11816 size_t relativecount
= 0;
11817 asection
*reldyn
= 0;
11819 asection
*attr_section
= NULL
;
11820 bfd_vma attr_size
= 0;
11821 const char *std_attrs_section
;
11822 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
11824 if (!is_elf_hash_table (htab
))
11827 if (bfd_link_pic (info
))
11828 abfd
->flags
|= DYNAMIC
;
11830 dynamic
= htab
->dynamic_sections_created
;
11831 dynobj
= htab
->dynobj
;
11833 emit_relocs
= (bfd_link_relocatable (info
)
11834 || info
->emitrelocations
);
11836 flinfo
.info
= info
;
11837 flinfo
.output_bfd
= abfd
;
11838 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
11839 if (flinfo
.symstrtab
== NULL
)
11844 flinfo
.hash_sec
= NULL
;
11845 flinfo
.symver_sec
= NULL
;
11849 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
11850 /* Note that dynsym_sec can be NULL (on VMS). */
11851 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
11852 /* Note that it is OK if symver_sec is NULL. */
11855 flinfo
.contents
= NULL
;
11856 flinfo
.external_relocs
= NULL
;
11857 flinfo
.internal_relocs
= NULL
;
11858 flinfo
.external_syms
= NULL
;
11859 flinfo
.locsym_shndx
= NULL
;
11860 flinfo
.internal_syms
= NULL
;
11861 flinfo
.indices
= NULL
;
11862 flinfo
.sections
= NULL
;
11863 flinfo
.symshndxbuf
= NULL
;
11864 flinfo
.filesym_count
= 0;
11866 /* The object attributes have been merged. Remove the input
11867 sections from the link, and set the contents of the output
11869 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
11870 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11872 bfd_boolean remove_section
= FALSE
;
11874 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
11875 || strcmp (o
->name
, ".gnu.attributes") == 0)
11877 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11879 asection
*input_section
;
11881 if (p
->type
!= bfd_indirect_link_order
)
11883 input_section
= p
->u
.indirect
.section
;
11884 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11885 elf_link_input_bfd ignores this section. */
11886 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
11889 attr_size
= bfd_elf_obj_attr_size (abfd
);
11890 bfd_set_section_size (o
, attr_size
);
11891 /* Skip this section later on. */
11892 o
->map_head
.link_order
= NULL
;
11896 remove_section
= TRUE
;
11898 else if ((o
->flags
& SEC_GROUP
) != 0 && o
->size
== 0)
11900 /* Remove empty group section from linker output. */
11901 remove_section
= TRUE
;
11903 if (remove_section
)
11905 o
->flags
|= SEC_EXCLUDE
;
11906 bfd_section_list_remove (abfd
, o
);
11907 abfd
->section_count
--;
11911 /* Count up the number of relocations we will output for each output
11912 section, so that we know the sizes of the reloc sections. We
11913 also figure out some maximum sizes. */
11914 max_contents_size
= 0;
11915 max_external_reloc_size
= 0;
11916 max_internal_reloc_count
= 0;
11918 max_sym_shndx_count
= 0;
11920 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11922 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11923 o
->reloc_count
= 0;
11925 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11927 unsigned int reloc_count
= 0;
11928 unsigned int additional_reloc_count
= 0;
11929 struct bfd_elf_section_data
*esdi
= NULL
;
11931 if (p
->type
== bfd_section_reloc_link_order
11932 || p
->type
== bfd_symbol_reloc_link_order
)
11934 else if (p
->type
== bfd_indirect_link_order
)
11938 sec
= p
->u
.indirect
.section
;
11940 /* Mark all sections which are to be included in the
11941 link. This will normally be every section. We need
11942 to do this so that we can identify any sections which
11943 the linker has decided to not include. */
11944 sec
->linker_mark
= TRUE
;
11946 if (sec
->flags
& SEC_MERGE
)
11949 if (sec
->rawsize
> max_contents_size
)
11950 max_contents_size
= sec
->rawsize
;
11951 if (sec
->size
> max_contents_size
)
11952 max_contents_size
= sec
->size
;
11954 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
11955 && (sec
->owner
->flags
& DYNAMIC
) == 0)
11959 /* We are interested in just local symbols, not all
11961 if (elf_bad_symtab (sec
->owner
))
11962 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
11963 / bed
->s
->sizeof_sym
);
11965 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
11967 if (sym_count
> max_sym_count
)
11968 max_sym_count
= sym_count
;
11970 if (sym_count
> max_sym_shndx_count
11971 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
11972 max_sym_shndx_count
= sym_count
;
11974 if (esdo
->this_hdr
.sh_type
== SHT_REL
11975 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
11976 /* Some backends use reloc_count in relocation sections
11977 to count particular types of relocs. Of course,
11978 reloc sections themselves can't have relocations. */
11980 else if (emit_relocs
)
11982 reloc_count
= sec
->reloc_count
;
11983 if (bed
->elf_backend_count_additional_relocs
)
11986 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
11987 additional_reloc_count
+= c
;
11990 else if (bed
->elf_backend_count_relocs
)
11991 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
11993 esdi
= elf_section_data (sec
);
11995 if ((sec
->flags
& SEC_RELOC
) != 0)
11997 size_t ext_size
= 0;
11999 if (esdi
->rel
.hdr
!= NULL
)
12000 ext_size
= esdi
->rel
.hdr
->sh_size
;
12001 if (esdi
->rela
.hdr
!= NULL
)
12002 ext_size
+= esdi
->rela
.hdr
->sh_size
;
12004 if (ext_size
> max_external_reloc_size
)
12005 max_external_reloc_size
= ext_size
;
12006 if (sec
->reloc_count
> max_internal_reloc_count
)
12007 max_internal_reloc_count
= sec
->reloc_count
;
12012 if (reloc_count
== 0)
12015 reloc_count
+= additional_reloc_count
;
12016 o
->reloc_count
+= reloc_count
;
12018 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
12022 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
12023 esdo
->rel
.count
+= additional_reloc_count
;
12025 if (esdi
->rela
.hdr
)
12027 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
12028 esdo
->rela
.count
+= additional_reloc_count
;
12034 esdo
->rela
.count
+= reloc_count
;
12036 esdo
->rel
.count
+= reloc_count
;
12040 if (o
->reloc_count
> 0)
12041 o
->flags
|= SEC_RELOC
;
12044 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12045 set it (this is probably a bug) and if it is set
12046 assign_section_numbers will create a reloc section. */
12047 o
->flags
&=~ SEC_RELOC
;
12050 /* If the SEC_ALLOC flag is not set, force the section VMA to
12051 zero. This is done in elf_fake_sections as well, but forcing
12052 the VMA to 0 here will ensure that relocs against these
12053 sections are handled correctly. */
12054 if ((o
->flags
& SEC_ALLOC
) == 0
12055 && ! o
->user_set_vma
)
12059 if (! bfd_link_relocatable (info
) && merged
)
12060 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
12062 /* Figure out the file positions for everything but the symbol table
12063 and the relocs. We set symcount to force assign_section_numbers
12064 to create a symbol table. */
12065 abfd
->symcount
= info
->strip
!= strip_all
|| emit_relocs
;
12066 BFD_ASSERT (! abfd
->output_has_begun
);
12067 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
12070 /* Set sizes, and assign file positions for reloc sections. */
12071 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12073 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12074 if ((o
->flags
& SEC_RELOC
) != 0)
12077 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
12081 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
12085 /* _bfd_elf_compute_section_file_positions makes temporary use
12086 of target_index. Reset it. */
12087 o
->target_index
= 0;
12089 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12090 to count upwards while actually outputting the relocations. */
12091 esdo
->rel
.count
= 0;
12092 esdo
->rela
.count
= 0;
12094 if ((esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
12095 && !bfd_section_is_ctf (o
))
12097 /* Cache the section contents so that they can be compressed
12098 later. Use bfd_malloc since it will be freed by
12099 bfd_compress_section_contents. */
12100 unsigned char *contents
= esdo
->this_hdr
.contents
;
12101 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
12104 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
12105 if (contents
== NULL
)
12107 esdo
->this_hdr
.contents
= contents
;
12111 /* We have now assigned file positions for all the sections except .symtab,
12112 .strtab, and non-loaded reloc and compressed debugging sections. We start
12113 the .symtab section at the current file position, and write directly to it.
12114 We build the .strtab section in memory. */
12115 abfd
->symcount
= 0;
12116 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12117 /* sh_name is set in prep_headers. */
12118 symtab_hdr
->sh_type
= SHT_SYMTAB
;
12119 /* sh_flags, sh_addr and sh_size all start off zero. */
12120 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
12121 /* sh_link is set in assign_section_numbers. */
12122 /* sh_info is set below. */
12123 /* sh_offset is set just below. */
12124 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
12126 if (max_sym_count
< 20)
12127 max_sym_count
= 20;
12128 htab
->strtabsize
= max_sym_count
;
12129 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
12130 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
12131 if (htab
->strtab
== NULL
)
12133 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12135 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
12136 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
12138 if (info
->strip
!= strip_all
|| emit_relocs
)
12140 file_ptr off
= elf_next_file_pos (abfd
);
12142 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
12144 /* Note that at this point elf_next_file_pos (abfd) is
12145 incorrect. We do not yet know the size of the .symtab section.
12146 We correct next_file_pos below, after we do know the size. */
12148 /* Start writing out the symbol table. The first symbol is always a
12150 elfsym
.st_value
= 0;
12151 elfsym
.st_size
= 0;
12152 elfsym
.st_info
= 0;
12153 elfsym
.st_other
= 0;
12154 elfsym
.st_shndx
= SHN_UNDEF
;
12155 elfsym
.st_target_internal
= 0;
12156 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
12157 bfd_und_section_ptr
, NULL
) != 1)
12160 /* Output a symbol for each section. We output these even if we are
12161 discarding local symbols, since they are used for relocs. These
12162 symbols have no names. We store the index of each one in the
12163 index field of the section, so that we can find it again when
12164 outputting relocs. */
12166 elfsym
.st_size
= 0;
12167 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12168 elfsym
.st_other
= 0;
12169 elfsym
.st_value
= 0;
12170 elfsym
.st_target_internal
= 0;
12171 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12173 o
= bfd_section_from_elf_index (abfd
, i
);
12176 o
->target_index
= bfd_get_symcount (abfd
);
12177 elfsym
.st_shndx
= i
;
12178 if (!bfd_link_relocatable (info
))
12179 elfsym
.st_value
= o
->vma
;
12180 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
, o
,
12187 /* Allocate some memory to hold information read in from the input
12189 if (max_contents_size
!= 0)
12191 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
12192 if (flinfo
.contents
== NULL
)
12196 if (max_external_reloc_size
!= 0)
12198 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
12199 if (flinfo
.external_relocs
== NULL
)
12203 if (max_internal_reloc_count
!= 0)
12205 amt
= max_internal_reloc_count
* sizeof (Elf_Internal_Rela
);
12206 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
12207 if (flinfo
.internal_relocs
== NULL
)
12211 if (max_sym_count
!= 0)
12213 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
12214 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
12215 if (flinfo
.external_syms
== NULL
)
12218 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
12219 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
12220 if (flinfo
.internal_syms
== NULL
)
12223 amt
= max_sym_count
* sizeof (long);
12224 flinfo
.indices
= (long int *) bfd_malloc (amt
);
12225 if (flinfo
.indices
== NULL
)
12228 amt
= max_sym_count
* sizeof (asection
*);
12229 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
12230 if (flinfo
.sections
== NULL
)
12234 if (max_sym_shndx_count
!= 0)
12236 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
12237 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
12238 if (flinfo
.locsym_shndx
== NULL
)
12244 bfd_vma base
, end
= 0;
12247 for (sec
= htab
->tls_sec
;
12248 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
12251 bfd_size_type size
= sec
->size
;
12254 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
12256 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
12259 size
= ord
->offset
+ ord
->size
;
12261 end
= sec
->vma
+ size
;
12263 base
= htab
->tls_sec
->vma
;
12264 /* Only align end of TLS section if static TLS doesn't have special
12265 alignment requirements. */
12266 if (bed
->static_tls_alignment
== 1)
12267 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
12268 htab
->tls_size
= end
- base
;
12271 /* Reorder SHF_LINK_ORDER sections. */
12272 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12274 if (!elf_fixup_link_order (abfd
, o
))
12278 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
12281 /* Since ELF permits relocations to be against local symbols, we
12282 must have the local symbols available when we do the relocations.
12283 Since we would rather only read the local symbols once, and we
12284 would rather not keep them in memory, we handle all the
12285 relocations for a single input file at the same time.
12287 Unfortunately, there is no way to know the total number of local
12288 symbols until we have seen all of them, and the local symbol
12289 indices precede the global symbol indices. This means that when
12290 we are generating relocatable output, and we see a reloc against
12291 a global symbol, we can not know the symbol index until we have
12292 finished examining all the local symbols to see which ones we are
12293 going to output. To deal with this, we keep the relocations in
12294 memory, and don't output them until the end of the link. This is
12295 an unfortunate waste of memory, but I don't see a good way around
12296 it. Fortunately, it only happens when performing a relocatable
12297 link, which is not the common case. FIXME: If keep_memory is set
12298 we could write the relocs out and then read them again; I don't
12299 know how bad the memory loss will be. */
12301 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12302 sub
->output_has_begun
= FALSE
;
12303 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12305 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12307 if (p
->type
== bfd_indirect_link_order
12308 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
12309 == bfd_target_elf_flavour
)
12310 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
12312 if (! sub
->output_has_begun
)
12314 if (! elf_link_input_bfd (&flinfo
, sub
))
12316 sub
->output_has_begun
= TRUE
;
12319 else if (p
->type
== bfd_section_reloc_link_order
12320 || p
->type
== bfd_symbol_reloc_link_order
)
12322 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
12327 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
12329 if (p
->type
== bfd_indirect_link_order
12330 && (bfd_get_flavour (sub
)
12331 == bfd_target_elf_flavour
)
12332 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
12333 != bed
->s
->elfclass
))
12335 const char *iclass
, *oclass
;
12337 switch (bed
->s
->elfclass
)
12339 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
12340 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
12341 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
12345 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
12347 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
12348 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
12349 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
12353 bfd_set_error (bfd_error_wrong_format
);
12355 /* xgettext:c-format */
12356 (_("%pB: file class %s incompatible with %s"),
12357 sub
, iclass
, oclass
);
12366 /* Free symbol buffer if needed. */
12367 if (!info
->reduce_memory_overheads
)
12369 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12370 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
12371 && elf_tdata (sub
)->symbuf
)
12373 free (elf_tdata (sub
)->symbuf
);
12374 elf_tdata (sub
)->symbuf
= NULL
;
12378 /* Output any global symbols that got converted to local in a
12379 version script or due to symbol visibility. We do this in a
12380 separate step since ELF requires all local symbols to appear
12381 prior to any global symbols. FIXME: We should only do this if
12382 some global symbols were, in fact, converted to become local.
12383 FIXME: Will this work correctly with the Irix 5 linker? */
12384 eoinfo
.failed
= FALSE
;
12385 eoinfo
.flinfo
= &flinfo
;
12386 eoinfo
.localsyms
= TRUE
;
12387 eoinfo
.file_sym_done
= FALSE
;
12388 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12392 /* If backend needs to output some local symbols not present in the hash
12393 table, do it now. */
12394 if (bed
->elf_backend_output_arch_local_syms
12395 && (info
->strip
!= strip_all
|| emit_relocs
))
12397 typedef int (*out_sym_func
)
12398 (void *, const char *, Elf_Internal_Sym
*, asection
*,
12399 struct elf_link_hash_entry
*);
12401 if (! ((*bed
->elf_backend_output_arch_local_syms
)
12402 (abfd
, info
, &flinfo
,
12403 (out_sym_func
) elf_link_output_symstrtab
)))
12407 /* That wrote out all the local symbols. Finish up the symbol table
12408 with the global symbols. Even if we want to strip everything we
12409 can, we still need to deal with those global symbols that got
12410 converted to local in a version script. */
12412 /* The sh_info field records the index of the first non local symbol. */
12413 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12416 && htab
->dynsym
!= NULL
12417 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
12419 Elf_Internal_Sym sym
;
12420 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
12422 o
= htab
->dynsym
->output_section
;
12423 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
12425 /* Write out the section symbols for the output sections. */
12426 if (bfd_link_pic (info
)
12427 || htab
->is_relocatable_executable
)
12433 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12435 sym
.st_target_internal
= 0;
12437 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
12443 dynindx
= elf_section_data (s
)->dynindx
;
12446 indx
= elf_section_data (s
)->this_idx
;
12447 BFD_ASSERT (indx
> 0);
12448 sym
.st_shndx
= indx
;
12449 if (! check_dynsym (abfd
, &sym
))
12451 sym
.st_value
= s
->vma
;
12452 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
12453 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12457 /* Write out the local dynsyms. */
12458 if (htab
->dynlocal
)
12460 struct elf_link_local_dynamic_entry
*e
;
12461 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
12466 /* Copy the internal symbol and turn off visibility.
12467 Note that we saved a word of storage and overwrote
12468 the original st_name with the dynstr_index. */
12470 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
12472 s
= bfd_section_from_elf_index (e
->input_bfd
,
12477 elf_section_data (s
->output_section
)->this_idx
;
12478 if (! check_dynsym (abfd
, &sym
))
12480 sym
.st_value
= (s
->output_section
->vma
12482 + e
->isym
.st_value
);
12485 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
12486 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12491 /* We get the global symbols from the hash table. */
12492 eoinfo
.failed
= FALSE
;
12493 eoinfo
.localsyms
= FALSE
;
12494 eoinfo
.flinfo
= &flinfo
;
12495 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12499 /* If backend needs to output some symbols not present in the hash
12500 table, do it now. */
12501 if (bed
->elf_backend_output_arch_syms
12502 && (info
->strip
!= strip_all
|| emit_relocs
))
12504 typedef int (*out_sym_func
)
12505 (void *, const char *, Elf_Internal_Sym
*, asection
*,
12506 struct elf_link_hash_entry
*);
12508 if (! ((*bed
->elf_backend_output_arch_syms
)
12509 (abfd
, info
, &flinfo
,
12510 (out_sym_func
) elf_link_output_symstrtab
)))
12514 /* Finalize the .strtab section. */
12515 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
12517 /* Swap out the .strtab section. */
12518 if (!elf_link_swap_symbols_out (&flinfo
))
12521 /* Now we know the size of the symtab section. */
12522 if (bfd_get_symcount (abfd
) > 0)
12524 /* Finish up and write out the symbol string table (.strtab)
12526 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
12527 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
12529 if (elf_symtab_shndx_list (abfd
))
12531 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
12533 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
12535 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
12536 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
12537 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
12538 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
12539 symtab_shndx_hdr
->sh_size
= amt
;
12541 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
12544 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
12545 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
12550 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
12551 /* sh_name was set in prep_headers. */
12552 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
12553 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
12554 symstrtab_hdr
->sh_addr
= 0;
12555 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
12556 symstrtab_hdr
->sh_entsize
= 0;
12557 symstrtab_hdr
->sh_link
= 0;
12558 symstrtab_hdr
->sh_info
= 0;
12559 /* sh_offset is set just below. */
12560 symstrtab_hdr
->sh_addralign
= 1;
12562 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
12564 elf_next_file_pos (abfd
) = off
;
12566 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
12567 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
12571 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
12573 _bfd_error_handler (_("%pB: failed to generate import library"),
12574 info
->out_implib_bfd
);
12578 /* Adjust the relocs to have the correct symbol indices. */
12579 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12581 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12584 if ((o
->flags
& SEC_RELOC
) == 0)
12587 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
12588 if (esdo
->rel
.hdr
!= NULL
12589 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
, info
))
12591 if (esdo
->rela
.hdr
!= NULL
12592 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
, info
))
12595 /* Set the reloc_count field to 0 to prevent write_relocs from
12596 trying to swap the relocs out itself. */
12597 o
->reloc_count
= 0;
12600 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
12601 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
12603 /* If we are linking against a dynamic object, or generating a
12604 shared library, finish up the dynamic linking information. */
12607 bfd_byte
*dyncon
, *dynconend
;
12609 /* Fix up .dynamic entries. */
12610 o
= bfd_get_linker_section (dynobj
, ".dynamic");
12611 BFD_ASSERT (o
!= NULL
);
12613 dyncon
= o
->contents
;
12614 dynconend
= o
->contents
+ o
->size
;
12615 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12617 Elf_Internal_Dyn dyn
;
12620 bfd_size_type sh_size
;
12623 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12630 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
12632 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
12634 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
12635 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
12638 dyn
.d_un
.d_val
= relativecount
;
12645 name
= info
->init_function
;
12648 name
= info
->fini_function
;
12651 struct elf_link_hash_entry
*h
;
12653 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
12655 && (h
->root
.type
== bfd_link_hash_defined
12656 || h
->root
.type
== bfd_link_hash_defweak
))
12658 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
12659 o
= h
->root
.u
.def
.section
;
12660 if (o
->output_section
!= NULL
)
12661 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
12662 + o
->output_offset
);
12665 /* The symbol is imported from another shared
12666 library and does not apply to this one. */
12667 dyn
.d_un
.d_ptr
= 0;
12674 case DT_PREINIT_ARRAYSZ
:
12675 name
= ".preinit_array";
12677 case DT_INIT_ARRAYSZ
:
12678 name
= ".init_array";
12680 case DT_FINI_ARRAYSZ
:
12681 name
= ".fini_array";
12683 o
= bfd_get_section_by_name (abfd
, name
);
12687 (_("could not find section %s"), name
);
12692 (_("warning: %s section has zero size"), name
);
12693 dyn
.d_un
.d_val
= o
->size
;
12696 case DT_PREINIT_ARRAY
:
12697 name
= ".preinit_array";
12699 case DT_INIT_ARRAY
:
12700 name
= ".init_array";
12702 case DT_FINI_ARRAY
:
12703 name
= ".fini_array";
12705 o
= bfd_get_section_by_name (abfd
, name
);
12712 name
= ".gnu.hash";
12721 name
= ".gnu.version_d";
12724 name
= ".gnu.version_r";
12727 name
= ".gnu.version";
12729 o
= bfd_get_linker_section (dynobj
, name
);
12731 if (o
== NULL
|| bfd_is_abs_section (o
->output_section
))
12734 (_("could not find section %s"), name
);
12737 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
12740 (_("warning: section '%s' is being made into a note"), name
);
12741 bfd_set_error (bfd_error_nonrepresentable_section
);
12744 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
12751 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
12757 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12759 Elf_Internal_Shdr
*hdr
;
12761 hdr
= elf_elfsections (abfd
)[i
];
12762 if (hdr
->sh_type
== type
12763 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
12765 sh_size
+= hdr
->sh_size
;
12767 || sh_addr
> hdr
->sh_addr
)
12768 sh_addr
= hdr
->sh_addr
;
12772 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
12774 /* Don't count procedure linkage table relocs in the
12775 overall reloc count. */
12776 sh_size
-= htab
->srelplt
->size
;
12778 /* If the size is zero, make the address zero too.
12779 This is to avoid a glibc bug. If the backend
12780 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12781 zero, then we'll put DT_RELA at the end of
12782 DT_JMPREL. glibc will interpret the end of
12783 DT_RELA matching the end of DT_JMPREL as the
12784 case where DT_RELA includes DT_JMPREL, and for
12785 LD_BIND_NOW will decide that processing DT_RELA
12786 will process the PLT relocs too. Net result:
12787 No PLT relocs applied. */
12790 /* If .rela.plt is the first .rela section, exclude
12791 it from DT_RELA. */
12792 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
12793 + htab
->srelplt
->output_offset
))
12794 sh_addr
+= htab
->srelplt
->size
;
12797 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
12798 dyn
.d_un
.d_val
= sh_size
;
12800 dyn
.d_un
.d_ptr
= sh_addr
;
12803 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
12807 /* If we have created any dynamic sections, then output them. */
12808 if (dynobj
!= NULL
)
12810 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
12813 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12814 if (((info
->warn_shared_textrel
&& bfd_link_pic (info
))
12815 || info
->error_textrel
)
12816 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
)
12818 bfd_byte
*dyncon
, *dynconend
;
12820 dyncon
= o
->contents
;
12821 dynconend
= o
->contents
+ o
->size
;
12822 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12824 Elf_Internal_Dyn dyn
;
12826 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12828 if (dyn
.d_tag
== DT_TEXTREL
)
12830 if (info
->error_textrel
)
12831 info
->callbacks
->einfo
12832 (_("%P%X: read-only segment has dynamic relocations\n"));
12834 info
->callbacks
->einfo
12835 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
12841 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
12843 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
12845 || o
->output_section
== bfd_abs_section_ptr
)
12847 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
12849 /* At this point, we are only interested in sections
12850 created by _bfd_elf_link_create_dynamic_sections. */
12853 if (htab
->stab_info
.stabstr
== o
)
12855 if (htab
->eh_info
.hdr_sec
== o
)
12857 if (strcmp (o
->name
, ".dynstr") != 0)
12859 if (! bfd_set_section_contents (abfd
, o
->output_section
,
12861 (file_ptr
) o
->output_offset
12862 * bfd_octets_per_byte (abfd
,
12869 /* The contents of the .dynstr section are actually in a
12873 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
12874 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
12875 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
12881 if (!info
->resolve_section_groups
)
12883 bfd_boolean failed
= FALSE
;
12885 BFD_ASSERT (bfd_link_relocatable (info
));
12886 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
12891 /* If we have optimized stabs strings, output them. */
12892 if (htab
->stab_info
.stabstr
!= NULL
)
12894 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
12898 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
12901 if (info
->callbacks
->emit_ctf
)
12902 info
->callbacks
->emit_ctf ();
12904 elf_final_link_free (abfd
, &flinfo
);
12908 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
12909 if (contents
== NULL
)
12910 return FALSE
; /* Bail out and fail. */
12911 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
12912 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
12919 elf_final_link_free (abfd
, &flinfo
);
12923 /* Initialize COOKIE for input bfd ABFD. */
12926 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
12927 struct bfd_link_info
*info
, bfd
*abfd
)
12929 Elf_Internal_Shdr
*symtab_hdr
;
12930 const struct elf_backend_data
*bed
;
12932 bed
= get_elf_backend_data (abfd
);
12933 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12935 cookie
->abfd
= abfd
;
12936 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
12937 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
12938 if (cookie
->bad_symtab
)
12940 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
12941 cookie
->extsymoff
= 0;
12945 cookie
->locsymcount
= symtab_hdr
->sh_info
;
12946 cookie
->extsymoff
= symtab_hdr
->sh_info
;
12949 if (bed
->s
->arch_size
== 32)
12950 cookie
->r_sym_shift
= 8;
12952 cookie
->r_sym_shift
= 32;
12954 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
12955 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
12957 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
12958 cookie
->locsymcount
, 0,
12960 if (cookie
->locsyms
== NULL
)
12962 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
12965 if (info
->keep_memory
)
12966 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
12971 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12974 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
12976 Elf_Internal_Shdr
*symtab_hdr
;
12978 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12979 if (cookie
->locsyms
!= NULL
12980 && symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
12981 free (cookie
->locsyms
);
12984 /* Initialize the relocation information in COOKIE for input section SEC
12985 of input bfd ABFD. */
12988 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12989 struct bfd_link_info
*info
, bfd
*abfd
,
12992 if (sec
->reloc_count
== 0)
12994 cookie
->rels
= NULL
;
12995 cookie
->relend
= NULL
;
12999 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
13000 info
->keep_memory
);
13001 if (cookie
->rels
== NULL
)
13003 cookie
->rel
= cookie
->rels
;
13004 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
13006 cookie
->rel
= cookie
->rels
;
13010 /* Free the memory allocated by init_reloc_cookie_rels,
13014 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13017 if (cookie
->rels
&& elf_section_data (sec
)->relocs
!= cookie
->rels
)
13018 free (cookie
->rels
);
13021 /* Initialize the whole of COOKIE for input section SEC. */
13024 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13025 struct bfd_link_info
*info
,
13028 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
13030 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
13035 fini_reloc_cookie (cookie
, sec
->owner
);
13040 /* Free the memory allocated by init_reloc_cookie_for_section,
13044 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13047 fini_reloc_cookie_rels (cookie
, sec
);
13048 fini_reloc_cookie (cookie
, sec
->owner
);
13051 /* Garbage collect unused sections. */
13053 /* Default gc_mark_hook. */
13056 _bfd_elf_gc_mark_hook (asection
*sec
,
13057 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13058 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13059 struct elf_link_hash_entry
*h
,
13060 Elf_Internal_Sym
*sym
)
13064 switch (h
->root
.type
)
13066 case bfd_link_hash_defined
:
13067 case bfd_link_hash_defweak
:
13068 return h
->root
.u
.def
.section
;
13070 case bfd_link_hash_common
:
13071 return h
->root
.u
.c
.p
->section
;
13078 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
13083 /* Return the debug definition section. */
13086 elf_gc_mark_debug_section (asection
*sec ATTRIBUTE_UNUSED
,
13087 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13088 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13089 struct elf_link_hash_entry
*h
,
13090 Elf_Internal_Sym
*sym
)
13094 /* Return the global debug definition section. */
13095 if ((h
->root
.type
== bfd_link_hash_defined
13096 || h
->root
.type
== bfd_link_hash_defweak
)
13097 && (h
->root
.u
.def
.section
->flags
& SEC_DEBUGGING
) != 0)
13098 return h
->root
.u
.def
.section
;
13102 /* Return the local debug definition section. */
13103 asection
*isec
= bfd_section_from_elf_index (sec
->owner
,
13105 if ((isec
->flags
& SEC_DEBUGGING
) != 0)
13112 /* COOKIE->rel describes a relocation against section SEC, which is
13113 a section we've decided to keep. Return the section that contains
13114 the relocation symbol, or NULL if no section contains it. */
13117 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
13118 elf_gc_mark_hook_fn gc_mark_hook
,
13119 struct elf_reloc_cookie
*cookie
,
13120 bfd_boolean
*start_stop
)
13122 unsigned long r_symndx
;
13123 struct elf_link_hash_entry
*h
;
13125 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
13126 if (r_symndx
== STN_UNDEF
)
13129 if (r_symndx
>= cookie
->locsymcount
13130 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13132 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
13135 info
->callbacks
->einfo (_("%F%P: corrupt input: %pB\n"),
13139 while (h
->root
.type
== bfd_link_hash_indirect
13140 || h
->root
.type
== bfd_link_hash_warning
)
13141 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13143 /* If this symbol is weak and there is a non-weak definition, we
13144 keep the non-weak definition because many backends put
13145 dynamic reloc info on the non-weak definition for code
13146 handling copy relocs. */
13147 if (h
->is_weakalias
)
13148 weakdef (h
)->mark
= 1;
13150 if (start_stop
!= NULL
)
13152 /* To work around a glibc bug, mark XXX input sections
13153 when there is a reference to __start_XXX or __stop_XXX
13157 asection
*s
= h
->u2
.start_stop_section
;
13158 *start_stop
= !s
->gc_mark
;
13163 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
13166 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
13167 &cookie
->locsyms
[r_symndx
]);
13170 /* COOKIE->rel describes a relocation against section SEC, which is
13171 a section we've decided to keep. Mark the section that contains
13172 the relocation symbol. */
13175 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
13177 elf_gc_mark_hook_fn gc_mark_hook
,
13178 struct elf_reloc_cookie
*cookie
)
13181 bfd_boolean start_stop
= FALSE
;
13183 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
13184 while (rsec
!= NULL
)
13186 if (!rsec
->gc_mark
)
13188 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
13189 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
13191 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
13196 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
13201 /* The mark phase of garbage collection. For a given section, mark
13202 it and any sections in this section's group, and all the sections
13203 which define symbols to which it refers. */
13206 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
13208 elf_gc_mark_hook_fn gc_mark_hook
)
13211 asection
*group_sec
, *eh_frame
;
13215 /* Mark all the sections in the group. */
13216 group_sec
= elf_section_data (sec
)->next_in_group
;
13217 if (group_sec
&& !group_sec
->gc_mark
)
13218 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
13221 /* Look through the section relocs. */
13223 eh_frame
= elf_eh_frame_section (sec
->owner
);
13224 if ((sec
->flags
& SEC_RELOC
) != 0
13225 && sec
->reloc_count
> 0
13226 && sec
!= eh_frame
)
13228 struct elf_reloc_cookie cookie
;
13230 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
13234 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
13235 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
13240 fini_reloc_cookie_for_section (&cookie
, sec
);
13244 if (ret
&& eh_frame
&& elf_fde_list (sec
))
13246 struct elf_reloc_cookie cookie
;
13248 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
13252 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
13253 gc_mark_hook
, &cookie
))
13255 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
13259 eh_frame
= elf_section_eh_frame_entry (sec
);
13260 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
13261 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
13267 /* Scan and mark sections in a special or debug section group. */
13270 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
13272 /* Point to first section of section group. */
13274 /* Used to iterate the section group. */
13277 bfd_boolean is_special_grp
= TRUE
;
13278 bfd_boolean is_debug_grp
= TRUE
;
13280 /* First scan to see if group contains any section other than debug
13281 and special section. */
13282 ssec
= msec
= elf_next_in_group (grp
);
13285 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
13286 is_debug_grp
= FALSE
;
13288 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
13289 is_special_grp
= FALSE
;
13291 msec
= elf_next_in_group (msec
);
13293 while (msec
!= ssec
);
13295 /* If this is a pure debug section group or pure special section group,
13296 keep all sections in this group. */
13297 if (is_debug_grp
|| is_special_grp
)
13302 msec
= elf_next_in_group (msec
);
13304 while (msec
!= ssec
);
13308 /* Keep debug and special sections. */
13311 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
13312 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED
)
13316 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13319 bfd_boolean some_kept
;
13320 bfd_boolean debug_frag_seen
;
13321 bfd_boolean has_kept_debug_info
;
13323 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13325 isec
= ibfd
->sections
;
13326 if (isec
== NULL
|| isec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13329 /* Ensure all linker created sections are kept,
13330 see if any other section is already marked,
13331 and note if we have any fragmented debug sections. */
13332 debug_frag_seen
= some_kept
= has_kept_debug_info
= FALSE
;
13333 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13335 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
13337 else if (isec
->gc_mark
13338 && (isec
->flags
& SEC_ALLOC
) != 0
13339 && elf_section_type (isec
) != SHT_NOTE
)
13342 if (!debug_frag_seen
13343 && (isec
->flags
& SEC_DEBUGGING
)
13344 && CONST_STRNEQ (isec
->name
, ".debug_line."))
13345 debug_frag_seen
= TRUE
;
13348 /* If no non-note alloc section in this file will be kept, then
13349 we can toss out the debug and special sections. */
13353 /* Keep debug and special sections like .comment when they are
13354 not part of a group. Also keep section groups that contain
13355 just debug sections or special sections. */
13356 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13358 if ((isec
->flags
& SEC_GROUP
) != 0)
13359 _bfd_elf_gc_mark_debug_special_section_group (isec
);
13360 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
13361 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
13362 && elf_next_in_group (isec
) == NULL
)
13364 if (isec
->gc_mark
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
13365 has_kept_debug_info
= TRUE
;
13368 /* Look for CODE sections which are going to be discarded,
13369 and find and discard any fragmented debug sections which
13370 are associated with that code section. */
13371 if (debug_frag_seen
)
13372 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13373 if ((isec
->flags
& SEC_CODE
) != 0
13374 && isec
->gc_mark
== 0)
13379 ilen
= strlen (isec
->name
);
13381 /* Association is determined by the name of the debug
13382 section containing the name of the code section as
13383 a suffix. For example .debug_line.text.foo is a
13384 debug section associated with .text.foo. */
13385 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
13389 if (dsec
->gc_mark
== 0
13390 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
13393 dlen
= strlen (dsec
->name
);
13396 && strncmp (dsec
->name
+ (dlen
- ilen
),
13397 isec
->name
, ilen
) == 0)
13402 /* Mark debug sections referenced by kept debug sections. */
13403 if (has_kept_debug_info
)
13404 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13406 && (isec
->flags
& SEC_DEBUGGING
) != 0)
13407 if (!_bfd_elf_gc_mark (info
, isec
,
13408 elf_gc_mark_debug_section
))
13415 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
13418 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13420 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13424 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13425 || elf_object_id (sub
) != elf_hash_table_id (elf_hash_table (info
))
13426 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13429 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13432 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13434 /* When any section in a section group is kept, we keep all
13435 sections in the section group. If the first member of
13436 the section group is excluded, we will also exclude the
13438 if (o
->flags
& SEC_GROUP
)
13440 asection
*first
= elf_next_in_group (o
);
13441 o
->gc_mark
= first
->gc_mark
;
13447 /* Skip sweeping sections already excluded. */
13448 if (o
->flags
& SEC_EXCLUDE
)
13451 /* Since this is early in the link process, it is simple
13452 to remove a section from the output. */
13453 o
->flags
|= SEC_EXCLUDE
;
13455 if (info
->print_gc_sections
&& o
->size
!= 0)
13456 /* xgettext:c-format */
13457 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13465 /* Propagate collected vtable information. This is called through
13466 elf_link_hash_traverse. */
13469 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
13471 /* Those that are not vtables. */
13473 || h
->u2
.vtable
== NULL
13474 || h
->u2
.vtable
->parent
== NULL
)
13477 /* Those vtables that do not have parents, we cannot merge. */
13478 if (h
->u2
.vtable
->parent
== (struct elf_link_hash_entry
*) -1)
13481 /* If we've already been done, exit. */
13482 if (h
->u2
.vtable
->used
&& h
->u2
.vtable
->used
[-1])
13485 /* Make sure the parent's table is up to date. */
13486 elf_gc_propagate_vtable_entries_used (h
->u2
.vtable
->parent
, okp
);
13488 if (h
->u2
.vtable
->used
== NULL
)
13490 /* None of this table's entries were referenced. Re-use the
13492 h
->u2
.vtable
->used
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13493 h
->u2
.vtable
->size
= h
->u2
.vtable
->parent
->u2
.vtable
->size
;
13498 bfd_boolean
*cu
, *pu
;
13500 /* Or the parent's entries into ours. */
13501 cu
= h
->u2
.vtable
->used
;
13503 pu
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13506 const struct elf_backend_data
*bed
;
13507 unsigned int log_file_align
;
13509 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
13510 log_file_align
= bed
->s
->log_file_align
;
13511 n
= h
->u2
.vtable
->parent
->u2
.vtable
->size
>> log_file_align
;
13526 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
13529 bfd_vma hstart
, hend
;
13530 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
13531 const struct elf_backend_data
*bed
;
13532 unsigned int log_file_align
;
13534 /* Take care of both those symbols that do not describe vtables as
13535 well as those that are not loaded. */
13537 || h
->u2
.vtable
== NULL
13538 || h
->u2
.vtable
->parent
== NULL
)
13541 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
13542 || h
->root
.type
== bfd_link_hash_defweak
);
13544 sec
= h
->root
.u
.def
.section
;
13545 hstart
= h
->root
.u
.def
.value
;
13546 hend
= hstart
+ h
->size
;
13548 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
13550 return *(bfd_boolean
*) okp
= FALSE
;
13551 bed
= get_elf_backend_data (sec
->owner
);
13552 log_file_align
= bed
->s
->log_file_align
;
13554 relend
= relstart
+ sec
->reloc_count
;
13556 for (rel
= relstart
; rel
< relend
; ++rel
)
13557 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
13559 /* If the entry is in use, do nothing. */
13560 if (h
->u2
.vtable
->used
13561 && (rel
->r_offset
- hstart
) < h
->u2
.vtable
->size
)
13563 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
13564 if (h
->u2
.vtable
->used
[entry
])
13567 /* Otherwise, kill it. */
13568 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
13574 /* Mark sections containing dynamically referenced symbols. When
13575 building shared libraries, we must assume that any visible symbol is
13579 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
13581 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
13582 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
13584 if ((h
->root
.type
== bfd_link_hash_defined
13585 || h
->root
.type
== bfd_link_hash_defweak
)
13586 && ((h
->ref_dynamic
&& !h
->forced_local
)
13587 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
13588 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
13589 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
13590 && (!bfd_link_executable (info
)
13591 || info
->gc_keep_exported
13592 || info
->export_dynamic
13595 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
13596 && (h
->versioned
>= versioned
13597 || !bfd_hide_sym_by_version (info
->version_info
,
13598 h
->root
.root
.string
)))))
13599 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13604 /* Keep all sections containing symbols undefined on the command-line,
13605 and the section containing the entry symbol. */
13608 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
13610 struct bfd_sym_chain
*sym
;
13612 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
13614 struct elf_link_hash_entry
*h
;
13616 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
13617 FALSE
, FALSE
, FALSE
);
13620 && (h
->root
.type
== bfd_link_hash_defined
13621 || h
->root
.type
== bfd_link_hash_defweak
)
13622 && !bfd_is_abs_section (h
->root
.u
.def
.section
)
13623 && !bfd_is_und_section (h
->root
.u
.def
.section
))
13624 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13629 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
13630 struct bfd_link_info
*info
)
13632 bfd
*ibfd
= info
->input_bfds
;
13634 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13637 struct elf_reloc_cookie cookie
;
13639 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13641 sec
= ibfd
->sections
;
13642 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13645 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
13648 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
13650 if (CONST_STRNEQ (bfd_section_name (sec
), ".eh_frame_entry")
13651 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
13653 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
13654 fini_reloc_cookie_rels (&cookie
, sec
);
13661 /* Do mark and sweep of unused sections. */
13664 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
13666 bfd_boolean ok
= TRUE
;
13668 elf_gc_mark_hook_fn gc_mark_hook
;
13669 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13670 struct elf_link_hash_table
*htab
;
13672 if (!bed
->can_gc_sections
13673 || !is_elf_hash_table (info
->hash
))
13675 _bfd_error_handler(_("warning: gc-sections option ignored"));
13679 bed
->gc_keep (info
);
13680 htab
= elf_hash_table (info
);
13682 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13683 at the .eh_frame section if we can mark the FDEs individually. */
13684 for (sub
= info
->input_bfds
;
13685 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
13686 sub
= sub
->link
.next
)
13689 struct elf_reloc_cookie cookie
;
13691 sec
= sub
->sections
;
13692 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13694 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
13695 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
13697 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
13698 if (elf_section_data (sec
)->sec_info
13699 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
13700 elf_eh_frame_section (sub
) = sec
;
13701 fini_reloc_cookie_for_section (&cookie
, sec
);
13702 sec
= bfd_get_next_section_by_name (NULL
, sec
);
13706 /* Apply transitive closure to the vtable entry usage info. */
13707 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
13711 /* Kill the vtable relocations that were not used. */
13712 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &ok
);
13716 /* Mark dynamically referenced symbols. */
13717 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
13718 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
13720 /* Grovel through relocs to find out who stays ... */
13721 gc_mark_hook
= bed
->gc_mark_hook
;
13722 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13726 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13727 || elf_object_id (sub
) != elf_hash_table_id (htab
)
13728 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13732 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13735 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13736 Also treat note sections as a root, if the section is not part
13737 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13738 well as FINI_ARRAY sections for ld -r. */
13739 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13741 && (o
->flags
& SEC_EXCLUDE
) == 0
13742 && ((o
->flags
& SEC_KEEP
) != 0
13743 || (bfd_link_relocatable (info
)
13744 && ((elf_section_data (o
)->this_hdr
.sh_type
13745 == SHT_PREINIT_ARRAY
)
13746 || (elf_section_data (o
)->this_hdr
.sh_type
13748 || (elf_section_data (o
)->this_hdr
.sh_type
13749 == SHT_FINI_ARRAY
)))
13750 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
13751 && elf_next_in_group (o
) == NULL
)))
13753 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
13758 /* Allow the backend to mark additional target specific sections. */
13759 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
13761 /* ... and mark SEC_EXCLUDE for those that go. */
13762 return elf_gc_sweep (abfd
, info
);
13765 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13768 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
13770 struct elf_link_hash_entry
*h
,
13773 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
13774 struct elf_link_hash_entry
**search
, *child
;
13775 size_t extsymcount
;
13776 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13778 /* The sh_info field of the symtab header tells us where the
13779 external symbols start. We don't care about the local symbols at
13781 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
13782 if (!elf_bad_symtab (abfd
))
13783 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
13785 sym_hashes
= elf_sym_hashes (abfd
);
13786 sym_hashes_end
= sym_hashes
+ extsymcount
;
13788 /* Hunt down the child symbol, which is in this section at the same
13789 offset as the relocation. */
13790 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
13792 if ((child
= *search
) != NULL
13793 && (child
->root
.type
== bfd_link_hash_defined
13794 || child
->root
.type
== bfd_link_hash_defweak
)
13795 && child
->root
.u
.def
.section
== sec
13796 && child
->root
.u
.def
.value
== offset
)
13800 /* xgettext:c-format */
13801 _bfd_error_handler (_("%pB: %pA+%#" PRIx64
": no symbol found for INHERIT"),
13802 abfd
, sec
, (uint64_t) offset
);
13803 bfd_set_error (bfd_error_invalid_operation
);
13807 if (!child
->u2
.vtable
)
13809 child
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
13810 bfd_zalloc (abfd
, sizeof (*child
->u2
.vtable
)));
13811 if (!child
->u2
.vtable
)
13816 /* This *should* only be the absolute section. It could potentially
13817 be that someone has defined a non-global vtable though, which
13818 would be bad. It isn't worth paging in the local symbols to be
13819 sure though; that case should simply be handled by the assembler. */
13821 child
->u2
.vtable
->parent
= (struct elf_link_hash_entry
*) -1;
13824 child
->u2
.vtable
->parent
= h
;
13829 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13832 bfd_elf_gc_record_vtentry (bfd
*abfd
, asection
*sec
,
13833 struct elf_link_hash_entry
*h
,
13836 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13837 unsigned int log_file_align
= bed
->s
->log_file_align
;
13841 /* xgettext:c-format */
13842 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
13844 bfd_set_error (bfd_error_bad_value
);
13850 h
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
13851 bfd_zalloc (abfd
, sizeof (*h
->u2
.vtable
)));
13856 if (addend
>= h
->u2
.vtable
->size
)
13858 size_t size
, bytes
, file_align
;
13859 bfd_boolean
*ptr
= h
->u2
.vtable
->used
;
13861 /* While the symbol is undefined, we have to be prepared to handle
13863 file_align
= 1 << log_file_align
;
13864 if (h
->root
.type
== bfd_link_hash_undefined
)
13865 size
= addend
+ file_align
;
13869 if (addend
>= size
)
13871 /* Oops! We've got a reference past the defined end of
13872 the table. This is probably a bug -- shall we warn? */
13873 size
= addend
+ file_align
;
13876 size
= (size
+ file_align
- 1) & -file_align
;
13878 /* Allocate one extra entry for use as a "done" flag for the
13879 consolidation pass. */
13880 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
13884 ptr
= (bfd_boolean
*) bfd_realloc (ptr
- 1, bytes
);
13890 oldbytes
= (((h
->u2
.vtable
->size
>> log_file_align
) + 1)
13891 * sizeof (bfd_boolean
));
13892 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
13896 ptr
= (bfd_boolean
*) bfd_zmalloc (bytes
);
13901 /* And arrange for that done flag to be at index -1. */
13902 h
->u2
.vtable
->used
= ptr
+ 1;
13903 h
->u2
.vtable
->size
= size
;
13906 h
->u2
.vtable
->used
[addend
>> log_file_align
] = TRUE
;
13911 /* Map an ELF section header flag to its corresponding string. */
13915 flagword flag_value
;
13916 } elf_flags_to_name_table
;
13918 static elf_flags_to_name_table elf_flags_to_names
[] =
13920 { "SHF_WRITE", SHF_WRITE
},
13921 { "SHF_ALLOC", SHF_ALLOC
},
13922 { "SHF_EXECINSTR", SHF_EXECINSTR
},
13923 { "SHF_MERGE", SHF_MERGE
},
13924 { "SHF_STRINGS", SHF_STRINGS
},
13925 { "SHF_INFO_LINK", SHF_INFO_LINK
},
13926 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
13927 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
13928 { "SHF_GROUP", SHF_GROUP
},
13929 { "SHF_TLS", SHF_TLS
},
13930 { "SHF_MASKOS", SHF_MASKOS
},
13931 { "SHF_EXCLUDE", SHF_EXCLUDE
},
13934 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13936 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
13937 struct flag_info
*flaginfo
,
13940 const bfd_vma sh_flags
= elf_section_flags (section
);
13942 if (!flaginfo
->flags_initialized
)
13944 bfd
*obfd
= info
->output_bfd
;
13945 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13946 struct flag_info_list
*tf
= flaginfo
->flag_list
;
13948 int without_hex
= 0;
13950 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
13953 flagword (*lookup
) (char *);
13955 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
13956 if (lookup
!= NULL
)
13958 flagword hexval
= (*lookup
) ((char *) tf
->name
);
13962 if (tf
->with
== with_flags
)
13963 with_hex
|= hexval
;
13964 else if (tf
->with
== without_flags
)
13965 without_hex
|= hexval
;
13970 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
13972 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
13974 if (tf
->with
== with_flags
)
13975 with_hex
|= elf_flags_to_names
[i
].flag_value
;
13976 else if (tf
->with
== without_flags
)
13977 without_hex
|= elf_flags_to_names
[i
].flag_value
;
13984 info
->callbacks
->einfo
13985 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
13989 flaginfo
->flags_initialized
= TRUE
;
13990 flaginfo
->only_with_flags
|= with_hex
;
13991 flaginfo
->not_with_flags
|= without_hex
;
13994 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
13997 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
14003 struct alloc_got_off_arg
{
14005 struct bfd_link_info
*info
;
14008 /* We need a special top-level link routine to convert got reference counts
14009 to real got offsets. */
14012 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
14014 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
14015 bfd
*obfd
= gofarg
->info
->output_bfd
;
14016 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14018 if (h
->got
.refcount
> 0)
14020 h
->got
.offset
= gofarg
->gotoff
;
14021 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
14024 h
->got
.offset
= (bfd_vma
) -1;
14029 /* And an accompanying bit to work out final got entry offsets once
14030 we're done. Should be called from final_link. */
14033 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
14034 struct bfd_link_info
*info
)
14037 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14039 struct alloc_got_off_arg gofarg
;
14041 BFD_ASSERT (abfd
== info
->output_bfd
);
14043 if (! is_elf_hash_table (info
->hash
))
14046 /* The GOT offset is relative to the .got section, but the GOT header is
14047 put into the .got.plt section, if the backend uses it. */
14048 if (bed
->want_got_plt
)
14051 gotoff
= bed
->got_header_size
;
14053 /* Do the local .got entries first. */
14054 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
14056 bfd_signed_vma
*local_got
;
14057 size_t j
, locsymcount
;
14058 Elf_Internal_Shdr
*symtab_hdr
;
14060 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
14063 local_got
= elf_local_got_refcounts (i
);
14067 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
14068 if (elf_bad_symtab (i
))
14069 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
14071 locsymcount
= symtab_hdr
->sh_info
;
14073 for (j
= 0; j
< locsymcount
; ++j
)
14075 if (local_got
[j
] > 0)
14077 local_got
[j
] = gotoff
;
14078 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
14081 local_got
[j
] = (bfd_vma
) -1;
14085 /* Then the global .got entries. .plt refcounts are handled by
14086 adjust_dynamic_symbol */
14087 gofarg
.gotoff
= gotoff
;
14088 gofarg
.info
= info
;
14089 elf_link_hash_traverse (elf_hash_table (info
),
14090 elf_gc_allocate_got_offsets
,
14095 /* Many folk need no more in the way of final link than this, once
14096 got entry reference counting is enabled. */
14099 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
14101 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
14104 /* Invoke the regular ELF backend linker to do all the work. */
14105 return bfd_elf_final_link (abfd
, info
);
14109 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
14111 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
14113 if (rcookie
->bad_symtab
)
14114 rcookie
->rel
= rcookie
->rels
;
14116 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
14118 unsigned long r_symndx
;
14120 if (! rcookie
->bad_symtab
)
14121 if (rcookie
->rel
->r_offset
> offset
)
14123 if (rcookie
->rel
->r_offset
!= offset
)
14126 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
14127 if (r_symndx
== STN_UNDEF
)
14130 if (r_symndx
>= rcookie
->locsymcount
14131 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
14133 struct elf_link_hash_entry
*h
;
14135 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
14137 while (h
->root
.type
== bfd_link_hash_indirect
14138 || h
->root
.type
== bfd_link_hash_warning
)
14139 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
14141 if ((h
->root
.type
== bfd_link_hash_defined
14142 || h
->root
.type
== bfd_link_hash_defweak
)
14143 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
14144 || h
->root
.u
.def
.section
->kept_section
!= NULL
14145 || discarded_section (h
->root
.u
.def
.section
)))
14150 /* It's not a relocation against a global symbol,
14151 but it could be a relocation against a local
14152 symbol for a discarded section. */
14154 Elf_Internal_Sym
*isym
;
14156 /* Need to: get the symbol; get the section. */
14157 isym
= &rcookie
->locsyms
[r_symndx
];
14158 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
14160 && (isec
->kept_section
!= NULL
14161 || discarded_section (isec
)))
14169 /* Discard unneeded references to discarded sections.
14170 Returns -1 on error, 1 if any section's size was changed, 0 if
14171 nothing changed. This function assumes that the relocations are in
14172 sorted order, which is true for all known assemblers. */
14175 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
14177 struct elf_reloc_cookie cookie
;
14182 if (info
->traditional_format
14183 || !is_elf_hash_table (info
->hash
))
14186 o
= bfd_get_section_by_name (output_bfd
, ".stab");
14191 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14194 || i
->reloc_count
== 0
14195 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
14199 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14202 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14205 if (_bfd_discard_section_stabs (abfd
, i
,
14206 elf_section_data (i
)->sec_info
,
14207 bfd_elf_reloc_symbol_deleted_p
,
14211 fini_reloc_cookie_for_section (&cookie
, i
);
14216 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
14217 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
14221 int eh_changed
= 0;
14222 unsigned int eh_alignment
;
14224 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14230 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14233 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14236 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
14237 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
14238 bfd_elf_reloc_symbol_deleted_p
,
14242 if (i
->size
!= i
->rawsize
)
14246 fini_reloc_cookie_for_section (&cookie
, i
);
14249 eh_alignment
= 1 << o
->alignment_power
;
14250 /* Skip over zero terminator, and prevent empty sections from
14251 adding alignment padding at the end. */
14252 for (i
= o
->map_tail
.s
; i
!= NULL
; i
= i
->map_tail
.s
)
14254 i
->flags
|= SEC_EXCLUDE
;
14255 else if (i
->size
> 4)
14257 /* The last non-empty eh_frame section doesn't need padding. */
14260 /* Any prior sections must pad the last FDE out to the output
14261 section alignment. Otherwise we might have zero padding
14262 between sections, which would be seen as a terminator. */
14263 for (; i
!= NULL
; i
= i
->map_tail
.s
)
14265 /* All but the last zero terminator should have been removed. */
14270 = (i
->size
+ eh_alignment
- 1) & -eh_alignment
;
14271 if (i
->size
!= size
)
14279 elf_link_hash_traverse (elf_hash_table (info
),
14280 _bfd_elf_adjust_eh_frame_global_symbol
, NULL
);
14283 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
14285 const struct elf_backend_data
*bed
;
14288 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14290 s
= abfd
->sections
;
14291 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14294 bed
= get_elf_backend_data (abfd
);
14296 if (bed
->elf_backend_discard_info
!= NULL
)
14298 if (!init_reloc_cookie (&cookie
, info
, abfd
))
14301 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
14304 fini_reloc_cookie (&cookie
, abfd
);
14308 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
14309 _bfd_elf_end_eh_frame_parsing (info
);
14311 if (info
->eh_frame_hdr_type
14312 && !bfd_link_relocatable (info
)
14313 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
14320 _bfd_elf_section_already_linked (bfd
*abfd
,
14322 struct bfd_link_info
*info
)
14325 const char *name
, *key
;
14326 struct bfd_section_already_linked
*l
;
14327 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
14329 if (sec
->output_section
== bfd_abs_section_ptr
)
14332 flags
= sec
->flags
;
14334 /* Return if it isn't a linkonce section. A comdat group section
14335 also has SEC_LINK_ONCE set. */
14336 if ((flags
& SEC_LINK_ONCE
) == 0)
14339 /* Don't put group member sections on our list of already linked
14340 sections. They are handled as a group via their group section. */
14341 if (elf_sec_group (sec
) != NULL
)
14344 /* For a SHT_GROUP section, use the group signature as the key. */
14346 if ((flags
& SEC_GROUP
) != 0
14347 && elf_next_in_group (sec
) != NULL
14348 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
14349 key
= elf_group_name (elf_next_in_group (sec
));
14352 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14353 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
14354 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
14357 /* Must be a user linkonce section that doesn't follow gcc's
14358 naming convention. In this case we won't be matching
14359 single member groups. */
14363 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
14365 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14367 /* We may have 2 different types of sections on the list: group
14368 sections with a signature of <key> (<key> is some string),
14369 and linkonce sections named .gnu.linkonce.<type>.<key>.
14370 Match like sections. LTO plugin sections are an exception.
14371 They are always named .gnu.linkonce.t.<key> and match either
14372 type of section. */
14373 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
14374 && ((flags
& SEC_GROUP
) != 0
14375 || strcmp (name
, l
->sec
->name
) == 0))
14376 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0)
14378 /* The section has already been linked. See if we should
14379 issue a warning. */
14380 if (!_bfd_handle_already_linked (sec
, l
, info
))
14383 if (flags
& SEC_GROUP
)
14385 asection
*first
= elf_next_in_group (sec
);
14386 asection
*s
= first
;
14390 s
->output_section
= bfd_abs_section_ptr
;
14391 /* Record which group discards it. */
14392 s
->kept_section
= l
->sec
;
14393 s
= elf_next_in_group (s
);
14394 /* These lists are circular. */
14404 /* A single member comdat group section may be discarded by a
14405 linkonce section and vice versa. */
14406 if ((flags
& SEC_GROUP
) != 0)
14408 asection
*first
= elf_next_in_group (sec
);
14410 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
14411 /* Check this single member group against linkonce sections. */
14412 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14413 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14414 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
14416 first
->output_section
= bfd_abs_section_ptr
;
14417 first
->kept_section
= l
->sec
;
14418 sec
->output_section
= bfd_abs_section_ptr
;
14423 /* Check this linkonce section against single member groups. */
14424 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14425 if (l
->sec
->flags
& SEC_GROUP
)
14427 asection
*first
= elf_next_in_group (l
->sec
);
14430 && elf_next_in_group (first
) == first
14431 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
14433 sec
->output_section
= bfd_abs_section_ptr
;
14434 sec
->kept_section
= first
;
14439 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14440 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14441 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14442 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14443 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14444 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14445 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14446 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14447 The reverse order cannot happen as there is never a bfd with only the
14448 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14449 matter as here were are looking only for cross-bfd sections. */
14451 if ((flags
& SEC_GROUP
) == 0 && CONST_STRNEQ (name
, ".gnu.linkonce.r."))
14452 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14453 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14454 && CONST_STRNEQ (l
->sec
->name
, ".gnu.linkonce.t."))
14456 if (abfd
!= l
->sec
->owner
)
14457 sec
->output_section
= bfd_abs_section_ptr
;
14461 /* This is the first section with this name. Record it. */
14462 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
14463 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
14464 return sec
->output_section
== bfd_abs_section_ptr
;
14468 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
14470 return sym
->st_shndx
== SHN_COMMON
;
14474 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
14480 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
14482 return bfd_com_section_ptr
;
14486 _bfd_elf_default_got_elt_size (bfd
*abfd
,
14487 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
14488 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
14489 bfd
*ibfd ATTRIBUTE_UNUSED
,
14490 unsigned long symndx ATTRIBUTE_UNUSED
)
14492 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14493 return bed
->s
->arch_size
/ 8;
14496 /* Routines to support the creation of dynamic relocs. */
14498 /* Returns the name of the dynamic reloc section associated with SEC. */
14500 static const char *
14501 get_dynamic_reloc_section_name (bfd
* abfd
,
14503 bfd_boolean is_rela
)
14506 const char *old_name
= bfd_section_name (sec
);
14507 const char *prefix
= is_rela
? ".rela" : ".rel";
14509 if (old_name
== NULL
)
14512 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
14513 sprintf (name
, "%s%s", prefix
, old_name
);
14518 /* Returns the dynamic reloc section associated with SEC.
14519 If necessary compute the name of the dynamic reloc section based
14520 on SEC's name (looked up in ABFD's string table) and the setting
14524 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
14526 bfd_boolean is_rela
)
14528 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14530 if (reloc_sec
== NULL
)
14532 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14536 reloc_sec
= bfd_get_linker_section (abfd
, name
);
14538 if (reloc_sec
!= NULL
)
14539 elf_section_data (sec
)->sreloc
= reloc_sec
;
14546 /* Returns the dynamic reloc section associated with SEC. If the
14547 section does not exist it is created and attached to the DYNOBJ
14548 bfd and stored in the SRELOC field of SEC's elf_section_data
14551 ALIGNMENT is the alignment for the newly created section and
14552 IS_RELA defines whether the name should be .rela.<SEC's name>
14553 or .rel.<SEC's name>. The section name is looked up in the
14554 string table associated with ABFD. */
14557 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
14559 unsigned int alignment
,
14561 bfd_boolean is_rela
)
14563 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14565 if (reloc_sec
== NULL
)
14567 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14572 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
14574 if (reloc_sec
== NULL
)
14576 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
14577 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
14578 if ((sec
->flags
& SEC_ALLOC
) != 0)
14579 flags
|= SEC_ALLOC
| SEC_LOAD
;
14581 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
14582 if (reloc_sec
!= NULL
)
14584 /* _bfd_elf_get_sec_type_attr chooses a section type by
14585 name. Override as it may be wrong, eg. for a user
14586 section named "auto" we'll get ".relauto" which is
14587 seen to be a .rela section. */
14588 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
14589 if (!bfd_set_section_alignment (reloc_sec
, alignment
))
14594 elf_section_data (sec
)->sreloc
= reloc_sec
;
14600 /* Copy the ELF symbol type and other attributes for a linker script
14601 assignment from HSRC to HDEST. Generally this should be treated as
14602 if we found a strong non-dynamic definition for HDEST (except that
14603 ld ignores multiple definition errors). */
14605 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
14606 struct bfd_link_hash_entry
*hdest
,
14607 struct bfd_link_hash_entry
*hsrc
)
14609 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
14610 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
14611 Elf_Internal_Sym isym
;
14613 ehdest
->type
= ehsrc
->type
;
14614 ehdest
->target_internal
= ehsrc
->target_internal
;
14616 isym
.st_other
= ehsrc
->other
;
14617 elf_merge_st_other (abfd
, ehdest
, &isym
, NULL
, TRUE
, FALSE
);
14620 /* Append a RELA relocation REL to section S in BFD. */
14623 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14625 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14626 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
14627 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
14628 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
14631 /* Append a REL relocation REL to section S in BFD. */
14634 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14636 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14637 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
14638 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
14639 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);
14642 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14644 struct bfd_link_hash_entry
*
14645 bfd_elf_define_start_stop (struct bfd_link_info
*info
,
14646 const char *symbol
, asection
*sec
)
14648 struct elf_link_hash_entry
*h
;
14650 h
= elf_link_hash_lookup (elf_hash_table (info
), symbol
,
14651 FALSE
, FALSE
, TRUE
);
14653 && (h
->root
.type
== bfd_link_hash_undefined
14654 || h
->root
.type
== bfd_link_hash_undefweak
14655 || ((h
->ref_regular
|| h
->def_dynamic
) && !h
->def_regular
)))
14657 bfd_boolean was_dynamic
= h
->ref_dynamic
|| h
->def_dynamic
;
14658 h
->root
.type
= bfd_link_hash_defined
;
14659 h
->root
.u
.def
.section
= sec
;
14660 h
->root
.u
.def
.value
= 0;
14661 h
->def_regular
= 1;
14662 h
->def_dynamic
= 0;
14664 h
->u2
.start_stop_section
= sec
;
14665 if (symbol
[0] == '.')
14667 /* .startof. and .sizeof. symbols are local. */
14668 const struct elf_backend_data
*bed
;
14669 bed
= get_elf_backend_data (info
->output_bfd
);
14670 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
14674 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
14675 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED
;
14677 bfd_elf_link_record_dynamic_symbol (info
, h
);