1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2018 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. */
23 #include "bfd_stdint.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
31 #if BFD_SUPPORTS_PLUGINS
32 #include "plugin-api.h"
36 /* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
39 struct elf_info_failed
41 struct bfd_link_info
*info
;
45 /* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
48 struct elf_find_verdep_info
50 /* General link information. */
51 struct bfd_link_info
*info
;
52 /* The number of dependencies. */
54 /* Whether we had a failure. */
58 static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry
*, struct elf_info_failed
*);
62 _bfd_elf_section_for_symbol (struct elf_reloc_cookie
*cookie
,
63 unsigned long r_symndx
,
66 if (r_symndx
>= cookie
->locsymcount
67 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
69 struct elf_link_hash_entry
*h
;
71 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
73 while (h
->root
.type
== bfd_link_hash_indirect
74 || h
->root
.type
== bfd_link_hash_warning
)
75 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
77 if ((h
->root
.type
== bfd_link_hash_defined
78 || h
->root
.type
== bfd_link_hash_defweak
)
79 && discarded_section (h
->root
.u
.def
.section
))
80 return h
->root
.u
.def
.section
;
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
90 Elf_Internal_Sym
*isym
;
92 /* Need to: get the symbol; get the section. */
93 isym
= &cookie
->locsyms
[r_symndx
];
94 isec
= bfd_section_from_elf_index (cookie
->abfd
, isym
->st_shndx
);
96 && discard
? discarded_section (isec
) : 1)
102 /* Define a symbol in a dynamic linkage section. */
104 struct elf_link_hash_entry
*
105 _bfd_elf_define_linkage_sym (bfd
*abfd
,
106 struct bfd_link_info
*info
,
110 struct elf_link_hash_entry
*h
;
111 struct bfd_link_hash_entry
*bh
;
112 const struct elf_backend_data
*bed
;
114 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h
->root
.type
= bfd_link_hash_new
;
127 bed
= get_elf_backend_data (abfd
);
128 if (!_bfd_generic_link_add_one_symbol (info
, abfd
, name
, BSF_GLOBAL
,
129 sec
, 0, NULL
, FALSE
, bed
->collect
,
132 h
= (struct elf_link_hash_entry
*) bh
;
133 BFD_ASSERT (h
!= NULL
);
136 h
->root
.linker_def
= 1;
137 h
->type
= STT_OBJECT
;
138 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
139 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
141 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
146 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
150 struct elf_link_hash_entry
*h
;
151 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
152 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
154 /* This function may be called more than once. */
155 if (htab
->sgot
!= NULL
)
158 flags
= bed
->dynamic_sec_flags
;
160 s
= bfd_make_section_anyway_with_flags (abfd
,
161 (bed
->rela_plts_and_copies_p
162 ? ".rela.got" : ".rel.got"),
163 (bed
->dynamic_sec_flags
166 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
170 s
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
172 || !bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
176 if (bed
->want_got_plt
)
178 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
180 || !bfd_set_section_alignment (abfd
, s
,
181 bed
->s
->log_file_align
))
186 /* The first bit of the global offset table is the header. */
187 s
->size
+= bed
->got_header_size
;
189 if (bed
->want_got_sym
)
191 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
192 (or .got.plt) section. We don't do this in the linker script
193 because we don't want to define the symbol if we are not creating
194 a global offset table. */
195 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
196 "_GLOBAL_OFFSET_TABLE_");
197 elf_hash_table (info
)->hgot
= h
;
205 /* Create a strtab to hold the dynamic symbol names. */
207 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
209 struct elf_link_hash_table
*hash_table
;
211 hash_table
= elf_hash_table (info
);
212 if (hash_table
->dynobj
== NULL
)
214 /* We may not set dynobj, an input file holding linker created
215 dynamic sections to abfd, which may be a dynamic object with
216 its own dynamic sections. We need to find a normal input file
217 to hold linker created sections if possible. */
218 if ((abfd
->flags
& (DYNAMIC
| BFD_PLUGIN
)) != 0)
222 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link
.next
)
224 & (DYNAMIC
| BFD_LINKER_CREATED
| BFD_PLUGIN
)) == 0
225 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
226 && !((s
= ibfd
->sections
) != NULL
227 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
))
233 hash_table
->dynobj
= abfd
;
236 if (hash_table
->dynstr
== NULL
)
238 hash_table
->dynstr
= _bfd_elf_strtab_init ();
239 if (hash_table
->dynstr
== NULL
)
245 /* Create some sections which will be filled in with dynamic linking
246 information. ABFD is an input file which requires dynamic sections
247 to be created. The dynamic sections take up virtual memory space
248 when the final executable is run, so we need to create them before
249 addresses are assigned to the output sections. We work out the
250 actual contents and size of these sections later. */
253 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
257 const struct elf_backend_data
*bed
;
258 struct elf_link_hash_entry
*h
;
260 if (! is_elf_hash_table (info
->hash
))
263 if (elf_hash_table (info
)->dynamic_sections_created
)
266 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
269 abfd
= elf_hash_table (info
)->dynobj
;
270 bed
= get_elf_backend_data (abfd
);
272 flags
= bed
->dynamic_sec_flags
;
274 /* A dynamically linked executable has a .interp section, but a
275 shared library does not. */
276 if (bfd_link_executable (info
) && !info
->nointerp
)
278 s
= bfd_make_section_anyway_with_flags (abfd
, ".interp",
279 flags
| SEC_READONLY
);
284 /* Create sections to hold version informations. These are removed
285 if they are not needed. */
286 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_d",
287 flags
| SEC_READONLY
);
289 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
292 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version",
293 flags
| SEC_READONLY
);
295 || ! bfd_set_section_alignment (abfd
, s
, 1))
298 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_r",
299 flags
| SEC_READONLY
);
301 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
304 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynsym",
305 flags
| SEC_READONLY
);
307 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
309 elf_hash_table (info
)->dynsym
= s
;
311 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynstr",
312 flags
| SEC_READONLY
);
316 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynamic", flags
);
318 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
321 /* The special symbol _DYNAMIC is always set to the start of the
322 .dynamic section. We could set _DYNAMIC in a linker script, but we
323 only want to define it if we are, in fact, creating a .dynamic
324 section. We don't want to define it if there is no .dynamic
325 section, since on some ELF platforms the start up code examines it
326 to decide how to initialize the process. */
327 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC");
328 elf_hash_table (info
)->hdynamic
= h
;
334 s
= bfd_make_section_anyway_with_flags (abfd
, ".hash",
335 flags
| SEC_READONLY
);
337 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
339 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
342 if (info
->emit_gnu_hash
)
344 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.hash",
345 flags
| SEC_READONLY
);
347 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
349 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
350 4 32-bit words followed by variable count of 64-bit words, then
351 variable count of 32-bit words. */
352 if (bed
->s
->arch_size
== 64)
353 elf_section_data (s
)->this_hdr
.sh_entsize
= 0;
355 elf_section_data (s
)->this_hdr
.sh_entsize
= 4;
358 /* Let the backend create the rest of the sections. This lets the
359 backend set the right flags. The backend will normally create
360 the .got and .plt sections. */
361 if (bed
->elf_backend_create_dynamic_sections
== NULL
362 || ! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
365 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
370 /* Create dynamic sections when linking against a dynamic object. */
373 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
375 flagword flags
, pltflags
;
376 struct elf_link_hash_entry
*h
;
378 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
379 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
381 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
382 .rel[a].bss sections. */
383 flags
= bed
->dynamic_sec_flags
;
386 if (bed
->plt_not_loaded
)
387 /* We do not clear SEC_ALLOC here because we still want the OS to
388 allocate space for the section; it's just that there's nothing
389 to read in from the object file. */
390 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
392 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
393 if (bed
->plt_readonly
)
394 pltflags
|= SEC_READONLY
;
396 s
= bfd_make_section_anyway_with_flags (abfd
, ".plt", pltflags
);
398 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
402 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
404 if (bed
->want_plt_sym
)
406 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
407 "_PROCEDURE_LINKAGE_TABLE_");
408 elf_hash_table (info
)->hplt
= h
;
413 s
= bfd_make_section_anyway_with_flags (abfd
,
414 (bed
->rela_plts_and_copies_p
415 ? ".rela.plt" : ".rel.plt"),
416 flags
| SEC_READONLY
);
418 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
422 if (! _bfd_elf_create_got_section (abfd
, info
))
425 if (bed
->want_dynbss
)
427 /* The .dynbss section is a place to put symbols which are defined
428 by dynamic objects, are referenced by regular objects, and are
429 not functions. We must allocate space for them in the process
430 image and use a R_*_COPY reloc to tell the dynamic linker to
431 initialize them at run time. The linker script puts the .dynbss
432 section into the .bss section of the final image. */
433 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynbss",
434 SEC_ALLOC
| SEC_LINKER_CREATED
);
439 if (bed
->want_dynrelro
)
441 /* Similarly, but for symbols that were originally in read-only
442 sections. This section doesn't really need to have contents,
443 but make it like other .data.rel.ro sections. */
444 s
= bfd_make_section_anyway_with_flags (abfd
, ".data.rel.ro",
451 /* The .rel[a].bss section holds copy relocs. This section is not
452 normally needed. We need to create it here, though, so that the
453 linker will map it to an output section. We can't just create it
454 only if we need it, because we will not know whether we need it
455 until we have seen all the input files, and the first time the
456 main linker code calls BFD after examining all the input files
457 (size_dynamic_sections) the input sections have already been
458 mapped to the output sections. If the section turns out not to
459 be needed, we can discard it later. We will never need this
460 section when generating a shared object, since they do not use
462 if (bfd_link_executable (info
))
464 s
= bfd_make_section_anyway_with_flags (abfd
,
465 (bed
->rela_plts_and_copies_p
466 ? ".rela.bss" : ".rel.bss"),
467 flags
| SEC_READONLY
);
469 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
473 if (bed
->want_dynrelro
)
475 s
= (bfd_make_section_anyway_with_flags
476 (abfd
, (bed
->rela_plts_and_copies_p
477 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
478 flags
| SEC_READONLY
));
480 || ! bfd_set_section_alignment (abfd
, s
,
481 bed
->s
->log_file_align
))
483 htab
->sreldynrelro
= s
;
491 /* Record a new dynamic symbol. We record the dynamic symbols as we
492 read the input files, since we need to have a list of all of them
493 before we can determine the final sizes of the output sections.
494 Note that we may actually call this function even though we are not
495 going to output any dynamic symbols; in some cases we know that a
496 symbol should be in the dynamic symbol table, but only if there is
500 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
501 struct elf_link_hash_entry
*h
)
503 if (h
->dynindx
== -1)
505 struct elf_strtab_hash
*dynstr
;
510 /* XXX: The ABI draft says the linker must turn hidden and
511 internal symbols into STB_LOCAL symbols when producing the
512 DSO. However, if ld.so honors st_other in the dynamic table,
513 this would not be necessary. */
514 switch (ELF_ST_VISIBILITY (h
->other
))
518 if (h
->root
.type
!= bfd_link_hash_undefined
519 && h
->root
.type
!= bfd_link_hash_undefweak
)
522 if (!elf_hash_table (info
)->is_relocatable_executable
)
530 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
531 ++elf_hash_table (info
)->dynsymcount
;
533 dynstr
= elf_hash_table (info
)->dynstr
;
536 /* Create a strtab to hold the dynamic symbol names. */
537 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
542 /* We don't put any version information in the dynamic string
544 name
= h
->root
.root
.string
;
545 p
= strchr (name
, ELF_VER_CHR
);
547 /* We know that the p points into writable memory. In fact,
548 there are only a few symbols that have read-only names, being
549 those like _GLOBAL_OFFSET_TABLE_ that are created specially
550 by the backends. Most symbols will have names pointing into
551 an ELF string table read from a file, or to objalloc memory. */
554 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
559 if (indx
== (size_t) -1)
561 h
->dynstr_index
= indx
;
567 /* Mark a symbol dynamic. */
570 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
571 struct elf_link_hash_entry
*h
,
572 Elf_Internal_Sym
*sym
)
574 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
576 /* It may be called more than once on the same H. */
577 if(h
->dynamic
|| bfd_link_relocatable (info
))
580 if ((info
->dynamic_data
581 && (h
->type
== STT_OBJECT
582 || h
->type
== STT_COMMON
584 && (ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
585 || ELF_ST_TYPE (sym
->st_info
) == STT_COMMON
))))
588 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
591 /* NB: If a symbol is made dynamic by --dynamic-list, it has
593 h
->root
.non_ir_ref_dynamic
= 1;
597 /* Record an assignment to a symbol made by a linker script. We need
598 this in case some dynamic object refers to this symbol. */
601 bfd_elf_record_link_assignment (bfd
*output_bfd
,
602 struct bfd_link_info
*info
,
607 struct elf_link_hash_entry
*h
, *hv
;
608 struct elf_link_hash_table
*htab
;
609 const struct elf_backend_data
*bed
;
611 if (!is_elf_hash_table (info
->hash
))
614 htab
= elf_hash_table (info
);
615 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
619 if (h
->root
.type
== bfd_link_hash_warning
)
620 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
622 if (h
->versioned
== unknown
)
624 /* Set versioned if symbol version is unknown. */
625 char *version
= strrchr (name
, ELF_VER_CHR
);
628 if (version
> name
&& version
[-1] != ELF_VER_CHR
)
629 h
->versioned
= versioned_hidden
;
631 h
->versioned
= versioned
;
635 /* Symbols defined in a linker script but not referenced anywhere
636 else will have non_elf set. */
639 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
643 switch (h
->root
.type
)
645 case bfd_link_hash_defined
:
646 case bfd_link_hash_defweak
:
647 case bfd_link_hash_common
:
649 case bfd_link_hash_undefweak
:
650 case bfd_link_hash_undefined
:
651 /* Since we're defining the symbol, don't let it seem to have not
652 been defined. record_dynamic_symbol and size_dynamic_sections
653 may depend on this. */
654 h
->root
.type
= bfd_link_hash_new
;
655 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
656 bfd_link_repair_undef_list (&htab
->root
);
658 case bfd_link_hash_new
:
660 case bfd_link_hash_indirect
:
661 /* We had a versioned symbol in a dynamic library. We make the
662 the versioned symbol point to this one. */
663 bed
= get_elf_backend_data (output_bfd
);
665 while (hv
->root
.type
== bfd_link_hash_indirect
666 || hv
->root
.type
== bfd_link_hash_warning
)
667 hv
= (struct elf_link_hash_entry
*) hv
->root
.u
.i
.link
;
668 /* We don't need to update h->root.u since linker will set them
670 h
->root
.type
= bfd_link_hash_undefined
;
671 hv
->root
.type
= bfd_link_hash_indirect
;
672 hv
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
673 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hv
);
680 /* If this symbol is being provided by the linker script, and it is
681 currently defined by a dynamic object, but not by a regular
682 object, then mark it as undefined so that the generic linker will
683 force the correct value. */
687 h
->root
.type
= bfd_link_hash_undefined
;
689 /* If this symbol is not being provided by the linker script, and it is
690 currently defined by a dynamic object, but not by a regular object,
691 then clear out any version information because the symbol will not be
692 associated with the dynamic object any more. */
696 h
->verinfo
.verdef
= NULL
;
698 /* Make sure this symbol is not garbage collected. */
705 bed
= get_elf_backend_data (output_bfd
);
706 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
707 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
708 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
711 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
713 if (!bfd_link_relocatable (info
)
715 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
716 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
721 || bfd_link_dll (info
)
722 || elf_hash_table (info
)->is_relocatable_executable
)
726 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
729 /* If this is a weak defined symbol, and we know a corresponding
730 real symbol from the same dynamic object, make sure the real
731 symbol is also made into a dynamic symbol. */
734 struct elf_link_hash_entry
*def
= weakdef (h
);
736 if (def
->dynindx
== -1
737 && !bfd_elf_link_record_dynamic_symbol (info
, def
))
745 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
746 success, and 2 on a failure caused by attempting to record a symbol
747 in a discarded section, eg. a discarded link-once section symbol. */
750 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
755 struct elf_link_local_dynamic_entry
*entry
;
756 struct elf_link_hash_table
*eht
;
757 struct elf_strtab_hash
*dynstr
;
760 Elf_External_Sym_Shndx eshndx
;
761 char esym
[sizeof (Elf64_External_Sym
)];
763 if (! is_elf_hash_table (info
->hash
))
766 /* See if the entry exists already. */
767 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
768 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
771 amt
= sizeof (*entry
);
772 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
776 /* Go find the symbol, so that we can find it's name. */
777 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
778 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
780 bfd_release (input_bfd
, entry
);
784 if (entry
->isym
.st_shndx
!= SHN_UNDEF
785 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
789 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
790 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
792 /* We can still bfd_release here as nothing has done another
793 bfd_alloc. We can't do this later in this function. */
794 bfd_release (input_bfd
, entry
);
799 name
= (bfd_elf_string_from_elf_section
800 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
801 entry
->isym
.st_name
));
803 dynstr
= elf_hash_table (info
)->dynstr
;
806 /* Create a strtab to hold the dynamic symbol names. */
807 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
812 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
813 if (dynstr_index
== (size_t) -1)
815 entry
->isym
.st_name
= dynstr_index
;
817 eht
= elf_hash_table (info
);
819 entry
->next
= eht
->dynlocal
;
820 eht
->dynlocal
= entry
;
821 entry
->input_bfd
= input_bfd
;
822 entry
->input_indx
= input_indx
;
825 /* Whatever binding the symbol had before, it's now local. */
827 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
829 /* The dynindx will be set at the end of size_dynamic_sections. */
834 /* Return the dynindex of a local dynamic symbol. */
837 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
841 struct elf_link_local_dynamic_entry
*e
;
843 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
844 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
849 /* This function is used to renumber the dynamic symbols, if some of
850 them are removed because they are marked as local. This is called
851 via elf_link_hash_traverse. */
854 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
857 size_t *count
= (size_t *) data
;
862 if (h
->dynindx
!= -1)
863 h
->dynindx
= ++(*count
);
869 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
870 STB_LOCAL binding. */
873 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
876 size_t *count
= (size_t *) data
;
878 if (!h
->forced_local
)
881 if (h
->dynindx
!= -1)
882 h
->dynindx
= ++(*count
);
887 /* Return true if the dynamic symbol for a given section should be
888 omitted when creating a shared library. */
890 _bfd_elf_omit_section_dynsym_default (bfd
*output_bfd ATTRIBUTE_UNUSED
,
891 struct bfd_link_info
*info
,
894 struct elf_link_hash_table
*htab
;
897 switch (elf_section_data (p
)->this_hdr
.sh_type
)
901 /* If sh_type is yet undecided, assume it could be
902 SHT_PROGBITS/SHT_NOBITS. */
904 htab
= elf_hash_table (info
);
905 if (p
== htab
->tls_sec
)
908 if (htab
->text_index_section
!= NULL
)
909 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
911 return (htab
->dynobj
!= NULL
912 && (ip
= bfd_get_linker_section (htab
->dynobj
, p
->name
)) != NULL
913 && ip
->output_section
== p
);
915 /* There shouldn't be section relative relocations
916 against any other section. */
923 _bfd_elf_omit_section_dynsym_all
924 (bfd
*output_bfd ATTRIBUTE_UNUSED
,
925 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
926 asection
*p ATTRIBUTE_UNUSED
)
931 /* Assign dynsym indices. In a shared library we generate a section
932 symbol for each output section, which come first. Next come symbols
933 which have been forced to local binding. Then all of the back-end
934 allocated local dynamic syms, followed by the rest of the global
935 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
936 (This prevents the early call before elf_backend_init_index_section
937 and strip_excluded_output_sections setting dynindx for sections
938 that are stripped.) */
941 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
942 struct bfd_link_info
*info
,
943 unsigned long *section_sym_count
)
945 unsigned long dynsymcount
= 0;
946 bfd_boolean do_sec
= section_sym_count
!= NULL
;
948 if (bfd_link_pic (info
)
949 || elf_hash_table (info
)->is_relocatable_executable
)
951 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
953 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
954 if ((p
->flags
& SEC_EXCLUDE
) == 0
955 && (p
->flags
& SEC_ALLOC
) != 0
956 && elf_hash_table (info
)->dynamic_relocs
957 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
961 elf_section_data (p
)->dynindx
= dynsymcount
;
964 elf_section_data (p
)->dynindx
= 0;
967 *section_sym_count
= dynsymcount
;
969 elf_link_hash_traverse (elf_hash_table (info
),
970 elf_link_renumber_local_hash_table_dynsyms
,
973 if (elf_hash_table (info
)->dynlocal
)
975 struct elf_link_local_dynamic_entry
*p
;
976 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
977 p
->dynindx
= ++dynsymcount
;
979 elf_hash_table (info
)->local_dynsymcount
= dynsymcount
;
981 elf_link_hash_traverse (elf_hash_table (info
),
982 elf_link_renumber_hash_table_dynsyms
,
985 /* There is an unused NULL entry at the head of the table which we
986 must account for in our count even if the table is empty since it
987 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
991 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
995 /* Merge st_other field. */
998 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
999 const Elf_Internal_Sym
*isym
, asection
*sec
,
1000 bfd_boolean definition
, bfd_boolean dynamic
)
1002 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1004 /* If st_other has a processor-specific meaning, specific
1005 code might be needed here. */
1006 if (bed
->elf_backend_merge_symbol_attribute
)
1007 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
1012 unsigned symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
1013 unsigned hvis
= ELF_ST_VISIBILITY (h
->other
);
1015 /* Keep the most constraining visibility. Leave the remainder
1016 of the st_other field to elf_backend_merge_symbol_attribute. */
1017 if (symvis
- 1 < hvis
- 1)
1018 h
->other
= symvis
| (h
->other
& ~ELF_ST_VISIBILITY (-1));
1021 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_DEFAULT
1022 && (sec
->flags
& SEC_READONLY
) == 0)
1023 h
->protected_def
= 1;
1026 /* This function is called when we want to merge a new symbol with an
1027 existing symbol. It handles the various cases which arise when we
1028 find a definition in a dynamic object, or when there is already a
1029 definition in a dynamic object. The new symbol is described by
1030 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1031 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1032 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1033 of an old common symbol. We set OVERRIDE if the old symbol is
1034 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1035 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1036 to change. By OK to change, we mean that we shouldn't warn if the
1037 type or size does change. */
1040 _bfd_elf_merge_symbol (bfd
*abfd
,
1041 struct bfd_link_info
*info
,
1043 Elf_Internal_Sym
*sym
,
1046 struct elf_link_hash_entry
**sym_hash
,
1048 bfd_boolean
*pold_weak
,
1049 unsigned int *pold_alignment
,
1051 bfd_boolean
*override
,
1052 bfd_boolean
*type_change_ok
,
1053 bfd_boolean
*size_change_ok
,
1054 bfd_boolean
*matched
)
1056 asection
*sec
, *oldsec
;
1057 struct elf_link_hash_entry
*h
;
1058 struct elf_link_hash_entry
*hi
;
1059 struct elf_link_hash_entry
*flip
;
1062 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
1063 bfd_boolean newweak
, oldweak
, newfunc
, oldfunc
;
1064 const struct elf_backend_data
*bed
;
1066 bfd_boolean default_sym
= *matched
;
1072 bind
= ELF_ST_BIND (sym
->st_info
);
1074 if (! bfd_is_und_section (sec
))
1075 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
1077 h
= ((struct elf_link_hash_entry
*)
1078 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
1083 bed
= get_elf_backend_data (abfd
);
1085 /* NEW_VERSION is the symbol version of the new symbol. */
1086 if (h
->versioned
!= unversioned
)
1088 /* Symbol version is unknown or versioned. */
1089 new_version
= strrchr (name
, ELF_VER_CHR
);
1092 if (h
->versioned
== unknown
)
1094 if (new_version
> name
&& new_version
[-1] != ELF_VER_CHR
)
1095 h
->versioned
= versioned_hidden
;
1097 h
->versioned
= versioned
;
1100 if (new_version
[0] == '\0')
1104 h
->versioned
= unversioned
;
1109 /* For merging, we only care about real symbols. But we need to make
1110 sure that indirect symbol dynamic flags are updated. */
1112 while (h
->root
.type
== bfd_link_hash_indirect
1113 || h
->root
.type
== bfd_link_hash_warning
)
1114 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1118 if (hi
== h
|| h
->root
.type
== bfd_link_hash_new
)
1122 /* OLD_HIDDEN is true if the existing symbol is only visible
1123 to the symbol with the same symbol version. NEW_HIDDEN is
1124 true if the new symbol is only visible to the symbol with
1125 the same symbol version. */
1126 bfd_boolean old_hidden
= h
->versioned
== versioned_hidden
;
1127 bfd_boolean new_hidden
= hi
->versioned
== versioned_hidden
;
1128 if (!old_hidden
&& !new_hidden
)
1129 /* The new symbol matches the existing symbol if both
1134 /* OLD_VERSION is the symbol version of the existing
1138 if (h
->versioned
>= versioned
)
1139 old_version
= strrchr (h
->root
.root
.string
,
1144 /* The new symbol matches the existing symbol if they
1145 have the same symbol version. */
1146 *matched
= (old_version
== new_version
1147 || (old_version
!= NULL
1148 && new_version
!= NULL
1149 && strcmp (old_version
, new_version
) == 0));
1154 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1159 switch (h
->root
.type
)
1164 case bfd_link_hash_undefined
:
1165 case bfd_link_hash_undefweak
:
1166 oldbfd
= h
->root
.u
.undef
.abfd
;
1169 case bfd_link_hash_defined
:
1170 case bfd_link_hash_defweak
:
1171 oldbfd
= h
->root
.u
.def
.section
->owner
;
1172 oldsec
= h
->root
.u
.def
.section
;
1175 case bfd_link_hash_common
:
1176 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1177 oldsec
= h
->root
.u
.c
.p
->section
;
1179 *pold_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1182 if (poldbfd
&& *poldbfd
== NULL
)
1185 /* Differentiate strong and weak symbols. */
1186 newweak
= bind
== STB_WEAK
;
1187 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1188 || h
->root
.type
== bfd_link_hash_undefweak
);
1190 *pold_weak
= oldweak
;
1192 /* We have to check it for every instance since the first few may be
1193 references and not all compilers emit symbol type for undefined
1195 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1197 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1198 respectively, is from a dynamic object. */
1200 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1202 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1203 syms and defined syms in dynamic libraries respectively.
1204 ref_dynamic on the other hand can be set for a symbol defined in
1205 a dynamic library, and def_dynamic may not be set; When the
1206 definition in a dynamic lib is overridden by a definition in the
1207 executable use of the symbol in the dynamic lib becomes a
1208 reference to the executable symbol. */
1211 if (bfd_is_und_section (sec
))
1213 if (bind
!= STB_WEAK
)
1215 h
->ref_dynamic_nonweak
= 1;
1216 hi
->ref_dynamic_nonweak
= 1;
1221 /* Update the existing symbol only if they match. */
1224 hi
->dynamic_def
= 1;
1228 /* If we just created the symbol, mark it as being an ELF symbol.
1229 Other than that, there is nothing to do--there is no merge issue
1230 with a newly defined symbol--so we just return. */
1232 if (h
->root
.type
== bfd_link_hash_new
)
1238 /* In cases involving weak versioned symbols, we may wind up trying
1239 to merge a symbol with itself. Catch that here, to avoid the
1240 confusion that results if we try to override a symbol with
1241 itself. The additional tests catch cases like
1242 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1243 dynamic object, which we do want to handle here. */
1245 && (newweak
|| oldweak
)
1246 && ((abfd
->flags
& DYNAMIC
) == 0
1247 || !h
->def_regular
))
1252 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1253 else if (oldsec
!= NULL
)
1255 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1256 indices used by MIPS ELF. */
1257 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1260 /* Handle a case where plugin_notice won't be called and thus won't
1261 set the non_ir_ref flags on the first pass over symbols. */
1263 && (oldbfd
->flags
& BFD_PLUGIN
) != (abfd
->flags
& BFD_PLUGIN
)
1264 && newdyn
!= olddyn
)
1266 h
->root
.non_ir_ref_dynamic
= TRUE
;
1267 hi
->root
.non_ir_ref_dynamic
= TRUE
;
1270 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1271 respectively, appear to be a definition rather than reference. */
1273 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1275 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1276 && h
->root
.type
!= bfd_link_hash_undefweak
1277 && h
->root
.type
!= bfd_link_hash_common
);
1279 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1280 respectively, appear to be a function. */
1282 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1283 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1285 oldfunc
= (h
->type
!= STT_NOTYPE
1286 && bed
->is_function_type (h
->type
));
1288 if (!(newfunc
&& oldfunc
)
1289 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1290 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1291 && h
->type
!= STT_NOTYPE
1292 && (newdef
|| bfd_is_com_section (sec
))
1293 && (olddef
|| h
->root
.type
== bfd_link_hash_common
))
1295 /* If creating a default indirect symbol ("foo" or "foo@") from
1296 a dynamic versioned definition ("foo@@") skip doing so if
1297 there is an existing regular definition with a different
1298 type. We don't want, for example, a "time" variable in the
1299 executable overriding a "time" function in a shared library. */
1307 /* When adding a symbol from a regular object file after we have
1308 created indirect symbols, undo the indirection and any
1315 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1316 h
->forced_local
= 0;
1320 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1322 h
->root
.type
= bfd_link_hash_undefined
;
1323 h
->root
.u
.undef
.abfd
= abfd
;
1327 h
->root
.type
= bfd_link_hash_new
;
1328 h
->root
.u
.undef
.abfd
= NULL
;
1334 /* Check TLS symbols. We don't check undefined symbols introduced
1335 by "ld -u" which have no type (and oldbfd NULL), and we don't
1336 check symbols from plugins because they also have no type. */
1338 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1339 && (abfd
->flags
& BFD_PLUGIN
) == 0
1340 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1341 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1344 bfd_boolean ntdef
, tdef
;
1345 asection
*ntsec
, *tsec
;
1347 if (h
->type
== STT_TLS
)
1368 /* xgettext:c-format */
1369 (_("%s: TLS definition in %pB section %pA "
1370 "mismatches non-TLS definition in %pB section %pA"),
1371 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
, ntsec
);
1372 else if (!tdef
&& !ntdef
)
1374 /* xgettext:c-format */
1375 (_("%s: TLS reference in %pB "
1376 "mismatches non-TLS reference in %pB"),
1377 h
->root
.root
.string
, tbfd
, ntbfd
);
1380 /* xgettext:c-format */
1381 (_("%s: TLS definition in %pB section %pA "
1382 "mismatches non-TLS reference in %pB"),
1383 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
);
1386 /* xgettext:c-format */
1387 (_("%s: TLS reference in %pB "
1388 "mismatches non-TLS definition in %pB section %pA"),
1389 h
->root
.root
.string
, tbfd
, ntbfd
, ntsec
);
1391 bfd_set_error (bfd_error_bad_value
);
1395 /* If the old symbol has non-default visibility, we ignore the new
1396 definition from a dynamic object. */
1398 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1399 && !bfd_is_und_section (sec
))
1402 /* Make sure this symbol is dynamic. */
1404 hi
->ref_dynamic
= 1;
1405 /* A protected symbol has external availability. Make sure it is
1406 recorded as dynamic.
1408 FIXME: Should we check type and size for protected symbol? */
1409 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1410 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1415 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1418 /* If the new symbol with non-default visibility comes from a
1419 relocatable file and the old definition comes from a dynamic
1420 object, we remove the old definition. */
1421 if (hi
->root
.type
== bfd_link_hash_indirect
)
1423 /* Handle the case where the old dynamic definition is
1424 default versioned. We need to copy the symbol info from
1425 the symbol with default version to the normal one if it
1426 was referenced before. */
1429 hi
->root
.type
= h
->root
.type
;
1430 h
->root
.type
= bfd_link_hash_indirect
;
1431 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1433 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1434 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1436 /* If the new symbol is hidden or internal, completely undo
1437 any dynamic link state. */
1438 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1439 h
->forced_local
= 0;
1446 /* FIXME: Should we check type and size for protected symbol? */
1456 /* If the old symbol was undefined before, then it will still be
1457 on the undefs list. If the new symbol is undefined or
1458 common, we can't make it bfd_link_hash_new here, because new
1459 undefined or common symbols will be added to the undefs list
1460 by _bfd_generic_link_add_one_symbol. Symbols may not be
1461 added twice to the undefs list. Also, if the new symbol is
1462 undefweak then we don't want to lose the strong undef. */
1463 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1465 h
->root
.type
= bfd_link_hash_undefined
;
1466 h
->root
.u
.undef
.abfd
= abfd
;
1470 h
->root
.type
= bfd_link_hash_new
;
1471 h
->root
.u
.undef
.abfd
= NULL
;
1474 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1476 /* If the new symbol is hidden or internal, completely undo
1477 any dynamic link state. */
1478 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1479 h
->forced_local
= 0;
1485 /* FIXME: Should we check type and size for protected symbol? */
1491 /* If a new weak symbol definition comes from a regular file and the
1492 old symbol comes from a dynamic library, we treat the new one as
1493 strong. Similarly, an old weak symbol definition from a regular
1494 file is treated as strong when the new symbol comes from a dynamic
1495 library. Further, an old weak symbol from a dynamic library is
1496 treated as strong if the new symbol is from a dynamic library.
1497 This reflects the way glibc's ld.so works.
1499 Also allow a weak symbol to override a linker script symbol
1500 defined by an early pass over the script. This is done so the
1501 linker knows the symbol is defined in an object file, for the
1502 DEFINED script function.
1504 Do this before setting *type_change_ok or *size_change_ok so that
1505 we warn properly when dynamic library symbols are overridden. */
1507 if (newdef
&& !newdyn
&& (olddyn
|| h
->root
.ldscript_def
))
1509 if (olddef
&& newdyn
)
1512 /* Allow changes between different types of function symbol. */
1513 if (newfunc
&& oldfunc
)
1514 *type_change_ok
= TRUE
;
1516 /* It's OK to change the type if either the existing symbol or the
1517 new symbol is weak. A type change is also OK if the old symbol
1518 is undefined and the new symbol is defined. */
1523 && h
->root
.type
== bfd_link_hash_undefined
))
1524 *type_change_ok
= TRUE
;
1526 /* It's OK to change the size if either the existing symbol or the
1527 new symbol is weak, or if the old symbol is undefined. */
1530 || h
->root
.type
== bfd_link_hash_undefined
)
1531 *size_change_ok
= TRUE
;
1533 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1534 symbol, respectively, appears to be a common symbol in a dynamic
1535 object. If a symbol appears in an uninitialized section, and is
1536 not weak, and is not a function, then it may be a common symbol
1537 which was resolved when the dynamic object was created. We want
1538 to treat such symbols specially, because they raise special
1539 considerations when setting the symbol size: if the symbol
1540 appears as a common symbol in a regular object, and the size in
1541 the regular object is larger, we must make sure that we use the
1542 larger size. This problematic case can always be avoided in C,
1543 but it must be handled correctly when using Fortran shared
1546 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1547 likewise for OLDDYNCOMMON and OLDDEF.
1549 Note that this test is just a heuristic, and that it is quite
1550 possible to have an uninitialized symbol in a shared object which
1551 is really a definition, rather than a common symbol. This could
1552 lead to some minor confusion when the symbol really is a common
1553 symbol in some regular object. However, I think it will be
1559 && (sec
->flags
& SEC_ALLOC
) != 0
1560 && (sec
->flags
& SEC_LOAD
) == 0
1563 newdyncommon
= TRUE
;
1565 newdyncommon
= FALSE
;
1569 && h
->root
.type
== bfd_link_hash_defined
1571 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1572 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1575 olddyncommon
= TRUE
;
1577 olddyncommon
= FALSE
;
1579 /* We now know everything about the old and new symbols. We ask the
1580 backend to check if we can merge them. */
1581 if (bed
->merge_symbol
!= NULL
)
1583 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1588 /* There are multiple definitions of a normal symbol. Skip the
1589 default symbol as well as definition from an IR object. */
1590 if (olddef
&& !olddyn
&& !oldweak
&& newdef
&& !newdyn
&& !newweak
1591 && !default_sym
&& h
->def_regular
1593 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1594 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1596 /* Handle a multiple definition. */
1597 (*info
->callbacks
->multiple_definition
) (info
, &h
->root
,
1598 abfd
, sec
, *pvalue
);
1603 /* If both the old and the new symbols look like common symbols in a
1604 dynamic object, set the size of the symbol to the larger of the
1609 && sym
->st_size
!= h
->size
)
1611 /* Since we think we have two common symbols, issue a multiple
1612 common warning if desired. Note that we only warn if the
1613 size is different. If the size is the same, we simply let
1614 the old symbol override the new one as normally happens with
1615 symbols defined in dynamic objects. */
1617 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1618 bfd_link_hash_common
, sym
->st_size
);
1619 if (sym
->st_size
> h
->size
)
1620 h
->size
= sym
->st_size
;
1622 *size_change_ok
= TRUE
;
1625 /* If we are looking at a dynamic object, and we have found a
1626 definition, we need to see if the symbol was already defined by
1627 some other object. If so, we want to use the existing
1628 definition, and we do not want to report a multiple symbol
1629 definition error; we do this by clobbering *PSEC to be
1630 bfd_und_section_ptr.
1632 We treat a common symbol as a definition if the symbol in the
1633 shared library is a function, since common symbols always
1634 represent variables; this can cause confusion in principle, but
1635 any such confusion would seem to indicate an erroneous program or
1636 shared library. We also permit a common symbol in a regular
1637 object to override a weak symbol in a shared object. */
1642 || (h
->root
.type
== bfd_link_hash_common
1643 && (newweak
|| newfunc
))))
1647 newdyncommon
= FALSE
;
1649 *psec
= sec
= bfd_und_section_ptr
;
1650 *size_change_ok
= TRUE
;
1652 /* If we get here when the old symbol is a common symbol, then
1653 we are explicitly letting it override a weak symbol or
1654 function in a dynamic object, and we don't want to warn about
1655 a type change. If the old symbol is a defined symbol, a type
1656 change warning may still be appropriate. */
1658 if (h
->root
.type
== bfd_link_hash_common
)
1659 *type_change_ok
= TRUE
;
1662 /* Handle the special case of an old common symbol merging with a
1663 new symbol which looks like a common symbol in a shared object.
1664 We change *PSEC and *PVALUE to make the new symbol look like a
1665 common symbol, and let _bfd_generic_link_add_one_symbol do the
1669 && h
->root
.type
== bfd_link_hash_common
)
1673 newdyncommon
= FALSE
;
1674 *pvalue
= sym
->st_size
;
1675 *psec
= sec
= bed
->common_section (oldsec
);
1676 *size_change_ok
= TRUE
;
1679 /* Skip weak definitions of symbols that are already defined. */
1680 if (newdef
&& olddef
&& newweak
)
1682 /* Don't skip new non-IR weak syms. */
1683 if (!(oldbfd
!= NULL
1684 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1685 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1691 /* Merge st_other. If the symbol already has a dynamic index,
1692 but visibility says it should not be visible, turn it into a
1694 elf_merge_st_other (abfd
, h
, sym
, sec
, newdef
, newdyn
);
1695 if (h
->dynindx
!= -1)
1696 switch (ELF_ST_VISIBILITY (h
->other
))
1700 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1705 /* If the old symbol is from a dynamic object, and the new symbol is
1706 a definition which is not from a dynamic object, then the new
1707 symbol overrides the old symbol. Symbols from regular files
1708 always take precedence over symbols from dynamic objects, even if
1709 they are defined after the dynamic object in the link.
1711 As above, we again permit a common symbol in a regular object to
1712 override a definition in a shared object if the shared object
1713 symbol is a function or is weak. */
1718 || (bfd_is_com_section (sec
)
1719 && (oldweak
|| oldfunc
)))
1724 /* Change the hash table entry to undefined, and let
1725 _bfd_generic_link_add_one_symbol do the right thing with the
1728 h
->root
.type
= bfd_link_hash_undefined
;
1729 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1730 *size_change_ok
= TRUE
;
1733 olddyncommon
= FALSE
;
1735 /* We again permit a type change when a common symbol may be
1736 overriding a function. */
1738 if (bfd_is_com_section (sec
))
1742 /* If a common symbol overrides a function, make sure
1743 that it isn't defined dynamically nor has type
1746 h
->type
= STT_NOTYPE
;
1748 *type_change_ok
= TRUE
;
1751 if (hi
->root
.type
== bfd_link_hash_indirect
)
1754 /* This union may have been set to be non-NULL when this symbol
1755 was seen in a dynamic object. We must force the union to be
1756 NULL, so that it is correct for a regular symbol. */
1757 h
->verinfo
.vertree
= NULL
;
1760 /* Handle the special case of a new common symbol merging with an
1761 old symbol that looks like it might be a common symbol defined in
1762 a shared object. Note that we have already handled the case in
1763 which a new common symbol should simply override the definition
1764 in the shared library. */
1767 && bfd_is_com_section (sec
)
1770 /* It would be best if we could set the hash table entry to a
1771 common symbol, but we don't know what to use for the section
1772 or the alignment. */
1773 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1774 bfd_link_hash_common
, sym
->st_size
);
1776 /* If the presumed common symbol in the dynamic object is
1777 larger, pretend that the new symbol has its size. */
1779 if (h
->size
> *pvalue
)
1782 /* We need to remember the alignment required by the symbol
1783 in the dynamic object. */
1784 BFD_ASSERT (pold_alignment
);
1785 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1788 olddyncommon
= FALSE
;
1790 h
->root
.type
= bfd_link_hash_undefined
;
1791 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1793 *size_change_ok
= TRUE
;
1794 *type_change_ok
= TRUE
;
1796 if (hi
->root
.type
== bfd_link_hash_indirect
)
1799 h
->verinfo
.vertree
= NULL
;
1804 /* Handle the case where we had a versioned symbol in a dynamic
1805 library and now find a definition in a normal object. In this
1806 case, we make the versioned symbol point to the normal one. */
1807 flip
->root
.type
= h
->root
.type
;
1808 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1809 h
->root
.type
= bfd_link_hash_indirect
;
1810 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1811 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1815 flip
->ref_dynamic
= 1;
1822 /* This function is called to create an indirect symbol from the
1823 default for the symbol with the default version if needed. The
1824 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1825 set DYNSYM if the new indirect symbol is dynamic. */
1828 _bfd_elf_add_default_symbol (bfd
*abfd
,
1829 struct bfd_link_info
*info
,
1830 struct elf_link_hash_entry
*h
,
1832 Elf_Internal_Sym
*sym
,
1836 bfd_boolean
*dynsym
)
1838 bfd_boolean type_change_ok
;
1839 bfd_boolean size_change_ok
;
1842 struct elf_link_hash_entry
*hi
;
1843 struct bfd_link_hash_entry
*bh
;
1844 const struct elf_backend_data
*bed
;
1845 bfd_boolean collect
;
1846 bfd_boolean dynamic
;
1847 bfd_boolean override
;
1849 size_t len
, shortlen
;
1851 bfd_boolean matched
;
1853 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1856 /* If this symbol has a version, and it is the default version, we
1857 create an indirect symbol from the default name to the fully
1858 decorated name. This will cause external references which do not
1859 specify a version to be bound to this version of the symbol. */
1860 p
= strchr (name
, ELF_VER_CHR
);
1861 if (h
->versioned
== unknown
)
1865 h
->versioned
= unversioned
;
1870 if (p
[1] != ELF_VER_CHR
)
1872 h
->versioned
= versioned_hidden
;
1876 h
->versioned
= versioned
;
1881 /* PR ld/19073: We may see an unversioned definition after the
1887 bed
= get_elf_backend_data (abfd
);
1888 collect
= bed
->collect
;
1889 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1891 shortlen
= p
- name
;
1892 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1893 if (shortname
== NULL
)
1895 memcpy (shortname
, name
, shortlen
);
1896 shortname
[shortlen
] = '\0';
1898 /* We are going to create a new symbol. Merge it with any existing
1899 symbol with this name. For the purposes of the merge, act as
1900 though we were defining the symbol we just defined, although we
1901 actually going to define an indirect symbol. */
1902 type_change_ok
= FALSE
;
1903 size_change_ok
= FALSE
;
1906 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1907 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1908 &type_change_ok
, &size_change_ok
, &matched
))
1914 if (hi
->def_regular
)
1916 /* If the undecorated symbol will have a version added by a
1917 script different to H, then don't indirect to/from the
1918 undecorated symbol. This isn't ideal because we may not yet
1919 have seen symbol versions, if given by a script on the
1920 command line rather than via --version-script. */
1921 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
1926 = bfd_find_version_for_sym (info
->version_info
,
1927 hi
->root
.root
.string
, &hide
);
1928 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
1930 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
1934 if (hi
->verinfo
.vertree
!= NULL
1935 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
1941 /* Add the default symbol if not performing a relocatable link. */
1942 if (! bfd_link_relocatable (info
))
1945 if (! (_bfd_generic_link_add_one_symbol
1946 (info
, abfd
, shortname
, BSF_INDIRECT
,
1947 bfd_ind_section_ptr
,
1948 0, name
, FALSE
, collect
, &bh
)))
1950 hi
= (struct elf_link_hash_entry
*) bh
;
1955 /* In this case the symbol named SHORTNAME is overriding the
1956 indirect symbol we want to add. We were planning on making
1957 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1958 is the name without a version. NAME is the fully versioned
1959 name, and it is the default version.
1961 Overriding means that we already saw a definition for the
1962 symbol SHORTNAME in a regular object, and it is overriding
1963 the symbol defined in the dynamic object.
1965 When this happens, we actually want to change NAME, the
1966 symbol we just added, to refer to SHORTNAME. This will cause
1967 references to NAME in the shared object to become references
1968 to SHORTNAME in the regular object. This is what we expect
1969 when we override a function in a shared object: that the
1970 references in the shared object will be mapped to the
1971 definition in the regular object. */
1973 while (hi
->root
.type
== bfd_link_hash_indirect
1974 || hi
->root
.type
== bfd_link_hash_warning
)
1975 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1977 h
->root
.type
= bfd_link_hash_indirect
;
1978 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1982 hi
->ref_dynamic
= 1;
1986 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1991 /* Now set HI to H, so that the following code will set the
1992 other fields correctly. */
1996 /* Check if HI is a warning symbol. */
1997 if (hi
->root
.type
== bfd_link_hash_warning
)
1998 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2000 /* If there is a duplicate definition somewhere, then HI may not
2001 point to an indirect symbol. We will have reported an error to
2002 the user in that case. */
2004 if (hi
->root
.type
== bfd_link_hash_indirect
)
2006 struct elf_link_hash_entry
*ht
;
2008 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2009 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
2011 /* A reference to the SHORTNAME symbol from a dynamic library
2012 will be satisfied by the versioned symbol at runtime. In
2013 effect, we have a reference to the versioned symbol. */
2014 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2015 hi
->dynamic_def
|= ht
->dynamic_def
;
2017 /* See if the new flags lead us to realize that the symbol must
2023 if (! bfd_link_executable (info
)
2030 if (hi
->ref_regular
)
2036 /* We also need to define an indirection from the nondefault version
2040 len
= strlen (name
);
2041 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
2042 if (shortname
== NULL
)
2044 memcpy (shortname
, name
, shortlen
);
2045 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
2047 /* Once again, merge with any existing symbol. */
2048 type_change_ok
= FALSE
;
2049 size_change_ok
= FALSE
;
2051 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
2052 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
2053 &type_change_ok
, &size_change_ok
, &matched
))
2061 /* Here SHORTNAME is a versioned name, so we don't expect to see
2062 the type of override we do in the case above unless it is
2063 overridden by a versioned definition. */
2064 if (hi
->root
.type
!= bfd_link_hash_defined
2065 && hi
->root
.type
!= bfd_link_hash_defweak
)
2067 /* xgettext:c-format */
2068 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2074 if (! (_bfd_generic_link_add_one_symbol
2075 (info
, abfd
, shortname
, BSF_INDIRECT
,
2076 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
2078 hi
= (struct elf_link_hash_entry
*) bh
;
2080 /* If there is a duplicate definition somewhere, then HI may not
2081 point to an indirect symbol. We will have reported an error
2082 to the user in that case. */
2084 if (hi
->root
.type
== bfd_link_hash_indirect
)
2086 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
2087 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2088 hi
->dynamic_def
|= h
->dynamic_def
;
2090 /* See if the new flags lead us to realize that the symbol
2096 if (! bfd_link_executable (info
)
2102 if (hi
->ref_regular
)
2112 /* This routine is used to export all defined symbols into the dynamic
2113 symbol table. It is called via elf_link_hash_traverse. */
2116 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
2118 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2120 /* Ignore indirect symbols. These are added by the versioning code. */
2121 if (h
->root
.type
== bfd_link_hash_indirect
)
2124 /* Ignore this if we won't export it. */
2125 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2128 if (h
->dynindx
== -1
2129 && (h
->def_regular
|| h
->ref_regular
)
2130 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2131 h
->root
.root
.string
))
2133 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2143 /* Look through the symbols which are defined in other shared
2144 libraries and referenced here. Update the list of version
2145 dependencies. This will be put into the .gnu.version_r section.
2146 This function is called via elf_link_hash_traverse. */
2149 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2152 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2153 Elf_Internal_Verneed
*t
;
2154 Elf_Internal_Vernaux
*a
;
2157 /* We only care about symbols defined in shared objects with version
2162 || h
->verinfo
.verdef
== NULL
2163 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2164 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2167 /* See if we already know about this version. */
2168 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2172 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2175 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2176 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2182 /* This is a new version. Add it to tree we are building. */
2187 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2190 rinfo
->failed
= TRUE
;
2194 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2195 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2196 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2200 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2203 rinfo
->failed
= TRUE
;
2207 /* Note that we are copying a string pointer here, and testing it
2208 above. If bfd_elf_string_from_elf_section is ever changed to
2209 discard the string data when low in memory, this will have to be
2211 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2213 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2214 a
->vna_nextptr
= t
->vn_auxptr
;
2216 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2219 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2226 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2227 hidden. Set *T_P to NULL if there is no match. */
2230 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info
*info
,
2231 struct elf_link_hash_entry
*h
,
2232 const char *version_p
,
2233 struct bfd_elf_version_tree
**t_p
,
2236 struct bfd_elf_version_tree
*t
;
2238 /* Look for the version. If we find it, it is no longer weak. */
2239 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
2241 if (strcmp (t
->name
, version_p
) == 0)
2245 struct bfd_elf_version_expr
*d
;
2247 len
= version_p
- h
->root
.root
.string
;
2248 alc
= (char *) bfd_malloc (len
);
2251 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2252 alc
[len
- 1] = '\0';
2253 if (alc
[len
- 2] == ELF_VER_CHR
)
2254 alc
[len
- 2] = '\0';
2256 h
->verinfo
.vertree
= t
;
2260 if (t
->globals
.list
!= NULL
)
2261 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2263 /* See if there is anything to force this symbol to
2265 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2267 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2270 && ! info
->export_dynamic
)
2284 /* Return TRUE if the symbol H is hidden by version script. */
2287 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info
*info
,
2288 struct elf_link_hash_entry
*h
)
2291 bfd_boolean hide
= FALSE
;
2292 const struct elf_backend_data
*bed
2293 = get_elf_backend_data (info
->output_bfd
);
2295 /* Version script only hides symbols defined in regular objects. */
2296 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2299 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2300 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2302 struct bfd_elf_version_tree
*t
;
2305 if (*p
== ELF_VER_CHR
)
2309 && _bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
)
2313 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2318 /* If we don't have a version for this symbol, see if we can find
2320 if (h
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
2323 = bfd_find_version_for_sym (info
->version_info
,
2324 h
->root
.root
.string
, &hide
);
2325 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2327 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2335 /* Figure out appropriate versions for all the symbols. We may not
2336 have the version number script until we have read all of the input
2337 files, so until that point we don't know which symbols should be
2338 local. This function is called via elf_link_hash_traverse. */
2341 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2343 struct elf_info_failed
*sinfo
;
2344 struct bfd_link_info
*info
;
2345 const struct elf_backend_data
*bed
;
2346 struct elf_info_failed eif
;
2350 sinfo
= (struct elf_info_failed
*) data
;
2353 /* Fix the symbol flags. */
2356 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2359 sinfo
->failed
= TRUE
;
2363 /* We only need version numbers for symbols defined in regular
2365 if (!h
->def_regular
)
2369 bed
= get_elf_backend_data (info
->output_bfd
);
2370 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2371 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2373 struct bfd_elf_version_tree
*t
;
2376 if (*p
== ELF_VER_CHR
)
2379 /* If there is no version string, we can just return out. */
2383 if (!_bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
))
2385 sinfo
->failed
= TRUE
;
2390 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2392 /* If we are building an application, we need to create a
2393 version node for this version. */
2394 if (t
== NULL
&& bfd_link_executable (info
))
2396 struct bfd_elf_version_tree
**pp
;
2399 /* If we aren't going to export this symbol, we don't need
2400 to worry about it. */
2401 if (h
->dynindx
== -1)
2404 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2408 sinfo
->failed
= TRUE
;
2413 t
->name_indx
= (unsigned int) -1;
2417 /* Don't count anonymous version tag. */
2418 if (sinfo
->info
->version_info
!= NULL
2419 && sinfo
->info
->version_info
->vernum
== 0)
2421 for (pp
= &sinfo
->info
->version_info
;
2425 t
->vernum
= version_index
;
2429 h
->verinfo
.vertree
= t
;
2433 /* We could not find the version for a symbol when
2434 generating a shared archive. Return an error. */
2436 /* xgettext:c-format */
2437 (_("%pB: version node not found for symbol %s"),
2438 info
->output_bfd
, h
->root
.root
.string
);
2439 bfd_set_error (bfd_error_bad_value
);
2440 sinfo
->failed
= TRUE
;
2445 /* If we don't have a version for this symbol, see if we can find
2448 && h
->verinfo
.vertree
== NULL
2449 && sinfo
->info
->version_info
!= NULL
)
2452 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2453 h
->root
.root
.string
, &hide
);
2454 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2455 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2461 /* Read and swap the relocs from the section indicated by SHDR. This
2462 may be either a REL or a RELA section. The relocations are
2463 translated into RELA relocations and stored in INTERNAL_RELOCS,
2464 which should have already been allocated to contain enough space.
2465 The EXTERNAL_RELOCS are a buffer where the external form of the
2466 relocations should be stored.
2468 Returns FALSE if something goes wrong. */
2471 elf_link_read_relocs_from_section (bfd
*abfd
,
2473 Elf_Internal_Shdr
*shdr
,
2474 void *external_relocs
,
2475 Elf_Internal_Rela
*internal_relocs
)
2477 const struct elf_backend_data
*bed
;
2478 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2479 const bfd_byte
*erela
;
2480 const bfd_byte
*erelaend
;
2481 Elf_Internal_Rela
*irela
;
2482 Elf_Internal_Shdr
*symtab_hdr
;
2485 /* Position ourselves at the start of the section. */
2486 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2489 /* Read the relocations. */
2490 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2493 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2494 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2496 bed
= get_elf_backend_data (abfd
);
2498 /* Convert the external relocations to the internal format. */
2499 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2500 swap_in
= bed
->s
->swap_reloc_in
;
2501 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2502 swap_in
= bed
->s
->swap_reloca_in
;
2505 bfd_set_error (bfd_error_wrong_format
);
2509 erela
= (const bfd_byte
*) external_relocs
;
2510 erelaend
= erela
+ shdr
->sh_size
;
2511 irela
= internal_relocs
;
2512 while (erela
< erelaend
)
2516 (*swap_in
) (abfd
, erela
, irela
);
2517 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2518 if (bed
->s
->arch_size
== 64)
2522 if ((size_t) r_symndx
>= nsyms
)
2525 /* xgettext:c-format */
2526 (_("%pB: bad reloc symbol index (%#" PRIx64
" >= %#lx)"
2527 " for offset %#" PRIx64
" in section `%pA'"),
2528 abfd
, (uint64_t) r_symndx
, (unsigned long) nsyms
,
2529 (uint64_t) irela
->r_offset
, sec
);
2530 bfd_set_error (bfd_error_bad_value
);
2534 else if (r_symndx
!= STN_UNDEF
)
2537 /* xgettext:c-format */
2538 (_("%pB: non-zero symbol index (%#" PRIx64
")"
2539 " for offset %#" PRIx64
" in section `%pA'"
2540 " when the object file has no symbol table"),
2541 abfd
, (uint64_t) r_symndx
,
2542 (uint64_t) irela
->r_offset
, sec
);
2543 bfd_set_error (bfd_error_bad_value
);
2546 irela
+= bed
->s
->int_rels_per_ext_rel
;
2547 erela
+= shdr
->sh_entsize
;
2553 /* Read and swap the relocs for a section O. They may have been
2554 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2555 not NULL, they are used as buffers to read into. They are known to
2556 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2557 the return value is allocated using either malloc or bfd_alloc,
2558 according to the KEEP_MEMORY argument. If O has two relocation
2559 sections (both REL and RELA relocations), then the REL_HDR
2560 relocations will appear first in INTERNAL_RELOCS, followed by the
2561 RELA_HDR relocations. */
2564 _bfd_elf_link_read_relocs (bfd
*abfd
,
2566 void *external_relocs
,
2567 Elf_Internal_Rela
*internal_relocs
,
2568 bfd_boolean keep_memory
)
2570 void *alloc1
= NULL
;
2571 Elf_Internal_Rela
*alloc2
= NULL
;
2572 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2573 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2574 Elf_Internal_Rela
*internal_rela_relocs
;
2576 if (esdo
->relocs
!= NULL
)
2577 return esdo
->relocs
;
2579 if (o
->reloc_count
== 0)
2582 if (internal_relocs
== NULL
)
2586 size
= (bfd_size_type
) o
->reloc_count
* sizeof (Elf_Internal_Rela
);
2588 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2590 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2591 if (internal_relocs
== NULL
)
2595 if (external_relocs
== NULL
)
2597 bfd_size_type size
= 0;
2600 size
+= esdo
->rel
.hdr
->sh_size
;
2602 size
+= esdo
->rela
.hdr
->sh_size
;
2604 alloc1
= bfd_malloc (size
);
2607 external_relocs
= alloc1
;
2610 internal_rela_relocs
= internal_relocs
;
2613 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2617 external_relocs
= (((bfd_byte
*) external_relocs
)
2618 + esdo
->rel
.hdr
->sh_size
);
2619 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2620 * bed
->s
->int_rels_per_ext_rel
);
2624 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2626 internal_rela_relocs
)))
2629 /* Cache the results for next time, if we can. */
2631 esdo
->relocs
= internal_relocs
;
2636 /* Don't free alloc2, since if it was allocated we are passing it
2637 back (under the name of internal_relocs). */
2639 return internal_relocs
;
2647 bfd_release (abfd
, alloc2
);
2654 /* Compute the size of, and allocate space for, REL_HDR which is the
2655 section header for a section containing relocations for O. */
2658 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2659 struct bfd_elf_section_reloc_data
*reldata
)
2661 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2663 /* That allows us to calculate the size of the section. */
2664 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2666 /* The contents field must last into write_object_contents, so we
2667 allocate it with bfd_alloc rather than malloc. Also since we
2668 cannot be sure that the contents will actually be filled in,
2669 we zero the allocated space. */
2670 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2671 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2674 if (reldata
->hashes
== NULL
&& reldata
->count
)
2676 struct elf_link_hash_entry
**p
;
2678 p
= ((struct elf_link_hash_entry
**)
2679 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2683 reldata
->hashes
= p
;
2689 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2690 originated from the section given by INPUT_REL_HDR) to the
2694 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2695 asection
*input_section
,
2696 Elf_Internal_Shdr
*input_rel_hdr
,
2697 Elf_Internal_Rela
*internal_relocs
,
2698 struct elf_link_hash_entry
**rel_hash
2701 Elf_Internal_Rela
*irela
;
2702 Elf_Internal_Rela
*irelaend
;
2704 struct bfd_elf_section_reloc_data
*output_reldata
;
2705 asection
*output_section
;
2706 const struct elf_backend_data
*bed
;
2707 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2708 struct bfd_elf_section_data
*esdo
;
2710 output_section
= input_section
->output_section
;
2712 bed
= get_elf_backend_data (output_bfd
);
2713 esdo
= elf_section_data (output_section
);
2714 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2716 output_reldata
= &esdo
->rel
;
2717 swap_out
= bed
->s
->swap_reloc_out
;
2719 else if (esdo
->rela
.hdr
2720 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2722 output_reldata
= &esdo
->rela
;
2723 swap_out
= bed
->s
->swap_reloca_out
;
2728 /* xgettext:c-format */
2729 (_("%pB: relocation size mismatch in %pB section %pA"),
2730 output_bfd
, input_section
->owner
, input_section
);
2731 bfd_set_error (bfd_error_wrong_format
);
2735 erel
= output_reldata
->hdr
->contents
;
2736 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2737 irela
= internal_relocs
;
2738 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2739 * bed
->s
->int_rels_per_ext_rel
);
2740 while (irela
< irelaend
)
2742 (*swap_out
) (output_bfd
, irela
, erel
);
2743 irela
+= bed
->s
->int_rels_per_ext_rel
;
2744 erel
+= input_rel_hdr
->sh_entsize
;
2747 /* Bump the counter, so that we know where to add the next set of
2749 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2754 /* Make weak undefined symbols in PIE dynamic. */
2757 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2758 struct elf_link_hash_entry
*h
)
2760 if (bfd_link_pie (info
)
2762 && h
->root
.type
== bfd_link_hash_undefweak
)
2763 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2768 /* Fix up the flags for a symbol. This handles various cases which
2769 can only be fixed after all the input files are seen. This is
2770 currently called by both adjust_dynamic_symbol and
2771 assign_sym_version, which is unnecessary but perhaps more robust in
2772 the face of future changes. */
2775 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2776 struct elf_info_failed
*eif
)
2778 const struct elf_backend_data
*bed
;
2780 /* If this symbol was mentioned in a non-ELF file, try to set
2781 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2782 permit a non-ELF file to correctly refer to a symbol defined in
2783 an ELF dynamic object. */
2786 while (h
->root
.type
== bfd_link_hash_indirect
)
2787 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2789 if (h
->root
.type
!= bfd_link_hash_defined
2790 && h
->root
.type
!= bfd_link_hash_defweak
)
2793 h
->ref_regular_nonweak
= 1;
2797 if (h
->root
.u
.def
.section
->owner
!= NULL
2798 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2799 == bfd_target_elf_flavour
))
2802 h
->ref_regular_nonweak
= 1;
2808 if (h
->dynindx
== -1
2812 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2821 /* Unfortunately, NON_ELF is only correct if the symbol
2822 was first seen in a non-ELF file. Fortunately, if the symbol
2823 was first seen in an ELF file, we're probably OK unless the
2824 symbol was defined in a non-ELF file. Catch that case here.
2825 FIXME: We're still in trouble if the symbol was first seen in
2826 a dynamic object, and then later in a non-ELF regular object. */
2827 if ((h
->root
.type
== bfd_link_hash_defined
2828 || h
->root
.type
== bfd_link_hash_defweak
)
2830 && (h
->root
.u
.def
.section
->owner
!= NULL
2831 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2832 != bfd_target_elf_flavour
)
2833 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2834 && !h
->def_dynamic
)))
2838 /* Backend specific symbol fixup. */
2839 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2840 if (bed
->elf_backend_fixup_symbol
2841 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2844 /* If this is a final link, and the symbol was defined as a common
2845 symbol in a regular object file, and there was no definition in
2846 any dynamic object, then the linker will have allocated space for
2847 the symbol in a common section but the DEF_REGULAR
2848 flag will not have been set. */
2849 if (h
->root
.type
== bfd_link_hash_defined
2853 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
2856 /* If a weak undefined symbol has non-default visibility, we also
2857 hide it from the dynamic linker. */
2858 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2859 && h
->root
.type
== bfd_link_hash_undefweak
)
2860 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2862 /* A hidden versioned symbol in executable should be forced local if
2863 it is is locally defined, not referenced by shared library and not
2865 else if (bfd_link_executable (eif
->info
)
2866 && h
->versioned
== versioned_hidden
2867 && !eif
->info
->export_dynamic
2871 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2873 /* If -Bsymbolic was used (which means to bind references to global
2874 symbols to the definition within the shared object), and this
2875 symbol was defined in a regular object, then it actually doesn't
2876 need a PLT entry. Likewise, if the symbol has non-default
2877 visibility. If the symbol has hidden or internal visibility, we
2878 will force it local. */
2879 else if (h
->needs_plt
2880 && bfd_link_pic (eif
->info
)
2881 && is_elf_hash_table (eif
->info
->hash
)
2882 && (SYMBOLIC_BIND (eif
->info
, h
)
2883 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2886 bfd_boolean force_local
;
2888 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2889 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2890 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2893 /* If this is a weak defined symbol in a dynamic object, and we know
2894 the real definition in the dynamic object, copy interesting flags
2895 over to the real definition. */
2896 if (h
->is_weakalias
)
2898 struct elf_link_hash_entry
*def
= weakdef (h
);
2900 /* If the real definition is defined by a regular object file,
2901 don't do anything special. See the longer description in
2902 _bfd_elf_adjust_dynamic_symbol, below. */
2903 if (def
->def_regular
)
2906 while ((h
= h
->u
.alias
) != def
)
2907 h
->is_weakalias
= 0;
2911 while (h
->root
.type
== bfd_link_hash_indirect
)
2912 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2913 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2914 || h
->root
.type
== bfd_link_hash_defweak
);
2915 BFD_ASSERT (def
->def_dynamic
);
2916 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
2917 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, def
, h
);
2924 /* Make the backend pick a good value for a dynamic symbol. This is
2925 called via elf_link_hash_traverse, and also calls itself
2929 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2931 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2932 struct elf_link_hash_table
*htab
;
2933 const struct elf_backend_data
*bed
;
2935 if (! is_elf_hash_table (eif
->info
->hash
))
2938 /* Ignore indirect symbols. These are added by the versioning code. */
2939 if (h
->root
.type
== bfd_link_hash_indirect
)
2942 /* Fix the symbol flags. */
2943 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2946 htab
= elf_hash_table (eif
->info
);
2947 bed
= get_elf_backend_data (htab
->dynobj
);
2949 if (h
->root
.type
== bfd_link_hash_undefweak
)
2951 if (eif
->info
->dynamic_undefined_weak
== 0)
2952 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2953 else if (eif
->info
->dynamic_undefined_weak
> 0
2955 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2956 && !bfd_hide_sym_by_version (eif
->info
->version_info
,
2957 h
->root
.root
.string
))
2959 if (!bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2967 /* If this symbol does not require a PLT entry, and it is not
2968 defined by a dynamic object, or is not referenced by a regular
2969 object, ignore it. We do have to handle a weak defined symbol,
2970 even if no regular object refers to it, if we decided to add it
2971 to the dynamic symbol table. FIXME: Do we normally need to worry
2972 about symbols which are defined by one dynamic object and
2973 referenced by another one? */
2975 && h
->type
!= STT_GNU_IFUNC
2979 && (!h
->is_weakalias
|| weakdef (h
)->dynindx
== -1))))
2981 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2985 /* If we've already adjusted this symbol, don't do it again. This
2986 can happen via a recursive call. */
2987 if (h
->dynamic_adjusted
)
2990 /* Don't look at this symbol again. Note that we must set this
2991 after checking the above conditions, because we may look at a
2992 symbol once, decide not to do anything, and then get called
2993 recursively later after REF_REGULAR is set below. */
2994 h
->dynamic_adjusted
= 1;
2996 /* If this is a weak definition, and we know a real definition, and
2997 the real symbol is not itself defined by a regular object file,
2998 then get a good value for the real definition. We handle the
2999 real symbol first, for the convenience of the backend routine.
3001 Note that there is a confusing case here. If the real definition
3002 is defined by a regular object file, we don't get the real symbol
3003 from the dynamic object, but we do get the weak symbol. If the
3004 processor backend uses a COPY reloc, then if some routine in the
3005 dynamic object changes the real symbol, we will not see that
3006 change in the corresponding weak symbol. This is the way other
3007 ELF linkers work as well, and seems to be a result of the shared
3010 I will clarify this issue. Most SVR4 shared libraries define the
3011 variable _timezone and define timezone as a weak synonym. The
3012 tzset call changes _timezone. If you write
3013 extern int timezone;
3015 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3016 you might expect that, since timezone is a synonym for _timezone,
3017 the same number will print both times. However, if the processor
3018 backend uses a COPY reloc, then actually timezone will be copied
3019 into your process image, and, since you define _timezone
3020 yourself, _timezone will not. Thus timezone and _timezone will
3021 wind up at different memory locations. The tzset call will set
3022 _timezone, leaving timezone unchanged. */
3024 if (h
->is_weakalias
)
3026 struct elf_link_hash_entry
*def
= weakdef (h
);
3028 /* If we get to this point, there is an implicit reference to
3029 the alias by a regular object file via the weak symbol H. */
3030 def
->ref_regular
= 1;
3032 /* Ensure that the backend adjust_dynamic_symbol function sees
3033 the strong alias before H by recursively calling ourselves. */
3034 if (!_bfd_elf_adjust_dynamic_symbol (def
, eif
))
3038 /* If a symbol has no type and no size and does not require a PLT
3039 entry, then we are probably about to do the wrong thing here: we
3040 are probably going to create a COPY reloc for an empty object.
3041 This case can arise when a shared object is built with assembly
3042 code, and the assembly code fails to set the symbol type. */
3044 && h
->type
== STT_NOTYPE
3047 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3048 h
->root
.root
.string
);
3050 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3059 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3063 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
3064 struct elf_link_hash_entry
*h
,
3067 unsigned int power_of_two
;
3069 asection
*sec
= h
->root
.u
.def
.section
;
3071 /* The section alignment of the definition is the maximum alignment
3072 requirement of symbols defined in the section. Since we don't
3073 know the symbol alignment requirement, we start with the
3074 maximum alignment and check low bits of the symbol address
3075 for the minimum alignment. */
3076 power_of_two
= bfd_get_section_alignment (sec
->owner
, sec
);
3077 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
3078 while ((h
->root
.u
.def
.value
& mask
) != 0)
3084 if (power_of_two
> bfd_get_section_alignment (dynbss
->owner
,
3087 /* Adjust the section alignment if needed. */
3088 if (! bfd_set_section_alignment (dynbss
->owner
, dynbss
,
3093 /* We make sure that the symbol will be aligned properly. */
3094 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
3096 /* Define the symbol as being at this point in DYNBSS. */
3097 h
->root
.u
.def
.section
= dynbss
;
3098 h
->root
.u
.def
.value
= dynbss
->size
;
3100 /* Increment the size of DYNBSS to make room for the symbol. */
3101 dynbss
->size
+= h
->size
;
3103 /* No error if extern_protected_data is true. */
3104 if (h
->protected_def
3105 && (!info
->extern_protected_data
3106 || (info
->extern_protected_data
< 0
3107 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
3108 info
->callbacks
->einfo
3109 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3110 h
->root
.root
.string
);
3115 /* Adjust all external symbols pointing into SEC_MERGE sections
3116 to reflect the object merging within the sections. */
3119 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
3123 if ((h
->root
.type
== bfd_link_hash_defined
3124 || h
->root
.type
== bfd_link_hash_defweak
)
3125 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
3126 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
3128 bfd
*output_bfd
= (bfd
*) data
;
3130 h
->root
.u
.def
.value
=
3131 _bfd_merged_section_offset (output_bfd
,
3132 &h
->root
.u
.def
.section
,
3133 elf_section_data (sec
)->sec_info
,
3134 h
->root
.u
.def
.value
);
3140 /* Returns false if the symbol referred to by H should be considered
3141 to resolve local to the current module, and true if it should be
3142 considered to bind dynamically. */
3145 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
3146 struct bfd_link_info
*info
,
3147 bfd_boolean not_local_protected
)
3149 bfd_boolean binding_stays_local_p
;
3150 const struct elf_backend_data
*bed
;
3151 struct elf_link_hash_table
*hash_table
;
3156 while (h
->root
.type
== bfd_link_hash_indirect
3157 || h
->root
.type
== bfd_link_hash_warning
)
3158 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3160 /* If it was forced local, then clearly it's not dynamic. */
3161 if (h
->dynindx
== -1)
3163 if (h
->forced_local
)
3166 /* Identify the cases where name binding rules say that a
3167 visible symbol resolves locally. */
3168 binding_stays_local_p
= (bfd_link_executable (info
)
3169 || SYMBOLIC_BIND (info
, h
));
3171 switch (ELF_ST_VISIBILITY (h
->other
))
3178 hash_table
= elf_hash_table (info
);
3179 if (!is_elf_hash_table (hash_table
))
3182 bed
= get_elf_backend_data (hash_table
->dynobj
);
3184 /* Proper resolution for function pointer equality may require
3185 that these symbols perhaps be resolved dynamically, even though
3186 we should be resolving them to the current module. */
3187 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
3188 binding_stays_local_p
= TRUE
;
3195 /* If it isn't defined locally, then clearly it's dynamic. */
3196 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
3199 /* Otherwise, the symbol is dynamic if binding rules don't tell
3200 us that it remains local. */
3201 return !binding_stays_local_p
;
3204 /* Return true if the symbol referred to by H should be considered
3205 to resolve local to the current module, and false otherwise. Differs
3206 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3207 undefined symbols. The two functions are virtually identical except
3208 for the place where dynindx == -1 is tested. If that test is true,
3209 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3210 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3212 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3213 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3214 treatment of undefined weak symbols. For those that do not make
3215 undefined weak symbols dynamic, both functions may return false. */
3218 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3219 struct bfd_link_info
*info
,
3220 bfd_boolean local_protected
)
3222 const struct elf_backend_data
*bed
;
3223 struct elf_link_hash_table
*hash_table
;
3225 /* If it's a local sym, of course we resolve locally. */
3229 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3230 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3231 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3234 /* Forced local symbols resolve locally. */
3235 if (h
->forced_local
)
3238 /* Common symbols that become definitions don't get the DEF_REGULAR
3239 flag set, so test it first, and don't bail out. */
3240 if (ELF_COMMON_DEF_P (h
))
3242 /* If we don't have a definition in a regular file, then we can't
3243 resolve locally. The sym is either undefined or dynamic. */
3244 else if (!h
->def_regular
)
3247 /* Non-dynamic symbols resolve locally. */
3248 if (h
->dynindx
== -1)
3251 /* At this point, we know the symbol is defined and dynamic. In an
3252 executable it must resolve locally, likewise when building symbolic
3253 shared libraries. */
3254 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3257 /* Now deal with defined dynamic symbols in shared libraries. Ones
3258 with default visibility might not resolve locally. */
3259 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3262 hash_table
= elf_hash_table (info
);
3263 if (!is_elf_hash_table (hash_table
))
3266 bed
= get_elf_backend_data (hash_table
->dynobj
);
3268 /* If extern_protected_data is false, STV_PROTECTED non-function
3269 symbols are local. */
3270 if ((!info
->extern_protected_data
3271 || (info
->extern_protected_data
< 0
3272 && !bed
->extern_protected_data
))
3273 && !bed
->is_function_type (h
->type
))
3276 /* Function pointer equality tests may require that STV_PROTECTED
3277 symbols be treated as dynamic symbols. If the address of a
3278 function not defined in an executable is set to that function's
3279 plt entry in the executable, then the address of the function in
3280 a shared library must also be the plt entry in the executable. */
3281 return local_protected
;
3284 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3285 aligned. Returns the first TLS output section. */
3287 struct bfd_section
*
3288 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3290 struct bfd_section
*sec
, *tls
;
3291 unsigned int align
= 0;
3293 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3294 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3298 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3299 if (sec
->alignment_power
> align
)
3300 align
= sec
->alignment_power
;
3302 elf_hash_table (info
)->tls_sec
= tls
;
3304 /* Ensure the alignment of the first section is the largest alignment,
3305 so that the tls segment starts aligned. */
3307 tls
->alignment_power
= align
;
3312 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3314 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3315 Elf_Internal_Sym
*sym
)
3317 const struct elf_backend_data
*bed
;
3319 /* Local symbols do not count, but target specific ones might. */
3320 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3321 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3324 bed
= get_elf_backend_data (abfd
);
3325 /* Function symbols do not count. */
3326 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3329 /* If the section is undefined, then so is the symbol. */
3330 if (sym
->st_shndx
== SHN_UNDEF
)
3333 /* If the symbol is defined in the common section, then
3334 it is a common definition and so does not count. */
3335 if (bed
->common_definition (sym
))
3338 /* If the symbol is in a target specific section then we
3339 must rely upon the backend to tell us what it is. */
3340 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3341 /* FIXME - this function is not coded yet:
3343 return _bfd_is_global_symbol_definition (abfd, sym);
3345 Instead for now assume that the definition is not global,
3346 Even if this is wrong, at least the linker will behave
3347 in the same way that it used to do. */
3353 /* Search the symbol table of the archive element of the archive ABFD
3354 whose archive map contains a mention of SYMDEF, and determine if
3355 the symbol is defined in this element. */
3357 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3359 Elf_Internal_Shdr
* hdr
;
3363 Elf_Internal_Sym
*isymbuf
;
3364 Elf_Internal_Sym
*isym
;
3365 Elf_Internal_Sym
*isymend
;
3368 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
3372 if (! bfd_check_format (abfd
, bfd_object
))
3375 /* Select the appropriate symbol table. If we don't know if the
3376 object file is an IR object, give linker LTO plugin a chance to
3377 get the correct symbol table. */
3378 if (abfd
->plugin_format
== bfd_plugin_yes
3379 #if BFD_SUPPORTS_PLUGINS
3380 || (abfd
->plugin_format
== bfd_plugin_unknown
3381 && bfd_link_plugin_object_p (abfd
))
3385 /* Use the IR symbol table if the object has been claimed by
3387 abfd
= abfd
->plugin_dummy_bfd
;
3388 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3390 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3391 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3393 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3395 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3397 /* The sh_info field of the symtab header tells us where the
3398 external symbols start. We don't care about the local symbols. */
3399 if (elf_bad_symtab (abfd
))
3401 extsymcount
= symcount
;
3406 extsymcount
= symcount
- hdr
->sh_info
;
3407 extsymoff
= hdr
->sh_info
;
3410 if (extsymcount
== 0)
3413 /* Read in the symbol table. */
3414 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3416 if (isymbuf
== NULL
)
3419 /* Scan the symbol table looking for SYMDEF. */
3421 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3425 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3430 if (strcmp (name
, symdef
->name
) == 0)
3432 result
= is_global_data_symbol_definition (abfd
, isym
);
3442 /* Add an entry to the .dynamic table. */
3445 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3449 struct elf_link_hash_table
*hash_table
;
3450 const struct elf_backend_data
*bed
;
3452 bfd_size_type newsize
;
3453 bfd_byte
*newcontents
;
3454 Elf_Internal_Dyn dyn
;
3456 hash_table
= elf_hash_table (info
);
3457 if (! is_elf_hash_table (hash_table
))
3460 if (tag
== DT_RELA
|| tag
== DT_REL
)
3461 hash_table
->dynamic_relocs
= TRUE
;
3463 bed
= get_elf_backend_data (hash_table
->dynobj
);
3464 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3465 BFD_ASSERT (s
!= NULL
);
3467 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3468 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3469 if (newcontents
== NULL
)
3473 dyn
.d_un
.d_val
= val
;
3474 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3477 s
->contents
= newcontents
;
3482 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3483 otherwise just check whether one already exists. Returns -1 on error,
3484 1 if a DT_NEEDED tag already exists, and 0 on success. */
3487 elf_add_dt_needed_tag (bfd
*abfd
,
3488 struct bfd_link_info
*info
,
3492 struct elf_link_hash_table
*hash_table
;
3495 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3498 hash_table
= elf_hash_table (info
);
3499 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3500 if (strindex
== (size_t) -1)
3503 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3506 const struct elf_backend_data
*bed
;
3509 bed
= get_elf_backend_data (hash_table
->dynobj
);
3510 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3512 for (extdyn
= sdyn
->contents
;
3513 extdyn
< sdyn
->contents
+ sdyn
->size
;
3514 extdyn
+= bed
->s
->sizeof_dyn
)
3516 Elf_Internal_Dyn dyn
;
3518 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3519 if (dyn
.d_tag
== DT_NEEDED
3520 && dyn
.d_un
.d_val
== strindex
)
3522 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3530 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3533 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3537 /* We were just checking for existence of the tag. */
3538 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3543 /* Return true if SONAME is on the needed list between NEEDED and STOP
3544 (or the end of list if STOP is NULL), and needed by a library that
3548 on_needed_list (const char *soname
,
3549 struct bfd_link_needed_list
*needed
,
3550 struct bfd_link_needed_list
*stop
)
3552 struct bfd_link_needed_list
*look
;
3553 for (look
= needed
; look
!= stop
; look
= look
->next
)
3554 if (strcmp (soname
, look
->name
) == 0
3555 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3556 /* If needed by a library that itself is not directly
3557 needed, recursively check whether that library is
3558 indirectly needed. Since we add DT_NEEDED entries to
3559 the end of the list, library dependencies appear after
3560 the library. Therefore search prior to the current
3561 LOOK, preventing possible infinite recursion. */
3562 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3568 /* Sort symbol by value, section, and size. */
3570 elf_sort_symbol (const void *arg1
, const void *arg2
)
3572 const struct elf_link_hash_entry
*h1
;
3573 const struct elf_link_hash_entry
*h2
;
3574 bfd_signed_vma vdiff
;
3576 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3577 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3578 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3580 return vdiff
> 0 ? 1 : -1;
3583 int sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3585 return sdiff
> 0 ? 1 : -1;
3587 vdiff
= h1
->size
- h2
->size
;
3588 return vdiff
== 0 ? 0 : vdiff
> 0 ? 1 : -1;
3591 /* This function is used to adjust offsets into .dynstr for
3592 dynamic symbols. This is called via elf_link_hash_traverse. */
3595 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3597 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3599 if (h
->dynindx
!= -1)
3600 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3604 /* Assign string offsets in .dynstr, update all structures referencing
3608 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3610 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3611 struct elf_link_local_dynamic_entry
*entry
;
3612 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3613 bfd
*dynobj
= hash_table
->dynobj
;
3616 const struct elf_backend_data
*bed
;
3619 _bfd_elf_strtab_finalize (dynstr
);
3620 size
= _bfd_elf_strtab_size (dynstr
);
3622 bed
= get_elf_backend_data (dynobj
);
3623 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3624 BFD_ASSERT (sdyn
!= NULL
);
3626 /* Update all .dynamic entries referencing .dynstr strings. */
3627 for (extdyn
= sdyn
->contents
;
3628 extdyn
< sdyn
->contents
+ sdyn
->size
;
3629 extdyn
+= bed
->s
->sizeof_dyn
)
3631 Elf_Internal_Dyn dyn
;
3633 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3637 dyn
.d_un
.d_val
= size
;
3647 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3652 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3655 /* Now update local dynamic symbols. */
3656 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3657 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3658 entry
->isym
.st_name
);
3660 /* And the rest of dynamic symbols. */
3661 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3663 /* Adjust version definitions. */
3664 if (elf_tdata (output_bfd
)->cverdefs
)
3669 Elf_Internal_Verdef def
;
3670 Elf_Internal_Verdaux defaux
;
3672 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3676 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3678 p
+= sizeof (Elf_External_Verdef
);
3679 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3681 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3683 _bfd_elf_swap_verdaux_in (output_bfd
,
3684 (Elf_External_Verdaux
*) p
, &defaux
);
3685 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3687 _bfd_elf_swap_verdaux_out (output_bfd
,
3688 &defaux
, (Elf_External_Verdaux
*) p
);
3689 p
+= sizeof (Elf_External_Verdaux
);
3692 while (def
.vd_next
);
3695 /* Adjust version references. */
3696 if (elf_tdata (output_bfd
)->verref
)
3701 Elf_Internal_Verneed need
;
3702 Elf_Internal_Vernaux needaux
;
3704 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
3708 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3710 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3711 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3712 (Elf_External_Verneed
*) p
);
3713 p
+= sizeof (Elf_External_Verneed
);
3714 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3716 _bfd_elf_swap_vernaux_in (output_bfd
,
3717 (Elf_External_Vernaux
*) p
, &needaux
);
3718 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3720 _bfd_elf_swap_vernaux_out (output_bfd
,
3722 (Elf_External_Vernaux
*) p
);
3723 p
+= sizeof (Elf_External_Vernaux
);
3726 while (need
.vn_next
);
3732 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3733 The default is to only match when the INPUT and OUTPUT are exactly
3737 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3738 const bfd_target
*output
)
3740 return input
== output
;
3743 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3744 This version is used when different targets for the same architecture
3745 are virtually identical. */
3748 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3749 const bfd_target
*output
)
3751 const struct elf_backend_data
*obed
, *ibed
;
3753 if (input
== output
)
3756 ibed
= xvec_get_elf_backend_data (input
);
3757 obed
= xvec_get_elf_backend_data (output
);
3759 if (ibed
->arch
!= obed
->arch
)
3762 /* If both backends are using this function, deem them compatible. */
3763 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3766 /* Make a special call to the linker "notice" function to tell it that
3767 we are about to handle an as-needed lib, or have finished
3768 processing the lib. */
3771 _bfd_elf_notice_as_needed (bfd
*ibfd
,
3772 struct bfd_link_info
*info
,
3773 enum notice_asneeded_action act
)
3775 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
3778 /* Check relocations an ELF object file. */
3781 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
3783 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3784 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
3786 /* If this object is the same format as the output object, and it is
3787 not a shared library, then let the backend look through the
3790 This is required to build global offset table entries and to
3791 arrange for dynamic relocs. It is not required for the
3792 particular common case of linking non PIC code, even when linking
3793 against shared libraries, but unfortunately there is no way of
3794 knowing whether an object file has been compiled PIC or not.
3795 Looking through the relocs is not particularly time consuming.
3796 The problem is that we must either (1) keep the relocs in memory,
3797 which causes the linker to require additional runtime memory or
3798 (2) read the relocs twice from the input file, which wastes time.
3799 This would be a good case for using mmap.
3801 I have no idea how to handle linking PIC code into a file of a
3802 different format. It probably can't be done. */
3803 if ((abfd
->flags
& DYNAMIC
) == 0
3804 && is_elf_hash_table (htab
)
3805 && bed
->check_relocs
!= NULL
3806 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
3807 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
3811 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3813 Elf_Internal_Rela
*internal_relocs
;
3816 /* Don't check relocations in excluded sections. */
3817 if ((o
->flags
& SEC_RELOC
) == 0
3818 || (o
->flags
& SEC_EXCLUDE
) != 0
3819 || o
->reloc_count
== 0
3820 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
3821 && (o
->flags
& SEC_DEBUGGING
) != 0)
3822 || bfd_is_abs_section (o
->output_section
))
3825 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
3827 if (internal_relocs
== NULL
)
3830 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
3832 if (elf_section_data (o
)->relocs
!= internal_relocs
)
3833 free (internal_relocs
);
3843 /* Add symbols from an ELF object file to the linker hash table. */
3846 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3848 Elf_Internal_Ehdr
*ehdr
;
3849 Elf_Internal_Shdr
*hdr
;
3853 struct elf_link_hash_entry
**sym_hash
;
3854 bfd_boolean dynamic
;
3855 Elf_External_Versym
*extversym
= NULL
;
3856 Elf_External_Versym
*ever
;
3857 struct elf_link_hash_entry
*weaks
;
3858 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3859 size_t nondeflt_vers_cnt
= 0;
3860 Elf_Internal_Sym
*isymbuf
= NULL
;
3861 Elf_Internal_Sym
*isym
;
3862 Elf_Internal_Sym
*isymend
;
3863 const struct elf_backend_data
*bed
;
3864 bfd_boolean add_needed
;
3865 struct elf_link_hash_table
*htab
;
3867 void *alloc_mark
= NULL
;
3868 struct bfd_hash_entry
**old_table
= NULL
;
3869 unsigned int old_size
= 0;
3870 unsigned int old_count
= 0;
3871 void *old_tab
= NULL
;
3873 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3874 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3875 void *old_strtab
= NULL
;
3878 bfd_boolean just_syms
;
3880 htab
= elf_hash_table (info
);
3881 bed
= get_elf_backend_data (abfd
);
3883 if ((abfd
->flags
& DYNAMIC
) == 0)
3889 /* You can't use -r against a dynamic object. Also, there's no
3890 hope of using a dynamic object which does not exactly match
3891 the format of the output file. */
3892 if (bfd_link_relocatable (info
)
3893 || !is_elf_hash_table (htab
)
3894 || info
->output_bfd
->xvec
!= abfd
->xvec
)
3896 if (bfd_link_relocatable (info
))
3897 bfd_set_error (bfd_error_invalid_operation
);
3899 bfd_set_error (bfd_error_wrong_format
);
3904 ehdr
= elf_elfheader (abfd
);
3905 if (info
->warn_alternate_em
3906 && bed
->elf_machine_code
!= ehdr
->e_machine
3907 && ((bed
->elf_machine_alt1
!= 0
3908 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
3909 || (bed
->elf_machine_alt2
!= 0
3910 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
3912 /* xgettext:c-format */
3913 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
3914 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
3916 /* As a GNU extension, any input sections which are named
3917 .gnu.warning.SYMBOL are treated as warning symbols for the given
3918 symbol. This differs from .gnu.warning sections, which generate
3919 warnings when they are included in an output file. */
3920 /* PR 12761: Also generate this warning when building shared libraries. */
3921 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3925 name
= bfd_get_section_name (abfd
, s
);
3926 if (CONST_STRNEQ (name
, ".gnu.warning."))
3931 name
+= sizeof ".gnu.warning." - 1;
3933 /* If this is a shared object, then look up the symbol
3934 in the hash table. If it is there, and it is already
3935 been defined, then we will not be using the entry
3936 from this shared object, so we don't need to warn.
3937 FIXME: If we see the definition in a regular object
3938 later on, we will warn, but we shouldn't. The only
3939 fix is to keep track of what warnings we are supposed
3940 to emit, and then handle them all at the end of the
3944 struct elf_link_hash_entry
*h
;
3946 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3948 /* FIXME: What about bfd_link_hash_common? */
3950 && (h
->root
.type
== bfd_link_hash_defined
3951 || h
->root
.type
== bfd_link_hash_defweak
))
3956 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
3960 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3965 if (! (_bfd_generic_link_add_one_symbol
3966 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3967 FALSE
, bed
->collect
, NULL
)))
3970 if (bfd_link_executable (info
))
3972 /* Clobber the section size so that the warning does
3973 not get copied into the output file. */
3976 /* Also set SEC_EXCLUDE, so that symbols defined in
3977 the warning section don't get copied to the output. */
3978 s
->flags
|= SEC_EXCLUDE
;
3983 just_syms
= ((s
= abfd
->sections
) != NULL
3984 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
3989 /* If we are creating a shared library, create all the dynamic
3990 sections immediately. We need to attach them to something,
3991 so we attach them to this BFD, provided it is the right
3992 format and is not from ld --just-symbols. Always create the
3993 dynamic sections for -E/--dynamic-list. FIXME: If there
3994 are no input BFD's of the same format as the output, we can't
3995 make a shared library. */
3997 && (bfd_link_pic (info
)
3998 || (!bfd_link_relocatable (info
)
4000 && (info
->export_dynamic
|| info
->dynamic
)))
4001 && is_elf_hash_table (htab
)
4002 && info
->output_bfd
->xvec
== abfd
->xvec
4003 && !htab
->dynamic_sections_created
)
4005 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
4009 else if (!is_elf_hash_table (htab
))
4013 const char *soname
= NULL
;
4015 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
4016 const Elf_Internal_Phdr
*phdr
;
4019 /* ld --just-symbols and dynamic objects don't mix very well.
4020 ld shouldn't allow it. */
4024 /* If this dynamic lib was specified on the command line with
4025 --as-needed in effect, then we don't want to add a DT_NEEDED
4026 tag unless the lib is actually used. Similary for libs brought
4027 in by another lib's DT_NEEDED. When --no-add-needed is used
4028 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4029 any dynamic library in DT_NEEDED tags in the dynamic lib at
4031 add_needed
= (elf_dyn_lib_class (abfd
)
4032 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
4033 | DYN_NO_NEEDED
)) == 0;
4035 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4040 unsigned int elfsec
;
4041 unsigned long shlink
;
4043 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
4050 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
4051 if (elfsec
== SHN_BAD
)
4052 goto error_free_dyn
;
4053 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
4055 for (extdyn
= dynbuf
;
4056 extdyn
< dynbuf
+ s
->size
;
4057 extdyn
+= bed
->s
->sizeof_dyn
)
4059 Elf_Internal_Dyn dyn
;
4061 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
4062 if (dyn
.d_tag
== DT_SONAME
)
4064 unsigned int tagv
= dyn
.d_un
.d_val
;
4065 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4067 goto error_free_dyn
;
4069 if (dyn
.d_tag
== DT_NEEDED
)
4071 struct bfd_link_needed_list
*n
, **pn
;
4073 unsigned int tagv
= dyn
.d_un
.d_val
;
4075 amt
= sizeof (struct bfd_link_needed_list
);
4076 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4077 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4078 if (n
== NULL
|| fnm
== NULL
)
4079 goto error_free_dyn
;
4080 amt
= strlen (fnm
) + 1;
4081 anm
= (char *) bfd_alloc (abfd
, amt
);
4083 goto error_free_dyn
;
4084 memcpy (anm
, fnm
, amt
);
4088 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4092 if (dyn
.d_tag
== DT_RUNPATH
)
4094 struct bfd_link_needed_list
*n
, **pn
;
4096 unsigned int tagv
= dyn
.d_un
.d_val
;
4098 amt
= sizeof (struct bfd_link_needed_list
);
4099 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4100 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4101 if (n
== NULL
|| fnm
== NULL
)
4102 goto error_free_dyn
;
4103 amt
= strlen (fnm
) + 1;
4104 anm
= (char *) bfd_alloc (abfd
, amt
);
4106 goto error_free_dyn
;
4107 memcpy (anm
, fnm
, amt
);
4111 for (pn
= & runpath
;
4117 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4118 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
4120 struct bfd_link_needed_list
*n
, **pn
;
4122 unsigned int tagv
= dyn
.d_un
.d_val
;
4124 amt
= sizeof (struct bfd_link_needed_list
);
4125 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4126 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4127 if (n
== NULL
|| fnm
== NULL
)
4128 goto error_free_dyn
;
4129 amt
= strlen (fnm
) + 1;
4130 anm
= (char *) bfd_alloc (abfd
, amt
);
4132 goto error_free_dyn
;
4133 memcpy (anm
, fnm
, amt
);
4143 if (dyn
.d_tag
== DT_AUDIT
)
4145 unsigned int tagv
= dyn
.d_un
.d_val
;
4146 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4153 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4154 frees all more recently bfd_alloc'd blocks as well. */
4160 struct bfd_link_needed_list
**pn
;
4161 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4166 /* If we have a PT_GNU_RELRO program header, mark as read-only
4167 all sections contained fully therein. This makes relro
4168 shared library sections appear as they will at run-time. */
4169 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4170 while (--phdr
>= elf_tdata (abfd
)->phdr
)
4171 if (phdr
->p_type
== PT_GNU_RELRO
)
4173 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4174 if ((s
->flags
& SEC_ALLOC
) != 0
4175 && s
->vma
>= phdr
->p_vaddr
4176 && s
->vma
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4177 s
->flags
|= SEC_READONLY
;
4181 /* We do not want to include any of the sections in a dynamic
4182 object in the output file. We hack by simply clobbering the
4183 list of sections in the BFD. This could be handled more
4184 cleanly by, say, a new section flag; the existing
4185 SEC_NEVER_LOAD flag is not the one we want, because that one
4186 still implies that the section takes up space in the output
4188 bfd_section_list_clear (abfd
);
4190 /* Find the name to use in a DT_NEEDED entry that refers to this
4191 object. If the object has a DT_SONAME entry, we use it.
4192 Otherwise, if the generic linker stuck something in
4193 elf_dt_name, we use that. Otherwise, we just use the file
4195 if (soname
== NULL
|| *soname
== '\0')
4197 soname
= elf_dt_name (abfd
);
4198 if (soname
== NULL
|| *soname
== '\0')
4199 soname
= bfd_get_filename (abfd
);
4202 /* Save the SONAME because sometimes the linker emulation code
4203 will need to know it. */
4204 elf_dt_name (abfd
) = soname
;
4206 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4210 /* If we have already included this dynamic object in the
4211 link, just ignore it. There is no reason to include a
4212 particular dynamic object more than once. */
4216 /* Save the DT_AUDIT entry for the linker emulation code. */
4217 elf_dt_audit (abfd
) = audit
;
4220 /* If this is a dynamic object, we always link against the .dynsym
4221 symbol table, not the .symtab symbol table. The dynamic linker
4222 will only see the .dynsym symbol table, so there is no reason to
4223 look at .symtab for a dynamic object. */
4225 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4226 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4228 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4230 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4232 /* The sh_info field of the symtab header tells us where the
4233 external symbols start. We don't care about the local symbols at
4235 if (elf_bad_symtab (abfd
))
4237 extsymcount
= symcount
;
4242 extsymcount
= symcount
- hdr
->sh_info
;
4243 extsymoff
= hdr
->sh_info
;
4246 sym_hash
= elf_sym_hashes (abfd
);
4247 if (extsymcount
!= 0)
4249 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4251 if (isymbuf
== NULL
)
4254 if (sym_hash
== NULL
)
4256 /* We store a pointer to the hash table entry for each
4259 amt
*= sizeof (struct elf_link_hash_entry
*);
4260 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4261 if (sym_hash
== NULL
)
4262 goto error_free_sym
;
4263 elf_sym_hashes (abfd
) = sym_hash
;
4269 /* Read in any version definitions. */
4270 if (!_bfd_elf_slurp_version_tables (abfd
,
4271 info
->default_imported_symver
))
4272 goto error_free_sym
;
4274 /* Read in the symbol versions, but don't bother to convert them
4275 to internal format. */
4276 if (elf_dynversym (abfd
) != 0)
4278 Elf_Internal_Shdr
*versymhdr
;
4280 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4281 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
4282 if (extversym
== NULL
)
4283 goto error_free_sym
;
4284 amt
= versymhdr
->sh_size
;
4285 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
4286 || bfd_bread (extversym
, amt
, abfd
) != amt
)
4287 goto error_free_vers
;
4291 /* If we are loading an as-needed shared lib, save the symbol table
4292 state before we start adding symbols. If the lib turns out
4293 to be unneeded, restore the state. */
4294 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4299 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4301 struct bfd_hash_entry
*p
;
4302 struct elf_link_hash_entry
*h
;
4304 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4306 h
= (struct elf_link_hash_entry
*) p
;
4307 entsize
+= htab
->root
.table
.entsize
;
4308 if (h
->root
.type
== bfd_link_hash_warning
)
4309 entsize
+= htab
->root
.table
.entsize
;
4313 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4314 old_tab
= bfd_malloc (tabsize
+ entsize
);
4315 if (old_tab
== NULL
)
4316 goto error_free_vers
;
4318 /* Remember the current objalloc pointer, so that all mem for
4319 symbols added can later be reclaimed. */
4320 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4321 if (alloc_mark
== NULL
)
4322 goto error_free_vers
;
4324 /* Make a special call to the linker "notice" function to
4325 tell it that we are about to handle an as-needed lib. */
4326 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4327 goto error_free_vers
;
4329 /* Clone the symbol table. Remember some pointers into the
4330 symbol table, and dynamic symbol count. */
4331 old_ent
= (char *) old_tab
+ tabsize
;
4332 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4333 old_undefs
= htab
->root
.undefs
;
4334 old_undefs_tail
= htab
->root
.undefs_tail
;
4335 old_table
= htab
->root
.table
.table
;
4336 old_size
= htab
->root
.table
.size
;
4337 old_count
= htab
->root
.table
.count
;
4338 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4339 if (old_strtab
== NULL
)
4340 goto error_free_vers
;
4342 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4344 struct bfd_hash_entry
*p
;
4345 struct elf_link_hash_entry
*h
;
4347 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4349 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
4350 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4351 h
= (struct elf_link_hash_entry
*) p
;
4352 if (h
->root
.type
== bfd_link_hash_warning
)
4354 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
4355 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4362 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
4363 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
4365 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4369 asection
*sec
, *new_sec
;
4372 struct elf_link_hash_entry
*h
;
4373 struct elf_link_hash_entry
*hi
;
4374 bfd_boolean definition
;
4375 bfd_boolean size_change_ok
;
4376 bfd_boolean type_change_ok
;
4377 bfd_boolean new_weak
;
4378 bfd_boolean old_weak
;
4379 bfd_boolean override
;
4381 bfd_boolean discarded
;
4382 unsigned int old_alignment
;
4384 bfd_boolean matched
;
4388 flags
= BSF_NO_FLAGS
;
4390 value
= isym
->st_value
;
4391 common
= bed
->common_definition (isym
);
4392 if (common
&& info
->inhibit_common_definition
)
4394 /* Treat common symbol as undefined for --no-define-common. */
4395 isym
->st_shndx
= SHN_UNDEF
;
4400 bind
= ELF_ST_BIND (isym
->st_info
);
4404 /* This should be impossible, since ELF requires that all
4405 global symbols follow all local symbols, and that sh_info
4406 point to the first global symbol. Unfortunately, Irix 5
4411 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4419 case STB_GNU_UNIQUE
:
4420 flags
= BSF_GNU_UNIQUE
;
4424 /* Leave it up to the processor backend. */
4428 if (isym
->st_shndx
== SHN_UNDEF
)
4429 sec
= bfd_und_section_ptr
;
4430 else if (isym
->st_shndx
== SHN_ABS
)
4431 sec
= bfd_abs_section_ptr
;
4432 else if (isym
->st_shndx
== SHN_COMMON
)
4434 sec
= bfd_com_section_ptr
;
4435 /* What ELF calls the size we call the value. What ELF
4436 calls the value we call the alignment. */
4437 value
= isym
->st_size
;
4441 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4443 sec
= bfd_abs_section_ptr
;
4444 else if (discarded_section (sec
))
4446 /* Symbols from discarded section are undefined. We keep
4448 sec
= bfd_und_section_ptr
;
4450 isym
->st_shndx
= SHN_UNDEF
;
4452 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4456 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4459 goto error_free_vers
;
4461 if (isym
->st_shndx
== SHN_COMMON
4462 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4464 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4468 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4470 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4472 goto error_free_vers
;
4476 else if (isym
->st_shndx
== SHN_COMMON
4477 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4478 && !bfd_link_relocatable (info
))
4480 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4484 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4485 | SEC_LINKER_CREATED
);
4486 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4488 goto error_free_vers
;
4492 else if (bed
->elf_add_symbol_hook
)
4494 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4496 goto error_free_vers
;
4498 /* The hook function sets the name to NULL if this symbol
4499 should be skipped for some reason. */
4504 /* Sanity check that all possibilities were handled. */
4507 bfd_set_error (bfd_error_bad_value
);
4508 goto error_free_vers
;
4511 /* Silently discard TLS symbols from --just-syms. There's
4512 no way to combine a static TLS block with a new TLS block
4513 for this executable. */
4514 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4515 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4518 if (bfd_is_und_section (sec
)
4519 || bfd_is_com_section (sec
))
4524 size_change_ok
= FALSE
;
4525 type_change_ok
= bed
->type_change_ok
;
4532 if (is_elf_hash_table (htab
))
4534 Elf_Internal_Versym iver
;
4535 unsigned int vernum
= 0;
4540 if (info
->default_imported_symver
)
4541 /* Use the default symbol version created earlier. */
4542 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4547 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4549 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4551 /* If this is a hidden symbol, or if it is not version
4552 1, we append the version name to the symbol name.
4553 However, we do not modify a non-hidden absolute symbol
4554 if it is not a function, because it might be the version
4555 symbol itself. FIXME: What if it isn't? */
4556 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4558 && (!bfd_is_abs_section (sec
)
4559 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4562 size_t namelen
, verlen
, newlen
;
4565 if (isym
->st_shndx
!= SHN_UNDEF
)
4567 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4569 else if (vernum
> 1)
4571 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4578 /* xgettext:c-format */
4579 (_("%pB: %s: invalid version %u (max %d)"),
4581 elf_tdata (abfd
)->cverdefs
);
4582 bfd_set_error (bfd_error_bad_value
);
4583 goto error_free_vers
;
4588 /* We cannot simply test for the number of
4589 entries in the VERNEED section since the
4590 numbers for the needed versions do not start
4592 Elf_Internal_Verneed
*t
;
4595 for (t
= elf_tdata (abfd
)->verref
;
4599 Elf_Internal_Vernaux
*a
;
4601 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4603 if (a
->vna_other
== vernum
)
4605 verstr
= a
->vna_nodename
;
4615 /* xgettext:c-format */
4616 (_("%pB: %s: invalid needed version %d"),
4617 abfd
, name
, vernum
);
4618 bfd_set_error (bfd_error_bad_value
);
4619 goto error_free_vers
;
4623 namelen
= strlen (name
);
4624 verlen
= strlen (verstr
);
4625 newlen
= namelen
+ verlen
+ 2;
4626 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4627 && isym
->st_shndx
!= SHN_UNDEF
)
4630 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4631 if (newname
== NULL
)
4632 goto error_free_vers
;
4633 memcpy (newname
, name
, namelen
);
4634 p
= newname
+ namelen
;
4636 /* If this is a defined non-hidden version symbol,
4637 we add another @ to the name. This indicates the
4638 default version of the symbol. */
4639 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4640 && isym
->st_shndx
!= SHN_UNDEF
)
4642 memcpy (p
, verstr
, verlen
+ 1);
4647 /* If this symbol has default visibility and the user has
4648 requested we not re-export it, then mark it as hidden. */
4649 if (!bfd_is_und_section (sec
)
4652 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4653 isym
->st_other
= (STV_HIDDEN
4654 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4656 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4657 sym_hash
, &old_bfd
, &old_weak
,
4658 &old_alignment
, &skip
, &override
,
4659 &type_change_ok
, &size_change_ok
,
4661 goto error_free_vers
;
4666 /* Override a definition only if the new symbol matches the
4668 if (override
&& matched
)
4672 while (h
->root
.type
== bfd_link_hash_indirect
4673 || h
->root
.type
== bfd_link_hash_warning
)
4674 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4676 if (elf_tdata (abfd
)->verdef
!= NULL
4679 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4682 if (! (_bfd_generic_link_add_one_symbol
4683 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4684 (struct bfd_link_hash_entry
**) sym_hash
)))
4685 goto error_free_vers
;
4687 if ((abfd
->flags
& DYNAMIC
) == 0
4688 && (bfd_get_flavour (info
->output_bfd
)
4689 == bfd_target_elf_flavour
))
4691 if (ELF_ST_TYPE (isym
->st_info
) == STT_GNU_IFUNC
)
4692 elf_tdata (info
->output_bfd
)->has_gnu_symbols
4693 |= elf_gnu_symbol_ifunc
;
4694 if ((flags
& BSF_GNU_UNIQUE
))
4695 elf_tdata (info
->output_bfd
)->has_gnu_symbols
4696 |= elf_gnu_symbol_unique
;
4700 /* We need to make sure that indirect symbol dynamic flags are
4703 while (h
->root
.type
== bfd_link_hash_indirect
4704 || h
->root
.type
== bfd_link_hash_warning
)
4705 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4707 /* Setting the index to -3 tells elf_link_output_extsym that
4708 this symbol is defined in a discarded section. */
4714 new_weak
= (flags
& BSF_WEAK
) != 0;
4718 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4719 && is_elf_hash_table (htab
)
4720 && h
->u
.alias
== NULL
)
4722 /* Keep a list of all weak defined non function symbols from
4723 a dynamic object, using the alias field. Later in this
4724 function we will set the alias field to the correct
4725 value. We only put non-function symbols from dynamic
4726 objects on this list, because that happens to be the only
4727 time we need to know the normal symbol corresponding to a
4728 weak symbol, and the information is time consuming to
4729 figure out. If the alias field is not already NULL,
4730 then this symbol was already defined by some previous
4731 dynamic object, and we will be using that previous
4732 definition anyhow. */
4738 /* Set the alignment of a common symbol. */
4739 if ((common
|| bfd_is_com_section (sec
))
4740 && h
->root
.type
== bfd_link_hash_common
)
4745 align
= bfd_log2 (isym
->st_value
);
4748 /* The new symbol is a common symbol in a shared object.
4749 We need to get the alignment from the section. */
4750 align
= new_sec
->alignment_power
;
4752 if (align
> old_alignment
)
4753 h
->root
.u
.c
.p
->alignment_power
= align
;
4755 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4758 if (is_elf_hash_table (htab
))
4760 /* Set a flag in the hash table entry indicating the type of
4761 reference or definition we just found. A dynamic symbol
4762 is one which is referenced or defined by both a regular
4763 object and a shared object. */
4764 bfd_boolean dynsym
= FALSE
;
4766 /* Plugin symbols aren't normal. Don't set def_regular or
4767 ref_regular for them, or make them dynamic. */
4768 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4775 if (bind
!= STB_WEAK
)
4776 h
->ref_regular_nonweak
= 1;
4788 /* If the indirect symbol has been forced local, don't
4789 make the real symbol dynamic. */
4790 if ((h
== hi
|| !hi
->forced_local
)
4791 && (bfd_link_dll (info
)
4801 hi
->ref_dynamic
= 1;
4806 hi
->def_dynamic
= 1;
4809 /* If the indirect symbol has been forced local, don't
4810 make the real symbol dynamic. */
4811 if ((h
== hi
|| !hi
->forced_local
)
4815 && weakdef (h
)->dynindx
!= -1)))
4819 /* Check to see if we need to add an indirect symbol for
4820 the default name. */
4822 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
4823 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4824 sec
, value
, &old_bfd
, &dynsym
))
4825 goto error_free_vers
;
4827 /* Check the alignment when a common symbol is involved. This
4828 can change when a common symbol is overridden by a normal
4829 definition or a common symbol is ignored due to the old
4830 normal definition. We need to make sure the maximum
4831 alignment is maintained. */
4832 if ((old_alignment
|| common
)
4833 && h
->root
.type
!= bfd_link_hash_common
)
4835 unsigned int common_align
;
4836 unsigned int normal_align
;
4837 unsigned int symbol_align
;
4841 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
4842 || h
->root
.type
== bfd_link_hash_defweak
);
4844 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4845 if (h
->root
.u
.def
.section
->owner
!= NULL
4846 && (h
->root
.u
.def
.section
->owner
->flags
4847 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
4849 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4850 if (normal_align
> symbol_align
)
4851 normal_align
= symbol_align
;
4854 normal_align
= symbol_align
;
4858 common_align
= old_alignment
;
4859 common_bfd
= old_bfd
;
4864 common_align
= bfd_log2 (isym
->st_value
);
4866 normal_bfd
= old_bfd
;
4869 if (normal_align
< common_align
)
4871 /* PR binutils/2735 */
4872 if (normal_bfd
== NULL
)
4874 /* xgettext:c-format */
4875 (_("warning: alignment %u of common symbol `%s' in %pB is"
4876 " greater than the alignment (%u) of its section %pA"),
4877 1 << common_align
, name
, common_bfd
,
4878 1 << normal_align
, h
->root
.u
.def
.section
);
4881 /* xgettext:c-format */
4882 (_("warning: alignment %u of symbol `%s' in %pB"
4883 " is smaller than %u in %pB"),
4884 1 << normal_align
, name
, normal_bfd
,
4885 1 << common_align
, common_bfd
);
4889 /* Remember the symbol size if it isn't undefined. */
4890 if (isym
->st_size
!= 0
4891 && isym
->st_shndx
!= SHN_UNDEF
4892 && (definition
|| h
->size
== 0))
4895 && h
->size
!= isym
->st_size
4896 && ! size_change_ok
)
4898 /* xgettext:c-format */
4899 (_("warning: size of symbol `%s' changed"
4900 " from %" PRIu64
" in %pB to %" PRIu64
" in %pB"),
4901 name
, (uint64_t) h
->size
, old_bfd
,
4902 (uint64_t) isym
->st_size
, abfd
);
4904 h
->size
= isym
->st_size
;
4907 /* If this is a common symbol, then we always want H->SIZE
4908 to be the size of the common symbol. The code just above
4909 won't fix the size if a common symbol becomes larger. We
4910 don't warn about a size change here, because that is
4911 covered by --warn-common. Allow changes between different
4913 if (h
->root
.type
== bfd_link_hash_common
)
4914 h
->size
= h
->root
.u
.c
.size
;
4916 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4917 && ((definition
&& !new_weak
)
4918 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
4919 || h
->type
== STT_NOTYPE
))
4921 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
4923 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4925 if (type
== STT_GNU_IFUNC
4926 && (abfd
->flags
& DYNAMIC
) != 0)
4929 if (h
->type
!= type
)
4931 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
4932 /* xgettext:c-format */
4934 (_("warning: type of symbol `%s' changed"
4935 " from %d to %d in %pB"),
4936 name
, h
->type
, type
, abfd
);
4942 /* Merge st_other field. */
4943 elf_merge_st_other (abfd
, h
, isym
, sec
, definition
, dynamic
);
4945 /* We don't want to make debug symbol dynamic. */
4947 && (sec
->flags
& SEC_DEBUGGING
)
4948 && !bfd_link_relocatable (info
))
4951 /* Nor should we make plugin symbols dynamic. */
4952 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4957 h
->target_internal
= isym
->st_target_internal
;
4958 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
4961 if (definition
&& !dynamic
)
4963 char *p
= strchr (name
, ELF_VER_CHR
);
4964 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4966 /* Queue non-default versions so that .symver x, x@FOO
4967 aliases can be checked. */
4970 amt
= ((isymend
- isym
+ 1)
4971 * sizeof (struct elf_link_hash_entry
*));
4973 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
4975 goto error_free_vers
;
4977 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4981 if (dynsym
&& h
->dynindx
== -1)
4983 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4984 goto error_free_vers
;
4986 && weakdef (h
)->dynindx
== -1)
4988 if (!bfd_elf_link_record_dynamic_symbol (info
, weakdef (h
)))
4989 goto error_free_vers
;
4992 else if (h
->dynindx
!= -1)
4993 /* If the symbol already has a dynamic index, but
4994 visibility says it should not be visible, turn it into
4996 switch (ELF_ST_VISIBILITY (h
->other
))
5000 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
5005 /* Don't add DT_NEEDED for references from the dummy bfd nor
5006 for unmatched symbol. */
5011 && h
->ref_regular_nonweak
5013 || (old_bfd
->flags
& BFD_PLUGIN
) == 0))
5014 || (h
->ref_dynamic_nonweak
5015 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5016 && !on_needed_list (elf_dt_name (abfd
),
5017 htab
->needed
, NULL
))))
5020 const char *soname
= elf_dt_name (abfd
);
5022 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
5023 h
->root
.root
.string
);
5025 /* A symbol from a library loaded via DT_NEEDED of some
5026 other library is referenced by a regular object.
5027 Add a DT_NEEDED entry for it. Issue an error if
5028 --no-add-needed is used and the reference was not
5031 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
5034 /* xgettext:c-format */
5035 (_("%pB: undefined reference to symbol '%s'"),
5037 bfd_set_error (bfd_error_missing_dso
);
5038 goto error_free_vers
;
5041 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
5042 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
5045 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
5047 goto error_free_vers
;
5049 BFD_ASSERT (ret
== 0);
5054 if (info
->lto_plugin_active
5055 && !bfd_link_relocatable (info
)
5056 && (abfd
->flags
& BFD_PLUGIN
) == 0
5062 if (bed
->s
->arch_size
== 32)
5067 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5068 referenced in regular objects so that linker plugin will get
5069 the correct symbol resolution. */
5071 sym_hash
= elf_sym_hashes (abfd
);
5072 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5074 Elf_Internal_Rela
*internal_relocs
;
5075 Elf_Internal_Rela
*rel
, *relend
;
5077 /* Don't check relocations in excluded sections. */
5078 if ((s
->flags
& SEC_RELOC
) == 0
5079 || s
->reloc_count
== 0
5080 || (s
->flags
& SEC_EXCLUDE
) != 0
5081 || ((info
->strip
== strip_all
5082 || info
->strip
== strip_debugger
)
5083 && (s
->flags
& SEC_DEBUGGING
) != 0))
5086 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, s
, NULL
,
5089 if (internal_relocs
== NULL
)
5090 goto error_free_vers
;
5092 rel
= internal_relocs
;
5093 relend
= rel
+ s
->reloc_count
;
5094 for ( ; rel
< relend
; rel
++)
5096 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
5097 struct elf_link_hash_entry
*h
;
5099 /* Skip local symbols. */
5100 if (r_symndx
< extsymoff
)
5103 h
= sym_hash
[r_symndx
- extsymoff
];
5105 h
->root
.non_ir_ref_regular
= 1;
5108 if (elf_section_data (s
)->relocs
!= internal_relocs
)
5109 free (internal_relocs
);
5113 if (extversym
!= NULL
)
5119 if (isymbuf
!= NULL
)
5125 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
5129 /* Restore the symbol table. */
5130 old_ent
= (char *) old_tab
+ tabsize
;
5131 memset (elf_sym_hashes (abfd
), 0,
5132 extsymcount
* sizeof (struct elf_link_hash_entry
*));
5133 htab
->root
.table
.table
= old_table
;
5134 htab
->root
.table
.size
= old_size
;
5135 htab
->root
.table
.count
= old_count
;
5136 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
5137 htab
->root
.undefs
= old_undefs
;
5138 htab
->root
.undefs_tail
= old_undefs_tail
;
5139 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
5142 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
5144 struct bfd_hash_entry
*p
;
5145 struct elf_link_hash_entry
*h
;
5147 unsigned int alignment_power
;
5148 unsigned int non_ir_ref_dynamic
;
5150 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
5152 h
= (struct elf_link_hash_entry
*) p
;
5153 if (h
->root
.type
== bfd_link_hash_warning
)
5154 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5156 /* Preserve the maximum alignment and size for common
5157 symbols even if this dynamic lib isn't on DT_NEEDED
5158 since it can still be loaded at run time by another
5160 if (h
->root
.type
== bfd_link_hash_common
)
5162 size
= h
->root
.u
.c
.size
;
5163 alignment_power
= h
->root
.u
.c
.p
->alignment_power
;
5168 alignment_power
= 0;
5170 /* Preserve non_ir_ref_dynamic so that this symbol
5171 will be exported when the dynamic lib becomes needed
5172 in the second pass. */
5173 non_ir_ref_dynamic
= h
->root
.non_ir_ref_dynamic
;
5174 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
5175 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5176 h
= (struct elf_link_hash_entry
*) p
;
5177 if (h
->root
.type
== bfd_link_hash_warning
)
5179 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
5180 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5181 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5183 if (h
->root
.type
== bfd_link_hash_common
)
5185 if (size
> h
->root
.u
.c
.size
)
5186 h
->root
.u
.c
.size
= size
;
5187 if (alignment_power
> h
->root
.u
.c
.p
->alignment_power
)
5188 h
->root
.u
.c
.p
->alignment_power
= alignment_power
;
5190 h
->root
.non_ir_ref_dynamic
= non_ir_ref_dynamic
;
5194 /* Make a special call to the linker "notice" function to
5195 tell it that symbols added for crefs may need to be removed. */
5196 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
5197 goto error_free_vers
;
5200 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
5202 if (nondeflt_vers
!= NULL
)
5203 free (nondeflt_vers
);
5207 if (old_tab
!= NULL
)
5209 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
5210 goto error_free_vers
;
5215 /* Now that all the symbols from this input file are created, if
5216 not performing a relocatable link, handle .symver foo, foo@BAR
5217 such that any relocs against foo become foo@BAR. */
5218 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5222 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5224 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5225 char *shortname
, *p
;
5227 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5229 || (h
->root
.type
!= bfd_link_hash_defined
5230 && h
->root
.type
!= bfd_link_hash_defweak
))
5233 amt
= p
- h
->root
.root
.string
;
5234 shortname
= (char *) bfd_malloc (amt
+ 1);
5236 goto error_free_vers
;
5237 memcpy (shortname
, h
->root
.root
.string
, amt
);
5238 shortname
[amt
] = '\0';
5240 hi
= (struct elf_link_hash_entry
*)
5241 bfd_link_hash_lookup (&htab
->root
, shortname
,
5242 FALSE
, FALSE
, FALSE
);
5244 && hi
->root
.type
== h
->root
.type
5245 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5246 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5248 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
5249 hi
->root
.type
= bfd_link_hash_indirect
;
5250 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5251 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5252 sym_hash
= elf_sym_hashes (abfd
);
5254 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5255 if (sym_hash
[symidx
] == hi
)
5257 sym_hash
[symidx
] = h
;
5263 free (nondeflt_vers
);
5264 nondeflt_vers
= NULL
;
5267 /* Now set the alias field correctly for all the weak defined
5268 symbols we found. The only way to do this is to search all the
5269 symbols. Since we only need the information for non functions in
5270 dynamic objects, that's the only time we actually put anything on
5271 the list WEAKS. We need this information so that if a regular
5272 object refers to a symbol defined weakly in a dynamic object, the
5273 real symbol in the dynamic object is also put in the dynamic
5274 symbols; we also must arrange for both symbols to point to the
5275 same memory location. We could handle the general case of symbol
5276 aliasing, but a general symbol alias can only be generated in
5277 assembler code, handling it correctly would be very time
5278 consuming, and other ELF linkers don't handle general aliasing
5282 struct elf_link_hash_entry
**hpp
;
5283 struct elf_link_hash_entry
**hppend
;
5284 struct elf_link_hash_entry
**sorted_sym_hash
;
5285 struct elf_link_hash_entry
*h
;
5288 /* Since we have to search the whole symbol list for each weak
5289 defined symbol, search time for N weak defined symbols will be
5290 O(N^2). Binary search will cut it down to O(NlogN). */
5292 amt
*= sizeof (struct elf_link_hash_entry
*);
5293 sorted_sym_hash
= (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5294 if (sorted_sym_hash
== NULL
)
5296 sym_hash
= sorted_sym_hash
;
5297 hpp
= elf_sym_hashes (abfd
);
5298 hppend
= hpp
+ extsymcount
;
5300 for (; hpp
< hppend
; hpp
++)
5304 && h
->root
.type
== bfd_link_hash_defined
5305 && !bed
->is_function_type (h
->type
))
5313 qsort (sorted_sym_hash
, sym_count
,
5314 sizeof (struct elf_link_hash_entry
*),
5317 while (weaks
!= NULL
)
5319 struct elf_link_hash_entry
*hlook
;
5322 size_t i
, j
, idx
= 0;
5325 weaks
= hlook
->u
.alias
;
5326 hlook
->u
.alias
= NULL
;
5328 if (hlook
->root
.type
!= bfd_link_hash_defined
5329 && hlook
->root
.type
!= bfd_link_hash_defweak
)
5332 slook
= hlook
->root
.u
.def
.section
;
5333 vlook
= hlook
->root
.u
.def
.value
;
5339 bfd_signed_vma vdiff
;
5341 h
= sorted_sym_hash
[idx
];
5342 vdiff
= vlook
- h
->root
.u
.def
.value
;
5349 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5359 /* We didn't find a value/section match. */
5363 /* With multiple aliases, or when the weak symbol is already
5364 strongly defined, we have multiple matching symbols and
5365 the binary search above may land on any of them. Step
5366 one past the matching symbol(s). */
5369 h
= sorted_sym_hash
[idx
];
5370 if (h
->root
.u
.def
.section
!= slook
5371 || h
->root
.u
.def
.value
!= vlook
)
5375 /* Now look back over the aliases. Since we sorted by size
5376 as well as value and section, we'll choose the one with
5377 the largest size. */
5380 h
= sorted_sym_hash
[idx
];
5382 /* Stop if value or section doesn't match. */
5383 if (h
->root
.u
.def
.section
!= slook
5384 || h
->root
.u
.def
.value
!= vlook
)
5386 else if (h
!= hlook
)
5388 struct elf_link_hash_entry
*t
;
5391 hlook
->is_weakalias
= 1;
5393 if (t
->u
.alias
!= NULL
)
5394 while (t
->u
.alias
!= h
)
5398 /* If the weak definition is in the list of dynamic
5399 symbols, make sure the real definition is put
5401 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5403 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5406 free (sorted_sym_hash
);
5411 /* If the real definition is in the list of dynamic
5412 symbols, make sure the weak definition is put
5413 there as well. If we don't do this, then the
5414 dynamic loader might not merge the entries for the
5415 real definition and the weak definition. */
5416 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5418 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5419 goto err_free_sym_hash
;
5426 free (sorted_sym_hash
);
5429 if (bed
->check_directives
5430 && !(*bed
->check_directives
) (abfd
, info
))
5433 /* If this is a non-traditional link, try to optimize the handling
5434 of the .stab/.stabstr sections. */
5436 && ! info
->traditional_format
5437 && is_elf_hash_table (htab
)
5438 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5442 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5443 if (stabstr
!= NULL
)
5445 bfd_size_type string_offset
= 0;
5448 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5449 if (CONST_STRNEQ (stab
->name
, ".stab")
5450 && (!stab
->name
[5] ||
5451 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5452 && (stab
->flags
& SEC_MERGE
) == 0
5453 && !bfd_is_abs_section (stab
->output_section
))
5455 struct bfd_elf_section_data
*secdata
;
5457 secdata
= elf_section_data (stab
);
5458 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5459 stabstr
, &secdata
->sec_info
,
5462 if (secdata
->sec_info
)
5463 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5468 if (is_elf_hash_table (htab
) && add_needed
)
5470 /* Add this bfd to the loaded list. */
5471 struct elf_link_loaded_list
*n
;
5473 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5477 n
->next
= htab
->loaded
;
5484 if (old_tab
!= NULL
)
5486 if (old_strtab
!= NULL
)
5488 if (nondeflt_vers
!= NULL
)
5489 free (nondeflt_vers
);
5490 if (extversym
!= NULL
)
5493 if (isymbuf
!= NULL
)
5499 /* Return the linker hash table entry of a symbol that might be
5500 satisfied by an archive symbol. Return -1 on error. */
5502 struct elf_link_hash_entry
*
5503 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5504 struct bfd_link_info
*info
,
5507 struct elf_link_hash_entry
*h
;
5511 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, TRUE
);
5515 /* If this is a default version (the name contains @@), look up the
5516 symbol again with only one `@' as well as without the version.
5517 The effect is that references to the symbol with and without the
5518 version will be matched by the default symbol in the archive. */
5520 p
= strchr (name
, ELF_VER_CHR
);
5521 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5524 /* First check with only one `@'. */
5525 len
= strlen (name
);
5526 copy
= (char *) bfd_alloc (abfd
, len
);
5528 return (struct elf_link_hash_entry
*) -1;
5530 first
= p
- name
+ 1;
5531 memcpy (copy
, name
, first
);
5532 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5534 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, TRUE
);
5537 /* We also need to check references to the symbol without the
5539 copy
[first
- 1] = '\0';
5540 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
5541 FALSE
, FALSE
, TRUE
);
5544 bfd_release (abfd
, copy
);
5548 /* Add symbols from an ELF archive file to the linker hash table. We
5549 don't use _bfd_generic_link_add_archive_symbols because we need to
5550 handle versioned symbols.
5552 Fortunately, ELF archive handling is simpler than that done by
5553 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5554 oddities. In ELF, if we find a symbol in the archive map, and the
5555 symbol is currently undefined, we know that we must pull in that
5558 Unfortunately, we do have to make multiple passes over the symbol
5559 table until nothing further is resolved. */
5562 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5565 unsigned char *included
= NULL
;
5569 const struct elf_backend_data
*bed
;
5570 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
5571 (bfd
*, struct bfd_link_info
*, const char *);
5573 if (! bfd_has_map (abfd
))
5575 /* An empty archive is a special case. */
5576 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5578 bfd_set_error (bfd_error_no_armap
);
5582 /* Keep track of all symbols we know to be already defined, and all
5583 files we know to be already included. This is to speed up the
5584 second and subsequent passes. */
5585 c
= bfd_ardata (abfd
)->symdef_count
;
5589 amt
*= sizeof (*included
);
5590 included
= (unsigned char *) bfd_zmalloc (amt
);
5591 if (included
== NULL
)
5594 symdefs
= bfd_ardata (abfd
)->symdefs
;
5595 bed
= get_elf_backend_data (abfd
);
5596 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5609 symdefend
= symdef
+ c
;
5610 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5612 struct elf_link_hash_entry
*h
;
5614 struct bfd_link_hash_entry
*undefs_tail
;
5619 if (symdef
->file_offset
== last
)
5625 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5626 if (h
== (struct elf_link_hash_entry
*) -1)
5632 if (h
->root
.type
== bfd_link_hash_common
)
5634 /* We currently have a common symbol. The archive map contains
5635 a reference to this symbol, so we may want to include it. We
5636 only want to include it however, if this archive element
5637 contains a definition of the symbol, not just another common
5640 Unfortunately some archivers (including GNU ar) will put
5641 declarations of common symbols into their archive maps, as
5642 well as real definitions, so we cannot just go by the archive
5643 map alone. Instead we must read in the element's symbol
5644 table and check that to see what kind of symbol definition
5646 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5649 else if (h
->root
.type
!= bfd_link_hash_undefined
)
5651 if (h
->root
.type
!= bfd_link_hash_undefweak
)
5652 /* Symbol must be defined. Don't check it again. */
5657 /* We need to include this archive member. */
5658 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5659 if (element
== NULL
)
5662 if (! bfd_check_format (element
, bfd_object
))
5665 undefs_tail
= info
->hash
->undefs_tail
;
5667 if (!(*info
->callbacks
5668 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5670 if (!bfd_link_add_symbols (element
, info
))
5673 /* If there are any new undefined symbols, we need to make
5674 another pass through the archive in order to see whether
5675 they can be defined. FIXME: This isn't perfect, because
5676 common symbols wind up on undefs_tail and because an
5677 undefined symbol which is defined later on in this pass
5678 does not require another pass. This isn't a bug, but it
5679 does make the code less efficient than it could be. */
5680 if (undefs_tail
!= info
->hash
->undefs_tail
)
5683 /* Look backward to mark all symbols from this object file
5684 which we have already seen in this pass. */
5688 included
[mark
] = TRUE
;
5693 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5695 /* We mark subsequent symbols from this object file as we go
5696 on through the loop. */
5697 last
= symdef
->file_offset
;
5707 if (included
!= NULL
)
5712 /* Given an ELF BFD, add symbols to the global hash table as
5716 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5718 switch (bfd_get_format (abfd
))
5721 return elf_link_add_object_symbols (abfd
, info
);
5723 return elf_link_add_archive_symbols (abfd
, info
);
5725 bfd_set_error (bfd_error_wrong_format
);
5730 struct hash_codes_info
5732 unsigned long *hashcodes
;
5736 /* This function will be called though elf_link_hash_traverse to store
5737 all hash value of the exported symbols in an array. */
5740 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5742 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
5747 /* Ignore indirect symbols. These are added by the versioning code. */
5748 if (h
->dynindx
== -1)
5751 name
= h
->root
.root
.string
;
5752 if (h
->versioned
>= versioned
)
5754 char *p
= strchr (name
, ELF_VER_CHR
);
5757 alc
= (char *) bfd_malloc (p
- name
+ 1);
5763 memcpy (alc
, name
, p
- name
);
5764 alc
[p
- name
] = '\0';
5769 /* Compute the hash value. */
5770 ha
= bfd_elf_hash (name
);
5772 /* Store the found hash value in the array given as the argument. */
5773 *(inf
->hashcodes
)++ = ha
;
5775 /* And store it in the struct so that we can put it in the hash table
5777 h
->u
.elf_hash_value
= ha
;
5785 struct collect_gnu_hash_codes
5788 const struct elf_backend_data
*bed
;
5789 unsigned long int nsyms
;
5790 unsigned long int maskbits
;
5791 unsigned long int *hashcodes
;
5792 unsigned long int *hashval
;
5793 unsigned long int *indx
;
5794 unsigned long int *counts
;
5797 long int min_dynindx
;
5798 unsigned long int bucketcount
;
5799 unsigned long int symindx
;
5800 long int local_indx
;
5801 long int shift1
, shift2
;
5802 unsigned long int mask
;
5806 /* This function will be called though elf_link_hash_traverse to store
5807 all hash value of the exported symbols in an array. */
5810 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5812 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5817 /* Ignore indirect symbols. These are added by the versioning code. */
5818 if (h
->dynindx
== -1)
5821 /* Ignore also local symbols and undefined symbols. */
5822 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5825 name
= h
->root
.root
.string
;
5826 if (h
->versioned
>= versioned
)
5828 char *p
= strchr (name
, ELF_VER_CHR
);
5831 alc
= (char *) bfd_malloc (p
- name
+ 1);
5837 memcpy (alc
, name
, p
- name
);
5838 alc
[p
- name
] = '\0';
5843 /* Compute the hash value. */
5844 ha
= bfd_elf_gnu_hash (name
);
5846 /* Store the found hash value in the array for compute_bucket_count,
5847 and also for .dynsym reordering purposes. */
5848 s
->hashcodes
[s
->nsyms
] = ha
;
5849 s
->hashval
[h
->dynindx
] = ha
;
5851 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5852 s
->min_dynindx
= h
->dynindx
;
5860 /* This function will be called though elf_link_hash_traverse to do
5861 final dynaminc symbol renumbering. */
5864 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5866 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5867 unsigned long int bucket
;
5868 unsigned long int val
;
5870 /* Ignore indirect symbols. */
5871 if (h
->dynindx
== -1)
5874 /* Ignore also local symbols and undefined symbols. */
5875 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5877 if (h
->dynindx
>= s
->min_dynindx
)
5878 h
->dynindx
= s
->local_indx
++;
5882 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5883 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5884 & ((s
->maskbits
>> s
->shift1
) - 1);
5885 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5887 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5888 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5889 if (s
->counts
[bucket
] == 1)
5890 /* Last element terminates the chain. */
5892 bfd_put_32 (s
->output_bfd
, val
,
5893 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5894 --s
->counts
[bucket
];
5895 h
->dynindx
= s
->indx
[bucket
]++;
5899 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5902 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5904 return !(h
->forced_local
5905 || h
->root
.type
== bfd_link_hash_undefined
5906 || h
->root
.type
== bfd_link_hash_undefweak
5907 || ((h
->root
.type
== bfd_link_hash_defined
5908 || h
->root
.type
== bfd_link_hash_defweak
)
5909 && h
->root
.u
.def
.section
->output_section
== NULL
));
5912 /* Array used to determine the number of hash table buckets to use
5913 based on the number of symbols there are. If there are fewer than
5914 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5915 fewer than 37 we use 17 buckets, and so forth. We never use more
5916 than 32771 buckets. */
5918 static const size_t elf_buckets
[] =
5920 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5924 /* Compute bucket count for hashing table. We do not use a static set
5925 of possible tables sizes anymore. Instead we determine for all
5926 possible reasonable sizes of the table the outcome (i.e., the
5927 number of collisions etc) and choose the best solution. The
5928 weighting functions are not too simple to allow the table to grow
5929 without bounds. Instead one of the weighting factors is the size.
5930 Therefore the result is always a good payoff between few collisions
5931 (= short chain lengths) and table size. */
5933 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
5934 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
5935 unsigned long int nsyms
,
5938 size_t best_size
= 0;
5939 unsigned long int i
;
5941 /* We have a problem here. The following code to optimize the table
5942 size requires an integer type with more the 32 bits. If
5943 BFD_HOST_U_64_BIT is set we know about such a type. */
5944 #ifdef BFD_HOST_U_64_BIT
5949 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5950 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5951 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5952 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5953 unsigned long int *counts
;
5955 unsigned int no_improvement_count
= 0;
5957 /* Possible optimization parameters: if we have NSYMS symbols we say
5958 that the hashing table must at least have NSYMS/4 and at most
5960 minsize
= nsyms
/ 4;
5963 best_size
= maxsize
= nsyms
* 2;
5968 if ((best_size
& 31) == 0)
5972 /* Create array where we count the collisions in. We must use bfd_malloc
5973 since the size could be large. */
5975 amt
*= sizeof (unsigned long int);
5976 counts
= (unsigned long int *) bfd_malloc (amt
);
5980 /* Compute the "optimal" size for the hash table. The criteria is a
5981 minimal chain length. The minor criteria is (of course) the size
5983 for (i
= minsize
; i
< maxsize
; ++i
)
5985 /* Walk through the array of hashcodes and count the collisions. */
5986 BFD_HOST_U_64_BIT max
;
5987 unsigned long int j
;
5988 unsigned long int fact
;
5990 if (gnu_hash
&& (i
& 31) == 0)
5993 memset (counts
, '\0', i
* sizeof (unsigned long int));
5995 /* Determine how often each hash bucket is used. */
5996 for (j
= 0; j
< nsyms
; ++j
)
5997 ++counts
[hashcodes
[j
] % i
];
5999 /* For the weight function we need some information about the
6000 pagesize on the target. This is information need not be 100%
6001 accurate. Since this information is not available (so far) we
6002 define it here to a reasonable default value. If it is crucial
6003 to have a better value some day simply define this value. */
6004 # ifndef BFD_TARGET_PAGESIZE
6005 # define BFD_TARGET_PAGESIZE (4096)
6008 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6010 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
6013 /* Variant 1: optimize for short chains. We add the squares
6014 of all the chain lengths (which favors many small chain
6015 over a few long chains). */
6016 for (j
= 0; j
< i
; ++j
)
6017 max
+= counts
[j
] * counts
[j
];
6019 /* This adds penalties for the overall size of the table. */
6020 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6023 /* Variant 2: Optimize a lot more for small table. Here we
6024 also add squares of the size but we also add penalties for
6025 empty slots (the +1 term). */
6026 for (j
= 0; j
< i
; ++j
)
6027 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
6029 /* The overall size of the table is considered, but not as
6030 strong as in variant 1, where it is squared. */
6031 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6035 /* Compare with current best results. */
6036 if (max
< best_chlen
)
6040 no_improvement_count
= 0;
6042 /* PR 11843: Avoid futile long searches for the best bucket size
6043 when there are a large number of symbols. */
6044 else if (++no_improvement_count
== 100)
6051 #endif /* defined (BFD_HOST_U_64_BIT) */
6053 /* This is the fallback solution if no 64bit type is available or if we
6054 are not supposed to spend much time on optimizations. We select the
6055 bucket count using a fixed set of numbers. */
6056 for (i
= 0; elf_buckets
[i
] != 0; i
++)
6058 best_size
= elf_buckets
[i
];
6059 if (nsyms
< elf_buckets
[i
+ 1])
6062 if (gnu_hash
&& best_size
< 2)
6069 /* Size any SHT_GROUP section for ld -r. */
6072 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
6077 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6078 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6079 && (s
= ibfd
->sections
) != NULL
6080 && s
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
6081 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
6086 /* Set a default stack segment size. The value in INFO wins. If it
6087 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6088 undefined it is initialized. */
6091 bfd_elf_stack_segment_size (bfd
*output_bfd
,
6092 struct bfd_link_info
*info
,
6093 const char *legacy_symbol
,
6094 bfd_vma default_size
)
6096 struct elf_link_hash_entry
*h
= NULL
;
6098 /* Look for legacy symbol. */
6100 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
6101 FALSE
, FALSE
, FALSE
);
6102 if (h
&& (h
->root
.type
== bfd_link_hash_defined
6103 || h
->root
.type
== bfd_link_hash_defweak
)
6105 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
6107 /* The symbol has no type if specified on the command line. */
6108 h
->type
= STT_OBJECT
;
6109 if (info
->stacksize
)
6110 /* xgettext:c-format */
6111 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6112 output_bfd
, legacy_symbol
);
6113 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
6114 /* xgettext:c-format */
6115 _bfd_error_handler (_("%pB: %s not absolute"),
6116 output_bfd
, legacy_symbol
);
6118 info
->stacksize
= h
->root
.u
.def
.value
;
6121 if (!info
->stacksize
)
6122 /* If the user didn't set a size, or explicitly inhibit the
6123 size, set it now. */
6124 info
->stacksize
= default_size
;
6126 /* Provide the legacy symbol, if it is referenced. */
6127 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
6128 || h
->root
.type
== bfd_link_hash_undefweak
))
6130 struct bfd_link_hash_entry
*bh
= NULL
;
6132 if (!(_bfd_generic_link_add_one_symbol
6133 (info
, output_bfd
, legacy_symbol
,
6134 BSF_GLOBAL
, bfd_abs_section_ptr
,
6135 info
->stacksize
>= 0 ? info
->stacksize
: 0,
6136 NULL
, FALSE
, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
6139 h
= (struct elf_link_hash_entry
*) bh
;
6141 h
->type
= STT_OBJECT
;
6147 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6149 struct elf_gc_sweep_symbol_info
6151 struct bfd_link_info
*info
;
6152 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
6157 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
6160 && (((h
->root
.type
== bfd_link_hash_defined
6161 || h
->root
.type
== bfd_link_hash_defweak
)
6162 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
6163 && h
->root
.u
.def
.section
->gc_mark
))
6164 || h
->root
.type
== bfd_link_hash_undefined
6165 || h
->root
.type
== bfd_link_hash_undefweak
))
6167 struct elf_gc_sweep_symbol_info
*inf
;
6169 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
6170 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
6173 h
->ref_regular_nonweak
= 0;
6179 /* Set up the sizes and contents of the ELF dynamic sections. This is
6180 called by the ELF linker emulation before_allocation routine. We
6181 must set the sizes of the sections before the linker sets the
6182 addresses of the various sections. */
6185 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
6188 const char *filter_shlib
,
6190 const char *depaudit
,
6191 const char * const *auxiliary_filters
,
6192 struct bfd_link_info
*info
,
6193 asection
**sinterpptr
)
6196 const struct elf_backend_data
*bed
;
6200 if (!is_elf_hash_table (info
->hash
))
6203 dynobj
= elf_hash_table (info
)->dynobj
;
6205 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6207 struct bfd_elf_version_tree
*verdefs
;
6208 struct elf_info_failed asvinfo
;
6209 struct bfd_elf_version_tree
*t
;
6210 struct bfd_elf_version_expr
*d
;
6214 /* If we are supposed to export all symbols into the dynamic symbol
6215 table (this is not the normal case), then do so. */
6216 if (info
->export_dynamic
6217 || (bfd_link_executable (info
) && info
->dynamic
))
6219 struct elf_info_failed eif
;
6223 elf_link_hash_traverse (elf_hash_table (info
),
6224 _bfd_elf_export_symbol
,
6232 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6234 if (soname_indx
== (size_t) -1
6235 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6239 soname_indx
= (size_t) -1;
6241 /* Make all global versions with definition. */
6242 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6243 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6244 if (!d
->symver
&& d
->literal
)
6246 const char *verstr
, *name
;
6247 size_t namelen
, verlen
, newlen
;
6248 char *newname
, *p
, leading_char
;
6249 struct elf_link_hash_entry
*newh
;
6251 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6253 namelen
= strlen (name
) + (leading_char
!= '\0');
6255 verlen
= strlen (verstr
);
6256 newlen
= namelen
+ verlen
+ 3;
6258 newname
= (char *) bfd_malloc (newlen
);
6259 if (newname
== NULL
)
6261 newname
[0] = leading_char
;
6262 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6264 /* Check the hidden versioned definition. */
6265 p
= newname
+ namelen
;
6267 memcpy (p
, verstr
, verlen
+ 1);
6268 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6269 newname
, FALSE
, FALSE
,
6272 || (newh
->root
.type
!= bfd_link_hash_defined
6273 && newh
->root
.type
!= bfd_link_hash_defweak
))
6275 /* Check the default versioned definition. */
6277 memcpy (p
, verstr
, verlen
+ 1);
6278 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6279 newname
, FALSE
, FALSE
,
6284 /* Mark this version if there is a definition and it is
6285 not defined in a shared object. */
6287 && !newh
->def_dynamic
6288 && (newh
->root
.type
== bfd_link_hash_defined
6289 || newh
->root
.type
== bfd_link_hash_defweak
))
6293 /* Attach all the symbols to their version information. */
6294 asvinfo
.info
= info
;
6295 asvinfo
.failed
= FALSE
;
6297 elf_link_hash_traverse (elf_hash_table (info
),
6298 _bfd_elf_link_assign_sym_version
,
6303 if (!info
->allow_undefined_version
)
6305 /* Check if all global versions have a definition. */
6306 bfd_boolean all_defined
= TRUE
;
6307 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6308 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6309 if (d
->literal
&& !d
->symver
&& !d
->script
)
6312 (_("%s: undefined version: %s"),
6313 d
->pattern
, t
->name
);
6314 all_defined
= FALSE
;
6319 bfd_set_error (bfd_error_bad_value
);
6324 /* Set up the version definition section. */
6325 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6326 BFD_ASSERT (s
!= NULL
);
6328 /* We may have created additional version definitions if we are
6329 just linking a regular application. */
6330 verdefs
= info
->version_info
;
6332 /* Skip anonymous version tag. */
6333 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6334 verdefs
= verdefs
->next
;
6336 if (verdefs
== NULL
&& !info
->create_default_symver
)
6337 s
->flags
|= SEC_EXCLUDE
;
6343 Elf_Internal_Verdef def
;
6344 Elf_Internal_Verdaux defaux
;
6345 struct bfd_link_hash_entry
*bh
;
6346 struct elf_link_hash_entry
*h
;
6352 /* Make space for the base version. */
6353 size
+= sizeof (Elf_External_Verdef
);
6354 size
+= sizeof (Elf_External_Verdaux
);
6357 /* Make space for the default version. */
6358 if (info
->create_default_symver
)
6360 size
+= sizeof (Elf_External_Verdef
);
6364 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6366 struct bfd_elf_version_deps
*n
;
6368 /* Don't emit base version twice. */
6372 size
+= sizeof (Elf_External_Verdef
);
6373 size
+= sizeof (Elf_External_Verdaux
);
6376 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6377 size
+= sizeof (Elf_External_Verdaux
);
6381 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6382 if (s
->contents
== NULL
&& s
->size
!= 0)
6385 /* Fill in the version definition section. */
6389 def
.vd_version
= VER_DEF_CURRENT
;
6390 def
.vd_flags
= VER_FLG_BASE
;
6393 if (info
->create_default_symver
)
6395 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6396 def
.vd_next
= sizeof (Elf_External_Verdef
);
6400 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6401 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6402 + sizeof (Elf_External_Verdaux
));
6405 if (soname_indx
!= (size_t) -1)
6407 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6409 def
.vd_hash
= bfd_elf_hash (soname
);
6410 defaux
.vda_name
= soname_indx
;
6417 name
= lbasename (output_bfd
->filename
);
6418 def
.vd_hash
= bfd_elf_hash (name
);
6419 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6421 if (indx
== (size_t) -1)
6423 defaux
.vda_name
= indx
;
6425 defaux
.vda_next
= 0;
6427 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6428 (Elf_External_Verdef
*) p
);
6429 p
+= sizeof (Elf_External_Verdef
);
6430 if (info
->create_default_symver
)
6432 /* Add a symbol representing this version. */
6434 if (! (_bfd_generic_link_add_one_symbol
6435 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6437 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6439 h
= (struct elf_link_hash_entry
*) bh
;
6442 h
->type
= STT_OBJECT
;
6443 h
->verinfo
.vertree
= NULL
;
6445 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6448 /* Create a duplicate of the base version with the same
6449 aux block, but different flags. */
6452 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6454 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6455 + sizeof (Elf_External_Verdaux
));
6458 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6459 (Elf_External_Verdef
*) p
);
6460 p
+= sizeof (Elf_External_Verdef
);
6462 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6463 (Elf_External_Verdaux
*) p
);
6464 p
+= sizeof (Elf_External_Verdaux
);
6466 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6469 struct bfd_elf_version_deps
*n
;
6471 /* Don't emit the base version twice. */
6476 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6479 /* Add a symbol representing this version. */
6481 if (! (_bfd_generic_link_add_one_symbol
6482 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6484 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6486 h
= (struct elf_link_hash_entry
*) bh
;
6489 h
->type
= STT_OBJECT
;
6490 h
->verinfo
.vertree
= t
;
6492 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6495 def
.vd_version
= VER_DEF_CURRENT
;
6497 if (t
->globals
.list
== NULL
6498 && t
->locals
.list
== NULL
6500 def
.vd_flags
|= VER_FLG_WEAK
;
6501 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6502 def
.vd_cnt
= cdeps
+ 1;
6503 def
.vd_hash
= bfd_elf_hash (t
->name
);
6504 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6507 /* If a basever node is next, it *must* be the last node in
6508 the chain, otherwise Verdef construction breaks. */
6509 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6510 BFD_ASSERT (t
->next
->next
== NULL
);
6512 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6513 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6514 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6516 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6517 (Elf_External_Verdef
*) p
);
6518 p
+= sizeof (Elf_External_Verdef
);
6520 defaux
.vda_name
= h
->dynstr_index
;
6521 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6523 defaux
.vda_next
= 0;
6524 if (t
->deps
!= NULL
)
6525 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6526 t
->name_indx
= defaux
.vda_name
;
6528 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6529 (Elf_External_Verdaux
*) p
);
6530 p
+= sizeof (Elf_External_Verdaux
);
6532 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6534 if (n
->version_needed
== NULL
)
6536 /* This can happen if there was an error in the
6538 defaux
.vda_name
= 0;
6542 defaux
.vda_name
= n
->version_needed
->name_indx
;
6543 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6546 if (n
->next
== NULL
)
6547 defaux
.vda_next
= 0;
6549 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6551 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6552 (Elf_External_Verdaux
*) p
);
6553 p
+= sizeof (Elf_External_Verdaux
);
6557 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6561 bed
= get_elf_backend_data (output_bfd
);
6563 if (info
->gc_sections
&& bed
->can_gc_sections
)
6565 struct elf_gc_sweep_symbol_info sweep_info
;
6567 /* Remove the symbols that were in the swept sections from the
6568 dynamic symbol table. */
6569 sweep_info
.info
= info
;
6570 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
6571 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
6575 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6578 struct elf_find_verdep_info sinfo
;
6580 /* Work out the size of the version reference section. */
6582 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6583 BFD_ASSERT (s
!= NULL
);
6586 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6587 if (sinfo
.vers
== 0)
6589 sinfo
.failed
= FALSE
;
6591 elf_link_hash_traverse (elf_hash_table (info
),
6592 _bfd_elf_link_find_version_dependencies
,
6597 if (elf_tdata (output_bfd
)->verref
== NULL
)
6598 s
->flags
|= SEC_EXCLUDE
;
6601 Elf_Internal_Verneed
*vn
;
6606 /* Build the version dependency section. */
6609 for (vn
= elf_tdata (output_bfd
)->verref
;
6611 vn
= vn
->vn_nextref
)
6613 Elf_Internal_Vernaux
*a
;
6615 size
+= sizeof (Elf_External_Verneed
);
6617 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6618 size
+= sizeof (Elf_External_Vernaux
);
6622 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6623 if (s
->contents
== NULL
)
6627 for (vn
= elf_tdata (output_bfd
)->verref
;
6629 vn
= vn
->vn_nextref
)
6632 Elf_Internal_Vernaux
*a
;
6636 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6639 vn
->vn_version
= VER_NEED_CURRENT
;
6641 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6642 elf_dt_name (vn
->vn_bfd
) != NULL
6643 ? elf_dt_name (vn
->vn_bfd
)
6644 : lbasename (vn
->vn_bfd
->filename
),
6646 if (indx
== (size_t) -1)
6649 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
6650 if (vn
->vn_nextref
== NULL
)
6653 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
6654 + caux
* sizeof (Elf_External_Vernaux
));
6656 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
6657 (Elf_External_Verneed
*) p
);
6658 p
+= sizeof (Elf_External_Verneed
);
6660 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6662 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6663 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6664 a
->vna_nodename
, FALSE
);
6665 if (indx
== (size_t) -1)
6668 if (a
->vna_nextptr
== NULL
)
6671 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6673 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6674 (Elf_External_Vernaux
*) p
);
6675 p
+= sizeof (Elf_External_Vernaux
);
6679 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6683 /* Any syms created from now on start with -1 in
6684 got.refcount/offset and plt.refcount/offset. */
6685 elf_hash_table (info
)->init_got_refcount
6686 = elf_hash_table (info
)->init_got_offset
;
6687 elf_hash_table (info
)->init_plt_refcount
6688 = elf_hash_table (info
)->init_plt_offset
;
6690 if (bfd_link_relocatable (info
)
6691 && !_bfd_elf_size_group_sections (info
))
6694 /* The backend may have to create some sections regardless of whether
6695 we're dynamic or not. */
6696 if (bed
->elf_backend_always_size_sections
6697 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
6700 /* Determine any GNU_STACK segment requirements, after the backend
6701 has had a chance to set a default segment size. */
6702 if (info
->execstack
)
6703 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
6704 else if (info
->noexecstack
)
6705 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
6709 asection
*notesec
= NULL
;
6712 for (inputobj
= info
->input_bfds
;
6714 inputobj
= inputobj
->link
.next
)
6719 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
6721 s
= inputobj
->sections
;
6722 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
6725 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
6728 if (s
->flags
& SEC_CODE
)
6732 else if (bed
->default_execstack
)
6735 if (notesec
|| info
->stacksize
> 0)
6736 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
6737 if (notesec
&& exec
&& bfd_link_relocatable (info
)
6738 && notesec
->output_section
!= bfd_abs_section_ptr
)
6739 notesec
->output_section
->flags
|= SEC_CODE
;
6742 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6744 struct elf_info_failed eif
;
6745 struct elf_link_hash_entry
*h
;
6749 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
6750 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
6754 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
6756 info
->flags
|= DF_SYMBOLIC
;
6764 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
6766 if (indx
== (size_t) -1)
6769 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
6770 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
6774 if (filter_shlib
!= NULL
)
6778 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6779 filter_shlib
, TRUE
);
6780 if (indx
== (size_t) -1
6781 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
6785 if (auxiliary_filters
!= NULL
)
6787 const char * const *p
;
6789 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
6793 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6795 if (indx
== (size_t) -1
6796 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
6805 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
6807 if (indx
== (size_t) -1
6808 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
6812 if (depaudit
!= NULL
)
6816 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
6818 if (indx
== (size_t) -1
6819 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
6826 /* Find all symbols which were defined in a dynamic object and make
6827 the backend pick a reasonable value for them. */
6828 elf_link_hash_traverse (elf_hash_table (info
),
6829 _bfd_elf_adjust_dynamic_symbol
,
6834 /* Add some entries to the .dynamic section. We fill in some of the
6835 values later, in bfd_elf_final_link, but we must add the entries
6836 now so that we know the final size of the .dynamic section. */
6838 /* If there are initialization and/or finalization functions to
6839 call then add the corresponding DT_INIT/DT_FINI entries. */
6840 h
= (info
->init_function
6841 ? elf_link_hash_lookup (elf_hash_table (info
),
6842 info
->init_function
, FALSE
,
6849 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
6852 h
= (info
->fini_function
6853 ? elf_link_hash_lookup (elf_hash_table (info
),
6854 info
->fini_function
, FALSE
,
6861 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
6865 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
6866 if (s
!= NULL
&& s
->linker_has_input
)
6868 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6869 if (! bfd_link_executable (info
))
6874 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
6875 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
6876 && (o
= sub
->sections
) != NULL
6877 && o
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
)
6878 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
6879 if (elf_section_data (o
)->this_hdr
.sh_type
6880 == SHT_PREINIT_ARRAY
)
6883 (_("%pB: .preinit_array section is not allowed in DSO"),
6888 bfd_set_error (bfd_error_nonrepresentable_section
);
6892 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
6893 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
6896 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
6897 if (s
!= NULL
&& s
->linker_has_input
)
6899 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
6900 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
6903 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
6904 if (s
!= NULL
&& s
->linker_has_input
)
6906 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
6907 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
6911 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
6912 /* If .dynstr is excluded from the link, we don't want any of
6913 these tags. Strictly, we should be checking each section
6914 individually; This quick check covers for the case where
6915 someone does a /DISCARD/ : { *(*) }. */
6916 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
6918 bfd_size_type strsize
;
6920 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6921 if ((info
->emit_hash
6922 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
6923 || (info
->emit_gnu_hash
6924 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
6925 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
6926 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
6927 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
6928 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
6929 bed
->s
->sizeof_sym
))
6934 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
6937 /* The backend must work out the sizes of all the other dynamic
6940 && bed
->elf_backend_size_dynamic_sections
!= NULL
6941 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
6944 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6946 if (elf_tdata (output_bfd
)->cverdefs
)
6948 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
6950 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
6951 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
6955 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
6957 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
6960 else if (info
->flags
& DF_BIND_NOW
)
6962 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
6968 if (bfd_link_executable (info
))
6969 info
->flags_1
&= ~ (DF_1_INITFIRST
6972 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
6976 if (elf_tdata (output_bfd
)->cverrefs
)
6978 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
6980 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
6981 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
6985 if ((elf_tdata (output_bfd
)->cverrefs
== 0
6986 && elf_tdata (output_bfd
)->cverdefs
== 0)
6987 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, NULL
) <= 1)
6991 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6992 s
->flags
|= SEC_EXCLUDE
;
6998 /* Find the first non-excluded output section. We'll use its
6999 section symbol for some emitted relocs. */
7001 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
7005 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7006 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7007 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7009 elf_hash_table (info
)->text_index_section
= s
;
7014 /* Find two non-excluded output sections, one for code, one for data.
7015 We'll use their section symbols for some emitted relocs. */
7017 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
7021 /* Data first, since setting text_index_section changes
7022 _bfd_elf_omit_section_dynsym_default. */
7023 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7024 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
7025 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7027 elf_hash_table (info
)->data_index_section
= s
;
7031 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7032 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
7033 == (SEC_ALLOC
| SEC_READONLY
))
7034 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7036 elf_hash_table (info
)->text_index_section
= s
;
7040 if (elf_hash_table (info
)->text_index_section
== NULL
)
7041 elf_hash_table (info
)->text_index_section
7042 = elf_hash_table (info
)->data_index_section
;
7046 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
7048 const struct elf_backend_data
*bed
;
7049 unsigned long section_sym_count
;
7050 bfd_size_type dynsymcount
= 0;
7052 if (!is_elf_hash_table (info
->hash
))
7055 bed
= get_elf_backend_data (output_bfd
);
7056 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
7058 /* Assign dynsym indices. In a shared library we generate a section
7059 symbol for each output section, which come first. Next come all
7060 of the back-end allocated local dynamic syms, followed by the rest
7061 of the global symbols.
7063 This is usually not needed for static binaries, however backends
7064 can request to always do it, e.g. the MIPS backend uses dynamic
7065 symbol counts to lay out GOT, which will be produced in the
7066 presence of GOT relocations even in static binaries (holding fixed
7067 data in that case, to satisfy those relocations). */
7069 if (elf_hash_table (info
)->dynamic_sections_created
7070 || bed
->always_renumber_dynsyms
)
7071 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
7072 §ion_sym_count
);
7074 if (elf_hash_table (info
)->dynamic_sections_created
)
7078 unsigned int dtagcount
;
7080 dynobj
= elf_hash_table (info
)->dynobj
;
7082 /* Work out the size of the symbol version section. */
7083 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7084 BFD_ASSERT (s
!= NULL
);
7085 if ((s
->flags
& SEC_EXCLUDE
) == 0)
7087 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
7088 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7089 if (s
->contents
== NULL
)
7092 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
7096 /* Set the size of the .dynsym and .hash sections. We counted
7097 the number of dynamic symbols in elf_link_add_object_symbols.
7098 We will build the contents of .dynsym and .hash when we build
7099 the final symbol table, because until then we do not know the
7100 correct value to give the symbols. We built the .dynstr
7101 section as we went along in elf_link_add_object_symbols. */
7102 s
= elf_hash_table (info
)->dynsym
;
7103 BFD_ASSERT (s
!= NULL
);
7104 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
7106 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7107 if (s
->contents
== NULL
)
7110 /* The first entry in .dynsym is a dummy symbol. Clear all the
7111 section syms, in case we don't output them all. */
7112 ++section_sym_count
;
7113 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
7115 elf_hash_table (info
)->bucketcount
= 0;
7117 /* Compute the size of the hashing table. As a side effect this
7118 computes the hash values for all the names we export. */
7119 if (info
->emit_hash
)
7121 unsigned long int *hashcodes
;
7122 struct hash_codes_info hashinf
;
7124 unsigned long int nsyms
;
7126 size_t hash_entry_size
;
7128 /* Compute the hash values for all exported symbols. At the same
7129 time store the values in an array so that we could use them for
7131 amt
= dynsymcount
* sizeof (unsigned long int);
7132 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
7133 if (hashcodes
== NULL
)
7135 hashinf
.hashcodes
= hashcodes
;
7136 hashinf
.error
= FALSE
;
7138 /* Put all hash values in HASHCODES. */
7139 elf_link_hash_traverse (elf_hash_table (info
),
7140 elf_collect_hash_codes
, &hashinf
);
7147 nsyms
= hashinf
.hashcodes
- hashcodes
;
7149 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
7152 if (bucketcount
== 0 && nsyms
> 0)
7155 elf_hash_table (info
)->bucketcount
= bucketcount
;
7157 s
= bfd_get_linker_section (dynobj
, ".hash");
7158 BFD_ASSERT (s
!= NULL
);
7159 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
7160 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
7161 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7162 if (s
->contents
== NULL
)
7165 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
7166 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
7167 s
->contents
+ hash_entry_size
);
7170 if (info
->emit_gnu_hash
)
7173 unsigned char *contents
;
7174 struct collect_gnu_hash_codes cinfo
;
7178 memset (&cinfo
, 0, sizeof (cinfo
));
7180 /* Compute the hash values for all exported symbols. At the same
7181 time store the values in an array so that we could use them for
7183 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
7184 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
7185 if (cinfo
.hashcodes
== NULL
)
7188 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
7189 cinfo
.min_dynindx
= -1;
7190 cinfo
.output_bfd
= output_bfd
;
7193 /* Put all hash values in HASHCODES. */
7194 elf_link_hash_traverse (elf_hash_table (info
),
7195 elf_collect_gnu_hash_codes
, &cinfo
);
7198 free (cinfo
.hashcodes
);
7203 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
7205 if (bucketcount
== 0)
7207 free (cinfo
.hashcodes
);
7211 s
= bfd_get_linker_section (dynobj
, ".gnu.hash");
7212 BFD_ASSERT (s
!= NULL
);
7214 if (cinfo
.nsyms
== 0)
7216 /* Empty .gnu.hash section is special. */
7217 BFD_ASSERT (cinfo
.min_dynindx
== -1);
7218 free (cinfo
.hashcodes
);
7219 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
7220 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7221 if (contents
== NULL
)
7223 s
->contents
= contents
;
7224 /* 1 empty bucket. */
7225 bfd_put_32 (output_bfd
, 1, contents
);
7226 /* SYMIDX above the special symbol 0. */
7227 bfd_put_32 (output_bfd
, 1, contents
+ 4);
7228 /* Just one word for bitmask. */
7229 bfd_put_32 (output_bfd
, 1, contents
+ 8);
7230 /* Only hash fn bloom filter. */
7231 bfd_put_32 (output_bfd
, 0, contents
+ 12);
7232 /* No hashes are valid - empty bitmask. */
7233 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
7234 /* No hashes in the only bucket. */
7235 bfd_put_32 (output_bfd
, 0,
7236 contents
+ 16 + bed
->s
->arch_size
/ 8);
7240 unsigned long int maskwords
, maskbitslog2
, x
;
7241 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
7245 while ((x
>>= 1) != 0)
7247 if (maskbitslog2
< 3)
7249 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
7250 maskbitslog2
= maskbitslog2
+ 3;
7252 maskbitslog2
= maskbitslog2
+ 2;
7253 if (bed
->s
->arch_size
== 64)
7255 if (maskbitslog2
== 5)
7261 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
7262 cinfo
.shift2
= maskbitslog2
;
7263 cinfo
.maskbits
= 1 << maskbitslog2
;
7264 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
7265 amt
= bucketcount
* sizeof (unsigned long int) * 2;
7266 amt
+= maskwords
* sizeof (bfd_vma
);
7267 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
7268 if (cinfo
.bitmask
== NULL
)
7270 free (cinfo
.hashcodes
);
7274 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
7275 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
7276 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
7277 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
7279 /* Determine how often each hash bucket is used. */
7280 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
7281 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
7282 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
7284 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
7285 if (cinfo
.counts
[i
] != 0)
7287 cinfo
.indx
[i
] = cnt
;
7288 cnt
+= cinfo
.counts
[i
];
7290 BFD_ASSERT (cnt
== dynsymcount
);
7291 cinfo
.bucketcount
= bucketcount
;
7292 cinfo
.local_indx
= cinfo
.min_dynindx
;
7294 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
7295 s
->size
+= cinfo
.maskbits
/ 8;
7296 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7297 if (contents
== NULL
)
7299 free (cinfo
.bitmask
);
7300 free (cinfo
.hashcodes
);
7304 s
->contents
= contents
;
7305 bfd_put_32 (output_bfd
, bucketcount
, contents
);
7306 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
7307 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
7308 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
7309 contents
+= 16 + cinfo
.maskbits
/ 8;
7311 for (i
= 0; i
< bucketcount
; ++i
)
7313 if (cinfo
.counts
[i
] == 0)
7314 bfd_put_32 (output_bfd
, 0, contents
);
7316 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
7320 cinfo
.contents
= contents
;
7322 /* Renumber dynamic symbols, populate .gnu.hash section. */
7323 elf_link_hash_traverse (elf_hash_table (info
),
7324 elf_renumber_gnu_hash_syms
, &cinfo
);
7326 contents
= s
->contents
+ 16;
7327 for (i
= 0; i
< maskwords
; ++i
)
7329 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
7331 contents
+= bed
->s
->arch_size
/ 8;
7334 free (cinfo
.bitmask
);
7335 free (cinfo
.hashcodes
);
7339 s
= bfd_get_linker_section (dynobj
, ".dynstr");
7340 BFD_ASSERT (s
!= NULL
);
7342 elf_finalize_dynstr (output_bfd
, info
);
7344 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7346 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
7347 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
7354 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7357 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
7360 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
7361 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
7364 /* Finish SHF_MERGE section merging. */
7367 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
7372 if (!is_elf_hash_table (info
->hash
))
7375 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
7376 if ((ibfd
->flags
& DYNAMIC
) == 0
7377 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
7378 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
7379 == get_elf_backend_data (obfd
)->s
->elfclass
))
7380 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7381 if ((sec
->flags
& SEC_MERGE
) != 0
7382 && !bfd_is_abs_section (sec
->output_section
))
7384 struct bfd_elf_section_data
*secdata
;
7386 secdata
= elf_section_data (sec
);
7387 if (! _bfd_add_merge_section (obfd
,
7388 &elf_hash_table (info
)->merge_info
,
7389 sec
, &secdata
->sec_info
))
7391 else if (secdata
->sec_info
)
7392 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7395 if (elf_hash_table (info
)->merge_info
!= NULL
)
7396 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7397 merge_sections_remove_hook
);
7401 /* Create an entry in an ELF linker hash table. */
7403 struct bfd_hash_entry
*
7404 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7405 struct bfd_hash_table
*table
,
7408 /* Allocate the structure if it has not already been allocated by a
7412 entry
= (struct bfd_hash_entry
*)
7413 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7418 /* Call the allocation method of the superclass. */
7419 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7422 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7423 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7425 /* Set local fields. */
7428 ret
->got
= htab
->init_got_refcount
;
7429 ret
->plt
= htab
->init_plt_refcount
;
7430 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7431 - offsetof (struct elf_link_hash_entry
, size
)));
7432 /* Assume that we have been called by a non-ELF symbol reader.
7433 This flag is then reset by the code which reads an ELF input
7434 file. This ensures that a symbol created by a non-ELF symbol
7435 reader will have the flag set correctly. */
7442 /* Copy data from an indirect symbol to its direct symbol, hiding the
7443 old indirect symbol. Also used for copying flags to a weakdef. */
7446 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7447 struct elf_link_hash_entry
*dir
,
7448 struct elf_link_hash_entry
*ind
)
7450 struct elf_link_hash_table
*htab
;
7452 /* Copy down any references that we may have already seen to the
7453 symbol which just became indirect. */
7455 if (dir
->versioned
!= versioned_hidden
)
7456 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7457 dir
->ref_regular
|= ind
->ref_regular
;
7458 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7459 dir
->non_got_ref
|= ind
->non_got_ref
;
7460 dir
->needs_plt
|= ind
->needs_plt
;
7461 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7463 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7466 /* Copy over the global and procedure linkage table refcount entries.
7467 These may have been already set up by a check_relocs routine. */
7468 htab
= elf_hash_table (info
);
7469 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7471 if (dir
->got
.refcount
< 0)
7472 dir
->got
.refcount
= 0;
7473 dir
->got
.refcount
+= ind
->got
.refcount
;
7474 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7477 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7479 if (dir
->plt
.refcount
< 0)
7480 dir
->plt
.refcount
= 0;
7481 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7482 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7485 if (ind
->dynindx
!= -1)
7487 if (dir
->dynindx
!= -1)
7488 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7489 dir
->dynindx
= ind
->dynindx
;
7490 dir
->dynstr_index
= ind
->dynstr_index
;
7492 ind
->dynstr_index
= 0;
7497 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7498 struct elf_link_hash_entry
*h
,
7499 bfd_boolean force_local
)
7501 /* STT_GNU_IFUNC symbol must go through PLT. */
7502 if (h
->type
!= STT_GNU_IFUNC
)
7504 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7509 h
->forced_local
= 1;
7510 if (h
->dynindx
!= -1)
7512 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7515 h
->dynstr_index
= 0;
7520 /* Hide a symbol. */
7523 _bfd_elf_link_hide_symbol (bfd
*output_bfd
,
7524 struct bfd_link_info
*info
,
7525 struct bfd_link_hash_entry
*h
)
7527 if (is_elf_hash_table (info
->hash
))
7529 const struct elf_backend_data
*bed
7530 = get_elf_backend_data (output_bfd
);
7531 struct elf_link_hash_entry
*eh
7532 = (struct elf_link_hash_entry
*) h
;
7533 bed
->elf_backend_hide_symbol (info
, eh
, TRUE
);
7534 eh
->def_dynamic
= 0;
7535 eh
->ref_dynamic
= 0;
7536 eh
->dynamic_def
= 0;
7540 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7544 _bfd_elf_link_hash_table_init
7545 (struct elf_link_hash_table
*table
,
7547 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
7548 struct bfd_hash_table
*,
7550 unsigned int entsize
,
7551 enum elf_target_id target_id
)
7554 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
7556 table
->init_got_refcount
.refcount
= can_refcount
- 1;
7557 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
7558 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
7559 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
7560 /* The first dynamic symbol is a dummy. */
7561 table
->dynsymcount
= 1;
7563 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
7565 table
->root
.type
= bfd_link_elf_hash_table
;
7566 table
->hash_table_id
= target_id
;
7571 /* Create an ELF linker hash table. */
7573 struct bfd_link_hash_table
*
7574 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7576 struct elf_link_hash_table
*ret
;
7577 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
7579 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7583 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7584 sizeof (struct elf_link_hash_entry
),
7590 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7595 /* Destroy an ELF linker hash table. */
7598 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7600 struct elf_link_hash_table
*htab
;
7602 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7603 if (htab
->dynstr
!= NULL
)
7604 _bfd_elf_strtab_free (htab
->dynstr
);
7605 _bfd_merge_sections_free (htab
->merge_info
);
7606 _bfd_generic_link_hash_table_free (obfd
);
7609 /* This is a hook for the ELF emulation code in the generic linker to
7610 tell the backend linker what file name to use for the DT_NEEDED
7611 entry for a dynamic object. */
7614 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7616 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7617 && bfd_get_format (abfd
) == bfd_object
)
7618 elf_dt_name (abfd
) = name
;
7622 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
7625 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7626 && bfd_get_format (abfd
) == bfd_object
)
7627 lib_class
= elf_dyn_lib_class (abfd
);
7634 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
7636 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7637 && bfd_get_format (abfd
) == bfd_object
)
7638 elf_dyn_lib_class (abfd
) = lib_class
;
7641 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7642 the linker ELF emulation code. */
7644 struct bfd_link_needed_list
*
7645 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7646 struct bfd_link_info
*info
)
7648 if (! is_elf_hash_table (info
->hash
))
7650 return elf_hash_table (info
)->needed
;
7653 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7654 hook for the linker ELF emulation code. */
7656 struct bfd_link_needed_list
*
7657 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7658 struct bfd_link_info
*info
)
7660 if (! is_elf_hash_table (info
->hash
))
7662 return elf_hash_table (info
)->runpath
;
7665 /* Get the name actually used for a dynamic object for a link. This
7666 is the SONAME entry if there is one. Otherwise, it is the string
7667 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7670 bfd_elf_get_dt_soname (bfd
*abfd
)
7672 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7673 && bfd_get_format (abfd
) == bfd_object
)
7674 return elf_dt_name (abfd
);
7678 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7679 the ELF linker emulation code. */
7682 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
7683 struct bfd_link_needed_list
**pneeded
)
7686 bfd_byte
*dynbuf
= NULL
;
7687 unsigned int elfsec
;
7688 unsigned long shlink
;
7689 bfd_byte
*extdyn
, *extdynend
;
7691 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
7695 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
7696 || bfd_get_format (abfd
) != bfd_object
)
7699 s
= bfd_get_section_by_name (abfd
, ".dynamic");
7700 if (s
== NULL
|| s
->size
== 0)
7703 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
7706 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
7707 if (elfsec
== SHN_BAD
)
7710 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
7712 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
7713 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
7716 extdynend
= extdyn
+ s
->size
;
7717 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
7719 Elf_Internal_Dyn dyn
;
7721 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
7723 if (dyn
.d_tag
== DT_NULL
)
7726 if (dyn
.d_tag
== DT_NEEDED
)
7729 struct bfd_link_needed_list
*l
;
7730 unsigned int tagv
= dyn
.d_un
.d_val
;
7733 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
7738 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
7759 struct elf_symbuf_symbol
7761 unsigned long st_name
; /* Symbol name, index in string tbl */
7762 unsigned char st_info
; /* Type and binding attributes */
7763 unsigned char st_other
; /* Visibilty, and target specific */
7766 struct elf_symbuf_head
7768 struct elf_symbuf_symbol
*ssym
;
7770 unsigned int st_shndx
;
7777 Elf_Internal_Sym
*isym
;
7778 struct elf_symbuf_symbol
*ssym
;
7783 /* Sort references to symbols by ascending section number. */
7786 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
7788 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
7789 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
7791 return s1
->st_shndx
- s2
->st_shndx
;
7795 elf_sym_name_compare (const void *arg1
, const void *arg2
)
7797 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
7798 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
7799 return strcmp (s1
->name
, s2
->name
);
7802 static struct elf_symbuf_head
*
7803 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
7805 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
7806 struct elf_symbuf_symbol
*ssym
;
7807 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
7808 size_t i
, shndx_count
, total_size
;
7810 indbuf
= (Elf_Internal_Sym
**) bfd_malloc2 (symcount
, sizeof (*indbuf
));
7814 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
7815 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
7816 *ind
++ = &isymbuf
[i
];
7819 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
7820 elf_sort_elf_symbol
);
7823 if (indbufend
> indbuf
)
7824 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
7825 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
7828 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
7829 + (indbufend
- indbuf
) * sizeof (*ssym
));
7830 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
7831 if (ssymbuf
== NULL
)
7837 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
7838 ssymbuf
->ssym
= NULL
;
7839 ssymbuf
->count
= shndx_count
;
7840 ssymbuf
->st_shndx
= 0;
7841 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
7843 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
7846 ssymhead
->ssym
= ssym
;
7847 ssymhead
->count
= 0;
7848 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
7850 ssym
->st_name
= (*ind
)->st_name
;
7851 ssym
->st_info
= (*ind
)->st_info
;
7852 ssym
->st_other
= (*ind
)->st_other
;
7855 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
7856 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
7863 /* Check if 2 sections define the same set of local and global
7867 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
7868 struct bfd_link_info
*info
)
7871 const struct elf_backend_data
*bed1
, *bed2
;
7872 Elf_Internal_Shdr
*hdr1
, *hdr2
;
7873 size_t symcount1
, symcount2
;
7874 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
7875 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
7876 Elf_Internal_Sym
*isym
, *isymend
;
7877 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
7878 size_t count1
, count2
, i
;
7879 unsigned int shndx1
, shndx2
;
7885 /* Both sections have to be in ELF. */
7886 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
7887 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
7890 if (elf_section_type (sec1
) != elf_section_type (sec2
))
7893 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
7894 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
7895 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
7898 bed1
= get_elf_backend_data (bfd1
);
7899 bed2
= get_elf_backend_data (bfd2
);
7900 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
7901 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
7902 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
7903 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
7905 if (symcount1
== 0 || symcount2
== 0)
7911 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
7912 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
7914 if (ssymbuf1
== NULL
)
7916 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
7918 if (isymbuf1
== NULL
)
7921 if (!info
->reduce_memory_overheads
)
7922 elf_tdata (bfd1
)->symbuf
= ssymbuf1
7923 = elf_create_symbuf (symcount1
, isymbuf1
);
7926 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
7928 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
7930 if (isymbuf2
== NULL
)
7933 if (ssymbuf1
!= NULL
&& !info
->reduce_memory_overheads
)
7934 elf_tdata (bfd2
)->symbuf
= ssymbuf2
7935 = elf_create_symbuf (symcount2
, isymbuf2
);
7938 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
7940 /* Optimized faster version. */
7942 struct elf_symbol
*symp
;
7943 struct elf_symbuf_symbol
*ssym
, *ssymend
;
7946 hi
= ssymbuf1
->count
;
7951 mid
= (lo
+ hi
) / 2;
7952 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
7954 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
7958 count1
= ssymbuf1
[mid
].count
;
7965 hi
= ssymbuf2
->count
;
7970 mid
= (lo
+ hi
) / 2;
7971 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
7973 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
7977 count2
= ssymbuf2
[mid
].count
;
7983 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7987 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
7989 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
7990 if (symtable1
== NULL
|| symtable2
== NULL
)
7994 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
7995 ssym
< ssymend
; ssym
++, symp
++)
7997 symp
->u
.ssym
= ssym
;
7998 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
8004 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
8005 ssym
< ssymend
; ssym
++, symp
++)
8007 symp
->u
.ssym
= ssym
;
8008 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
8013 /* Sort symbol by name. */
8014 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8015 elf_sym_name_compare
);
8016 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8017 elf_sym_name_compare
);
8019 for (i
= 0; i
< count1
; i
++)
8020 /* Two symbols must have the same binding, type and name. */
8021 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
8022 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
8023 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8030 symtable1
= (struct elf_symbol
*)
8031 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
8032 symtable2
= (struct elf_symbol
*)
8033 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
8034 if (symtable1
== NULL
|| symtable2
== NULL
)
8037 /* Count definitions in the section. */
8039 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
8040 if (isym
->st_shndx
== shndx1
)
8041 symtable1
[count1
++].u
.isym
= isym
;
8044 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
8045 if (isym
->st_shndx
== shndx2
)
8046 symtable2
[count2
++].u
.isym
= isym
;
8048 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8051 for (i
= 0; i
< count1
; i
++)
8053 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
8054 symtable1
[i
].u
.isym
->st_name
);
8056 for (i
= 0; i
< count2
; i
++)
8058 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
8059 symtable2
[i
].u
.isym
->st_name
);
8061 /* Sort symbol by name. */
8062 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8063 elf_sym_name_compare
);
8064 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8065 elf_sym_name_compare
);
8067 for (i
= 0; i
< count1
; i
++)
8068 /* Two symbols must have the same binding, type and name. */
8069 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
8070 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
8071 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8089 /* Return TRUE if 2 section types are compatible. */
8092 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
8093 bfd
*bbfd
, const asection
*bsec
)
8097 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
8098 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8101 return elf_section_type (asec
) == elf_section_type (bsec
);
8104 /* Final phase of ELF linker. */
8106 /* A structure we use to avoid passing large numbers of arguments. */
8108 struct elf_final_link_info
8110 /* General link information. */
8111 struct bfd_link_info
*info
;
8114 /* Symbol string table. */
8115 struct elf_strtab_hash
*symstrtab
;
8116 /* .hash section. */
8118 /* symbol version section (.gnu.version). */
8119 asection
*symver_sec
;
8120 /* Buffer large enough to hold contents of any section. */
8122 /* Buffer large enough to hold external relocs of any section. */
8123 void *external_relocs
;
8124 /* Buffer large enough to hold internal relocs of any section. */
8125 Elf_Internal_Rela
*internal_relocs
;
8126 /* Buffer large enough to hold external local symbols of any input
8128 bfd_byte
*external_syms
;
8129 /* And a buffer for symbol section indices. */
8130 Elf_External_Sym_Shndx
*locsym_shndx
;
8131 /* Buffer large enough to hold internal local symbols of any input
8133 Elf_Internal_Sym
*internal_syms
;
8134 /* Array large enough to hold a symbol index for each local symbol
8135 of any input BFD. */
8137 /* Array large enough to hold a section pointer for each local
8138 symbol of any input BFD. */
8139 asection
**sections
;
8140 /* Buffer for SHT_SYMTAB_SHNDX section. */
8141 Elf_External_Sym_Shndx
*symshndxbuf
;
8142 /* Number of STT_FILE syms seen. */
8143 size_t filesym_count
;
8146 /* This struct is used to pass information to elf_link_output_extsym. */
8148 struct elf_outext_info
8151 bfd_boolean localsyms
;
8152 bfd_boolean file_sym_done
;
8153 struct elf_final_link_info
*flinfo
;
8157 /* Support for evaluating a complex relocation.
8159 Complex relocations are generalized, self-describing relocations. The
8160 implementation of them consists of two parts: complex symbols, and the
8161 relocations themselves.
8163 The relocations are use a reserved elf-wide relocation type code (R_RELC
8164 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8165 information (start bit, end bit, word width, etc) into the addend. This
8166 information is extracted from CGEN-generated operand tables within gas.
8168 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8169 internal) representing prefix-notation expressions, including but not
8170 limited to those sorts of expressions normally encoded as addends in the
8171 addend field. The symbol mangling format is:
8174 | <unary-operator> ':' <node>
8175 | <binary-operator> ':' <node> ':' <node>
8178 <literal> := 's' <digits=N> ':' <N character symbol name>
8179 | 'S' <digits=N> ':' <N character section name>
8183 <binary-operator> := as in C
8184 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8187 set_symbol_value (bfd
*bfd_with_globals
,
8188 Elf_Internal_Sym
*isymbuf
,
8193 struct elf_link_hash_entry
**sym_hashes
;
8194 struct elf_link_hash_entry
*h
;
8195 size_t extsymoff
= locsymcount
;
8197 if (symidx
< locsymcount
)
8199 Elf_Internal_Sym
*sym
;
8201 sym
= isymbuf
+ symidx
;
8202 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
8204 /* It is a local symbol: move it to the
8205 "absolute" section and give it a value. */
8206 sym
->st_shndx
= SHN_ABS
;
8207 sym
->st_value
= val
;
8210 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
8214 /* It is a global symbol: set its link type
8215 to "defined" and give it a value. */
8217 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
8218 h
= sym_hashes
[symidx
- extsymoff
];
8219 while (h
->root
.type
== bfd_link_hash_indirect
8220 || h
->root
.type
== bfd_link_hash_warning
)
8221 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8222 h
->root
.type
= bfd_link_hash_defined
;
8223 h
->root
.u
.def
.value
= val
;
8224 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
8228 resolve_symbol (const char *name
,
8230 struct elf_final_link_info
*flinfo
,
8232 Elf_Internal_Sym
*isymbuf
,
8235 Elf_Internal_Sym
*sym
;
8236 struct bfd_link_hash_entry
*global_entry
;
8237 const char *candidate
= NULL
;
8238 Elf_Internal_Shdr
*symtab_hdr
;
8241 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
8243 for (i
= 0; i
< locsymcount
; ++ i
)
8247 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
8250 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
8251 symtab_hdr
->sh_link
,
8254 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8255 name
, candidate
, (unsigned long) sym
->st_value
);
8257 if (candidate
&& strcmp (candidate
, name
) == 0)
8259 asection
*sec
= flinfo
->sections
[i
];
8261 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
8262 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
8264 printf ("Found symbol with value %8.8lx\n",
8265 (unsigned long) *result
);
8271 /* Hmm, haven't found it yet. perhaps it is a global. */
8272 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
8273 FALSE
, FALSE
, TRUE
);
8277 if (global_entry
->type
== bfd_link_hash_defined
8278 || global_entry
->type
== bfd_link_hash_defweak
)
8280 *result
= (global_entry
->u
.def
.value
8281 + global_entry
->u
.def
.section
->output_section
->vma
8282 + global_entry
->u
.def
.section
->output_offset
);
8284 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8285 global_entry
->root
.string
, (unsigned long) *result
);
8293 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8294 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8295 names like "foo.end" which is the end address of section "foo". */
8298 resolve_section (const char *name
,
8306 for (curr
= sections
; curr
; curr
= curr
->next
)
8307 if (strcmp (curr
->name
, name
) == 0)
8309 *result
= curr
->vma
;
8313 /* Hmm. still haven't found it. try pseudo-section names. */
8314 /* FIXME: This could be coded more efficiently... */
8315 for (curr
= sections
; curr
; curr
= curr
->next
)
8317 len
= strlen (curr
->name
);
8318 if (len
> strlen (name
))
8321 if (strncmp (curr
->name
, name
, len
) == 0)
8323 if (strncmp (".end", name
+ len
, 4) == 0)
8325 *result
= curr
->vma
+ curr
->size
/ bfd_octets_per_byte (abfd
);
8329 /* Insert more pseudo-section names here, if you like. */
8337 undefined_reference (const char *reftype
, const char *name
)
8339 /* xgettext:c-format */
8340 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8345 eval_symbol (bfd_vma
*result
,
8348 struct elf_final_link_info
*flinfo
,
8350 Elf_Internal_Sym
*isymbuf
,
8359 const char *sym
= *symp
;
8361 bfd_boolean symbol_is_section
= FALSE
;
8366 if (len
< 1 || len
> sizeof (symbuf
))
8368 bfd_set_error (bfd_error_invalid_operation
);
8381 *result
= strtoul (sym
, (char **) symp
, 16);
8385 symbol_is_section
= TRUE
;
8389 symlen
= strtol (sym
, (char **) symp
, 10);
8390 sym
= *symp
+ 1; /* Skip the trailing ':'. */
8392 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
8394 bfd_set_error (bfd_error_invalid_operation
);
8398 memcpy (symbuf
, sym
, symlen
);
8399 symbuf
[symlen
] = '\0';
8400 *symp
= sym
+ symlen
;
8402 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8403 the symbol as a section, or vice-versa. so we're pretty liberal in our
8404 interpretation here; section means "try section first", not "must be a
8405 section", and likewise with symbol. */
8407 if (symbol_is_section
)
8409 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
8410 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8411 isymbuf
, locsymcount
))
8413 undefined_reference ("section", symbuf
);
8419 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8420 isymbuf
, locsymcount
)
8421 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8424 undefined_reference ("symbol", symbuf
);
8431 /* All that remains are operators. */
8433 #define UNARY_OP(op) \
8434 if (strncmp (sym, #op, strlen (#op)) == 0) \
8436 sym += strlen (#op); \
8440 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8441 isymbuf, locsymcount, signed_p)) \
8444 *result = op ((bfd_signed_vma) a); \
8450 #define BINARY_OP(op) \
8451 if (strncmp (sym, #op, strlen (#op)) == 0) \
8453 sym += strlen (#op); \
8457 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8458 isymbuf, locsymcount, signed_p)) \
8461 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8462 isymbuf, locsymcount, signed_p)) \
8465 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8495 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
8496 bfd_set_error (bfd_error_invalid_operation
);
8502 put_value (bfd_vma size
,
8503 unsigned long chunksz
,
8508 location
+= (size
- chunksz
);
8510 for (; size
; size
-= chunksz
, location
-= chunksz
)
8515 bfd_put_8 (input_bfd
, x
, location
);
8519 bfd_put_16 (input_bfd
, x
, location
);
8523 bfd_put_32 (input_bfd
, x
, location
);
8524 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8530 bfd_put_64 (input_bfd
, x
, location
);
8531 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8544 get_value (bfd_vma size
,
8545 unsigned long chunksz
,
8552 /* Sanity checks. */
8553 BFD_ASSERT (chunksz
<= sizeof (x
)
8556 && (size
% chunksz
) == 0
8557 && input_bfd
!= NULL
8558 && location
!= NULL
);
8560 if (chunksz
== sizeof (x
))
8562 BFD_ASSERT (size
== chunksz
);
8564 /* Make sure that we do not perform an undefined shift operation.
8565 We know that size == chunksz so there will only be one iteration
8566 of the loop below. */
8570 shift
= 8 * chunksz
;
8572 for (; size
; size
-= chunksz
, location
+= chunksz
)
8577 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
8580 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
8583 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
8587 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
8598 decode_complex_addend (unsigned long *start
, /* in bits */
8599 unsigned long *oplen
, /* in bits */
8600 unsigned long *len
, /* in bits */
8601 unsigned long *wordsz
, /* in bytes */
8602 unsigned long *chunksz
, /* in bytes */
8603 unsigned long *lsb0_p
,
8604 unsigned long *signed_p
,
8605 unsigned long *trunc_p
,
8606 unsigned long encoded
)
8608 * start
= encoded
& 0x3F;
8609 * len
= (encoded
>> 6) & 0x3F;
8610 * oplen
= (encoded
>> 12) & 0x3F;
8611 * wordsz
= (encoded
>> 18) & 0xF;
8612 * chunksz
= (encoded
>> 22) & 0xF;
8613 * lsb0_p
= (encoded
>> 27) & 1;
8614 * signed_p
= (encoded
>> 28) & 1;
8615 * trunc_p
= (encoded
>> 29) & 1;
8618 bfd_reloc_status_type
8619 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
8620 asection
*input_section ATTRIBUTE_UNUSED
,
8622 Elf_Internal_Rela
*rel
,
8625 bfd_vma shift
, x
, mask
;
8626 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
8627 bfd_reloc_status_type r
;
8629 /* Perform this reloc, since it is complex.
8630 (this is not to say that it necessarily refers to a complex
8631 symbol; merely that it is a self-describing CGEN based reloc.
8632 i.e. the addend has the complete reloc information (bit start, end,
8633 word size, etc) encoded within it.). */
8635 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
8636 &chunksz
, &lsb0_p
, &signed_p
,
8637 &trunc_p
, rel
->r_addend
);
8639 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
8642 shift
= (start
+ 1) - len
;
8644 shift
= (8 * wordsz
) - (start
+ len
);
8646 x
= get_value (wordsz
, chunksz
, input_bfd
,
8647 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8650 printf ("Doing complex reloc: "
8651 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8652 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8653 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8654 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
8655 oplen
, (unsigned long) x
, (unsigned long) mask
,
8656 (unsigned long) relocation
);
8661 /* Now do an overflow check. */
8662 r
= bfd_check_overflow ((signed_p
8663 ? complain_overflow_signed
8664 : complain_overflow_unsigned
),
8665 len
, 0, (8 * wordsz
),
8669 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
8672 printf (" relocation: %8.8lx\n"
8673 " shifted mask: %8.8lx\n"
8674 " shifted/masked reloc: %8.8lx\n"
8675 " result: %8.8lx\n",
8676 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
8677 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
8679 put_value (wordsz
, chunksz
, input_bfd
, x
,
8680 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8684 /* Functions to read r_offset from external (target order) reloc
8685 entry. Faster than bfd_getl32 et al, because we let the compiler
8686 know the value is aligned. */
8689 ext32l_r_offset (const void *p
)
8696 const union aligned32
*a
8697 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8699 uint32_t aval
= ( (uint32_t) a
->c
[0]
8700 | (uint32_t) a
->c
[1] << 8
8701 | (uint32_t) a
->c
[2] << 16
8702 | (uint32_t) a
->c
[3] << 24);
8707 ext32b_r_offset (const void *p
)
8714 const union aligned32
*a
8715 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8717 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
8718 | (uint32_t) a
->c
[1] << 16
8719 | (uint32_t) a
->c
[2] << 8
8720 | (uint32_t) a
->c
[3]);
8724 #ifdef BFD_HOST_64_BIT
8726 ext64l_r_offset (const void *p
)
8733 const union aligned64
*a
8734 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8736 uint64_t aval
= ( (uint64_t) a
->c
[0]
8737 | (uint64_t) a
->c
[1] << 8
8738 | (uint64_t) a
->c
[2] << 16
8739 | (uint64_t) a
->c
[3] << 24
8740 | (uint64_t) a
->c
[4] << 32
8741 | (uint64_t) a
->c
[5] << 40
8742 | (uint64_t) a
->c
[6] << 48
8743 | (uint64_t) a
->c
[7] << 56);
8748 ext64b_r_offset (const void *p
)
8755 const union aligned64
*a
8756 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8758 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
8759 | (uint64_t) a
->c
[1] << 48
8760 | (uint64_t) a
->c
[2] << 40
8761 | (uint64_t) a
->c
[3] << 32
8762 | (uint64_t) a
->c
[4] << 24
8763 | (uint64_t) a
->c
[5] << 16
8764 | (uint64_t) a
->c
[6] << 8
8765 | (uint64_t) a
->c
[7]);
8770 /* When performing a relocatable link, the input relocations are
8771 preserved. But, if they reference global symbols, the indices
8772 referenced must be updated. Update all the relocations found in
8776 elf_link_adjust_relocs (bfd
*abfd
,
8778 struct bfd_elf_section_reloc_data
*reldata
,
8780 struct bfd_link_info
*info
)
8783 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8785 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8786 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8787 bfd_vma r_type_mask
;
8789 unsigned int count
= reldata
->count
;
8790 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
8792 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
8794 swap_in
= bed
->s
->swap_reloc_in
;
8795 swap_out
= bed
->s
->swap_reloc_out
;
8797 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
8799 swap_in
= bed
->s
->swap_reloca_in
;
8800 swap_out
= bed
->s
->swap_reloca_out
;
8805 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
8808 if (bed
->s
->arch_size
== 32)
8815 r_type_mask
= 0xffffffff;
8819 erela
= reldata
->hdr
->contents
;
8820 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
8822 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
8825 if (*rel_hash
== NULL
)
8828 if ((*rel_hash
)->indx
== -2
8829 && info
->gc_sections
8830 && ! info
->gc_keep_exported
)
8832 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8833 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
8835 (*rel_hash
)->root
.root
.string
);
8836 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
8838 bfd_set_error (bfd_error_invalid_operation
);
8841 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
8843 (*swap_in
) (abfd
, erela
, irela
);
8844 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
8845 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
8846 | (irela
[j
].r_info
& r_type_mask
));
8847 (*swap_out
) (abfd
, irela
, erela
);
8850 if (bed
->elf_backend_update_relocs
)
8851 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
8853 if (sort
&& count
!= 0)
8855 bfd_vma (*ext_r_off
) (const void *);
8858 bfd_byte
*base
, *end
, *p
, *loc
;
8859 bfd_byte
*buf
= NULL
;
8861 if (bed
->s
->arch_size
== 32)
8863 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8864 ext_r_off
= ext32l_r_offset
;
8865 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8866 ext_r_off
= ext32b_r_offset
;
8872 #ifdef BFD_HOST_64_BIT
8873 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8874 ext_r_off
= ext64l_r_offset
;
8875 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8876 ext_r_off
= ext64b_r_offset
;
8882 /* Must use a stable sort here. A modified insertion sort,
8883 since the relocs are mostly sorted already. */
8884 elt_size
= reldata
->hdr
->sh_entsize
;
8885 base
= reldata
->hdr
->contents
;
8886 end
= base
+ count
* elt_size
;
8887 if (elt_size
> sizeof (Elf64_External_Rela
))
8890 /* Ensure the first element is lowest. This acts as a sentinel,
8891 speeding the main loop below. */
8892 r_off
= (*ext_r_off
) (base
);
8893 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
8895 bfd_vma r_off2
= (*ext_r_off
) (p
);
8904 /* Don't just swap *base and *loc as that changes the order
8905 of the original base[0] and base[1] if they happen to
8906 have the same r_offset. */
8907 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
8908 memcpy (onebuf
, loc
, elt_size
);
8909 memmove (base
+ elt_size
, base
, loc
- base
);
8910 memcpy (base
, onebuf
, elt_size
);
8913 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
8915 /* base to p is sorted, *p is next to insert. */
8916 r_off
= (*ext_r_off
) (p
);
8917 /* Search the sorted region for location to insert. */
8919 while (r_off
< (*ext_r_off
) (loc
))
8924 /* Chances are there is a run of relocs to insert here,
8925 from one of more input files. Files are not always
8926 linked in order due to the way elf_link_input_bfd is
8927 called. See pr17666. */
8928 size_t sortlen
= p
- loc
;
8929 bfd_vma r_off2
= (*ext_r_off
) (loc
);
8930 size_t runlen
= elt_size
;
8931 size_t buf_size
= 96 * 1024;
8932 while (p
+ runlen
< end
8933 && (sortlen
<= buf_size
8934 || runlen
+ elt_size
<= buf_size
)
8935 && r_off2
> (*ext_r_off
) (p
+ runlen
))
8939 buf
= bfd_malloc (buf_size
);
8943 if (runlen
< sortlen
)
8945 memcpy (buf
, p
, runlen
);
8946 memmove (loc
+ runlen
, loc
, sortlen
);
8947 memcpy (loc
, buf
, runlen
);
8951 memcpy (buf
, loc
, sortlen
);
8952 memmove (loc
, p
, runlen
);
8953 memcpy (loc
+ runlen
, buf
, sortlen
);
8955 p
+= runlen
- elt_size
;
8958 /* Hashes are no longer valid. */
8959 free (reldata
->hashes
);
8960 reldata
->hashes
= NULL
;
8966 struct elf_link_sort_rela
8972 enum elf_reloc_type_class type
;
8973 /* We use this as an array of size int_rels_per_ext_rel. */
8974 Elf_Internal_Rela rela
[1];
8978 elf_link_sort_cmp1 (const void *A
, const void *B
)
8980 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8981 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8982 int relativea
, relativeb
;
8984 relativea
= a
->type
== reloc_class_relative
;
8985 relativeb
= b
->type
== reloc_class_relative
;
8987 if (relativea
< relativeb
)
8989 if (relativea
> relativeb
)
8991 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
8993 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
8995 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8997 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9003 elf_link_sort_cmp2 (const void *A
, const void *B
)
9005 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9006 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9008 if (a
->type
< b
->type
)
9010 if (a
->type
> b
->type
)
9012 if (a
->u
.offset
< b
->u
.offset
)
9014 if (a
->u
.offset
> b
->u
.offset
)
9016 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9018 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9024 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
9026 asection
*dynamic_relocs
;
9029 bfd_size_type count
, size
;
9030 size_t i
, ret
, sort_elt
, ext_size
;
9031 bfd_byte
*sort
, *s_non_relative
, *p
;
9032 struct elf_link_sort_rela
*sq
;
9033 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9034 int i2e
= bed
->s
->int_rels_per_ext_rel
;
9035 unsigned int opb
= bfd_octets_per_byte (abfd
);
9036 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9037 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9038 struct bfd_link_order
*lo
;
9040 bfd_boolean use_rela
;
9042 /* Find a dynamic reloc section. */
9043 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
9044 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
9045 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
9046 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9048 bfd_boolean use_rela_initialised
= FALSE
;
9050 /* This is just here to stop gcc from complaining.
9051 Its initialization checking code is not perfect. */
9054 /* Both sections are present. Examine the sizes
9055 of the indirect sections to help us choose. */
9056 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9057 if (lo
->type
== bfd_indirect_link_order
)
9059 asection
*o
= lo
->u
.indirect
.section
;
9061 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9063 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9064 /* Section size is divisible by both rel and rela sizes.
9065 It is of no help to us. */
9069 /* Section size is only divisible by rela. */
9070 if (use_rela_initialised
&& !use_rela
)
9072 _bfd_error_handler (_("%pB: unable to sort relocs - "
9073 "they are in more than one size"),
9075 bfd_set_error (bfd_error_invalid_operation
);
9081 use_rela_initialised
= TRUE
;
9085 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9087 /* Section size is only divisible by rel. */
9088 if (use_rela_initialised
&& use_rela
)
9090 _bfd_error_handler (_("%pB: unable to sort relocs - "
9091 "they are in more than one size"),
9093 bfd_set_error (bfd_error_invalid_operation
);
9099 use_rela_initialised
= TRUE
;
9104 /* The section size is not divisible by either -
9105 something is wrong. */
9106 _bfd_error_handler (_("%pB: unable to sort relocs - "
9107 "they are of an unknown size"), abfd
);
9108 bfd_set_error (bfd_error_invalid_operation
);
9113 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9114 if (lo
->type
== bfd_indirect_link_order
)
9116 asection
*o
= lo
->u
.indirect
.section
;
9118 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9120 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9121 /* Section size is divisible by both rel and rela sizes.
9122 It is of no help to us. */
9126 /* Section size is only divisible by rela. */
9127 if (use_rela_initialised
&& !use_rela
)
9129 _bfd_error_handler (_("%pB: unable to sort relocs - "
9130 "they are in more than one size"),
9132 bfd_set_error (bfd_error_invalid_operation
);
9138 use_rela_initialised
= TRUE
;
9142 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9144 /* Section size is only divisible by rel. */
9145 if (use_rela_initialised
&& use_rela
)
9147 _bfd_error_handler (_("%pB: unable to sort relocs - "
9148 "they are in more than one size"),
9150 bfd_set_error (bfd_error_invalid_operation
);
9156 use_rela_initialised
= TRUE
;
9161 /* The section size is not divisible by either -
9162 something is wrong. */
9163 _bfd_error_handler (_("%pB: unable to sort relocs - "
9164 "they are of an unknown size"), abfd
);
9165 bfd_set_error (bfd_error_invalid_operation
);
9170 if (! use_rela_initialised
)
9174 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
9176 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9183 dynamic_relocs
= rela_dyn
;
9184 ext_size
= bed
->s
->sizeof_rela
;
9185 swap_in
= bed
->s
->swap_reloca_in
;
9186 swap_out
= bed
->s
->swap_reloca_out
;
9190 dynamic_relocs
= rel_dyn
;
9191 ext_size
= bed
->s
->sizeof_rel
;
9192 swap_in
= bed
->s
->swap_reloc_in
;
9193 swap_out
= bed
->s
->swap_reloc_out
;
9197 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9198 if (lo
->type
== bfd_indirect_link_order
)
9199 size
+= lo
->u
.indirect
.section
->size
;
9201 if (size
!= dynamic_relocs
->size
)
9204 sort_elt
= (sizeof (struct elf_link_sort_rela
)
9205 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
9207 count
= dynamic_relocs
->size
/ ext_size
;
9210 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
9214 (*info
->callbacks
->warning
)
9215 (info
, _("not enough memory to sort relocations"), 0, abfd
, 0, 0);
9219 if (bed
->s
->arch_size
== 32)
9220 r_sym_mask
= ~(bfd_vma
) 0xff;
9222 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
9224 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9225 if (lo
->type
== bfd_indirect_link_order
)
9227 bfd_byte
*erel
, *erelend
;
9228 asection
*o
= lo
->u
.indirect
.section
;
9230 if (o
->contents
== NULL
&& o
->size
!= 0)
9232 /* This is a reloc section that is being handled as a normal
9233 section. See bfd_section_from_shdr. We can't combine
9234 relocs in this case. */
9239 erelend
= o
->contents
+ o
->size
;
9240 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
9242 while (erel
< erelend
)
9244 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9246 (*swap_in
) (abfd
, erel
, s
->rela
);
9247 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
9248 s
->u
.sym_mask
= r_sym_mask
;
9254 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
9256 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
9258 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9259 if (s
->type
!= reloc_class_relative
)
9265 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
9266 for (; i
< count
; i
++, p
+= sort_elt
)
9268 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
9269 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
9271 sp
->u
.offset
= sq
->rela
->r_offset
;
9274 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
9276 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
9277 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
9279 /* We have plt relocs in .rela.dyn. */
9280 sq
= (struct elf_link_sort_rela
*) sort
;
9281 for (i
= 0; i
< count
; i
++)
9282 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
9284 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
9286 struct bfd_link_order
**plo
;
9287 /* Put srelplt link_order last. This is so the output_offset
9288 set in the next loop is correct for DT_JMPREL. */
9289 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
9290 if ((*plo
)->type
== bfd_indirect_link_order
9291 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
9297 plo
= &(*plo
)->next
;
9300 dynamic_relocs
->map_tail
.link_order
= lo
;
9305 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9306 if (lo
->type
== bfd_indirect_link_order
)
9308 bfd_byte
*erel
, *erelend
;
9309 asection
*o
= lo
->u
.indirect
.section
;
9312 erelend
= o
->contents
+ o
->size
;
9313 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
9314 while (erel
< erelend
)
9316 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9317 (*swap_out
) (abfd
, s
->rela
, erel
);
9324 *psec
= dynamic_relocs
;
9328 /* Add a symbol to the output symbol string table. */
9331 elf_link_output_symstrtab (struct elf_final_link_info
*flinfo
,
9333 Elf_Internal_Sym
*elfsym
,
9334 asection
*input_sec
,
9335 struct elf_link_hash_entry
*h
)
9337 int (*output_symbol_hook
)
9338 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
9339 struct elf_link_hash_entry
*);
9340 struct elf_link_hash_table
*hash_table
;
9341 const struct elf_backend_data
*bed
;
9342 bfd_size_type strtabsize
;
9344 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9346 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9347 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
9348 if (output_symbol_hook
!= NULL
)
9350 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
9357 || (input_sec
->flags
& SEC_EXCLUDE
))
9358 elfsym
->st_name
= (unsigned long) -1;
9361 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9362 to get the final offset for st_name. */
9364 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
9366 if (elfsym
->st_name
== (unsigned long) -1)
9370 hash_table
= elf_hash_table (flinfo
->info
);
9371 strtabsize
= hash_table
->strtabsize
;
9372 if (strtabsize
<= hash_table
->strtabcount
)
9374 strtabsize
+= strtabsize
;
9375 hash_table
->strtabsize
= strtabsize
;
9376 strtabsize
*= sizeof (*hash_table
->strtab
);
9378 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
9380 if (hash_table
->strtab
== NULL
)
9383 hash_table
->strtab
[hash_table
->strtabcount
].sym
= *elfsym
;
9384 hash_table
->strtab
[hash_table
->strtabcount
].dest_index
9385 = hash_table
->strtabcount
;
9386 hash_table
->strtab
[hash_table
->strtabcount
].destshndx_index
9387 = flinfo
->symshndxbuf
? bfd_get_symcount (flinfo
->output_bfd
) : 0;
9389 bfd_get_symcount (flinfo
->output_bfd
) += 1;
9390 hash_table
->strtabcount
+= 1;
9395 /* Swap symbols out to the symbol table and flush the output symbols to
9399 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
9401 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
9404 const struct elf_backend_data
*bed
;
9406 Elf_Internal_Shdr
*hdr
;
9410 if (!hash_table
->strtabcount
)
9413 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9415 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9417 amt
= bed
->s
->sizeof_sym
* hash_table
->strtabcount
;
9418 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
9422 if (flinfo
->symshndxbuf
)
9424 amt
= sizeof (Elf_External_Sym_Shndx
);
9425 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
9426 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
9427 if (flinfo
->symshndxbuf
== NULL
)
9434 for (i
= 0; i
< hash_table
->strtabcount
; i
++)
9436 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
9437 if (elfsym
->sym
.st_name
== (unsigned long) -1)
9438 elfsym
->sym
.st_name
= 0;
9441 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
9442 elfsym
->sym
.st_name
);
9443 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
9444 ((bfd_byte
*) symbuf
9445 + (elfsym
->dest_index
9446 * bed
->s
->sizeof_sym
)),
9447 (flinfo
->symshndxbuf
9448 + elfsym
->destshndx_index
));
9451 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
9452 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
9453 amt
= hash_table
->strtabcount
* bed
->s
->sizeof_sym
;
9454 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
9455 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
9457 hdr
->sh_size
+= amt
;
9465 free (hash_table
->strtab
);
9466 hash_table
->strtab
= NULL
;
9471 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9474 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
9476 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
9477 && sym
->st_shndx
< SHN_LORESERVE
)
9479 /* The gABI doesn't support dynamic symbols in output sections
9482 /* xgettext:c-format */
9483 (_("%pB: too many sections: %d (>= %d)"),
9484 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
9485 bfd_set_error (bfd_error_nonrepresentable_section
);
9491 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9492 allowing an unsatisfied unversioned symbol in the DSO to match a
9493 versioned symbol that would normally require an explicit version.
9494 We also handle the case that a DSO references a hidden symbol
9495 which may be satisfied by a versioned symbol in another DSO. */
9498 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
9499 const struct elf_backend_data
*bed
,
9500 struct elf_link_hash_entry
*h
)
9503 struct elf_link_loaded_list
*loaded
;
9505 if (!is_elf_hash_table (info
->hash
))
9508 /* Check indirect symbol. */
9509 while (h
->root
.type
== bfd_link_hash_indirect
)
9510 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9512 switch (h
->root
.type
)
9518 case bfd_link_hash_undefined
:
9519 case bfd_link_hash_undefweak
:
9520 abfd
= h
->root
.u
.undef
.abfd
;
9522 || (abfd
->flags
& DYNAMIC
) == 0
9523 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
9527 case bfd_link_hash_defined
:
9528 case bfd_link_hash_defweak
:
9529 abfd
= h
->root
.u
.def
.section
->owner
;
9532 case bfd_link_hash_common
:
9533 abfd
= h
->root
.u
.c
.p
->section
->owner
;
9536 BFD_ASSERT (abfd
!= NULL
);
9538 for (loaded
= elf_hash_table (info
)->loaded
;
9540 loaded
= loaded
->next
)
9543 Elf_Internal_Shdr
*hdr
;
9547 Elf_Internal_Shdr
*versymhdr
;
9548 Elf_Internal_Sym
*isym
;
9549 Elf_Internal_Sym
*isymend
;
9550 Elf_Internal_Sym
*isymbuf
;
9551 Elf_External_Versym
*ever
;
9552 Elf_External_Versym
*extversym
;
9554 input
= loaded
->abfd
;
9556 /* We check each DSO for a possible hidden versioned definition. */
9558 || (input
->flags
& DYNAMIC
) == 0
9559 || elf_dynversym (input
) == 0)
9562 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
9564 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9565 if (elf_bad_symtab (input
))
9567 extsymcount
= symcount
;
9572 extsymcount
= symcount
- hdr
->sh_info
;
9573 extsymoff
= hdr
->sh_info
;
9576 if (extsymcount
== 0)
9579 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
9581 if (isymbuf
== NULL
)
9584 /* Read in any version definitions. */
9585 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
9586 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
9587 if (extversym
== NULL
)
9590 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
9591 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
9592 != versymhdr
->sh_size
))
9600 ever
= extversym
+ extsymoff
;
9601 isymend
= isymbuf
+ extsymcount
;
9602 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
9605 Elf_Internal_Versym iver
;
9606 unsigned short version_index
;
9608 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
9609 || isym
->st_shndx
== SHN_UNDEF
)
9612 name
= bfd_elf_string_from_elf_section (input
,
9615 if (strcmp (name
, h
->root
.root
.string
) != 0)
9618 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
9620 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
9622 && h
->forced_local
))
9624 /* If we have a non-hidden versioned sym, then it should
9625 have provided a definition for the undefined sym unless
9626 it is defined in a non-shared object and forced local.
9631 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
9632 if (version_index
== 1 || version_index
== 2)
9634 /* This is the base or first version. We can use it. */
9648 /* Convert ELF common symbol TYPE. */
9651 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
9653 /* Commom symbol can only appear in relocatable link. */
9654 if (!bfd_link_relocatable (info
))
9656 switch (info
->elf_stt_common
)
9660 case elf_stt_common
:
9663 case no_elf_stt_common
:
9670 /* Add an external symbol to the symbol table. This is called from
9671 the hash table traversal routine. When generating a shared object,
9672 we go through the symbol table twice. The first time we output
9673 anything that might have been forced to local scope in a version
9674 script. The second time we output the symbols that are still
9678 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
9680 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
9681 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
9682 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
9684 Elf_Internal_Sym sym
;
9685 asection
*input_sec
;
9686 const struct elf_backend_data
*bed
;
9691 if (h
->root
.type
== bfd_link_hash_warning
)
9693 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9694 if (h
->root
.type
== bfd_link_hash_new
)
9698 /* Decide whether to output this symbol in this pass. */
9699 if (eoinfo
->localsyms
)
9701 if (!h
->forced_local
)
9706 if (h
->forced_local
)
9710 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9712 if (h
->root
.type
== bfd_link_hash_undefined
)
9714 /* If we have an undefined symbol reference here then it must have
9715 come from a shared library that is being linked in. (Undefined
9716 references in regular files have already been handled unless
9717 they are in unreferenced sections which are removed by garbage
9719 bfd_boolean ignore_undef
= FALSE
;
9721 /* Some symbols may be special in that the fact that they're
9722 undefined can be safely ignored - let backend determine that. */
9723 if (bed
->elf_backend_ignore_undef_symbol
)
9724 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
9726 /* If we are reporting errors for this situation then do so now. */
9729 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
9730 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
9731 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
9732 (*flinfo
->info
->callbacks
->undefined_symbol
)
9733 (flinfo
->info
, h
->root
.root
.string
,
9734 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
,
9736 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
);
9738 /* Strip a global symbol defined in a discarded section. */
9743 /* We should also warn if a forced local symbol is referenced from
9744 shared libraries. */
9745 if (bfd_link_executable (flinfo
->info
)
9750 && h
->ref_dynamic_nonweak
9751 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
9755 struct elf_link_hash_entry
*hi
= h
;
9757 /* Check indirect symbol. */
9758 while (hi
->root
.type
== bfd_link_hash_indirect
)
9759 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
9761 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
9762 /* xgettext:c-format */
9763 msg
= _("%pB: internal symbol `%s' in %pB is referenced by DSO");
9764 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
9765 /* xgettext:c-format */
9766 msg
= _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
9768 /* xgettext:c-format */
9769 msg
= _("%pB: local symbol `%s' in %pB is referenced by DSO");
9770 def_bfd
= flinfo
->output_bfd
;
9771 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
9772 def_bfd
= hi
->root
.u
.def
.section
->owner
;
9773 _bfd_error_handler (msg
, flinfo
->output_bfd
,
9774 h
->root
.root
.string
, def_bfd
);
9775 bfd_set_error (bfd_error_bad_value
);
9776 eoinfo
->failed
= TRUE
;
9780 /* We don't want to output symbols that have never been mentioned by
9781 a regular file, or that we have been told to strip. However, if
9782 h->indx is set to -2, the symbol is used by a reloc and we must
9787 else if ((h
->def_dynamic
9789 || h
->root
.type
== bfd_link_hash_new
)
9793 else if (flinfo
->info
->strip
== strip_all
)
9795 else if (flinfo
->info
->strip
== strip_some
9796 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
9797 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
9799 else if ((h
->root
.type
== bfd_link_hash_defined
9800 || h
->root
.type
== bfd_link_hash_defweak
)
9801 && ((flinfo
->info
->strip_discarded
9802 && discarded_section (h
->root
.u
.def
.section
))
9803 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
9804 && h
->root
.u
.def
.section
->owner
!= NULL
9805 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
9807 else if ((h
->root
.type
== bfd_link_hash_undefined
9808 || h
->root
.type
== bfd_link_hash_undefweak
)
9809 && h
->root
.u
.undef
.abfd
!= NULL
9810 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
9815 /* If we're stripping it, and it's not a dynamic symbol, there's
9816 nothing else to do. However, if it is a forced local symbol or
9817 an ifunc symbol we need to give the backend finish_dynamic_symbol
9818 function a chance to make it dynamic. */
9821 && type
!= STT_GNU_IFUNC
9822 && !h
->forced_local
)
9826 sym
.st_size
= h
->size
;
9827 sym
.st_other
= h
->other
;
9828 switch (h
->root
.type
)
9831 case bfd_link_hash_new
:
9832 case bfd_link_hash_warning
:
9836 case bfd_link_hash_undefined
:
9837 case bfd_link_hash_undefweak
:
9838 input_sec
= bfd_und_section_ptr
;
9839 sym
.st_shndx
= SHN_UNDEF
;
9842 case bfd_link_hash_defined
:
9843 case bfd_link_hash_defweak
:
9845 input_sec
= h
->root
.u
.def
.section
;
9846 if (input_sec
->output_section
!= NULL
)
9849 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
9850 input_sec
->output_section
);
9851 if (sym
.st_shndx
== SHN_BAD
)
9854 /* xgettext:c-format */
9855 (_("%pB: could not find output section %pA for input section %pA"),
9856 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
9857 bfd_set_error (bfd_error_nonrepresentable_section
);
9858 eoinfo
->failed
= TRUE
;
9862 /* ELF symbols in relocatable files are section relative,
9863 but in nonrelocatable files they are virtual
9865 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
9866 if (!bfd_link_relocatable (flinfo
->info
))
9868 sym
.st_value
+= input_sec
->output_section
->vma
;
9869 if (h
->type
== STT_TLS
)
9871 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
9872 if (tls_sec
!= NULL
)
9873 sym
.st_value
-= tls_sec
->vma
;
9879 BFD_ASSERT (input_sec
->owner
== NULL
9880 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
9881 sym
.st_shndx
= SHN_UNDEF
;
9882 input_sec
= bfd_und_section_ptr
;
9887 case bfd_link_hash_common
:
9888 input_sec
= h
->root
.u
.c
.p
->section
;
9889 sym
.st_shndx
= bed
->common_section_index (input_sec
);
9890 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
9893 case bfd_link_hash_indirect
:
9894 /* These symbols are created by symbol versioning. They point
9895 to the decorated version of the name. For example, if the
9896 symbol foo@@GNU_1.2 is the default, which should be used when
9897 foo is used with no version, then we add an indirect symbol
9898 foo which points to foo@@GNU_1.2. We ignore these symbols,
9899 since the indirected symbol is already in the hash table. */
9903 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
9904 switch (h
->root
.type
)
9906 case bfd_link_hash_common
:
9907 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9909 case bfd_link_hash_defined
:
9910 case bfd_link_hash_defweak
:
9911 if (bed
->common_definition (&sym
))
9912 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9916 case bfd_link_hash_undefined
:
9917 case bfd_link_hash_undefweak
:
9923 if (h
->forced_local
)
9925 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
9926 /* Turn off visibility on local symbol. */
9927 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
9929 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9930 else if (h
->unique_global
&& h
->def_regular
)
9931 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
9932 else if (h
->root
.type
== bfd_link_hash_undefweak
9933 || h
->root
.type
== bfd_link_hash_defweak
)
9934 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
9936 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
9937 sym
.st_target_internal
= h
->target_internal
;
9939 /* Give the processor backend a chance to tweak the symbol value,
9940 and also to finish up anything that needs to be done for this
9941 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9942 forced local syms when non-shared is due to a historical quirk.
9943 STT_GNU_IFUNC symbol must go through PLT. */
9944 if ((h
->type
== STT_GNU_IFUNC
9946 && !bfd_link_relocatable (flinfo
->info
))
9947 || ((h
->dynindx
!= -1
9949 && ((bfd_link_pic (flinfo
->info
)
9950 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
9951 || h
->root
.type
!= bfd_link_hash_undefweak
))
9952 || !h
->forced_local
)
9953 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
9955 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
9956 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
9958 eoinfo
->failed
= TRUE
;
9963 /* If we are marking the symbol as undefined, and there are no
9964 non-weak references to this symbol from a regular object, then
9965 mark the symbol as weak undefined; if there are non-weak
9966 references, mark the symbol as strong. We can't do this earlier,
9967 because it might not be marked as undefined until the
9968 finish_dynamic_symbol routine gets through with it. */
9969 if (sym
.st_shndx
== SHN_UNDEF
9971 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
9972 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
9975 type
= ELF_ST_TYPE (sym
.st_info
);
9977 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9978 if (type
== STT_GNU_IFUNC
)
9981 if (h
->ref_regular_nonweak
)
9982 bindtype
= STB_GLOBAL
;
9984 bindtype
= STB_WEAK
;
9985 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
9988 /* If this is a symbol defined in a dynamic library, don't use the
9989 symbol size from the dynamic library. Relinking an executable
9990 against a new library may introduce gratuitous changes in the
9991 executable's symbols if we keep the size. */
9992 if (sym
.st_shndx
== SHN_UNDEF
9997 /* If a non-weak symbol with non-default visibility is not defined
9998 locally, it is a fatal error. */
9999 if (!bfd_link_relocatable (flinfo
->info
)
10000 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
10001 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
10002 && h
->root
.type
== bfd_link_hash_undefined
10003 && !h
->def_regular
)
10007 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
10008 /* xgettext:c-format */
10009 msg
= _("%pB: protected symbol `%s' isn't defined");
10010 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
10011 /* xgettext:c-format */
10012 msg
= _("%pB: internal symbol `%s' isn't defined");
10014 /* xgettext:c-format */
10015 msg
= _("%pB: hidden symbol `%s' isn't defined");
10016 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
10017 bfd_set_error (bfd_error_bad_value
);
10018 eoinfo
->failed
= TRUE
;
10022 /* If this symbol should be put in the .dynsym section, then put it
10023 there now. We already know the symbol index. We also fill in
10024 the entry in the .hash section. */
10025 if (elf_hash_table (flinfo
->info
)->dynsym
!= NULL
10026 && h
->dynindx
!= -1
10027 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
)
10031 /* Since there is no version information in the dynamic string,
10032 if there is no version info in symbol version section, we will
10033 have a run-time problem if not linking executable, referenced
10034 by shared library, or not bound locally. */
10035 if (h
->verinfo
.verdef
== NULL
10036 && (!bfd_link_executable (flinfo
->info
)
10038 || !h
->def_regular
))
10040 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
10042 if (p
&& p
[1] != '\0')
10045 /* xgettext:c-format */
10046 (_("%pB: no symbol version section for versioned symbol `%s'"),
10047 flinfo
->output_bfd
, h
->root
.root
.string
);
10048 eoinfo
->failed
= TRUE
;
10053 sym
.st_name
= h
->dynstr_index
;
10054 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
10055 + h
->dynindx
* bed
->s
->sizeof_sym
);
10056 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
10058 eoinfo
->failed
= TRUE
;
10061 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
10063 if (flinfo
->hash_sec
!= NULL
)
10065 size_t hash_entry_size
;
10066 bfd_byte
*bucketpos
;
10068 size_t bucketcount
;
10071 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
10072 bucket
= h
->u
.elf_hash_value
% bucketcount
;
10075 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
10076 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
10077 + (bucket
+ 2) * hash_entry_size
);
10078 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
10079 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
10081 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
10082 ((bfd_byte
*) flinfo
->hash_sec
->contents
10083 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
10086 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
10088 Elf_Internal_Versym iversym
;
10089 Elf_External_Versym
*eversym
;
10091 if (!h
->def_regular
)
10093 if (h
->verinfo
.verdef
== NULL
10094 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
10095 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
10096 iversym
.vs_vers
= 0;
10098 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
10102 if (h
->verinfo
.vertree
== NULL
)
10103 iversym
.vs_vers
= 1;
10105 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
10106 if (flinfo
->info
->create_default_symver
)
10110 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10111 defined locally. */
10112 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
10113 iversym
.vs_vers
|= VERSYM_HIDDEN
;
10115 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
10116 eversym
+= h
->dynindx
;
10117 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
10121 /* If the symbol is undefined, and we didn't output it to .dynsym,
10122 strip it from .symtab too. Obviously we can't do this for
10123 relocatable output or when needed for --emit-relocs. */
10124 else if (input_sec
== bfd_und_section_ptr
10126 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10127 && (h
->mark
!= 1 || ELF_ST_BIND (sym
.st_info
) != STB_GLOBAL
)
10128 && !bfd_link_relocatable (flinfo
->info
))
10131 /* Also strip others that we couldn't earlier due to dynamic symbol
10135 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
10138 /* Output a FILE symbol so that following locals are not associated
10139 with the wrong input file. We need one for forced local symbols
10140 if we've seen more than one FILE symbol or when we have exactly
10141 one FILE symbol but global symbols are present in a file other
10142 than the one with the FILE symbol. We also need one if linker
10143 defined symbols are present. In practice these conditions are
10144 always met, so just emit the FILE symbol unconditionally. */
10145 if (eoinfo
->localsyms
10146 && !eoinfo
->file_sym_done
10147 && eoinfo
->flinfo
->filesym_count
!= 0)
10149 Elf_Internal_Sym fsym
;
10151 memset (&fsym
, 0, sizeof (fsym
));
10152 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10153 fsym
.st_shndx
= SHN_ABS
;
10154 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
10155 bfd_und_section_ptr
, NULL
))
10158 eoinfo
->file_sym_done
= TRUE
;
10161 indx
= bfd_get_symcount (flinfo
->output_bfd
);
10162 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
10166 eoinfo
->failed
= TRUE
;
10171 else if (h
->indx
== -2)
10177 /* Return TRUE if special handling is done for relocs in SEC against
10178 symbols defined in discarded sections. */
10181 elf_section_ignore_discarded_relocs (asection
*sec
)
10183 const struct elf_backend_data
*bed
;
10185 switch (sec
->sec_info_type
)
10187 case SEC_INFO_TYPE_STABS
:
10188 case SEC_INFO_TYPE_EH_FRAME
:
10189 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10195 bed
= get_elf_backend_data (sec
->owner
);
10196 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
10197 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
10203 /* Return a mask saying how ld should treat relocations in SEC against
10204 symbols defined in discarded sections. If this function returns
10205 COMPLAIN set, ld will issue a warning message. If this function
10206 returns PRETEND set, and the discarded section was link-once and the
10207 same size as the kept link-once section, ld will pretend that the
10208 symbol was actually defined in the kept section. Otherwise ld will
10209 zero the reloc (at least that is the intent, but some cooperation by
10210 the target dependent code is needed, particularly for REL targets). */
10213 _bfd_elf_default_action_discarded (asection
*sec
)
10215 if (sec
->flags
& SEC_DEBUGGING
)
10218 if (strcmp (".eh_frame", sec
->name
) == 0)
10221 if (strcmp (".gcc_except_table", sec
->name
) == 0)
10224 return COMPLAIN
| PRETEND
;
10227 /* Find a match between a section and a member of a section group. */
10230 match_group_member (asection
*sec
, asection
*group
,
10231 struct bfd_link_info
*info
)
10233 asection
*first
= elf_next_in_group (group
);
10234 asection
*s
= first
;
10238 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
10241 s
= elf_next_in_group (s
);
10249 /* Check if the kept section of a discarded section SEC can be used
10250 to replace it. Return the replacement if it is OK. Otherwise return
10254 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
10258 kept
= sec
->kept_section
;
10261 if ((kept
->flags
& SEC_GROUP
) != 0)
10262 kept
= match_group_member (sec
, kept
, info
);
10264 && ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
10265 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
)))
10267 sec
->kept_section
= kept
;
10272 /* Link an input file into the linker output file. This function
10273 handles all the sections and relocations of the input file at once.
10274 This is so that we only have to read the local symbols once, and
10275 don't have to keep them in memory. */
10278 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
10280 int (*relocate_section
)
10281 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
10282 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
10284 Elf_Internal_Shdr
*symtab_hdr
;
10285 size_t locsymcount
;
10287 Elf_Internal_Sym
*isymbuf
;
10288 Elf_Internal_Sym
*isym
;
10289 Elf_Internal_Sym
*isymend
;
10291 asection
**ppsection
;
10293 const struct elf_backend_data
*bed
;
10294 struct elf_link_hash_entry
**sym_hashes
;
10295 bfd_size_type address_size
;
10296 bfd_vma r_type_mask
;
10298 bfd_boolean have_file_sym
= FALSE
;
10300 output_bfd
= flinfo
->output_bfd
;
10301 bed
= get_elf_backend_data (output_bfd
);
10302 relocate_section
= bed
->elf_backend_relocate_section
;
10304 /* If this is a dynamic object, we don't want to do anything here:
10305 we don't want the local symbols, and we don't want the section
10307 if ((input_bfd
->flags
& DYNAMIC
) != 0)
10310 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
10311 if (elf_bad_symtab (input_bfd
))
10313 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10318 locsymcount
= symtab_hdr
->sh_info
;
10319 extsymoff
= symtab_hdr
->sh_info
;
10322 /* Read the local symbols. */
10323 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10324 if (isymbuf
== NULL
&& locsymcount
!= 0)
10326 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
10327 flinfo
->internal_syms
,
10328 flinfo
->external_syms
,
10329 flinfo
->locsym_shndx
);
10330 if (isymbuf
== NULL
)
10334 /* Find local symbol sections and adjust values of symbols in
10335 SEC_MERGE sections. Write out those local symbols we know are
10336 going into the output file. */
10337 isymend
= isymbuf
+ locsymcount
;
10338 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
10340 isym
++, pindex
++, ppsection
++)
10344 Elf_Internal_Sym osym
;
10350 if (elf_bad_symtab (input_bfd
))
10352 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
10359 if (isym
->st_shndx
== SHN_UNDEF
)
10360 isec
= bfd_und_section_ptr
;
10361 else if (isym
->st_shndx
== SHN_ABS
)
10362 isec
= bfd_abs_section_ptr
;
10363 else if (isym
->st_shndx
== SHN_COMMON
)
10364 isec
= bfd_com_section_ptr
;
10367 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
10370 /* Don't attempt to output symbols with st_shnx in the
10371 reserved range other than SHN_ABS and SHN_COMMON. */
10375 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
10376 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
10378 _bfd_merged_section_offset (output_bfd
, &isec
,
10379 elf_section_data (isec
)->sec_info
,
10385 /* Don't output the first, undefined, symbol. In fact, don't
10386 output any undefined local symbol. */
10387 if (isec
== bfd_und_section_ptr
)
10390 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
10392 /* We never output section symbols. Instead, we use the
10393 section symbol of the corresponding section in the output
10398 /* If we are stripping all symbols, we don't want to output this
10400 if (flinfo
->info
->strip
== strip_all
)
10403 /* If we are discarding all local symbols, we don't want to
10404 output this one. If we are generating a relocatable output
10405 file, then some of the local symbols may be required by
10406 relocs; we output them below as we discover that they are
10408 if (flinfo
->info
->discard
== discard_all
)
10411 /* If this symbol is defined in a section which we are
10412 discarding, we don't need to keep it. */
10413 if (isym
->st_shndx
!= SHN_UNDEF
10414 && isym
->st_shndx
< SHN_LORESERVE
10415 && bfd_section_removed_from_list (output_bfd
,
10416 isec
->output_section
))
10419 /* Get the name of the symbol. */
10420 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
10425 /* See if we are discarding symbols with this name. */
10426 if ((flinfo
->info
->strip
== strip_some
10427 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
10429 || (((flinfo
->info
->discard
== discard_sec_merge
10430 && (isec
->flags
& SEC_MERGE
)
10431 && !bfd_link_relocatable (flinfo
->info
))
10432 || flinfo
->info
->discard
== discard_l
)
10433 && bfd_is_local_label_name (input_bfd
, name
)))
10436 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
10438 if (input_bfd
->lto_output
)
10439 /* -flto puts a temp file name here. This means builds
10440 are not reproducible. Discard the symbol. */
10442 have_file_sym
= TRUE
;
10443 flinfo
->filesym_count
+= 1;
10445 if (!have_file_sym
)
10447 /* In the absence of debug info, bfd_find_nearest_line uses
10448 FILE symbols to determine the source file for local
10449 function symbols. Provide a FILE symbol here if input
10450 files lack such, so that their symbols won't be
10451 associated with a previous input file. It's not the
10452 source file, but the best we can do. */
10453 have_file_sym
= TRUE
;
10454 flinfo
->filesym_count
+= 1;
10455 memset (&osym
, 0, sizeof (osym
));
10456 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10457 osym
.st_shndx
= SHN_ABS
;
10458 if (!elf_link_output_symstrtab (flinfo
,
10459 (input_bfd
->lto_output
? NULL
10460 : input_bfd
->filename
),
10461 &osym
, bfd_abs_section_ptr
,
10468 /* Adjust the section index for the output file. */
10469 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10470 isec
->output_section
);
10471 if (osym
.st_shndx
== SHN_BAD
)
10474 /* ELF symbols in relocatable files are section relative, but
10475 in executable files they are virtual addresses. Note that
10476 this code assumes that all ELF sections have an associated
10477 BFD section with a reasonable value for output_offset; below
10478 we assume that they also have a reasonable value for
10479 output_section. Any special sections must be set up to meet
10480 these requirements. */
10481 osym
.st_value
+= isec
->output_offset
;
10482 if (!bfd_link_relocatable (flinfo
->info
))
10484 osym
.st_value
+= isec
->output_section
->vma
;
10485 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
10487 /* STT_TLS symbols are relative to PT_TLS segment base. */
10488 BFD_ASSERT (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
);
10489 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
10493 indx
= bfd_get_symcount (output_bfd
);
10494 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
10501 if (bed
->s
->arch_size
== 32)
10503 r_type_mask
= 0xff;
10509 r_type_mask
= 0xffffffff;
10514 /* Relocate the contents of each section. */
10515 sym_hashes
= elf_sym_hashes (input_bfd
);
10516 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
10518 bfd_byte
*contents
;
10520 if (! o
->linker_mark
)
10522 /* This section was omitted from the link. */
10526 if (!flinfo
->info
->resolve_section_groups
10527 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
10529 /* Deal with the group signature symbol. */
10530 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
10531 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
10532 asection
*osec
= o
->output_section
;
10534 BFD_ASSERT (bfd_link_relocatable (flinfo
->info
));
10535 if (symndx
>= locsymcount
10536 || (elf_bad_symtab (input_bfd
)
10537 && flinfo
->sections
[symndx
] == NULL
))
10539 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
10540 while (h
->root
.type
== bfd_link_hash_indirect
10541 || h
->root
.type
== bfd_link_hash_warning
)
10542 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10543 /* Arrange for symbol to be output. */
10545 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
10547 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
10549 /* We'll use the output section target_index. */
10550 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10551 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
10555 if (flinfo
->indices
[symndx
] == -1)
10557 /* Otherwise output the local symbol now. */
10558 Elf_Internal_Sym sym
= isymbuf
[symndx
];
10559 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10564 name
= bfd_elf_string_from_elf_section (input_bfd
,
10565 symtab_hdr
->sh_link
,
10570 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10572 if (sym
.st_shndx
== SHN_BAD
)
10575 sym
.st_value
+= o
->output_offset
;
10577 indx
= bfd_get_symcount (output_bfd
);
10578 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
10583 flinfo
->indices
[symndx
] = indx
;
10587 elf_section_data (osec
)->this_hdr
.sh_info
10588 = flinfo
->indices
[symndx
];
10592 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
10593 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
10596 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
10598 /* Section was created by _bfd_elf_link_create_dynamic_sections
10603 /* Get the contents of the section. They have been cached by a
10604 relaxation routine. Note that o is a section in an input
10605 file, so the contents field will not have been set by any of
10606 the routines which work on output files. */
10607 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
10609 contents
= elf_section_data (o
)->this_hdr
.contents
;
10610 if (bed
->caches_rawsize
10612 && o
->rawsize
< o
->size
)
10614 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
10615 contents
= flinfo
->contents
;
10620 contents
= flinfo
->contents
;
10621 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
10625 if ((o
->flags
& SEC_RELOC
) != 0)
10627 Elf_Internal_Rela
*internal_relocs
;
10628 Elf_Internal_Rela
*rel
, *relend
;
10629 int action_discarded
;
10632 /* Get the swapped relocs. */
10634 = _bfd_elf_link_read_relocs (input_bfd
, o
, flinfo
->external_relocs
,
10635 flinfo
->internal_relocs
, FALSE
);
10636 if (internal_relocs
== NULL
10637 && o
->reloc_count
> 0)
10640 /* We need to reverse-copy input .ctors/.dtors sections if
10641 they are placed in .init_array/.finit_array for output. */
10642 if (o
->size
> address_size
10643 && ((strncmp (o
->name
, ".ctors", 6) == 0
10644 && strcmp (o
->output_section
->name
,
10645 ".init_array") == 0)
10646 || (strncmp (o
->name
, ".dtors", 6) == 0
10647 && strcmp (o
->output_section
->name
,
10648 ".fini_array") == 0))
10649 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
10651 if (o
->size
* bed
->s
->int_rels_per_ext_rel
10652 != o
->reloc_count
* address_size
)
10655 /* xgettext:c-format */
10656 (_("error: %pB: size of section %pA is not "
10657 "multiple of address size"),
10659 bfd_set_error (bfd_error_bad_value
);
10662 o
->flags
|= SEC_ELF_REVERSE_COPY
;
10665 action_discarded
= -1;
10666 if (!elf_section_ignore_discarded_relocs (o
))
10667 action_discarded
= (*bed
->action_discarded
) (o
);
10669 /* Run through the relocs evaluating complex reloc symbols and
10670 looking for relocs against symbols from discarded sections
10671 or section symbols from removed link-once sections.
10672 Complain about relocs against discarded sections. Zero
10673 relocs against removed link-once sections. */
10675 rel
= internal_relocs
;
10676 relend
= rel
+ o
->reloc_count
;
10677 for ( ; rel
< relend
; rel
++)
10679 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
10680 unsigned int s_type
;
10681 asection
**ps
, *sec
;
10682 struct elf_link_hash_entry
*h
= NULL
;
10683 const char *sym_name
;
10685 if (r_symndx
== STN_UNDEF
)
10688 if (r_symndx
>= locsymcount
10689 || (elf_bad_symtab (input_bfd
)
10690 && flinfo
->sections
[r_symndx
] == NULL
))
10692 h
= sym_hashes
[r_symndx
- extsymoff
];
10694 /* Badly formatted input files can contain relocs that
10695 reference non-existant symbols. Check here so that
10696 we do not seg fault. */
10700 /* xgettext:c-format */
10701 (_("error: %pB contains a reloc (%#" PRIx64
") for section %pA "
10702 "that references a non-existent global symbol"),
10703 input_bfd
, (uint64_t) rel
->r_info
, o
);
10704 bfd_set_error (bfd_error_bad_value
);
10708 while (h
->root
.type
== bfd_link_hash_indirect
10709 || h
->root
.type
== bfd_link_hash_warning
)
10710 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10714 /* If a plugin symbol is referenced from a non-IR file,
10715 mark the symbol as undefined. Note that the
10716 linker may attach linker created dynamic sections
10717 to the plugin bfd. Symbols defined in linker
10718 created sections are not plugin symbols. */
10719 if ((h
->root
.non_ir_ref_regular
10720 || h
->root
.non_ir_ref_dynamic
)
10721 && (h
->root
.type
== bfd_link_hash_defined
10722 || h
->root
.type
== bfd_link_hash_defweak
)
10723 && (h
->root
.u
.def
.section
->flags
10724 & SEC_LINKER_CREATED
) == 0
10725 && h
->root
.u
.def
.section
->owner
!= NULL
10726 && (h
->root
.u
.def
.section
->owner
->flags
10727 & BFD_PLUGIN
) != 0)
10729 h
->root
.type
= bfd_link_hash_undefined
;
10730 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
10734 if (h
->root
.type
== bfd_link_hash_defined
10735 || h
->root
.type
== bfd_link_hash_defweak
)
10736 ps
= &h
->root
.u
.def
.section
;
10738 sym_name
= h
->root
.root
.string
;
10742 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
10744 s_type
= ELF_ST_TYPE (sym
->st_info
);
10745 ps
= &flinfo
->sections
[r_symndx
];
10746 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
10750 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
10751 && !bfd_link_relocatable (flinfo
->info
))
10754 bfd_vma dot
= (rel
->r_offset
10755 + o
->output_offset
+ o
->output_section
->vma
);
10757 printf ("Encountered a complex symbol!");
10758 printf (" (input_bfd %s, section %s, reloc %ld\n",
10759 input_bfd
->filename
, o
->name
,
10760 (long) (rel
- internal_relocs
));
10761 printf (" symbol: idx %8.8lx, name %s\n",
10762 r_symndx
, sym_name
);
10763 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10764 (unsigned long) rel
->r_info
,
10765 (unsigned long) rel
->r_offset
);
10767 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
10768 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
10771 /* Symbol evaluated OK. Update to absolute value. */
10772 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
10777 if (action_discarded
!= -1 && ps
!= NULL
)
10779 /* Complain if the definition comes from a
10780 discarded section. */
10781 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
10783 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
10784 if (action_discarded
& COMPLAIN
)
10785 (*flinfo
->info
->callbacks
->einfo
)
10786 /* xgettext:c-format */
10787 (_("%X`%s' referenced in section `%pA' of %pB: "
10788 "defined in discarded section `%pA' of %pB\n"),
10789 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
10791 /* Try to do the best we can to support buggy old
10792 versions of gcc. Pretend that the symbol is
10793 really defined in the kept linkonce section.
10794 FIXME: This is quite broken. Modifying the
10795 symbol here means we will be changing all later
10796 uses of the symbol, not just in this section. */
10797 if (action_discarded
& PRETEND
)
10801 kept
= _bfd_elf_check_kept_section (sec
,
10813 /* Relocate the section by invoking a back end routine.
10815 The back end routine is responsible for adjusting the
10816 section contents as necessary, and (if using Rela relocs
10817 and generating a relocatable output file) adjusting the
10818 reloc addend as necessary.
10820 The back end routine does not have to worry about setting
10821 the reloc address or the reloc symbol index.
10823 The back end routine is given a pointer to the swapped in
10824 internal symbols, and can access the hash table entries
10825 for the external symbols via elf_sym_hashes (input_bfd).
10827 When generating relocatable output, the back end routine
10828 must handle STB_LOCAL/STT_SECTION symbols specially. The
10829 output symbol is going to be a section symbol
10830 corresponding to the output section, which will require
10831 the addend to be adjusted. */
10833 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
10834 input_bfd
, o
, contents
,
10842 || bfd_link_relocatable (flinfo
->info
)
10843 || flinfo
->info
->emitrelocations
)
10845 Elf_Internal_Rela
*irela
;
10846 Elf_Internal_Rela
*irelaend
, *irelamid
;
10847 bfd_vma last_offset
;
10848 struct elf_link_hash_entry
**rel_hash
;
10849 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
10850 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
10851 unsigned int next_erel
;
10852 bfd_boolean rela_normal
;
10853 struct bfd_elf_section_data
*esdi
, *esdo
;
10855 esdi
= elf_section_data (o
);
10856 esdo
= elf_section_data (o
->output_section
);
10857 rela_normal
= FALSE
;
10859 /* Adjust the reloc addresses and symbol indices. */
10861 irela
= internal_relocs
;
10862 irelaend
= irela
+ o
->reloc_count
;
10863 rel_hash
= esdo
->rel
.hashes
+ esdo
->rel
.count
;
10864 /* We start processing the REL relocs, if any. When we reach
10865 IRELAMID in the loop, we switch to the RELA relocs. */
10867 if (esdi
->rel
.hdr
!= NULL
)
10868 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
10869 * bed
->s
->int_rels_per_ext_rel
);
10870 rel_hash_list
= rel_hash
;
10871 rela_hash_list
= NULL
;
10872 last_offset
= o
->output_offset
;
10873 if (!bfd_link_relocatable (flinfo
->info
))
10874 last_offset
+= o
->output_section
->vma
;
10875 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
10877 unsigned long r_symndx
;
10879 Elf_Internal_Sym sym
;
10881 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
10887 if (irela
== irelamid
)
10889 rel_hash
= esdo
->rela
.hashes
+ esdo
->rela
.count
;
10890 rela_hash_list
= rel_hash
;
10891 rela_normal
= bed
->rela_normal
;
10894 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
10897 if (irela
->r_offset
>= (bfd_vma
) -2)
10899 /* This is a reloc for a deleted entry or somesuch.
10900 Turn it into an R_*_NONE reloc, at the same
10901 offset as the last reloc. elf_eh_frame.c and
10902 bfd_elf_discard_info rely on reloc offsets
10904 irela
->r_offset
= last_offset
;
10906 irela
->r_addend
= 0;
10910 irela
->r_offset
+= o
->output_offset
;
10912 /* Relocs in an executable have to be virtual addresses. */
10913 if (!bfd_link_relocatable (flinfo
->info
))
10914 irela
->r_offset
+= o
->output_section
->vma
;
10916 last_offset
= irela
->r_offset
;
10918 r_symndx
= irela
->r_info
>> r_sym_shift
;
10919 if (r_symndx
== STN_UNDEF
)
10922 if (r_symndx
>= locsymcount
10923 || (elf_bad_symtab (input_bfd
)
10924 && flinfo
->sections
[r_symndx
] == NULL
))
10926 struct elf_link_hash_entry
*rh
;
10927 unsigned long indx
;
10929 /* This is a reloc against a global symbol. We
10930 have not yet output all the local symbols, so
10931 we do not know the symbol index of any global
10932 symbol. We set the rel_hash entry for this
10933 reloc to point to the global hash table entry
10934 for this symbol. The symbol index is then
10935 set at the end of bfd_elf_final_link. */
10936 indx
= r_symndx
- extsymoff
;
10937 rh
= elf_sym_hashes (input_bfd
)[indx
];
10938 while (rh
->root
.type
== bfd_link_hash_indirect
10939 || rh
->root
.type
== bfd_link_hash_warning
)
10940 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
10942 /* Setting the index to -2 tells
10943 elf_link_output_extsym that this symbol is
10944 used by a reloc. */
10945 BFD_ASSERT (rh
->indx
< 0);
10952 /* This is a reloc against a local symbol. */
10955 sym
= isymbuf
[r_symndx
];
10956 sec
= flinfo
->sections
[r_symndx
];
10957 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
10959 /* I suppose the backend ought to fill in the
10960 section of any STT_SECTION symbol against a
10961 processor specific section. */
10962 r_symndx
= STN_UNDEF
;
10963 if (bfd_is_abs_section (sec
))
10965 else if (sec
== NULL
|| sec
->owner
== NULL
)
10967 bfd_set_error (bfd_error_bad_value
);
10972 asection
*osec
= sec
->output_section
;
10974 /* If we have discarded a section, the output
10975 section will be the absolute section. In
10976 case of discarded SEC_MERGE sections, use
10977 the kept section. relocate_section should
10978 have already handled discarded linkonce
10980 if (bfd_is_abs_section (osec
)
10981 && sec
->kept_section
!= NULL
10982 && sec
->kept_section
->output_section
!= NULL
)
10984 osec
= sec
->kept_section
->output_section
;
10985 irela
->r_addend
-= osec
->vma
;
10988 if (!bfd_is_abs_section (osec
))
10990 r_symndx
= osec
->target_index
;
10991 if (r_symndx
== STN_UNDEF
)
10993 irela
->r_addend
+= osec
->vma
;
10994 osec
= _bfd_nearby_section (output_bfd
, osec
,
10996 irela
->r_addend
-= osec
->vma
;
10997 r_symndx
= osec
->target_index
;
11002 /* Adjust the addend according to where the
11003 section winds up in the output section. */
11005 irela
->r_addend
+= sec
->output_offset
;
11009 if (flinfo
->indices
[r_symndx
] == -1)
11011 unsigned long shlink
;
11016 if (flinfo
->info
->strip
== strip_all
)
11018 /* You can't do ld -r -s. */
11019 bfd_set_error (bfd_error_invalid_operation
);
11023 /* This symbol was skipped earlier, but
11024 since it is needed by a reloc, we
11025 must output it now. */
11026 shlink
= symtab_hdr
->sh_link
;
11027 name
= (bfd_elf_string_from_elf_section
11028 (input_bfd
, shlink
, sym
.st_name
));
11032 osec
= sec
->output_section
;
11034 _bfd_elf_section_from_bfd_section (output_bfd
,
11036 if (sym
.st_shndx
== SHN_BAD
)
11039 sym
.st_value
+= sec
->output_offset
;
11040 if (!bfd_link_relocatable (flinfo
->info
))
11042 sym
.st_value
+= osec
->vma
;
11043 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
11045 /* STT_TLS symbols are relative to PT_TLS
11047 BFD_ASSERT (elf_hash_table (flinfo
->info
)
11048 ->tls_sec
!= NULL
);
11049 sym
.st_value
-= (elf_hash_table (flinfo
->info
)
11054 indx
= bfd_get_symcount (output_bfd
);
11055 ret
= elf_link_output_symstrtab (flinfo
, name
,
11061 flinfo
->indices
[r_symndx
] = indx
;
11066 r_symndx
= flinfo
->indices
[r_symndx
];
11069 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
11070 | (irela
->r_info
& r_type_mask
));
11073 /* Swap out the relocs. */
11074 input_rel_hdr
= esdi
->rel
.hdr
;
11075 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
11077 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11082 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
11083 * bed
->s
->int_rels_per_ext_rel
);
11084 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
11087 input_rela_hdr
= esdi
->rela
.hdr
;
11088 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
11090 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11099 /* Write out the modified section contents. */
11100 if (bed
->elf_backend_write_section
11101 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
11104 /* Section written out. */
11106 else switch (o
->sec_info_type
)
11108 case SEC_INFO_TYPE_STABS
:
11109 if (! (_bfd_write_section_stabs
11111 &elf_hash_table (flinfo
->info
)->stab_info
,
11112 o
, &elf_section_data (o
)->sec_info
, contents
)))
11115 case SEC_INFO_TYPE_MERGE
:
11116 if (! _bfd_write_merged_section (output_bfd
, o
,
11117 elf_section_data (o
)->sec_info
))
11120 case SEC_INFO_TYPE_EH_FRAME
:
11122 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
11127 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
11129 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
11137 if (! (o
->flags
& SEC_EXCLUDE
))
11139 file_ptr offset
= (file_ptr
) o
->output_offset
;
11140 bfd_size_type todo
= o
->size
;
11142 offset
*= bfd_octets_per_byte (output_bfd
);
11144 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
11146 /* Reverse-copy input section to output. */
11149 todo
-= address_size
;
11150 if (! bfd_set_section_contents (output_bfd
,
11158 offset
+= address_size
;
11162 else if (! bfd_set_section_contents (output_bfd
,
11176 /* Generate a reloc when linking an ELF file. This is a reloc
11177 requested by the linker, and does not come from any input file. This
11178 is used to build constructor and destructor tables when linking
11182 elf_reloc_link_order (bfd
*output_bfd
,
11183 struct bfd_link_info
*info
,
11184 asection
*output_section
,
11185 struct bfd_link_order
*link_order
)
11187 reloc_howto_type
*howto
;
11191 struct bfd_elf_section_reloc_data
*reldata
;
11192 struct elf_link_hash_entry
**rel_hash_ptr
;
11193 Elf_Internal_Shdr
*rel_hdr
;
11194 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
11195 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
11198 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
11200 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
11203 bfd_set_error (bfd_error_bad_value
);
11207 addend
= link_order
->u
.reloc
.p
->addend
;
11210 reldata
= &esdo
->rel
;
11211 else if (esdo
->rela
.hdr
)
11212 reldata
= &esdo
->rela
;
11219 /* Figure out the symbol index. */
11220 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
11221 if (link_order
->type
== bfd_section_reloc_link_order
)
11223 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
11224 BFD_ASSERT (indx
!= 0);
11225 *rel_hash_ptr
= NULL
;
11229 struct elf_link_hash_entry
*h
;
11231 /* Treat a reloc against a defined symbol as though it were
11232 actually against the section. */
11233 h
= ((struct elf_link_hash_entry
*)
11234 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
11235 link_order
->u
.reloc
.p
->u
.name
,
11236 FALSE
, FALSE
, TRUE
));
11238 && (h
->root
.type
== bfd_link_hash_defined
11239 || h
->root
.type
== bfd_link_hash_defweak
))
11243 section
= h
->root
.u
.def
.section
;
11244 indx
= section
->output_section
->target_index
;
11245 *rel_hash_ptr
= NULL
;
11246 /* It seems that we ought to add the symbol value to the
11247 addend here, but in practice it has already been added
11248 because it was passed to constructor_callback. */
11249 addend
+= section
->output_section
->vma
+ section
->output_offset
;
11251 else if (h
!= NULL
)
11253 /* Setting the index to -2 tells elf_link_output_extsym that
11254 this symbol is used by a reloc. */
11261 (*info
->callbacks
->unattached_reloc
)
11262 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
11267 /* If this is an inplace reloc, we must write the addend into the
11269 if (howto
->partial_inplace
&& addend
!= 0)
11271 bfd_size_type size
;
11272 bfd_reloc_status_type rstat
;
11275 const char *sym_name
;
11277 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
11278 buf
= (bfd_byte
*) bfd_zmalloc (size
);
11279 if (buf
== NULL
&& size
!= 0)
11281 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
11288 case bfd_reloc_outofrange
:
11291 case bfd_reloc_overflow
:
11292 if (link_order
->type
== bfd_section_reloc_link_order
)
11293 sym_name
= bfd_section_name (output_bfd
,
11294 link_order
->u
.reloc
.p
->u
.section
);
11296 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
11297 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
11298 howto
->name
, addend
, NULL
, NULL
,
11303 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
11305 * bfd_octets_per_byte (output_bfd
),
11312 /* The address of a reloc is relative to the section in a
11313 relocatable file, and is a virtual address in an executable
11315 offset
= link_order
->offset
;
11316 if (! bfd_link_relocatable (info
))
11317 offset
+= output_section
->vma
;
11319 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
11321 irel
[i
].r_offset
= offset
;
11322 irel
[i
].r_info
= 0;
11323 irel
[i
].r_addend
= 0;
11325 if (bed
->s
->arch_size
== 32)
11326 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
11328 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
11330 rel_hdr
= reldata
->hdr
;
11331 erel
= rel_hdr
->contents
;
11332 if (rel_hdr
->sh_type
== SHT_REL
)
11334 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
11335 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
11339 irel
[0].r_addend
= addend
;
11340 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
11341 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
11350 /* Get the output vma of the section pointed to by the sh_link field. */
11353 elf_get_linked_section_vma (struct bfd_link_order
*p
)
11355 Elf_Internal_Shdr
**elf_shdrp
;
11359 s
= p
->u
.indirect
.section
;
11360 elf_shdrp
= elf_elfsections (s
->owner
);
11361 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
11362 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
11364 The Intel C compiler generates SHT_IA_64_UNWIND with
11365 SHF_LINK_ORDER. But it doesn't set the sh_link or
11366 sh_info fields. Hence we could get the situation
11367 where elfsec is 0. */
11370 const struct elf_backend_data
*bed
11371 = get_elf_backend_data (s
->owner
);
11372 if (bed
->link_order_error_handler
)
11373 bed
->link_order_error_handler
11374 /* xgettext:c-format */
11375 (_("%pB: warning: sh_link not set for section `%pA'"), s
->owner
, s
);
11380 s
= elf_shdrp
[elfsec
]->bfd_section
;
11381 return s
->output_section
->vma
+ s
->output_offset
;
11386 /* Compare two sections based on the locations of the sections they are
11387 linked to. Used by elf_fixup_link_order. */
11390 compare_link_order (const void * a
, const void * b
)
11395 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
11396 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
11399 return apos
> bpos
;
11403 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11404 order as their linked sections. Returns false if this could not be done
11405 because an output section includes both ordered and unordered
11406 sections. Ideally we'd do this in the linker proper. */
11409 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
11411 int seen_linkorder
;
11414 struct bfd_link_order
*p
;
11416 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11418 struct bfd_link_order
**sections
;
11419 asection
*s
, *other_sec
, *linkorder_sec
;
11423 linkorder_sec
= NULL
;
11425 seen_linkorder
= 0;
11426 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11428 if (p
->type
== bfd_indirect_link_order
)
11430 s
= p
->u
.indirect
.section
;
11432 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
11433 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
11434 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
11435 && elfsec
< elf_numsections (sub
)
11436 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
11437 && elf_elfsections (sub
)[elfsec
]->sh_link
< elf_numsections (sub
))
11451 if (seen_other
&& seen_linkorder
)
11453 if (other_sec
&& linkorder_sec
)
11455 /* xgettext:c-format */
11456 (_("%pA has both ordered [`%pA' in %pB] "
11457 "and unordered [`%pA' in %pB] sections"),
11458 o
, linkorder_sec
, linkorder_sec
->owner
,
11459 other_sec
, other_sec
->owner
);
11462 (_("%pA has both ordered and unordered sections"), o
);
11463 bfd_set_error (bfd_error_bad_value
);
11468 if (!seen_linkorder
)
11471 sections
= (struct bfd_link_order
**)
11472 bfd_malloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
11473 if (sections
== NULL
)
11475 seen_linkorder
= 0;
11477 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11479 sections
[seen_linkorder
++] = p
;
11481 /* Sort the input sections in the order of their linked section. */
11482 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
11483 compare_link_order
);
11485 /* Change the offsets of the sections. */
11487 for (n
= 0; n
< seen_linkorder
; n
++)
11489 s
= sections
[n
]->u
.indirect
.section
;
11490 offset
&= ~(bfd_vma
) 0 << s
->alignment_power
;
11491 s
->output_offset
= offset
/ bfd_octets_per_byte (abfd
);
11492 sections
[n
]->offset
= offset
;
11493 offset
+= sections
[n
]->size
;
11500 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11501 Returns TRUE upon success, FALSE otherwise. */
11504 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
11506 bfd_boolean ret
= FALSE
;
11508 const struct elf_backend_data
*bed
;
11510 enum bfd_architecture arch
;
11512 asymbol
**sympp
= NULL
;
11516 elf_symbol_type
*osymbuf
;
11518 implib_bfd
= info
->out_implib_bfd
;
11519 bed
= get_elf_backend_data (abfd
);
11521 if (!bfd_set_format (implib_bfd
, bfd_object
))
11524 /* Use flag from executable but make it a relocatable object. */
11525 flags
= bfd_get_file_flags (abfd
);
11526 flags
&= ~HAS_RELOC
;
11527 if (!bfd_set_start_address (implib_bfd
, 0)
11528 || !bfd_set_file_flags (implib_bfd
, flags
& ~EXEC_P
))
11531 /* Copy architecture of output file to import library file. */
11532 arch
= bfd_get_arch (abfd
);
11533 mach
= bfd_get_mach (abfd
);
11534 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
11535 && (abfd
->target_defaulted
11536 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
11539 /* Get symbol table size. */
11540 symsize
= bfd_get_symtab_upper_bound (abfd
);
11544 /* Read in the symbol table. */
11545 sympp
= (asymbol
**) xmalloc (symsize
);
11546 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
11550 /* Allow the BFD backend to copy any private header data it
11551 understands from the output BFD to the import library BFD. */
11552 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
11555 /* Filter symbols to appear in the import library. */
11556 if (bed
->elf_backend_filter_implib_symbols
)
11557 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
11560 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
11563 bfd_set_error (bfd_error_no_symbols
);
11564 _bfd_error_handler (_("%pB: no symbol found for import library"),
11570 /* Make symbols absolute. */
11571 osymbuf
= (elf_symbol_type
*) bfd_alloc2 (implib_bfd
, symcount
,
11572 sizeof (*osymbuf
));
11573 for (src_count
= 0; src_count
< symcount
; src_count
++)
11575 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
11576 sizeof (*osymbuf
));
11577 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
11578 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
11579 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
11580 osymbuf
[src_count
].internal_elf_sym
.st_value
=
11581 osymbuf
[src_count
].symbol
.value
;
11582 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
11585 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
11587 /* Allow the BFD backend to copy any private data it understands
11588 from the output BFD to the import library BFD. This is done last
11589 to permit the routine to look at the filtered symbol table. */
11590 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
11593 if (!bfd_close (implib_bfd
))
11604 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
11608 if (flinfo
->symstrtab
!= NULL
)
11609 _bfd_elf_strtab_free (flinfo
->symstrtab
);
11610 if (flinfo
->contents
!= NULL
)
11611 free (flinfo
->contents
);
11612 if (flinfo
->external_relocs
!= NULL
)
11613 free (flinfo
->external_relocs
);
11614 if (flinfo
->internal_relocs
!= NULL
)
11615 free (flinfo
->internal_relocs
);
11616 if (flinfo
->external_syms
!= NULL
)
11617 free (flinfo
->external_syms
);
11618 if (flinfo
->locsym_shndx
!= NULL
)
11619 free (flinfo
->locsym_shndx
);
11620 if (flinfo
->internal_syms
!= NULL
)
11621 free (flinfo
->internal_syms
);
11622 if (flinfo
->indices
!= NULL
)
11623 free (flinfo
->indices
);
11624 if (flinfo
->sections
!= NULL
)
11625 free (flinfo
->sections
);
11626 if (flinfo
->symshndxbuf
!= NULL
)
11627 free (flinfo
->symshndxbuf
);
11628 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
11630 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11631 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rel
.hashes
!= NULL
)
11632 free (esdo
->rel
.hashes
);
11633 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rela
.hashes
!= NULL
)
11634 free (esdo
->rela
.hashes
);
11638 /* Do the final step of an ELF link. */
11641 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
11643 bfd_boolean dynamic
;
11644 bfd_boolean emit_relocs
;
11646 struct elf_final_link_info flinfo
;
11648 struct bfd_link_order
*p
;
11650 bfd_size_type max_contents_size
;
11651 bfd_size_type max_external_reloc_size
;
11652 bfd_size_type max_internal_reloc_count
;
11653 bfd_size_type max_sym_count
;
11654 bfd_size_type max_sym_shndx_count
;
11655 Elf_Internal_Sym elfsym
;
11657 Elf_Internal_Shdr
*symtab_hdr
;
11658 Elf_Internal_Shdr
*symtab_shndx_hdr
;
11659 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11660 struct elf_outext_info eoinfo
;
11661 bfd_boolean merged
;
11662 size_t relativecount
= 0;
11663 asection
*reldyn
= 0;
11665 asection
*attr_section
= NULL
;
11666 bfd_vma attr_size
= 0;
11667 const char *std_attrs_section
;
11668 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
11670 if (!is_elf_hash_table (htab
))
11673 if (bfd_link_pic (info
))
11674 abfd
->flags
|= DYNAMIC
;
11676 dynamic
= htab
->dynamic_sections_created
;
11677 dynobj
= htab
->dynobj
;
11679 emit_relocs
= (bfd_link_relocatable (info
)
11680 || info
->emitrelocations
);
11682 flinfo
.info
= info
;
11683 flinfo
.output_bfd
= abfd
;
11684 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
11685 if (flinfo
.symstrtab
== NULL
)
11690 flinfo
.hash_sec
= NULL
;
11691 flinfo
.symver_sec
= NULL
;
11695 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
11696 /* Note that dynsym_sec can be NULL (on VMS). */
11697 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
11698 /* Note that it is OK if symver_sec is NULL. */
11701 flinfo
.contents
= NULL
;
11702 flinfo
.external_relocs
= NULL
;
11703 flinfo
.internal_relocs
= NULL
;
11704 flinfo
.external_syms
= NULL
;
11705 flinfo
.locsym_shndx
= NULL
;
11706 flinfo
.internal_syms
= NULL
;
11707 flinfo
.indices
= NULL
;
11708 flinfo
.sections
= NULL
;
11709 flinfo
.symshndxbuf
= NULL
;
11710 flinfo
.filesym_count
= 0;
11712 /* The object attributes have been merged. Remove the input
11713 sections from the link, and set the contents of the output
11715 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
11716 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11718 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
11719 || strcmp (o
->name
, ".gnu.attributes") == 0)
11721 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11723 asection
*input_section
;
11725 if (p
->type
!= bfd_indirect_link_order
)
11727 input_section
= p
->u
.indirect
.section
;
11728 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11729 elf_link_input_bfd ignores this section. */
11730 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
11733 attr_size
= bfd_elf_obj_attr_size (abfd
);
11736 bfd_set_section_size (abfd
, o
, attr_size
);
11738 /* Skip this section later on. */
11739 o
->map_head
.link_order
= NULL
;
11742 o
->flags
|= SEC_EXCLUDE
;
11744 else if ((o
->flags
& SEC_GROUP
) != 0 && o
->size
== 0)
11746 /* Remove empty group section from linker output. */
11747 o
->flags
|= SEC_EXCLUDE
;
11748 bfd_section_list_remove (abfd
, o
);
11749 abfd
->section_count
--;
11753 /* Count up the number of relocations we will output for each output
11754 section, so that we know the sizes of the reloc sections. We
11755 also figure out some maximum sizes. */
11756 max_contents_size
= 0;
11757 max_external_reloc_size
= 0;
11758 max_internal_reloc_count
= 0;
11760 max_sym_shndx_count
= 0;
11762 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11764 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11765 o
->reloc_count
= 0;
11767 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11769 unsigned int reloc_count
= 0;
11770 unsigned int additional_reloc_count
= 0;
11771 struct bfd_elf_section_data
*esdi
= NULL
;
11773 if (p
->type
== bfd_section_reloc_link_order
11774 || p
->type
== bfd_symbol_reloc_link_order
)
11776 else if (p
->type
== bfd_indirect_link_order
)
11780 sec
= p
->u
.indirect
.section
;
11782 /* Mark all sections which are to be included in the
11783 link. This will normally be every section. We need
11784 to do this so that we can identify any sections which
11785 the linker has decided to not include. */
11786 sec
->linker_mark
= TRUE
;
11788 if (sec
->flags
& SEC_MERGE
)
11791 if (sec
->rawsize
> max_contents_size
)
11792 max_contents_size
= sec
->rawsize
;
11793 if (sec
->size
> max_contents_size
)
11794 max_contents_size
= sec
->size
;
11796 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
11797 && (sec
->owner
->flags
& DYNAMIC
) == 0)
11801 /* We are interested in just local symbols, not all
11803 if (elf_bad_symtab (sec
->owner
))
11804 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
11805 / bed
->s
->sizeof_sym
);
11807 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
11809 if (sym_count
> max_sym_count
)
11810 max_sym_count
= sym_count
;
11812 if (sym_count
> max_sym_shndx_count
11813 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
11814 max_sym_shndx_count
= sym_count
;
11816 if (esdo
->this_hdr
.sh_type
== SHT_REL
11817 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
11818 /* Some backends use reloc_count in relocation sections
11819 to count particular types of relocs. Of course,
11820 reloc sections themselves can't have relocations. */
11822 else if (emit_relocs
)
11824 reloc_count
= sec
->reloc_count
;
11825 if (bed
->elf_backend_count_additional_relocs
)
11828 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
11829 additional_reloc_count
+= c
;
11832 else if (bed
->elf_backend_count_relocs
)
11833 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
11835 esdi
= elf_section_data (sec
);
11837 if ((sec
->flags
& SEC_RELOC
) != 0)
11839 size_t ext_size
= 0;
11841 if (esdi
->rel
.hdr
!= NULL
)
11842 ext_size
= esdi
->rel
.hdr
->sh_size
;
11843 if (esdi
->rela
.hdr
!= NULL
)
11844 ext_size
+= esdi
->rela
.hdr
->sh_size
;
11846 if (ext_size
> max_external_reloc_size
)
11847 max_external_reloc_size
= ext_size
;
11848 if (sec
->reloc_count
> max_internal_reloc_count
)
11849 max_internal_reloc_count
= sec
->reloc_count
;
11854 if (reloc_count
== 0)
11857 reloc_count
+= additional_reloc_count
;
11858 o
->reloc_count
+= reloc_count
;
11860 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
11864 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
11865 esdo
->rel
.count
+= additional_reloc_count
;
11867 if (esdi
->rela
.hdr
)
11869 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
11870 esdo
->rela
.count
+= additional_reloc_count
;
11876 esdo
->rela
.count
+= reloc_count
;
11878 esdo
->rel
.count
+= reloc_count
;
11882 if (o
->reloc_count
> 0)
11883 o
->flags
|= SEC_RELOC
;
11886 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11887 set it (this is probably a bug) and if it is set
11888 assign_section_numbers will create a reloc section. */
11889 o
->flags
&=~ SEC_RELOC
;
11892 /* If the SEC_ALLOC flag is not set, force the section VMA to
11893 zero. This is done in elf_fake_sections as well, but forcing
11894 the VMA to 0 here will ensure that relocs against these
11895 sections are handled correctly. */
11896 if ((o
->flags
& SEC_ALLOC
) == 0
11897 && ! o
->user_set_vma
)
11901 if (! bfd_link_relocatable (info
) && merged
)
11902 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
11904 /* Figure out the file positions for everything but the symbol table
11905 and the relocs. We set symcount to force assign_section_numbers
11906 to create a symbol table. */
11907 bfd_get_symcount (abfd
) = info
->strip
!= strip_all
|| emit_relocs
;
11908 BFD_ASSERT (! abfd
->output_has_begun
);
11909 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
11912 /* Set sizes, and assign file positions for reloc sections. */
11913 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11915 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11916 if ((o
->flags
& SEC_RELOC
) != 0)
11919 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
11923 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
11927 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11928 to count upwards while actually outputting the relocations. */
11929 esdo
->rel
.count
= 0;
11930 esdo
->rela
.count
= 0;
11932 if (esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
11934 /* Cache the section contents so that they can be compressed
11935 later. Use bfd_malloc since it will be freed by
11936 bfd_compress_section_contents. */
11937 unsigned char *contents
= esdo
->this_hdr
.contents
;
11938 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
11941 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
11942 if (contents
== NULL
)
11944 esdo
->this_hdr
.contents
= contents
;
11948 /* We have now assigned file positions for all the sections except
11949 .symtab, .strtab, and non-loaded reloc sections. We start the
11950 .symtab section at the current file position, and write directly
11951 to it. We build the .strtab section in memory. */
11952 bfd_get_symcount (abfd
) = 0;
11953 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11954 /* sh_name is set in prep_headers. */
11955 symtab_hdr
->sh_type
= SHT_SYMTAB
;
11956 /* sh_flags, sh_addr and sh_size all start off zero. */
11957 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
11958 /* sh_link is set in assign_section_numbers. */
11959 /* sh_info is set below. */
11960 /* sh_offset is set just below. */
11961 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
11963 if (max_sym_count
< 20)
11964 max_sym_count
= 20;
11965 htab
->strtabsize
= max_sym_count
;
11966 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
11967 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
11968 if (htab
->strtab
== NULL
)
11970 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11972 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
11973 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
11975 if (info
->strip
!= strip_all
|| emit_relocs
)
11977 file_ptr off
= elf_next_file_pos (abfd
);
11979 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
11981 /* Note that at this point elf_next_file_pos (abfd) is
11982 incorrect. We do not yet know the size of the .symtab section.
11983 We correct next_file_pos below, after we do know the size. */
11985 /* Start writing out the symbol table. The first symbol is always a
11987 elfsym
.st_value
= 0;
11988 elfsym
.st_size
= 0;
11989 elfsym
.st_info
= 0;
11990 elfsym
.st_other
= 0;
11991 elfsym
.st_shndx
= SHN_UNDEF
;
11992 elfsym
.st_target_internal
= 0;
11993 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
11994 bfd_und_section_ptr
, NULL
) != 1)
11997 /* Output a symbol for each section. We output these even if we are
11998 discarding local symbols, since they are used for relocs. These
11999 symbols have no names. We store the index of each one in the
12000 index field of the section, so that we can find it again when
12001 outputting relocs. */
12003 elfsym
.st_size
= 0;
12004 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12005 elfsym
.st_other
= 0;
12006 elfsym
.st_value
= 0;
12007 elfsym
.st_target_internal
= 0;
12008 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12010 o
= bfd_section_from_elf_index (abfd
, i
);
12013 o
->target_index
= bfd_get_symcount (abfd
);
12014 elfsym
.st_shndx
= i
;
12015 if (!bfd_link_relocatable (info
))
12016 elfsym
.st_value
= o
->vma
;
12017 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
, o
,
12024 /* Allocate some memory to hold information read in from the input
12026 if (max_contents_size
!= 0)
12028 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
12029 if (flinfo
.contents
== NULL
)
12033 if (max_external_reloc_size
!= 0)
12035 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
12036 if (flinfo
.external_relocs
== NULL
)
12040 if (max_internal_reloc_count
!= 0)
12042 amt
= max_internal_reloc_count
* sizeof (Elf_Internal_Rela
);
12043 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
12044 if (flinfo
.internal_relocs
== NULL
)
12048 if (max_sym_count
!= 0)
12050 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
12051 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
12052 if (flinfo
.external_syms
== NULL
)
12055 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
12056 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
12057 if (flinfo
.internal_syms
== NULL
)
12060 amt
= max_sym_count
* sizeof (long);
12061 flinfo
.indices
= (long int *) bfd_malloc (amt
);
12062 if (flinfo
.indices
== NULL
)
12065 amt
= max_sym_count
* sizeof (asection
*);
12066 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
12067 if (flinfo
.sections
== NULL
)
12071 if (max_sym_shndx_count
!= 0)
12073 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
12074 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
12075 if (flinfo
.locsym_shndx
== NULL
)
12081 bfd_vma base
, end
= 0;
12084 for (sec
= htab
->tls_sec
;
12085 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
12088 bfd_size_type size
= sec
->size
;
12091 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
12093 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
12096 size
= ord
->offset
+ ord
->size
;
12098 end
= sec
->vma
+ size
;
12100 base
= htab
->tls_sec
->vma
;
12101 /* Only align end of TLS section if static TLS doesn't have special
12102 alignment requirements. */
12103 if (bed
->static_tls_alignment
== 1)
12104 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
12105 htab
->tls_size
= end
- base
;
12108 /* Reorder SHF_LINK_ORDER sections. */
12109 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12111 if (!elf_fixup_link_order (abfd
, o
))
12115 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
12118 /* Since ELF permits relocations to be against local symbols, we
12119 must have the local symbols available when we do the relocations.
12120 Since we would rather only read the local symbols once, and we
12121 would rather not keep them in memory, we handle all the
12122 relocations for a single input file at the same time.
12124 Unfortunately, there is no way to know the total number of local
12125 symbols until we have seen all of them, and the local symbol
12126 indices precede the global symbol indices. This means that when
12127 we are generating relocatable output, and we see a reloc against
12128 a global symbol, we can not know the symbol index until we have
12129 finished examining all the local symbols to see which ones we are
12130 going to output. To deal with this, we keep the relocations in
12131 memory, and don't output them until the end of the link. This is
12132 an unfortunate waste of memory, but I don't see a good way around
12133 it. Fortunately, it only happens when performing a relocatable
12134 link, which is not the common case. FIXME: If keep_memory is set
12135 we could write the relocs out and then read them again; I don't
12136 know how bad the memory loss will be. */
12138 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12139 sub
->output_has_begun
= FALSE
;
12140 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12142 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12144 if (p
->type
== bfd_indirect_link_order
12145 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
12146 == bfd_target_elf_flavour
)
12147 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
12149 if (! sub
->output_has_begun
)
12151 if (! elf_link_input_bfd (&flinfo
, sub
))
12153 sub
->output_has_begun
= TRUE
;
12156 else if (p
->type
== bfd_section_reloc_link_order
12157 || p
->type
== bfd_symbol_reloc_link_order
)
12159 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
12164 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
12166 if (p
->type
== bfd_indirect_link_order
12167 && (bfd_get_flavour (sub
)
12168 == bfd_target_elf_flavour
)
12169 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
12170 != bed
->s
->elfclass
))
12172 const char *iclass
, *oclass
;
12174 switch (bed
->s
->elfclass
)
12176 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
12177 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
12178 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
12182 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
12184 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
12185 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
12186 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
12190 bfd_set_error (bfd_error_wrong_format
);
12192 /* xgettext:c-format */
12193 (_("%pB: file class %s incompatible with %s"),
12194 sub
, iclass
, oclass
);
12203 /* Free symbol buffer if needed. */
12204 if (!info
->reduce_memory_overheads
)
12206 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12207 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
12208 && elf_tdata (sub
)->symbuf
)
12210 free (elf_tdata (sub
)->symbuf
);
12211 elf_tdata (sub
)->symbuf
= NULL
;
12215 /* Output any global symbols that got converted to local in a
12216 version script or due to symbol visibility. We do this in a
12217 separate step since ELF requires all local symbols to appear
12218 prior to any global symbols. FIXME: We should only do this if
12219 some global symbols were, in fact, converted to become local.
12220 FIXME: Will this work correctly with the Irix 5 linker? */
12221 eoinfo
.failed
= FALSE
;
12222 eoinfo
.flinfo
= &flinfo
;
12223 eoinfo
.localsyms
= TRUE
;
12224 eoinfo
.file_sym_done
= FALSE
;
12225 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12229 /* If backend needs to output some local symbols not present in the hash
12230 table, do it now. */
12231 if (bed
->elf_backend_output_arch_local_syms
12232 && (info
->strip
!= strip_all
|| emit_relocs
))
12234 typedef int (*out_sym_func
)
12235 (void *, const char *, Elf_Internal_Sym
*, asection
*,
12236 struct elf_link_hash_entry
*);
12238 if (! ((*bed
->elf_backend_output_arch_local_syms
)
12239 (abfd
, info
, &flinfo
,
12240 (out_sym_func
) elf_link_output_symstrtab
)))
12244 /* That wrote out all the local symbols. Finish up the symbol table
12245 with the global symbols. Even if we want to strip everything we
12246 can, we still need to deal with those global symbols that got
12247 converted to local in a version script. */
12249 /* The sh_info field records the index of the first non local symbol. */
12250 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12253 && htab
->dynsym
!= NULL
12254 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
12256 Elf_Internal_Sym sym
;
12257 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
12259 o
= htab
->dynsym
->output_section
;
12260 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
12262 /* Write out the section symbols for the output sections. */
12263 if (bfd_link_pic (info
)
12264 || htab
->is_relocatable_executable
)
12270 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12272 sym
.st_target_internal
= 0;
12274 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
12280 dynindx
= elf_section_data (s
)->dynindx
;
12283 indx
= elf_section_data (s
)->this_idx
;
12284 BFD_ASSERT (indx
> 0);
12285 sym
.st_shndx
= indx
;
12286 if (! check_dynsym (abfd
, &sym
))
12288 sym
.st_value
= s
->vma
;
12289 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
12290 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12294 /* Write out the local dynsyms. */
12295 if (htab
->dynlocal
)
12297 struct elf_link_local_dynamic_entry
*e
;
12298 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
12303 /* Copy the internal symbol and turn off visibility.
12304 Note that we saved a word of storage and overwrote
12305 the original st_name with the dynstr_index. */
12307 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
12309 s
= bfd_section_from_elf_index (e
->input_bfd
,
12314 elf_section_data (s
->output_section
)->this_idx
;
12315 if (! check_dynsym (abfd
, &sym
))
12317 sym
.st_value
= (s
->output_section
->vma
12319 + e
->isym
.st_value
);
12322 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
12323 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12328 /* We get the global symbols from the hash table. */
12329 eoinfo
.failed
= FALSE
;
12330 eoinfo
.localsyms
= FALSE
;
12331 eoinfo
.flinfo
= &flinfo
;
12332 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12336 /* If backend needs to output some symbols not present in the hash
12337 table, do it now. */
12338 if (bed
->elf_backend_output_arch_syms
12339 && (info
->strip
!= strip_all
|| emit_relocs
))
12341 typedef int (*out_sym_func
)
12342 (void *, const char *, Elf_Internal_Sym
*, asection
*,
12343 struct elf_link_hash_entry
*);
12345 if (! ((*bed
->elf_backend_output_arch_syms
)
12346 (abfd
, info
, &flinfo
,
12347 (out_sym_func
) elf_link_output_symstrtab
)))
12351 /* Finalize the .strtab section. */
12352 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
12354 /* Swap out the .strtab section. */
12355 if (!elf_link_swap_symbols_out (&flinfo
))
12358 /* Now we know the size of the symtab section. */
12359 if (bfd_get_symcount (abfd
) > 0)
12361 /* Finish up and write out the symbol string table (.strtab)
12363 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
12364 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
12366 if (elf_symtab_shndx_list (abfd
))
12368 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
12370 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
12372 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
12373 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
12374 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
12375 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
12376 symtab_shndx_hdr
->sh_size
= amt
;
12378 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
12381 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
12382 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
12387 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
12388 /* sh_name was set in prep_headers. */
12389 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
12390 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
12391 symstrtab_hdr
->sh_addr
= 0;
12392 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
12393 symstrtab_hdr
->sh_entsize
= 0;
12394 symstrtab_hdr
->sh_link
= 0;
12395 symstrtab_hdr
->sh_info
= 0;
12396 /* sh_offset is set just below. */
12397 symstrtab_hdr
->sh_addralign
= 1;
12399 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
12401 elf_next_file_pos (abfd
) = off
;
12403 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
12404 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
12408 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
12410 _bfd_error_handler (_("%pB: failed to generate import library"),
12411 info
->out_implib_bfd
);
12415 /* Adjust the relocs to have the correct symbol indices. */
12416 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12418 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12421 if ((o
->flags
& SEC_RELOC
) == 0)
12424 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
12425 if (esdo
->rel
.hdr
!= NULL
12426 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
, info
))
12428 if (esdo
->rela
.hdr
!= NULL
12429 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
, info
))
12432 /* Set the reloc_count field to 0 to prevent write_relocs from
12433 trying to swap the relocs out itself. */
12434 o
->reloc_count
= 0;
12437 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
12438 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
12440 /* If we are linking against a dynamic object, or generating a
12441 shared library, finish up the dynamic linking information. */
12444 bfd_byte
*dyncon
, *dynconend
;
12446 /* Fix up .dynamic entries. */
12447 o
= bfd_get_linker_section (dynobj
, ".dynamic");
12448 BFD_ASSERT (o
!= NULL
);
12450 dyncon
= o
->contents
;
12451 dynconend
= o
->contents
+ o
->size
;
12452 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12454 Elf_Internal_Dyn dyn
;
12457 bfd_size_type sh_size
;
12460 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12467 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
12469 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
12471 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
12472 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
12475 dyn
.d_un
.d_val
= relativecount
;
12482 name
= info
->init_function
;
12485 name
= info
->fini_function
;
12488 struct elf_link_hash_entry
*h
;
12490 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
12492 && (h
->root
.type
== bfd_link_hash_defined
12493 || h
->root
.type
== bfd_link_hash_defweak
))
12495 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
12496 o
= h
->root
.u
.def
.section
;
12497 if (o
->output_section
!= NULL
)
12498 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
12499 + o
->output_offset
);
12502 /* The symbol is imported from another shared
12503 library and does not apply to this one. */
12504 dyn
.d_un
.d_ptr
= 0;
12511 case DT_PREINIT_ARRAYSZ
:
12512 name
= ".preinit_array";
12514 case DT_INIT_ARRAYSZ
:
12515 name
= ".init_array";
12517 case DT_FINI_ARRAYSZ
:
12518 name
= ".fini_array";
12520 o
= bfd_get_section_by_name (abfd
, name
);
12524 (_("could not find section %s"), name
);
12529 (_("warning: %s section has zero size"), name
);
12530 dyn
.d_un
.d_val
= o
->size
;
12533 case DT_PREINIT_ARRAY
:
12534 name
= ".preinit_array";
12536 case DT_INIT_ARRAY
:
12537 name
= ".init_array";
12539 case DT_FINI_ARRAY
:
12540 name
= ".fini_array";
12542 o
= bfd_get_section_by_name (abfd
, name
);
12549 name
= ".gnu.hash";
12558 name
= ".gnu.version_d";
12561 name
= ".gnu.version_r";
12564 name
= ".gnu.version";
12566 o
= bfd_get_linker_section (dynobj
, name
);
12568 if (o
== NULL
|| bfd_is_abs_section (o
->output_section
))
12571 (_("could not find section %s"), name
);
12574 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
12577 (_("warning: section '%s' is being made into a note"), name
);
12578 bfd_set_error (bfd_error_nonrepresentable_section
);
12581 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
12588 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
12594 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12596 Elf_Internal_Shdr
*hdr
;
12598 hdr
= elf_elfsections (abfd
)[i
];
12599 if (hdr
->sh_type
== type
12600 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
12602 sh_size
+= hdr
->sh_size
;
12604 || sh_addr
> hdr
->sh_addr
)
12605 sh_addr
= hdr
->sh_addr
;
12609 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
12611 /* Don't count procedure linkage table relocs in the
12612 overall reloc count. */
12613 sh_size
-= htab
->srelplt
->size
;
12615 /* If the size is zero, make the address zero too.
12616 This is to avoid a glibc bug. If the backend
12617 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12618 zero, then we'll put DT_RELA at the end of
12619 DT_JMPREL. glibc will interpret the end of
12620 DT_RELA matching the end of DT_JMPREL as the
12621 case where DT_RELA includes DT_JMPREL, and for
12622 LD_BIND_NOW will decide that processing DT_RELA
12623 will process the PLT relocs too. Net result:
12624 No PLT relocs applied. */
12627 /* If .rela.plt is the first .rela section, exclude
12628 it from DT_RELA. */
12629 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
12630 + htab
->srelplt
->output_offset
))
12631 sh_addr
+= htab
->srelplt
->size
;
12634 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
12635 dyn
.d_un
.d_val
= sh_size
;
12637 dyn
.d_un
.d_ptr
= sh_addr
;
12640 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
12644 /* If we have created any dynamic sections, then output them. */
12645 if (dynobj
!= NULL
)
12647 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
12650 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12651 if (((info
->warn_shared_textrel
&& bfd_link_pic (info
))
12652 || info
->error_textrel
)
12653 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
)
12655 bfd_byte
*dyncon
, *dynconend
;
12657 dyncon
= o
->contents
;
12658 dynconend
= o
->contents
+ o
->size
;
12659 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12661 Elf_Internal_Dyn dyn
;
12663 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12665 if (dyn
.d_tag
== DT_TEXTREL
)
12667 if (info
->error_textrel
)
12668 info
->callbacks
->einfo
12669 (_("%P%X: read-only segment has dynamic relocations\n"));
12671 info
->callbacks
->einfo
12672 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
12678 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
12680 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
12682 || o
->output_section
== bfd_abs_section_ptr
)
12684 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
12686 /* At this point, we are only interested in sections
12687 created by _bfd_elf_link_create_dynamic_sections. */
12690 if (htab
->stab_info
.stabstr
== o
)
12692 if (htab
->eh_info
.hdr_sec
== o
)
12694 if (strcmp (o
->name
, ".dynstr") != 0)
12696 if (! bfd_set_section_contents (abfd
, o
->output_section
,
12698 (file_ptr
) o
->output_offset
12699 * bfd_octets_per_byte (abfd
),
12705 /* The contents of the .dynstr section are actually in a
12709 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
12710 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
12711 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
12717 if (!info
->resolve_section_groups
)
12719 bfd_boolean failed
= FALSE
;
12721 BFD_ASSERT (bfd_link_relocatable (info
));
12722 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
12727 /* If we have optimized stabs strings, output them. */
12728 if (htab
->stab_info
.stabstr
!= NULL
)
12730 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
12734 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
12737 elf_final_link_free (abfd
, &flinfo
);
12739 elf_linker (abfd
) = TRUE
;
12743 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
12744 if (contents
== NULL
)
12745 return FALSE
; /* Bail out and fail. */
12746 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
12747 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
12754 elf_final_link_free (abfd
, &flinfo
);
12758 /* Initialize COOKIE for input bfd ABFD. */
12761 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
12762 struct bfd_link_info
*info
, bfd
*abfd
)
12764 Elf_Internal_Shdr
*symtab_hdr
;
12765 const struct elf_backend_data
*bed
;
12767 bed
= get_elf_backend_data (abfd
);
12768 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12770 cookie
->abfd
= abfd
;
12771 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
12772 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
12773 if (cookie
->bad_symtab
)
12775 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
12776 cookie
->extsymoff
= 0;
12780 cookie
->locsymcount
= symtab_hdr
->sh_info
;
12781 cookie
->extsymoff
= symtab_hdr
->sh_info
;
12784 if (bed
->s
->arch_size
== 32)
12785 cookie
->r_sym_shift
= 8;
12787 cookie
->r_sym_shift
= 32;
12789 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
12790 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
12792 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
12793 cookie
->locsymcount
, 0,
12795 if (cookie
->locsyms
== NULL
)
12797 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
12800 if (info
->keep_memory
)
12801 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
12806 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12809 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
12811 Elf_Internal_Shdr
*symtab_hdr
;
12813 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12814 if (cookie
->locsyms
!= NULL
12815 && symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
12816 free (cookie
->locsyms
);
12819 /* Initialize the relocation information in COOKIE for input section SEC
12820 of input bfd ABFD. */
12823 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12824 struct bfd_link_info
*info
, bfd
*abfd
,
12827 if (sec
->reloc_count
== 0)
12829 cookie
->rels
= NULL
;
12830 cookie
->relend
= NULL
;
12834 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
12835 info
->keep_memory
);
12836 if (cookie
->rels
== NULL
)
12838 cookie
->rel
= cookie
->rels
;
12839 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
12841 cookie
->rel
= cookie
->rels
;
12845 /* Free the memory allocated by init_reloc_cookie_rels,
12849 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12852 if (cookie
->rels
&& elf_section_data (sec
)->relocs
!= cookie
->rels
)
12853 free (cookie
->rels
);
12856 /* Initialize the whole of COOKIE for input section SEC. */
12859 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12860 struct bfd_link_info
*info
,
12863 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
12865 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
12870 fini_reloc_cookie (cookie
, sec
->owner
);
12875 /* Free the memory allocated by init_reloc_cookie_for_section,
12879 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12882 fini_reloc_cookie_rels (cookie
, sec
);
12883 fini_reloc_cookie (cookie
, sec
->owner
);
12886 /* Garbage collect unused sections. */
12888 /* Default gc_mark_hook. */
12891 _bfd_elf_gc_mark_hook (asection
*sec
,
12892 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12893 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
12894 struct elf_link_hash_entry
*h
,
12895 Elf_Internal_Sym
*sym
)
12899 switch (h
->root
.type
)
12901 case bfd_link_hash_defined
:
12902 case bfd_link_hash_defweak
:
12903 return h
->root
.u
.def
.section
;
12905 case bfd_link_hash_common
:
12906 return h
->root
.u
.c
.p
->section
;
12913 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
12918 /* Return the debug definition section. */
12921 elf_gc_mark_debug_section (asection
*sec ATTRIBUTE_UNUSED
,
12922 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12923 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
12924 struct elf_link_hash_entry
*h
,
12925 Elf_Internal_Sym
*sym
)
12929 /* Return the global debug definition section. */
12930 if ((h
->root
.type
== bfd_link_hash_defined
12931 || h
->root
.type
== bfd_link_hash_defweak
)
12932 && (h
->root
.u
.def
.section
->flags
& SEC_DEBUGGING
) != 0)
12933 return h
->root
.u
.def
.section
;
12937 /* Return the local debug definition section. */
12938 asection
*isec
= bfd_section_from_elf_index (sec
->owner
,
12940 if ((isec
->flags
& SEC_DEBUGGING
) != 0)
12947 /* COOKIE->rel describes a relocation against section SEC, which is
12948 a section we've decided to keep. Return the section that contains
12949 the relocation symbol, or NULL if no section contains it. */
12952 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
12953 elf_gc_mark_hook_fn gc_mark_hook
,
12954 struct elf_reloc_cookie
*cookie
,
12955 bfd_boolean
*start_stop
)
12957 unsigned long r_symndx
;
12958 struct elf_link_hash_entry
*h
;
12960 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
12961 if (r_symndx
== STN_UNDEF
)
12964 if (r_symndx
>= cookie
->locsymcount
12965 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
12967 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
12970 info
->callbacks
->einfo (_("%F%P: corrupt input: %pB\n"),
12974 while (h
->root
.type
== bfd_link_hash_indirect
12975 || h
->root
.type
== bfd_link_hash_warning
)
12976 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
12978 /* If this symbol is weak and there is a non-weak definition, we
12979 keep the non-weak definition because many backends put
12980 dynamic reloc info on the non-weak definition for code
12981 handling copy relocs. */
12982 if (h
->is_weakalias
)
12983 weakdef (h
)->mark
= 1;
12985 if (start_stop
!= NULL
)
12987 /* To work around a glibc bug, mark XXX input sections
12988 when there is a reference to __start_XXX or __stop_XXX
12992 asection
*s
= h
->u2
.start_stop_section
;
12993 *start_stop
= !s
->gc_mark
;
12998 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
13001 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
13002 &cookie
->locsyms
[r_symndx
]);
13005 /* COOKIE->rel describes a relocation against section SEC, which is
13006 a section we've decided to keep. Mark the section that contains
13007 the relocation symbol. */
13010 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
13012 elf_gc_mark_hook_fn gc_mark_hook
,
13013 struct elf_reloc_cookie
*cookie
)
13016 bfd_boolean start_stop
= FALSE
;
13018 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
13019 while (rsec
!= NULL
)
13021 if (!rsec
->gc_mark
)
13023 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
13024 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
13026 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
13031 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
13036 /* The mark phase of garbage collection. For a given section, mark
13037 it and any sections in this section's group, and all the sections
13038 which define symbols to which it refers. */
13041 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
13043 elf_gc_mark_hook_fn gc_mark_hook
)
13046 asection
*group_sec
, *eh_frame
;
13050 /* Mark all the sections in the group. */
13051 group_sec
= elf_section_data (sec
)->next_in_group
;
13052 if (group_sec
&& !group_sec
->gc_mark
)
13053 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
13056 /* Look through the section relocs. */
13058 eh_frame
= elf_eh_frame_section (sec
->owner
);
13059 if ((sec
->flags
& SEC_RELOC
) != 0
13060 && sec
->reloc_count
> 0
13061 && sec
!= eh_frame
)
13063 struct elf_reloc_cookie cookie
;
13065 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
13069 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
13070 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
13075 fini_reloc_cookie_for_section (&cookie
, sec
);
13079 if (ret
&& eh_frame
&& elf_fde_list (sec
))
13081 struct elf_reloc_cookie cookie
;
13083 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
13087 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
13088 gc_mark_hook
, &cookie
))
13090 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
13094 eh_frame
= elf_section_eh_frame_entry (sec
);
13095 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
13096 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
13102 /* Scan and mark sections in a special or debug section group. */
13105 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
13107 /* Point to first section of section group. */
13109 /* Used to iterate the section group. */
13112 bfd_boolean is_special_grp
= TRUE
;
13113 bfd_boolean is_debug_grp
= TRUE
;
13115 /* First scan to see if group contains any section other than debug
13116 and special section. */
13117 ssec
= msec
= elf_next_in_group (grp
);
13120 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
13121 is_debug_grp
= FALSE
;
13123 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
13124 is_special_grp
= FALSE
;
13126 msec
= elf_next_in_group (msec
);
13128 while (msec
!= ssec
);
13130 /* If this is a pure debug section group or pure special section group,
13131 keep all sections in this group. */
13132 if (is_debug_grp
|| is_special_grp
)
13137 msec
= elf_next_in_group (msec
);
13139 while (msec
!= ssec
);
13143 /* Keep debug and special sections. */
13146 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
13147 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED
)
13151 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13154 bfd_boolean some_kept
;
13155 bfd_boolean debug_frag_seen
;
13156 bfd_boolean has_kept_debug_info
;
13158 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13160 isec
= ibfd
->sections
;
13161 if (isec
== NULL
|| isec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13164 /* Ensure all linker created sections are kept,
13165 see if any other section is already marked,
13166 and note if we have any fragmented debug sections. */
13167 debug_frag_seen
= some_kept
= has_kept_debug_info
= FALSE
;
13168 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13170 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
13172 else if (isec
->gc_mark
13173 && (isec
->flags
& SEC_ALLOC
) != 0
13174 && elf_section_type (isec
) != SHT_NOTE
)
13177 if (!debug_frag_seen
13178 && (isec
->flags
& SEC_DEBUGGING
)
13179 && CONST_STRNEQ (isec
->name
, ".debug_line."))
13180 debug_frag_seen
= TRUE
;
13183 /* If no non-note alloc section in this file will be kept, then
13184 we can toss out the debug and special sections. */
13188 /* Keep debug and special sections like .comment when they are
13189 not part of a group. Also keep section groups that contain
13190 just debug sections or special sections. */
13191 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13193 if ((isec
->flags
& SEC_GROUP
) != 0)
13194 _bfd_elf_gc_mark_debug_special_section_group (isec
);
13195 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
13196 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
13197 && elf_next_in_group (isec
) == NULL
)
13199 if (isec
->gc_mark
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
13200 has_kept_debug_info
= TRUE
;
13203 /* Look for CODE sections which are going to be discarded,
13204 and find and discard any fragmented debug sections which
13205 are associated with that code section. */
13206 if (debug_frag_seen
)
13207 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13208 if ((isec
->flags
& SEC_CODE
) != 0
13209 && isec
->gc_mark
== 0)
13214 ilen
= strlen (isec
->name
);
13216 /* Association is determined by the name of the debug
13217 section containing the name of the code section as
13218 a suffix. For example .debug_line.text.foo is a
13219 debug section associated with .text.foo. */
13220 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
13224 if (dsec
->gc_mark
== 0
13225 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
13228 dlen
= strlen (dsec
->name
);
13231 && strncmp (dsec
->name
+ (dlen
- ilen
),
13232 isec
->name
, ilen
) == 0)
13237 /* Mark debug sections referenced by kept debug sections. */
13238 if (has_kept_debug_info
)
13239 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13241 && (isec
->flags
& SEC_DEBUGGING
) != 0)
13242 if (!_bfd_elf_gc_mark (info
, isec
,
13243 elf_gc_mark_debug_section
))
13250 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
13253 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13255 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13259 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13260 || elf_object_id (sub
) != elf_hash_table_id (elf_hash_table (info
))
13261 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13264 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13267 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13269 /* When any section in a section group is kept, we keep all
13270 sections in the section group. If the first member of
13271 the section group is excluded, we will also exclude the
13273 if (o
->flags
& SEC_GROUP
)
13275 asection
*first
= elf_next_in_group (o
);
13276 o
->gc_mark
= first
->gc_mark
;
13282 /* Skip sweeping sections already excluded. */
13283 if (o
->flags
& SEC_EXCLUDE
)
13286 /* Since this is early in the link process, it is simple
13287 to remove a section from the output. */
13288 o
->flags
|= SEC_EXCLUDE
;
13290 if (info
->print_gc_sections
&& o
->size
!= 0)
13291 /* xgettext:c-format */
13292 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13300 /* Propagate collected vtable information. This is called through
13301 elf_link_hash_traverse. */
13304 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
13306 /* Those that are not vtables. */
13308 || h
->u2
.vtable
== NULL
13309 || h
->u2
.vtable
->parent
== NULL
)
13312 /* Those vtables that do not have parents, we cannot merge. */
13313 if (h
->u2
.vtable
->parent
== (struct elf_link_hash_entry
*) -1)
13316 /* If we've already been done, exit. */
13317 if (h
->u2
.vtable
->used
&& h
->u2
.vtable
->used
[-1])
13320 /* Make sure the parent's table is up to date. */
13321 elf_gc_propagate_vtable_entries_used (h
->u2
.vtable
->parent
, okp
);
13323 if (h
->u2
.vtable
->used
== NULL
)
13325 /* None of this table's entries were referenced. Re-use the
13327 h
->u2
.vtable
->used
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13328 h
->u2
.vtable
->size
= h
->u2
.vtable
->parent
->u2
.vtable
->size
;
13333 bfd_boolean
*cu
, *pu
;
13335 /* Or the parent's entries into ours. */
13336 cu
= h
->u2
.vtable
->used
;
13338 pu
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13341 const struct elf_backend_data
*bed
;
13342 unsigned int log_file_align
;
13344 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
13345 log_file_align
= bed
->s
->log_file_align
;
13346 n
= h
->u2
.vtable
->parent
->u2
.vtable
->size
>> log_file_align
;
13361 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
13364 bfd_vma hstart
, hend
;
13365 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
13366 const struct elf_backend_data
*bed
;
13367 unsigned int log_file_align
;
13369 /* Take care of both those symbols that do not describe vtables as
13370 well as those that are not loaded. */
13372 || h
->u2
.vtable
== NULL
13373 || h
->u2
.vtable
->parent
== NULL
)
13376 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
13377 || h
->root
.type
== bfd_link_hash_defweak
);
13379 sec
= h
->root
.u
.def
.section
;
13380 hstart
= h
->root
.u
.def
.value
;
13381 hend
= hstart
+ h
->size
;
13383 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
13385 return *(bfd_boolean
*) okp
= FALSE
;
13386 bed
= get_elf_backend_data (sec
->owner
);
13387 log_file_align
= bed
->s
->log_file_align
;
13389 relend
= relstart
+ sec
->reloc_count
;
13391 for (rel
= relstart
; rel
< relend
; ++rel
)
13392 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
13394 /* If the entry is in use, do nothing. */
13395 if (h
->u2
.vtable
->used
13396 && (rel
->r_offset
- hstart
) < h
->u2
.vtable
->size
)
13398 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
13399 if (h
->u2
.vtable
->used
[entry
])
13402 /* Otherwise, kill it. */
13403 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
13409 /* Mark sections containing dynamically referenced symbols. When
13410 building shared libraries, we must assume that any visible symbol is
13414 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
13416 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
13417 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
13419 if ((h
->root
.type
== bfd_link_hash_defined
13420 || h
->root
.type
== bfd_link_hash_defweak
)
13421 && ((h
->ref_dynamic
&& !h
->forced_local
)
13422 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
13423 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
13424 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
13425 && (!bfd_link_executable (info
)
13426 || info
->gc_keep_exported
13427 || info
->export_dynamic
13430 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
13431 && (h
->versioned
>= versioned
13432 || !bfd_hide_sym_by_version (info
->version_info
,
13433 h
->root
.root
.string
)))))
13434 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13439 /* Keep all sections containing symbols undefined on the command-line,
13440 and the section containing the entry symbol. */
13443 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
13445 struct bfd_sym_chain
*sym
;
13447 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
13449 struct elf_link_hash_entry
*h
;
13451 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
13452 FALSE
, FALSE
, FALSE
);
13455 && (h
->root
.type
== bfd_link_hash_defined
13456 || h
->root
.type
== bfd_link_hash_defweak
)
13457 && !bfd_is_abs_section (h
->root
.u
.def
.section
)
13458 && !bfd_is_und_section (h
->root
.u
.def
.section
))
13459 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13464 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
13465 struct bfd_link_info
*info
)
13467 bfd
*ibfd
= info
->input_bfds
;
13469 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13472 struct elf_reloc_cookie cookie
;
13474 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13476 sec
= ibfd
->sections
;
13477 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13480 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
13483 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
13485 if (CONST_STRNEQ (bfd_section_name (ibfd
, sec
), ".eh_frame_entry")
13486 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
13488 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
13489 fini_reloc_cookie_rels (&cookie
, sec
);
13496 /* Do mark and sweep of unused sections. */
13499 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
13501 bfd_boolean ok
= TRUE
;
13503 elf_gc_mark_hook_fn gc_mark_hook
;
13504 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13505 struct elf_link_hash_table
*htab
;
13507 if (!bed
->can_gc_sections
13508 || !is_elf_hash_table (info
->hash
))
13510 _bfd_error_handler(_("warning: gc-sections option ignored"));
13514 bed
->gc_keep (info
);
13515 htab
= elf_hash_table (info
);
13517 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13518 at the .eh_frame section if we can mark the FDEs individually. */
13519 for (sub
= info
->input_bfds
;
13520 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
13521 sub
= sub
->link
.next
)
13524 struct elf_reloc_cookie cookie
;
13526 sec
= sub
->sections
;
13527 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13529 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
13530 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
13532 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
13533 if (elf_section_data (sec
)->sec_info
13534 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
13535 elf_eh_frame_section (sub
) = sec
;
13536 fini_reloc_cookie_for_section (&cookie
, sec
);
13537 sec
= bfd_get_next_section_by_name (NULL
, sec
);
13541 /* Apply transitive closure to the vtable entry usage info. */
13542 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
13546 /* Kill the vtable relocations that were not used. */
13547 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &ok
);
13551 /* Mark dynamically referenced symbols. */
13552 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
13553 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
13555 /* Grovel through relocs to find out who stays ... */
13556 gc_mark_hook
= bed
->gc_mark_hook
;
13557 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13561 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13562 || elf_object_id (sub
) != elf_hash_table_id (htab
)
13563 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13567 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13570 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13571 Also treat note sections as a root, if the section is not part
13572 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13573 well as FINI_ARRAY sections for ld -r. */
13574 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13576 && (o
->flags
& SEC_EXCLUDE
) == 0
13577 && ((o
->flags
& SEC_KEEP
) != 0
13578 || (bfd_link_relocatable (info
)
13579 && ((elf_section_data (o
)->this_hdr
.sh_type
13580 == SHT_PREINIT_ARRAY
)
13581 || (elf_section_data (o
)->this_hdr
.sh_type
13583 || (elf_section_data (o
)->this_hdr
.sh_type
13584 == SHT_FINI_ARRAY
)))
13585 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
13586 && elf_next_in_group (o
) == NULL
)))
13588 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
13593 /* Allow the backend to mark additional target specific sections. */
13594 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
13596 /* ... and mark SEC_EXCLUDE for those that go. */
13597 return elf_gc_sweep (abfd
, info
);
13600 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13603 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
13605 struct elf_link_hash_entry
*h
,
13608 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
13609 struct elf_link_hash_entry
**search
, *child
;
13610 size_t extsymcount
;
13611 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13613 /* The sh_info field of the symtab header tells us where the
13614 external symbols start. We don't care about the local symbols at
13616 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
13617 if (!elf_bad_symtab (abfd
))
13618 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
13620 sym_hashes
= elf_sym_hashes (abfd
);
13621 sym_hashes_end
= sym_hashes
+ extsymcount
;
13623 /* Hunt down the child symbol, which is in this section at the same
13624 offset as the relocation. */
13625 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
13627 if ((child
= *search
) != NULL
13628 && (child
->root
.type
== bfd_link_hash_defined
13629 || child
->root
.type
== bfd_link_hash_defweak
)
13630 && child
->root
.u
.def
.section
== sec
13631 && child
->root
.u
.def
.value
== offset
)
13635 /* xgettext:c-format */
13636 _bfd_error_handler (_("%pB: %pA+%#" PRIx64
": no symbol found for INHERIT"),
13637 abfd
, sec
, (uint64_t) offset
);
13638 bfd_set_error (bfd_error_invalid_operation
);
13642 if (!child
->u2
.vtable
)
13644 child
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
13645 bfd_zalloc (abfd
, sizeof (*child
->u2
.vtable
)));
13646 if (!child
->u2
.vtable
)
13651 /* This *should* only be the absolute section. It could potentially
13652 be that someone has defined a non-global vtable though, which
13653 would be bad. It isn't worth paging in the local symbols to be
13654 sure though; that case should simply be handled by the assembler. */
13656 child
->u2
.vtable
->parent
= (struct elf_link_hash_entry
*) -1;
13659 child
->u2
.vtable
->parent
= h
;
13664 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13667 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
13668 asection
*sec ATTRIBUTE_UNUSED
,
13669 struct elf_link_hash_entry
*h
,
13672 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13673 unsigned int log_file_align
= bed
->s
->log_file_align
;
13677 h
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
13678 bfd_zalloc (abfd
, sizeof (*h
->u2
.vtable
)));
13683 if (addend
>= h
->u2
.vtable
->size
)
13685 size_t size
, bytes
, file_align
;
13686 bfd_boolean
*ptr
= h
->u2
.vtable
->used
;
13688 /* While the symbol is undefined, we have to be prepared to handle
13690 file_align
= 1 << log_file_align
;
13691 if (h
->root
.type
== bfd_link_hash_undefined
)
13692 size
= addend
+ file_align
;
13696 if (addend
>= size
)
13698 /* Oops! We've got a reference past the defined end of
13699 the table. This is probably a bug -- shall we warn? */
13700 size
= addend
+ file_align
;
13703 size
= (size
+ file_align
- 1) & -file_align
;
13705 /* Allocate one extra entry for use as a "done" flag for the
13706 consolidation pass. */
13707 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
13711 ptr
= (bfd_boolean
*) bfd_realloc (ptr
- 1, bytes
);
13717 oldbytes
= (((h
->u2
.vtable
->size
>> log_file_align
) + 1)
13718 * sizeof (bfd_boolean
));
13719 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
13723 ptr
= (bfd_boolean
*) bfd_zmalloc (bytes
);
13728 /* And arrange for that done flag to be at index -1. */
13729 h
->u2
.vtable
->used
= ptr
+ 1;
13730 h
->u2
.vtable
->size
= size
;
13733 h
->u2
.vtable
->used
[addend
>> log_file_align
] = TRUE
;
13738 /* Map an ELF section header flag to its corresponding string. */
13742 flagword flag_value
;
13743 } elf_flags_to_name_table
;
13745 static elf_flags_to_name_table elf_flags_to_names
[] =
13747 { "SHF_WRITE", SHF_WRITE
},
13748 { "SHF_ALLOC", SHF_ALLOC
},
13749 { "SHF_EXECINSTR", SHF_EXECINSTR
},
13750 { "SHF_MERGE", SHF_MERGE
},
13751 { "SHF_STRINGS", SHF_STRINGS
},
13752 { "SHF_INFO_LINK", SHF_INFO_LINK
},
13753 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
13754 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
13755 { "SHF_GROUP", SHF_GROUP
},
13756 { "SHF_TLS", SHF_TLS
},
13757 { "SHF_MASKOS", SHF_MASKOS
},
13758 { "SHF_EXCLUDE", SHF_EXCLUDE
},
13761 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13763 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
13764 struct flag_info
*flaginfo
,
13767 const bfd_vma sh_flags
= elf_section_flags (section
);
13769 if (!flaginfo
->flags_initialized
)
13771 bfd
*obfd
= info
->output_bfd
;
13772 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13773 struct flag_info_list
*tf
= flaginfo
->flag_list
;
13775 int without_hex
= 0;
13777 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
13780 flagword (*lookup
) (char *);
13782 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
13783 if (lookup
!= NULL
)
13785 flagword hexval
= (*lookup
) ((char *) tf
->name
);
13789 if (tf
->with
== with_flags
)
13790 with_hex
|= hexval
;
13791 else if (tf
->with
== without_flags
)
13792 without_hex
|= hexval
;
13797 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
13799 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
13801 if (tf
->with
== with_flags
)
13802 with_hex
|= elf_flags_to_names
[i
].flag_value
;
13803 else if (tf
->with
== without_flags
)
13804 without_hex
|= elf_flags_to_names
[i
].flag_value
;
13811 info
->callbacks
->einfo
13812 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
13816 flaginfo
->flags_initialized
= TRUE
;
13817 flaginfo
->only_with_flags
|= with_hex
;
13818 flaginfo
->not_with_flags
|= without_hex
;
13821 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
13824 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
13830 struct alloc_got_off_arg
{
13832 struct bfd_link_info
*info
;
13835 /* We need a special top-level link routine to convert got reference counts
13836 to real got offsets. */
13839 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
13841 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
13842 bfd
*obfd
= gofarg
->info
->output_bfd
;
13843 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13845 if (h
->got
.refcount
> 0)
13847 h
->got
.offset
= gofarg
->gotoff
;
13848 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
13851 h
->got
.offset
= (bfd_vma
) -1;
13856 /* And an accompanying bit to work out final got entry offsets once
13857 we're done. Should be called from final_link. */
13860 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
13861 struct bfd_link_info
*info
)
13864 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13866 struct alloc_got_off_arg gofarg
;
13868 BFD_ASSERT (abfd
== info
->output_bfd
);
13870 if (! is_elf_hash_table (info
->hash
))
13873 /* The GOT offset is relative to the .got section, but the GOT header is
13874 put into the .got.plt section, if the backend uses it. */
13875 if (bed
->want_got_plt
)
13878 gotoff
= bed
->got_header_size
;
13880 /* Do the local .got entries first. */
13881 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
13883 bfd_signed_vma
*local_got
;
13884 size_t j
, locsymcount
;
13885 Elf_Internal_Shdr
*symtab_hdr
;
13887 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
13890 local_got
= elf_local_got_refcounts (i
);
13894 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
13895 if (elf_bad_symtab (i
))
13896 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13898 locsymcount
= symtab_hdr
->sh_info
;
13900 for (j
= 0; j
< locsymcount
; ++j
)
13902 if (local_got
[j
] > 0)
13904 local_got
[j
] = gotoff
;
13905 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
13908 local_got
[j
] = (bfd_vma
) -1;
13912 /* Then the global .got entries. .plt refcounts are handled by
13913 adjust_dynamic_symbol */
13914 gofarg
.gotoff
= gotoff
;
13915 gofarg
.info
= info
;
13916 elf_link_hash_traverse (elf_hash_table (info
),
13917 elf_gc_allocate_got_offsets
,
13922 /* Many folk need no more in the way of final link than this, once
13923 got entry reference counting is enabled. */
13926 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
13928 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
13931 /* Invoke the regular ELF backend linker to do all the work. */
13932 return bfd_elf_final_link (abfd
, info
);
13936 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
13938 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
13940 if (rcookie
->bad_symtab
)
13941 rcookie
->rel
= rcookie
->rels
;
13943 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
13945 unsigned long r_symndx
;
13947 if (! rcookie
->bad_symtab
)
13948 if (rcookie
->rel
->r_offset
> offset
)
13950 if (rcookie
->rel
->r_offset
!= offset
)
13953 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
13954 if (r_symndx
== STN_UNDEF
)
13957 if (r_symndx
>= rcookie
->locsymcount
13958 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13960 struct elf_link_hash_entry
*h
;
13962 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
13964 while (h
->root
.type
== bfd_link_hash_indirect
13965 || h
->root
.type
== bfd_link_hash_warning
)
13966 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13968 if ((h
->root
.type
== bfd_link_hash_defined
13969 || h
->root
.type
== bfd_link_hash_defweak
)
13970 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
13971 || h
->root
.u
.def
.section
->kept_section
!= NULL
13972 || discarded_section (h
->root
.u
.def
.section
)))
13977 /* It's not a relocation against a global symbol,
13978 but it could be a relocation against a local
13979 symbol for a discarded section. */
13981 Elf_Internal_Sym
*isym
;
13983 /* Need to: get the symbol; get the section. */
13984 isym
= &rcookie
->locsyms
[r_symndx
];
13985 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
13987 && (isec
->kept_section
!= NULL
13988 || discarded_section (isec
)))
13996 /* Discard unneeded references to discarded sections.
13997 Returns -1 on error, 1 if any section's size was changed, 0 if
13998 nothing changed. This function assumes that the relocations are in
13999 sorted order, which is true for all known assemblers. */
14002 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
14004 struct elf_reloc_cookie cookie
;
14009 if (info
->traditional_format
14010 || !is_elf_hash_table (info
->hash
))
14013 o
= bfd_get_section_by_name (output_bfd
, ".stab");
14018 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14021 || i
->reloc_count
== 0
14022 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
14026 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14029 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14032 if (_bfd_discard_section_stabs (abfd
, i
,
14033 elf_section_data (i
)->sec_info
,
14034 bfd_elf_reloc_symbol_deleted_p
,
14038 fini_reloc_cookie_for_section (&cookie
, i
);
14043 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
14044 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
14048 int eh_changed
= 0;
14049 unsigned int eh_alignment
;
14051 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14057 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14060 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14063 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
14064 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
14065 bfd_elf_reloc_symbol_deleted_p
,
14069 if (i
->size
!= i
->rawsize
)
14073 fini_reloc_cookie_for_section (&cookie
, i
);
14076 eh_alignment
= 1 << o
->alignment_power
;
14077 /* Skip over zero terminator, and prevent empty sections from
14078 adding alignment padding at the end. */
14079 for (i
= o
->map_tail
.s
; i
!= NULL
; i
= i
->map_tail
.s
)
14081 i
->flags
|= SEC_EXCLUDE
;
14082 else if (i
->size
> 4)
14084 /* The last non-empty eh_frame section doesn't need padding. */
14087 /* Any prior sections must pad the last FDE out to the output
14088 section alignment. Otherwise we might have zero padding
14089 between sections, which would be seen as a terminator. */
14090 for (; i
!= NULL
; i
= i
->map_tail
.s
)
14092 /* All but the last zero terminator should have been removed. */
14097 = (i
->size
+ eh_alignment
- 1) & -eh_alignment
;
14098 if (i
->size
!= size
)
14106 elf_link_hash_traverse (elf_hash_table (info
),
14107 _bfd_elf_adjust_eh_frame_global_symbol
, NULL
);
14110 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
14112 const struct elf_backend_data
*bed
;
14115 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14117 s
= abfd
->sections
;
14118 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14121 bed
= get_elf_backend_data (abfd
);
14123 if (bed
->elf_backend_discard_info
!= NULL
)
14125 if (!init_reloc_cookie (&cookie
, info
, abfd
))
14128 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
14131 fini_reloc_cookie (&cookie
, abfd
);
14135 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
14136 _bfd_elf_end_eh_frame_parsing (info
);
14138 if (info
->eh_frame_hdr_type
14139 && !bfd_link_relocatable (info
)
14140 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
14147 _bfd_elf_section_already_linked (bfd
*abfd
,
14149 struct bfd_link_info
*info
)
14152 const char *name
, *key
;
14153 struct bfd_section_already_linked
*l
;
14154 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
14156 if (sec
->output_section
== bfd_abs_section_ptr
)
14159 flags
= sec
->flags
;
14161 /* Return if it isn't a linkonce section. A comdat group section
14162 also has SEC_LINK_ONCE set. */
14163 if ((flags
& SEC_LINK_ONCE
) == 0)
14166 /* Don't put group member sections on our list of already linked
14167 sections. They are handled as a group via their group section. */
14168 if (elf_sec_group (sec
) != NULL
)
14171 /* For a SHT_GROUP section, use the group signature as the key. */
14173 if ((flags
& SEC_GROUP
) != 0
14174 && elf_next_in_group (sec
) != NULL
14175 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
14176 key
= elf_group_name (elf_next_in_group (sec
));
14179 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14180 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
14181 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
14184 /* Must be a user linkonce section that doesn't follow gcc's
14185 naming convention. In this case we won't be matching
14186 single member groups. */
14190 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
14192 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14194 /* We may have 2 different types of sections on the list: group
14195 sections with a signature of <key> (<key> is some string),
14196 and linkonce sections named .gnu.linkonce.<type>.<key>.
14197 Match like sections. LTO plugin sections are an exception.
14198 They are always named .gnu.linkonce.t.<key> and match either
14199 type of section. */
14200 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
14201 && ((flags
& SEC_GROUP
) != 0
14202 || strcmp (name
, l
->sec
->name
) == 0))
14203 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0)
14205 /* The section has already been linked. See if we should
14206 issue a warning. */
14207 if (!_bfd_handle_already_linked (sec
, l
, info
))
14210 if (flags
& SEC_GROUP
)
14212 asection
*first
= elf_next_in_group (sec
);
14213 asection
*s
= first
;
14217 s
->output_section
= bfd_abs_section_ptr
;
14218 /* Record which group discards it. */
14219 s
->kept_section
= l
->sec
;
14220 s
= elf_next_in_group (s
);
14221 /* These lists are circular. */
14231 /* A single member comdat group section may be discarded by a
14232 linkonce section and vice versa. */
14233 if ((flags
& SEC_GROUP
) != 0)
14235 asection
*first
= elf_next_in_group (sec
);
14237 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
14238 /* Check this single member group against linkonce sections. */
14239 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14240 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14241 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
14243 first
->output_section
= bfd_abs_section_ptr
;
14244 first
->kept_section
= l
->sec
;
14245 sec
->output_section
= bfd_abs_section_ptr
;
14250 /* Check this linkonce section against single member groups. */
14251 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14252 if (l
->sec
->flags
& SEC_GROUP
)
14254 asection
*first
= elf_next_in_group (l
->sec
);
14257 && elf_next_in_group (first
) == first
14258 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
14260 sec
->output_section
= bfd_abs_section_ptr
;
14261 sec
->kept_section
= first
;
14266 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14267 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14268 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14269 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14270 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14271 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14272 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14273 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14274 The reverse order cannot happen as there is never a bfd with only the
14275 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14276 matter as here were are looking only for cross-bfd sections. */
14278 if ((flags
& SEC_GROUP
) == 0 && CONST_STRNEQ (name
, ".gnu.linkonce.r."))
14279 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14280 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14281 && CONST_STRNEQ (l
->sec
->name
, ".gnu.linkonce.t."))
14283 if (abfd
!= l
->sec
->owner
)
14284 sec
->output_section
= bfd_abs_section_ptr
;
14288 /* This is the first section with this name. Record it. */
14289 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
14290 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
14291 return sec
->output_section
== bfd_abs_section_ptr
;
14295 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
14297 return sym
->st_shndx
== SHN_COMMON
;
14301 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
14307 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
14309 return bfd_com_section_ptr
;
14313 _bfd_elf_default_got_elt_size (bfd
*abfd
,
14314 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
14315 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
14316 bfd
*ibfd ATTRIBUTE_UNUSED
,
14317 unsigned long symndx ATTRIBUTE_UNUSED
)
14319 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14320 return bed
->s
->arch_size
/ 8;
14323 /* Routines to support the creation of dynamic relocs. */
14325 /* Returns the name of the dynamic reloc section associated with SEC. */
14327 static const char *
14328 get_dynamic_reloc_section_name (bfd
* abfd
,
14330 bfd_boolean is_rela
)
14333 const char *old_name
= bfd_get_section_name (NULL
, sec
);
14334 const char *prefix
= is_rela
? ".rela" : ".rel";
14336 if (old_name
== NULL
)
14339 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
14340 sprintf (name
, "%s%s", prefix
, old_name
);
14345 /* Returns the dynamic reloc section associated with SEC.
14346 If necessary compute the name of the dynamic reloc section based
14347 on SEC's name (looked up in ABFD's string table) and the setting
14351 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
14353 bfd_boolean is_rela
)
14355 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14357 if (reloc_sec
== NULL
)
14359 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14363 reloc_sec
= bfd_get_linker_section (abfd
, name
);
14365 if (reloc_sec
!= NULL
)
14366 elf_section_data (sec
)->sreloc
= reloc_sec
;
14373 /* Returns the dynamic reloc section associated with SEC. If the
14374 section does not exist it is created and attached to the DYNOBJ
14375 bfd and stored in the SRELOC field of SEC's elf_section_data
14378 ALIGNMENT is the alignment for the newly created section and
14379 IS_RELA defines whether the name should be .rela.<SEC's name>
14380 or .rel.<SEC's name>. The section name is looked up in the
14381 string table associated with ABFD. */
14384 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
14386 unsigned int alignment
,
14388 bfd_boolean is_rela
)
14390 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14392 if (reloc_sec
== NULL
)
14394 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14399 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
14401 if (reloc_sec
== NULL
)
14403 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
14404 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
14405 if ((sec
->flags
& SEC_ALLOC
) != 0)
14406 flags
|= SEC_ALLOC
| SEC_LOAD
;
14408 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
14409 if (reloc_sec
!= NULL
)
14411 /* _bfd_elf_get_sec_type_attr chooses a section type by
14412 name. Override as it may be wrong, eg. for a user
14413 section named "auto" we'll get ".relauto" which is
14414 seen to be a .rela section. */
14415 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
14416 if (! bfd_set_section_alignment (dynobj
, reloc_sec
, alignment
))
14421 elf_section_data (sec
)->sreloc
= reloc_sec
;
14427 /* Copy the ELF symbol type and other attributes for a linker script
14428 assignment from HSRC to HDEST. Generally this should be treated as
14429 if we found a strong non-dynamic definition for HDEST (except that
14430 ld ignores multiple definition errors). */
14432 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
14433 struct bfd_link_hash_entry
*hdest
,
14434 struct bfd_link_hash_entry
*hsrc
)
14436 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
14437 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
14438 Elf_Internal_Sym isym
;
14440 ehdest
->type
= ehsrc
->type
;
14441 ehdest
->target_internal
= ehsrc
->target_internal
;
14443 isym
.st_other
= ehsrc
->other
;
14444 elf_merge_st_other (abfd
, ehdest
, &isym
, NULL
, TRUE
, FALSE
);
14447 /* Append a RELA relocation REL to section S in BFD. */
14450 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14452 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14453 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
14454 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
14455 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
14458 /* Append a REL relocation REL to section S in BFD. */
14461 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14463 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14464 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
14465 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
14466 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);
14469 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14471 struct bfd_link_hash_entry
*
14472 bfd_elf_define_start_stop (struct bfd_link_info
*info
,
14473 const char *symbol
, asection
*sec
)
14475 struct elf_link_hash_entry
*h
;
14477 h
= elf_link_hash_lookup (elf_hash_table (info
), symbol
,
14478 FALSE
, FALSE
, TRUE
);
14480 && (h
->root
.type
== bfd_link_hash_undefined
14481 || h
->root
.type
== bfd_link_hash_undefweak
14482 || ((h
->ref_regular
|| h
->def_dynamic
) && !h
->def_regular
)))
14484 bfd_boolean was_dynamic
= h
->ref_dynamic
|| h
->def_dynamic
;
14485 h
->root
.type
= bfd_link_hash_defined
;
14486 h
->root
.u
.def
.section
= sec
;
14487 h
->root
.u
.def
.value
= 0;
14488 h
->def_regular
= 1;
14489 h
->def_dynamic
= 0;
14491 h
->u2
.start_stop_section
= sec
;
14492 if (symbol
[0] == '.')
14494 /* .startof. and .sizeof. symbols are local. */
14495 const struct elf_backend_data
*bed
;
14496 bed
= get_elf_backend_data (info
->output_bfd
);
14497 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
14501 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
14502 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED
;
14504 bfd_elf_link_record_dynamic_symbol (info
, h
);