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
)))
592 /* Record an assignment to a symbol made by a linker script. We need
593 this in case some dynamic object refers to this symbol. */
596 bfd_elf_record_link_assignment (bfd
*output_bfd
,
597 struct bfd_link_info
*info
,
602 struct elf_link_hash_entry
*h
, *hv
;
603 struct elf_link_hash_table
*htab
;
604 const struct elf_backend_data
*bed
;
606 if (!is_elf_hash_table (info
->hash
))
609 htab
= elf_hash_table (info
);
610 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
614 if (h
->root
.type
== bfd_link_hash_warning
)
615 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
617 if (h
->versioned
== unknown
)
619 /* Set versioned if symbol version is unknown. */
620 char *version
= strrchr (name
, ELF_VER_CHR
);
623 if (version
> name
&& version
[-1] != ELF_VER_CHR
)
624 h
->versioned
= versioned_hidden
;
626 h
->versioned
= versioned
;
630 /* Symbols defined in a linker script but not referenced anywhere
631 else will have non_elf set. */
634 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
638 switch (h
->root
.type
)
640 case bfd_link_hash_defined
:
641 case bfd_link_hash_defweak
:
642 case bfd_link_hash_common
:
644 case bfd_link_hash_undefweak
:
645 case bfd_link_hash_undefined
:
646 /* Since we're defining the symbol, don't let it seem to have not
647 been defined. record_dynamic_symbol and size_dynamic_sections
648 may depend on this. */
649 h
->root
.type
= bfd_link_hash_new
;
650 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
651 bfd_link_repair_undef_list (&htab
->root
);
653 case bfd_link_hash_new
:
655 case bfd_link_hash_indirect
:
656 /* We had a versioned symbol in a dynamic library. We make the
657 the versioned symbol point to this one. */
658 bed
= get_elf_backend_data (output_bfd
);
660 while (hv
->root
.type
== bfd_link_hash_indirect
661 || hv
->root
.type
== bfd_link_hash_warning
)
662 hv
= (struct elf_link_hash_entry
*) hv
->root
.u
.i
.link
;
663 /* We don't need to update h->root.u since linker will set them
665 h
->root
.type
= bfd_link_hash_undefined
;
666 hv
->root
.type
= bfd_link_hash_indirect
;
667 hv
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
668 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hv
);
675 /* If this symbol is being provided by the linker script, and it is
676 currently defined by a dynamic object, but not by a regular
677 object, then mark it as undefined so that the generic linker will
678 force the correct value. */
682 h
->root
.type
= bfd_link_hash_undefined
;
684 /* If this symbol is not being provided by the linker script, and it is
685 currently defined by a dynamic object, but not by a regular object,
686 then clear out any version information because the symbol will not be
687 associated with the dynamic object any more. */
691 h
->verinfo
.verdef
= NULL
;
693 /* Make sure this symbol is not garbage collected. */
700 bed
= get_elf_backend_data (output_bfd
);
701 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
702 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
703 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
706 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
708 if (!bfd_link_relocatable (info
)
710 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
711 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
716 || bfd_link_dll (info
)
717 || elf_hash_table (info
)->is_relocatable_executable
)
720 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
723 /* If this is a weak defined symbol, and we know a corresponding
724 real symbol from the same dynamic object, make sure the real
725 symbol is also made into a dynamic symbol. */
728 struct elf_link_hash_entry
*def
= weakdef (h
);
730 if (def
->dynindx
== -1
731 && !bfd_elf_link_record_dynamic_symbol (info
, def
))
739 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
740 success, and 2 on a failure caused by attempting to record a symbol
741 in a discarded section, eg. a discarded link-once section symbol. */
744 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
749 struct elf_link_local_dynamic_entry
*entry
;
750 struct elf_link_hash_table
*eht
;
751 struct elf_strtab_hash
*dynstr
;
754 Elf_External_Sym_Shndx eshndx
;
755 char esym
[sizeof (Elf64_External_Sym
)];
757 if (! is_elf_hash_table (info
->hash
))
760 /* See if the entry exists already. */
761 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
762 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
765 amt
= sizeof (*entry
);
766 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
770 /* Go find the symbol, so that we can find it's name. */
771 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
772 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
774 bfd_release (input_bfd
, entry
);
778 if (entry
->isym
.st_shndx
!= SHN_UNDEF
779 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
783 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
784 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
786 /* We can still bfd_release here as nothing has done another
787 bfd_alloc. We can't do this later in this function. */
788 bfd_release (input_bfd
, entry
);
793 name
= (bfd_elf_string_from_elf_section
794 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
795 entry
->isym
.st_name
));
797 dynstr
= elf_hash_table (info
)->dynstr
;
800 /* Create a strtab to hold the dynamic symbol names. */
801 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
806 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
807 if (dynstr_index
== (size_t) -1)
809 entry
->isym
.st_name
= dynstr_index
;
811 eht
= elf_hash_table (info
);
813 entry
->next
= eht
->dynlocal
;
814 eht
->dynlocal
= entry
;
815 entry
->input_bfd
= input_bfd
;
816 entry
->input_indx
= input_indx
;
819 /* Whatever binding the symbol had before, it's now local. */
821 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
823 /* The dynindx will be set at the end of size_dynamic_sections. */
828 /* Return the dynindex of a local dynamic symbol. */
831 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
835 struct elf_link_local_dynamic_entry
*e
;
837 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
838 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
843 /* This function is used to renumber the dynamic symbols, if some of
844 them are removed because they are marked as local. This is called
845 via elf_link_hash_traverse. */
848 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
851 size_t *count
= (size_t *) data
;
856 if (h
->dynindx
!= -1)
857 h
->dynindx
= ++(*count
);
863 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
864 STB_LOCAL binding. */
867 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
870 size_t *count
= (size_t *) data
;
872 if (!h
->forced_local
)
875 if (h
->dynindx
!= -1)
876 h
->dynindx
= ++(*count
);
881 /* Return true if the dynamic symbol for a given section should be
882 omitted when creating a shared library. */
884 _bfd_elf_omit_section_dynsym_default (bfd
*output_bfd ATTRIBUTE_UNUSED
,
885 struct bfd_link_info
*info
,
888 struct elf_link_hash_table
*htab
;
891 switch (elf_section_data (p
)->this_hdr
.sh_type
)
895 /* If sh_type is yet undecided, assume it could be
896 SHT_PROGBITS/SHT_NOBITS. */
898 htab
= elf_hash_table (info
);
899 if (p
== htab
->tls_sec
)
902 if (htab
->text_index_section
!= NULL
)
903 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
905 return (htab
->dynobj
!= NULL
906 && (ip
= bfd_get_linker_section (htab
->dynobj
, p
->name
)) != NULL
907 && ip
->output_section
== p
);
909 /* There shouldn't be section relative relocations
910 against any other section. */
917 _bfd_elf_omit_section_dynsym_all
918 (bfd
*output_bfd ATTRIBUTE_UNUSED
,
919 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
920 asection
*p ATTRIBUTE_UNUSED
)
925 /* Assign dynsym indices. In a shared library we generate a section
926 symbol for each output section, which come first. Next come symbols
927 which have been forced to local binding. Then all of the back-end
928 allocated local dynamic syms, followed by the rest of the global
929 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
930 (This prevents the early call before elf_backend_init_index_section
931 and strip_excluded_output_sections setting dynindx for sections
932 that are stripped.) */
935 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
936 struct bfd_link_info
*info
,
937 unsigned long *section_sym_count
)
939 unsigned long dynsymcount
= 0;
940 bfd_boolean do_sec
= section_sym_count
!= NULL
;
942 if (bfd_link_pic (info
)
943 || elf_hash_table (info
)->is_relocatable_executable
)
945 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
947 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
948 if ((p
->flags
& SEC_EXCLUDE
) == 0
949 && (p
->flags
& SEC_ALLOC
) != 0
950 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
954 elf_section_data (p
)->dynindx
= dynsymcount
;
957 elf_section_data (p
)->dynindx
= 0;
960 *section_sym_count
= dynsymcount
;
962 elf_link_hash_traverse (elf_hash_table (info
),
963 elf_link_renumber_local_hash_table_dynsyms
,
966 if (elf_hash_table (info
)->dynlocal
)
968 struct elf_link_local_dynamic_entry
*p
;
969 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
970 p
->dynindx
= ++dynsymcount
;
972 elf_hash_table (info
)->local_dynsymcount
= dynsymcount
;
974 elf_link_hash_traverse (elf_hash_table (info
),
975 elf_link_renumber_hash_table_dynsyms
,
978 /* There is an unused NULL entry at the head of the table which we
979 must account for in our count even if the table is empty since it
980 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
984 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
988 /* Merge st_other field. */
991 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
992 const Elf_Internal_Sym
*isym
, asection
*sec
,
993 bfd_boolean definition
, bfd_boolean dynamic
)
995 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
997 /* If st_other has a processor-specific meaning, specific
998 code might be needed here. */
999 if (bed
->elf_backend_merge_symbol_attribute
)
1000 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
1005 unsigned symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
1006 unsigned hvis
= ELF_ST_VISIBILITY (h
->other
);
1008 /* Keep the most constraining visibility. Leave the remainder
1009 of the st_other field to elf_backend_merge_symbol_attribute. */
1010 if (symvis
- 1 < hvis
- 1)
1011 h
->other
= symvis
| (h
->other
& ~ELF_ST_VISIBILITY (-1));
1014 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_DEFAULT
1015 && (sec
->flags
& SEC_READONLY
) == 0)
1016 h
->protected_def
= 1;
1019 /* This function is called when we want to merge a new symbol with an
1020 existing symbol. It handles the various cases which arise when we
1021 find a definition in a dynamic object, or when there is already a
1022 definition in a dynamic object. The new symbol is described by
1023 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1024 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1025 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1026 of an old common symbol. We set OVERRIDE if the old symbol is
1027 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1028 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1029 to change. By OK to change, we mean that we shouldn't warn if the
1030 type or size does change. */
1033 _bfd_elf_merge_symbol (bfd
*abfd
,
1034 struct bfd_link_info
*info
,
1036 Elf_Internal_Sym
*sym
,
1039 struct elf_link_hash_entry
**sym_hash
,
1041 bfd_boolean
*pold_weak
,
1042 unsigned int *pold_alignment
,
1044 bfd_boolean
*override
,
1045 bfd_boolean
*type_change_ok
,
1046 bfd_boolean
*size_change_ok
,
1047 bfd_boolean
*matched
)
1049 asection
*sec
, *oldsec
;
1050 struct elf_link_hash_entry
*h
;
1051 struct elf_link_hash_entry
*hi
;
1052 struct elf_link_hash_entry
*flip
;
1055 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
1056 bfd_boolean newweak
, oldweak
, newfunc
, oldfunc
;
1057 const struct elf_backend_data
*bed
;
1059 bfd_boolean default_sym
= *matched
;
1065 bind
= ELF_ST_BIND (sym
->st_info
);
1067 if (! bfd_is_und_section (sec
))
1068 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
1070 h
= ((struct elf_link_hash_entry
*)
1071 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
1076 bed
= get_elf_backend_data (abfd
);
1078 /* NEW_VERSION is the symbol version of the new symbol. */
1079 if (h
->versioned
!= unversioned
)
1081 /* Symbol version is unknown or versioned. */
1082 new_version
= strrchr (name
, ELF_VER_CHR
);
1085 if (h
->versioned
== unknown
)
1087 if (new_version
> name
&& new_version
[-1] != ELF_VER_CHR
)
1088 h
->versioned
= versioned_hidden
;
1090 h
->versioned
= versioned
;
1093 if (new_version
[0] == '\0')
1097 h
->versioned
= unversioned
;
1102 /* For merging, we only care about real symbols. But we need to make
1103 sure that indirect symbol dynamic flags are updated. */
1105 while (h
->root
.type
== bfd_link_hash_indirect
1106 || h
->root
.type
== bfd_link_hash_warning
)
1107 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1111 if (hi
== h
|| h
->root
.type
== bfd_link_hash_new
)
1115 /* OLD_HIDDEN is true if the existing symbol is only visible
1116 to the symbol with the same symbol version. NEW_HIDDEN is
1117 true if the new symbol is only visible to the symbol with
1118 the same symbol version. */
1119 bfd_boolean old_hidden
= h
->versioned
== versioned_hidden
;
1120 bfd_boolean new_hidden
= hi
->versioned
== versioned_hidden
;
1121 if (!old_hidden
&& !new_hidden
)
1122 /* The new symbol matches the existing symbol if both
1127 /* OLD_VERSION is the symbol version of the existing
1131 if (h
->versioned
>= versioned
)
1132 old_version
= strrchr (h
->root
.root
.string
,
1137 /* The new symbol matches the existing symbol if they
1138 have the same symbol version. */
1139 *matched
= (old_version
== new_version
1140 || (old_version
!= NULL
1141 && new_version
!= NULL
1142 && strcmp (old_version
, new_version
) == 0));
1147 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1152 switch (h
->root
.type
)
1157 case bfd_link_hash_undefined
:
1158 case bfd_link_hash_undefweak
:
1159 oldbfd
= h
->root
.u
.undef
.abfd
;
1162 case bfd_link_hash_defined
:
1163 case bfd_link_hash_defweak
:
1164 oldbfd
= h
->root
.u
.def
.section
->owner
;
1165 oldsec
= h
->root
.u
.def
.section
;
1168 case bfd_link_hash_common
:
1169 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1170 oldsec
= h
->root
.u
.c
.p
->section
;
1172 *pold_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1175 if (poldbfd
&& *poldbfd
== NULL
)
1178 /* Differentiate strong and weak symbols. */
1179 newweak
= bind
== STB_WEAK
;
1180 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1181 || h
->root
.type
== bfd_link_hash_undefweak
);
1183 *pold_weak
= oldweak
;
1185 /* We have to check it for every instance since the first few may be
1186 references and not all compilers emit symbol type for undefined
1188 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1190 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1191 respectively, is from a dynamic object. */
1193 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1195 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1196 syms and defined syms in dynamic libraries respectively.
1197 ref_dynamic on the other hand can be set for a symbol defined in
1198 a dynamic library, and def_dynamic may not be set; When the
1199 definition in a dynamic lib is overridden by a definition in the
1200 executable use of the symbol in the dynamic lib becomes a
1201 reference to the executable symbol. */
1204 if (bfd_is_und_section (sec
))
1206 if (bind
!= STB_WEAK
)
1208 h
->ref_dynamic_nonweak
= 1;
1209 hi
->ref_dynamic_nonweak
= 1;
1214 /* Update the existing symbol only if they match. */
1217 hi
->dynamic_def
= 1;
1221 /* If we just created the symbol, mark it as being an ELF symbol.
1222 Other than that, there is nothing to do--there is no merge issue
1223 with a newly defined symbol--so we just return. */
1225 if (h
->root
.type
== bfd_link_hash_new
)
1231 /* In cases involving weak versioned symbols, we may wind up trying
1232 to merge a symbol with itself. Catch that here, to avoid the
1233 confusion that results if we try to override a symbol with
1234 itself. The additional tests catch cases like
1235 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1236 dynamic object, which we do want to handle here. */
1238 && (newweak
|| oldweak
)
1239 && ((abfd
->flags
& DYNAMIC
) == 0
1240 || !h
->def_regular
))
1245 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1246 else if (oldsec
!= NULL
)
1248 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1249 indices used by MIPS ELF. */
1250 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1253 /* Handle a case where plugin_notice won't be called and thus won't
1254 set the non_ir_ref flags on the first pass over symbols. */
1256 && (oldbfd
->flags
& BFD_PLUGIN
) != (abfd
->flags
& BFD_PLUGIN
)
1257 && newdyn
!= olddyn
)
1259 h
->root
.non_ir_ref_dynamic
= TRUE
;
1260 hi
->root
.non_ir_ref_dynamic
= TRUE
;
1263 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1264 respectively, appear to be a definition rather than reference. */
1266 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1268 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1269 && h
->root
.type
!= bfd_link_hash_undefweak
1270 && h
->root
.type
!= bfd_link_hash_common
);
1272 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1273 respectively, appear to be a function. */
1275 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1276 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1278 oldfunc
= (h
->type
!= STT_NOTYPE
1279 && bed
->is_function_type (h
->type
));
1281 if (!(newfunc
&& oldfunc
)
1282 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1283 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1284 && h
->type
!= STT_NOTYPE
1285 && (newdef
|| bfd_is_com_section (sec
))
1286 && (olddef
|| h
->root
.type
== bfd_link_hash_common
))
1288 /* If creating a default indirect symbol ("foo" or "foo@") from
1289 a dynamic versioned definition ("foo@@") skip doing so if
1290 there is an existing regular definition with a different
1291 type. We don't want, for example, a "time" variable in the
1292 executable overriding a "time" function in a shared library. */
1300 /* When adding a symbol from a regular object file after we have
1301 created indirect symbols, undo the indirection and any
1308 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1309 h
->forced_local
= 0;
1313 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1315 h
->root
.type
= bfd_link_hash_undefined
;
1316 h
->root
.u
.undef
.abfd
= abfd
;
1320 h
->root
.type
= bfd_link_hash_new
;
1321 h
->root
.u
.undef
.abfd
= NULL
;
1327 /* Check TLS symbols. We don't check undefined symbols introduced
1328 by "ld -u" which have no type (and oldbfd NULL), and we don't
1329 check symbols from plugins because they also have no type. */
1331 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1332 && (abfd
->flags
& BFD_PLUGIN
) == 0
1333 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1334 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1337 bfd_boolean ntdef
, tdef
;
1338 asection
*ntsec
, *tsec
;
1340 if (h
->type
== STT_TLS
)
1361 /* xgettext:c-format */
1362 (_("%s: TLS definition in %pB section %pA "
1363 "mismatches non-TLS definition in %pB section %pA"),
1364 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
, ntsec
);
1365 else if (!tdef
&& !ntdef
)
1367 /* xgettext:c-format */
1368 (_("%s: TLS reference in %pB "
1369 "mismatches non-TLS reference in %pB"),
1370 h
->root
.root
.string
, tbfd
, ntbfd
);
1373 /* xgettext:c-format */
1374 (_("%s: TLS definition in %pB section %pA "
1375 "mismatches non-TLS reference in %pB"),
1376 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
);
1379 /* xgettext:c-format */
1380 (_("%s: TLS reference in %pB "
1381 "mismatches non-TLS definition in %pB section %pA"),
1382 h
->root
.root
.string
, tbfd
, ntbfd
, ntsec
);
1384 bfd_set_error (bfd_error_bad_value
);
1388 /* If the old symbol has non-default visibility, we ignore the new
1389 definition from a dynamic object. */
1391 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1392 && !bfd_is_und_section (sec
))
1395 /* Make sure this symbol is dynamic. */
1397 hi
->ref_dynamic
= 1;
1398 /* A protected symbol has external availability. Make sure it is
1399 recorded as dynamic.
1401 FIXME: Should we check type and size for protected symbol? */
1402 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1403 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1408 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1411 /* If the new symbol with non-default visibility comes from a
1412 relocatable file and the old definition comes from a dynamic
1413 object, we remove the old definition. */
1414 if (hi
->root
.type
== bfd_link_hash_indirect
)
1416 /* Handle the case where the old dynamic definition is
1417 default versioned. We need to copy the symbol info from
1418 the symbol with default version to the normal one if it
1419 was referenced before. */
1422 hi
->root
.type
= h
->root
.type
;
1423 h
->root
.type
= bfd_link_hash_indirect
;
1424 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1426 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1427 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1429 /* If the new symbol is hidden or internal, completely undo
1430 any dynamic link state. */
1431 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1432 h
->forced_local
= 0;
1439 /* FIXME: Should we check type and size for protected symbol? */
1449 /* If the old symbol was undefined before, then it will still be
1450 on the undefs list. If the new symbol is undefined or
1451 common, we can't make it bfd_link_hash_new here, because new
1452 undefined or common symbols will be added to the undefs list
1453 by _bfd_generic_link_add_one_symbol. Symbols may not be
1454 added twice to the undefs list. Also, if the new symbol is
1455 undefweak then we don't want to lose the strong undef. */
1456 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1458 h
->root
.type
= bfd_link_hash_undefined
;
1459 h
->root
.u
.undef
.abfd
= abfd
;
1463 h
->root
.type
= bfd_link_hash_new
;
1464 h
->root
.u
.undef
.abfd
= NULL
;
1467 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1469 /* If the new symbol is hidden or internal, completely undo
1470 any dynamic link state. */
1471 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1472 h
->forced_local
= 0;
1478 /* FIXME: Should we check type and size for protected symbol? */
1484 /* If a new weak symbol definition comes from a regular file and the
1485 old symbol comes from a dynamic library, we treat the new one as
1486 strong. Similarly, an old weak symbol definition from a regular
1487 file is treated as strong when the new symbol comes from a dynamic
1488 library. Further, an old weak symbol from a dynamic library is
1489 treated as strong if the new symbol is from a dynamic library.
1490 This reflects the way glibc's ld.so works.
1492 Also allow a weak symbol to override a linker script symbol
1493 defined by an early pass over the script. This is done so the
1494 linker knows the symbol is defined in an object file, for the
1495 DEFINED script function.
1497 Do this before setting *type_change_ok or *size_change_ok so that
1498 we warn properly when dynamic library symbols are overridden. */
1500 if (newdef
&& !newdyn
&& (olddyn
|| h
->root
.ldscript_def
))
1502 if (olddef
&& newdyn
)
1505 /* Allow changes between different types of function symbol. */
1506 if (newfunc
&& oldfunc
)
1507 *type_change_ok
= TRUE
;
1509 /* It's OK to change the type if either the existing symbol or the
1510 new symbol is weak. A type change is also OK if the old symbol
1511 is undefined and the new symbol is defined. */
1516 && h
->root
.type
== bfd_link_hash_undefined
))
1517 *type_change_ok
= TRUE
;
1519 /* It's OK to change the size if either the existing symbol or the
1520 new symbol is weak, or if the old symbol is undefined. */
1523 || h
->root
.type
== bfd_link_hash_undefined
)
1524 *size_change_ok
= TRUE
;
1526 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1527 symbol, respectively, appears to be a common symbol in a dynamic
1528 object. If a symbol appears in an uninitialized section, and is
1529 not weak, and is not a function, then it may be a common symbol
1530 which was resolved when the dynamic object was created. We want
1531 to treat such symbols specially, because they raise special
1532 considerations when setting the symbol size: if the symbol
1533 appears as a common symbol in a regular object, and the size in
1534 the regular object is larger, we must make sure that we use the
1535 larger size. This problematic case can always be avoided in C,
1536 but it must be handled correctly when using Fortran shared
1539 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1540 likewise for OLDDYNCOMMON and OLDDEF.
1542 Note that this test is just a heuristic, and that it is quite
1543 possible to have an uninitialized symbol in a shared object which
1544 is really a definition, rather than a common symbol. This could
1545 lead to some minor confusion when the symbol really is a common
1546 symbol in some regular object. However, I think it will be
1552 && (sec
->flags
& SEC_ALLOC
) != 0
1553 && (sec
->flags
& SEC_LOAD
) == 0
1556 newdyncommon
= TRUE
;
1558 newdyncommon
= FALSE
;
1562 && h
->root
.type
== bfd_link_hash_defined
1564 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1565 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1568 olddyncommon
= TRUE
;
1570 olddyncommon
= FALSE
;
1572 /* We now know everything about the old and new symbols. We ask the
1573 backend to check if we can merge them. */
1574 if (bed
->merge_symbol
!= NULL
)
1576 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1581 /* There are multiple definitions of a normal symbol. Skip the
1582 default symbol as well as definition from an IR object. */
1583 if (olddef
&& !olddyn
&& !oldweak
&& newdef
&& !newdyn
&& !newweak
1584 && !default_sym
&& h
->def_regular
1586 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1587 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1589 /* Handle a multiple definition. */
1590 (*info
->callbacks
->multiple_definition
) (info
, &h
->root
,
1591 abfd
, sec
, *pvalue
);
1596 /* If both the old and the new symbols look like common symbols in a
1597 dynamic object, set the size of the symbol to the larger of the
1602 && sym
->st_size
!= h
->size
)
1604 /* Since we think we have two common symbols, issue a multiple
1605 common warning if desired. Note that we only warn if the
1606 size is different. If the size is the same, we simply let
1607 the old symbol override the new one as normally happens with
1608 symbols defined in dynamic objects. */
1610 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1611 bfd_link_hash_common
, sym
->st_size
);
1612 if (sym
->st_size
> h
->size
)
1613 h
->size
= sym
->st_size
;
1615 *size_change_ok
= TRUE
;
1618 /* If we are looking at a dynamic object, and we have found a
1619 definition, we need to see if the symbol was already defined by
1620 some other object. If so, we want to use the existing
1621 definition, and we do not want to report a multiple symbol
1622 definition error; we do this by clobbering *PSEC to be
1623 bfd_und_section_ptr.
1625 We treat a common symbol as a definition if the symbol in the
1626 shared library is a function, since common symbols always
1627 represent variables; this can cause confusion in principle, but
1628 any such confusion would seem to indicate an erroneous program or
1629 shared library. We also permit a common symbol in a regular
1630 object to override a weak symbol in a shared object. */
1635 || (h
->root
.type
== bfd_link_hash_common
1636 && (newweak
|| newfunc
))))
1640 newdyncommon
= FALSE
;
1642 *psec
= sec
= bfd_und_section_ptr
;
1643 *size_change_ok
= TRUE
;
1645 /* If we get here when the old symbol is a common symbol, then
1646 we are explicitly letting it override a weak symbol or
1647 function in a dynamic object, and we don't want to warn about
1648 a type change. If the old symbol is a defined symbol, a type
1649 change warning may still be appropriate. */
1651 if (h
->root
.type
== bfd_link_hash_common
)
1652 *type_change_ok
= TRUE
;
1655 /* Handle the special case of an old common symbol merging with a
1656 new symbol which looks like a common symbol in a shared object.
1657 We change *PSEC and *PVALUE to make the new symbol look like a
1658 common symbol, and let _bfd_generic_link_add_one_symbol do the
1662 && h
->root
.type
== bfd_link_hash_common
)
1666 newdyncommon
= FALSE
;
1667 *pvalue
= sym
->st_size
;
1668 *psec
= sec
= bed
->common_section (oldsec
);
1669 *size_change_ok
= TRUE
;
1672 /* Skip weak definitions of symbols that are already defined. */
1673 if (newdef
&& olddef
&& newweak
)
1675 /* Don't skip new non-IR weak syms. */
1676 if (!(oldbfd
!= NULL
1677 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1678 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1684 /* Merge st_other. If the symbol already has a dynamic index,
1685 but visibility says it should not be visible, turn it into a
1687 elf_merge_st_other (abfd
, h
, sym
, sec
, newdef
, newdyn
);
1688 if (h
->dynindx
!= -1)
1689 switch (ELF_ST_VISIBILITY (h
->other
))
1693 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1698 /* If the old symbol is from a dynamic object, and the new symbol is
1699 a definition which is not from a dynamic object, then the new
1700 symbol overrides the old symbol. Symbols from regular files
1701 always take precedence over symbols from dynamic objects, even if
1702 they are defined after the dynamic object in the link.
1704 As above, we again permit a common symbol in a regular object to
1705 override a definition in a shared object if the shared object
1706 symbol is a function or is weak. */
1711 || (bfd_is_com_section (sec
)
1712 && (oldweak
|| oldfunc
)))
1717 /* Change the hash table entry to undefined, and let
1718 _bfd_generic_link_add_one_symbol do the right thing with the
1721 h
->root
.type
= bfd_link_hash_undefined
;
1722 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1723 *size_change_ok
= TRUE
;
1726 olddyncommon
= FALSE
;
1728 /* We again permit a type change when a common symbol may be
1729 overriding a function. */
1731 if (bfd_is_com_section (sec
))
1735 /* If a common symbol overrides a function, make sure
1736 that it isn't defined dynamically nor has type
1739 h
->type
= STT_NOTYPE
;
1741 *type_change_ok
= TRUE
;
1744 if (hi
->root
.type
== bfd_link_hash_indirect
)
1747 /* This union may have been set to be non-NULL when this symbol
1748 was seen in a dynamic object. We must force the union to be
1749 NULL, so that it is correct for a regular symbol. */
1750 h
->verinfo
.vertree
= NULL
;
1753 /* Handle the special case of a new common symbol merging with an
1754 old symbol that looks like it might be a common symbol defined in
1755 a shared object. Note that we have already handled the case in
1756 which a new common symbol should simply override the definition
1757 in the shared library. */
1760 && bfd_is_com_section (sec
)
1763 /* It would be best if we could set the hash table entry to a
1764 common symbol, but we don't know what to use for the section
1765 or the alignment. */
1766 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1767 bfd_link_hash_common
, sym
->st_size
);
1769 /* If the presumed common symbol in the dynamic object is
1770 larger, pretend that the new symbol has its size. */
1772 if (h
->size
> *pvalue
)
1775 /* We need to remember the alignment required by the symbol
1776 in the dynamic object. */
1777 BFD_ASSERT (pold_alignment
);
1778 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1781 olddyncommon
= FALSE
;
1783 h
->root
.type
= bfd_link_hash_undefined
;
1784 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1786 *size_change_ok
= TRUE
;
1787 *type_change_ok
= TRUE
;
1789 if (hi
->root
.type
== bfd_link_hash_indirect
)
1792 h
->verinfo
.vertree
= NULL
;
1797 /* Handle the case where we had a versioned symbol in a dynamic
1798 library and now find a definition in a normal object. In this
1799 case, we make the versioned symbol point to the normal one. */
1800 flip
->root
.type
= h
->root
.type
;
1801 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1802 h
->root
.type
= bfd_link_hash_indirect
;
1803 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1804 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1808 flip
->ref_dynamic
= 1;
1815 /* This function is called to create an indirect symbol from the
1816 default for the symbol with the default version if needed. The
1817 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1818 set DYNSYM if the new indirect symbol is dynamic. */
1821 _bfd_elf_add_default_symbol (bfd
*abfd
,
1822 struct bfd_link_info
*info
,
1823 struct elf_link_hash_entry
*h
,
1825 Elf_Internal_Sym
*sym
,
1829 bfd_boolean
*dynsym
)
1831 bfd_boolean type_change_ok
;
1832 bfd_boolean size_change_ok
;
1835 struct elf_link_hash_entry
*hi
;
1836 struct bfd_link_hash_entry
*bh
;
1837 const struct elf_backend_data
*bed
;
1838 bfd_boolean collect
;
1839 bfd_boolean dynamic
;
1840 bfd_boolean override
;
1842 size_t len
, shortlen
;
1844 bfd_boolean matched
;
1846 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1849 /* If this symbol has a version, and it is the default version, we
1850 create an indirect symbol from the default name to the fully
1851 decorated name. This will cause external references which do not
1852 specify a version to be bound to this version of the symbol. */
1853 p
= strchr (name
, ELF_VER_CHR
);
1854 if (h
->versioned
== unknown
)
1858 h
->versioned
= unversioned
;
1863 if (p
[1] != ELF_VER_CHR
)
1865 h
->versioned
= versioned_hidden
;
1869 h
->versioned
= versioned
;
1874 /* PR ld/19073: We may see an unversioned definition after the
1880 bed
= get_elf_backend_data (abfd
);
1881 collect
= bed
->collect
;
1882 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1884 shortlen
= p
- name
;
1885 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1886 if (shortname
== NULL
)
1888 memcpy (shortname
, name
, shortlen
);
1889 shortname
[shortlen
] = '\0';
1891 /* We are going to create a new symbol. Merge it with any existing
1892 symbol with this name. For the purposes of the merge, act as
1893 though we were defining the symbol we just defined, although we
1894 actually going to define an indirect symbol. */
1895 type_change_ok
= FALSE
;
1896 size_change_ok
= FALSE
;
1899 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1900 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1901 &type_change_ok
, &size_change_ok
, &matched
))
1907 if (hi
->def_regular
)
1909 /* If the undecorated symbol will have a version added by a
1910 script different to H, then don't indirect to/from the
1911 undecorated symbol. This isn't ideal because we may not yet
1912 have seen symbol versions, if given by a script on the
1913 command line rather than via --version-script. */
1914 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
1919 = bfd_find_version_for_sym (info
->version_info
,
1920 hi
->root
.root
.string
, &hide
);
1921 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
1923 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
1927 if (hi
->verinfo
.vertree
!= NULL
1928 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
1934 /* Add the default symbol if not performing a relocatable link. */
1935 if (! bfd_link_relocatable (info
))
1938 if (! (_bfd_generic_link_add_one_symbol
1939 (info
, abfd
, shortname
, BSF_INDIRECT
,
1940 bfd_ind_section_ptr
,
1941 0, name
, FALSE
, collect
, &bh
)))
1943 hi
= (struct elf_link_hash_entry
*) bh
;
1948 /* In this case the symbol named SHORTNAME is overriding the
1949 indirect symbol we want to add. We were planning on making
1950 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1951 is the name without a version. NAME is the fully versioned
1952 name, and it is the default version.
1954 Overriding means that we already saw a definition for the
1955 symbol SHORTNAME in a regular object, and it is overriding
1956 the symbol defined in the dynamic object.
1958 When this happens, we actually want to change NAME, the
1959 symbol we just added, to refer to SHORTNAME. This will cause
1960 references to NAME in the shared object to become references
1961 to SHORTNAME in the regular object. This is what we expect
1962 when we override a function in a shared object: that the
1963 references in the shared object will be mapped to the
1964 definition in the regular object. */
1966 while (hi
->root
.type
== bfd_link_hash_indirect
1967 || hi
->root
.type
== bfd_link_hash_warning
)
1968 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1970 h
->root
.type
= bfd_link_hash_indirect
;
1971 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1975 hi
->ref_dynamic
= 1;
1979 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1984 /* Now set HI to H, so that the following code will set the
1985 other fields correctly. */
1989 /* Check if HI is a warning symbol. */
1990 if (hi
->root
.type
== bfd_link_hash_warning
)
1991 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1993 /* If there is a duplicate definition somewhere, then HI may not
1994 point to an indirect symbol. We will have reported an error to
1995 the user in that case. */
1997 if (hi
->root
.type
== bfd_link_hash_indirect
)
1999 struct elf_link_hash_entry
*ht
;
2001 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2002 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
2004 /* A reference to the SHORTNAME symbol from a dynamic library
2005 will be satisfied by the versioned symbol at runtime. In
2006 effect, we have a reference to the versioned symbol. */
2007 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2008 hi
->dynamic_def
|= ht
->dynamic_def
;
2010 /* See if the new flags lead us to realize that the symbol must
2016 if (! bfd_link_executable (info
)
2023 if (hi
->ref_regular
)
2029 /* We also need to define an indirection from the nondefault version
2033 len
= strlen (name
);
2034 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
2035 if (shortname
== NULL
)
2037 memcpy (shortname
, name
, shortlen
);
2038 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
2040 /* Once again, merge with any existing symbol. */
2041 type_change_ok
= FALSE
;
2042 size_change_ok
= FALSE
;
2044 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
2045 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
2046 &type_change_ok
, &size_change_ok
, &matched
))
2054 /* Here SHORTNAME is a versioned name, so we don't expect to see
2055 the type of override we do in the case above unless it is
2056 overridden by a versioned definition. */
2057 if (hi
->root
.type
!= bfd_link_hash_defined
2058 && hi
->root
.type
!= bfd_link_hash_defweak
)
2060 /* xgettext:c-format */
2061 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2067 if (! (_bfd_generic_link_add_one_symbol
2068 (info
, abfd
, shortname
, BSF_INDIRECT
,
2069 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
2071 hi
= (struct elf_link_hash_entry
*) bh
;
2073 /* If there is a duplicate definition somewhere, then HI may not
2074 point to an indirect symbol. We will have reported an error
2075 to the user in that case. */
2077 if (hi
->root
.type
== bfd_link_hash_indirect
)
2079 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
2080 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2081 hi
->dynamic_def
|= h
->dynamic_def
;
2083 /* See if the new flags lead us to realize that the symbol
2089 if (! bfd_link_executable (info
)
2095 if (hi
->ref_regular
)
2105 /* This routine is used to export all defined symbols into the dynamic
2106 symbol table. It is called via elf_link_hash_traverse. */
2109 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
2111 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2113 /* Ignore indirect symbols. These are added by the versioning code. */
2114 if (h
->root
.type
== bfd_link_hash_indirect
)
2117 /* Ignore this if we won't export it. */
2118 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2121 if (h
->dynindx
== -1
2122 && (h
->def_regular
|| h
->ref_regular
)
2123 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2124 h
->root
.root
.string
))
2126 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2136 /* Look through the symbols which are defined in other shared
2137 libraries and referenced here. Update the list of version
2138 dependencies. This will be put into the .gnu.version_r section.
2139 This function is called via elf_link_hash_traverse. */
2142 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2145 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2146 Elf_Internal_Verneed
*t
;
2147 Elf_Internal_Vernaux
*a
;
2150 /* We only care about symbols defined in shared objects with version
2155 || h
->verinfo
.verdef
== NULL
2156 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2157 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2160 /* See if we already know about this version. */
2161 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2165 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2168 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2169 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2175 /* This is a new version. Add it to tree we are building. */
2180 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2183 rinfo
->failed
= TRUE
;
2187 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2188 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2189 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2193 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2196 rinfo
->failed
= TRUE
;
2200 /* Note that we are copying a string pointer here, and testing it
2201 above. If bfd_elf_string_from_elf_section is ever changed to
2202 discard the string data when low in memory, this will have to be
2204 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2206 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2207 a
->vna_nextptr
= t
->vn_auxptr
;
2209 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2212 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2219 /* Figure out appropriate versions for all the symbols. We may not
2220 have the version number script until we have read all of the input
2221 files, so until that point we don't know which symbols should be
2222 local. This function is called via elf_link_hash_traverse. */
2225 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2227 struct elf_info_failed
*sinfo
;
2228 struct bfd_link_info
*info
;
2229 const struct elf_backend_data
*bed
;
2230 struct elf_info_failed eif
;
2233 sinfo
= (struct elf_info_failed
*) data
;
2236 /* Fix the symbol flags. */
2239 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2242 sinfo
->failed
= TRUE
;
2246 /* We only need version numbers for symbols defined in regular
2248 if (!h
->def_regular
)
2251 bed
= get_elf_backend_data (info
->output_bfd
);
2252 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2253 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2255 struct bfd_elf_version_tree
*t
;
2258 if (*p
== ELF_VER_CHR
)
2261 /* If there is no version string, we can just return out. */
2265 /* Look for the version. If we find it, it is no longer weak. */
2266 for (t
= sinfo
->info
->version_info
; t
!= NULL
; t
= t
->next
)
2268 if (strcmp (t
->name
, p
) == 0)
2272 struct bfd_elf_version_expr
*d
;
2274 len
= p
- h
->root
.root
.string
;
2275 alc
= (char *) bfd_malloc (len
);
2278 sinfo
->failed
= TRUE
;
2281 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2282 alc
[len
- 1] = '\0';
2283 if (alc
[len
- 2] == ELF_VER_CHR
)
2284 alc
[len
- 2] = '\0';
2286 h
->verinfo
.vertree
= t
;
2290 if (t
->globals
.list
!= NULL
)
2291 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2293 /* See if there is anything to force this symbol to
2295 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2297 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2300 && ! info
->export_dynamic
)
2301 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2309 /* If we are building an application, we need to create a
2310 version node for this version. */
2311 if (t
== NULL
&& bfd_link_executable (info
))
2313 struct bfd_elf_version_tree
**pp
;
2316 /* If we aren't going to export this symbol, we don't need
2317 to worry about it. */
2318 if (h
->dynindx
== -1)
2321 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2325 sinfo
->failed
= TRUE
;
2330 t
->name_indx
= (unsigned int) -1;
2334 /* Don't count anonymous version tag. */
2335 if (sinfo
->info
->version_info
!= NULL
2336 && sinfo
->info
->version_info
->vernum
== 0)
2338 for (pp
= &sinfo
->info
->version_info
;
2342 t
->vernum
= version_index
;
2346 h
->verinfo
.vertree
= t
;
2350 /* We could not find the version for a symbol when
2351 generating a shared archive. Return an error. */
2353 /* xgettext:c-format */
2354 (_("%pB: version node not found for symbol %s"),
2355 info
->output_bfd
, h
->root
.root
.string
);
2356 bfd_set_error (bfd_error_bad_value
);
2357 sinfo
->failed
= TRUE
;
2362 /* If we don't have a version for this symbol, see if we can find
2364 if (h
->verinfo
.vertree
== NULL
&& sinfo
->info
->version_info
!= NULL
)
2369 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2370 h
->root
.root
.string
, &hide
);
2371 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2372 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2378 /* Read and swap the relocs from the section indicated by SHDR. This
2379 may be either a REL or a RELA section. The relocations are
2380 translated into RELA relocations and stored in INTERNAL_RELOCS,
2381 which should have already been allocated to contain enough space.
2382 The EXTERNAL_RELOCS are a buffer where the external form of the
2383 relocations should be stored.
2385 Returns FALSE if something goes wrong. */
2388 elf_link_read_relocs_from_section (bfd
*abfd
,
2390 Elf_Internal_Shdr
*shdr
,
2391 void *external_relocs
,
2392 Elf_Internal_Rela
*internal_relocs
)
2394 const struct elf_backend_data
*bed
;
2395 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2396 const bfd_byte
*erela
;
2397 const bfd_byte
*erelaend
;
2398 Elf_Internal_Rela
*irela
;
2399 Elf_Internal_Shdr
*symtab_hdr
;
2402 /* Position ourselves at the start of the section. */
2403 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2406 /* Read the relocations. */
2407 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2410 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2411 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2413 bed
= get_elf_backend_data (abfd
);
2415 /* Convert the external relocations to the internal format. */
2416 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2417 swap_in
= bed
->s
->swap_reloc_in
;
2418 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2419 swap_in
= bed
->s
->swap_reloca_in
;
2422 bfd_set_error (bfd_error_wrong_format
);
2426 erela
= (const bfd_byte
*) external_relocs
;
2427 erelaend
= erela
+ shdr
->sh_size
;
2428 irela
= internal_relocs
;
2429 while (erela
< erelaend
)
2433 (*swap_in
) (abfd
, erela
, irela
);
2434 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2435 if (bed
->s
->arch_size
== 64)
2439 if ((size_t) r_symndx
>= nsyms
)
2442 /* xgettext:c-format */
2443 (_("%pB: bad reloc symbol index (%#" PRIx64
" >= %#lx)"
2444 " for offset %#" PRIx64
" in section `%pA'"),
2445 abfd
, (uint64_t) r_symndx
, (unsigned long) nsyms
,
2446 (uint64_t) irela
->r_offset
, sec
);
2447 bfd_set_error (bfd_error_bad_value
);
2451 else if (r_symndx
!= STN_UNDEF
)
2454 /* xgettext:c-format */
2455 (_("%pB: non-zero symbol index (%#" PRIx64
")"
2456 " for offset %#" PRIx64
" in section `%pA'"
2457 " when the object file has no symbol table"),
2458 abfd
, (uint64_t) r_symndx
,
2459 (uint64_t) irela
->r_offset
, sec
);
2460 bfd_set_error (bfd_error_bad_value
);
2463 irela
+= bed
->s
->int_rels_per_ext_rel
;
2464 erela
+= shdr
->sh_entsize
;
2470 /* Read and swap the relocs for a section O. They may have been
2471 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2472 not NULL, they are used as buffers to read into. They are known to
2473 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2474 the return value is allocated using either malloc or bfd_alloc,
2475 according to the KEEP_MEMORY argument. If O has two relocation
2476 sections (both REL and RELA relocations), then the REL_HDR
2477 relocations will appear first in INTERNAL_RELOCS, followed by the
2478 RELA_HDR relocations. */
2481 _bfd_elf_link_read_relocs (bfd
*abfd
,
2483 void *external_relocs
,
2484 Elf_Internal_Rela
*internal_relocs
,
2485 bfd_boolean keep_memory
)
2487 void *alloc1
= NULL
;
2488 Elf_Internal_Rela
*alloc2
= NULL
;
2489 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2490 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2491 Elf_Internal_Rela
*internal_rela_relocs
;
2493 if (esdo
->relocs
!= NULL
)
2494 return esdo
->relocs
;
2496 if (o
->reloc_count
== 0)
2499 if (internal_relocs
== NULL
)
2503 size
= (bfd_size_type
) o
->reloc_count
* sizeof (Elf_Internal_Rela
);
2505 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2507 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2508 if (internal_relocs
== NULL
)
2512 if (external_relocs
== NULL
)
2514 bfd_size_type size
= 0;
2517 size
+= esdo
->rel
.hdr
->sh_size
;
2519 size
+= esdo
->rela
.hdr
->sh_size
;
2521 alloc1
= bfd_malloc (size
);
2524 external_relocs
= alloc1
;
2527 internal_rela_relocs
= internal_relocs
;
2530 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2534 external_relocs
= (((bfd_byte
*) external_relocs
)
2535 + esdo
->rel
.hdr
->sh_size
);
2536 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2537 * bed
->s
->int_rels_per_ext_rel
);
2541 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2543 internal_rela_relocs
)))
2546 /* Cache the results for next time, if we can. */
2548 esdo
->relocs
= internal_relocs
;
2553 /* Don't free alloc2, since if it was allocated we are passing it
2554 back (under the name of internal_relocs). */
2556 return internal_relocs
;
2564 bfd_release (abfd
, alloc2
);
2571 /* Compute the size of, and allocate space for, REL_HDR which is the
2572 section header for a section containing relocations for O. */
2575 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2576 struct bfd_elf_section_reloc_data
*reldata
)
2578 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2580 /* That allows us to calculate the size of the section. */
2581 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2583 /* The contents field must last into write_object_contents, so we
2584 allocate it with bfd_alloc rather than malloc. Also since we
2585 cannot be sure that the contents will actually be filled in,
2586 we zero the allocated space. */
2587 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2588 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2591 if (reldata
->hashes
== NULL
&& reldata
->count
)
2593 struct elf_link_hash_entry
**p
;
2595 p
= ((struct elf_link_hash_entry
**)
2596 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2600 reldata
->hashes
= p
;
2606 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2607 originated from the section given by INPUT_REL_HDR) to the
2611 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2612 asection
*input_section
,
2613 Elf_Internal_Shdr
*input_rel_hdr
,
2614 Elf_Internal_Rela
*internal_relocs
,
2615 struct elf_link_hash_entry
**rel_hash
2618 Elf_Internal_Rela
*irela
;
2619 Elf_Internal_Rela
*irelaend
;
2621 struct bfd_elf_section_reloc_data
*output_reldata
;
2622 asection
*output_section
;
2623 const struct elf_backend_data
*bed
;
2624 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2625 struct bfd_elf_section_data
*esdo
;
2627 output_section
= input_section
->output_section
;
2629 bed
= get_elf_backend_data (output_bfd
);
2630 esdo
= elf_section_data (output_section
);
2631 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2633 output_reldata
= &esdo
->rel
;
2634 swap_out
= bed
->s
->swap_reloc_out
;
2636 else if (esdo
->rela
.hdr
2637 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2639 output_reldata
= &esdo
->rela
;
2640 swap_out
= bed
->s
->swap_reloca_out
;
2645 /* xgettext:c-format */
2646 (_("%pB: relocation size mismatch in %pB section %pA"),
2647 output_bfd
, input_section
->owner
, input_section
);
2648 bfd_set_error (bfd_error_wrong_format
);
2652 erel
= output_reldata
->hdr
->contents
;
2653 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2654 irela
= internal_relocs
;
2655 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2656 * bed
->s
->int_rels_per_ext_rel
);
2657 while (irela
< irelaend
)
2659 (*swap_out
) (output_bfd
, irela
, erel
);
2660 irela
+= bed
->s
->int_rels_per_ext_rel
;
2661 erel
+= input_rel_hdr
->sh_entsize
;
2664 /* Bump the counter, so that we know where to add the next set of
2666 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2671 /* Make weak undefined symbols in PIE dynamic. */
2674 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2675 struct elf_link_hash_entry
*h
)
2677 if (bfd_link_pie (info
)
2679 && h
->root
.type
== bfd_link_hash_undefweak
)
2680 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2685 /* Fix up the flags for a symbol. This handles various cases which
2686 can only be fixed after all the input files are seen. This is
2687 currently called by both adjust_dynamic_symbol and
2688 assign_sym_version, which is unnecessary but perhaps more robust in
2689 the face of future changes. */
2692 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2693 struct elf_info_failed
*eif
)
2695 const struct elf_backend_data
*bed
;
2697 /* If this symbol was mentioned in a non-ELF file, try to set
2698 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2699 permit a non-ELF file to correctly refer to a symbol defined in
2700 an ELF dynamic object. */
2703 while (h
->root
.type
== bfd_link_hash_indirect
)
2704 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2706 if (h
->root
.type
!= bfd_link_hash_defined
2707 && h
->root
.type
!= bfd_link_hash_defweak
)
2710 h
->ref_regular_nonweak
= 1;
2714 if (h
->root
.u
.def
.section
->owner
!= NULL
2715 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2716 == bfd_target_elf_flavour
))
2719 h
->ref_regular_nonweak
= 1;
2725 if (h
->dynindx
== -1
2729 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2738 /* Unfortunately, NON_ELF is only correct if the symbol
2739 was first seen in a non-ELF file. Fortunately, if the symbol
2740 was first seen in an ELF file, we're probably OK unless the
2741 symbol was defined in a non-ELF file. Catch that case here.
2742 FIXME: We're still in trouble if the symbol was first seen in
2743 a dynamic object, and then later in a non-ELF regular object. */
2744 if ((h
->root
.type
== bfd_link_hash_defined
2745 || h
->root
.type
== bfd_link_hash_defweak
)
2747 && (h
->root
.u
.def
.section
->owner
!= NULL
2748 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2749 != bfd_target_elf_flavour
)
2750 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2751 && !h
->def_dynamic
)))
2755 /* Backend specific symbol fixup. */
2756 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2757 if (bed
->elf_backend_fixup_symbol
2758 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2761 /* If this is a final link, and the symbol was defined as a common
2762 symbol in a regular object file, and there was no definition in
2763 any dynamic object, then the linker will have allocated space for
2764 the symbol in a common section but the DEF_REGULAR
2765 flag will not have been set. */
2766 if (h
->root
.type
== bfd_link_hash_defined
2770 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
2773 /* If a weak undefined symbol has non-default visibility, we also
2774 hide it from the dynamic linker. */
2775 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2776 && h
->root
.type
== bfd_link_hash_undefweak
)
2777 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2779 /* A hidden versioned symbol in executable should be forced local if
2780 it is is locally defined, not referenced by shared library and not
2782 else if (bfd_link_executable (eif
->info
)
2783 && h
->versioned
== versioned_hidden
2784 && !eif
->info
->export_dynamic
2788 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2790 /* If -Bsymbolic was used (which means to bind references to global
2791 symbols to the definition within the shared object), and this
2792 symbol was defined in a regular object, then it actually doesn't
2793 need a PLT entry. Likewise, if the symbol has non-default
2794 visibility. If the symbol has hidden or internal visibility, we
2795 will force it local. */
2796 else if (h
->needs_plt
2797 && bfd_link_pic (eif
->info
)
2798 && is_elf_hash_table (eif
->info
->hash
)
2799 && (SYMBOLIC_BIND (eif
->info
, h
)
2800 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2803 bfd_boolean force_local
;
2805 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2806 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2807 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2810 /* If this is a weak defined symbol in a dynamic object, and we know
2811 the real definition in the dynamic object, copy interesting flags
2812 over to the real definition. */
2813 if (h
->is_weakalias
)
2815 struct elf_link_hash_entry
*def
= weakdef (h
);
2817 /* If the real definition is defined by a regular object file,
2818 don't do anything special. See the longer description in
2819 _bfd_elf_adjust_dynamic_symbol, below. */
2820 if (def
->def_regular
)
2823 while ((h
= h
->u
.alias
) != def
)
2824 h
->is_weakalias
= 0;
2828 while (h
->root
.type
== bfd_link_hash_indirect
)
2829 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2830 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2831 || h
->root
.type
== bfd_link_hash_defweak
);
2832 BFD_ASSERT (def
->def_dynamic
);
2833 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
2834 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, def
, h
);
2841 /* Make the backend pick a good value for a dynamic symbol. This is
2842 called via elf_link_hash_traverse, and also calls itself
2846 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2848 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2849 struct elf_link_hash_table
*htab
;
2850 const struct elf_backend_data
*bed
;
2852 if (! is_elf_hash_table (eif
->info
->hash
))
2855 /* Ignore indirect symbols. These are added by the versioning code. */
2856 if (h
->root
.type
== bfd_link_hash_indirect
)
2859 /* Fix the symbol flags. */
2860 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2863 htab
= elf_hash_table (eif
->info
);
2864 bed
= get_elf_backend_data (htab
->dynobj
);
2866 if (h
->root
.type
== bfd_link_hash_undefweak
)
2868 if (eif
->info
->dynamic_undefined_weak
== 0)
2869 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2870 else if (eif
->info
->dynamic_undefined_weak
> 0
2872 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2873 && !bfd_hide_sym_by_version (eif
->info
->version_info
,
2874 h
->root
.root
.string
))
2876 if (!bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2884 /* If this symbol does not require a PLT entry, and it is not
2885 defined by a dynamic object, or is not referenced by a regular
2886 object, ignore it. We do have to handle a weak defined symbol,
2887 even if no regular object refers to it, if we decided to add it
2888 to the dynamic symbol table. FIXME: Do we normally need to worry
2889 about symbols which are defined by one dynamic object and
2890 referenced by another one? */
2892 && h
->type
!= STT_GNU_IFUNC
2896 && (!h
->is_weakalias
|| weakdef (h
)->dynindx
== -1))))
2898 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2902 /* If we've already adjusted this symbol, don't do it again. This
2903 can happen via a recursive call. */
2904 if (h
->dynamic_adjusted
)
2907 /* Don't look at this symbol again. Note that we must set this
2908 after checking the above conditions, because we may look at a
2909 symbol once, decide not to do anything, and then get called
2910 recursively later after REF_REGULAR is set below. */
2911 h
->dynamic_adjusted
= 1;
2913 /* If this is a weak definition, and we know a real definition, and
2914 the real symbol is not itself defined by a regular object file,
2915 then get a good value for the real definition. We handle the
2916 real symbol first, for the convenience of the backend routine.
2918 Note that there is a confusing case here. If the real definition
2919 is defined by a regular object file, we don't get the real symbol
2920 from the dynamic object, but we do get the weak symbol. If the
2921 processor backend uses a COPY reloc, then if some routine in the
2922 dynamic object changes the real symbol, we will not see that
2923 change in the corresponding weak symbol. This is the way other
2924 ELF linkers work as well, and seems to be a result of the shared
2927 I will clarify this issue. Most SVR4 shared libraries define the
2928 variable _timezone and define timezone as a weak synonym. The
2929 tzset call changes _timezone. If you write
2930 extern int timezone;
2932 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2933 you might expect that, since timezone is a synonym for _timezone,
2934 the same number will print both times. However, if the processor
2935 backend uses a COPY reloc, then actually timezone will be copied
2936 into your process image, and, since you define _timezone
2937 yourself, _timezone will not. Thus timezone and _timezone will
2938 wind up at different memory locations. The tzset call will set
2939 _timezone, leaving timezone unchanged. */
2941 if (h
->is_weakalias
)
2943 struct elf_link_hash_entry
*def
= weakdef (h
);
2945 /* If we get to this point, there is an implicit reference to
2946 the alias by a regular object file via the weak symbol H. */
2947 def
->ref_regular
= 1;
2949 /* Ensure that the backend adjust_dynamic_symbol function sees
2950 the strong alias before H by recursively calling ourselves. */
2951 if (!_bfd_elf_adjust_dynamic_symbol (def
, eif
))
2955 /* If a symbol has no type and no size and does not require a PLT
2956 entry, then we are probably about to do the wrong thing here: we
2957 are probably going to create a COPY reloc for an empty object.
2958 This case can arise when a shared object is built with assembly
2959 code, and the assembly code fails to set the symbol type. */
2961 && h
->type
== STT_NOTYPE
2964 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2965 h
->root
.root
.string
);
2967 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2976 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2980 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
2981 struct elf_link_hash_entry
*h
,
2984 unsigned int power_of_two
;
2986 asection
*sec
= h
->root
.u
.def
.section
;
2988 /* The section alignment of the definition is the maximum alignment
2989 requirement of symbols defined in the section. Since we don't
2990 know the symbol alignment requirement, we start with the
2991 maximum alignment and check low bits of the symbol address
2992 for the minimum alignment. */
2993 power_of_two
= bfd_get_section_alignment (sec
->owner
, sec
);
2994 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
2995 while ((h
->root
.u
.def
.value
& mask
) != 0)
3001 if (power_of_two
> bfd_get_section_alignment (dynbss
->owner
,
3004 /* Adjust the section alignment if needed. */
3005 if (! bfd_set_section_alignment (dynbss
->owner
, dynbss
,
3010 /* We make sure that the symbol will be aligned properly. */
3011 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
3013 /* Define the symbol as being at this point in DYNBSS. */
3014 h
->root
.u
.def
.section
= dynbss
;
3015 h
->root
.u
.def
.value
= dynbss
->size
;
3017 /* Increment the size of DYNBSS to make room for the symbol. */
3018 dynbss
->size
+= h
->size
;
3020 /* No error if extern_protected_data is true. */
3021 if (h
->protected_def
3022 && (!info
->extern_protected_data
3023 || (info
->extern_protected_data
< 0
3024 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
3025 info
->callbacks
->einfo
3026 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3027 h
->root
.root
.string
);
3032 /* Adjust all external symbols pointing into SEC_MERGE sections
3033 to reflect the object merging within the sections. */
3036 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
3040 if ((h
->root
.type
== bfd_link_hash_defined
3041 || h
->root
.type
== bfd_link_hash_defweak
)
3042 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
3043 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
3045 bfd
*output_bfd
= (bfd
*) data
;
3047 h
->root
.u
.def
.value
=
3048 _bfd_merged_section_offset (output_bfd
,
3049 &h
->root
.u
.def
.section
,
3050 elf_section_data (sec
)->sec_info
,
3051 h
->root
.u
.def
.value
);
3057 /* Returns false if the symbol referred to by H should be considered
3058 to resolve local to the current module, and true if it should be
3059 considered to bind dynamically. */
3062 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
3063 struct bfd_link_info
*info
,
3064 bfd_boolean not_local_protected
)
3066 bfd_boolean binding_stays_local_p
;
3067 const struct elf_backend_data
*bed
;
3068 struct elf_link_hash_table
*hash_table
;
3073 while (h
->root
.type
== bfd_link_hash_indirect
3074 || h
->root
.type
== bfd_link_hash_warning
)
3075 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3077 /* If it was forced local, then clearly it's not dynamic. */
3078 if (h
->dynindx
== -1)
3080 if (h
->forced_local
)
3083 /* Identify the cases where name binding rules say that a
3084 visible symbol resolves locally. */
3085 binding_stays_local_p
= (bfd_link_executable (info
)
3086 || SYMBOLIC_BIND (info
, h
));
3088 switch (ELF_ST_VISIBILITY (h
->other
))
3095 hash_table
= elf_hash_table (info
);
3096 if (!is_elf_hash_table (hash_table
))
3099 bed
= get_elf_backend_data (hash_table
->dynobj
);
3101 /* Proper resolution for function pointer equality may require
3102 that these symbols perhaps be resolved dynamically, even though
3103 we should be resolving them to the current module. */
3104 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
3105 binding_stays_local_p
= TRUE
;
3112 /* If it isn't defined locally, then clearly it's dynamic. */
3113 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
3116 /* Otherwise, the symbol is dynamic if binding rules don't tell
3117 us that it remains local. */
3118 return !binding_stays_local_p
;
3121 /* Return true if the symbol referred to by H should be considered
3122 to resolve local to the current module, and false otherwise. Differs
3123 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3124 undefined symbols. The two functions are virtually identical except
3125 for the place where dynindx == -1 is tested. If that test is true,
3126 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3127 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3129 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3130 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3131 treatment of undefined weak symbols. For those that do not make
3132 undefined weak symbols dynamic, both functions may return false. */
3135 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3136 struct bfd_link_info
*info
,
3137 bfd_boolean local_protected
)
3139 const struct elf_backend_data
*bed
;
3140 struct elf_link_hash_table
*hash_table
;
3142 /* If it's a local sym, of course we resolve locally. */
3146 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3147 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3148 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3151 /* Forced local symbols resolve locally. */
3152 if (h
->forced_local
)
3155 /* Common symbols that become definitions don't get the DEF_REGULAR
3156 flag set, so test it first, and don't bail out. */
3157 if (ELF_COMMON_DEF_P (h
))
3159 /* If we don't have a definition in a regular file, then we can't
3160 resolve locally. The sym is either undefined or dynamic. */
3161 else if (!h
->def_regular
)
3164 /* Non-dynamic symbols resolve locally. */
3165 if (h
->dynindx
== -1)
3168 /* At this point, we know the symbol is defined and dynamic. In an
3169 executable it must resolve locally, likewise when building symbolic
3170 shared libraries. */
3171 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3174 /* Now deal with defined dynamic symbols in shared libraries. Ones
3175 with default visibility might not resolve locally. */
3176 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3179 hash_table
= elf_hash_table (info
);
3180 if (!is_elf_hash_table (hash_table
))
3183 bed
= get_elf_backend_data (hash_table
->dynobj
);
3185 /* If extern_protected_data is false, STV_PROTECTED non-function
3186 symbols are local. */
3187 if ((!info
->extern_protected_data
3188 || (info
->extern_protected_data
< 0
3189 && !bed
->extern_protected_data
))
3190 && !bed
->is_function_type (h
->type
))
3193 /* Function pointer equality tests may require that STV_PROTECTED
3194 symbols be treated as dynamic symbols. If the address of a
3195 function not defined in an executable is set to that function's
3196 plt entry in the executable, then the address of the function in
3197 a shared library must also be the plt entry in the executable. */
3198 return local_protected
;
3201 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3202 aligned. Returns the first TLS output section. */
3204 struct bfd_section
*
3205 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3207 struct bfd_section
*sec
, *tls
;
3208 unsigned int align
= 0;
3210 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3211 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3215 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3216 if (sec
->alignment_power
> align
)
3217 align
= sec
->alignment_power
;
3219 elf_hash_table (info
)->tls_sec
= tls
;
3221 /* Ensure the alignment of the first section is the largest alignment,
3222 so that the tls segment starts aligned. */
3224 tls
->alignment_power
= align
;
3229 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3231 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3232 Elf_Internal_Sym
*sym
)
3234 const struct elf_backend_data
*bed
;
3236 /* Local symbols do not count, but target specific ones might. */
3237 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3238 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3241 bed
= get_elf_backend_data (abfd
);
3242 /* Function symbols do not count. */
3243 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3246 /* If the section is undefined, then so is the symbol. */
3247 if (sym
->st_shndx
== SHN_UNDEF
)
3250 /* If the symbol is defined in the common section, then
3251 it is a common definition and so does not count. */
3252 if (bed
->common_definition (sym
))
3255 /* If the symbol is in a target specific section then we
3256 must rely upon the backend to tell us what it is. */
3257 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3258 /* FIXME - this function is not coded yet:
3260 return _bfd_is_global_symbol_definition (abfd, sym);
3262 Instead for now assume that the definition is not global,
3263 Even if this is wrong, at least the linker will behave
3264 in the same way that it used to do. */
3270 /* Search the symbol table of the archive element of the archive ABFD
3271 whose archive map contains a mention of SYMDEF, and determine if
3272 the symbol is defined in this element. */
3274 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3276 Elf_Internal_Shdr
* hdr
;
3280 Elf_Internal_Sym
*isymbuf
;
3281 Elf_Internal_Sym
*isym
;
3282 Elf_Internal_Sym
*isymend
;
3285 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
3289 if (! bfd_check_format (abfd
, bfd_object
))
3292 /* Select the appropriate symbol table. If we don't know if the
3293 object file is an IR object, give linker LTO plugin a chance to
3294 get the correct symbol table. */
3295 if (abfd
->plugin_format
== bfd_plugin_yes
3296 #if BFD_SUPPORTS_PLUGINS
3297 || (abfd
->plugin_format
== bfd_plugin_unknown
3298 && bfd_link_plugin_object_p (abfd
))
3302 /* Use the IR symbol table if the object has been claimed by
3304 abfd
= abfd
->plugin_dummy_bfd
;
3305 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3307 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3308 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3310 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3312 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3314 /* The sh_info field of the symtab header tells us where the
3315 external symbols start. We don't care about the local symbols. */
3316 if (elf_bad_symtab (abfd
))
3318 extsymcount
= symcount
;
3323 extsymcount
= symcount
- hdr
->sh_info
;
3324 extsymoff
= hdr
->sh_info
;
3327 if (extsymcount
== 0)
3330 /* Read in the symbol table. */
3331 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3333 if (isymbuf
== NULL
)
3336 /* Scan the symbol table looking for SYMDEF. */
3338 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3342 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3347 if (strcmp (name
, symdef
->name
) == 0)
3349 result
= is_global_data_symbol_definition (abfd
, isym
);
3359 /* Add an entry to the .dynamic table. */
3362 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3366 struct elf_link_hash_table
*hash_table
;
3367 const struct elf_backend_data
*bed
;
3369 bfd_size_type newsize
;
3370 bfd_byte
*newcontents
;
3371 Elf_Internal_Dyn dyn
;
3373 hash_table
= elf_hash_table (info
);
3374 if (! is_elf_hash_table (hash_table
))
3377 bed
= get_elf_backend_data (hash_table
->dynobj
);
3378 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3379 BFD_ASSERT (s
!= NULL
);
3381 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3382 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3383 if (newcontents
== NULL
)
3387 dyn
.d_un
.d_val
= val
;
3388 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3391 s
->contents
= newcontents
;
3396 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3397 otherwise just check whether one already exists. Returns -1 on error,
3398 1 if a DT_NEEDED tag already exists, and 0 on success. */
3401 elf_add_dt_needed_tag (bfd
*abfd
,
3402 struct bfd_link_info
*info
,
3406 struct elf_link_hash_table
*hash_table
;
3409 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3412 hash_table
= elf_hash_table (info
);
3413 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3414 if (strindex
== (size_t) -1)
3417 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3420 const struct elf_backend_data
*bed
;
3423 bed
= get_elf_backend_data (hash_table
->dynobj
);
3424 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3426 for (extdyn
= sdyn
->contents
;
3427 extdyn
< sdyn
->contents
+ sdyn
->size
;
3428 extdyn
+= bed
->s
->sizeof_dyn
)
3430 Elf_Internal_Dyn dyn
;
3432 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3433 if (dyn
.d_tag
== DT_NEEDED
3434 && dyn
.d_un
.d_val
== strindex
)
3436 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3444 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3447 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3451 /* We were just checking for existence of the tag. */
3452 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3457 /* Return true if SONAME is on the needed list between NEEDED and STOP
3458 (or the end of list if STOP is NULL), and needed by a library that
3462 on_needed_list (const char *soname
,
3463 struct bfd_link_needed_list
*needed
,
3464 struct bfd_link_needed_list
*stop
)
3466 struct bfd_link_needed_list
*look
;
3467 for (look
= needed
; look
!= stop
; look
= look
->next
)
3468 if (strcmp (soname
, look
->name
) == 0
3469 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3470 /* If needed by a library that itself is not directly
3471 needed, recursively check whether that library is
3472 indirectly needed. Since we add DT_NEEDED entries to
3473 the end of the list, library dependencies appear after
3474 the library. Therefore search prior to the current
3475 LOOK, preventing possible infinite recursion. */
3476 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3482 /* Sort symbol by value, section, and size. */
3484 elf_sort_symbol (const void *arg1
, const void *arg2
)
3486 const struct elf_link_hash_entry
*h1
;
3487 const struct elf_link_hash_entry
*h2
;
3488 bfd_signed_vma vdiff
;
3490 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3491 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3492 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3494 return vdiff
> 0 ? 1 : -1;
3497 int sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3499 return sdiff
> 0 ? 1 : -1;
3501 vdiff
= h1
->size
- h2
->size
;
3502 return vdiff
== 0 ? 0 : vdiff
> 0 ? 1 : -1;
3505 /* This function is used to adjust offsets into .dynstr for
3506 dynamic symbols. This is called via elf_link_hash_traverse. */
3509 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3511 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3513 if (h
->dynindx
!= -1)
3514 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3518 /* Assign string offsets in .dynstr, update all structures referencing
3522 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3524 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3525 struct elf_link_local_dynamic_entry
*entry
;
3526 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3527 bfd
*dynobj
= hash_table
->dynobj
;
3530 const struct elf_backend_data
*bed
;
3533 _bfd_elf_strtab_finalize (dynstr
);
3534 size
= _bfd_elf_strtab_size (dynstr
);
3536 bed
= get_elf_backend_data (dynobj
);
3537 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3538 BFD_ASSERT (sdyn
!= NULL
);
3540 /* Update all .dynamic entries referencing .dynstr strings. */
3541 for (extdyn
= sdyn
->contents
;
3542 extdyn
< sdyn
->contents
+ sdyn
->size
;
3543 extdyn
+= bed
->s
->sizeof_dyn
)
3545 Elf_Internal_Dyn dyn
;
3547 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3551 dyn
.d_un
.d_val
= size
;
3561 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3566 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3569 /* Now update local dynamic symbols. */
3570 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3571 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3572 entry
->isym
.st_name
);
3574 /* And the rest of dynamic symbols. */
3575 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3577 /* Adjust version definitions. */
3578 if (elf_tdata (output_bfd
)->cverdefs
)
3583 Elf_Internal_Verdef def
;
3584 Elf_Internal_Verdaux defaux
;
3586 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3590 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3592 p
+= sizeof (Elf_External_Verdef
);
3593 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3595 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3597 _bfd_elf_swap_verdaux_in (output_bfd
,
3598 (Elf_External_Verdaux
*) p
, &defaux
);
3599 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3601 _bfd_elf_swap_verdaux_out (output_bfd
,
3602 &defaux
, (Elf_External_Verdaux
*) p
);
3603 p
+= sizeof (Elf_External_Verdaux
);
3606 while (def
.vd_next
);
3609 /* Adjust version references. */
3610 if (elf_tdata (output_bfd
)->verref
)
3615 Elf_Internal_Verneed need
;
3616 Elf_Internal_Vernaux needaux
;
3618 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
3622 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3624 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3625 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3626 (Elf_External_Verneed
*) p
);
3627 p
+= sizeof (Elf_External_Verneed
);
3628 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3630 _bfd_elf_swap_vernaux_in (output_bfd
,
3631 (Elf_External_Vernaux
*) p
, &needaux
);
3632 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3634 _bfd_elf_swap_vernaux_out (output_bfd
,
3636 (Elf_External_Vernaux
*) p
);
3637 p
+= sizeof (Elf_External_Vernaux
);
3640 while (need
.vn_next
);
3646 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3647 The default is to only match when the INPUT and OUTPUT are exactly
3651 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3652 const bfd_target
*output
)
3654 return input
== output
;
3657 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3658 This version is used when different targets for the same architecture
3659 are virtually identical. */
3662 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3663 const bfd_target
*output
)
3665 const struct elf_backend_data
*obed
, *ibed
;
3667 if (input
== output
)
3670 ibed
= xvec_get_elf_backend_data (input
);
3671 obed
= xvec_get_elf_backend_data (output
);
3673 if (ibed
->arch
!= obed
->arch
)
3676 /* If both backends are using this function, deem them compatible. */
3677 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3680 /* Make a special call to the linker "notice" function to tell it that
3681 we are about to handle an as-needed lib, or have finished
3682 processing the lib. */
3685 _bfd_elf_notice_as_needed (bfd
*ibfd
,
3686 struct bfd_link_info
*info
,
3687 enum notice_asneeded_action act
)
3689 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
3692 /* Check relocations an ELF object file. */
3695 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
3697 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3698 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
3700 /* If this object is the same format as the output object, and it is
3701 not a shared library, then let the backend look through the
3704 This is required to build global offset table entries and to
3705 arrange for dynamic relocs. It is not required for the
3706 particular common case of linking non PIC code, even when linking
3707 against shared libraries, but unfortunately there is no way of
3708 knowing whether an object file has been compiled PIC or not.
3709 Looking through the relocs is not particularly time consuming.
3710 The problem is that we must either (1) keep the relocs in memory,
3711 which causes the linker to require additional runtime memory or
3712 (2) read the relocs twice from the input file, which wastes time.
3713 This would be a good case for using mmap.
3715 I have no idea how to handle linking PIC code into a file of a
3716 different format. It probably can't be done. */
3717 if ((abfd
->flags
& DYNAMIC
) == 0
3718 && is_elf_hash_table (htab
)
3719 && bed
->check_relocs
!= NULL
3720 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
3721 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
3725 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3727 Elf_Internal_Rela
*internal_relocs
;
3730 /* Don't check relocations in excluded sections. */
3731 if ((o
->flags
& SEC_RELOC
) == 0
3732 || (o
->flags
& SEC_EXCLUDE
) != 0
3733 || o
->reloc_count
== 0
3734 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
3735 && (o
->flags
& SEC_DEBUGGING
) != 0)
3736 || bfd_is_abs_section (o
->output_section
))
3739 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
3741 if (internal_relocs
== NULL
)
3744 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
3746 if (elf_section_data (o
)->relocs
!= internal_relocs
)
3747 free (internal_relocs
);
3757 /* Add symbols from an ELF object file to the linker hash table. */
3760 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3762 Elf_Internal_Ehdr
*ehdr
;
3763 Elf_Internal_Shdr
*hdr
;
3767 struct elf_link_hash_entry
**sym_hash
;
3768 bfd_boolean dynamic
;
3769 Elf_External_Versym
*extversym
= NULL
;
3770 Elf_External_Versym
*ever
;
3771 struct elf_link_hash_entry
*weaks
;
3772 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3773 size_t nondeflt_vers_cnt
= 0;
3774 Elf_Internal_Sym
*isymbuf
= NULL
;
3775 Elf_Internal_Sym
*isym
;
3776 Elf_Internal_Sym
*isymend
;
3777 const struct elf_backend_data
*bed
;
3778 bfd_boolean add_needed
;
3779 struct elf_link_hash_table
*htab
;
3781 void *alloc_mark
= NULL
;
3782 struct bfd_hash_entry
**old_table
= NULL
;
3783 unsigned int old_size
= 0;
3784 unsigned int old_count
= 0;
3785 void *old_tab
= NULL
;
3787 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3788 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3789 void *old_strtab
= NULL
;
3792 bfd_boolean just_syms
;
3794 htab
= elf_hash_table (info
);
3795 bed
= get_elf_backend_data (abfd
);
3797 if ((abfd
->flags
& DYNAMIC
) == 0)
3803 /* You can't use -r against a dynamic object. Also, there's no
3804 hope of using a dynamic object which does not exactly match
3805 the format of the output file. */
3806 if (bfd_link_relocatable (info
)
3807 || !is_elf_hash_table (htab
)
3808 || info
->output_bfd
->xvec
!= abfd
->xvec
)
3810 if (bfd_link_relocatable (info
))
3811 bfd_set_error (bfd_error_invalid_operation
);
3813 bfd_set_error (bfd_error_wrong_format
);
3818 ehdr
= elf_elfheader (abfd
);
3819 if (info
->warn_alternate_em
3820 && bed
->elf_machine_code
!= ehdr
->e_machine
3821 && ((bed
->elf_machine_alt1
!= 0
3822 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
3823 || (bed
->elf_machine_alt2
!= 0
3824 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
3826 /* xgettext:c-format */
3827 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
3828 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
3830 /* As a GNU extension, any input sections which are named
3831 .gnu.warning.SYMBOL are treated as warning symbols for the given
3832 symbol. This differs from .gnu.warning sections, which generate
3833 warnings when they are included in an output file. */
3834 /* PR 12761: Also generate this warning when building shared libraries. */
3835 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3839 name
= bfd_get_section_name (abfd
, s
);
3840 if (CONST_STRNEQ (name
, ".gnu.warning."))
3845 name
+= sizeof ".gnu.warning." - 1;
3847 /* If this is a shared object, then look up the symbol
3848 in the hash table. If it is there, and it is already
3849 been defined, then we will not be using the entry
3850 from this shared object, so we don't need to warn.
3851 FIXME: If we see the definition in a regular object
3852 later on, we will warn, but we shouldn't. The only
3853 fix is to keep track of what warnings we are supposed
3854 to emit, and then handle them all at the end of the
3858 struct elf_link_hash_entry
*h
;
3860 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3862 /* FIXME: What about bfd_link_hash_common? */
3864 && (h
->root
.type
== bfd_link_hash_defined
3865 || h
->root
.type
== bfd_link_hash_defweak
))
3870 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
3874 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3879 if (! (_bfd_generic_link_add_one_symbol
3880 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3881 FALSE
, bed
->collect
, NULL
)))
3884 if (bfd_link_executable (info
))
3886 /* Clobber the section size so that the warning does
3887 not get copied into the output file. */
3890 /* Also set SEC_EXCLUDE, so that symbols defined in
3891 the warning section don't get copied to the output. */
3892 s
->flags
|= SEC_EXCLUDE
;
3897 just_syms
= ((s
= abfd
->sections
) != NULL
3898 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
3903 /* If we are creating a shared library, create all the dynamic
3904 sections immediately. We need to attach them to something,
3905 so we attach them to this BFD, provided it is the right
3906 format and is not from ld --just-symbols. Always create the
3907 dynamic sections for -E/--dynamic-list. FIXME: If there
3908 are no input BFD's of the same format as the output, we can't
3909 make a shared library. */
3911 && (bfd_link_pic (info
)
3912 || (!bfd_link_relocatable (info
)
3914 && (info
->export_dynamic
|| info
->dynamic
)))
3915 && is_elf_hash_table (htab
)
3916 && info
->output_bfd
->xvec
== abfd
->xvec
3917 && !htab
->dynamic_sections_created
)
3919 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3923 else if (!is_elf_hash_table (htab
))
3927 const char *soname
= NULL
;
3929 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3930 const Elf_Internal_Phdr
*phdr
;
3933 /* ld --just-symbols and dynamic objects don't mix very well.
3934 ld shouldn't allow it. */
3938 /* If this dynamic lib was specified on the command line with
3939 --as-needed in effect, then we don't want to add a DT_NEEDED
3940 tag unless the lib is actually used. Similary for libs brought
3941 in by another lib's DT_NEEDED. When --no-add-needed is used
3942 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3943 any dynamic library in DT_NEEDED tags in the dynamic lib at
3945 add_needed
= (elf_dyn_lib_class (abfd
)
3946 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3947 | DYN_NO_NEEDED
)) == 0;
3949 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3954 unsigned int elfsec
;
3955 unsigned long shlink
;
3957 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3964 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3965 if (elfsec
== SHN_BAD
)
3966 goto error_free_dyn
;
3967 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3969 for (extdyn
= dynbuf
;
3970 extdyn
< dynbuf
+ s
->size
;
3971 extdyn
+= bed
->s
->sizeof_dyn
)
3973 Elf_Internal_Dyn dyn
;
3975 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3976 if (dyn
.d_tag
== DT_SONAME
)
3978 unsigned int tagv
= dyn
.d_un
.d_val
;
3979 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3981 goto error_free_dyn
;
3983 if (dyn
.d_tag
== DT_NEEDED
)
3985 struct bfd_link_needed_list
*n
, **pn
;
3987 unsigned int tagv
= dyn
.d_un
.d_val
;
3989 amt
= sizeof (struct bfd_link_needed_list
);
3990 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3991 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3992 if (n
== NULL
|| fnm
== NULL
)
3993 goto error_free_dyn
;
3994 amt
= strlen (fnm
) + 1;
3995 anm
= (char *) bfd_alloc (abfd
, amt
);
3997 goto error_free_dyn
;
3998 memcpy (anm
, fnm
, amt
);
4002 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4006 if (dyn
.d_tag
== DT_RUNPATH
)
4008 struct bfd_link_needed_list
*n
, **pn
;
4010 unsigned int tagv
= dyn
.d_un
.d_val
;
4012 amt
= sizeof (struct bfd_link_needed_list
);
4013 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4014 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4015 if (n
== NULL
|| fnm
== NULL
)
4016 goto error_free_dyn
;
4017 amt
= strlen (fnm
) + 1;
4018 anm
= (char *) bfd_alloc (abfd
, amt
);
4020 goto error_free_dyn
;
4021 memcpy (anm
, fnm
, amt
);
4025 for (pn
= & runpath
;
4031 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4032 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
4034 struct bfd_link_needed_list
*n
, **pn
;
4036 unsigned int tagv
= dyn
.d_un
.d_val
;
4038 amt
= sizeof (struct bfd_link_needed_list
);
4039 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4040 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4041 if (n
== NULL
|| fnm
== NULL
)
4042 goto error_free_dyn
;
4043 amt
= strlen (fnm
) + 1;
4044 anm
= (char *) bfd_alloc (abfd
, amt
);
4046 goto error_free_dyn
;
4047 memcpy (anm
, fnm
, amt
);
4057 if (dyn
.d_tag
== DT_AUDIT
)
4059 unsigned int tagv
= dyn
.d_un
.d_val
;
4060 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4067 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4068 frees all more recently bfd_alloc'd blocks as well. */
4074 struct bfd_link_needed_list
**pn
;
4075 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4080 /* If we have a PT_GNU_RELRO program header, mark as read-only
4081 all sections contained fully therein. This makes relro
4082 shared library sections appear as they will at run-time. */
4083 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4084 while (--phdr
>= elf_tdata (abfd
)->phdr
)
4085 if (phdr
->p_type
== PT_GNU_RELRO
)
4087 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4088 if ((s
->flags
& SEC_ALLOC
) != 0
4089 && s
->vma
>= phdr
->p_vaddr
4090 && s
->vma
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4091 s
->flags
|= SEC_READONLY
;
4095 /* We do not want to include any of the sections in a dynamic
4096 object in the output file. We hack by simply clobbering the
4097 list of sections in the BFD. This could be handled more
4098 cleanly by, say, a new section flag; the existing
4099 SEC_NEVER_LOAD flag is not the one we want, because that one
4100 still implies that the section takes up space in the output
4102 bfd_section_list_clear (abfd
);
4104 /* Find the name to use in a DT_NEEDED entry that refers to this
4105 object. If the object has a DT_SONAME entry, we use it.
4106 Otherwise, if the generic linker stuck something in
4107 elf_dt_name, we use that. Otherwise, we just use the file
4109 if (soname
== NULL
|| *soname
== '\0')
4111 soname
= elf_dt_name (abfd
);
4112 if (soname
== NULL
|| *soname
== '\0')
4113 soname
= bfd_get_filename (abfd
);
4116 /* Save the SONAME because sometimes the linker emulation code
4117 will need to know it. */
4118 elf_dt_name (abfd
) = soname
;
4120 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4124 /* If we have already included this dynamic object in the
4125 link, just ignore it. There is no reason to include a
4126 particular dynamic object more than once. */
4130 /* Save the DT_AUDIT entry for the linker emulation code. */
4131 elf_dt_audit (abfd
) = audit
;
4134 /* If this is a dynamic object, we always link against the .dynsym
4135 symbol table, not the .symtab symbol table. The dynamic linker
4136 will only see the .dynsym symbol table, so there is no reason to
4137 look at .symtab for a dynamic object. */
4139 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4140 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4142 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4144 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4146 /* The sh_info field of the symtab header tells us where the
4147 external symbols start. We don't care about the local symbols at
4149 if (elf_bad_symtab (abfd
))
4151 extsymcount
= symcount
;
4156 extsymcount
= symcount
- hdr
->sh_info
;
4157 extsymoff
= hdr
->sh_info
;
4160 sym_hash
= elf_sym_hashes (abfd
);
4161 if (extsymcount
!= 0)
4163 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4165 if (isymbuf
== NULL
)
4168 if (sym_hash
== NULL
)
4170 /* We store a pointer to the hash table entry for each
4173 amt
*= sizeof (struct elf_link_hash_entry
*);
4174 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4175 if (sym_hash
== NULL
)
4176 goto error_free_sym
;
4177 elf_sym_hashes (abfd
) = sym_hash
;
4183 /* Read in any version definitions. */
4184 if (!_bfd_elf_slurp_version_tables (abfd
,
4185 info
->default_imported_symver
))
4186 goto error_free_sym
;
4188 /* Read in the symbol versions, but don't bother to convert them
4189 to internal format. */
4190 if (elf_dynversym (abfd
) != 0)
4192 Elf_Internal_Shdr
*versymhdr
;
4194 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4195 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
4196 if (extversym
== NULL
)
4197 goto error_free_sym
;
4198 amt
= versymhdr
->sh_size
;
4199 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
4200 || bfd_bread (extversym
, amt
, abfd
) != amt
)
4201 goto error_free_vers
;
4205 /* If we are loading an as-needed shared lib, save the symbol table
4206 state before we start adding symbols. If the lib turns out
4207 to be unneeded, restore the state. */
4208 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4213 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4215 struct bfd_hash_entry
*p
;
4216 struct elf_link_hash_entry
*h
;
4218 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4220 h
= (struct elf_link_hash_entry
*) p
;
4221 entsize
+= htab
->root
.table
.entsize
;
4222 if (h
->root
.type
== bfd_link_hash_warning
)
4223 entsize
+= htab
->root
.table
.entsize
;
4227 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4228 old_tab
= bfd_malloc (tabsize
+ entsize
);
4229 if (old_tab
== NULL
)
4230 goto error_free_vers
;
4232 /* Remember the current objalloc pointer, so that all mem for
4233 symbols added can later be reclaimed. */
4234 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4235 if (alloc_mark
== NULL
)
4236 goto error_free_vers
;
4238 /* Make a special call to the linker "notice" function to
4239 tell it that we are about to handle an as-needed lib. */
4240 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4241 goto error_free_vers
;
4243 /* Clone the symbol table. Remember some pointers into the
4244 symbol table, and dynamic symbol count. */
4245 old_ent
= (char *) old_tab
+ tabsize
;
4246 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4247 old_undefs
= htab
->root
.undefs
;
4248 old_undefs_tail
= htab
->root
.undefs_tail
;
4249 old_table
= htab
->root
.table
.table
;
4250 old_size
= htab
->root
.table
.size
;
4251 old_count
= htab
->root
.table
.count
;
4252 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4253 if (old_strtab
== NULL
)
4254 goto error_free_vers
;
4256 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4258 struct bfd_hash_entry
*p
;
4259 struct elf_link_hash_entry
*h
;
4261 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4263 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
4264 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4265 h
= (struct elf_link_hash_entry
*) p
;
4266 if (h
->root
.type
== bfd_link_hash_warning
)
4268 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
4269 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4276 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
4277 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
4279 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4283 asection
*sec
, *new_sec
;
4286 struct elf_link_hash_entry
*h
;
4287 struct elf_link_hash_entry
*hi
;
4288 bfd_boolean definition
;
4289 bfd_boolean size_change_ok
;
4290 bfd_boolean type_change_ok
;
4291 bfd_boolean new_weak
;
4292 bfd_boolean old_weak
;
4293 bfd_boolean override
;
4295 bfd_boolean discarded
;
4296 unsigned int old_alignment
;
4298 bfd_boolean matched
;
4302 flags
= BSF_NO_FLAGS
;
4304 value
= isym
->st_value
;
4305 common
= bed
->common_definition (isym
);
4306 if (common
&& info
->inhibit_common_definition
)
4308 /* Treat common symbol as undefined for --no-define-common. */
4309 isym
->st_shndx
= SHN_UNDEF
;
4314 bind
= ELF_ST_BIND (isym
->st_info
);
4318 /* This should be impossible, since ELF requires that all
4319 global symbols follow all local symbols, and that sh_info
4320 point to the first global symbol. Unfortunately, Irix 5
4325 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4333 case STB_GNU_UNIQUE
:
4334 flags
= BSF_GNU_UNIQUE
;
4338 /* Leave it up to the processor backend. */
4342 if (isym
->st_shndx
== SHN_UNDEF
)
4343 sec
= bfd_und_section_ptr
;
4344 else if (isym
->st_shndx
== SHN_ABS
)
4345 sec
= bfd_abs_section_ptr
;
4346 else if (isym
->st_shndx
== SHN_COMMON
)
4348 sec
= bfd_com_section_ptr
;
4349 /* What ELF calls the size we call the value. What ELF
4350 calls the value we call the alignment. */
4351 value
= isym
->st_size
;
4355 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4357 sec
= bfd_abs_section_ptr
;
4358 else if (discarded_section (sec
))
4360 /* Symbols from discarded section are undefined. We keep
4362 sec
= bfd_und_section_ptr
;
4364 isym
->st_shndx
= SHN_UNDEF
;
4366 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4370 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4373 goto error_free_vers
;
4375 if (isym
->st_shndx
== SHN_COMMON
4376 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4378 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4382 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4384 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4386 goto error_free_vers
;
4390 else if (isym
->st_shndx
== SHN_COMMON
4391 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4392 && !bfd_link_relocatable (info
))
4394 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4398 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4399 | SEC_LINKER_CREATED
);
4400 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4402 goto error_free_vers
;
4406 else if (bed
->elf_add_symbol_hook
)
4408 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4410 goto error_free_vers
;
4412 /* The hook function sets the name to NULL if this symbol
4413 should be skipped for some reason. */
4418 /* Sanity check that all possibilities were handled. */
4421 bfd_set_error (bfd_error_bad_value
);
4422 goto error_free_vers
;
4425 /* Silently discard TLS symbols from --just-syms. There's
4426 no way to combine a static TLS block with a new TLS block
4427 for this executable. */
4428 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4429 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4432 if (bfd_is_und_section (sec
)
4433 || bfd_is_com_section (sec
))
4438 size_change_ok
= FALSE
;
4439 type_change_ok
= bed
->type_change_ok
;
4446 if (is_elf_hash_table (htab
))
4448 Elf_Internal_Versym iver
;
4449 unsigned int vernum
= 0;
4454 if (info
->default_imported_symver
)
4455 /* Use the default symbol version created earlier. */
4456 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4461 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4463 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4465 /* If this is a hidden symbol, or if it is not version
4466 1, we append the version name to the symbol name.
4467 However, we do not modify a non-hidden absolute symbol
4468 if it is not a function, because it might be the version
4469 symbol itself. FIXME: What if it isn't? */
4470 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4472 && (!bfd_is_abs_section (sec
)
4473 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4476 size_t namelen
, verlen
, newlen
;
4479 if (isym
->st_shndx
!= SHN_UNDEF
)
4481 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4483 else if (vernum
> 1)
4485 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4492 /* xgettext:c-format */
4493 (_("%pB: %s: invalid version %u (max %d)"),
4495 elf_tdata (abfd
)->cverdefs
);
4496 bfd_set_error (bfd_error_bad_value
);
4497 goto error_free_vers
;
4502 /* We cannot simply test for the number of
4503 entries in the VERNEED section since the
4504 numbers for the needed versions do not start
4506 Elf_Internal_Verneed
*t
;
4509 for (t
= elf_tdata (abfd
)->verref
;
4513 Elf_Internal_Vernaux
*a
;
4515 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4517 if (a
->vna_other
== vernum
)
4519 verstr
= a
->vna_nodename
;
4529 /* xgettext:c-format */
4530 (_("%pB: %s: invalid needed version %d"),
4531 abfd
, name
, vernum
);
4532 bfd_set_error (bfd_error_bad_value
);
4533 goto error_free_vers
;
4537 namelen
= strlen (name
);
4538 verlen
= strlen (verstr
);
4539 newlen
= namelen
+ verlen
+ 2;
4540 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4541 && isym
->st_shndx
!= SHN_UNDEF
)
4544 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4545 if (newname
== NULL
)
4546 goto error_free_vers
;
4547 memcpy (newname
, name
, namelen
);
4548 p
= newname
+ namelen
;
4550 /* If this is a defined non-hidden version symbol,
4551 we add another @ to the name. This indicates the
4552 default version of the symbol. */
4553 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4554 && isym
->st_shndx
!= SHN_UNDEF
)
4556 memcpy (p
, verstr
, verlen
+ 1);
4561 /* If this symbol has default visibility and the user has
4562 requested we not re-export it, then mark it as hidden. */
4563 if (!bfd_is_und_section (sec
)
4566 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4567 isym
->st_other
= (STV_HIDDEN
4568 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4570 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4571 sym_hash
, &old_bfd
, &old_weak
,
4572 &old_alignment
, &skip
, &override
,
4573 &type_change_ok
, &size_change_ok
,
4575 goto error_free_vers
;
4580 /* Override a definition only if the new symbol matches the
4582 if (override
&& matched
)
4586 while (h
->root
.type
== bfd_link_hash_indirect
4587 || h
->root
.type
== bfd_link_hash_warning
)
4588 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4590 if (elf_tdata (abfd
)->verdef
!= NULL
4593 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4596 if (! (_bfd_generic_link_add_one_symbol
4597 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4598 (struct bfd_link_hash_entry
**) sym_hash
)))
4599 goto error_free_vers
;
4601 if ((flags
& BSF_GNU_UNIQUE
)
4602 && (abfd
->flags
& DYNAMIC
) == 0
4603 && bfd_get_flavour (info
->output_bfd
) == bfd_target_elf_flavour
)
4604 elf_tdata (info
->output_bfd
)->has_gnu_symbols
|= elf_gnu_symbol_unique
;
4607 /* We need to make sure that indirect symbol dynamic flags are
4610 while (h
->root
.type
== bfd_link_hash_indirect
4611 || h
->root
.type
== bfd_link_hash_warning
)
4612 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4614 /* Setting the index to -3 tells elf_link_output_extsym that
4615 this symbol is defined in a discarded section. */
4621 new_weak
= (flags
& BSF_WEAK
) != 0;
4625 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4626 && is_elf_hash_table (htab
)
4627 && h
->u
.alias
== NULL
)
4629 /* Keep a list of all weak defined non function symbols from
4630 a dynamic object, using the alias field. Later in this
4631 function we will set the alias field to the correct
4632 value. We only put non-function symbols from dynamic
4633 objects on this list, because that happens to be the only
4634 time we need to know the normal symbol corresponding to a
4635 weak symbol, and the information is time consuming to
4636 figure out. If the alias field is not already NULL,
4637 then this symbol was already defined by some previous
4638 dynamic object, and we will be using that previous
4639 definition anyhow. */
4645 /* Set the alignment of a common symbol. */
4646 if ((common
|| bfd_is_com_section (sec
))
4647 && h
->root
.type
== bfd_link_hash_common
)
4652 align
= bfd_log2 (isym
->st_value
);
4655 /* The new symbol is a common symbol in a shared object.
4656 We need to get the alignment from the section. */
4657 align
= new_sec
->alignment_power
;
4659 if (align
> old_alignment
)
4660 h
->root
.u
.c
.p
->alignment_power
= align
;
4662 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4665 if (is_elf_hash_table (htab
))
4667 /* Set a flag in the hash table entry indicating the type of
4668 reference or definition we just found. A dynamic symbol
4669 is one which is referenced or defined by both a regular
4670 object and a shared object. */
4671 bfd_boolean dynsym
= FALSE
;
4673 /* Plugin symbols aren't normal. Don't set def_regular or
4674 ref_regular for them, or make them dynamic. */
4675 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4682 if (bind
!= STB_WEAK
)
4683 h
->ref_regular_nonweak
= 1;
4695 /* If the indirect symbol has been forced local, don't
4696 make the real symbol dynamic. */
4697 if ((h
== hi
|| !hi
->forced_local
)
4698 && (bfd_link_dll (info
)
4708 hi
->ref_dynamic
= 1;
4713 hi
->def_dynamic
= 1;
4716 /* If the indirect symbol has been forced local, don't
4717 make the real symbol dynamic. */
4718 if ((h
== hi
|| !hi
->forced_local
)
4722 && weakdef (h
)->dynindx
!= -1)))
4726 /* Check to see if we need to add an indirect symbol for
4727 the default name. */
4729 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
4730 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4731 sec
, value
, &old_bfd
, &dynsym
))
4732 goto error_free_vers
;
4734 /* Check the alignment when a common symbol is involved. This
4735 can change when a common symbol is overridden by a normal
4736 definition or a common symbol is ignored due to the old
4737 normal definition. We need to make sure the maximum
4738 alignment is maintained. */
4739 if ((old_alignment
|| common
)
4740 && h
->root
.type
!= bfd_link_hash_common
)
4742 unsigned int common_align
;
4743 unsigned int normal_align
;
4744 unsigned int symbol_align
;
4748 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
4749 || h
->root
.type
== bfd_link_hash_defweak
);
4751 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4752 if (h
->root
.u
.def
.section
->owner
!= NULL
4753 && (h
->root
.u
.def
.section
->owner
->flags
4754 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
4756 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4757 if (normal_align
> symbol_align
)
4758 normal_align
= symbol_align
;
4761 normal_align
= symbol_align
;
4765 common_align
= old_alignment
;
4766 common_bfd
= old_bfd
;
4771 common_align
= bfd_log2 (isym
->st_value
);
4773 normal_bfd
= old_bfd
;
4776 if (normal_align
< common_align
)
4778 /* PR binutils/2735 */
4779 if (normal_bfd
== NULL
)
4781 /* xgettext:c-format */
4782 (_("warning: alignment %u of common symbol `%s' in %pB is"
4783 " greater than the alignment (%u) of its section %pA"),
4784 1 << common_align
, name
, common_bfd
,
4785 1 << normal_align
, h
->root
.u
.def
.section
);
4788 /* xgettext:c-format */
4789 (_("warning: alignment %u of symbol `%s' in %pB"
4790 " is smaller than %u in %pB"),
4791 1 << normal_align
, name
, normal_bfd
,
4792 1 << common_align
, common_bfd
);
4796 /* Remember the symbol size if it isn't undefined. */
4797 if (isym
->st_size
!= 0
4798 && isym
->st_shndx
!= SHN_UNDEF
4799 && (definition
|| h
->size
== 0))
4802 && h
->size
!= isym
->st_size
4803 && ! size_change_ok
)
4805 /* xgettext:c-format */
4806 (_("warning: size of symbol `%s' changed"
4807 " from %" PRIu64
" in %pB to %" PRIu64
" in %pB"),
4808 name
, (uint64_t) h
->size
, old_bfd
,
4809 (uint64_t) isym
->st_size
, abfd
);
4811 h
->size
= isym
->st_size
;
4814 /* If this is a common symbol, then we always want H->SIZE
4815 to be the size of the common symbol. The code just above
4816 won't fix the size if a common symbol becomes larger. We
4817 don't warn about a size change here, because that is
4818 covered by --warn-common. Allow changes between different
4820 if (h
->root
.type
== bfd_link_hash_common
)
4821 h
->size
= h
->root
.u
.c
.size
;
4823 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4824 && ((definition
&& !new_weak
)
4825 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
4826 || h
->type
== STT_NOTYPE
))
4828 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
4830 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4832 if (type
== STT_GNU_IFUNC
4833 && (abfd
->flags
& DYNAMIC
) != 0)
4836 if (h
->type
!= type
)
4838 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
4839 /* xgettext:c-format */
4841 (_("warning: type of symbol `%s' changed"
4842 " from %d to %d in %pB"),
4843 name
, h
->type
, type
, abfd
);
4849 /* Merge st_other field. */
4850 elf_merge_st_other (abfd
, h
, isym
, sec
, definition
, dynamic
);
4852 /* We don't want to make debug symbol dynamic. */
4854 && (sec
->flags
& SEC_DEBUGGING
)
4855 && !bfd_link_relocatable (info
))
4858 /* Nor should we make plugin symbols dynamic. */
4859 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4864 h
->target_internal
= isym
->st_target_internal
;
4865 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
4868 if (definition
&& !dynamic
)
4870 char *p
= strchr (name
, ELF_VER_CHR
);
4871 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4873 /* Queue non-default versions so that .symver x, x@FOO
4874 aliases can be checked. */
4877 amt
= ((isymend
- isym
+ 1)
4878 * sizeof (struct elf_link_hash_entry
*));
4880 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
4882 goto error_free_vers
;
4884 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4888 if (dynsym
&& h
->dynindx
== -1)
4890 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4891 goto error_free_vers
;
4893 && weakdef (h
)->dynindx
== -1)
4895 if (!bfd_elf_link_record_dynamic_symbol (info
, weakdef (h
)))
4896 goto error_free_vers
;
4899 else if (h
->dynindx
!= -1)
4900 /* If the symbol already has a dynamic index, but
4901 visibility says it should not be visible, turn it into
4903 switch (ELF_ST_VISIBILITY (h
->other
))
4907 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4912 /* Don't add DT_NEEDED for references from the dummy bfd nor
4913 for unmatched symbol. */
4918 && h
->ref_regular_nonweak
4920 || (old_bfd
->flags
& BFD_PLUGIN
) == 0))
4921 || (h
->ref_dynamic_nonweak
4922 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
4923 && !on_needed_list (elf_dt_name (abfd
),
4924 htab
->needed
, NULL
))))
4927 const char *soname
= elf_dt_name (abfd
);
4929 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
4930 h
->root
.root
.string
);
4932 /* A symbol from a library loaded via DT_NEEDED of some
4933 other library is referenced by a regular object.
4934 Add a DT_NEEDED entry for it. Issue an error if
4935 --no-add-needed is used and the reference was not
4938 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4941 /* xgettext:c-format */
4942 (_("%pB: undefined reference to symbol '%s'"),
4944 bfd_set_error (bfd_error_missing_dso
);
4945 goto error_free_vers
;
4948 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
4949 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
4952 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4954 goto error_free_vers
;
4956 BFD_ASSERT (ret
== 0);
4961 if (info
->lto_plugin_active
4962 && !bfd_link_relocatable (info
)
4963 && (abfd
->flags
& BFD_PLUGIN
) == 0
4969 if (bed
->s
->arch_size
== 32)
4974 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
4975 referenced in regular objects so that linker plugin will get
4976 the correct symbol resolution. */
4978 sym_hash
= elf_sym_hashes (abfd
);
4979 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4981 Elf_Internal_Rela
*internal_relocs
;
4982 Elf_Internal_Rela
*rel
, *relend
;
4984 /* Don't check relocations in excluded sections. */
4985 if ((s
->flags
& SEC_RELOC
) == 0
4986 || s
->reloc_count
== 0
4987 || (s
->flags
& SEC_EXCLUDE
) != 0
4988 || ((info
->strip
== strip_all
4989 || info
->strip
== strip_debugger
)
4990 && (s
->flags
& SEC_DEBUGGING
) != 0))
4993 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, s
, NULL
,
4996 if (internal_relocs
== NULL
)
4997 goto error_free_vers
;
4999 rel
= internal_relocs
;
5000 relend
= rel
+ s
->reloc_count
;
5001 for ( ; rel
< relend
; rel
++)
5003 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
5004 struct elf_link_hash_entry
*h
;
5006 /* Skip local symbols. */
5007 if (r_symndx
< extsymoff
)
5010 h
= sym_hash
[r_symndx
- extsymoff
];
5012 h
->root
.non_ir_ref_regular
= 1;
5015 if (elf_section_data (s
)->relocs
!= internal_relocs
)
5016 free (internal_relocs
);
5020 if (extversym
!= NULL
)
5026 if (isymbuf
!= NULL
)
5032 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
5036 /* Restore the symbol table. */
5037 old_ent
= (char *) old_tab
+ tabsize
;
5038 memset (elf_sym_hashes (abfd
), 0,
5039 extsymcount
* sizeof (struct elf_link_hash_entry
*));
5040 htab
->root
.table
.table
= old_table
;
5041 htab
->root
.table
.size
= old_size
;
5042 htab
->root
.table
.count
= old_count
;
5043 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
5044 htab
->root
.undefs
= old_undefs
;
5045 htab
->root
.undefs_tail
= old_undefs_tail
;
5046 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
5049 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
5051 struct bfd_hash_entry
*p
;
5052 struct elf_link_hash_entry
*h
;
5054 unsigned int alignment_power
;
5055 unsigned int non_ir_ref_dynamic
;
5057 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
5059 h
= (struct elf_link_hash_entry
*) p
;
5060 if (h
->root
.type
== bfd_link_hash_warning
)
5061 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5063 /* Preserve the maximum alignment and size for common
5064 symbols even if this dynamic lib isn't on DT_NEEDED
5065 since it can still be loaded at run time by another
5067 if (h
->root
.type
== bfd_link_hash_common
)
5069 size
= h
->root
.u
.c
.size
;
5070 alignment_power
= h
->root
.u
.c
.p
->alignment_power
;
5075 alignment_power
= 0;
5077 /* Preserve non_ir_ref_dynamic so that this symbol
5078 will be exported when the dynamic lib becomes needed
5079 in the second pass. */
5080 non_ir_ref_dynamic
= h
->root
.non_ir_ref_dynamic
;
5081 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
5082 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5083 h
= (struct elf_link_hash_entry
*) p
;
5084 if (h
->root
.type
== bfd_link_hash_warning
)
5086 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
5087 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5088 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5090 if (h
->root
.type
== bfd_link_hash_common
)
5092 if (size
> h
->root
.u
.c
.size
)
5093 h
->root
.u
.c
.size
= size
;
5094 if (alignment_power
> h
->root
.u
.c
.p
->alignment_power
)
5095 h
->root
.u
.c
.p
->alignment_power
= alignment_power
;
5097 h
->root
.non_ir_ref_dynamic
= non_ir_ref_dynamic
;
5101 /* Make a special call to the linker "notice" function to
5102 tell it that symbols added for crefs may need to be removed. */
5103 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
5104 goto error_free_vers
;
5107 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
5109 if (nondeflt_vers
!= NULL
)
5110 free (nondeflt_vers
);
5114 if (old_tab
!= NULL
)
5116 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
5117 goto error_free_vers
;
5122 /* Now that all the symbols from this input file are created, if
5123 not performing a relocatable link, handle .symver foo, foo@BAR
5124 such that any relocs against foo become foo@BAR. */
5125 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5129 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5131 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5132 char *shortname
, *p
;
5134 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5136 || (h
->root
.type
!= bfd_link_hash_defined
5137 && h
->root
.type
!= bfd_link_hash_defweak
))
5140 amt
= p
- h
->root
.root
.string
;
5141 shortname
= (char *) bfd_malloc (amt
+ 1);
5143 goto error_free_vers
;
5144 memcpy (shortname
, h
->root
.root
.string
, amt
);
5145 shortname
[amt
] = '\0';
5147 hi
= (struct elf_link_hash_entry
*)
5148 bfd_link_hash_lookup (&htab
->root
, shortname
,
5149 FALSE
, FALSE
, FALSE
);
5151 && hi
->root
.type
== h
->root
.type
5152 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5153 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5155 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
5156 hi
->root
.type
= bfd_link_hash_indirect
;
5157 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5158 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5159 sym_hash
= elf_sym_hashes (abfd
);
5161 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5162 if (sym_hash
[symidx
] == hi
)
5164 sym_hash
[symidx
] = h
;
5170 free (nondeflt_vers
);
5171 nondeflt_vers
= NULL
;
5174 /* Now set the alias field correctly for all the weak defined
5175 symbols we found. The only way to do this is to search all the
5176 symbols. Since we only need the information for non functions in
5177 dynamic objects, that's the only time we actually put anything on
5178 the list WEAKS. We need this information so that if a regular
5179 object refers to a symbol defined weakly in a dynamic object, the
5180 real symbol in the dynamic object is also put in the dynamic
5181 symbols; we also must arrange for both symbols to point to the
5182 same memory location. We could handle the general case of symbol
5183 aliasing, but a general symbol alias can only be generated in
5184 assembler code, handling it correctly would be very time
5185 consuming, and other ELF linkers don't handle general aliasing
5189 struct elf_link_hash_entry
**hpp
;
5190 struct elf_link_hash_entry
**hppend
;
5191 struct elf_link_hash_entry
**sorted_sym_hash
;
5192 struct elf_link_hash_entry
*h
;
5195 /* Since we have to search the whole symbol list for each weak
5196 defined symbol, search time for N weak defined symbols will be
5197 O(N^2). Binary search will cut it down to O(NlogN). */
5199 amt
*= sizeof (struct elf_link_hash_entry
*);
5200 sorted_sym_hash
= (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5201 if (sorted_sym_hash
== NULL
)
5203 sym_hash
= sorted_sym_hash
;
5204 hpp
= elf_sym_hashes (abfd
);
5205 hppend
= hpp
+ extsymcount
;
5207 for (; hpp
< hppend
; hpp
++)
5211 && h
->root
.type
== bfd_link_hash_defined
5212 && !bed
->is_function_type (h
->type
))
5220 qsort (sorted_sym_hash
, sym_count
,
5221 sizeof (struct elf_link_hash_entry
*),
5224 while (weaks
!= NULL
)
5226 struct elf_link_hash_entry
*hlook
;
5229 size_t i
, j
, idx
= 0;
5232 weaks
= hlook
->u
.alias
;
5233 hlook
->u
.alias
= NULL
;
5235 if (hlook
->root
.type
!= bfd_link_hash_defined
5236 && hlook
->root
.type
!= bfd_link_hash_defweak
)
5239 slook
= hlook
->root
.u
.def
.section
;
5240 vlook
= hlook
->root
.u
.def
.value
;
5246 bfd_signed_vma vdiff
;
5248 h
= sorted_sym_hash
[idx
];
5249 vdiff
= vlook
- h
->root
.u
.def
.value
;
5256 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5266 /* We didn't find a value/section match. */
5270 /* With multiple aliases, or when the weak symbol is already
5271 strongly defined, we have multiple matching symbols and
5272 the binary search above may land on any of them. Step
5273 one past the matching symbol(s). */
5276 h
= sorted_sym_hash
[idx
];
5277 if (h
->root
.u
.def
.section
!= slook
5278 || h
->root
.u
.def
.value
!= vlook
)
5282 /* Now look back over the aliases. Since we sorted by size
5283 as well as value and section, we'll choose the one with
5284 the largest size. */
5287 h
= sorted_sym_hash
[idx
];
5289 /* Stop if value or section doesn't match. */
5290 if (h
->root
.u
.def
.section
!= slook
5291 || h
->root
.u
.def
.value
!= vlook
)
5293 else if (h
!= hlook
)
5295 struct elf_link_hash_entry
*t
;
5298 hlook
->is_weakalias
= 1;
5300 if (t
->u
.alias
!= NULL
)
5301 while (t
->u
.alias
!= h
)
5305 /* If the weak definition is in the list of dynamic
5306 symbols, make sure the real definition is put
5308 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5310 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5313 free (sorted_sym_hash
);
5318 /* If the real definition is in the list of dynamic
5319 symbols, make sure the weak definition is put
5320 there as well. If we don't do this, then the
5321 dynamic loader might not merge the entries for the
5322 real definition and the weak definition. */
5323 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5325 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5326 goto err_free_sym_hash
;
5333 free (sorted_sym_hash
);
5336 if (bed
->check_directives
5337 && !(*bed
->check_directives
) (abfd
, info
))
5340 /* If this is a non-traditional link, try to optimize the handling
5341 of the .stab/.stabstr sections. */
5343 && ! info
->traditional_format
5344 && is_elf_hash_table (htab
)
5345 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5349 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5350 if (stabstr
!= NULL
)
5352 bfd_size_type string_offset
= 0;
5355 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5356 if (CONST_STRNEQ (stab
->name
, ".stab")
5357 && (!stab
->name
[5] ||
5358 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5359 && (stab
->flags
& SEC_MERGE
) == 0
5360 && !bfd_is_abs_section (stab
->output_section
))
5362 struct bfd_elf_section_data
*secdata
;
5364 secdata
= elf_section_data (stab
);
5365 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5366 stabstr
, &secdata
->sec_info
,
5369 if (secdata
->sec_info
)
5370 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5375 if (is_elf_hash_table (htab
) && add_needed
)
5377 /* Add this bfd to the loaded list. */
5378 struct elf_link_loaded_list
*n
;
5380 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5384 n
->next
= htab
->loaded
;
5391 if (old_tab
!= NULL
)
5393 if (old_strtab
!= NULL
)
5395 if (nondeflt_vers
!= NULL
)
5396 free (nondeflt_vers
);
5397 if (extversym
!= NULL
)
5400 if (isymbuf
!= NULL
)
5406 /* Return the linker hash table entry of a symbol that might be
5407 satisfied by an archive symbol. Return -1 on error. */
5409 struct elf_link_hash_entry
*
5410 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5411 struct bfd_link_info
*info
,
5414 struct elf_link_hash_entry
*h
;
5418 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, TRUE
);
5422 /* If this is a default version (the name contains @@), look up the
5423 symbol again with only one `@' as well as without the version.
5424 The effect is that references to the symbol with and without the
5425 version will be matched by the default symbol in the archive. */
5427 p
= strchr (name
, ELF_VER_CHR
);
5428 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5431 /* First check with only one `@'. */
5432 len
= strlen (name
);
5433 copy
= (char *) bfd_alloc (abfd
, len
);
5435 return (struct elf_link_hash_entry
*) -1;
5437 first
= p
- name
+ 1;
5438 memcpy (copy
, name
, first
);
5439 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5441 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, TRUE
);
5444 /* We also need to check references to the symbol without the
5446 copy
[first
- 1] = '\0';
5447 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
5448 FALSE
, FALSE
, TRUE
);
5451 bfd_release (abfd
, copy
);
5455 /* Add symbols from an ELF archive file to the linker hash table. We
5456 don't use _bfd_generic_link_add_archive_symbols because we need to
5457 handle versioned symbols.
5459 Fortunately, ELF archive handling is simpler than that done by
5460 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5461 oddities. In ELF, if we find a symbol in the archive map, and the
5462 symbol is currently undefined, we know that we must pull in that
5465 Unfortunately, we do have to make multiple passes over the symbol
5466 table until nothing further is resolved. */
5469 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5472 unsigned char *included
= NULL
;
5476 const struct elf_backend_data
*bed
;
5477 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
5478 (bfd
*, struct bfd_link_info
*, const char *);
5480 if (! bfd_has_map (abfd
))
5482 /* An empty archive is a special case. */
5483 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5485 bfd_set_error (bfd_error_no_armap
);
5489 /* Keep track of all symbols we know to be already defined, and all
5490 files we know to be already included. This is to speed up the
5491 second and subsequent passes. */
5492 c
= bfd_ardata (abfd
)->symdef_count
;
5496 amt
*= sizeof (*included
);
5497 included
= (unsigned char *) bfd_zmalloc (amt
);
5498 if (included
== NULL
)
5501 symdefs
= bfd_ardata (abfd
)->symdefs
;
5502 bed
= get_elf_backend_data (abfd
);
5503 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5516 symdefend
= symdef
+ c
;
5517 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5519 struct elf_link_hash_entry
*h
;
5521 struct bfd_link_hash_entry
*undefs_tail
;
5526 if (symdef
->file_offset
== last
)
5532 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5533 if (h
== (struct elf_link_hash_entry
*) -1)
5539 if (h
->root
.type
== bfd_link_hash_common
)
5541 /* We currently have a common symbol. The archive map contains
5542 a reference to this symbol, so we may want to include it. We
5543 only want to include it however, if this archive element
5544 contains a definition of the symbol, not just another common
5547 Unfortunately some archivers (including GNU ar) will put
5548 declarations of common symbols into their archive maps, as
5549 well as real definitions, so we cannot just go by the archive
5550 map alone. Instead we must read in the element's symbol
5551 table and check that to see what kind of symbol definition
5553 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5556 else if (h
->root
.type
!= bfd_link_hash_undefined
)
5558 if (h
->root
.type
!= bfd_link_hash_undefweak
)
5559 /* Symbol must be defined. Don't check it again. */
5564 /* We need to include this archive member. */
5565 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5566 if (element
== NULL
)
5569 if (! bfd_check_format (element
, bfd_object
))
5572 undefs_tail
= info
->hash
->undefs_tail
;
5574 if (!(*info
->callbacks
5575 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5577 if (!bfd_link_add_symbols (element
, info
))
5580 /* If there are any new undefined symbols, we need to make
5581 another pass through the archive in order to see whether
5582 they can be defined. FIXME: This isn't perfect, because
5583 common symbols wind up on undefs_tail and because an
5584 undefined symbol which is defined later on in this pass
5585 does not require another pass. This isn't a bug, but it
5586 does make the code less efficient than it could be. */
5587 if (undefs_tail
!= info
->hash
->undefs_tail
)
5590 /* Look backward to mark all symbols from this object file
5591 which we have already seen in this pass. */
5595 included
[mark
] = TRUE
;
5600 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5602 /* We mark subsequent symbols from this object file as we go
5603 on through the loop. */
5604 last
= symdef
->file_offset
;
5614 if (included
!= NULL
)
5619 /* Given an ELF BFD, add symbols to the global hash table as
5623 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5625 switch (bfd_get_format (abfd
))
5628 return elf_link_add_object_symbols (abfd
, info
);
5630 return elf_link_add_archive_symbols (abfd
, info
);
5632 bfd_set_error (bfd_error_wrong_format
);
5637 struct hash_codes_info
5639 unsigned long *hashcodes
;
5643 /* This function will be called though elf_link_hash_traverse to store
5644 all hash value of the exported symbols in an array. */
5647 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5649 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
5654 /* Ignore indirect symbols. These are added by the versioning code. */
5655 if (h
->dynindx
== -1)
5658 name
= h
->root
.root
.string
;
5659 if (h
->versioned
>= versioned
)
5661 char *p
= strchr (name
, ELF_VER_CHR
);
5664 alc
= (char *) bfd_malloc (p
- name
+ 1);
5670 memcpy (alc
, name
, p
- name
);
5671 alc
[p
- name
] = '\0';
5676 /* Compute the hash value. */
5677 ha
= bfd_elf_hash (name
);
5679 /* Store the found hash value in the array given as the argument. */
5680 *(inf
->hashcodes
)++ = ha
;
5682 /* And store it in the struct so that we can put it in the hash table
5684 h
->u
.elf_hash_value
= ha
;
5692 struct collect_gnu_hash_codes
5695 const struct elf_backend_data
*bed
;
5696 unsigned long int nsyms
;
5697 unsigned long int maskbits
;
5698 unsigned long int *hashcodes
;
5699 unsigned long int *hashval
;
5700 unsigned long int *indx
;
5701 unsigned long int *counts
;
5704 long int min_dynindx
;
5705 unsigned long int bucketcount
;
5706 unsigned long int symindx
;
5707 long int local_indx
;
5708 long int shift1
, shift2
;
5709 unsigned long int mask
;
5713 /* This function will be called though elf_link_hash_traverse to store
5714 all hash value of the exported symbols in an array. */
5717 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5719 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5724 /* Ignore indirect symbols. These are added by the versioning code. */
5725 if (h
->dynindx
== -1)
5728 /* Ignore also local symbols and undefined symbols. */
5729 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5732 name
= h
->root
.root
.string
;
5733 if (h
->versioned
>= versioned
)
5735 char *p
= strchr (name
, ELF_VER_CHR
);
5738 alc
= (char *) bfd_malloc (p
- name
+ 1);
5744 memcpy (alc
, name
, p
- name
);
5745 alc
[p
- name
] = '\0';
5750 /* Compute the hash value. */
5751 ha
= bfd_elf_gnu_hash (name
);
5753 /* Store the found hash value in the array for compute_bucket_count,
5754 and also for .dynsym reordering purposes. */
5755 s
->hashcodes
[s
->nsyms
] = ha
;
5756 s
->hashval
[h
->dynindx
] = ha
;
5758 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5759 s
->min_dynindx
= h
->dynindx
;
5767 /* This function will be called though elf_link_hash_traverse to do
5768 final dynaminc symbol renumbering. */
5771 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5773 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5774 unsigned long int bucket
;
5775 unsigned long int val
;
5777 /* Ignore indirect symbols. */
5778 if (h
->dynindx
== -1)
5781 /* Ignore also local symbols and undefined symbols. */
5782 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5784 if (h
->dynindx
>= s
->min_dynindx
)
5785 h
->dynindx
= s
->local_indx
++;
5789 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5790 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5791 & ((s
->maskbits
>> s
->shift1
) - 1);
5792 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5794 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5795 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5796 if (s
->counts
[bucket
] == 1)
5797 /* Last element terminates the chain. */
5799 bfd_put_32 (s
->output_bfd
, val
,
5800 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5801 --s
->counts
[bucket
];
5802 h
->dynindx
= s
->indx
[bucket
]++;
5806 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5809 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5811 return !(h
->forced_local
5812 || h
->root
.type
== bfd_link_hash_undefined
5813 || h
->root
.type
== bfd_link_hash_undefweak
5814 || ((h
->root
.type
== bfd_link_hash_defined
5815 || h
->root
.type
== bfd_link_hash_defweak
)
5816 && h
->root
.u
.def
.section
->output_section
== NULL
));
5819 /* Array used to determine the number of hash table buckets to use
5820 based on the number of symbols there are. If there are fewer than
5821 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5822 fewer than 37 we use 17 buckets, and so forth. We never use more
5823 than 32771 buckets. */
5825 static const size_t elf_buckets
[] =
5827 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5831 /* Compute bucket count for hashing table. We do not use a static set
5832 of possible tables sizes anymore. Instead we determine for all
5833 possible reasonable sizes of the table the outcome (i.e., the
5834 number of collisions etc) and choose the best solution. The
5835 weighting functions are not too simple to allow the table to grow
5836 without bounds. Instead one of the weighting factors is the size.
5837 Therefore the result is always a good payoff between few collisions
5838 (= short chain lengths) and table size. */
5840 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
5841 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
5842 unsigned long int nsyms
,
5845 size_t best_size
= 0;
5846 unsigned long int i
;
5848 /* We have a problem here. The following code to optimize the table
5849 size requires an integer type with more the 32 bits. If
5850 BFD_HOST_U_64_BIT is set we know about such a type. */
5851 #ifdef BFD_HOST_U_64_BIT
5856 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5857 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5858 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5859 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5860 unsigned long int *counts
;
5862 unsigned int no_improvement_count
= 0;
5864 /* Possible optimization parameters: if we have NSYMS symbols we say
5865 that the hashing table must at least have NSYMS/4 and at most
5867 minsize
= nsyms
/ 4;
5870 best_size
= maxsize
= nsyms
* 2;
5875 if ((best_size
& 31) == 0)
5879 /* Create array where we count the collisions in. We must use bfd_malloc
5880 since the size could be large. */
5882 amt
*= sizeof (unsigned long int);
5883 counts
= (unsigned long int *) bfd_malloc (amt
);
5887 /* Compute the "optimal" size for the hash table. The criteria is a
5888 minimal chain length. The minor criteria is (of course) the size
5890 for (i
= minsize
; i
< maxsize
; ++i
)
5892 /* Walk through the array of hashcodes and count the collisions. */
5893 BFD_HOST_U_64_BIT max
;
5894 unsigned long int j
;
5895 unsigned long int fact
;
5897 if (gnu_hash
&& (i
& 31) == 0)
5900 memset (counts
, '\0', i
* sizeof (unsigned long int));
5902 /* Determine how often each hash bucket is used. */
5903 for (j
= 0; j
< nsyms
; ++j
)
5904 ++counts
[hashcodes
[j
] % i
];
5906 /* For the weight function we need some information about the
5907 pagesize on the target. This is information need not be 100%
5908 accurate. Since this information is not available (so far) we
5909 define it here to a reasonable default value. If it is crucial
5910 to have a better value some day simply define this value. */
5911 # ifndef BFD_TARGET_PAGESIZE
5912 # define BFD_TARGET_PAGESIZE (4096)
5915 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5917 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5920 /* Variant 1: optimize for short chains. We add the squares
5921 of all the chain lengths (which favors many small chain
5922 over a few long chains). */
5923 for (j
= 0; j
< i
; ++j
)
5924 max
+= counts
[j
] * counts
[j
];
5926 /* This adds penalties for the overall size of the table. */
5927 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5930 /* Variant 2: Optimize a lot more for small table. Here we
5931 also add squares of the size but we also add penalties for
5932 empty slots (the +1 term). */
5933 for (j
= 0; j
< i
; ++j
)
5934 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5936 /* The overall size of the table is considered, but not as
5937 strong as in variant 1, where it is squared. */
5938 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5942 /* Compare with current best results. */
5943 if (max
< best_chlen
)
5947 no_improvement_count
= 0;
5949 /* PR 11843: Avoid futile long searches for the best bucket size
5950 when there are a large number of symbols. */
5951 else if (++no_improvement_count
== 100)
5958 #endif /* defined (BFD_HOST_U_64_BIT) */
5960 /* This is the fallback solution if no 64bit type is available or if we
5961 are not supposed to spend much time on optimizations. We select the
5962 bucket count using a fixed set of numbers. */
5963 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5965 best_size
= elf_buckets
[i
];
5966 if (nsyms
< elf_buckets
[i
+ 1])
5969 if (gnu_hash
&& best_size
< 2)
5976 /* Size any SHT_GROUP section for ld -r. */
5979 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
5984 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
5985 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
5986 && (s
= ibfd
->sections
) != NULL
5987 && s
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
5988 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
5993 /* Set a default stack segment size. The value in INFO wins. If it
5994 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5995 undefined it is initialized. */
5998 bfd_elf_stack_segment_size (bfd
*output_bfd
,
5999 struct bfd_link_info
*info
,
6000 const char *legacy_symbol
,
6001 bfd_vma default_size
)
6003 struct elf_link_hash_entry
*h
= NULL
;
6005 /* Look for legacy symbol. */
6007 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
6008 FALSE
, FALSE
, FALSE
);
6009 if (h
&& (h
->root
.type
== bfd_link_hash_defined
6010 || h
->root
.type
== bfd_link_hash_defweak
)
6012 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
6014 /* The symbol has no type if specified on the command line. */
6015 h
->type
= STT_OBJECT
;
6016 if (info
->stacksize
)
6017 /* xgettext:c-format */
6018 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6019 output_bfd
, legacy_symbol
);
6020 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
6021 /* xgettext:c-format */
6022 _bfd_error_handler (_("%pB: %s not absolute"),
6023 output_bfd
, legacy_symbol
);
6025 info
->stacksize
= h
->root
.u
.def
.value
;
6028 if (!info
->stacksize
)
6029 /* If the user didn't set a size, or explicitly inhibit the
6030 size, set it now. */
6031 info
->stacksize
= default_size
;
6033 /* Provide the legacy symbol, if it is referenced. */
6034 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
6035 || h
->root
.type
== bfd_link_hash_undefweak
))
6037 struct bfd_link_hash_entry
*bh
= NULL
;
6039 if (!(_bfd_generic_link_add_one_symbol
6040 (info
, output_bfd
, legacy_symbol
,
6041 BSF_GLOBAL
, bfd_abs_section_ptr
,
6042 info
->stacksize
>= 0 ? info
->stacksize
: 0,
6043 NULL
, FALSE
, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
6046 h
= (struct elf_link_hash_entry
*) bh
;
6048 h
->type
= STT_OBJECT
;
6054 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6056 struct elf_gc_sweep_symbol_info
6058 struct bfd_link_info
*info
;
6059 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
6064 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
6067 && (((h
->root
.type
== bfd_link_hash_defined
6068 || h
->root
.type
== bfd_link_hash_defweak
)
6069 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
6070 && h
->root
.u
.def
.section
->gc_mark
))
6071 || h
->root
.type
== bfd_link_hash_undefined
6072 || h
->root
.type
== bfd_link_hash_undefweak
))
6074 struct elf_gc_sweep_symbol_info
*inf
;
6076 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
6077 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
6080 h
->ref_regular_nonweak
= 0;
6086 /* Set up the sizes and contents of the ELF dynamic sections. This is
6087 called by the ELF linker emulation before_allocation routine. We
6088 must set the sizes of the sections before the linker sets the
6089 addresses of the various sections. */
6092 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
6095 const char *filter_shlib
,
6097 const char *depaudit
,
6098 const char * const *auxiliary_filters
,
6099 struct bfd_link_info
*info
,
6100 asection
**sinterpptr
)
6103 const struct elf_backend_data
*bed
;
6107 if (!is_elf_hash_table (info
->hash
))
6110 dynobj
= elf_hash_table (info
)->dynobj
;
6112 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6114 struct bfd_elf_version_tree
*verdefs
;
6115 struct elf_info_failed asvinfo
;
6116 struct bfd_elf_version_tree
*t
;
6117 struct bfd_elf_version_expr
*d
;
6121 /* If we are supposed to export all symbols into the dynamic symbol
6122 table (this is not the normal case), then do so. */
6123 if (info
->export_dynamic
6124 || (bfd_link_executable (info
) && info
->dynamic
))
6126 struct elf_info_failed eif
;
6130 elf_link_hash_traverse (elf_hash_table (info
),
6131 _bfd_elf_export_symbol
,
6139 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6141 if (soname_indx
== (size_t) -1
6142 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6146 soname_indx
= (size_t) -1;
6148 /* Make all global versions with definition. */
6149 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6150 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6151 if (!d
->symver
&& d
->literal
)
6153 const char *verstr
, *name
;
6154 size_t namelen
, verlen
, newlen
;
6155 char *newname
, *p
, leading_char
;
6156 struct elf_link_hash_entry
*newh
;
6158 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6160 namelen
= strlen (name
) + (leading_char
!= '\0');
6162 verlen
= strlen (verstr
);
6163 newlen
= namelen
+ verlen
+ 3;
6165 newname
= (char *) bfd_malloc (newlen
);
6166 if (newname
== NULL
)
6168 newname
[0] = leading_char
;
6169 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6171 /* Check the hidden versioned definition. */
6172 p
= newname
+ namelen
;
6174 memcpy (p
, verstr
, verlen
+ 1);
6175 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6176 newname
, FALSE
, FALSE
,
6179 || (newh
->root
.type
!= bfd_link_hash_defined
6180 && newh
->root
.type
!= bfd_link_hash_defweak
))
6182 /* Check the default versioned definition. */
6184 memcpy (p
, verstr
, verlen
+ 1);
6185 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6186 newname
, FALSE
, FALSE
,
6191 /* Mark this version if there is a definition and it is
6192 not defined in a shared object. */
6194 && !newh
->def_dynamic
6195 && (newh
->root
.type
== bfd_link_hash_defined
6196 || newh
->root
.type
== bfd_link_hash_defweak
))
6200 /* Attach all the symbols to their version information. */
6201 asvinfo
.info
= info
;
6202 asvinfo
.failed
= FALSE
;
6204 elf_link_hash_traverse (elf_hash_table (info
),
6205 _bfd_elf_link_assign_sym_version
,
6210 if (!info
->allow_undefined_version
)
6212 /* Check if all global versions have a definition. */
6213 bfd_boolean all_defined
= TRUE
;
6214 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6215 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6216 if (d
->literal
&& !d
->symver
&& !d
->script
)
6219 (_("%s: undefined version: %s"),
6220 d
->pattern
, t
->name
);
6221 all_defined
= FALSE
;
6226 bfd_set_error (bfd_error_bad_value
);
6231 /* Set up the version definition section. */
6232 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6233 BFD_ASSERT (s
!= NULL
);
6235 /* We may have created additional version definitions if we are
6236 just linking a regular application. */
6237 verdefs
= info
->version_info
;
6239 /* Skip anonymous version tag. */
6240 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6241 verdefs
= verdefs
->next
;
6243 if (verdefs
== NULL
&& !info
->create_default_symver
)
6244 s
->flags
|= SEC_EXCLUDE
;
6250 Elf_Internal_Verdef def
;
6251 Elf_Internal_Verdaux defaux
;
6252 struct bfd_link_hash_entry
*bh
;
6253 struct elf_link_hash_entry
*h
;
6259 /* Make space for the base version. */
6260 size
+= sizeof (Elf_External_Verdef
);
6261 size
+= sizeof (Elf_External_Verdaux
);
6264 /* Make space for the default version. */
6265 if (info
->create_default_symver
)
6267 size
+= sizeof (Elf_External_Verdef
);
6271 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6273 struct bfd_elf_version_deps
*n
;
6275 /* Don't emit base version twice. */
6279 size
+= sizeof (Elf_External_Verdef
);
6280 size
+= sizeof (Elf_External_Verdaux
);
6283 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6284 size
+= sizeof (Elf_External_Verdaux
);
6288 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6289 if (s
->contents
== NULL
&& s
->size
!= 0)
6292 /* Fill in the version definition section. */
6296 def
.vd_version
= VER_DEF_CURRENT
;
6297 def
.vd_flags
= VER_FLG_BASE
;
6300 if (info
->create_default_symver
)
6302 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6303 def
.vd_next
= sizeof (Elf_External_Verdef
);
6307 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6308 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6309 + sizeof (Elf_External_Verdaux
));
6312 if (soname_indx
!= (size_t) -1)
6314 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6316 def
.vd_hash
= bfd_elf_hash (soname
);
6317 defaux
.vda_name
= soname_indx
;
6324 name
= lbasename (output_bfd
->filename
);
6325 def
.vd_hash
= bfd_elf_hash (name
);
6326 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6328 if (indx
== (size_t) -1)
6330 defaux
.vda_name
= indx
;
6332 defaux
.vda_next
= 0;
6334 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6335 (Elf_External_Verdef
*) p
);
6336 p
+= sizeof (Elf_External_Verdef
);
6337 if (info
->create_default_symver
)
6339 /* Add a symbol representing this version. */
6341 if (! (_bfd_generic_link_add_one_symbol
6342 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6344 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6346 h
= (struct elf_link_hash_entry
*) bh
;
6349 h
->type
= STT_OBJECT
;
6350 h
->verinfo
.vertree
= NULL
;
6352 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6355 /* Create a duplicate of the base version with the same
6356 aux block, but different flags. */
6359 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6361 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6362 + sizeof (Elf_External_Verdaux
));
6365 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6366 (Elf_External_Verdef
*) p
);
6367 p
+= sizeof (Elf_External_Verdef
);
6369 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6370 (Elf_External_Verdaux
*) p
);
6371 p
+= sizeof (Elf_External_Verdaux
);
6373 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6376 struct bfd_elf_version_deps
*n
;
6378 /* Don't emit the base version twice. */
6383 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6386 /* Add a symbol representing this version. */
6388 if (! (_bfd_generic_link_add_one_symbol
6389 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6391 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6393 h
= (struct elf_link_hash_entry
*) bh
;
6396 h
->type
= STT_OBJECT
;
6397 h
->verinfo
.vertree
= t
;
6399 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6402 def
.vd_version
= VER_DEF_CURRENT
;
6404 if (t
->globals
.list
== NULL
6405 && t
->locals
.list
== NULL
6407 def
.vd_flags
|= VER_FLG_WEAK
;
6408 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6409 def
.vd_cnt
= cdeps
+ 1;
6410 def
.vd_hash
= bfd_elf_hash (t
->name
);
6411 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6414 /* If a basever node is next, it *must* be the last node in
6415 the chain, otherwise Verdef construction breaks. */
6416 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6417 BFD_ASSERT (t
->next
->next
== NULL
);
6419 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6420 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6421 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6423 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6424 (Elf_External_Verdef
*) p
);
6425 p
+= sizeof (Elf_External_Verdef
);
6427 defaux
.vda_name
= h
->dynstr_index
;
6428 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6430 defaux
.vda_next
= 0;
6431 if (t
->deps
!= NULL
)
6432 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6433 t
->name_indx
= defaux
.vda_name
;
6435 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6436 (Elf_External_Verdaux
*) p
);
6437 p
+= sizeof (Elf_External_Verdaux
);
6439 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6441 if (n
->version_needed
== NULL
)
6443 /* This can happen if there was an error in the
6445 defaux
.vda_name
= 0;
6449 defaux
.vda_name
= n
->version_needed
->name_indx
;
6450 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6453 if (n
->next
== NULL
)
6454 defaux
.vda_next
= 0;
6456 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6458 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6459 (Elf_External_Verdaux
*) p
);
6460 p
+= sizeof (Elf_External_Verdaux
);
6464 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6468 bed
= get_elf_backend_data (output_bfd
);
6470 if (info
->gc_sections
&& bed
->can_gc_sections
)
6472 struct elf_gc_sweep_symbol_info sweep_info
;
6474 /* Remove the symbols that were in the swept sections from the
6475 dynamic symbol table. */
6476 sweep_info
.info
= info
;
6477 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
6478 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
6482 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6485 struct elf_find_verdep_info sinfo
;
6487 /* Work out the size of the version reference section. */
6489 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6490 BFD_ASSERT (s
!= NULL
);
6493 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6494 if (sinfo
.vers
== 0)
6496 sinfo
.failed
= FALSE
;
6498 elf_link_hash_traverse (elf_hash_table (info
),
6499 _bfd_elf_link_find_version_dependencies
,
6504 if (elf_tdata (output_bfd
)->verref
== NULL
)
6505 s
->flags
|= SEC_EXCLUDE
;
6508 Elf_Internal_Verneed
*vn
;
6513 /* Build the version dependency section. */
6516 for (vn
= elf_tdata (output_bfd
)->verref
;
6518 vn
= vn
->vn_nextref
)
6520 Elf_Internal_Vernaux
*a
;
6522 size
+= sizeof (Elf_External_Verneed
);
6524 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6525 size
+= sizeof (Elf_External_Vernaux
);
6529 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6530 if (s
->contents
== NULL
)
6534 for (vn
= elf_tdata (output_bfd
)->verref
;
6536 vn
= vn
->vn_nextref
)
6539 Elf_Internal_Vernaux
*a
;
6543 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6546 vn
->vn_version
= VER_NEED_CURRENT
;
6548 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6549 elf_dt_name (vn
->vn_bfd
) != NULL
6550 ? elf_dt_name (vn
->vn_bfd
)
6551 : lbasename (vn
->vn_bfd
->filename
),
6553 if (indx
== (size_t) -1)
6556 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
6557 if (vn
->vn_nextref
== NULL
)
6560 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
6561 + caux
* sizeof (Elf_External_Vernaux
));
6563 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
6564 (Elf_External_Verneed
*) p
);
6565 p
+= sizeof (Elf_External_Verneed
);
6567 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6569 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6570 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6571 a
->vna_nodename
, FALSE
);
6572 if (indx
== (size_t) -1)
6575 if (a
->vna_nextptr
== NULL
)
6578 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6580 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6581 (Elf_External_Vernaux
*) p
);
6582 p
+= sizeof (Elf_External_Vernaux
);
6586 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6590 /* Any syms created from now on start with -1 in
6591 got.refcount/offset and plt.refcount/offset. */
6592 elf_hash_table (info
)->init_got_refcount
6593 = elf_hash_table (info
)->init_got_offset
;
6594 elf_hash_table (info
)->init_plt_refcount
6595 = elf_hash_table (info
)->init_plt_offset
;
6597 if (bfd_link_relocatable (info
)
6598 && !_bfd_elf_size_group_sections (info
))
6601 /* The backend may have to create some sections regardless of whether
6602 we're dynamic or not. */
6603 if (bed
->elf_backend_always_size_sections
6604 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
6607 /* Determine any GNU_STACK segment requirements, after the backend
6608 has had a chance to set a default segment size. */
6609 if (info
->execstack
)
6610 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
6611 else if (info
->noexecstack
)
6612 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
6616 asection
*notesec
= NULL
;
6619 for (inputobj
= info
->input_bfds
;
6621 inputobj
= inputobj
->link
.next
)
6626 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
6628 s
= inputobj
->sections
;
6629 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
6632 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
6635 if (s
->flags
& SEC_CODE
)
6639 else if (bed
->default_execstack
)
6642 if (notesec
|| info
->stacksize
> 0)
6643 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
6644 if (notesec
&& exec
&& bfd_link_relocatable (info
)
6645 && notesec
->output_section
!= bfd_abs_section_ptr
)
6646 notesec
->output_section
->flags
|= SEC_CODE
;
6649 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6651 struct elf_info_failed eif
;
6652 struct elf_link_hash_entry
*h
;
6656 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
6657 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
6661 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
6663 info
->flags
|= DF_SYMBOLIC
;
6671 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
6673 if (indx
== (size_t) -1)
6676 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
6677 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
6681 if (filter_shlib
!= NULL
)
6685 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6686 filter_shlib
, TRUE
);
6687 if (indx
== (size_t) -1
6688 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
6692 if (auxiliary_filters
!= NULL
)
6694 const char * const *p
;
6696 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
6700 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6702 if (indx
== (size_t) -1
6703 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
6712 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
6714 if (indx
== (size_t) -1
6715 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
6719 if (depaudit
!= NULL
)
6723 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
6725 if (indx
== (size_t) -1
6726 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
6733 /* Find all symbols which were defined in a dynamic object and make
6734 the backend pick a reasonable value for them. */
6735 elf_link_hash_traverse (elf_hash_table (info
),
6736 _bfd_elf_adjust_dynamic_symbol
,
6741 /* Add some entries to the .dynamic section. We fill in some of the
6742 values later, in bfd_elf_final_link, but we must add the entries
6743 now so that we know the final size of the .dynamic section. */
6745 /* If there are initialization and/or finalization functions to
6746 call then add the corresponding DT_INIT/DT_FINI entries. */
6747 h
= (info
->init_function
6748 ? elf_link_hash_lookup (elf_hash_table (info
),
6749 info
->init_function
, FALSE
,
6756 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
6759 h
= (info
->fini_function
6760 ? elf_link_hash_lookup (elf_hash_table (info
),
6761 info
->fini_function
, FALSE
,
6768 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
6772 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
6773 if (s
!= NULL
&& s
->linker_has_input
)
6775 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6776 if (! bfd_link_executable (info
))
6781 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
6782 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
6783 && (o
= sub
->sections
) != NULL
6784 && o
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
)
6785 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
6786 if (elf_section_data (o
)->this_hdr
.sh_type
6787 == SHT_PREINIT_ARRAY
)
6790 (_("%pB: .preinit_array section is not allowed in DSO"),
6795 bfd_set_error (bfd_error_nonrepresentable_section
);
6799 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
6800 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
6803 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
6804 if (s
!= NULL
&& s
->linker_has_input
)
6806 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
6807 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
6810 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
6811 if (s
!= NULL
&& s
->linker_has_input
)
6813 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
6814 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
6818 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
6819 /* If .dynstr is excluded from the link, we don't want any of
6820 these tags. Strictly, we should be checking each section
6821 individually; This quick check covers for the case where
6822 someone does a /DISCARD/ : { *(*) }. */
6823 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
6825 bfd_size_type strsize
;
6827 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6828 if ((info
->emit_hash
6829 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
6830 || (info
->emit_gnu_hash
6831 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
6832 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
6833 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
6834 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
6835 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
6836 bed
->s
->sizeof_sym
))
6841 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
6844 /* The backend must work out the sizes of all the other dynamic
6847 && bed
->elf_backend_size_dynamic_sections
!= NULL
6848 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
6851 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6853 if (elf_tdata (output_bfd
)->cverdefs
)
6855 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
6857 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
6858 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
6862 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
6864 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
6867 else if (info
->flags
& DF_BIND_NOW
)
6869 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
6875 if (bfd_link_executable (info
))
6876 info
->flags_1
&= ~ (DF_1_INITFIRST
6879 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
6883 if (elf_tdata (output_bfd
)->cverrefs
)
6885 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
6887 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
6888 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
6892 if ((elf_tdata (output_bfd
)->cverrefs
== 0
6893 && elf_tdata (output_bfd
)->cverdefs
== 0)
6894 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, NULL
) <= 1)
6898 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6899 s
->flags
|= SEC_EXCLUDE
;
6905 /* Find the first non-excluded output section. We'll use its
6906 section symbol for some emitted relocs. */
6908 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
6912 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6913 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
6914 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
6916 elf_hash_table (info
)->text_index_section
= s
;
6921 /* Find two non-excluded output sections, one for code, one for data.
6922 We'll use their section symbols for some emitted relocs. */
6924 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6928 /* Data first, since setting text_index_section changes
6929 _bfd_elf_link_omit_section_dynsym. */
6930 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6931 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
6932 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
6934 elf_hash_table (info
)->data_index_section
= s
;
6938 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6939 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
6940 == (SEC_ALLOC
| SEC_READONLY
))
6941 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
6943 elf_hash_table (info
)->text_index_section
= s
;
6947 if (elf_hash_table (info
)->text_index_section
== NULL
)
6948 elf_hash_table (info
)->text_index_section
6949 = elf_hash_table (info
)->data_index_section
;
6953 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6955 const struct elf_backend_data
*bed
;
6956 unsigned long section_sym_count
;
6957 bfd_size_type dynsymcount
= 0;
6959 if (!is_elf_hash_table (info
->hash
))
6962 bed
= get_elf_backend_data (output_bfd
);
6963 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6965 /* Assign dynsym indices. In a shared library we generate a section
6966 symbol for each output section, which come first. Next come all
6967 of the back-end allocated local dynamic syms, followed by the rest
6968 of the global symbols.
6970 This is usually not needed for static binaries, however backends
6971 can request to always do it, e.g. the MIPS backend uses dynamic
6972 symbol counts to lay out GOT, which will be produced in the
6973 presence of GOT relocations even in static binaries (holding fixed
6974 data in that case, to satisfy those relocations). */
6976 if (elf_hash_table (info
)->dynamic_sections_created
6977 || bed
->always_renumber_dynsyms
)
6978 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6979 §ion_sym_count
);
6981 if (elf_hash_table (info
)->dynamic_sections_created
)
6985 unsigned int dtagcount
;
6987 dynobj
= elf_hash_table (info
)->dynobj
;
6989 /* Work out the size of the symbol version section. */
6990 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6991 BFD_ASSERT (s
!= NULL
);
6992 if ((s
->flags
& SEC_EXCLUDE
) == 0)
6994 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6995 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6996 if (s
->contents
== NULL
)
6999 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
7003 /* Set the size of the .dynsym and .hash sections. We counted
7004 the number of dynamic symbols in elf_link_add_object_symbols.
7005 We will build the contents of .dynsym and .hash when we build
7006 the final symbol table, because until then we do not know the
7007 correct value to give the symbols. We built the .dynstr
7008 section as we went along in elf_link_add_object_symbols. */
7009 s
= elf_hash_table (info
)->dynsym
;
7010 BFD_ASSERT (s
!= NULL
);
7011 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
7013 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7014 if (s
->contents
== NULL
)
7017 /* The first entry in .dynsym is a dummy symbol. Clear all the
7018 section syms, in case we don't output them all. */
7019 ++section_sym_count
;
7020 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
7022 elf_hash_table (info
)->bucketcount
= 0;
7024 /* Compute the size of the hashing table. As a side effect this
7025 computes the hash values for all the names we export. */
7026 if (info
->emit_hash
)
7028 unsigned long int *hashcodes
;
7029 struct hash_codes_info hashinf
;
7031 unsigned long int nsyms
;
7033 size_t hash_entry_size
;
7035 /* Compute the hash values for all exported symbols. At the same
7036 time store the values in an array so that we could use them for
7038 amt
= dynsymcount
* sizeof (unsigned long int);
7039 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
7040 if (hashcodes
== NULL
)
7042 hashinf
.hashcodes
= hashcodes
;
7043 hashinf
.error
= FALSE
;
7045 /* Put all hash values in HASHCODES. */
7046 elf_link_hash_traverse (elf_hash_table (info
),
7047 elf_collect_hash_codes
, &hashinf
);
7054 nsyms
= hashinf
.hashcodes
- hashcodes
;
7056 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
7059 if (bucketcount
== 0 && nsyms
> 0)
7062 elf_hash_table (info
)->bucketcount
= bucketcount
;
7064 s
= bfd_get_linker_section (dynobj
, ".hash");
7065 BFD_ASSERT (s
!= NULL
);
7066 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
7067 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
7068 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7069 if (s
->contents
== NULL
)
7072 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
7073 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
7074 s
->contents
+ hash_entry_size
);
7077 if (info
->emit_gnu_hash
)
7080 unsigned char *contents
;
7081 struct collect_gnu_hash_codes cinfo
;
7085 memset (&cinfo
, 0, sizeof (cinfo
));
7087 /* Compute the hash values for all exported symbols. At the same
7088 time store the values in an array so that we could use them for
7090 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
7091 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
7092 if (cinfo
.hashcodes
== NULL
)
7095 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
7096 cinfo
.min_dynindx
= -1;
7097 cinfo
.output_bfd
= output_bfd
;
7100 /* Put all hash values in HASHCODES. */
7101 elf_link_hash_traverse (elf_hash_table (info
),
7102 elf_collect_gnu_hash_codes
, &cinfo
);
7105 free (cinfo
.hashcodes
);
7110 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
7112 if (bucketcount
== 0)
7114 free (cinfo
.hashcodes
);
7118 s
= bfd_get_linker_section (dynobj
, ".gnu.hash");
7119 BFD_ASSERT (s
!= NULL
);
7121 if (cinfo
.nsyms
== 0)
7123 /* Empty .gnu.hash section is special. */
7124 BFD_ASSERT (cinfo
.min_dynindx
== -1);
7125 free (cinfo
.hashcodes
);
7126 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
7127 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7128 if (contents
== NULL
)
7130 s
->contents
= contents
;
7131 /* 1 empty bucket. */
7132 bfd_put_32 (output_bfd
, 1, contents
);
7133 /* SYMIDX above the special symbol 0. */
7134 bfd_put_32 (output_bfd
, 1, contents
+ 4);
7135 /* Just one word for bitmask. */
7136 bfd_put_32 (output_bfd
, 1, contents
+ 8);
7137 /* Only hash fn bloom filter. */
7138 bfd_put_32 (output_bfd
, 0, contents
+ 12);
7139 /* No hashes are valid - empty bitmask. */
7140 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
7141 /* No hashes in the only bucket. */
7142 bfd_put_32 (output_bfd
, 0,
7143 contents
+ 16 + bed
->s
->arch_size
/ 8);
7147 unsigned long int maskwords
, maskbitslog2
, x
;
7148 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
7152 while ((x
>>= 1) != 0)
7154 if (maskbitslog2
< 3)
7156 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
7157 maskbitslog2
= maskbitslog2
+ 3;
7159 maskbitslog2
= maskbitslog2
+ 2;
7160 if (bed
->s
->arch_size
== 64)
7162 if (maskbitslog2
== 5)
7168 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
7169 cinfo
.shift2
= maskbitslog2
;
7170 cinfo
.maskbits
= 1 << maskbitslog2
;
7171 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
7172 amt
= bucketcount
* sizeof (unsigned long int) * 2;
7173 amt
+= maskwords
* sizeof (bfd_vma
);
7174 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
7175 if (cinfo
.bitmask
== NULL
)
7177 free (cinfo
.hashcodes
);
7181 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
7182 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
7183 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
7184 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
7186 /* Determine how often each hash bucket is used. */
7187 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
7188 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
7189 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
7191 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
7192 if (cinfo
.counts
[i
] != 0)
7194 cinfo
.indx
[i
] = cnt
;
7195 cnt
+= cinfo
.counts
[i
];
7197 BFD_ASSERT (cnt
== dynsymcount
);
7198 cinfo
.bucketcount
= bucketcount
;
7199 cinfo
.local_indx
= cinfo
.min_dynindx
;
7201 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
7202 s
->size
+= cinfo
.maskbits
/ 8;
7203 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7204 if (contents
== NULL
)
7206 free (cinfo
.bitmask
);
7207 free (cinfo
.hashcodes
);
7211 s
->contents
= contents
;
7212 bfd_put_32 (output_bfd
, bucketcount
, contents
);
7213 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
7214 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
7215 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
7216 contents
+= 16 + cinfo
.maskbits
/ 8;
7218 for (i
= 0; i
< bucketcount
; ++i
)
7220 if (cinfo
.counts
[i
] == 0)
7221 bfd_put_32 (output_bfd
, 0, contents
);
7223 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
7227 cinfo
.contents
= contents
;
7229 /* Renumber dynamic symbols, populate .gnu.hash section. */
7230 elf_link_hash_traverse (elf_hash_table (info
),
7231 elf_renumber_gnu_hash_syms
, &cinfo
);
7233 contents
= s
->contents
+ 16;
7234 for (i
= 0; i
< maskwords
; ++i
)
7236 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
7238 contents
+= bed
->s
->arch_size
/ 8;
7241 free (cinfo
.bitmask
);
7242 free (cinfo
.hashcodes
);
7246 s
= bfd_get_linker_section (dynobj
, ".dynstr");
7247 BFD_ASSERT (s
!= NULL
);
7249 elf_finalize_dynstr (output_bfd
, info
);
7251 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7253 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
7254 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
7261 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7264 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
7267 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
7268 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
7271 /* Finish SHF_MERGE section merging. */
7274 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
7279 if (!is_elf_hash_table (info
->hash
))
7282 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
7283 if ((ibfd
->flags
& DYNAMIC
) == 0
7284 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
7285 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
7286 == get_elf_backend_data (obfd
)->s
->elfclass
))
7287 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7288 if ((sec
->flags
& SEC_MERGE
) != 0
7289 && !bfd_is_abs_section (sec
->output_section
))
7291 struct bfd_elf_section_data
*secdata
;
7293 secdata
= elf_section_data (sec
);
7294 if (! _bfd_add_merge_section (obfd
,
7295 &elf_hash_table (info
)->merge_info
,
7296 sec
, &secdata
->sec_info
))
7298 else if (secdata
->sec_info
)
7299 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7302 if (elf_hash_table (info
)->merge_info
!= NULL
)
7303 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7304 merge_sections_remove_hook
);
7308 /* Create an entry in an ELF linker hash table. */
7310 struct bfd_hash_entry
*
7311 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7312 struct bfd_hash_table
*table
,
7315 /* Allocate the structure if it has not already been allocated by a
7319 entry
= (struct bfd_hash_entry
*)
7320 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7325 /* Call the allocation method of the superclass. */
7326 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7329 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7330 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7332 /* Set local fields. */
7335 ret
->got
= htab
->init_got_refcount
;
7336 ret
->plt
= htab
->init_plt_refcount
;
7337 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7338 - offsetof (struct elf_link_hash_entry
, size
)));
7339 /* Assume that we have been called by a non-ELF symbol reader.
7340 This flag is then reset by the code which reads an ELF input
7341 file. This ensures that a symbol created by a non-ELF symbol
7342 reader will have the flag set correctly. */
7349 /* Copy data from an indirect symbol to its direct symbol, hiding the
7350 old indirect symbol. Also used for copying flags to a weakdef. */
7353 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7354 struct elf_link_hash_entry
*dir
,
7355 struct elf_link_hash_entry
*ind
)
7357 struct elf_link_hash_table
*htab
;
7359 /* Copy down any references that we may have already seen to the
7360 symbol which just became indirect. */
7362 if (dir
->versioned
!= versioned_hidden
)
7363 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7364 dir
->ref_regular
|= ind
->ref_regular
;
7365 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7366 dir
->non_got_ref
|= ind
->non_got_ref
;
7367 dir
->needs_plt
|= ind
->needs_plt
;
7368 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7370 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7373 /* Copy over the global and procedure linkage table refcount entries.
7374 These may have been already set up by a check_relocs routine. */
7375 htab
= elf_hash_table (info
);
7376 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7378 if (dir
->got
.refcount
< 0)
7379 dir
->got
.refcount
= 0;
7380 dir
->got
.refcount
+= ind
->got
.refcount
;
7381 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7384 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7386 if (dir
->plt
.refcount
< 0)
7387 dir
->plt
.refcount
= 0;
7388 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7389 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7392 if (ind
->dynindx
!= -1)
7394 if (dir
->dynindx
!= -1)
7395 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7396 dir
->dynindx
= ind
->dynindx
;
7397 dir
->dynstr_index
= ind
->dynstr_index
;
7399 ind
->dynstr_index
= 0;
7404 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7405 struct elf_link_hash_entry
*h
,
7406 bfd_boolean force_local
)
7408 /* STT_GNU_IFUNC symbol must go through PLT. */
7409 if (h
->type
!= STT_GNU_IFUNC
)
7411 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7416 h
->forced_local
= 1;
7417 if (h
->dynindx
!= -1)
7419 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7422 h
->dynstr_index
= 0;
7427 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7431 _bfd_elf_link_hash_table_init
7432 (struct elf_link_hash_table
*table
,
7434 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
7435 struct bfd_hash_table
*,
7437 unsigned int entsize
,
7438 enum elf_target_id target_id
)
7441 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
7443 table
->init_got_refcount
.refcount
= can_refcount
- 1;
7444 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
7445 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
7446 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
7447 /* The first dynamic symbol is a dummy. */
7448 table
->dynsymcount
= 1;
7450 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
7452 table
->root
.type
= bfd_link_elf_hash_table
;
7453 table
->hash_table_id
= target_id
;
7458 /* Create an ELF linker hash table. */
7460 struct bfd_link_hash_table
*
7461 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7463 struct elf_link_hash_table
*ret
;
7464 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
7466 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7470 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7471 sizeof (struct elf_link_hash_entry
),
7477 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7482 /* Destroy an ELF linker hash table. */
7485 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7487 struct elf_link_hash_table
*htab
;
7489 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7490 if (htab
->dynstr
!= NULL
)
7491 _bfd_elf_strtab_free (htab
->dynstr
);
7492 _bfd_merge_sections_free (htab
->merge_info
);
7493 _bfd_generic_link_hash_table_free (obfd
);
7496 /* This is a hook for the ELF emulation code in the generic linker to
7497 tell the backend linker what file name to use for the DT_NEEDED
7498 entry for a dynamic object. */
7501 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7503 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7504 && bfd_get_format (abfd
) == bfd_object
)
7505 elf_dt_name (abfd
) = name
;
7509 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
7512 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7513 && bfd_get_format (abfd
) == bfd_object
)
7514 lib_class
= elf_dyn_lib_class (abfd
);
7521 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
7523 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7524 && bfd_get_format (abfd
) == bfd_object
)
7525 elf_dyn_lib_class (abfd
) = lib_class
;
7528 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7529 the linker ELF emulation code. */
7531 struct bfd_link_needed_list
*
7532 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7533 struct bfd_link_info
*info
)
7535 if (! is_elf_hash_table (info
->hash
))
7537 return elf_hash_table (info
)->needed
;
7540 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7541 hook for the linker ELF emulation code. */
7543 struct bfd_link_needed_list
*
7544 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7545 struct bfd_link_info
*info
)
7547 if (! is_elf_hash_table (info
->hash
))
7549 return elf_hash_table (info
)->runpath
;
7552 /* Get the name actually used for a dynamic object for a link. This
7553 is the SONAME entry if there is one. Otherwise, it is the string
7554 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7557 bfd_elf_get_dt_soname (bfd
*abfd
)
7559 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7560 && bfd_get_format (abfd
) == bfd_object
)
7561 return elf_dt_name (abfd
);
7565 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7566 the ELF linker emulation code. */
7569 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
7570 struct bfd_link_needed_list
**pneeded
)
7573 bfd_byte
*dynbuf
= NULL
;
7574 unsigned int elfsec
;
7575 unsigned long shlink
;
7576 bfd_byte
*extdyn
, *extdynend
;
7578 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
7582 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
7583 || bfd_get_format (abfd
) != bfd_object
)
7586 s
= bfd_get_section_by_name (abfd
, ".dynamic");
7587 if (s
== NULL
|| s
->size
== 0)
7590 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
7593 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
7594 if (elfsec
== SHN_BAD
)
7597 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
7599 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
7600 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
7603 extdynend
= extdyn
+ s
->size
;
7604 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
7606 Elf_Internal_Dyn dyn
;
7608 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
7610 if (dyn
.d_tag
== DT_NULL
)
7613 if (dyn
.d_tag
== DT_NEEDED
)
7616 struct bfd_link_needed_list
*l
;
7617 unsigned int tagv
= dyn
.d_un
.d_val
;
7620 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
7625 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
7646 struct elf_symbuf_symbol
7648 unsigned long st_name
; /* Symbol name, index in string tbl */
7649 unsigned char st_info
; /* Type and binding attributes */
7650 unsigned char st_other
; /* Visibilty, and target specific */
7653 struct elf_symbuf_head
7655 struct elf_symbuf_symbol
*ssym
;
7657 unsigned int st_shndx
;
7664 Elf_Internal_Sym
*isym
;
7665 struct elf_symbuf_symbol
*ssym
;
7670 /* Sort references to symbols by ascending section number. */
7673 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
7675 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
7676 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
7678 return s1
->st_shndx
- s2
->st_shndx
;
7682 elf_sym_name_compare (const void *arg1
, const void *arg2
)
7684 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
7685 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
7686 return strcmp (s1
->name
, s2
->name
);
7689 static struct elf_symbuf_head
*
7690 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
7692 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
7693 struct elf_symbuf_symbol
*ssym
;
7694 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
7695 size_t i
, shndx_count
, total_size
;
7697 indbuf
= (Elf_Internal_Sym
**) bfd_malloc2 (symcount
, sizeof (*indbuf
));
7701 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
7702 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
7703 *ind
++ = &isymbuf
[i
];
7706 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
7707 elf_sort_elf_symbol
);
7710 if (indbufend
> indbuf
)
7711 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
7712 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
7715 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
7716 + (indbufend
- indbuf
) * sizeof (*ssym
));
7717 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
7718 if (ssymbuf
== NULL
)
7724 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
7725 ssymbuf
->ssym
= NULL
;
7726 ssymbuf
->count
= shndx_count
;
7727 ssymbuf
->st_shndx
= 0;
7728 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
7730 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
7733 ssymhead
->ssym
= ssym
;
7734 ssymhead
->count
= 0;
7735 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
7737 ssym
->st_name
= (*ind
)->st_name
;
7738 ssym
->st_info
= (*ind
)->st_info
;
7739 ssym
->st_other
= (*ind
)->st_other
;
7742 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
7743 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
7750 /* Check if 2 sections define the same set of local and global
7754 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
7755 struct bfd_link_info
*info
)
7758 const struct elf_backend_data
*bed1
, *bed2
;
7759 Elf_Internal_Shdr
*hdr1
, *hdr2
;
7760 size_t symcount1
, symcount2
;
7761 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
7762 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
7763 Elf_Internal_Sym
*isym
, *isymend
;
7764 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
7765 size_t count1
, count2
, i
;
7766 unsigned int shndx1
, shndx2
;
7772 /* Both sections have to be in ELF. */
7773 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
7774 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
7777 if (elf_section_type (sec1
) != elf_section_type (sec2
))
7780 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
7781 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
7782 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
7785 bed1
= get_elf_backend_data (bfd1
);
7786 bed2
= get_elf_backend_data (bfd2
);
7787 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
7788 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
7789 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
7790 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
7792 if (symcount1
== 0 || symcount2
== 0)
7798 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
7799 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
7801 if (ssymbuf1
== NULL
)
7803 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
7805 if (isymbuf1
== NULL
)
7808 if (!info
->reduce_memory_overheads
)
7809 elf_tdata (bfd1
)->symbuf
= ssymbuf1
7810 = elf_create_symbuf (symcount1
, isymbuf1
);
7813 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
7815 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
7817 if (isymbuf2
== NULL
)
7820 if (ssymbuf1
!= NULL
&& !info
->reduce_memory_overheads
)
7821 elf_tdata (bfd2
)->symbuf
= ssymbuf2
7822 = elf_create_symbuf (symcount2
, isymbuf2
);
7825 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
7827 /* Optimized faster version. */
7829 struct elf_symbol
*symp
;
7830 struct elf_symbuf_symbol
*ssym
, *ssymend
;
7833 hi
= ssymbuf1
->count
;
7838 mid
= (lo
+ hi
) / 2;
7839 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
7841 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
7845 count1
= ssymbuf1
[mid
].count
;
7852 hi
= ssymbuf2
->count
;
7857 mid
= (lo
+ hi
) / 2;
7858 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
7860 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
7864 count2
= ssymbuf2
[mid
].count
;
7870 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7874 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
7876 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
7877 if (symtable1
== NULL
|| symtable2
== NULL
)
7881 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
7882 ssym
< ssymend
; ssym
++, symp
++)
7884 symp
->u
.ssym
= ssym
;
7885 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
7891 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
7892 ssym
< ssymend
; ssym
++, symp
++)
7894 symp
->u
.ssym
= ssym
;
7895 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
7900 /* Sort symbol by name. */
7901 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7902 elf_sym_name_compare
);
7903 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7904 elf_sym_name_compare
);
7906 for (i
= 0; i
< count1
; i
++)
7907 /* Two symbols must have the same binding, type and name. */
7908 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
7909 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
7910 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7917 symtable1
= (struct elf_symbol
*)
7918 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
7919 symtable2
= (struct elf_symbol
*)
7920 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
7921 if (symtable1
== NULL
|| symtable2
== NULL
)
7924 /* Count definitions in the section. */
7926 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
7927 if (isym
->st_shndx
== shndx1
)
7928 symtable1
[count1
++].u
.isym
= isym
;
7931 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
7932 if (isym
->st_shndx
== shndx2
)
7933 symtable2
[count2
++].u
.isym
= isym
;
7935 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7938 for (i
= 0; i
< count1
; i
++)
7940 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
7941 symtable1
[i
].u
.isym
->st_name
);
7943 for (i
= 0; i
< count2
; i
++)
7945 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
7946 symtable2
[i
].u
.isym
->st_name
);
7948 /* Sort symbol by name. */
7949 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7950 elf_sym_name_compare
);
7951 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7952 elf_sym_name_compare
);
7954 for (i
= 0; i
< count1
; i
++)
7955 /* Two symbols must have the same binding, type and name. */
7956 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
7957 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
7958 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7976 /* Return TRUE if 2 section types are compatible. */
7979 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
7980 bfd
*bbfd
, const asection
*bsec
)
7984 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
7985 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7988 return elf_section_type (asec
) == elf_section_type (bsec
);
7991 /* Final phase of ELF linker. */
7993 /* A structure we use to avoid passing large numbers of arguments. */
7995 struct elf_final_link_info
7997 /* General link information. */
7998 struct bfd_link_info
*info
;
8001 /* Symbol string table. */
8002 struct elf_strtab_hash
*symstrtab
;
8003 /* .hash section. */
8005 /* symbol version section (.gnu.version). */
8006 asection
*symver_sec
;
8007 /* Buffer large enough to hold contents of any section. */
8009 /* Buffer large enough to hold external relocs of any section. */
8010 void *external_relocs
;
8011 /* Buffer large enough to hold internal relocs of any section. */
8012 Elf_Internal_Rela
*internal_relocs
;
8013 /* Buffer large enough to hold external local symbols of any input
8015 bfd_byte
*external_syms
;
8016 /* And a buffer for symbol section indices. */
8017 Elf_External_Sym_Shndx
*locsym_shndx
;
8018 /* Buffer large enough to hold internal local symbols of any input
8020 Elf_Internal_Sym
*internal_syms
;
8021 /* Array large enough to hold a symbol index for each local symbol
8022 of any input BFD. */
8024 /* Array large enough to hold a section pointer for each local
8025 symbol of any input BFD. */
8026 asection
**sections
;
8027 /* Buffer for SHT_SYMTAB_SHNDX section. */
8028 Elf_External_Sym_Shndx
*symshndxbuf
;
8029 /* Number of STT_FILE syms seen. */
8030 size_t filesym_count
;
8033 /* This struct is used to pass information to elf_link_output_extsym. */
8035 struct elf_outext_info
8038 bfd_boolean localsyms
;
8039 bfd_boolean file_sym_done
;
8040 struct elf_final_link_info
*flinfo
;
8044 /* Support for evaluating a complex relocation.
8046 Complex relocations are generalized, self-describing relocations. The
8047 implementation of them consists of two parts: complex symbols, and the
8048 relocations themselves.
8050 The relocations are use a reserved elf-wide relocation type code (R_RELC
8051 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8052 information (start bit, end bit, word width, etc) into the addend. This
8053 information is extracted from CGEN-generated operand tables within gas.
8055 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8056 internal) representing prefix-notation expressions, including but not
8057 limited to those sorts of expressions normally encoded as addends in the
8058 addend field. The symbol mangling format is:
8061 | <unary-operator> ':' <node>
8062 | <binary-operator> ':' <node> ':' <node>
8065 <literal> := 's' <digits=N> ':' <N character symbol name>
8066 | 'S' <digits=N> ':' <N character section name>
8070 <binary-operator> := as in C
8071 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8074 set_symbol_value (bfd
*bfd_with_globals
,
8075 Elf_Internal_Sym
*isymbuf
,
8080 struct elf_link_hash_entry
**sym_hashes
;
8081 struct elf_link_hash_entry
*h
;
8082 size_t extsymoff
= locsymcount
;
8084 if (symidx
< locsymcount
)
8086 Elf_Internal_Sym
*sym
;
8088 sym
= isymbuf
+ symidx
;
8089 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
8091 /* It is a local symbol: move it to the
8092 "absolute" section and give it a value. */
8093 sym
->st_shndx
= SHN_ABS
;
8094 sym
->st_value
= val
;
8097 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
8101 /* It is a global symbol: set its link type
8102 to "defined" and give it a value. */
8104 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
8105 h
= sym_hashes
[symidx
- extsymoff
];
8106 while (h
->root
.type
== bfd_link_hash_indirect
8107 || h
->root
.type
== bfd_link_hash_warning
)
8108 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8109 h
->root
.type
= bfd_link_hash_defined
;
8110 h
->root
.u
.def
.value
= val
;
8111 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
8115 resolve_symbol (const char *name
,
8117 struct elf_final_link_info
*flinfo
,
8119 Elf_Internal_Sym
*isymbuf
,
8122 Elf_Internal_Sym
*sym
;
8123 struct bfd_link_hash_entry
*global_entry
;
8124 const char *candidate
= NULL
;
8125 Elf_Internal_Shdr
*symtab_hdr
;
8128 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
8130 for (i
= 0; i
< locsymcount
; ++ i
)
8134 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
8137 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
8138 symtab_hdr
->sh_link
,
8141 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8142 name
, candidate
, (unsigned long) sym
->st_value
);
8144 if (candidate
&& strcmp (candidate
, name
) == 0)
8146 asection
*sec
= flinfo
->sections
[i
];
8148 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
8149 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
8151 printf ("Found symbol with value %8.8lx\n",
8152 (unsigned long) *result
);
8158 /* Hmm, haven't found it yet. perhaps it is a global. */
8159 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
8160 FALSE
, FALSE
, TRUE
);
8164 if (global_entry
->type
== bfd_link_hash_defined
8165 || global_entry
->type
== bfd_link_hash_defweak
)
8167 *result
= (global_entry
->u
.def
.value
8168 + global_entry
->u
.def
.section
->output_section
->vma
8169 + global_entry
->u
.def
.section
->output_offset
);
8171 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8172 global_entry
->root
.string
, (unsigned long) *result
);
8180 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8181 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8182 names like "foo.end" which is the end address of section "foo". */
8185 resolve_section (const char *name
,
8193 for (curr
= sections
; curr
; curr
= curr
->next
)
8194 if (strcmp (curr
->name
, name
) == 0)
8196 *result
= curr
->vma
;
8200 /* Hmm. still haven't found it. try pseudo-section names. */
8201 /* FIXME: This could be coded more efficiently... */
8202 for (curr
= sections
; curr
; curr
= curr
->next
)
8204 len
= strlen (curr
->name
);
8205 if (len
> strlen (name
))
8208 if (strncmp (curr
->name
, name
, len
) == 0)
8210 if (strncmp (".end", name
+ len
, 4) == 0)
8212 *result
= curr
->vma
+ curr
->size
/ bfd_octets_per_byte (abfd
);
8216 /* Insert more pseudo-section names here, if you like. */
8224 undefined_reference (const char *reftype
, const char *name
)
8226 /* xgettext:c-format */
8227 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8232 eval_symbol (bfd_vma
*result
,
8235 struct elf_final_link_info
*flinfo
,
8237 Elf_Internal_Sym
*isymbuf
,
8246 const char *sym
= *symp
;
8248 bfd_boolean symbol_is_section
= FALSE
;
8253 if (len
< 1 || len
> sizeof (symbuf
))
8255 bfd_set_error (bfd_error_invalid_operation
);
8268 *result
= strtoul (sym
, (char **) symp
, 16);
8272 symbol_is_section
= TRUE
;
8276 symlen
= strtol (sym
, (char **) symp
, 10);
8277 sym
= *symp
+ 1; /* Skip the trailing ':'. */
8279 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
8281 bfd_set_error (bfd_error_invalid_operation
);
8285 memcpy (symbuf
, sym
, symlen
);
8286 symbuf
[symlen
] = '\0';
8287 *symp
= sym
+ symlen
;
8289 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8290 the symbol as a section, or vice-versa. so we're pretty liberal in our
8291 interpretation here; section means "try section first", not "must be a
8292 section", and likewise with symbol. */
8294 if (symbol_is_section
)
8296 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
8297 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8298 isymbuf
, locsymcount
))
8300 undefined_reference ("section", symbuf
);
8306 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8307 isymbuf
, locsymcount
)
8308 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8311 undefined_reference ("symbol", symbuf
);
8318 /* All that remains are operators. */
8320 #define UNARY_OP(op) \
8321 if (strncmp (sym, #op, strlen (#op)) == 0) \
8323 sym += strlen (#op); \
8327 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8328 isymbuf, locsymcount, signed_p)) \
8331 *result = op ((bfd_signed_vma) a); \
8337 #define BINARY_OP(op) \
8338 if (strncmp (sym, #op, strlen (#op)) == 0) \
8340 sym += strlen (#op); \
8344 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8345 isymbuf, locsymcount, signed_p)) \
8348 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8349 isymbuf, locsymcount, signed_p)) \
8352 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8382 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
8383 bfd_set_error (bfd_error_invalid_operation
);
8389 put_value (bfd_vma size
,
8390 unsigned long chunksz
,
8395 location
+= (size
- chunksz
);
8397 for (; size
; size
-= chunksz
, location
-= chunksz
)
8402 bfd_put_8 (input_bfd
, x
, location
);
8406 bfd_put_16 (input_bfd
, x
, location
);
8410 bfd_put_32 (input_bfd
, x
, location
);
8411 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8417 bfd_put_64 (input_bfd
, x
, location
);
8418 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8431 get_value (bfd_vma size
,
8432 unsigned long chunksz
,
8439 /* Sanity checks. */
8440 BFD_ASSERT (chunksz
<= sizeof (x
)
8443 && (size
% chunksz
) == 0
8444 && input_bfd
!= NULL
8445 && location
!= NULL
);
8447 if (chunksz
== sizeof (x
))
8449 BFD_ASSERT (size
== chunksz
);
8451 /* Make sure that we do not perform an undefined shift operation.
8452 We know that size == chunksz so there will only be one iteration
8453 of the loop below. */
8457 shift
= 8 * chunksz
;
8459 for (; size
; size
-= chunksz
, location
+= chunksz
)
8464 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
8467 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
8470 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
8474 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
8485 decode_complex_addend (unsigned long *start
, /* in bits */
8486 unsigned long *oplen
, /* in bits */
8487 unsigned long *len
, /* in bits */
8488 unsigned long *wordsz
, /* in bytes */
8489 unsigned long *chunksz
, /* in bytes */
8490 unsigned long *lsb0_p
,
8491 unsigned long *signed_p
,
8492 unsigned long *trunc_p
,
8493 unsigned long encoded
)
8495 * start
= encoded
& 0x3F;
8496 * len
= (encoded
>> 6) & 0x3F;
8497 * oplen
= (encoded
>> 12) & 0x3F;
8498 * wordsz
= (encoded
>> 18) & 0xF;
8499 * chunksz
= (encoded
>> 22) & 0xF;
8500 * lsb0_p
= (encoded
>> 27) & 1;
8501 * signed_p
= (encoded
>> 28) & 1;
8502 * trunc_p
= (encoded
>> 29) & 1;
8505 bfd_reloc_status_type
8506 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
8507 asection
*input_section ATTRIBUTE_UNUSED
,
8509 Elf_Internal_Rela
*rel
,
8512 bfd_vma shift
, x
, mask
;
8513 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
8514 bfd_reloc_status_type r
;
8516 /* Perform this reloc, since it is complex.
8517 (this is not to say that it necessarily refers to a complex
8518 symbol; merely that it is a self-describing CGEN based reloc.
8519 i.e. the addend has the complete reloc information (bit start, end,
8520 word size, etc) encoded within it.). */
8522 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
8523 &chunksz
, &lsb0_p
, &signed_p
,
8524 &trunc_p
, rel
->r_addend
);
8526 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
8529 shift
= (start
+ 1) - len
;
8531 shift
= (8 * wordsz
) - (start
+ len
);
8533 x
= get_value (wordsz
, chunksz
, input_bfd
,
8534 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8537 printf ("Doing complex reloc: "
8538 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8539 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8540 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8541 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
8542 oplen
, (unsigned long) x
, (unsigned long) mask
,
8543 (unsigned long) relocation
);
8548 /* Now do an overflow check. */
8549 r
= bfd_check_overflow ((signed_p
8550 ? complain_overflow_signed
8551 : complain_overflow_unsigned
),
8552 len
, 0, (8 * wordsz
),
8556 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
8559 printf (" relocation: %8.8lx\n"
8560 " shifted mask: %8.8lx\n"
8561 " shifted/masked reloc: %8.8lx\n"
8562 " result: %8.8lx\n",
8563 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
8564 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
8566 put_value (wordsz
, chunksz
, input_bfd
, x
,
8567 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8571 /* Functions to read r_offset from external (target order) reloc
8572 entry. Faster than bfd_getl32 et al, because we let the compiler
8573 know the value is aligned. */
8576 ext32l_r_offset (const void *p
)
8583 const union aligned32
*a
8584 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8586 uint32_t aval
= ( (uint32_t) a
->c
[0]
8587 | (uint32_t) a
->c
[1] << 8
8588 | (uint32_t) a
->c
[2] << 16
8589 | (uint32_t) a
->c
[3] << 24);
8594 ext32b_r_offset (const void *p
)
8601 const union aligned32
*a
8602 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8604 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
8605 | (uint32_t) a
->c
[1] << 16
8606 | (uint32_t) a
->c
[2] << 8
8607 | (uint32_t) a
->c
[3]);
8611 #ifdef BFD_HOST_64_BIT
8613 ext64l_r_offset (const void *p
)
8620 const union aligned64
*a
8621 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8623 uint64_t aval
= ( (uint64_t) a
->c
[0]
8624 | (uint64_t) a
->c
[1] << 8
8625 | (uint64_t) a
->c
[2] << 16
8626 | (uint64_t) a
->c
[3] << 24
8627 | (uint64_t) a
->c
[4] << 32
8628 | (uint64_t) a
->c
[5] << 40
8629 | (uint64_t) a
->c
[6] << 48
8630 | (uint64_t) a
->c
[7] << 56);
8635 ext64b_r_offset (const void *p
)
8642 const union aligned64
*a
8643 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8645 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
8646 | (uint64_t) a
->c
[1] << 48
8647 | (uint64_t) a
->c
[2] << 40
8648 | (uint64_t) a
->c
[3] << 32
8649 | (uint64_t) a
->c
[4] << 24
8650 | (uint64_t) a
->c
[5] << 16
8651 | (uint64_t) a
->c
[6] << 8
8652 | (uint64_t) a
->c
[7]);
8657 /* When performing a relocatable link, the input relocations are
8658 preserved. But, if they reference global symbols, the indices
8659 referenced must be updated. Update all the relocations found in
8663 elf_link_adjust_relocs (bfd
*abfd
,
8665 struct bfd_elf_section_reloc_data
*reldata
,
8667 struct bfd_link_info
*info
)
8670 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8672 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8673 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8674 bfd_vma r_type_mask
;
8676 unsigned int count
= reldata
->count
;
8677 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
8679 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
8681 swap_in
= bed
->s
->swap_reloc_in
;
8682 swap_out
= bed
->s
->swap_reloc_out
;
8684 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
8686 swap_in
= bed
->s
->swap_reloca_in
;
8687 swap_out
= bed
->s
->swap_reloca_out
;
8692 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
8695 if (bed
->s
->arch_size
== 32)
8702 r_type_mask
= 0xffffffff;
8706 erela
= reldata
->hdr
->contents
;
8707 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
8709 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
8712 if (*rel_hash
== NULL
)
8715 if ((*rel_hash
)->indx
== -2
8716 && info
->gc_sections
8717 && ! info
->gc_keep_exported
)
8719 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8720 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
8722 (*rel_hash
)->root
.root
.string
);
8723 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
8725 bfd_set_error (bfd_error_invalid_operation
);
8728 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
8730 (*swap_in
) (abfd
, erela
, irela
);
8731 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
8732 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
8733 | (irela
[j
].r_info
& r_type_mask
));
8734 (*swap_out
) (abfd
, irela
, erela
);
8737 if (bed
->elf_backend_update_relocs
)
8738 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
8740 if (sort
&& count
!= 0)
8742 bfd_vma (*ext_r_off
) (const void *);
8745 bfd_byte
*base
, *end
, *p
, *loc
;
8746 bfd_byte
*buf
= NULL
;
8748 if (bed
->s
->arch_size
== 32)
8750 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8751 ext_r_off
= ext32l_r_offset
;
8752 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8753 ext_r_off
= ext32b_r_offset
;
8759 #ifdef BFD_HOST_64_BIT
8760 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8761 ext_r_off
= ext64l_r_offset
;
8762 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8763 ext_r_off
= ext64b_r_offset
;
8769 /* Must use a stable sort here. A modified insertion sort,
8770 since the relocs are mostly sorted already. */
8771 elt_size
= reldata
->hdr
->sh_entsize
;
8772 base
= reldata
->hdr
->contents
;
8773 end
= base
+ count
* elt_size
;
8774 if (elt_size
> sizeof (Elf64_External_Rela
))
8777 /* Ensure the first element is lowest. This acts as a sentinel,
8778 speeding the main loop below. */
8779 r_off
= (*ext_r_off
) (base
);
8780 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
8782 bfd_vma r_off2
= (*ext_r_off
) (p
);
8791 /* Don't just swap *base and *loc as that changes the order
8792 of the original base[0] and base[1] if they happen to
8793 have the same r_offset. */
8794 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
8795 memcpy (onebuf
, loc
, elt_size
);
8796 memmove (base
+ elt_size
, base
, loc
- base
);
8797 memcpy (base
, onebuf
, elt_size
);
8800 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
8802 /* base to p is sorted, *p is next to insert. */
8803 r_off
= (*ext_r_off
) (p
);
8804 /* Search the sorted region for location to insert. */
8806 while (r_off
< (*ext_r_off
) (loc
))
8811 /* Chances are there is a run of relocs to insert here,
8812 from one of more input files. Files are not always
8813 linked in order due to the way elf_link_input_bfd is
8814 called. See pr17666. */
8815 size_t sortlen
= p
- loc
;
8816 bfd_vma r_off2
= (*ext_r_off
) (loc
);
8817 size_t runlen
= elt_size
;
8818 size_t buf_size
= 96 * 1024;
8819 while (p
+ runlen
< end
8820 && (sortlen
<= buf_size
8821 || runlen
+ elt_size
<= buf_size
)
8822 && r_off2
> (*ext_r_off
) (p
+ runlen
))
8826 buf
= bfd_malloc (buf_size
);
8830 if (runlen
< sortlen
)
8832 memcpy (buf
, p
, runlen
);
8833 memmove (loc
+ runlen
, loc
, sortlen
);
8834 memcpy (loc
, buf
, runlen
);
8838 memcpy (buf
, loc
, sortlen
);
8839 memmove (loc
, p
, runlen
);
8840 memcpy (loc
+ runlen
, buf
, sortlen
);
8842 p
+= runlen
- elt_size
;
8845 /* Hashes are no longer valid. */
8846 free (reldata
->hashes
);
8847 reldata
->hashes
= NULL
;
8853 struct elf_link_sort_rela
8859 enum elf_reloc_type_class type
;
8860 /* We use this as an array of size int_rels_per_ext_rel. */
8861 Elf_Internal_Rela rela
[1];
8865 elf_link_sort_cmp1 (const void *A
, const void *B
)
8867 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8868 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8869 int relativea
, relativeb
;
8871 relativea
= a
->type
== reloc_class_relative
;
8872 relativeb
= b
->type
== reloc_class_relative
;
8874 if (relativea
< relativeb
)
8876 if (relativea
> relativeb
)
8878 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
8880 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
8882 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8884 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8890 elf_link_sort_cmp2 (const void *A
, const void *B
)
8892 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8893 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8895 if (a
->type
< b
->type
)
8897 if (a
->type
> b
->type
)
8899 if (a
->u
.offset
< b
->u
.offset
)
8901 if (a
->u
.offset
> b
->u
.offset
)
8903 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8905 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8911 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
8913 asection
*dynamic_relocs
;
8916 bfd_size_type count
, size
;
8917 size_t i
, ret
, sort_elt
, ext_size
;
8918 bfd_byte
*sort
, *s_non_relative
, *p
;
8919 struct elf_link_sort_rela
*sq
;
8920 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8921 int i2e
= bed
->s
->int_rels_per_ext_rel
;
8922 unsigned int opb
= bfd_octets_per_byte (abfd
);
8923 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8924 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8925 struct bfd_link_order
*lo
;
8927 bfd_boolean use_rela
;
8929 /* Find a dynamic reloc section. */
8930 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
8931 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
8932 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
8933 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8935 bfd_boolean use_rela_initialised
= FALSE
;
8937 /* This is just here to stop gcc from complaining.
8938 Its initialization checking code is not perfect. */
8941 /* Both sections are present. Examine the sizes
8942 of the indirect sections to help us choose. */
8943 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8944 if (lo
->type
== bfd_indirect_link_order
)
8946 asection
*o
= lo
->u
.indirect
.section
;
8948 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8950 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8951 /* Section size is divisible by both rel and rela sizes.
8952 It is of no help to us. */
8956 /* Section size is only divisible by rela. */
8957 if (use_rela_initialised
&& !use_rela
)
8959 _bfd_error_handler (_("%pB: unable to sort relocs - "
8960 "they are in more than one size"),
8962 bfd_set_error (bfd_error_invalid_operation
);
8968 use_rela_initialised
= TRUE
;
8972 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8974 /* Section size is only divisible by rel. */
8975 if (use_rela_initialised
&& use_rela
)
8977 _bfd_error_handler (_("%pB: unable to sort relocs - "
8978 "they are in more than one size"),
8980 bfd_set_error (bfd_error_invalid_operation
);
8986 use_rela_initialised
= TRUE
;
8991 /* The section size is not divisible by either -
8992 something is wrong. */
8993 _bfd_error_handler (_("%pB: unable to sort relocs - "
8994 "they are of an unknown size"), abfd
);
8995 bfd_set_error (bfd_error_invalid_operation
);
9000 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9001 if (lo
->type
== bfd_indirect_link_order
)
9003 asection
*o
= lo
->u
.indirect
.section
;
9005 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9007 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9008 /* Section size is divisible by both rel and rela sizes.
9009 It is of no help to us. */
9013 /* Section size is only divisible by rela. */
9014 if (use_rela_initialised
&& !use_rela
)
9016 _bfd_error_handler (_("%pB: unable to sort relocs - "
9017 "they are in more than one size"),
9019 bfd_set_error (bfd_error_invalid_operation
);
9025 use_rela_initialised
= TRUE
;
9029 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9031 /* Section size is only divisible by rel. */
9032 if (use_rela_initialised
&& use_rela
)
9034 _bfd_error_handler (_("%pB: unable to sort relocs - "
9035 "they are in more than one size"),
9037 bfd_set_error (bfd_error_invalid_operation
);
9043 use_rela_initialised
= TRUE
;
9048 /* The section size is not divisible by either -
9049 something is wrong. */
9050 _bfd_error_handler (_("%pB: unable to sort relocs - "
9051 "they are of an unknown size"), abfd
);
9052 bfd_set_error (bfd_error_invalid_operation
);
9057 if (! use_rela_initialised
)
9061 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
9063 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9070 dynamic_relocs
= rela_dyn
;
9071 ext_size
= bed
->s
->sizeof_rela
;
9072 swap_in
= bed
->s
->swap_reloca_in
;
9073 swap_out
= bed
->s
->swap_reloca_out
;
9077 dynamic_relocs
= rel_dyn
;
9078 ext_size
= bed
->s
->sizeof_rel
;
9079 swap_in
= bed
->s
->swap_reloc_in
;
9080 swap_out
= bed
->s
->swap_reloc_out
;
9084 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9085 if (lo
->type
== bfd_indirect_link_order
)
9086 size
+= lo
->u
.indirect
.section
->size
;
9088 if (size
!= dynamic_relocs
->size
)
9091 sort_elt
= (sizeof (struct elf_link_sort_rela
)
9092 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
9094 count
= dynamic_relocs
->size
/ ext_size
;
9097 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
9101 (*info
->callbacks
->warning
)
9102 (info
, _("not enough memory to sort relocations"), 0, abfd
, 0, 0);
9106 if (bed
->s
->arch_size
== 32)
9107 r_sym_mask
= ~(bfd_vma
) 0xff;
9109 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
9111 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9112 if (lo
->type
== bfd_indirect_link_order
)
9114 bfd_byte
*erel
, *erelend
;
9115 asection
*o
= lo
->u
.indirect
.section
;
9117 if (o
->contents
== NULL
&& o
->size
!= 0)
9119 /* This is a reloc section that is being handled as a normal
9120 section. See bfd_section_from_shdr. We can't combine
9121 relocs in this case. */
9126 erelend
= o
->contents
+ o
->size
;
9127 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
9129 while (erel
< erelend
)
9131 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9133 (*swap_in
) (abfd
, erel
, s
->rela
);
9134 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
9135 s
->u
.sym_mask
= r_sym_mask
;
9141 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
9143 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
9145 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9146 if (s
->type
!= reloc_class_relative
)
9152 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
9153 for (; i
< count
; i
++, p
+= sort_elt
)
9155 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
9156 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
9158 sp
->u
.offset
= sq
->rela
->r_offset
;
9161 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
9163 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
9164 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
9166 /* We have plt relocs in .rela.dyn. */
9167 sq
= (struct elf_link_sort_rela
*) sort
;
9168 for (i
= 0; i
< count
; i
++)
9169 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
9171 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
9173 struct bfd_link_order
**plo
;
9174 /* Put srelplt link_order last. This is so the output_offset
9175 set in the next loop is correct for DT_JMPREL. */
9176 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
9177 if ((*plo
)->type
== bfd_indirect_link_order
9178 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
9184 plo
= &(*plo
)->next
;
9187 dynamic_relocs
->map_tail
.link_order
= lo
;
9192 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9193 if (lo
->type
== bfd_indirect_link_order
)
9195 bfd_byte
*erel
, *erelend
;
9196 asection
*o
= lo
->u
.indirect
.section
;
9199 erelend
= o
->contents
+ o
->size
;
9200 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
9201 while (erel
< erelend
)
9203 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9204 (*swap_out
) (abfd
, s
->rela
, erel
);
9211 *psec
= dynamic_relocs
;
9215 /* Add a symbol to the output symbol string table. */
9218 elf_link_output_symstrtab (struct elf_final_link_info
*flinfo
,
9220 Elf_Internal_Sym
*elfsym
,
9221 asection
*input_sec
,
9222 struct elf_link_hash_entry
*h
)
9224 int (*output_symbol_hook
)
9225 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
9226 struct elf_link_hash_entry
*);
9227 struct elf_link_hash_table
*hash_table
;
9228 const struct elf_backend_data
*bed
;
9229 bfd_size_type strtabsize
;
9231 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9233 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9234 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
9235 if (output_symbol_hook
!= NULL
)
9237 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
9244 || (input_sec
->flags
& SEC_EXCLUDE
))
9245 elfsym
->st_name
= (unsigned long) -1;
9248 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9249 to get the final offset for st_name. */
9251 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
9253 if (elfsym
->st_name
== (unsigned long) -1)
9257 hash_table
= elf_hash_table (flinfo
->info
);
9258 strtabsize
= hash_table
->strtabsize
;
9259 if (strtabsize
<= hash_table
->strtabcount
)
9261 strtabsize
+= strtabsize
;
9262 hash_table
->strtabsize
= strtabsize
;
9263 strtabsize
*= sizeof (*hash_table
->strtab
);
9265 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
9267 if (hash_table
->strtab
== NULL
)
9270 hash_table
->strtab
[hash_table
->strtabcount
].sym
= *elfsym
;
9271 hash_table
->strtab
[hash_table
->strtabcount
].dest_index
9272 = hash_table
->strtabcount
;
9273 hash_table
->strtab
[hash_table
->strtabcount
].destshndx_index
9274 = flinfo
->symshndxbuf
? bfd_get_symcount (flinfo
->output_bfd
) : 0;
9276 bfd_get_symcount (flinfo
->output_bfd
) += 1;
9277 hash_table
->strtabcount
+= 1;
9282 /* Swap symbols out to the symbol table and flush the output symbols to
9286 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
9288 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
9291 const struct elf_backend_data
*bed
;
9293 Elf_Internal_Shdr
*hdr
;
9297 if (!hash_table
->strtabcount
)
9300 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9302 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9304 amt
= bed
->s
->sizeof_sym
* hash_table
->strtabcount
;
9305 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
9309 if (flinfo
->symshndxbuf
)
9311 amt
= sizeof (Elf_External_Sym_Shndx
);
9312 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
9313 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
9314 if (flinfo
->symshndxbuf
== NULL
)
9321 for (i
= 0; i
< hash_table
->strtabcount
; i
++)
9323 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
9324 if (elfsym
->sym
.st_name
== (unsigned long) -1)
9325 elfsym
->sym
.st_name
= 0;
9328 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
9329 elfsym
->sym
.st_name
);
9330 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
9331 ((bfd_byte
*) symbuf
9332 + (elfsym
->dest_index
9333 * bed
->s
->sizeof_sym
)),
9334 (flinfo
->symshndxbuf
9335 + elfsym
->destshndx_index
));
9338 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
9339 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
9340 amt
= hash_table
->strtabcount
* bed
->s
->sizeof_sym
;
9341 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
9342 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
9344 hdr
->sh_size
+= amt
;
9352 free (hash_table
->strtab
);
9353 hash_table
->strtab
= NULL
;
9358 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9361 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
9363 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
9364 && sym
->st_shndx
< SHN_LORESERVE
)
9366 /* The gABI doesn't support dynamic symbols in output sections
9369 /* xgettext:c-format */
9370 (_("%pB: too many sections: %d (>= %d)"),
9371 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
9372 bfd_set_error (bfd_error_nonrepresentable_section
);
9378 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9379 allowing an unsatisfied unversioned symbol in the DSO to match a
9380 versioned symbol that would normally require an explicit version.
9381 We also handle the case that a DSO references a hidden symbol
9382 which may be satisfied by a versioned symbol in another DSO. */
9385 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
9386 const struct elf_backend_data
*bed
,
9387 struct elf_link_hash_entry
*h
)
9390 struct elf_link_loaded_list
*loaded
;
9392 if (!is_elf_hash_table (info
->hash
))
9395 /* Check indirect symbol. */
9396 while (h
->root
.type
== bfd_link_hash_indirect
)
9397 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9399 switch (h
->root
.type
)
9405 case bfd_link_hash_undefined
:
9406 case bfd_link_hash_undefweak
:
9407 abfd
= h
->root
.u
.undef
.abfd
;
9409 || (abfd
->flags
& DYNAMIC
) == 0
9410 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
9414 case bfd_link_hash_defined
:
9415 case bfd_link_hash_defweak
:
9416 abfd
= h
->root
.u
.def
.section
->owner
;
9419 case bfd_link_hash_common
:
9420 abfd
= h
->root
.u
.c
.p
->section
->owner
;
9423 BFD_ASSERT (abfd
!= NULL
);
9425 for (loaded
= elf_hash_table (info
)->loaded
;
9427 loaded
= loaded
->next
)
9430 Elf_Internal_Shdr
*hdr
;
9434 Elf_Internal_Shdr
*versymhdr
;
9435 Elf_Internal_Sym
*isym
;
9436 Elf_Internal_Sym
*isymend
;
9437 Elf_Internal_Sym
*isymbuf
;
9438 Elf_External_Versym
*ever
;
9439 Elf_External_Versym
*extversym
;
9441 input
= loaded
->abfd
;
9443 /* We check each DSO for a possible hidden versioned definition. */
9445 || (input
->flags
& DYNAMIC
) == 0
9446 || elf_dynversym (input
) == 0)
9449 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
9451 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9452 if (elf_bad_symtab (input
))
9454 extsymcount
= symcount
;
9459 extsymcount
= symcount
- hdr
->sh_info
;
9460 extsymoff
= hdr
->sh_info
;
9463 if (extsymcount
== 0)
9466 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
9468 if (isymbuf
== NULL
)
9471 /* Read in any version definitions. */
9472 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
9473 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
9474 if (extversym
== NULL
)
9477 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
9478 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
9479 != versymhdr
->sh_size
))
9487 ever
= extversym
+ extsymoff
;
9488 isymend
= isymbuf
+ extsymcount
;
9489 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
9492 Elf_Internal_Versym iver
;
9493 unsigned short version_index
;
9495 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
9496 || isym
->st_shndx
== SHN_UNDEF
)
9499 name
= bfd_elf_string_from_elf_section (input
,
9502 if (strcmp (name
, h
->root
.root
.string
) != 0)
9505 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
9507 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
9509 && h
->forced_local
))
9511 /* If we have a non-hidden versioned sym, then it should
9512 have provided a definition for the undefined sym unless
9513 it is defined in a non-shared object and forced local.
9518 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
9519 if (version_index
== 1 || version_index
== 2)
9521 /* This is the base or first version. We can use it. */
9535 /* Convert ELF common symbol TYPE. */
9538 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
9540 /* Commom symbol can only appear in relocatable link. */
9541 if (!bfd_link_relocatable (info
))
9543 switch (info
->elf_stt_common
)
9547 case elf_stt_common
:
9550 case no_elf_stt_common
:
9557 /* Add an external symbol to the symbol table. This is called from
9558 the hash table traversal routine. When generating a shared object,
9559 we go through the symbol table twice. The first time we output
9560 anything that might have been forced to local scope in a version
9561 script. The second time we output the symbols that are still
9565 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
9567 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
9568 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
9569 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
9571 Elf_Internal_Sym sym
;
9572 asection
*input_sec
;
9573 const struct elf_backend_data
*bed
;
9578 if (h
->root
.type
== bfd_link_hash_warning
)
9580 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9581 if (h
->root
.type
== bfd_link_hash_new
)
9585 /* Decide whether to output this symbol in this pass. */
9586 if (eoinfo
->localsyms
)
9588 if (!h
->forced_local
)
9593 if (h
->forced_local
)
9597 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9599 if (h
->root
.type
== bfd_link_hash_undefined
)
9601 /* If we have an undefined symbol reference here then it must have
9602 come from a shared library that is being linked in. (Undefined
9603 references in regular files have already been handled unless
9604 they are in unreferenced sections which are removed by garbage
9606 bfd_boolean ignore_undef
= FALSE
;
9608 /* Some symbols may be special in that the fact that they're
9609 undefined can be safely ignored - let backend determine that. */
9610 if (bed
->elf_backend_ignore_undef_symbol
)
9611 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
9613 /* If we are reporting errors for this situation then do so now. */
9616 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
9617 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
9618 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
9619 (*flinfo
->info
->callbacks
->undefined_symbol
)
9620 (flinfo
->info
, h
->root
.root
.string
,
9621 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
,
9623 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
);
9625 /* Strip a global symbol defined in a discarded section. */
9630 /* We should also warn if a forced local symbol is referenced from
9631 shared libraries. */
9632 if (bfd_link_executable (flinfo
->info
)
9637 && h
->ref_dynamic_nonweak
9638 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
9642 struct elf_link_hash_entry
*hi
= h
;
9644 /* Check indirect symbol. */
9645 while (hi
->root
.type
== bfd_link_hash_indirect
)
9646 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
9648 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
9649 /* xgettext:c-format */
9650 msg
= _("%pB: internal symbol `%s' in %pB is referenced by DSO");
9651 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
9652 /* xgettext:c-format */
9653 msg
= _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
9655 /* xgettext:c-format */
9656 msg
= _("%pB: local symbol `%s' in %pB is referenced by DSO");
9657 def_bfd
= flinfo
->output_bfd
;
9658 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
9659 def_bfd
= hi
->root
.u
.def
.section
->owner
;
9660 _bfd_error_handler (msg
, flinfo
->output_bfd
,
9661 h
->root
.root
.string
, def_bfd
);
9662 bfd_set_error (bfd_error_bad_value
);
9663 eoinfo
->failed
= TRUE
;
9667 /* We don't want to output symbols that have never been mentioned by
9668 a regular file, or that we have been told to strip. However, if
9669 h->indx is set to -2, the symbol is used by a reloc and we must
9674 else if ((h
->def_dynamic
9676 || h
->root
.type
== bfd_link_hash_new
)
9680 else if (flinfo
->info
->strip
== strip_all
)
9682 else if (flinfo
->info
->strip
== strip_some
9683 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
9684 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
9686 else if ((h
->root
.type
== bfd_link_hash_defined
9687 || h
->root
.type
== bfd_link_hash_defweak
)
9688 && ((flinfo
->info
->strip_discarded
9689 && discarded_section (h
->root
.u
.def
.section
))
9690 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
9691 && h
->root
.u
.def
.section
->owner
!= NULL
9692 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
9694 else if ((h
->root
.type
== bfd_link_hash_undefined
9695 || h
->root
.type
== bfd_link_hash_undefweak
)
9696 && h
->root
.u
.undef
.abfd
!= NULL
9697 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
9702 /* If we're stripping it, and it's not a dynamic symbol, there's
9703 nothing else to do. However, if it is a forced local symbol or
9704 an ifunc symbol we need to give the backend finish_dynamic_symbol
9705 function a chance to make it dynamic. */
9708 && type
!= STT_GNU_IFUNC
9709 && !h
->forced_local
)
9713 sym
.st_size
= h
->size
;
9714 sym
.st_other
= h
->other
;
9715 switch (h
->root
.type
)
9718 case bfd_link_hash_new
:
9719 case bfd_link_hash_warning
:
9723 case bfd_link_hash_undefined
:
9724 case bfd_link_hash_undefweak
:
9725 input_sec
= bfd_und_section_ptr
;
9726 sym
.st_shndx
= SHN_UNDEF
;
9729 case bfd_link_hash_defined
:
9730 case bfd_link_hash_defweak
:
9732 input_sec
= h
->root
.u
.def
.section
;
9733 if (input_sec
->output_section
!= NULL
)
9736 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
9737 input_sec
->output_section
);
9738 if (sym
.st_shndx
== SHN_BAD
)
9741 /* xgettext:c-format */
9742 (_("%pB: could not find output section %pA for input section %pA"),
9743 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
9744 bfd_set_error (bfd_error_nonrepresentable_section
);
9745 eoinfo
->failed
= TRUE
;
9749 /* ELF symbols in relocatable files are section relative,
9750 but in nonrelocatable files they are virtual
9752 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
9753 if (!bfd_link_relocatable (flinfo
->info
))
9755 sym
.st_value
+= input_sec
->output_section
->vma
;
9756 if (h
->type
== STT_TLS
)
9758 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
9759 if (tls_sec
!= NULL
)
9760 sym
.st_value
-= tls_sec
->vma
;
9766 BFD_ASSERT (input_sec
->owner
== NULL
9767 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
9768 sym
.st_shndx
= SHN_UNDEF
;
9769 input_sec
= bfd_und_section_ptr
;
9774 case bfd_link_hash_common
:
9775 input_sec
= h
->root
.u
.c
.p
->section
;
9776 sym
.st_shndx
= bed
->common_section_index (input_sec
);
9777 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
9780 case bfd_link_hash_indirect
:
9781 /* These symbols are created by symbol versioning. They point
9782 to the decorated version of the name. For example, if the
9783 symbol foo@@GNU_1.2 is the default, which should be used when
9784 foo is used with no version, then we add an indirect symbol
9785 foo which points to foo@@GNU_1.2. We ignore these symbols,
9786 since the indirected symbol is already in the hash table. */
9790 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
9791 switch (h
->root
.type
)
9793 case bfd_link_hash_common
:
9794 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9796 case bfd_link_hash_defined
:
9797 case bfd_link_hash_defweak
:
9798 if (bed
->common_definition (&sym
))
9799 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9803 case bfd_link_hash_undefined
:
9804 case bfd_link_hash_undefweak
:
9810 if (h
->forced_local
)
9812 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
9813 /* Turn off visibility on local symbol. */
9814 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
9816 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9817 else if (h
->unique_global
&& h
->def_regular
)
9818 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
9819 else if (h
->root
.type
== bfd_link_hash_undefweak
9820 || h
->root
.type
== bfd_link_hash_defweak
)
9821 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
9823 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
9824 sym
.st_target_internal
= h
->target_internal
;
9826 /* Give the processor backend a chance to tweak the symbol value,
9827 and also to finish up anything that needs to be done for this
9828 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9829 forced local syms when non-shared is due to a historical quirk.
9830 STT_GNU_IFUNC symbol must go through PLT. */
9831 if ((h
->type
== STT_GNU_IFUNC
9833 && !bfd_link_relocatable (flinfo
->info
))
9834 || ((h
->dynindx
!= -1
9836 && ((bfd_link_pic (flinfo
->info
)
9837 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
9838 || h
->root
.type
!= bfd_link_hash_undefweak
))
9839 || !h
->forced_local
)
9840 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
9842 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
9843 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
9845 eoinfo
->failed
= TRUE
;
9850 /* If we are marking the symbol as undefined, and there are no
9851 non-weak references to this symbol from a regular object, then
9852 mark the symbol as weak undefined; if there are non-weak
9853 references, mark the symbol as strong. We can't do this earlier,
9854 because it might not be marked as undefined until the
9855 finish_dynamic_symbol routine gets through with it. */
9856 if (sym
.st_shndx
== SHN_UNDEF
9858 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
9859 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
9862 type
= ELF_ST_TYPE (sym
.st_info
);
9864 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9865 if (type
== STT_GNU_IFUNC
)
9868 if (h
->ref_regular_nonweak
)
9869 bindtype
= STB_GLOBAL
;
9871 bindtype
= STB_WEAK
;
9872 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
9875 /* If this is a symbol defined in a dynamic library, don't use the
9876 symbol size from the dynamic library. Relinking an executable
9877 against a new library may introduce gratuitous changes in the
9878 executable's symbols if we keep the size. */
9879 if (sym
.st_shndx
== SHN_UNDEF
9884 /* If a non-weak symbol with non-default visibility is not defined
9885 locally, it is a fatal error. */
9886 if (!bfd_link_relocatable (flinfo
->info
)
9887 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
9888 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
9889 && h
->root
.type
== bfd_link_hash_undefined
9894 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
9895 /* xgettext:c-format */
9896 msg
= _("%pB: protected symbol `%s' isn't defined");
9897 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
9898 /* xgettext:c-format */
9899 msg
= _("%pB: internal symbol `%s' isn't defined");
9901 /* xgettext:c-format */
9902 msg
= _("%pB: hidden symbol `%s' isn't defined");
9903 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
9904 bfd_set_error (bfd_error_bad_value
);
9905 eoinfo
->failed
= TRUE
;
9909 /* If this symbol should be put in the .dynsym section, then put it
9910 there now. We already know the symbol index. We also fill in
9911 the entry in the .hash section. */
9912 if (elf_hash_table (flinfo
->info
)->dynsym
!= NULL
9914 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
)
9918 /* Since there is no version information in the dynamic string,
9919 if there is no version info in symbol version section, we will
9920 have a run-time problem if not linking executable, referenced
9921 by shared library, or not bound locally. */
9922 if (h
->verinfo
.verdef
== NULL
9923 && (!bfd_link_executable (flinfo
->info
)
9925 || !h
->def_regular
))
9927 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
9929 if (p
&& p
[1] != '\0')
9932 /* xgettext:c-format */
9933 (_("%pB: no symbol version section for versioned symbol `%s'"),
9934 flinfo
->output_bfd
, h
->root
.root
.string
);
9935 eoinfo
->failed
= TRUE
;
9940 sym
.st_name
= h
->dynstr_index
;
9941 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
9942 + h
->dynindx
* bed
->s
->sizeof_sym
);
9943 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
9945 eoinfo
->failed
= TRUE
;
9948 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
9950 if (flinfo
->hash_sec
!= NULL
)
9952 size_t hash_entry_size
;
9953 bfd_byte
*bucketpos
;
9958 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
9959 bucket
= h
->u
.elf_hash_value
% bucketcount
;
9962 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
9963 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
9964 + (bucket
+ 2) * hash_entry_size
);
9965 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
9966 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
9968 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
9969 ((bfd_byte
*) flinfo
->hash_sec
->contents
9970 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
9973 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
9975 Elf_Internal_Versym iversym
;
9976 Elf_External_Versym
*eversym
;
9978 if (!h
->def_regular
)
9980 if (h
->verinfo
.verdef
== NULL
9981 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
9982 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
9983 iversym
.vs_vers
= 0;
9985 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
9989 if (h
->verinfo
.vertree
== NULL
)
9990 iversym
.vs_vers
= 1;
9992 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
9993 if (flinfo
->info
->create_default_symver
)
9997 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9999 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
10000 iversym
.vs_vers
|= VERSYM_HIDDEN
;
10002 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
10003 eversym
+= h
->dynindx
;
10004 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
10008 /* If the symbol is undefined, and we didn't output it to .dynsym,
10009 strip it from .symtab too. Obviously we can't do this for
10010 relocatable output or when needed for --emit-relocs. */
10011 else if (input_sec
== bfd_und_section_ptr
10013 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10014 && (h
->mark
!= 1 || ELF_ST_BIND (sym
.st_info
) != STB_GLOBAL
)
10015 && !bfd_link_relocatable (flinfo
->info
))
10018 /* Also strip others that we couldn't earlier due to dynamic symbol
10022 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
10025 /* Output a FILE symbol so that following locals are not associated
10026 with the wrong input file. We need one for forced local symbols
10027 if we've seen more than one FILE symbol or when we have exactly
10028 one FILE symbol but global symbols are present in a file other
10029 than the one with the FILE symbol. We also need one if linker
10030 defined symbols are present. In practice these conditions are
10031 always met, so just emit the FILE symbol unconditionally. */
10032 if (eoinfo
->localsyms
10033 && !eoinfo
->file_sym_done
10034 && eoinfo
->flinfo
->filesym_count
!= 0)
10036 Elf_Internal_Sym fsym
;
10038 memset (&fsym
, 0, sizeof (fsym
));
10039 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10040 fsym
.st_shndx
= SHN_ABS
;
10041 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
10042 bfd_und_section_ptr
, NULL
))
10045 eoinfo
->file_sym_done
= TRUE
;
10048 indx
= bfd_get_symcount (flinfo
->output_bfd
);
10049 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
10053 eoinfo
->failed
= TRUE
;
10058 else if (h
->indx
== -2)
10064 /* Return TRUE if special handling is done for relocs in SEC against
10065 symbols defined in discarded sections. */
10068 elf_section_ignore_discarded_relocs (asection
*sec
)
10070 const struct elf_backend_data
*bed
;
10072 switch (sec
->sec_info_type
)
10074 case SEC_INFO_TYPE_STABS
:
10075 case SEC_INFO_TYPE_EH_FRAME
:
10076 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10082 bed
= get_elf_backend_data (sec
->owner
);
10083 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
10084 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
10090 /* Return a mask saying how ld should treat relocations in SEC against
10091 symbols defined in discarded sections. If this function returns
10092 COMPLAIN set, ld will issue a warning message. If this function
10093 returns PRETEND set, and the discarded section was link-once and the
10094 same size as the kept link-once section, ld will pretend that the
10095 symbol was actually defined in the kept section. Otherwise ld will
10096 zero the reloc (at least that is the intent, but some cooperation by
10097 the target dependent code is needed, particularly for REL targets). */
10100 _bfd_elf_default_action_discarded (asection
*sec
)
10102 if (sec
->flags
& SEC_DEBUGGING
)
10105 if (strcmp (".eh_frame", sec
->name
) == 0)
10108 if (strcmp (".gcc_except_table", sec
->name
) == 0)
10111 return COMPLAIN
| PRETEND
;
10114 /* Find a match between a section and a member of a section group. */
10117 match_group_member (asection
*sec
, asection
*group
,
10118 struct bfd_link_info
*info
)
10120 asection
*first
= elf_next_in_group (group
);
10121 asection
*s
= first
;
10125 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
10128 s
= elf_next_in_group (s
);
10136 /* Check if the kept section of a discarded section SEC can be used
10137 to replace it. Return the replacement if it is OK. Otherwise return
10141 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
10145 kept
= sec
->kept_section
;
10148 if ((kept
->flags
& SEC_GROUP
) != 0)
10149 kept
= match_group_member (sec
, kept
, info
);
10151 && ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
10152 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
)))
10154 sec
->kept_section
= kept
;
10159 /* Link an input file into the linker output file. This function
10160 handles all the sections and relocations of the input file at once.
10161 This is so that we only have to read the local symbols once, and
10162 don't have to keep them in memory. */
10165 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
10167 int (*relocate_section
)
10168 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
10169 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
10171 Elf_Internal_Shdr
*symtab_hdr
;
10172 size_t locsymcount
;
10174 Elf_Internal_Sym
*isymbuf
;
10175 Elf_Internal_Sym
*isym
;
10176 Elf_Internal_Sym
*isymend
;
10178 asection
**ppsection
;
10180 const struct elf_backend_data
*bed
;
10181 struct elf_link_hash_entry
**sym_hashes
;
10182 bfd_size_type address_size
;
10183 bfd_vma r_type_mask
;
10185 bfd_boolean have_file_sym
= FALSE
;
10187 output_bfd
= flinfo
->output_bfd
;
10188 bed
= get_elf_backend_data (output_bfd
);
10189 relocate_section
= bed
->elf_backend_relocate_section
;
10191 /* If this is a dynamic object, we don't want to do anything here:
10192 we don't want the local symbols, and we don't want the section
10194 if ((input_bfd
->flags
& DYNAMIC
) != 0)
10197 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
10198 if (elf_bad_symtab (input_bfd
))
10200 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10205 locsymcount
= symtab_hdr
->sh_info
;
10206 extsymoff
= symtab_hdr
->sh_info
;
10209 /* Read the local symbols. */
10210 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10211 if (isymbuf
== NULL
&& locsymcount
!= 0)
10213 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
10214 flinfo
->internal_syms
,
10215 flinfo
->external_syms
,
10216 flinfo
->locsym_shndx
);
10217 if (isymbuf
== NULL
)
10221 /* Find local symbol sections and adjust values of symbols in
10222 SEC_MERGE sections. Write out those local symbols we know are
10223 going into the output file. */
10224 isymend
= isymbuf
+ locsymcount
;
10225 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
10227 isym
++, pindex
++, ppsection
++)
10231 Elf_Internal_Sym osym
;
10237 if (elf_bad_symtab (input_bfd
))
10239 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
10246 if (isym
->st_shndx
== SHN_UNDEF
)
10247 isec
= bfd_und_section_ptr
;
10248 else if (isym
->st_shndx
== SHN_ABS
)
10249 isec
= bfd_abs_section_ptr
;
10250 else if (isym
->st_shndx
== SHN_COMMON
)
10251 isec
= bfd_com_section_ptr
;
10254 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
10257 /* Don't attempt to output symbols with st_shnx in the
10258 reserved range other than SHN_ABS and SHN_COMMON. */
10262 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
10263 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
10265 _bfd_merged_section_offset (output_bfd
, &isec
,
10266 elf_section_data (isec
)->sec_info
,
10272 /* Don't output the first, undefined, symbol. In fact, don't
10273 output any undefined local symbol. */
10274 if (isec
== bfd_und_section_ptr
)
10277 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
10279 /* We never output section symbols. Instead, we use the
10280 section symbol of the corresponding section in the output
10285 /* If we are stripping all symbols, we don't want to output this
10287 if (flinfo
->info
->strip
== strip_all
)
10290 /* If we are discarding all local symbols, we don't want to
10291 output this one. If we are generating a relocatable output
10292 file, then some of the local symbols may be required by
10293 relocs; we output them below as we discover that they are
10295 if (flinfo
->info
->discard
== discard_all
)
10298 /* If this symbol is defined in a section which we are
10299 discarding, we don't need to keep it. */
10300 if (isym
->st_shndx
!= SHN_UNDEF
10301 && isym
->st_shndx
< SHN_LORESERVE
10302 && bfd_section_removed_from_list (output_bfd
,
10303 isec
->output_section
))
10306 /* Get the name of the symbol. */
10307 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
10312 /* See if we are discarding symbols with this name. */
10313 if ((flinfo
->info
->strip
== strip_some
10314 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
10316 || (((flinfo
->info
->discard
== discard_sec_merge
10317 && (isec
->flags
& SEC_MERGE
)
10318 && !bfd_link_relocatable (flinfo
->info
))
10319 || flinfo
->info
->discard
== discard_l
)
10320 && bfd_is_local_label_name (input_bfd
, name
)))
10323 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
10325 if (input_bfd
->lto_output
)
10326 /* -flto puts a temp file name here. This means builds
10327 are not reproducible. Discard the symbol. */
10329 have_file_sym
= TRUE
;
10330 flinfo
->filesym_count
+= 1;
10332 if (!have_file_sym
)
10334 /* In the absence of debug info, bfd_find_nearest_line uses
10335 FILE symbols to determine the source file for local
10336 function symbols. Provide a FILE symbol here if input
10337 files lack such, so that their symbols won't be
10338 associated with a previous input file. It's not the
10339 source file, but the best we can do. */
10340 have_file_sym
= TRUE
;
10341 flinfo
->filesym_count
+= 1;
10342 memset (&osym
, 0, sizeof (osym
));
10343 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10344 osym
.st_shndx
= SHN_ABS
;
10345 if (!elf_link_output_symstrtab (flinfo
,
10346 (input_bfd
->lto_output
? NULL
10347 : input_bfd
->filename
),
10348 &osym
, bfd_abs_section_ptr
,
10355 /* Adjust the section index for the output file. */
10356 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10357 isec
->output_section
);
10358 if (osym
.st_shndx
== SHN_BAD
)
10361 /* ELF symbols in relocatable files are section relative, but
10362 in executable files they are virtual addresses. Note that
10363 this code assumes that all ELF sections have an associated
10364 BFD section with a reasonable value for output_offset; below
10365 we assume that they also have a reasonable value for
10366 output_section. Any special sections must be set up to meet
10367 these requirements. */
10368 osym
.st_value
+= isec
->output_offset
;
10369 if (!bfd_link_relocatable (flinfo
->info
))
10371 osym
.st_value
+= isec
->output_section
->vma
;
10372 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
10374 /* STT_TLS symbols are relative to PT_TLS segment base. */
10375 BFD_ASSERT (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
);
10376 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
10380 indx
= bfd_get_symcount (output_bfd
);
10381 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
10388 if (bed
->s
->arch_size
== 32)
10390 r_type_mask
= 0xff;
10396 r_type_mask
= 0xffffffff;
10401 /* Relocate the contents of each section. */
10402 sym_hashes
= elf_sym_hashes (input_bfd
);
10403 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
10405 bfd_byte
*contents
;
10407 if (! o
->linker_mark
)
10409 /* This section was omitted from the link. */
10413 if (!flinfo
->info
->resolve_section_groups
10414 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
10416 /* Deal with the group signature symbol. */
10417 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
10418 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
10419 asection
*osec
= o
->output_section
;
10421 BFD_ASSERT (bfd_link_relocatable (flinfo
->info
));
10422 if (symndx
>= locsymcount
10423 || (elf_bad_symtab (input_bfd
)
10424 && flinfo
->sections
[symndx
] == NULL
))
10426 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
10427 while (h
->root
.type
== bfd_link_hash_indirect
10428 || h
->root
.type
== bfd_link_hash_warning
)
10429 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10430 /* Arrange for symbol to be output. */
10432 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
10434 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
10436 /* We'll use the output section target_index. */
10437 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10438 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
10442 if (flinfo
->indices
[symndx
] == -1)
10444 /* Otherwise output the local symbol now. */
10445 Elf_Internal_Sym sym
= isymbuf
[symndx
];
10446 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10451 name
= bfd_elf_string_from_elf_section (input_bfd
,
10452 symtab_hdr
->sh_link
,
10457 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10459 if (sym
.st_shndx
== SHN_BAD
)
10462 sym
.st_value
+= o
->output_offset
;
10464 indx
= bfd_get_symcount (output_bfd
);
10465 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
10470 flinfo
->indices
[symndx
] = indx
;
10474 elf_section_data (osec
)->this_hdr
.sh_info
10475 = flinfo
->indices
[symndx
];
10479 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
10480 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
10483 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
10485 /* Section was created by _bfd_elf_link_create_dynamic_sections
10490 /* Get the contents of the section. They have been cached by a
10491 relaxation routine. Note that o is a section in an input
10492 file, so the contents field will not have been set by any of
10493 the routines which work on output files. */
10494 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
10496 contents
= elf_section_data (o
)->this_hdr
.contents
;
10497 if (bed
->caches_rawsize
10499 && o
->rawsize
< o
->size
)
10501 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
10502 contents
= flinfo
->contents
;
10507 contents
= flinfo
->contents
;
10508 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
10512 if ((o
->flags
& SEC_RELOC
) != 0)
10514 Elf_Internal_Rela
*internal_relocs
;
10515 Elf_Internal_Rela
*rel
, *relend
;
10516 int action_discarded
;
10519 /* Get the swapped relocs. */
10521 = _bfd_elf_link_read_relocs (input_bfd
, o
, flinfo
->external_relocs
,
10522 flinfo
->internal_relocs
, FALSE
);
10523 if (internal_relocs
== NULL
10524 && o
->reloc_count
> 0)
10527 /* We need to reverse-copy input .ctors/.dtors sections if
10528 they are placed in .init_array/.finit_array for output. */
10529 if (o
->size
> address_size
10530 && ((strncmp (o
->name
, ".ctors", 6) == 0
10531 && strcmp (o
->output_section
->name
,
10532 ".init_array") == 0)
10533 || (strncmp (o
->name
, ".dtors", 6) == 0
10534 && strcmp (o
->output_section
->name
,
10535 ".fini_array") == 0))
10536 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
10538 if (o
->size
* bed
->s
->int_rels_per_ext_rel
10539 != o
->reloc_count
* address_size
)
10542 /* xgettext:c-format */
10543 (_("error: %pB: size of section %pA is not "
10544 "multiple of address size"),
10546 bfd_set_error (bfd_error_bad_value
);
10549 o
->flags
|= SEC_ELF_REVERSE_COPY
;
10552 action_discarded
= -1;
10553 if (!elf_section_ignore_discarded_relocs (o
))
10554 action_discarded
= (*bed
->action_discarded
) (o
);
10556 /* Run through the relocs evaluating complex reloc symbols and
10557 looking for relocs against symbols from discarded sections
10558 or section symbols from removed link-once sections.
10559 Complain about relocs against discarded sections. Zero
10560 relocs against removed link-once sections. */
10562 rel
= internal_relocs
;
10563 relend
= rel
+ o
->reloc_count
;
10564 for ( ; rel
< relend
; rel
++)
10566 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
10567 unsigned int s_type
;
10568 asection
**ps
, *sec
;
10569 struct elf_link_hash_entry
*h
= NULL
;
10570 const char *sym_name
;
10572 if (r_symndx
== STN_UNDEF
)
10575 if (r_symndx
>= locsymcount
10576 || (elf_bad_symtab (input_bfd
)
10577 && flinfo
->sections
[r_symndx
] == NULL
))
10579 h
= sym_hashes
[r_symndx
- extsymoff
];
10581 /* Badly formatted input files can contain relocs that
10582 reference non-existant symbols. Check here so that
10583 we do not seg fault. */
10587 /* xgettext:c-format */
10588 (_("error: %pB contains a reloc (%#" PRIx64
") for section %pA "
10589 "that references a non-existent global symbol"),
10590 input_bfd
, (uint64_t) rel
->r_info
, o
);
10591 bfd_set_error (bfd_error_bad_value
);
10595 while (h
->root
.type
== bfd_link_hash_indirect
10596 || h
->root
.type
== bfd_link_hash_warning
)
10597 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10601 /* If a plugin symbol is referenced from a non-IR file,
10602 mark the symbol as undefined. Note that the
10603 linker may attach linker created dynamic sections
10604 to the plugin bfd. Symbols defined in linker
10605 created sections are not plugin symbols. */
10606 if ((h
->root
.non_ir_ref_regular
10607 || h
->root
.non_ir_ref_dynamic
)
10608 && (h
->root
.type
== bfd_link_hash_defined
10609 || h
->root
.type
== bfd_link_hash_defweak
)
10610 && (h
->root
.u
.def
.section
->flags
10611 & SEC_LINKER_CREATED
) == 0
10612 && h
->root
.u
.def
.section
->owner
!= NULL
10613 && (h
->root
.u
.def
.section
->owner
->flags
10614 & BFD_PLUGIN
) != 0)
10616 h
->root
.type
= bfd_link_hash_undefined
;
10617 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
10621 if (h
->root
.type
== bfd_link_hash_defined
10622 || h
->root
.type
== bfd_link_hash_defweak
)
10623 ps
= &h
->root
.u
.def
.section
;
10625 sym_name
= h
->root
.root
.string
;
10629 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
10631 s_type
= ELF_ST_TYPE (sym
->st_info
);
10632 ps
= &flinfo
->sections
[r_symndx
];
10633 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
10637 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
10638 && !bfd_link_relocatable (flinfo
->info
))
10641 bfd_vma dot
= (rel
->r_offset
10642 + o
->output_offset
+ o
->output_section
->vma
);
10644 printf ("Encountered a complex symbol!");
10645 printf (" (input_bfd %s, section %s, reloc %ld\n",
10646 input_bfd
->filename
, o
->name
,
10647 (long) (rel
- internal_relocs
));
10648 printf (" symbol: idx %8.8lx, name %s\n",
10649 r_symndx
, sym_name
);
10650 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10651 (unsigned long) rel
->r_info
,
10652 (unsigned long) rel
->r_offset
);
10654 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
10655 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
10658 /* Symbol evaluated OK. Update to absolute value. */
10659 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
10664 if (action_discarded
!= -1 && ps
!= NULL
)
10666 /* Complain if the definition comes from a
10667 discarded section. */
10668 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
10670 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
10671 if (action_discarded
& COMPLAIN
)
10672 (*flinfo
->info
->callbacks
->einfo
)
10673 /* xgettext:c-format */
10674 (_("%X`%s' referenced in section `%pA' of %pB: "
10675 "defined in discarded section `%pA' of %pB\n"),
10676 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
10678 /* Try to do the best we can to support buggy old
10679 versions of gcc. Pretend that the symbol is
10680 really defined in the kept linkonce section.
10681 FIXME: This is quite broken. Modifying the
10682 symbol here means we will be changing all later
10683 uses of the symbol, not just in this section. */
10684 if (action_discarded
& PRETEND
)
10688 kept
= _bfd_elf_check_kept_section (sec
,
10700 /* Relocate the section by invoking a back end routine.
10702 The back end routine is responsible for adjusting the
10703 section contents as necessary, and (if using Rela relocs
10704 and generating a relocatable output file) adjusting the
10705 reloc addend as necessary.
10707 The back end routine does not have to worry about setting
10708 the reloc address or the reloc symbol index.
10710 The back end routine is given a pointer to the swapped in
10711 internal symbols, and can access the hash table entries
10712 for the external symbols via elf_sym_hashes (input_bfd).
10714 When generating relocatable output, the back end routine
10715 must handle STB_LOCAL/STT_SECTION symbols specially. The
10716 output symbol is going to be a section symbol
10717 corresponding to the output section, which will require
10718 the addend to be adjusted. */
10720 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
10721 input_bfd
, o
, contents
,
10729 || bfd_link_relocatable (flinfo
->info
)
10730 || flinfo
->info
->emitrelocations
)
10732 Elf_Internal_Rela
*irela
;
10733 Elf_Internal_Rela
*irelaend
, *irelamid
;
10734 bfd_vma last_offset
;
10735 struct elf_link_hash_entry
**rel_hash
;
10736 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
10737 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
10738 unsigned int next_erel
;
10739 bfd_boolean rela_normal
;
10740 struct bfd_elf_section_data
*esdi
, *esdo
;
10742 esdi
= elf_section_data (o
);
10743 esdo
= elf_section_data (o
->output_section
);
10744 rela_normal
= FALSE
;
10746 /* Adjust the reloc addresses and symbol indices. */
10748 irela
= internal_relocs
;
10749 irelaend
= irela
+ o
->reloc_count
;
10750 rel_hash
= esdo
->rel
.hashes
+ esdo
->rel
.count
;
10751 /* We start processing the REL relocs, if any. When we reach
10752 IRELAMID in the loop, we switch to the RELA relocs. */
10754 if (esdi
->rel
.hdr
!= NULL
)
10755 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
10756 * bed
->s
->int_rels_per_ext_rel
);
10757 rel_hash_list
= rel_hash
;
10758 rela_hash_list
= NULL
;
10759 last_offset
= o
->output_offset
;
10760 if (!bfd_link_relocatable (flinfo
->info
))
10761 last_offset
+= o
->output_section
->vma
;
10762 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
10764 unsigned long r_symndx
;
10766 Elf_Internal_Sym sym
;
10768 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
10774 if (irela
== irelamid
)
10776 rel_hash
= esdo
->rela
.hashes
+ esdo
->rela
.count
;
10777 rela_hash_list
= rel_hash
;
10778 rela_normal
= bed
->rela_normal
;
10781 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
10784 if (irela
->r_offset
>= (bfd_vma
) -2)
10786 /* This is a reloc for a deleted entry or somesuch.
10787 Turn it into an R_*_NONE reloc, at the same
10788 offset as the last reloc. elf_eh_frame.c and
10789 bfd_elf_discard_info rely on reloc offsets
10791 irela
->r_offset
= last_offset
;
10793 irela
->r_addend
= 0;
10797 irela
->r_offset
+= o
->output_offset
;
10799 /* Relocs in an executable have to be virtual addresses. */
10800 if (!bfd_link_relocatable (flinfo
->info
))
10801 irela
->r_offset
+= o
->output_section
->vma
;
10803 last_offset
= irela
->r_offset
;
10805 r_symndx
= irela
->r_info
>> r_sym_shift
;
10806 if (r_symndx
== STN_UNDEF
)
10809 if (r_symndx
>= locsymcount
10810 || (elf_bad_symtab (input_bfd
)
10811 && flinfo
->sections
[r_symndx
] == NULL
))
10813 struct elf_link_hash_entry
*rh
;
10814 unsigned long indx
;
10816 /* This is a reloc against a global symbol. We
10817 have not yet output all the local symbols, so
10818 we do not know the symbol index of any global
10819 symbol. We set the rel_hash entry for this
10820 reloc to point to the global hash table entry
10821 for this symbol. The symbol index is then
10822 set at the end of bfd_elf_final_link. */
10823 indx
= r_symndx
- extsymoff
;
10824 rh
= elf_sym_hashes (input_bfd
)[indx
];
10825 while (rh
->root
.type
== bfd_link_hash_indirect
10826 || rh
->root
.type
== bfd_link_hash_warning
)
10827 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
10829 /* Setting the index to -2 tells
10830 elf_link_output_extsym that this symbol is
10831 used by a reloc. */
10832 BFD_ASSERT (rh
->indx
< 0);
10839 /* This is a reloc against a local symbol. */
10842 sym
= isymbuf
[r_symndx
];
10843 sec
= flinfo
->sections
[r_symndx
];
10844 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
10846 /* I suppose the backend ought to fill in the
10847 section of any STT_SECTION symbol against a
10848 processor specific section. */
10849 r_symndx
= STN_UNDEF
;
10850 if (bfd_is_abs_section (sec
))
10852 else if (sec
== NULL
|| sec
->owner
== NULL
)
10854 bfd_set_error (bfd_error_bad_value
);
10859 asection
*osec
= sec
->output_section
;
10861 /* If we have discarded a section, the output
10862 section will be the absolute section. In
10863 case of discarded SEC_MERGE sections, use
10864 the kept section. relocate_section should
10865 have already handled discarded linkonce
10867 if (bfd_is_abs_section (osec
)
10868 && sec
->kept_section
!= NULL
10869 && sec
->kept_section
->output_section
!= NULL
)
10871 osec
= sec
->kept_section
->output_section
;
10872 irela
->r_addend
-= osec
->vma
;
10875 if (!bfd_is_abs_section (osec
))
10877 r_symndx
= osec
->target_index
;
10878 if (r_symndx
== STN_UNDEF
)
10880 irela
->r_addend
+= osec
->vma
;
10881 osec
= _bfd_nearby_section (output_bfd
, osec
,
10883 irela
->r_addend
-= osec
->vma
;
10884 r_symndx
= osec
->target_index
;
10889 /* Adjust the addend according to where the
10890 section winds up in the output section. */
10892 irela
->r_addend
+= sec
->output_offset
;
10896 if (flinfo
->indices
[r_symndx
] == -1)
10898 unsigned long shlink
;
10903 if (flinfo
->info
->strip
== strip_all
)
10905 /* You can't do ld -r -s. */
10906 bfd_set_error (bfd_error_invalid_operation
);
10910 /* This symbol was skipped earlier, but
10911 since it is needed by a reloc, we
10912 must output it now. */
10913 shlink
= symtab_hdr
->sh_link
;
10914 name
= (bfd_elf_string_from_elf_section
10915 (input_bfd
, shlink
, sym
.st_name
));
10919 osec
= sec
->output_section
;
10921 _bfd_elf_section_from_bfd_section (output_bfd
,
10923 if (sym
.st_shndx
== SHN_BAD
)
10926 sym
.st_value
+= sec
->output_offset
;
10927 if (!bfd_link_relocatable (flinfo
->info
))
10929 sym
.st_value
+= osec
->vma
;
10930 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
10932 /* STT_TLS symbols are relative to PT_TLS
10934 BFD_ASSERT (elf_hash_table (flinfo
->info
)
10935 ->tls_sec
!= NULL
);
10936 sym
.st_value
-= (elf_hash_table (flinfo
->info
)
10941 indx
= bfd_get_symcount (output_bfd
);
10942 ret
= elf_link_output_symstrtab (flinfo
, name
,
10948 flinfo
->indices
[r_symndx
] = indx
;
10953 r_symndx
= flinfo
->indices
[r_symndx
];
10956 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
10957 | (irela
->r_info
& r_type_mask
));
10960 /* Swap out the relocs. */
10961 input_rel_hdr
= esdi
->rel
.hdr
;
10962 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
10964 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10969 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
10970 * bed
->s
->int_rels_per_ext_rel
);
10971 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
10974 input_rela_hdr
= esdi
->rela
.hdr
;
10975 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
10977 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10986 /* Write out the modified section contents. */
10987 if (bed
->elf_backend_write_section
10988 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
10991 /* Section written out. */
10993 else switch (o
->sec_info_type
)
10995 case SEC_INFO_TYPE_STABS
:
10996 if (! (_bfd_write_section_stabs
10998 &elf_hash_table (flinfo
->info
)->stab_info
,
10999 o
, &elf_section_data (o
)->sec_info
, contents
)))
11002 case SEC_INFO_TYPE_MERGE
:
11003 if (! _bfd_write_merged_section (output_bfd
, o
,
11004 elf_section_data (o
)->sec_info
))
11007 case SEC_INFO_TYPE_EH_FRAME
:
11009 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
11014 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
11016 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
11024 if (! (o
->flags
& SEC_EXCLUDE
))
11026 file_ptr offset
= (file_ptr
) o
->output_offset
;
11027 bfd_size_type todo
= o
->size
;
11029 offset
*= bfd_octets_per_byte (output_bfd
);
11031 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
11033 /* Reverse-copy input section to output. */
11036 todo
-= address_size
;
11037 if (! bfd_set_section_contents (output_bfd
,
11045 offset
+= address_size
;
11049 else if (! bfd_set_section_contents (output_bfd
,
11063 /* Generate a reloc when linking an ELF file. This is a reloc
11064 requested by the linker, and does not come from any input file. This
11065 is used to build constructor and destructor tables when linking
11069 elf_reloc_link_order (bfd
*output_bfd
,
11070 struct bfd_link_info
*info
,
11071 asection
*output_section
,
11072 struct bfd_link_order
*link_order
)
11074 reloc_howto_type
*howto
;
11078 struct bfd_elf_section_reloc_data
*reldata
;
11079 struct elf_link_hash_entry
**rel_hash_ptr
;
11080 Elf_Internal_Shdr
*rel_hdr
;
11081 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
11082 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
11085 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
11087 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
11090 bfd_set_error (bfd_error_bad_value
);
11094 addend
= link_order
->u
.reloc
.p
->addend
;
11097 reldata
= &esdo
->rel
;
11098 else if (esdo
->rela
.hdr
)
11099 reldata
= &esdo
->rela
;
11106 /* Figure out the symbol index. */
11107 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
11108 if (link_order
->type
== bfd_section_reloc_link_order
)
11110 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
11111 BFD_ASSERT (indx
!= 0);
11112 *rel_hash_ptr
= NULL
;
11116 struct elf_link_hash_entry
*h
;
11118 /* Treat a reloc against a defined symbol as though it were
11119 actually against the section. */
11120 h
= ((struct elf_link_hash_entry
*)
11121 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
11122 link_order
->u
.reloc
.p
->u
.name
,
11123 FALSE
, FALSE
, TRUE
));
11125 && (h
->root
.type
== bfd_link_hash_defined
11126 || h
->root
.type
== bfd_link_hash_defweak
))
11130 section
= h
->root
.u
.def
.section
;
11131 indx
= section
->output_section
->target_index
;
11132 *rel_hash_ptr
= NULL
;
11133 /* It seems that we ought to add the symbol value to the
11134 addend here, but in practice it has already been added
11135 because it was passed to constructor_callback. */
11136 addend
+= section
->output_section
->vma
+ section
->output_offset
;
11138 else if (h
!= NULL
)
11140 /* Setting the index to -2 tells elf_link_output_extsym that
11141 this symbol is used by a reloc. */
11148 (*info
->callbacks
->unattached_reloc
)
11149 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
11154 /* If this is an inplace reloc, we must write the addend into the
11156 if (howto
->partial_inplace
&& addend
!= 0)
11158 bfd_size_type size
;
11159 bfd_reloc_status_type rstat
;
11162 const char *sym_name
;
11164 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
11165 buf
= (bfd_byte
*) bfd_zmalloc (size
);
11166 if (buf
== NULL
&& size
!= 0)
11168 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
11175 case bfd_reloc_outofrange
:
11178 case bfd_reloc_overflow
:
11179 if (link_order
->type
== bfd_section_reloc_link_order
)
11180 sym_name
= bfd_section_name (output_bfd
,
11181 link_order
->u
.reloc
.p
->u
.section
);
11183 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
11184 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
11185 howto
->name
, addend
, NULL
, NULL
,
11190 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
11192 * bfd_octets_per_byte (output_bfd
),
11199 /* The address of a reloc is relative to the section in a
11200 relocatable file, and is a virtual address in an executable
11202 offset
= link_order
->offset
;
11203 if (! bfd_link_relocatable (info
))
11204 offset
+= output_section
->vma
;
11206 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
11208 irel
[i
].r_offset
= offset
;
11209 irel
[i
].r_info
= 0;
11210 irel
[i
].r_addend
= 0;
11212 if (bed
->s
->arch_size
== 32)
11213 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
11215 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
11217 rel_hdr
= reldata
->hdr
;
11218 erel
= rel_hdr
->contents
;
11219 if (rel_hdr
->sh_type
== SHT_REL
)
11221 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
11222 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
11226 irel
[0].r_addend
= addend
;
11227 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
11228 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
11237 /* Get the output vma of the section pointed to by the sh_link field. */
11240 elf_get_linked_section_vma (struct bfd_link_order
*p
)
11242 Elf_Internal_Shdr
**elf_shdrp
;
11246 s
= p
->u
.indirect
.section
;
11247 elf_shdrp
= elf_elfsections (s
->owner
);
11248 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
11249 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
11251 The Intel C compiler generates SHT_IA_64_UNWIND with
11252 SHF_LINK_ORDER. But it doesn't set the sh_link or
11253 sh_info fields. Hence we could get the situation
11254 where elfsec is 0. */
11257 const struct elf_backend_data
*bed
11258 = get_elf_backend_data (s
->owner
);
11259 if (bed
->link_order_error_handler
)
11260 bed
->link_order_error_handler
11261 /* xgettext:c-format */
11262 (_("%pB: warning: sh_link not set for section `%pA'"), s
->owner
, s
);
11267 s
= elf_shdrp
[elfsec
]->bfd_section
;
11268 return s
->output_section
->vma
+ s
->output_offset
;
11273 /* Compare two sections based on the locations of the sections they are
11274 linked to. Used by elf_fixup_link_order. */
11277 compare_link_order (const void * a
, const void * b
)
11282 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
11283 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
11286 return apos
> bpos
;
11290 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11291 order as their linked sections. Returns false if this could not be done
11292 because an output section includes both ordered and unordered
11293 sections. Ideally we'd do this in the linker proper. */
11296 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
11298 int seen_linkorder
;
11301 struct bfd_link_order
*p
;
11303 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11305 struct bfd_link_order
**sections
;
11306 asection
*s
, *other_sec
, *linkorder_sec
;
11310 linkorder_sec
= NULL
;
11312 seen_linkorder
= 0;
11313 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11315 if (p
->type
== bfd_indirect_link_order
)
11317 s
= p
->u
.indirect
.section
;
11319 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
11320 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
11321 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
11322 && elfsec
< elf_numsections (sub
)
11323 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
11324 && elf_elfsections (sub
)[elfsec
]->sh_link
< elf_numsections (sub
))
11338 if (seen_other
&& seen_linkorder
)
11340 if (other_sec
&& linkorder_sec
)
11342 /* xgettext:c-format */
11343 (_("%pA has both ordered [`%pA' in %pB] "
11344 "and unordered [`%pA' in %pB] sections"),
11345 o
, linkorder_sec
, linkorder_sec
->owner
,
11346 other_sec
, other_sec
->owner
);
11349 (_("%pA has both ordered and unordered sections"), o
);
11350 bfd_set_error (bfd_error_bad_value
);
11355 if (!seen_linkorder
)
11358 sections
= (struct bfd_link_order
**)
11359 bfd_malloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
11360 if (sections
== NULL
)
11362 seen_linkorder
= 0;
11364 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11366 sections
[seen_linkorder
++] = p
;
11368 /* Sort the input sections in the order of their linked section. */
11369 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
11370 compare_link_order
);
11372 /* Change the offsets of the sections. */
11374 for (n
= 0; n
< seen_linkorder
; n
++)
11376 s
= sections
[n
]->u
.indirect
.section
;
11377 offset
&= ~(bfd_vma
) 0 << s
->alignment_power
;
11378 s
->output_offset
= offset
/ bfd_octets_per_byte (abfd
);
11379 sections
[n
]->offset
= offset
;
11380 offset
+= sections
[n
]->size
;
11387 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11388 Returns TRUE upon success, FALSE otherwise. */
11391 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
11393 bfd_boolean ret
= FALSE
;
11395 const struct elf_backend_data
*bed
;
11397 enum bfd_architecture arch
;
11399 asymbol
**sympp
= NULL
;
11403 elf_symbol_type
*osymbuf
;
11405 implib_bfd
= info
->out_implib_bfd
;
11406 bed
= get_elf_backend_data (abfd
);
11408 if (!bfd_set_format (implib_bfd
, bfd_object
))
11411 /* Use flag from executable but make it a relocatable object. */
11412 flags
= bfd_get_file_flags (abfd
);
11413 flags
&= ~HAS_RELOC
;
11414 if (!bfd_set_start_address (implib_bfd
, 0)
11415 || !bfd_set_file_flags (implib_bfd
, flags
& ~EXEC_P
))
11418 /* Copy architecture of output file to import library file. */
11419 arch
= bfd_get_arch (abfd
);
11420 mach
= bfd_get_mach (abfd
);
11421 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
11422 && (abfd
->target_defaulted
11423 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
11426 /* Get symbol table size. */
11427 symsize
= bfd_get_symtab_upper_bound (abfd
);
11431 /* Read in the symbol table. */
11432 sympp
= (asymbol
**) xmalloc (symsize
);
11433 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
11437 /* Allow the BFD backend to copy any private header data it
11438 understands from the output BFD to the import library BFD. */
11439 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
11442 /* Filter symbols to appear in the import library. */
11443 if (bed
->elf_backend_filter_implib_symbols
)
11444 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
11447 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
11450 bfd_set_error (bfd_error_no_symbols
);
11451 _bfd_error_handler (_("%pB: no symbol found for import library"),
11457 /* Make symbols absolute. */
11458 osymbuf
= (elf_symbol_type
*) bfd_alloc2 (implib_bfd
, symcount
,
11459 sizeof (*osymbuf
));
11460 for (src_count
= 0; src_count
< symcount
; src_count
++)
11462 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
11463 sizeof (*osymbuf
));
11464 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
11465 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
11466 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
11467 osymbuf
[src_count
].internal_elf_sym
.st_value
=
11468 osymbuf
[src_count
].symbol
.value
;
11469 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
11472 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
11474 /* Allow the BFD backend to copy any private data it understands
11475 from the output BFD to the import library BFD. This is done last
11476 to permit the routine to look at the filtered symbol table. */
11477 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
11480 if (!bfd_close (implib_bfd
))
11491 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
11495 if (flinfo
->symstrtab
!= NULL
)
11496 _bfd_elf_strtab_free (flinfo
->symstrtab
);
11497 if (flinfo
->contents
!= NULL
)
11498 free (flinfo
->contents
);
11499 if (flinfo
->external_relocs
!= NULL
)
11500 free (flinfo
->external_relocs
);
11501 if (flinfo
->internal_relocs
!= NULL
)
11502 free (flinfo
->internal_relocs
);
11503 if (flinfo
->external_syms
!= NULL
)
11504 free (flinfo
->external_syms
);
11505 if (flinfo
->locsym_shndx
!= NULL
)
11506 free (flinfo
->locsym_shndx
);
11507 if (flinfo
->internal_syms
!= NULL
)
11508 free (flinfo
->internal_syms
);
11509 if (flinfo
->indices
!= NULL
)
11510 free (flinfo
->indices
);
11511 if (flinfo
->sections
!= NULL
)
11512 free (flinfo
->sections
);
11513 if (flinfo
->symshndxbuf
!= NULL
)
11514 free (flinfo
->symshndxbuf
);
11515 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
11517 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11518 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rel
.hashes
!= NULL
)
11519 free (esdo
->rel
.hashes
);
11520 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rela
.hashes
!= NULL
)
11521 free (esdo
->rela
.hashes
);
11525 /* Do the final step of an ELF link. */
11528 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
11530 bfd_boolean dynamic
;
11531 bfd_boolean emit_relocs
;
11533 struct elf_final_link_info flinfo
;
11535 struct bfd_link_order
*p
;
11537 bfd_size_type max_contents_size
;
11538 bfd_size_type max_external_reloc_size
;
11539 bfd_size_type max_internal_reloc_count
;
11540 bfd_size_type max_sym_count
;
11541 bfd_size_type max_sym_shndx_count
;
11542 Elf_Internal_Sym elfsym
;
11544 Elf_Internal_Shdr
*symtab_hdr
;
11545 Elf_Internal_Shdr
*symtab_shndx_hdr
;
11546 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11547 struct elf_outext_info eoinfo
;
11548 bfd_boolean merged
;
11549 size_t relativecount
= 0;
11550 asection
*reldyn
= 0;
11552 asection
*attr_section
= NULL
;
11553 bfd_vma attr_size
= 0;
11554 const char *std_attrs_section
;
11555 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
11557 if (!is_elf_hash_table (htab
))
11560 if (bfd_link_pic (info
))
11561 abfd
->flags
|= DYNAMIC
;
11563 dynamic
= htab
->dynamic_sections_created
;
11564 dynobj
= htab
->dynobj
;
11566 emit_relocs
= (bfd_link_relocatable (info
)
11567 || info
->emitrelocations
);
11569 flinfo
.info
= info
;
11570 flinfo
.output_bfd
= abfd
;
11571 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
11572 if (flinfo
.symstrtab
== NULL
)
11577 flinfo
.hash_sec
= NULL
;
11578 flinfo
.symver_sec
= NULL
;
11582 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
11583 /* Note that dynsym_sec can be NULL (on VMS). */
11584 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
11585 /* Note that it is OK if symver_sec is NULL. */
11588 flinfo
.contents
= NULL
;
11589 flinfo
.external_relocs
= NULL
;
11590 flinfo
.internal_relocs
= NULL
;
11591 flinfo
.external_syms
= NULL
;
11592 flinfo
.locsym_shndx
= NULL
;
11593 flinfo
.internal_syms
= NULL
;
11594 flinfo
.indices
= NULL
;
11595 flinfo
.sections
= NULL
;
11596 flinfo
.symshndxbuf
= NULL
;
11597 flinfo
.filesym_count
= 0;
11599 /* The object attributes have been merged. Remove the input
11600 sections from the link, and set the contents of the output
11602 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
11603 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11605 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
11606 || strcmp (o
->name
, ".gnu.attributes") == 0)
11608 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11610 asection
*input_section
;
11612 if (p
->type
!= bfd_indirect_link_order
)
11614 input_section
= p
->u
.indirect
.section
;
11615 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11616 elf_link_input_bfd ignores this section. */
11617 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
11620 attr_size
= bfd_elf_obj_attr_size (abfd
);
11623 bfd_set_section_size (abfd
, o
, attr_size
);
11625 /* Skip this section later on. */
11626 o
->map_head
.link_order
= NULL
;
11629 o
->flags
|= SEC_EXCLUDE
;
11631 else if ((o
->flags
& SEC_GROUP
) != 0 && o
->size
== 0)
11633 /* Remove empty group section from linker output. */
11634 o
->flags
|= SEC_EXCLUDE
;
11635 bfd_section_list_remove (abfd
, o
);
11636 abfd
->section_count
--;
11640 /* Count up the number of relocations we will output for each output
11641 section, so that we know the sizes of the reloc sections. We
11642 also figure out some maximum sizes. */
11643 max_contents_size
= 0;
11644 max_external_reloc_size
= 0;
11645 max_internal_reloc_count
= 0;
11647 max_sym_shndx_count
= 0;
11649 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11651 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11652 o
->reloc_count
= 0;
11654 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11656 unsigned int reloc_count
= 0;
11657 unsigned int additional_reloc_count
= 0;
11658 struct bfd_elf_section_data
*esdi
= NULL
;
11660 if (p
->type
== bfd_section_reloc_link_order
11661 || p
->type
== bfd_symbol_reloc_link_order
)
11663 else if (p
->type
== bfd_indirect_link_order
)
11667 sec
= p
->u
.indirect
.section
;
11669 /* Mark all sections which are to be included in the
11670 link. This will normally be every section. We need
11671 to do this so that we can identify any sections which
11672 the linker has decided to not include. */
11673 sec
->linker_mark
= TRUE
;
11675 if (sec
->flags
& SEC_MERGE
)
11678 if (sec
->rawsize
> max_contents_size
)
11679 max_contents_size
= sec
->rawsize
;
11680 if (sec
->size
> max_contents_size
)
11681 max_contents_size
= sec
->size
;
11683 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
11684 && (sec
->owner
->flags
& DYNAMIC
) == 0)
11688 /* We are interested in just local symbols, not all
11690 if (elf_bad_symtab (sec
->owner
))
11691 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
11692 / bed
->s
->sizeof_sym
);
11694 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
11696 if (sym_count
> max_sym_count
)
11697 max_sym_count
= sym_count
;
11699 if (sym_count
> max_sym_shndx_count
11700 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
11701 max_sym_shndx_count
= sym_count
;
11703 if (esdo
->this_hdr
.sh_type
== SHT_REL
11704 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
11705 /* Some backends use reloc_count in relocation sections
11706 to count particular types of relocs. Of course,
11707 reloc sections themselves can't have relocations. */
11709 else if (emit_relocs
)
11711 reloc_count
= sec
->reloc_count
;
11712 if (bed
->elf_backend_count_additional_relocs
)
11715 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
11716 additional_reloc_count
+= c
;
11719 else if (bed
->elf_backend_count_relocs
)
11720 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
11722 esdi
= elf_section_data (sec
);
11724 if ((sec
->flags
& SEC_RELOC
) != 0)
11726 size_t ext_size
= 0;
11728 if (esdi
->rel
.hdr
!= NULL
)
11729 ext_size
= esdi
->rel
.hdr
->sh_size
;
11730 if (esdi
->rela
.hdr
!= NULL
)
11731 ext_size
+= esdi
->rela
.hdr
->sh_size
;
11733 if (ext_size
> max_external_reloc_size
)
11734 max_external_reloc_size
= ext_size
;
11735 if (sec
->reloc_count
> max_internal_reloc_count
)
11736 max_internal_reloc_count
= sec
->reloc_count
;
11741 if (reloc_count
== 0)
11744 reloc_count
+= additional_reloc_count
;
11745 o
->reloc_count
+= reloc_count
;
11747 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
11751 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
11752 esdo
->rel
.count
+= additional_reloc_count
;
11754 if (esdi
->rela
.hdr
)
11756 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
11757 esdo
->rela
.count
+= additional_reloc_count
;
11763 esdo
->rela
.count
+= reloc_count
;
11765 esdo
->rel
.count
+= reloc_count
;
11769 if (o
->reloc_count
> 0)
11770 o
->flags
|= SEC_RELOC
;
11773 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11774 set it (this is probably a bug) and if it is set
11775 assign_section_numbers will create a reloc section. */
11776 o
->flags
&=~ SEC_RELOC
;
11779 /* If the SEC_ALLOC flag is not set, force the section VMA to
11780 zero. This is done in elf_fake_sections as well, but forcing
11781 the VMA to 0 here will ensure that relocs against these
11782 sections are handled correctly. */
11783 if ((o
->flags
& SEC_ALLOC
) == 0
11784 && ! o
->user_set_vma
)
11788 if (! bfd_link_relocatable (info
) && merged
)
11789 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
11791 /* Figure out the file positions for everything but the symbol table
11792 and the relocs. We set symcount to force assign_section_numbers
11793 to create a symbol table. */
11794 bfd_get_symcount (abfd
) = info
->strip
!= strip_all
|| emit_relocs
;
11795 BFD_ASSERT (! abfd
->output_has_begun
);
11796 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
11799 /* Set sizes, and assign file positions for reloc sections. */
11800 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11802 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11803 if ((o
->flags
& SEC_RELOC
) != 0)
11806 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
11810 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
11814 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11815 to count upwards while actually outputting the relocations. */
11816 esdo
->rel
.count
= 0;
11817 esdo
->rela
.count
= 0;
11819 if (esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
11821 /* Cache the section contents so that they can be compressed
11822 later. Use bfd_malloc since it will be freed by
11823 bfd_compress_section_contents. */
11824 unsigned char *contents
= esdo
->this_hdr
.contents
;
11825 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
11828 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
11829 if (contents
== NULL
)
11831 esdo
->this_hdr
.contents
= contents
;
11835 /* We have now assigned file positions for all the sections except
11836 .symtab, .strtab, and non-loaded reloc sections. We start the
11837 .symtab section at the current file position, and write directly
11838 to it. We build the .strtab section in memory. */
11839 bfd_get_symcount (abfd
) = 0;
11840 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11841 /* sh_name is set in prep_headers. */
11842 symtab_hdr
->sh_type
= SHT_SYMTAB
;
11843 /* sh_flags, sh_addr and sh_size all start off zero. */
11844 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
11845 /* sh_link is set in assign_section_numbers. */
11846 /* sh_info is set below. */
11847 /* sh_offset is set just below. */
11848 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
11850 if (max_sym_count
< 20)
11851 max_sym_count
= 20;
11852 htab
->strtabsize
= max_sym_count
;
11853 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
11854 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
11855 if (htab
->strtab
== NULL
)
11857 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11859 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
11860 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
11862 if (info
->strip
!= strip_all
|| emit_relocs
)
11864 file_ptr off
= elf_next_file_pos (abfd
);
11866 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
11868 /* Note that at this point elf_next_file_pos (abfd) is
11869 incorrect. We do not yet know the size of the .symtab section.
11870 We correct next_file_pos below, after we do know the size. */
11872 /* Start writing out the symbol table. The first symbol is always a
11874 elfsym
.st_value
= 0;
11875 elfsym
.st_size
= 0;
11876 elfsym
.st_info
= 0;
11877 elfsym
.st_other
= 0;
11878 elfsym
.st_shndx
= SHN_UNDEF
;
11879 elfsym
.st_target_internal
= 0;
11880 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
11881 bfd_und_section_ptr
, NULL
) != 1)
11884 /* Output a symbol for each section. We output these even if we are
11885 discarding local symbols, since they are used for relocs. These
11886 symbols have no names. We store the index of each one in the
11887 index field of the section, so that we can find it again when
11888 outputting relocs. */
11890 elfsym
.st_size
= 0;
11891 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
11892 elfsym
.st_other
= 0;
11893 elfsym
.st_value
= 0;
11894 elfsym
.st_target_internal
= 0;
11895 for (i
= 1; i
< elf_numsections (abfd
); i
++)
11897 o
= bfd_section_from_elf_index (abfd
, i
);
11900 o
->target_index
= bfd_get_symcount (abfd
);
11901 elfsym
.st_shndx
= i
;
11902 if (!bfd_link_relocatable (info
))
11903 elfsym
.st_value
= o
->vma
;
11904 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
, o
,
11911 /* Allocate some memory to hold information read in from the input
11913 if (max_contents_size
!= 0)
11915 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
11916 if (flinfo
.contents
== NULL
)
11920 if (max_external_reloc_size
!= 0)
11922 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
11923 if (flinfo
.external_relocs
== NULL
)
11927 if (max_internal_reloc_count
!= 0)
11929 amt
= max_internal_reloc_count
* sizeof (Elf_Internal_Rela
);
11930 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
11931 if (flinfo
.internal_relocs
== NULL
)
11935 if (max_sym_count
!= 0)
11937 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
11938 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
11939 if (flinfo
.external_syms
== NULL
)
11942 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
11943 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
11944 if (flinfo
.internal_syms
== NULL
)
11947 amt
= max_sym_count
* sizeof (long);
11948 flinfo
.indices
= (long int *) bfd_malloc (amt
);
11949 if (flinfo
.indices
== NULL
)
11952 amt
= max_sym_count
* sizeof (asection
*);
11953 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
11954 if (flinfo
.sections
== NULL
)
11958 if (max_sym_shndx_count
!= 0)
11960 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
11961 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
11962 if (flinfo
.locsym_shndx
== NULL
)
11968 bfd_vma base
, end
= 0;
11971 for (sec
= htab
->tls_sec
;
11972 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
11975 bfd_size_type size
= sec
->size
;
11978 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
11980 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
11983 size
= ord
->offset
+ ord
->size
;
11985 end
= sec
->vma
+ size
;
11987 base
= htab
->tls_sec
->vma
;
11988 /* Only align end of TLS section if static TLS doesn't have special
11989 alignment requirements. */
11990 if (bed
->static_tls_alignment
== 1)
11991 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
11992 htab
->tls_size
= end
- base
;
11995 /* Reorder SHF_LINK_ORDER sections. */
11996 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11998 if (!elf_fixup_link_order (abfd
, o
))
12002 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
12005 /* Since ELF permits relocations to be against local symbols, we
12006 must have the local symbols available when we do the relocations.
12007 Since we would rather only read the local symbols once, and we
12008 would rather not keep them in memory, we handle all the
12009 relocations for a single input file at the same time.
12011 Unfortunately, there is no way to know the total number of local
12012 symbols until we have seen all of them, and the local symbol
12013 indices precede the global symbol indices. This means that when
12014 we are generating relocatable output, and we see a reloc against
12015 a global symbol, we can not know the symbol index until we have
12016 finished examining all the local symbols to see which ones we are
12017 going to output. To deal with this, we keep the relocations in
12018 memory, and don't output them until the end of the link. This is
12019 an unfortunate waste of memory, but I don't see a good way around
12020 it. Fortunately, it only happens when performing a relocatable
12021 link, which is not the common case. FIXME: If keep_memory is set
12022 we could write the relocs out and then read them again; I don't
12023 know how bad the memory loss will be. */
12025 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12026 sub
->output_has_begun
= FALSE
;
12027 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12029 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12031 if (p
->type
== bfd_indirect_link_order
12032 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
12033 == bfd_target_elf_flavour
)
12034 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
12036 if (! sub
->output_has_begun
)
12038 if (! elf_link_input_bfd (&flinfo
, sub
))
12040 sub
->output_has_begun
= TRUE
;
12043 else if (p
->type
== bfd_section_reloc_link_order
12044 || p
->type
== bfd_symbol_reloc_link_order
)
12046 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
12051 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
12053 if (p
->type
== bfd_indirect_link_order
12054 && (bfd_get_flavour (sub
)
12055 == bfd_target_elf_flavour
)
12056 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
12057 != bed
->s
->elfclass
))
12059 const char *iclass
, *oclass
;
12061 switch (bed
->s
->elfclass
)
12063 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
12064 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
12065 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
12069 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
12071 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
12072 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
12073 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
12077 bfd_set_error (bfd_error_wrong_format
);
12079 /* xgettext:c-format */
12080 (_("%pB: file class %s incompatible with %s"),
12081 sub
, iclass
, oclass
);
12090 /* Free symbol buffer if needed. */
12091 if (!info
->reduce_memory_overheads
)
12093 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12094 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
12095 && elf_tdata (sub
)->symbuf
)
12097 free (elf_tdata (sub
)->symbuf
);
12098 elf_tdata (sub
)->symbuf
= NULL
;
12102 /* Output any global symbols that got converted to local in a
12103 version script or due to symbol visibility. We do this in a
12104 separate step since ELF requires all local symbols to appear
12105 prior to any global symbols. FIXME: We should only do this if
12106 some global symbols were, in fact, converted to become local.
12107 FIXME: Will this work correctly with the Irix 5 linker? */
12108 eoinfo
.failed
= FALSE
;
12109 eoinfo
.flinfo
= &flinfo
;
12110 eoinfo
.localsyms
= TRUE
;
12111 eoinfo
.file_sym_done
= FALSE
;
12112 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12116 /* If backend needs to output some local symbols not present in the hash
12117 table, do it now. */
12118 if (bed
->elf_backend_output_arch_local_syms
12119 && (info
->strip
!= strip_all
|| emit_relocs
))
12121 typedef int (*out_sym_func
)
12122 (void *, const char *, Elf_Internal_Sym
*, asection
*,
12123 struct elf_link_hash_entry
*);
12125 if (! ((*bed
->elf_backend_output_arch_local_syms
)
12126 (abfd
, info
, &flinfo
,
12127 (out_sym_func
) elf_link_output_symstrtab
)))
12131 /* That wrote out all the local symbols. Finish up the symbol table
12132 with the global symbols. Even if we want to strip everything we
12133 can, we still need to deal with those global symbols that got
12134 converted to local in a version script. */
12136 /* The sh_info field records the index of the first non local symbol. */
12137 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12140 && htab
->dynsym
!= NULL
12141 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
12143 Elf_Internal_Sym sym
;
12144 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
12146 o
= htab
->dynsym
->output_section
;
12147 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
12149 /* Write out the section symbols for the output sections. */
12150 if (bfd_link_pic (info
)
12151 || htab
->is_relocatable_executable
)
12157 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12159 sym
.st_target_internal
= 0;
12161 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
12167 dynindx
= elf_section_data (s
)->dynindx
;
12170 indx
= elf_section_data (s
)->this_idx
;
12171 BFD_ASSERT (indx
> 0);
12172 sym
.st_shndx
= indx
;
12173 if (! check_dynsym (abfd
, &sym
))
12175 sym
.st_value
= s
->vma
;
12176 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
12177 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12181 /* Write out the local dynsyms. */
12182 if (htab
->dynlocal
)
12184 struct elf_link_local_dynamic_entry
*e
;
12185 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
12190 /* Copy the internal symbol and turn off visibility.
12191 Note that we saved a word of storage and overwrote
12192 the original st_name with the dynstr_index. */
12194 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
12196 s
= bfd_section_from_elf_index (e
->input_bfd
,
12201 elf_section_data (s
->output_section
)->this_idx
;
12202 if (! check_dynsym (abfd
, &sym
))
12204 sym
.st_value
= (s
->output_section
->vma
12206 + e
->isym
.st_value
);
12209 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
12210 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12215 /* We get the global symbols from the hash table. */
12216 eoinfo
.failed
= FALSE
;
12217 eoinfo
.localsyms
= FALSE
;
12218 eoinfo
.flinfo
= &flinfo
;
12219 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12223 /* If backend needs to output some symbols not present in the hash
12224 table, do it now. */
12225 if (bed
->elf_backend_output_arch_syms
12226 && (info
->strip
!= strip_all
|| emit_relocs
))
12228 typedef int (*out_sym_func
)
12229 (void *, const char *, Elf_Internal_Sym
*, asection
*,
12230 struct elf_link_hash_entry
*);
12232 if (! ((*bed
->elf_backend_output_arch_syms
)
12233 (abfd
, info
, &flinfo
,
12234 (out_sym_func
) elf_link_output_symstrtab
)))
12238 /* Finalize the .strtab section. */
12239 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
12241 /* Swap out the .strtab section. */
12242 if (!elf_link_swap_symbols_out (&flinfo
))
12245 /* Now we know the size of the symtab section. */
12246 if (bfd_get_symcount (abfd
) > 0)
12248 /* Finish up and write out the symbol string table (.strtab)
12250 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
12251 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
12253 if (elf_symtab_shndx_list (abfd
))
12255 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
12257 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
12259 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
12260 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
12261 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
12262 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
12263 symtab_shndx_hdr
->sh_size
= amt
;
12265 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
12268 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
12269 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
12274 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
12275 /* sh_name was set in prep_headers. */
12276 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
12277 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
12278 symstrtab_hdr
->sh_addr
= 0;
12279 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
12280 symstrtab_hdr
->sh_entsize
= 0;
12281 symstrtab_hdr
->sh_link
= 0;
12282 symstrtab_hdr
->sh_info
= 0;
12283 /* sh_offset is set just below. */
12284 symstrtab_hdr
->sh_addralign
= 1;
12286 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
12288 elf_next_file_pos (abfd
) = off
;
12290 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
12291 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
12295 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
12297 _bfd_error_handler (_("%pB: failed to generate import library"),
12298 info
->out_implib_bfd
);
12302 /* Adjust the relocs to have the correct symbol indices. */
12303 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12305 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12308 if ((o
->flags
& SEC_RELOC
) == 0)
12311 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
12312 if (esdo
->rel
.hdr
!= NULL
12313 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
, info
))
12315 if (esdo
->rela
.hdr
!= NULL
12316 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
, info
))
12319 /* Set the reloc_count field to 0 to prevent write_relocs from
12320 trying to swap the relocs out itself. */
12321 o
->reloc_count
= 0;
12324 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
12325 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
12327 /* If we are linking against a dynamic object, or generating a
12328 shared library, finish up the dynamic linking information. */
12331 bfd_byte
*dyncon
, *dynconend
;
12333 /* Fix up .dynamic entries. */
12334 o
= bfd_get_linker_section (dynobj
, ".dynamic");
12335 BFD_ASSERT (o
!= NULL
);
12337 dyncon
= o
->contents
;
12338 dynconend
= o
->contents
+ o
->size
;
12339 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12341 Elf_Internal_Dyn dyn
;
12344 bfd_size_type sh_size
;
12347 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12354 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
12356 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
12358 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
12359 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
12362 dyn
.d_un
.d_val
= relativecount
;
12369 name
= info
->init_function
;
12372 name
= info
->fini_function
;
12375 struct elf_link_hash_entry
*h
;
12377 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
12379 && (h
->root
.type
== bfd_link_hash_defined
12380 || h
->root
.type
== bfd_link_hash_defweak
))
12382 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
12383 o
= h
->root
.u
.def
.section
;
12384 if (o
->output_section
!= NULL
)
12385 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
12386 + o
->output_offset
);
12389 /* The symbol is imported from another shared
12390 library and does not apply to this one. */
12391 dyn
.d_un
.d_ptr
= 0;
12398 case DT_PREINIT_ARRAYSZ
:
12399 name
= ".preinit_array";
12401 case DT_INIT_ARRAYSZ
:
12402 name
= ".init_array";
12404 case DT_FINI_ARRAYSZ
:
12405 name
= ".fini_array";
12407 o
= bfd_get_section_by_name (abfd
, name
);
12411 (_("could not find section %s"), name
);
12416 (_("warning: %s section has zero size"), name
);
12417 dyn
.d_un
.d_val
= o
->size
;
12420 case DT_PREINIT_ARRAY
:
12421 name
= ".preinit_array";
12423 case DT_INIT_ARRAY
:
12424 name
= ".init_array";
12426 case DT_FINI_ARRAY
:
12427 name
= ".fini_array";
12429 o
= bfd_get_section_by_name (abfd
, name
);
12436 name
= ".gnu.hash";
12445 name
= ".gnu.version_d";
12448 name
= ".gnu.version_r";
12451 name
= ".gnu.version";
12453 o
= bfd_get_linker_section (dynobj
, name
);
12455 if (o
== NULL
|| bfd_is_abs_section (o
->output_section
))
12458 (_("could not find section %s"), name
);
12461 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
12464 (_("warning: section '%s' is being made into a note"), name
);
12465 bfd_set_error (bfd_error_nonrepresentable_section
);
12468 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
12475 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
12481 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12483 Elf_Internal_Shdr
*hdr
;
12485 hdr
= elf_elfsections (abfd
)[i
];
12486 if (hdr
->sh_type
== type
12487 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
12489 sh_size
+= hdr
->sh_size
;
12491 || sh_addr
> hdr
->sh_addr
)
12492 sh_addr
= hdr
->sh_addr
;
12496 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
12498 /* Don't count procedure linkage table relocs in the
12499 overall reloc count. */
12500 sh_size
-= htab
->srelplt
->size
;
12502 /* If the size is zero, make the address zero too.
12503 This is to avoid a glibc bug. If the backend
12504 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12505 zero, then we'll put DT_RELA at the end of
12506 DT_JMPREL. glibc will interpret the end of
12507 DT_RELA matching the end of DT_JMPREL as the
12508 case where DT_RELA includes DT_JMPREL, and for
12509 LD_BIND_NOW will decide that processing DT_RELA
12510 will process the PLT relocs too. Net result:
12511 No PLT relocs applied. */
12514 /* If .rela.plt is the first .rela section, exclude
12515 it from DT_RELA. */
12516 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
12517 + htab
->srelplt
->output_offset
))
12518 sh_addr
+= htab
->srelplt
->size
;
12521 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
12522 dyn
.d_un
.d_val
= sh_size
;
12524 dyn
.d_un
.d_ptr
= sh_addr
;
12527 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
12531 /* If we have created any dynamic sections, then output them. */
12532 if (dynobj
!= NULL
)
12534 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
12537 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12538 if (((info
->warn_shared_textrel
&& bfd_link_pic (info
))
12539 || info
->error_textrel
)
12540 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
)
12542 bfd_byte
*dyncon
, *dynconend
;
12544 dyncon
= o
->contents
;
12545 dynconend
= o
->contents
+ o
->size
;
12546 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12548 Elf_Internal_Dyn dyn
;
12550 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12552 if (dyn
.d_tag
== DT_TEXTREL
)
12554 if (info
->error_textrel
)
12555 info
->callbacks
->einfo
12556 (_("%P%X: read-only segment has dynamic relocations\n"));
12558 info
->callbacks
->einfo
12559 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
12565 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
12567 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
12569 || o
->output_section
== bfd_abs_section_ptr
)
12571 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
12573 /* At this point, we are only interested in sections
12574 created by _bfd_elf_link_create_dynamic_sections. */
12577 if (htab
->stab_info
.stabstr
== o
)
12579 if (htab
->eh_info
.hdr_sec
== o
)
12581 if (strcmp (o
->name
, ".dynstr") != 0)
12583 if (! bfd_set_section_contents (abfd
, o
->output_section
,
12585 (file_ptr
) o
->output_offset
12586 * bfd_octets_per_byte (abfd
),
12592 /* The contents of the .dynstr section are actually in a
12596 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
12597 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
12598 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
12604 if (!info
->resolve_section_groups
)
12606 bfd_boolean failed
= FALSE
;
12608 BFD_ASSERT (bfd_link_relocatable (info
));
12609 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
12614 /* If we have optimized stabs strings, output them. */
12615 if (htab
->stab_info
.stabstr
!= NULL
)
12617 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
12621 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
12624 elf_final_link_free (abfd
, &flinfo
);
12626 elf_linker (abfd
) = TRUE
;
12630 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
12631 if (contents
== NULL
)
12632 return FALSE
; /* Bail out and fail. */
12633 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
12634 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
12641 elf_final_link_free (abfd
, &flinfo
);
12645 /* Initialize COOKIE for input bfd ABFD. */
12648 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
12649 struct bfd_link_info
*info
, bfd
*abfd
)
12651 Elf_Internal_Shdr
*symtab_hdr
;
12652 const struct elf_backend_data
*bed
;
12654 bed
= get_elf_backend_data (abfd
);
12655 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12657 cookie
->abfd
= abfd
;
12658 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
12659 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
12660 if (cookie
->bad_symtab
)
12662 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
12663 cookie
->extsymoff
= 0;
12667 cookie
->locsymcount
= symtab_hdr
->sh_info
;
12668 cookie
->extsymoff
= symtab_hdr
->sh_info
;
12671 if (bed
->s
->arch_size
== 32)
12672 cookie
->r_sym_shift
= 8;
12674 cookie
->r_sym_shift
= 32;
12676 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
12677 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
12679 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
12680 cookie
->locsymcount
, 0,
12682 if (cookie
->locsyms
== NULL
)
12684 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
12687 if (info
->keep_memory
)
12688 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
12693 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12696 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
12698 Elf_Internal_Shdr
*symtab_hdr
;
12700 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12701 if (cookie
->locsyms
!= NULL
12702 && symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
12703 free (cookie
->locsyms
);
12706 /* Initialize the relocation information in COOKIE for input section SEC
12707 of input bfd ABFD. */
12710 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12711 struct bfd_link_info
*info
, bfd
*abfd
,
12714 if (sec
->reloc_count
== 0)
12716 cookie
->rels
= NULL
;
12717 cookie
->relend
= NULL
;
12721 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
12722 info
->keep_memory
);
12723 if (cookie
->rels
== NULL
)
12725 cookie
->rel
= cookie
->rels
;
12726 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
12728 cookie
->rel
= cookie
->rels
;
12732 /* Free the memory allocated by init_reloc_cookie_rels,
12736 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12739 if (cookie
->rels
&& elf_section_data (sec
)->relocs
!= cookie
->rels
)
12740 free (cookie
->rels
);
12743 /* Initialize the whole of COOKIE for input section SEC. */
12746 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12747 struct bfd_link_info
*info
,
12750 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
12752 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
12757 fini_reloc_cookie (cookie
, sec
->owner
);
12762 /* Free the memory allocated by init_reloc_cookie_for_section,
12766 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12769 fini_reloc_cookie_rels (cookie
, sec
);
12770 fini_reloc_cookie (cookie
, sec
->owner
);
12773 /* Garbage collect unused sections. */
12775 /* Default gc_mark_hook. */
12778 _bfd_elf_gc_mark_hook (asection
*sec
,
12779 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12780 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
12781 struct elf_link_hash_entry
*h
,
12782 Elf_Internal_Sym
*sym
)
12786 switch (h
->root
.type
)
12788 case bfd_link_hash_defined
:
12789 case bfd_link_hash_defweak
:
12790 return h
->root
.u
.def
.section
;
12792 case bfd_link_hash_common
:
12793 return h
->root
.u
.c
.p
->section
;
12800 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
12805 /* Return the global debug definition section. */
12808 elf_gc_mark_debug_section (asection
*sec ATTRIBUTE_UNUSED
,
12809 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12810 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
12811 struct elf_link_hash_entry
*h
,
12812 Elf_Internal_Sym
*sym ATTRIBUTE_UNUSED
)
12815 && (h
->root
.type
== bfd_link_hash_defined
12816 || h
->root
.type
== bfd_link_hash_defweak
)
12817 && (h
->root
.u
.def
.section
->flags
& SEC_DEBUGGING
) != 0)
12818 return h
->root
.u
.def
.section
;
12823 /* COOKIE->rel describes a relocation against section SEC, which is
12824 a section we've decided to keep. Return the section that contains
12825 the relocation symbol, or NULL if no section contains it. */
12828 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
12829 elf_gc_mark_hook_fn gc_mark_hook
,
12830 struct elf_reloc_cookie
*cookie
,
12831 bfd_boolean
*start_stop
)
12833 unsigned long r_symndx
;
12834 struct elf_link_hash_entry
*h
;
12836 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
12837 if (r_symndx
== STN_UNDEF
)
12840 if (r_symndx
>= cookie
->locsymcount
12841 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
12843 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
12846 info
->callbacks
->einfo (_("%F%P: corrupt input: %pB\n"),
12850 while (h
->root
.type
== bfd_link_hash_indirect
12851 || h
->root
.type
== bfd_link_hash_warning
)
12852 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
12854 /* If this symbol is weak and there is a non-weak definition, we
12855 keep the non-weak definition because many backends put
12856 dynamic reloc info on the non-weak definition for code
12857 handling copy relocs. */
12858 if (h
->is_weakalias
)
12859 weakdef (h
)->mark
= 1;
12861 if (start_stop
!= NULL
)
12863 /* To work around a glibc bug, mark XXX input sections
12864 when there is a reference to __start_XXX or __stop_XXX
12868 asection
*s
= h
->u2
.start_stop_section
;
12869 *start_stop
= !s
->gc_mark
;
12874 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
12877 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
12878 &cookie
->locsyms
[r_symndx
]);
12881 /* COOKIE->rel describes a relocation against section SEC, which is
12882 a section we've decided to keep. Mark the section that contains
12883 the relocation symbol. */
12886 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
12888 elf_gc_mark_hook_fn gc_mark_hook
,
12889 struct elf_reloc_cookie
*cookie
)
12892 bfd_boolean start_stop
= FALSE
;
12894 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
12895 while (rsec
!= NULL
)
12897 if (!rsec
->gc_mark
)
12899 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
12900 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
12902 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
12907 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
12912 /* The mark phase of garbage collection. For a given section, mark
12913 it and any sections in this section's group, and all the sections
12914 which define symbols to which it refers. */
12917 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
12919 elf_gc_mark_hook_fn gc_mark_hook
)
12922 asection
*group_sec
, *eh_frame
;
12926 /* Mark all the sections in the group. */
12927 group_sec
= elf_section_data (sec
)->next_in_group
;
12928 if (group_sec
&& !group_sec
->gc_mark
)
12929 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
12932 /* Look through the section relocs. */
12934 eh_frame
= elf_eh_frame_section (sec
->owner
);
12935 if ((sec
->flags
& SEC_RELOC
) != 0
12936 && sec
->reloc_count
> 0
12937 && sec
!= eh_frame
)
12939 struct elf_reloc_cookie cookie
;
12941 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
12945 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
12946 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
12951 fini_reloc_cookie_for_section (&cookie
, sec
);
12955 if (ret
&& eh_frame
&& elf_fde_list (sec
))
12957 struct elf_reloc_cookie cookie
;
12959 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
12963 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
12964 gc_mark_hook
, &cookie
))
12966 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
12970 eh_frame
= elf_section_eh_frame_entry (sec
);
12971 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
12972 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
12978 /* Scan and mark sections in a special or debug section group. */
12981 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
12983 /* Point to first section of section group. */
12985 /* Used to iterate the section group. */
12988 bfd_boolean is_special_grp
= TRUE
;
12989 bfd_boolean is_debug_grp
= TRUE
;
12991 /* First scan to see if group contains any section other than debug
12992 and special section. */
12993 ssec
= msec
= elf_next_in_group (grp
);
12996 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
12997 is_debug_grp
= FALSE
;
12999 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
13000 is_special_grp
= FALSE
;
13002 msec
= elf_next_in_group (msec
);
13004 while (msec
!= ssec
);
13006 /* If this is a pure debug section group or pure special section group,
13007 keep all sections in this group. */
13008 if (is_debug_grp
|| is_special_grp
)
13013 msec
= elf_next_in_group (msec
);
13015 while (msec
!= ssec
);
13019 /* Keep debug and special sections. */
13022 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
13023 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED
)
13027 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13030 bfd_boolean some_kept
;
13031 bfd_boolean debug_frag_seen
;
13032 bfd_boolean has_kept_debug_info
;
13034 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13036 isec
= ibfd
->sections
;
13037 if (isec
== NULL
|| isec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13040 /* Ensure all linker created sections are kept,
13041 see if any other section is already marked,
13042 and note if we have any fragmented debug sections. */
13043 debug_frag_seen
= some_kept
= has_kept_debug_info
= FALSE
;
13044 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13046 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
13048 else if (isec
->gc_mark
13049 && (isec
->flags
& SEC_ALLOC
) != 0
13050 && elf_section_type (isec
) != SHT_NOTE
)
13053 if (!debug_frag_seen
13054 && (isec
->flags
& SEC_DEBUGGING
)
13055 && CONST_STRNEQ (isec
->name
, ".debug_line."))
13056 debug_frag_seen
= TRUE
;
13059 /* If no non-note alloc section in this file will be kept, then
13060 we can toss out the debug and special sections. */
13064 /* Keep debug and special sections like .comment when they are
13065 not part of a group. Also keep section groups that contain
13066 just debug sections or special sections. */
13067 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13069 if ((isec
->flags
& SEC_GROUP
) != 0)
13070 _bfd_elf_gc_mark_debug_special_section_group (isec
);
13071 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
13072 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
13073 && elf_next_in_group (isec
) == NULL
)
13075 if (isec
->gc_mark
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
13076 has_kept_debug_info
= TRUE
;
13079 /* Look for CODE sections which are going to be discarded,
13080 and find and discard any fragmented debug sections which
13081 are associated with that code section. */
13082 if (debug_frag_seen
)
13083 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13084 if ((isec
->flags
& SEC_CODE
) != 0
13085 && isec
->gc_mark
== 0)
13090 ilen
= strlen (isec
->name
);
13092 /* Association is determined by the name of the debug
13093 section containing the name of the code section as
13094 a suffix. For example .debug_line.text.foo is a
13095 debug section associated with .text.foo. */
13096 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
13100 if (dsec
->gc_mark
== 0
13101 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
13104 dlen
= strlen (dsec
->name
);
13107 && strncmp (dsec
->name
+ (dlen
- ilen
),
13108 isec
->name
, ilen
) == 0)
13113 /* Mark debug sections referenced by kept debug sections. */
13114 if (has_kept_debug_info
)
13115 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13117 && (isec
->flags
& SEC_DEBUGGING
) != 0)
13118 if (!_bfd_elf_gc_mark (info
, isec
,
13119 elf_gc_mark_debug_section
))
13126 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
13129 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13131 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13135 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13136 || elf_object_id (sub
) != elf_hash_table_id (elf_hash_table (info
))
13137 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13140 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13143 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13145 /* When any section in a section group is kept, we keep all
13146 sections in the section group. If the first member of
13147 the section group is excluded, we will also exclude the
13149 if (o
->flags
& SEC_GROUP
)
13151 asection
*first
= elf_next_in_group (o
);
13152 o
->gc_mark
= first
->gc_mark
;
13158 /* Skip sweeping sections already excluded. */
13159 if (o
->flags
& SEC_EXCLUDE
)
13162 /* Since this is early in the link process, it is simple
13163 to remove a section from the output. */
13164 o
->flags
|= SEC_EXCLUDE
;
13166 if (info
->print_gc_sections
&& o
->size
!= 0)
13167 /* xgettext:c-format */
13168 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13176 /* Propagate collected vtable information. This is called through
13177 elf_link_hash_traverse. */
13180 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
13182 /* Those that are not vtables. */
13184 || h
->u2
.vtable
== NULL
13185 || h
->u2
.vtable
->parent
== NULL
)
13188 /* Those vtables that do not have parents, we cannot merge. */
13189 if (h
->u2
.vtable
->parent
== (struct elf_link_hash_entry
*) -1)
13192 /* If we've already been done, exit. */
13193 if (h
->u2
.vtable
->used
&& h
->u2
.vtable
->used
[-1])
13196 /* Make sure the parent's table is up to date. */
13197 elf_gc_propagate_vtable_entries_used (h
->u2
.vtable
->parent
, okp
);
13199 if (h
->u2
.vtable
->used
== NULL
)
13201 /* None of this table's entries were referenced. Re-use the
13203 h
->u2
.vtable
->used
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13204 h
->u2
.vtable
->size
= h
->u2
.vtable
->parent
->u2
.vtable
->size
;
13209 bfd_boolean
*cu
, *pu
;
13211 /* Or the parent's entries into ours. */
13212 cu
= h
->u2
.vtable
->used
;
13214 pu
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13217 const struct elf_backend_data
*bed
;
13218 unsigned int log_file_align
;
13220 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
13221 log_file_align
= bed
->s
->log_file_align
;
13222 n
= h
->u2
.vtable
->parent
->u2
.vtable
->size
>> log_file_align
;
13237 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
13240 bfd_vma hstart
, hend
;
13241 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
13242 const struct elf_backend_data
*bed
;
13243 unsigned int log_file_align
;
13245 /* Take care of both those symbols that do not describe vtables as
13246 well as those that are not loaded. */
13248 || h
->u2
.vtable
== NULL
13249 || h
->u2
.vtable
->parent
== NULL
)
13252 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
13253 || h
->root
.type
== bfd_link_hash_defweak
);
13255 sec
= h
->root
.u
.def
.section
;
13256 hstart
= h
->root
.u
.def
.value
;
13257 hend
= hstart
+ h
->size
;
13259 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
13261 return *(bfd_boolean
*) okp
= FALSE
;
13262 bed
= get_elf_backend_data (sec
->owner
);
13263 log_file_align
= bed
->s
->log_file_align
;
13265 relend
= relstart
+ sec
->reloc_count
;
13267 for (rel
= relstart
; rel
< relend
; ++rel
)
13268 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
13270 /* If the entry is in use, do nothing. */
13271 if (h
->u2
.vtable
->used
13272 && (rel
->r_offset
- hstart
) < h
->u2
.vtable
->size
)
13274 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
13275 if (h
->u2
.vtable
->used
[entry
])
13278 /* Otherwise, kill it. */
13279 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
13285 /* Mark sections containing dynamically referenced symbols. When
13286 building shared libraries, we must assume that any visible symbol is
13290 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
13292 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
13293 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
13295 if ((h
->root
.type
== bfd_link_hash_defined
13296 || h
->root
.type
== bfd_link_hash_defweak
)
13297 && ((h
->ref_dynamic
&& !h
->forced_local
)
13298 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
13299 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
13300 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
13301 && (!bfd_link_executable (info
)
13302 || info
->gc_keep_exported
13303 || info
->export_dynamic
13306 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
13307 && (h
->versioned
>= versioned
13308 || !bfd_hide_sym_by_version (info
->version_info
,
13309 h
->root
.root
.string
)))))
13310 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13315 /* Keep all sections containing symbols undefined on the command-line,
13316 and the section containing the entry symbol. */
13319 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
13321 struct bfd_sym_chain
*sym
;
13323 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
13325 struct elf_link_hash_entry
*h
;
13327 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
13328 FALSE
, FALSE
, FALSE
);
13331 && (h
->root
.type
== bfd_link_hash_defined
13332 || h
->root
.type
== bfd_link_hash_defweak
)
13333 && !bfd_is_abs_section (h
->root
.u
.def
.section
)
13334 && !bfd_is_und_section (h
->root
.u
.def
.section
))
13335 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13340 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
13341 struct bfd_link_info
*info
)
13343 bfd
*ibfd
= info
->input_bfds
;
13345 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13348 struct elf_reloc_cookie cookie
;
13350 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13352 sec
= ibfd
->sections
;
13353 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13356 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
13359 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
13361 if (CONST_STRNEQ (bfd_section_name (ibfd
, sec
), ".eh_frame_entry")
13362 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
13364 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
13365 fini_reloc_cookie_rels (&cookie
, sec
);
13372 /* Do mark and sweep of unused sections. */
13375 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
13377 bfd_boolean ok
= TRUE
;
13379 elf_gc_mark_hook_fn gc_mark_hook
;
13380 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13381 struct elf_link_hash_table
*htab
;
13383 if (!bed
->can_gc_sections
13384 || !is_elf_hash_table (info
->hash
))
13386 _bfd_error_handler(_("warning: gc-sections option ignored"));
13390 bed
->gc_keep (info
);
13391 htab
= elf_hash_table (info
);
13393 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13394 at the .eh_frame section if we can mark the FDEs individually. */
13395 for (sub
= info
->input_bfds
;
13396 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
13397 sub
= sub
->link
.next
)
13400 struct elf_reloc_cookie cookie
;
13402 sec
= sub
->sections
;
13403 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13405 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
13406 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
13408 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
13409 if (elf_section_data (sec
)->sec_info
13410 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
13411 elf_eh_frame_section (sub
) = sec
;
13412 fini_reloc_cookie_for_section (&cookie
, sec
);
13413 sec
= bfd_get_next_section_by_name (NULL
, sec
);
13417 /* Apply transitive closure to the vtable entry usage info. */
13418 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
13422 /* Kill the vtable relocations that were not used. */
13423 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &ok
);
13427 /* Mark dynamically referenced symbols. */
13428 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
13429 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
13431 /* Grovel through relocs to find out who stays ... */
13432 gc_mark_hook
= bed
->gc_mark_hook
;
13433 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13437 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13438 || elf_object_id (sub
) != elf_hash_table_id (htab
)
13439 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13443 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13446 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13447 Also treat note sections as a root, if the section is not part
13448 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13449 well as FINI_ARRAY sections for ld -r. */
13450 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13452 && (o
->flags
& SEC_EXCLUDE
) == 0
13453 && ((o
->flags
& SEC_KEEP
) != 0
13454 || (bfd_link_relocatable (info
)
13455 && ((elf_section_data (o
)->this_hdr
.sh_type
13456 == SHT_PREINIT_ARRAY
)
13457 || (elf_section_data (o
)->this_hdr
.sh_type
13459 || (elf_section_data (o
)->this_hdr
.sh_type
13460 == SHT_FINI_ARRAY
)))
13461 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
13462 && elf_next_in_group (o
) == NULL
)))
13464 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
13469 /* Allow the backend to mark additional target specific sections. */
13470 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
13472 /* ... and mark SEC_EXCLUDE for those that go. */
13473 return elf_gc_sweep (abfd
, info
);
13476 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13479 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
13481 struct elf_link_hash_entry
*h
,
13484 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
13485 struct elf_link_hash_entry
**search
, *child
;
13486 size_t extsymcount
;
13487 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13489 /* The sh_info field of the symtab header tells us where the
13490 external symbols start. We don't care about the local symbols at
13492 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
13493 if (!elf_bad_symtab (abfd
))
13494 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
13496 sym_hashes
= elf_sym_hashes (abfd
);
13497 sym_hashes_end
= sym_hashes
+ extsymcount
;
13499 /* Hunt down the child symbol, which is in this section at the same
13500 offset as the relocation. */
13501 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
13503 if ((child
= *search
) != NULL
13504 && (child
->root
.type
== bfd_link_hash_defined
13505 || child
->root
.type
== bfd_link_hash_defweak
)
13506 && child
->root
.u
.def
.section
== sec
13507 && child
->root
.u
.def
.value
== offset
)
13511 /* xgettext:c-format */
13512 _bfd_error_handler (_("%pB: %pA+%#" PRIx64
": no symbol found for INHERIT"),
13513 abfd
, sec
, (uint64_t) offset
);
13514 bfd_set_error (bfd_error_invalid_operation
);
13518 if (!child
->u2
.vtable
)
13520 child
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
13521 bfd_zalloc (abfd
, sizeof (*child
->u2
.vtable
)));
13522 if (!child
->u2
.vtable
)
13527 /* This *should* only be the absolute section. It could potentially
13528 be that someone has defined a non-global vtable though, which
13529 would be bad. It isn't worth paging in the local symbols to be
13530 sure though; that case should simply be handled by the assembler. */
13532 child
->u2
.vtable
->parent
= (struct elf_link_hash_entry
*) -1;
13535 child
->u2
.vtable
->parent
= h
;
13540 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13543 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
13544 asection
*sec ATTRIBUTE_UNUSED
,
13545 struct elf_link_hash_entry
*h
,
13548 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13549 unsigned int log_file_align
= bed
->s
->log_file_align
;
13553 h
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
13554 bfd_zalloc (abfd
, sizeof (*h
->u2
.vtable
)));
13559 if (addend
>= h
->u2
.vtable
->size
)
13561 size_t size
, bytes
, file_align
;
13562 bfd_boolean
*ptr
= h
->u2
.vtable
->used
;
13564 /* While the symbol is undefined, we have to be prepared to handle
13566 file_align
= 1 << log_file_align
;
13567 if (h
->root
.type
== bfd_link_hash_undefined
)
13568 size
= addend
+ file_align
;
13572 if (addend
>= size
)
13574 /* Oops! We've got a reference past the defined end of
13575 the table. This is probably a bug -- shall we warn? */
13576 size
= addend
+ file_align
;
13579 size
= (size
+ file_align
- 1) & -file_align
;
13581 /* Allocate one extra entry for use as a "done" flag for the
13582 consolidation pass. */
13583 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
13587 ptr
= (bfd_boolean
*) bfd_realloc (ptr
- 1, bytes
);
13593 oldbytes
= (((h
->u2
.vtable
->size
>> log_file_align
) + 1)
13594 * sizeof (bfd_boolean
));
13595 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
13599 ptr
= (bfd_boolean
*) bfd_zmalloc (bytes
);
13604 /* And arrange for that done flag to be at index -1. */
13605 h
->u2
.vtable
->used
= ptr
+ 1;
13606 h
->u2
.vtable
->size
= size
;
13609 h
->u2
.vtable
->used
[addend
>> log_file_align
] = TRUE
;
13614 /* Map an ELF section header flag to its corresponding string. */
13618 flagword flag_value
;
13619 } elf_flags_to_name_table
;
13621 static elf_flags_to_name_table elf_flags_to_names
[] =
13623 { "SHF_WRITE", SHF_WRITE
},
13624 { "SHF_ALLOC", SHF_ALLOC
},
13625 { "SHF_EXECINSTR", SHF_EXECINSTR
},
13626 { "SHF_MERGE", SHF_MERGE
},
13627 { "SHF_STRINGS", SHF_STRINGS
},
13628 { "SHF_INFO_LINK", SHF_INFO_LINK
},
13629 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
13630 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
13631 { "SHF_GROUP", SHF_GROUP
},
13632 { "SHF_TLS", SHF_TLS
},
13633 { "SHF_MASKOS", SHF_MASKOS
},
13634 { "SHF_EXCLUDE", SHF_EXCLUDE
},
13637 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13639 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
13640 struct flag_info
*flaginfo
,
13643 const bfd_vma sh_flags
= elf_section_flags (section
);
13645 if (!flaginfo
->flags_initialized
)
13647 bfd
*obfd
= info
->output_bfd
;
13648 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13649 struct flag_info_list
*tf
= flaginfo
->flag_list
;
13651 int without_hex
= 0;
13653 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
13656 flagword (*lookup
) (char *);
13658 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
13659 if (lookup
!= NULL
)
13661 flagword hexval
= (*lookup
) ((char *) tf
->name
);
13665 if (tf
->with
== with_flags
)
13666 with_hex
|= hexval
;
13667 else if (tf
->with
== without_flags
)
13668 without_hex
|= hexval
;
13673 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
13675 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
13677 if (tf
->with
== with_flags
)
13678 with_hex
|= elf_flags_to_names
[i
].flag_value
;
13679 else if (tf
->with
== without_flags
)
13680 without_hex
|= elf_flags_to_names
[i
].flag_value
;
13687 info
->callbacks
->einfo
13688 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
13692 flaginfo
->flags_initialized
= TRUE
;
13693 flaginfo
->only_with_flags
|= with_hex
;
13694 flaginfo
->not_with_flags
|= without_hex
;
13697 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
13700 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
13706 struct alloc_got_off_arg
{
13708 struct bfd_link_info
*info
;
13711 /* We need a special top-level link routine to convert got reference counts
13712 to real got offsets. */
13715 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
13717 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
13718 bfd
*obfd
= gofarg
->info
->output_bfd
;
13719 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13721 if (h
->got
.refcount
> 0)
13723 h
->got
.offset
= gofarg
->gotoff
;
13724 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
13727 h
->got
.offset
= (bfd_vma
) -1;
13732 /* And an accompanying bit to work out final got entry offsets once
13733 we're done. Should be called from final_link. */
13736 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
13737 struct bfd_link_info
*info
)
13740 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13742 struct alloc_got_off_arg gofarg
;
13744 BFD_ASSERT (abfd
== info
->output_bfd
);
13746 if (! is_elf_hash_table (info
->hash
))
13749 /* The GOT offset is relative to the .got section, but the GOT header is
13750 put into the .got.plt section, if the backend uses it. */
13751 if (bed
->want_got_plt
)
13754 gotoff
= bed
->got_header_size
;
13756 /* Do the local .got entries first. */
13757 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
13759 bfd_signed_vma
*local_got
;
13760 size_t j
, locsymcount
;
13761 Elf_Internal_Shdr
*symtab_hdr
;
13763 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
13766 local_got
= elf_local_got_refcounts (i
);
13770 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
13771 if (elf_bad_symtab (i
))
13772 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13774 locsymcount
= symtab_hdr
->sh_info
;
13776 for (j
= 0; j
< locsymcount
; ++j
)
13778 if (local_got
[j
] > 0)
13780 local_got
[j
] = gotoff
;
13781 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
13784 local_got
[j
] = (bfd_vma
) -1;
13788 /* Then the global .got entries. .plt refcounts are handled by
13789 adjust_dynamic_symbol */
13790 gofarg
.gotoff
= gotoff
;
13791 gofarg
.info
= info
;
13792 elf_link_hash_traverse (elf_hash_table (info
),
13793 elf_gc_allocate_got_offsets
,
13798 /* Many folk need no more in the way of final link than this, once
13799 got entry reference counting is enabled. */
13802 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
13804 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
13807 /* Invoke the regular ELF backend linker to do all the work. */
13808 return bfd_elf_final_link (abfd
, info
);
13812 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
13814 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
13816 if (rcookie
->bad_symtab
)
13817 rcookie
->rel
= rcookie
->rels
;
13819 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
13821 unsigned long r_symndx
;
13823 if (! rcookie
->bad_symtab
)
13824 if (rcookie
->rel
->r_offset
> offset
)
13826 if (rcookie
->rel
->r_offset
!= offset
)
13829 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
13830 if (r_symndx
== STN_UNDEF
)
13833 if (r_symndx
>= rcookie
->locsymcount
13834 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13836 struct elf_link_hash_entry
*h
;
13838 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
13840 while (h
->root
.type
== bfd_link_hash_indirect
13841 || h
->root
.type
== bfd_link_hash_warning
)
13842 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13844 if ((h
->root
.type
== bfd_link_hash_defined
13845 || h
->root
.type
== bfd_link_hash_defweak
)
13846 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
13847 || h
->root
.u
.def
.section
->kept_section
!= NULL
13848 || discarded_section (h
->root
.u
.def
.section
)))
13853 /* It's not a relocation against a global symbol,
13854 but it could be a relocation against a local
13855 symbol for a discarded section. */
13857 Elf_Internal_Sym
*isym
;
13859 /* Need to: get the symbol; get the section. */
13860 isym
= &rcookie
->locsyms
[r_symndx
];
13861 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
13863 && (isec
->kept_section
!= NULL
13864 || discarded_section (isec
)))
13872 /* Discard unneeded references to discarded sections.
13873 Returns -1 on error, 1 if any section's size was changed, 0 if
13874 nothing changed. This function assumes that the relocations are in
13875 sorted order, which is true for all known assemblers. */
13878 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
13880 struct elf_reloc_cookie cookie
;
13885 if (info
->traditional_format
13886 || !is_elf_hash_table (info
->hash
))
13889 o
= bfd_get_section_by_name (output_bfd
, ".stab");
13894 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13897 || i
->reloc_count
== 0
13898 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
13902 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13905 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13908 if (_bfd_discard_section_stabs (abfd
, i
,
13909 elf_section_data (i
)->sec_info
,
13910 bfd_elf_reloc_symbol_deleted_p
,
13914 fini_reloc_cookie_for_section (&cookie
, i
);
13919 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
13920 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
13924 int eh_changed
= 0;
13925 unsigned int eh_alignment
;
13927 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13933 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13936 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13939 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
13940 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
13941 bfd_elf_reloc_symbol_deleted_p
,
13945 if (i
->size
!= i
->rawsize
)
13949 fini_reloc_cookie_for_section (&cookie
, i
);
13952 eh_alignment
= 1 << o
->alignment_power
;
13953 /* Skip over zero terminator, and prevent empty sections from
13954 adding alignment padding at the end. */
13955 for (i
= o
->map_tail
.s
; i
!= NULL
; i
= i
->map_tail
.s
)
13957 i
->flags
|= SEC_EXCLUDE
;
13958 else if (i
->size
> 4)
13960 /* The last non-empty eh_frame section doesn't need padding. */
13963 /* Any prior sections must pad the last FDE out to the output
13964 section alignment. Otherwise we might have zero padding
13965 between sections, which would be seen as a terminator. */
13966 for (; i
!= NULL
; i
= i
->map_tail
.s
)
13968 /* All but the last zero terminator should have been removed. */
13973 = (i
->size
+ eh_alignment
- 1) & -eh_alignment
;
13974 if (i
->size
!= size
)
13982 elf_link_hash_traverse (elf_hash_table (info
),
13983 _bfd_elf_adjust_eh_frame_global_symbol
, NULL
);
13986 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
13988 const struct elf_backend_data
*bed
;
13991 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13993 s
= abfd
->sections
;
13994 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13997 bed
= get_elf_backend_data (abfd
);
13999 if (bed
->elf_backend_discard_info
!= NULL
)
14001 if (!init_reloc_cookie (&cookie
, info
, abfd
))
14004 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
14007 fini_reloc_cookie (&cookie
, abfd
);
14011 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
14012 _bfd_elf_end_eh_frame_parsing (info
);
14014 if (info
->eh_frame_hdr_type
14015 && !bfd_link_relocatable (info
)
14016 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
14023 _bfd_elf_section_already_linked (bfd
*abfd
,
14025 struct bfd_link_info
*info
)
14028 const char *name
, *key
;
14029 struct bfd_section_already_linked
*l
;
14030 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
14032 if (sec
->output_section
== bfd_abs_section_ptr
)
14035 flags
= sec
->flags
;
14037 /* Return if it isn't a linkonce section. A comdat group section
14038 also has SEC_LINK_ONCE set. */
14039 if ((flags
& SEC_LINK_ONCE
) == 0)
14042 /* Don't put group member sections on our list of already linked
14043 sections. They are handled as a group via their group section. */
14044 if (elf_sec_group (sec
) != NULL
)
14047 /* For a SHT_GROUP section, use the group signature as the key. */
14049 if ((flags
& SEC_GROUP
) != 0
14050 && elf_next_in_group (sec
) != NULL
14051 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
14052 key
= elf_group_name (elf_next_in_group (sec
));
14055 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14056 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
14057 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
14060 /* Must be a user linkonce section that doesn't follow gcc's
14061 naming convention. In this case we won't be matching
14062 single member groups. */
14066 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
14068 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14070 /* We may have 2 different types of sections on the list: group
14071 sections with a signature of <key> (<key> is some string),
14072 and linkonce sections named .gnu.linkonce.<type>.<key>.
14073 Match like sections. LTO plugin sections are an exception.
14074 They are always named .gnu.linkonce.t.<key> and match either
14075 type of section. */
14076 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
14077 && ((flags
& SEC_GROUP
) != 0
14078 || strcmp (name
, l
->sec
->name
) == 0))
14079 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0)
14081 /* The section has already been linked. See if we should
14082 issue a warning. */
14083 if (!_bfd_handle_already_linked (sec
, l
, info
))
14086 if (flags
& SEC_GROUP
)
14088 asection
*first
= elf_next_in_group (sec
);
14089 asection
*s
= first
;
14093 s
->output_section
= bfd_abs_section_ptr
;
14094 /* Record which group discards it. */
14095 s
->kept_section
= l
->sec
;
14096 s
= elf_next_in_group (s
);
14097 /* These lists are circular. */
14107 /* A single member comdat group section may be discarded by a
14108 linkonce section and vice versa. */
14109 if ((flags
& SEC_GROUP
) != 0)
14111 asection
*first
= elf_next_in_group (sec
);
14113 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
14114 /* Check this single member group against linkonce sections. */
14115 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14116 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14117 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
14119 first
->output_section
= bfd_abs_section_ptr
;
14120 first
->kept_section
= l
->sec
;
14121 sec
->output_section
= bfd_abs_section_ptr
;
14126 /* Check this linkonce section against single member groups. */
14127 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14128 if (l
->sec
->flags
& SEC_GROUP
)
14130 asection
*first
= elf_next_in_group (l
->sec
);
14133 && elf_next_in_group (first
) == first
14134 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
14136 sec
->output_section
= bfd_abs_section_ptr
;
14137 sec
->kept_section
= first
;
14142 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14143 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14144 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14145 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14146 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14147 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14148 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14149 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14150 The reverse order cannot happen as there is never a bfd with only the
14151 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14152 matter as here were are looking only for cross-bfd sections. */
14154 if ((flags
& SEC_GROUP
) == 0 && CONST_STRNEQ (name
, ".gnu.linkonce.r."))
14155 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14156 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14157 && CONST_STRNEQ (l
->sec
->name
, ".gnu.linkonce.t."))
14159 if (abfd
!= l
->sec
->owner
)
14160 sec
->output_section
= bfd_abs_section_ptr
;
14164 /* This is the first section with this name. Record it. */
14165 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
14166 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
14167 return sec
->output_section
== bfd_abs_section_ptr
;
14171 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
14173 return sym
->st_shndx
== SHN_COMMON
;
14177 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
14183 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
14185 return bfd_com_section_ptr
;
14189 _bfd_elf_default_got_elt_size (bfd
*abfd
,
14190 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
14191 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
14192 bfd
*ibfd ATTRIBUTE_UNUSED
,
14193 unsigned long symndx ATTRIBUTE_UNUSED
)
14195 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14196 return bed
->s
->arch_size
/ 8;
14199 /* Routines to support the creation of dynamic relocs. */
14201 /* Returns the name of the dynamic reloc section associated with SEC. */
14203 static const char *
14204 get_dynamic_reloc_section_name (bfd
* abfd
,
14206 bfd_boolean is_rela
)
14209 const char *old_name
= bfd_get_section_name (NULL
, sec
);
14210 const char *prefix
= is_rela
? ".rela" : ".rel";
14212 if (old_name
== NULL
)
14215 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
14216 sprintf (name
, "%s%s", prefix
, old_name
);
14221 /* Returns the dynamic reloc section associated with SEC.
14222 If necessary compute the name of the dynamic reloc section based
14223 on SEC's name (looked up in ABFD's string table) and the setting
14227 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
14229 bfd_boolean is_rela
)
14231 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14233 if (reloc_sec
== NULL
)
14235 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14239 reloc_sec
= bfd_get_linker_section (abfd
, name
);
14241 if (reloc_sec
!= NULL
)
14242 elf_section_data (sec
)->sreloc
= reloc_sec
;
14249 /* Returns the dynamic reloc section associated with SEC. If the
14250 section does not exist it is created and attached to the DYNOBJ
14251 bfd and stored in the SRELOC field of SEC's elf_section_data
14254 ALIGNMENT is the alignment for the newly created section and
14255 IS_RELA defines whether the name should be .rela.<SEC's name>
14256 or .rel.<SEC's name>. The section name is looked up in the
14257 string table associated with ABFD. */
14260 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
14262 unsigned int alignment
,
14264 bfd_boolean is_rela
)
14266 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14268 if (reloc_sec
== NULL
)
14270 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14275 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
14277 if (reloc_sec
== NULL
)
14279 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
14280 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
14281 if ((sec
->flags
& SEC_ALLOC
) != 0)
14282 flags
|= SEC_ALLOC
| SEC_LOAD
;
14284 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
14285 if (reloc_sec
!= NULL
)
14287 /* _bfd_elf_get_sec_type_attr chooses a section type by
14288 name. Override as it may be wrong, eg. for a user
14289 section named "auto" we'll get ".relauto" which is
14290 seen to be a .rela section. */
14291 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
14292 if (! bfd_set_section_alignment (dynobj
, reloc_sec
, alignment
))
14297 elf_section_data (sec
)->sreloc
= reloc_sec
;
14303 /* Copy the ELF symbol type and other attributes for a linker script
14304 assignment from HSRC to HDEST. Generally this should be treated as
14305 if we found a strong non-dynamic definition for HDEST (except that
14306 ld ignores multiple definition errors). */
14308 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
14309 struct bfd_link_hash_entry
*hdest
,
14310 struct bfd_link_hash_entry
*hsrc
)
14312 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
14313 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
14314 Elf_Internal_Sym isym
;
14316 ehdest
->type
= ehsrc
->type
;
14317 ehdest
->target_internal
= ehsrc
->target_internal
;
14319 isym
.st_other
= ehsrc
->other
;
14320 elf_merge_st_other (abfd
, ehdest
, &isym
, NULL
, TRUE
, FALSE
);
14323 /* Append a RELA relocation REL to section S in BFD. */
14326 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14328 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14329 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
14330 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
14331 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
14334 /* Append a REL relocation REL to section S in BFD. */
14337 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14339 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14340 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
14341 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
14342 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);
14345 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14347 struct bfd_link_hash_entry
*
14348 bfd_elf_define_start_stop (struct bfd_link_info
*info
,
14349 const char *symbol
, asection
*sec
)
14351 struct elf_link_hash_entry
*h
;
14353 h
= elf_link_hash_lookup (elf_hash_table (info
), symbol
,
14354 FALSE
, FALSE
, TRUE
);
14356 && (h
->root
.type
== bfd_link_hash_undefined
14357 || h
->root
.type
== bfd_link_hash_undefweak
14358 || ((h
->ref_regular
|| h
->def_dynamic
) && !h
->def_regular
)))
14360 bfd_boolean was_dynamic
= h
->ref_dynamic
|| h
->def_dynamic
;
14361 h
->root
.type
= bfd_link_hash_defined
;
14362 h
->root
.u
.def
.section
= sec
;
14363 h
->root
.u
.def
.value
= 0;
14364 h
->def_regular
= 1;
14365 h
->def_dynamic
= 0;
14367 h
->u2
.start_stop_section
= sec
;
14368 if (symbol
[0] == '.')
14370 /* .startof. and .sizeof. symbols are local. */
14371 const struct elf_backend_data
*bed
;
14372 bed
= get_elf_backend_data (info
->output_bfd
);
14373 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
14377 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
14378 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED
;
14380 bfd_elf_link_record_dynamic_symbol (info
, h
);