1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2017 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. */
726 if (h
->u
.weakdef
!= NULL
727 && h
->u
.weakdef
->dynindx
== -1)
729 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
737 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
738 success, and 2 on a failure caused by attempting to record a symbol
739 in a discarded section, eg. a discarded link-once section symbol. */
742 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
747 struct elf_link_local_dynamic_entry
*entry
;
748 struct elf_link_hash_table
*eht
;
749 struct elf_strtab_hash
*dynstr
;
752 Elf_External_Sym_Shndx eshndx
;
753 char esym
[sizeof (Elf64_External_Sym
)];
755 if (! is_elf_hash_table (info
->hash
))
758 /* See if the entry exists already. */
759 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
760 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
763 amt
= sizeof (*entry
);
764 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
768 /* Go find the symbol, so that we can find it's name. */
769 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
770 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
772 bfd_release (input_bfd
, entry
);
776 if (entry
->isym
.st_shndx
!= SHN_UNDEF
777 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
781 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
782 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
784 /* We can still bfd_release here as nothing has done another
785 bfd_alloc. We can't do this later in this function. */
786 bfd_release (input_bfd
, entry
);
791 name
= (bfd_elf_string_from_elf_section
792 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
793 entry
->isym
.st_name
));
795 dynstr
= elf_hash_table (info
)->dynstr
;
798 /* Create a strtab to hold the dynamic symbol names. */
799 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
804 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
805 if (dynstr_index
== (size_t) -1)
807 entry
->isym
.st_name
= dynstr_index
;
809 eht
= elf_hash_table (info
);
811 entry
->next
= eht
->dynlocal
;
812 eht
->dynlocal
= entry
;
813 entry
->input_bfd
= input_bfd
;
814 entry
->input_indx
= input_indx
;
817 /* Whatever binding the symbol had before, it's now local. */
819 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
821 /* The dynindx will be set at the end of size_dynamic_sections. */
826 /* Return the dynindex of a local dynamic symbol. */
829 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
833 struct elf_link_local_dynamic_entry
*e
;
835 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
836 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
841 /* This function is used to renumber the dynamic symbols, if some of
842 them are removed because they are marked as local. This is called
843 via elf_link_hash_traverse. */
846 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
849 size_t *count
= (size_t *) data
;
854 if (h
->dynindx
!= -1)
855 h
->dynindx
= ++(*count
);
861 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
862 STB_LOCAL binding. */
865 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
868 size_t *count
= (size_t *) data
;
870 if (!h
->forced_local
)
873 if (h
->dynindx
!= -1)
874 h
->dynindx
= ++(*count
);
879 /* Return true if the dynamic symbol for a given section should be
880 omitted when creating a shared library. */
882 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
883 struct bfd_link_info
*info
,
886 struct elf_link_hash_table
*htab
;
889 switch (elf_section_data (p
)->this_hdr
.sh_type
)
893 /* If sh_type is yet undecided, assume it could be
894 SHT_PROGBITS/SHT_NOBITS. */
896 htab
= elf_hash_table (info
);
897 if (p
== htab
->tls_sec
)
900 if (htab
->text_index_section
!= NULL
)
901 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
903 return (htab
->dynobj
!= NULL
904 && (ip
= bfd_get_linker_section (htab
->dynobj
, p
->name
)) != NULL
905 && ip
->output_section
== p
);
907 /* There shouldn't be section relative relocations
908 against any other section. */
914 /* Assign dynsym indices. In a shared library we generate a section
915 symbol for each output section, which come first. Next come symbols
916 which have been forced to local binding. Then all of the back-end
917 allocated local dynamic syms, followed by the rest of the global
921 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
922 struct bfd_link_info
*info
,
923 unsigned long *section_sym_count
)
925 unsigned long dynsymcount
= 0;
927 if (bfd_link_pic (info
)
928 || elf_hash_table (info
)->is_relocatable_executable
)
930 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
932 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
933 if ((p
->flags
& SEC_EXCLUDE
) == 0
934 && (p
->flags
& SEC_ALLOC
) != 0
935 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
936 elf_section_data (p
)->dynindx
= ++dynsymcount
;
938 elf_section_data (p
)->dynindx
= 0;
940 *section_sym_count
= dynsymcount
;
942 elf_link_hash_traverse (elf_hash_table (info
),
943 elf_link_renumber_local_hash_table_dynsyms
,
946 if (elf_hash_table (info
)->dynlocal
)
948 struct elf_link_local_dynamic_entry
*p
;
949 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
950 p
->dynindx
= ++dynsymcount
;
952 elf_hash_table (info
)->local_dynsymcount
= dynsymcount
;
954 elf_link_hash_traverse (elf_hash_table (info
),
955 elf_link_renumber_hash_table_dynsyms
,
958 /* There is an unused NULL entry at the head of the table which we
959 must account for in our count even if the table is empty since it
960 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
964 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
968 /* Merge st_other field. */
971 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
972 const Elf_Internal_Sym
*isym
, asection
*sec
,
973 bfd_boolean definition
, bfd_boolean dynamic
)
975 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
977 /* If st_other has a processor-specific meaning, specific
978 code might be needed here. */
979 if (bed
->elf_backend_merge_symbol_attribute
)
980 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
985 unsigned symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
986 unsigned hvis
= ELF_ST_VISIBILITY (h
->other
);
988 /* Keep the most constraining visibility. Leave the remainder
989 of the st_other field to elf_backend_merge_symbol_attribute. */
990 if (symvis
- 1 < hvis
- 1)
991 h
->other
= symvis
| (h
->other
& ~ELF_ST_VISIBILITY (-1));
994 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_DEFAULT
995 && (sec
->flags
& SEC_READONLY
) == 0)
996 h
->protected_def
= 1;
999 /* This function is called when we want to merge a new symbol with an
1000 existing symbol. It handles the various cases which arise when we
1001 find a definition in a dynamic object, or when there is already a
1002 definition in a dynamic object. The new symbol is described by
1003 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1004 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1005 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1006 of an old common symbol. We set OVERRIDE if the old symbol is
1007 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1008 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1009 to change. By OK to change, we mean that we shouldn't warn if the
1010 type or size does change. */
1013 _bfd_elf_merge_symbol (bfd
*abfd
,
1014 struct bfd_link_info
*info
,
1016 Elf_Internal_Sym
*sym
,
1019 struct elf_link_hash_entry
**sym_hash
,
1021 bfd_boolean
*pold_weak
,
1022 unsigned int *pold_alignment
,
1024 bfd_boolean
*override
,
1025 bfd_boolean
*type_change_ok
,
1026 bfd_boolean
*size_change_ok
,
1027 bfd_boolean
*matched
)
1029 asection
*sec
, *oldsec
;
1030 struct elf_link_hash_entry
*h
;
1031 struct elf_link_hash_entry
*hi
;
1032 struct elf_link_hash_entry
*flip
;
1035 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
1036 bfd_boolean newweak
, oldweak
, newfunc
, oldfunc
;
1037 const struct elf_backend_data
*bed
;
1044 bind
= ELF_ST_BIND (sym
->st_info
);
1046 if (! bfd_is_und_section (sec
))
1047 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
1049 h
= ((struct elf_link_hash_entry
*)
1050 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
1055 bed
= get_elf_backend_data (abfd
);
1057 /* NEW_VERSION is the symbol version of the new symbol. */
1058 if (h
->versioned
!= unversioned
)
1060 /* Symbol version is unknown or versioned. */
1061 new_version
= strrchr (name
, ELF_VER_CHR
);
1064 if (h
->versioned
== unknown
)
1066 if (new_version
> name
&& new_version
[-1] != ELF_VER_CHR
)
1067 h
->versioned
= versioned_hidden
;
1069 h
->versioned
= versioned
;
1072 if (new_version
[0] == '\0')
1076 h
->versioned
= unversioned
;
1081 /* For merging, we only care about real symbols. But we need to make
1082 sure that indirect symbol dynamic flags are updated. */
1084 while (h
->root
.type
== bfd_link_hash_indirect
1085 || h
->root
.type
== bfd_link_hash_warning
)
1086 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1090 if (hi
== h
|| h
->root
.type
== bfd_link_hash_new
)
1094 /* OLD_HIDDEN is true if the existing symbol is only visible
1095 to the symbol with the same symbol version. NEW_HIDDEN is
1096 true if the new symbol is only visible to the symbol with
1097 the same symbol version. */
1098 bfd_boolean old_hidden
= h
->versioned
== versioned_hidden
;
1099 bfd_boolean new_hidden
= hi
->versioned
== versioned_hidden
;
1100 if (!old_hidden
&& !new_hidden
)
1101 /* The new symbol matches the existing symbol if both
1106 /* OLD_VERSION is the symbol version of the existing
1110 if (h
->versioned
>= versioned
)
1111 old_version
= strrchr (h
->root
.root
.string
,
1116 /* The new symbol matches the existing symbol if they
1117 have the same symbol version. */
1118 *matched
= (old_version
== new_version
1119 || (old_version
!= NULL
1120 && new_version
!= NULL
1121 && strcmp (old_version
, new_version
) == 0));
1126 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1131 switch (h
->root
.type
)
1136 case bfd_link_hash_undefined
:
1137 case bfd_link_hash_undefweak
:
1138 oldbfd
= h
->root
.u
.undef
.abfd
;
1141 case bfd_link_hash_defined
:
1142 case bfd_link_hash_defweak
:
1143 oldbfd
= h
->root
.u
.def
.section
->owner
;
1144 oldsec
= h
->root
.u
.def
.section
;
1147 case bfd_link_hash_common
:
1148 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1149 oldsec
= h
->root
.u
.c
.p
->section
;
1151 *pold_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1154 if (poldbfd
&& *poldbfd
== NULL
)
1157 /* Differentiate strong and weak symbols. */
1158 newweak
= bind
== STB_WEAK
;
1159 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1160 || h
->root
.type
== bfd_link_hash_undefweak
);
1162 *pold_weak
= oldweak
;
1164 /* This code is for coping with dynamic objects, and is only useful
1165 if we are doing an ELF link. */
1166 if (!(*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
1169 /* We have to check it for every instance since the first few may be
1170 references and not all compilers emit symbol type for undefined
1172 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1174 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1175 respectively, is from a dynamic object. */
1177 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1179 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1180 syms and defined syms in dynamic libraries respectively.
1181 ref_dynamic on the other hand can be set for a symbol defined in
1182 a dynamic library, and def_dynamic may not be set; When the
1183 definition in a dynamic lib is overridden by a definition in the
1184 executable use of the symbol in the dynamic lib becomes a
1185 reference to the executable symbol. */
1188 if (bfd_is_und_section (sec
))
1190 if (bind
!= STB_WEAK
)
1192 h
->ref_dynamic_nonweak
= 1;
1193 hi
->ref_dynamic_nonweak
= 1;
1198 /* Update the existing symbol only if they match. */
1201 hi
->dynamic_def
= 1;
1205 /* If we just created the symbol, mark it as being an ELF symbol.
1206 Other than that, there is nothing to do--there is no merge issue
1207 with a newly defined symbol--so we just return. */
1209 if (h
->root
.type
== bfd_link_hash_new
)
1215 /* In cases involving weak versioned symbols, we may wind up trying
1216 to merge a symbol with itself. Catch that here, to avoid the
1217 confusion that results if we try to override a symbol with
1218 itself. The additional tests catch cases like
1219 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1220 dynamic object, which we do want to handle here. */
1222 && (newweak
|| oldweak
)
1223 && ((abfd
->flags
& DYNAMIC
) == 0
1224 || !h
->def_regular
))
1229 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1230 else if (oldsec
!= NULL
)
1232 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1233 indices used by MIPS ELF. */
1234 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1237 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1238 respectively, appear to be a definition rather than reference. */
1240 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1242 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1243 && h
->root
.type
!= bfd_link_hash_undefweak
1244 && h
->root
.type
!= bfd_link_hash_common
);
1246 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1247 respectively, appear to be a function. */
1249 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1250 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1252 oldfunc
= (h
->type
!= STT_NOTYPE
1253 && bed
->is_function_type (h
->type
));
1255 if (!(newfunc
&& oldfunc
)
1256 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1257 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1258 && h
->type
!= STT_NOTYPE
1259 && (newdef
|| bfd_is_com_section (sec
))
1260 && (olddef
|| h
->root
.type
== bfd_link_hash_common
))
1262 /* If creating a default indirect symbol ("foo" or "foo@") from
1263 a dynamic versioned definition ("foo@@") skip doing so if
1264 there is an existing regular definition with a different
1265 type. We don't want, for example, a "time" variable in the
1266 executable overriding a "time" function in a shared library. */
1274 /* When adding a symbol from a regular object file after we have
1275 created indirect symbols, undo the indirection and any
1282 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1283 h
->forced_local
= 0;
1287 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1289 h
->root
.type
= bfd_link_hash_undefined
;
1290 h
->root
.u
.undef
.abfd
= abfd
;
1294 h
->root
.type
= bfd_link_hash_new
;
1295 h
->root
.u
.undef
.abfd
= NULL
;
1301 /* Check TLS symbols. We don't check undefined symbols introduced
1302 by "ld -u" which have no type (and oldbfd NULL), and we don't
1303 check symbols from plugins because they also have no type. */
1305 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1306 && (abfd
->flags
& BFD_PLUGIN
) == 0
1307 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1308 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1311 bfd_boolean ntdef
, tdef
;
1312 asection
*ntsec
, *tsec
;
1314 if (h
->type
== STT_TLS
)
1335 /* xgettext:c-format */
1336 (_("%s: TLS definition in %B section %A "
1337 "mismatches non-TLS definition in %B section %A"),
1338 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
, ntsec
);
1339 else if (!tdef
&& !ntdef
)
1341 /* xgettext:c-format */
1342 (_("%s: TLS reference in %B "
1343 "mismatches non-TLS reference in %B"),
1344 h
->root
.root
.string
, tbfd
, ntbfd
);
1347 /* xgettext:c-format */
1348 (_("%s: TLS definition in %B section %A "
1349 "mismatches non-TLS reference in %B"),
1350 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
);
1353 /* xgettext:c-format */
1354 (_("%s: TLS reference in %B "
1355 "mismatches non-TLS definition in %B section %A"),
1356 h
->root
.root
.string
, tbfd
, ntbfd
, ntsec
);
1358 bfd_set_error (bfd_error_bad_value
);
1362 /* If the old symbol has non-default visibility, we ignore the new
1363 definition from a dynamic object. */
1365 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1366 && !bfd_is_und_section (sec
))
1369 /* Make sure this symbol is dynamic. */
1371 hi
->ref_dynamic
= 1;
1372 /* A protected symbol has external availability. Make sure it is
1373 recorded as dynamic.
1375 FIXME: Should we check type and size for protected symbol? */
1376 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1377 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1382 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1385 /* If the new symbol with non-default visibility comes from a
1386 relocatable file and the old definition comes from a dynamic
1387 object, we remove the old definition. */
1388 if (hi
->root
.type
== bfd_link_hash_indirect
)
1390 /* Handle the case where the old dynamic definition is
1391 default versioned. We need to copy the symbol info from
1392 the symbol with default version to the normal one if it
1393 was referenced before. */
1396 hi
->root
.type
= h
->root
.type
;
1397 h
->root
.type
= bfd_link_hash_indirect
;
1398 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1400 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1401 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1403 /* If the new symbol is hidden or internal, completely undo
1404 any dynamic link state. */
1405 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1406 h
->forced_local
= 0;
1413 /* FIXME: Should we check type and size for protected symbol? */
1423 /* If the old symbol was undefined before, then it will still be
1424 on the undefs list. If the new symbol is undefined or
1425 common, we can't make it bfd_link_hash_new here, because new
1426 undefined or common symbols will be added to the undefs list
1427 by _bfd_generic_link_add_one_symbol. Symbols may not be
1428 added twice to the undefs list. Also, if the new symbol is
1429 undefweak then we don't want to lose the strong undef. */
1430 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1432 h
->root
.type
= bfd_link_hash_undefined
;
1433 h
->root
.u
.undef
.abfd
= abfd
;
1437 h
->root
.type
= bfd_link_hash_new
;
1438 h
->root
.u
.undef
.abfd
= NULL
;
1441 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1443 /* If the new symbol is hidden or internal, completely undo
1444 any dynamic link state. */
1445 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1446 h
->forced_local
= 0;
1452 /* FIXME: Should we check type and size for protected symbol? */
1458 /* If a new weak symbol definition comes from a regular file and the
1459 old symbol comes from a dynamic library, we treat the new one as
1460 strong. Similarly, an old weak symbol definition from a regular
1461 file is treated as strong when the new symbol comes from a dynamic
1462 library. Further, an old weak symbol from a dynamic library is
1463 treated as strong if the new symbol is from a dynamic library.
1464 This reflects the way glibc's ld.so works.
1466 Do this before setting *type_change_ok or *size_change_ok so that
1467 we warn properly when dynamic library symbols are overridden. */
1469 if (newdef
&& !newdyn
&& olddyn
)
1471 if (olddef
&& newdyn
)
1474 /* Allow changes between different types of function symbol. */
1475 if (newfunc
&& oldfunc
)
1476 *type_change_ok
= TRUE
;
1478 /* It's OK to change the type if either the existing symbol or the
1479 new symbol is weak. A type change is also OK if the old symbol
1480 is undefined and the new symbol is defined. */
1485 && h
->root
.type
== bfd_link_hash_undefined
))
1486 *type_change_ok
= TRUE
;
1488 /* It's OK to change the size if either the existing symbol or the
1489 new symbol is weak, or if the old symbol is undefined. */
1492 || h
->root
.type
== bfd_link_hash_undefined
)
1493 *size_change_ok
= TRUE
;
1495 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1496 symbol, respectively, appears to be a common symbol in a dynamic
1497 object. If a symbol appears in an uninitialized section, and is
1498 not weak, and is not a function, then it may be a common symbol
1499 which was resolved when the dynamic object was created. We want
1500 to treat such symbols specially, because they raise special
1501 considerations when setting the symbol size: if the symbol
1502 appears as a common symbol in a regular object, and the size in
1503 the regular object is larger, we must make sure that we use the
1504 larger size. This problematic case can always be avoided in C,
1505 but it must be handled correctly when using Fortran shared
1508 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1509 likewise for OLDDYNCOMMON and OLDDEF.
1511 Note that this test is just a heuristic, and that it is quite
1512 possible to have an uninitialized symbol in a shared object which
1513 is really a definition, rather than a common symbol. This could
1514 lead to some minor confusion when the symbol really is a common
1515 symbol in some regular object. However, I think it will be
1521 && (sec
->flags
& SEC_ALLOC
) != 0
1522 && (sec
->flags
& SEC_LOAD
) == 0
1525 newdyncommon
= TRUE
;
1527 newdyncommon
= FALSE
;
1531 && h
->root
.type
== bfd_link_hash_defined
1533 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1534 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1537 olddyncommon
= TRUE
;
1539 olddyncommon
= FALSE
;
1541 /* We now know everything about the old and new symbols. We ask the
1542 backend to check if we can merge them. */
1543 if (bed
->merge_symbol
!= NULL
)
1545 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1550 /* If both the old and the new symbols look like common symbols in a
1551 dynamic object, set the size of the symbol to the larger of the
1556 && sym
->st_size
!= h
->size
)
1558 /* Since we think we have two common symbols, issue a multiple
1559 common warning if desired. Note that we only warn if the
1560 size is different. If the size is the same, we simply let
1561 the old symbol override the new one as normally happens with
1562 symbols defined in dynamic objects. */
1564 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1565 bfd_link_hash_common
, sym
->st_size
);
1566 if (sym
->st_size
> h
->size
)
1567 h
->size
= sym
->st_size
;
1569 *size_change_ok
= TRUE
;
1572 /* If we are looking at a dynamic object, and we have found a
1573 definition, we need to see if the symbol was already defined by
1574 some other object. If so, we want to use the existing
1575 definition, and we do not want to report a multiple symbol
1576 definition error; we do this by clobbering *PSEC to be
1577 bfd_und_section_ptr.
1579 We treat a common symbol as a definition if the symbol in the
1580 shared library is a function, since common symbols always
1581 represent variables; this can cause confusion in principle, but
1582 any such confusion would seem to indicate an erroneous program or
1583 shared library. We also permit a common symbol in a regular
1584 object to override a weak symbol in a shared object. */
1589 || (h
->root
.type
== bfd_link_hash_common
1590 && (newweak
|| newfunc
))))
1594 newdyncommon
= FALSE
;
1596 *psec
= sec
= bfd_und_section_ptr
;
1597 *size_change_ok
= TRUE
;
1599 /* If we get here when the old symbol is a common symbol, then
1600 we are explicitly letting it override a weak symbol or
1601 function in a dynamic object, and we don't want to warn about
1602 a type change. If the old symbol is a defined symbol, a type
1603 change warning may still be appropriate. */
1605 if (h
->root
.type
== bfd_link_hash_common
)
1606 *type_change_ok
= TRUE
;
1609 /* Handle the special case of an old common symbol merging with a
1610 new symbol which looks like a common symbol in a shared object.
1611 We change *PSEC and *PVALUE to make the new symbol look like a
1612 common symbol, and let _bfd_generic_link_add_one_symbol do the
1616 && h
->root
.type
== bfd_link_hash_common
)
1620 newdyncommon
= FALSE
;
1621 *pvalue
= sym
->st_size
;
1622 *psec
= sec
= bed
->common_section (oldsec
);
1623 *size_change_ok
= TRUE
;
1626 /* Skip weak definitions of symbols that are already defined. */
1627 if (newdef
&& olddef
&& newweak
)
1629 /* Don't skip new non-IR weak syms. */
1630 if (!(oldbfd
!= NULL
1631 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1632 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1638 /* Merge st_other. If the symbol already has a dynamic index,
1639 but visibility says it should not be visible, turn it into a
1641 elf_merge_st_other (abfd
, h
, sym
, sec
, newdef
, newdyn
);
1642 if (h
->dynindx
!= -1)
1643 switch (ELF_ST_VISIBILITY (h
->other
))
1647 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1652 /* If the old symbol is from a dynamic object, and the new symbol is
1653 a definition which is not from a dynamic object, then the new
1654 symbol overrides the old symbol. Symbols from regular files
1655 always take precedence over symbols from dynamic objects, even if
1656 they are defined after the dynamic object in the link.
1658 As above, we again permit a common symbol in a regular object to
1659 override a definition in a shared object if the shared object
1660 symbol is a function or is weak. */
1665 || (bfd_is_com_section (sec
)
1666 && (oldweak
|| oldfunc
)))
1671 /* Change the hash table entry to undefined, and let
1672 _bfd_generic_link_add_one_symbol do the right thing with the
1675 h
->root
.type
= bfd_link_hash_undefined
;
1676 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1677 *size_change_ok
= TRUE
;
1680 olddyncommon
= FALSE
;
1682 /* We again permit a type change when a common symbol may be
1683 overriding a function. */
1685 if (bfd_is_com_section (sec
))
1689 /* If a common symbol overrides a function, make sure
1690 that it isn't defined dynamically nor has type
1693 h
->type
= STT_NOTYPE
;
1695 *type_change_ok
= TRUE
;
1698 if (hi
->root
.type
== bfd_link_hash_indirect
)
1701 /* This union may have been set to be non-NULL when this symbol
1702 was seen in a dynamic object. We must force the union to be
1703 NULL, so that it is correct for a regular symbol. */
1704 h
->verinfo
.vertree
= NULL
;
1707 /* Handle the special case of a new common symbol merging with an
1708 old symbol that looks like it might be a common symbol defined in
1709 a shared object. Note that we have already handled the case in
1710 which a new common symbol should simply override the definition
1711 in the shared library. */
1714 && bfd_is_com_section (sec
)
1717 /* It would be best if we could set the hash table entry to a
1718 common symbol, but we don't know what to use for the section
1719 or the alignment. */
1720 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1721 bfd_link_hash_common
, sym
->st_size
);
1723 /* If the presumed common symbol in the dynamic object is
1724 larger, pretend that the new symbol has its size. */
1726 if (h
->size
> *pvalue
)
1729 /* We need to remember the alignment required by the symbol
1730 in the dynamic object. */
1731 BFD_ASSERT (pold_alignment
);
1732 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1735 olddyncommon
= FALSE
;
1737 h
->root
.type
= bfd_link_hash_undefined
;
1738 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1740 *size_change_ok
= TRUE
;
1741 *type_change_ok
= TRUE
;
1743 if (hi
->root
.type
== bfd_link_hash_indirect
)
1746 h
->verinfo
.vertree
= NULL
;
1751 /* Handle the case where we had a versioned symbol in a dynamic
1752 library and now find a definition in a normal object. In this
1753 case, we make the versioned symbol point to the normal one. */
1754 flip
->root
.type
= h
->root
.type
;
1755 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1756 h
->root
.type
= bfd_link_hash_indirect
;
1757 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1758 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1762 flip
->ref_dynamic
= 1;
1769 /* This function is called to create an indirect symbol from the
1770 default for the symbol with the default version if needed. The
1771 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1772 set DYNSYM if the new indirect symbol is dynamic. */
1775 _bfd_elf_add_default_symbol (bfd
*abfd
,
1776 struct bfd_link_info
*info
,
1777 struct elf_link_hash_entry
*h
,
1779 Elf_Internal_Sym
*sym
,
1783 bfd_boolean
*dynsym
)
1785 bfd_boolean type_change_ok
;
1786 bfd_boolean size_change_ok
;
1789 struct elf_link_hash_entry
*hi
;
1790 struct bfd_link_hash_entry
*bh
;
1791 const struct elf_backend_data
*bed
;
1792 bfd_boolean collect
;
1793 bfd_boolean dynamic
;
1794 bfd_boolean override
;
1796 size_t len
, shortlen
;
1798 bfd_boolean matched
;
1800 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1803 /* If this symbol has a version, and it is the default version, we
1804 create an indirect symbol from the default name to the fully
1805 decorated name. This will cause external references which do not
1806 specify a version to be bound to this version of the symbol. */
1807 p
= strchr (name
, ELF_VER_CHR
);
1808 if (h
->versioned
== unknown
)
1812 h
->versioned
= unversioned
;
1817 if (p
[1] != ELF_VER_CHR
)
1819 h
->versioned
= versioned_hidden
;
1823 h
->versioned
= versioned
;
1828 /* PR ld/19073: We may see an unversioned definition after the
1834 bed
= get_elf_backend_data (abfd
);
1835 collect
= bed
->collect
;
1836 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1838 shortlen
= p
- name
;
1839 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1840 if (shortname
== NULL
)
1842 memcpy (shortname
, name
, shortlen
);
1843 shortname
[shortlen
] = '\0';
1845 /* We are going to create a new symbol. Merge it with any existing
1846 symbol with this name. For the purposes of the merge, act as
1847 though we were defining the symbol we just defined, although we
1848 actually going to define an indirect symbol. */
1849 type_change_ok
= FALSE
;
1850 size_change_ok
= FALSE
;
1853 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1854 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1855 &type_change_ok
, &size_change_ok
, &matched
))
1861 if (hi
->def_regular
)
1863 /* If the undecorated symbol will have a version added by a
1864 script different to H, then don't indirect to/from the
1865 undecorated symbol. This isn't ideal because we may not yet
1866 have seen symbol versions, if given by a script on the
1867 command line rather than via --version-script. */
1868 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
1873 = bfd_find_version_for_sym (info
->version_info
,
1874 hi
->root
.root
.string
, &hide
);
1875 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
1877 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
1881 if (hi
->verinfo
.vertree
!= NULL
1882 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
1888 /* Add the default symbol if not performing a relocatable link. */
1889 if (! bfd_link_relocatable (info
))
1892 if (! (_bfd_generic_link_add_one_symbol
1893 (info
, abfd
, shortname
, BSF_INDIRECT
,
1894 bfd_ind_section_ptr
,
1895 0, name
, FALSE
, collect
, &bh
)))
1897 hi
= (struct elf_link_hash_entry
*) bh
;
1902 /* In this case the symbol named SHORTNAME is overriding the
1903 indirect symbol we want to add. We were planning on making
1904 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1905 is the name without a version. NAME is the fully versioned
1906 name, and it is the default version.
1908 Overriding means that we already saw a definition for the
1909 symbol SHORTNAME in a regular object, and it is overriding
1910 the symbol defined in the dynamic object.
1912 When this happens, we actually want to change NAME, the
1913 symbol we just added, to refer to SHORTNAME. This will cause
1914 references to NAME in the shared object to become references
1915 to SHORTNAME in the regular object. This is what we expect
1916 when we override a function in a shared object: that the
1917 references in the shared object will be mapped to the
1918 definition in the regular object. */
1920 while (hi
->root
.type
== bfd_link_hash_indirect
1921 || hi
->root
.type
== bfd_link_hash_warning
)
1922 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1924 h
->root
.type
= bfd_link_hash_indirect
;
1925 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1929 hi
->ref_dynamic
= 1;
1933 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1938 /* Now set HI to H, so that the following code will set the
1939 other fields correctly. */
1943 /* Check if HI is a warning symbol. */
1944 if (hi
->root
.type
== bfd_link_hash_warning
)
1945 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1947 /* If there is a duplicate definition somewhere, then HI may not
1948 point to an indirect symbol. We will have reported an error to
1949 the user in that case. */
1951 if (hi
->root
.type
== bfd_link_hash_indirect
)
1953 struct elf_link_hash_entry
*ht
;
1955 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1956 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
1958 /* A reference to the SHORTNAME symbol from a dynamic library
1959 will be satisfied by the versioned symbol at runtime. In
1960 effect, we have a reference to the versioned symbol. */
1961 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
1962 hi
->dynamic_def
|= ht
->dynamic_def
;
1964 /* See if the new flags lead us to realize that the symbol must
1970 if (! bfd_link_executable (info
)
1977 if (hi
->ref_regular
)
1983 /* We also need to define an indirection from the nondefault version
1987 len
= strlen (name
);
1988 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
1989 if (shortname
== NULL
)
1991 memcpy (shortname
, name
, shortlen
);
1992 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1994 /* Once again, merge with any existing symbol. */
1995 type_change_ok
= FALSE
;
1996 size_change_ok
= FALSE
;
1998 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1999 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
2000 &type_change_ok
, &size_change_ok
, &matched
))
2008 /* Here SHORTNAME is a versioned name, so we don't expect to see
2009 the type of override we do in the case above unless it is
2010 overridden by a versioned definition. */
2011 if (hi
->root
.type
!= bfd_link_hash_defined
2012 && hi
->root
.type
!= bfd_link_hash_defweak
)
2014 /* xgettext:c-format */
2015 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
2021 if (! (_bfd_generic_link_add_one_symbol
2022 (info
, abfd
, shortname
, BSF_INDIRECT
,
2023 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
2025 hi
= (struct elf_link_hash_entry
*) bh
;
2027 /* If there is a duplicate definition somewhere, then HI may not
2028 point to an indirect symbol. We will have reported an error
2029 to the user in that case. */
2031 if (hi
->root
.type
== bfd_link_hash_indirect
)
2033 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
2034 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2035 hi
->dynamic_def
|= h
->dynamic_def
;
2037 /* See if the new flags lead us to realize that the symbol
2043 if (! bfd_link_executable (info
)
2049 if (hi
->ref_regular
)
2059 /* This routine is used to export all defined symbols into the dynamic
2060 symbol table. It is called via elf_link_hash_traverse. */
2063 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
2065 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2067 /* Ignore indirect symbols. These are added by the versioning code. */
2068 if (h
->root
.type
== bfd_link_hash_indirect
)
2071 /* Ignore this if we won't export it. */
2072 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2075 if (h
->dynindx
== -1
2076 && (h
->def_regular
|| h
->ref_regular
)
2077 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2078 h
->root
.root
.string
))
2080 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2090 /* Look through the symbols which are defined in other shared
2091 libraries and referenced here. Update the list of version
2092 dependencies. This will be put into the .gnu.version_r section.
2093 This function is called via elf_link_hash_traverse. */
2096 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2099 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2100 Elf_Internal_Verneed
*t
;
2101 Elf_Internal_Vernaux
*a
;
2104 /* We only care about symbols defined in shared objects with version
2109 || h
->verinfo
.verdef
== NULL
2110 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2111 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2114 /* See if we already know about this version. */
2115 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2119 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2122 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2123 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2129 /* This is a new version. Add it to tree we are building. */
2134 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2137 rinfo
->failed
= TRUE
;
2141 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2142 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2143 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2147 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2150 rinfo
->failed
= TRUE
;
2154 /* Note that we are copying a string pointer here, and testing it
2155 above. If bfd_elf_string_from_elf_section is ever changed to
2156 discard the string data when low in memory, this will have to be
2158 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2160 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2161 a
->vna_nextptr
= t
->vn_auxptr
;
2163 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2166 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2173 /* Figure out appropriate versions for all the symbols. We may not
2174 have the version number script until we have read all of the input
2175 files, so until that point we don't know which symbols should be
2176 local. This function is called via elf_link_hash_traverse. */
2179 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2181 struct elf_info_failed
*sinfo
;
2182 struct bfd_link_info
*info
;
2183 const struct elf_backend_data
*bed
;
2184 struct elf_info_failed eif
;
2187 sinfo
= (struct elf_info_failed
*) data
;
2190 /* Fix the symbol flags. */
2193 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2196 sinfo
->failed
= TRUE
;
2200 /* We only need version numbers for symbols defined in regular
2202 if (!h
->def_regular
)
2205 bed
= get_elf_backend_data (info
->output_bfd
);
2206 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2207 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2209 struct bfd_elf_version_tree
*t
;
2212 if (*p
== ELF_VER_CHR
)
2215 /* If there is no version string, we can just return out. */
2219 /* Look for the version. If we find it, it is no longer weak. */
2220 for (t
= sinfo
->info
->version_info
; t
!= NULL
; t
= t
->next
)
2222 if (strcmp (t
->name
, p
) == 0)
2226 struct bfd_elf_version_expr
*d
;
2228 len
= p
- h
->root
.root
.string
;
2229 alc
= (char *) bfd_malloc (len
);
2232 sinfo
->failed
= TRUE
;
2235 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2236 alc
[len
- 1] = '\0';
2237 if (alc
[len
- 2] == ELF_VER_CHR
)
2238 alc
[len
- 2] = '\0';
2240 h
->verinfo
.vertree
= t
;
2244 if (t
->globals
.list
!= NULL
)
2245 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2247 /* See if there is anything to force this symbol to
2249 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2251 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2254 && ! info
->export_dynamic
)
2255 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2263 /* If we are building an application, we need to create a
2264 version node for this version. */
2265 if (t
== NULL
&& bfd_link_executable (info
))
2267 struct bfd_elf_version_tree
**pp
;
2270 /* If we aren't going to export this symbol, we don't need
2271 to worry about it. */
2272 if (h
->dynindx
== -1)
2275 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2279 sinfo
->failed
= TRUE
;
2284 t
->name_indx
= (unsigned int) -1;
2288 /* Don't count anonymous version tag. */
2289 if (sinfo
->info
->version_info
!= NULL
2290 && sinfo
->info
->version_info
->vernum
== 0)
2292 for (pp
= &sinfo
->info
->version_info
;
2296 t
->vernum
= version_index
;
2300 h
->verinfo
.vertree
= t
;
2304 /* We could not find the version for a symbol when
2305 generating a shared archive. Return an error. */
2307 /* xgettext:c-format */
2308 (_("%B: version node not found for symbol %s"),
2309 info
->output_bfd
, h
->root
.root
.string
);
2310 bfd_set_error (bfd_error_bad_value
);
2311 sinfo
->failed
= TRUE
;
2316 /* If we don't have a version for this symbol, see if we can find
2318 if (h
->verinfo
.vertree
== NULL
&& sinfo
->info
->version_info
!= NULL
)
2323 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2324 h
->root
.root
.string
, &hide
);
2325 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2326 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2332 /* Read and swap the relocs from the section indicated by SHDR. This
2333 may be either a REL or a RELA section. The relocations are
2334 translated into RELA relocations and stored in INTERNAL_RELOCS,
2335 which should have already been allocated to contain enough space.
2336 The EXTERNAL_RELOCS are a buffer where the external form of the
2337 relocations should be stored.
2339 Returns FALSE if something goes wrong. */
2342 elf_link_read_relocs_from_section (bfd
*abfd
,
2344 Elf_Internal_Shdr
*shdr
,
2345 void *external_relocs
,
2346 Elf_Internal_Rela
*internal_relocs
)
2348 const struct elf_backend_data
*bed
;
2349 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2350 const bfd_byte
*erela
;
2351 const bfd_byte
*erelaend
;
2352 Elf_Internal_Rela
*irela
;
2353 Elf_Internal_Shdr
*symtab_hdr
;
2356 /* Position ourselves at the start of the section. */
2357 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2360 /* Read the relocations. */
2361 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2364 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2365 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2367 bed
= get_elf_backend_data (abfd
);
2369 /* Convert the external relocations to the internal format. */
2370 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2371 swap_in
= bed
->s
->swap_reloc_in
;
2372 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2373 swap_in
= bed
->s
->swap_reloca_in
;
2376 bfd_set_error (bfd_error_wrong_format
);
2380 erela
= (const bfd_byte
*) external_relocs
;
2381 erelaend
= erela
+ shdr
->sh_size
;
2382 irela
= internal_relocs
;
2383 while (erela
< erelaend
)
2387 (*swap_in
) (abfd
, erela
, irela
);
2388 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2389 if (bed
->s
->arch_size
== 64)
2393 if ((size_t) r_symndx
>= nsyms
)
2396 /* xgettext:c-format */
2397 (_("%B: bad reloc symbol index (%#Lx >= %#lx)"
2398 " for offset %#Lx in section `%A'"),
2399 abfd
, r_symndx
, (unsigned long) nsyms
,
2400 irela
->r_offset
, sec
);
2401 bfd_set_error (bfd_error_bad_value
);
2405 else if (r_symndx
!= STN_UNDEF
)
2408 /* xgettext:c-format */
2409 (_("%B: non-zero symbol index (%#Lx)"
2410 " for offset %#Lx in section `%A'"
2411 " when the object file has no symbol table"),
2413 irela
->r_offset
, sec
);
2414 bfd_set_error (bfd_error_bad_value
);
2417 irela
+= bed
->s
->int_rels_per_ext_rel
;
2418 erela
+= shdr
->sh_entsize
;
2424 /* Read and swap the relocs for a section O. They may have been
2425 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2426 not NULL, they are used as buffers to read into. They are known to
2427 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2428 the return value is allocated using either malloc or bfd_alloc,
2429 according to the KEEP_MEMORY argument. If O has two relocation
2430 sections (both REL and RELA relocations), then the REL_HDR
2431 relocations will appear first in INTERNAL_RELOCS, followed by the
2432 RELA_HDR relocations. */
2435 _bfd_elf_link_read_relocs (bfd
*abfd
,
2437 void *external_relocs
,
2438 Elf_Internal_Rela
*internal_relocs
,
2439 bfd_boolean keep_memory
)
2441 void *alloc1
= NULL
;
2442 Elf_Internal_Rela
*alloc2
= NULL
;
2443 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2444 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2445 Elf_Internal_Rela
*internal_rela_relocs
;
2447 if (esdo
->relocs
!= NULL
)
2448 return esdo
->relocs
;
2450 if (o
->reloc_count
== 0)
2453 if (internal_relocs
== NULL
)
2457 size
= (bfd_size_type
) o
->reloc_count
* sizeof (Elf_Internal_Rela
);
2459 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2461 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2462 if (internal_relocs
== NULL
)
2466 if (external_relocs
== NULL
)
2468 bfd_size_type size
= 0;
2471 size
+= esdo
->rel
.hdr
->sh_size
;
2473 size
+= esdo
->rela
.hdr
->sh_size
;
2475 alloc1
= bfd_malloc (size
);
2478 external_relocs
= alloc1
;
2481 internal_rela_relocs
= internal_relocs
;
2484 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2488 external_relocs
= (((bfd_byte
*) external_relocs
)
2489 + esdo
->rel
.hdr
->sh_size
);
2490 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2491 * bed
->s
->int_rels_per_ext_rel
);
2495 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2497 internal_rela_relocs
)))
2500 /* Cache the results for next time, if we can. */
2502 esdo
->relocs
= internal_relocs
;
2507 /* Don't free alloc2, since if it was allocated we are passing it
2508 back (under the name of internal_relocs). */
2510 return internal_relocs
;
2518 bfd_release (abfd
, alloc2
);
2525 /* Compute the size of, and allocate space for, REL_HDR which is the
2526 section header for a section containing relocations for O. */
2529 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2530 struct bfd_elf_section_reloc_data
*reldata
)
2532 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2534 /* That allows us to calculate the size of the section. */
2535 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2537 /* The contents field must last into write_object_contents, so we
2538 allocate it with bfd_alloc rather than malloc. Also since we
2539 cannot be sure that the contents will actually be filled in,
2540 we zero the allocated space. */
2541 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2542 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2545 if (reldata
->hashes
== NULL
&& reldata
->count
)
2547 struct elf_link_hash_entry
**p
;
2549 p
= ((struct elf_link_hash_entry
**)
2550 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2554 reldata
->hashes
= p
;
2560 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2561 originated from the section given by INPUT_REL_HDR) to the
2565 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2566 asection
*input_section
,
2567 Elf_Internal_Shdr
*input_rel_hdr
,
2568 Elf_Internal_Rela
*internal_relocs
,
2569 struct elf_link_hash_entry
**rel_hash
2572 Elf_Internal_Rela
*irela
;
2573 Elf_Internal_Rela
*irelaend
;
2575 struct bfd_elf_section_reloc_data
*output_reldata
;
2576 asection
*output_section
;
2577 const struct elf_backend_data
*bed
;
2578 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2579 struct bfd_elf_section_data
*esdo
;
2581 output_section
= input_section
->output_section
;
2583 bed
= get_elf_backend_data (output_bfd
);
2584 esdo
= elf_section_data (output_section
);
2585 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2587 output_reldata
= &esdo
->rel
;
2588 swap_out
= bed
->s
->swap_reloc_out
;
2590 else if (esdo
->rela
.hdr
2591 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2593 output_reldata
= &esdo
->rela
;
2594 swap_out
= bed
->s
->swap_reloca_out
;
2599 /* xgettext:c-format */
2600 (_("%B: relocation size mismatch in %B section %A"),
2601 output_bfd
, input_section
->owner
, input_section
);
2602 bfd_set_error (bfd_error_wrong_format
);
2606 erel
= output_reldata
->hdr
->contents
;
2607 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2608 irela
= internal_relocs
;
2609 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2610 * bed
->s
->int_rels_per_ext_rel
);
2611 while (irela
< irelaend
)
2613 (*swap_out
) (output_bfd
, irela
, erel
);
2614 irela
+= bed
->s
->int_rels_per_ext_rel
;
2615 erel
+= input_rel_hdr
->sh_entsize
;
2618 /* Bump the counter, so that we know where to add the next set of
2620 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2625 /* Make weak undefined symbols in PIE dynamic. */
2628 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2629 struct elf_link_hash_entry
*h
)
2631 if (bfd_link_pie (info
)
2633 && h
->root
.type
== bfd_link_hash_undefweak
)
2634 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2639 /* Fix up the flags for a symbol. This handles various cases which
2640 can only be fixed after all the input files are seen. This is
2641 currently called by both adjust_dynamic_symbol and
2642 assign_sym_version, which is unnecessary but perhaps more robust in
2643 the face of future changes. */
2646 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2647 struct elf_info_failed
*eif
)
2649 const struct elf_backend_data
*bed
;
2651 /* If this symbol was mentioned in a non-ELF file, try to set
2652 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2653 permit a non-ELF file to correctly refer to a symbol defined in
2654 an ELF dynamic object. */
2657 while (h
->root
.type
== bfd_link_hash_indirect
)
2658 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2660 if (h
->root
.type
!= bfd_link_hash_defined
2661 && h
->root
.type
!= bfd_link_hash_defweak
)
2664 h
->ref_regular_nonweak
= 1;
2668 if (h
->root
.u
.def
.section
->owner
!= NULL
2669 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2670 == bfd_target_elf_flavour
))
2673 h
->ref_regular_nonweak
= 1;
2679 if (h
->dynindx
== -1
2683 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2692 /* Unfortunately, NON_ELF is only correct if the symbol
2693 was first seen in a non-ELF file. Fortunately, if the symbol
2694 was first seen in an ELF file, we're probably OK unless the
2695 symbol was defined in a non-ELF file. Catch that case here.
2696 FIXME: We're still in trouble if the symbol was first seen in
2697 a dynamic object, and then later in a non-ELF regular object. */
2698 if ((h
->root
.type
== bfd_link_hash_defined
2699 || h
->root
.type
== bfd_link_hash_defweak
)
2701 && (h
->root
.u
.def
.section
->owner
!= NULL
2702 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2703 != bfd_target_elf_flavour
)
2704 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2705 && !h
->def_dynamic
)))
2709 /* Backend specific symbol fixup. */
2710 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2711 if (bed
->elf_backend_fixup_symbol
2712 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2715 /* If this is a final link, and the symbol was defined as a common
2716 symbol in a regular object file, and there was no definition in
2717 any dynamic object, then the linker will have allocated space for
2718 the symbol in a common section but the DEF_REGULAR
2719 flag will not have been set. */
2720 if (h
->root
.type
== bfd_link_hash_defined
2724 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
2727 /* If a weak undefined symbol has non-default visibility, we also
2728 hide it from the dynamic linker. */
2729 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2730 && h
->root
.type
== bfd_link_hash_undefweak
)
2731 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2733 /* A hidden versioned symbol in executable should be forced local if
2734 it is is locally defined, not referenced by shared library and not
2736 else if (bfd_link_executable (eif
->info
)
2737 && h
->versioned
== versioned_hidden
2738 && !eif
->info
->export_dynamic
2742 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2744 /* If -Bsymbolic was used (which means to bind references to global
2745 symbols to the definition within the shared object), and this
2746 symbol was defined in a regular object, then it actually doesn't
2747 need a PLT entry. Likewise, if the symbol has non-default
2748 visibility. If the symbol has hidden or internal visibility, we
2749 will force it local. */
2750 else if (h
->needs_plt
2751 && bfd_link_pic (eif
->info
)
2752 && is_elf_hash_table (eif
->info
->hash
)
2753 && (SYMBOLIC_BIND (eif
->info
, h
)
2754 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2757 bfd_boolean force_local
;
2759 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2760 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2761 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2764 /* If this is a weak defined symbol in a dynamic object, and we know
2765 the real definition in the dynamic object, copy interesting flags
2766 over to the real definition. */
2767 if (h
->u
.weakdef
!= NULL
)
2769 /* If the real definition is defined by a regular object file,
2770 don't do anything special. See the longer description in
2771 _bfd_elf_adjust_dynamic_symbol, below. */
2772 if (h
->u
.weakdef
->def_regular
)
2773 h
->u
.weakdef
= NULL
;
2776 struct elf_link_hash_entry
*weakdef
= h
->u
.weakdef
;
2778 while (h
->root
.type
== bfd_link_hash_indirect
)
2779 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2781 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2782 || h
->root
.type
== bfd_link_hash_defweak
);
2783 BFD_ASSERT (weakdef
->def_dynamic
);
2784 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2785 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2786 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
, h
);
2793 /* Make the backend pick a good value for a dynamic symbol. This is
2794 called via elf_link_hash_traverse, and also calls itself
2798 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2800 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2802 const struct elf_backend_data
*bed
;
2804 if (! is_elf_hash_table (eif
->info
->hash
))
2807 /* Ignore indirect symbols. These are added by the versioning code. */
2808 if (h
->root
.type
== bfd_link_hash_indirect
)
2811 /* Fix the symbol flags. */
2812 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2815 if (h
->root
.type
== bfd_link_hash_undefweak
)
2817 if (eif
->info
->dynamic_undefined_weak
== 0)
2818 _bfd_elf_link_hash_hide_symbol (eif
->info
, h
, TRUE
);
2819 else if (eif
->info
->dynamic_undefined_weak
> 0
2821 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2822 && !bfd_hide_sym_by_version (eif
->info
->version_info
,
2823 h
->root
.root
.string
))
2825 if (!bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2833 /* If this symbol does not require a PLT entry, and it is not
2834 defined by a dynamic object, or is not referenced by a regular
2835 object, ignore it. We do have to handle a weak defined symbol,
2836 even if no regular object refers to it, if we decided to add it
2837 to the dynamic symbol table. FIXME: Do we normally need to worry
2838 about symbols which are defined by one dynamic object and
2839 referenced by another one? */
2841 && h
->type
!= STT_GNU_IFUNC
2845 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2847 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2851 /* If we've already adjusted this symbol, don't do it again. This
2852 can happen via a recursive call. */
2853 if (h
->dynamic_adjusted
)
2856 /* Don't look at this symbol again. Note that we must set this
2857 after checking the above conditions, because we may look at a
2858 symbol once, decide not to do anything, and then get called
2859 recursively later after REF_REGULAR is set below. */
2860 h
->dynamic_adjusted
= 1;
2862 /* If this is a weak definition, and we know a real definition, and
2863 the real symbol is not itself defined by a regular object file,
2864 then get a good value for the real definition. We handle the
2865 real symbol first, for the convenience of the backend routine.
2867 Note that there is a confusing case here. If the real definition
2868 is defined by a regular object file, we don't get the real symbol
2869 from the dynamic object, but we do get the weak symbol. If the
2870 processor backend uses a COPY reloc, then if some routine in the
2871 dynamic object changes the real symbol, we will not see that
2872 change in the corresponding weak symbol. This is the way other
2873 ELF linkers work as well, and seems to be a result of the shared
2876 I will clarify this issue. Most SVR4 shared libraries define the
2877 variable _timezone and define timezone as a weak synonym. The
2878 tzset call changes _timezone. If you write
2879 extern int timezone;
2881 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2882 you might expect that, since timezone is a synonym for _timezone,
2883 the same number will print both times. However, if the processor
2884 backend uses a COPY reloc, then actually timezone will be copied
2885 into your process image, and, since you define _timezone
2886 yourself, _timezone will not. Thus timezone and _timezone will
2887 wind up at different memory locations. The tzset call will set
2888 _timezone, leaving timezone unchanged. */
2890 if (h
->u
.weakdef
!= NULL
)
2892 /* If we get to this point, there is an implicit reference to
2893 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2894 h
->u
.weakdef
->ref_regular
= 1;
2896 /* Ensure that the backend adjust_dynamic_symbol function sees
2897 H->U.WEAKDEF before H by recursively calling ourselves. */
2898 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2902 /* If a symbol has no type and no size and does not require a PLT
2903 entry, then we are probably about to do the wrong thing here: we
2904 are probably going to create a COPY reloc for an empty object.
2905 This case can arise when a shared object is built with assembly
2906 code, and the assembly code fails to set the symbol type. */
2908 && h
->type
== STT_NOTYPE
2911 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2912 h
->root
.root
.string
);
2914 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2915 bed
= get_elf_backend_data (dynobj
);
2917 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2926 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2930 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
2931 struct elf_link_hash_entry
*h
,
2934 unsigned int power_of_two
;
2936 asection
*sec
= h
->root
.u
.def
.section
;
2938 /* The section alignment of the definition is the maximum alignment
2939 requirement of symbols defined in the section. Since we don't
2940 know the symbol alignment requirement, we start with the
2941 maximum alignment and check low bits of the symbol address
2942 for the minimum alignment. */
2943 power_of_two
= bfd_get_section_alignment (sec
->owner
, sec
);
2944 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
2945 while ((h
->root
.u
.def
.value
& mask
) != 0)
2951 if (power_of_two
> bfd_get_section_alignment (dynbss
->owner
,
2954 /* Adjust the section alignment if needed. */
2955 if (! bfd_set_section_alignment (dynbss
->owner
, dynbss
,
2960 /* We make sure that the symbol will be aligned properly. */
2961 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
2963 /* Define the symbol as being at this point in DYNBSS. */
2964 h
->root
.u
.def
.section
= dynbss
;
2965 h
->root
.u
.def
.value
= dynbss
->size
;
2967 /* Increment the size of DYNBSS to make room for the symbol. */
2968 dynbss
->size
+= h
->size
;
2970 /* No error if extern_protected_data is true. */
2971 if (h
->protected_def
2972 && (!info
->extern_protected_data
2973 || (info
->extern_protected_data
< 0
2974 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
2975 info
->callbacks
->einfo
2976 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2977 h
->root
.root
.string
);
2982 /* Adjust all external symbols pointing into SEC_MERGE sections
2983 to reflect the object merging within the sections. */
2986 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2990 if ((h
->root
.type
== bfd_link_hash_defined
2991 || h
->root
.type
== bfd_link_hash_defweak
)
2992 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2993 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
2995 bfd
*output_bfd
= (bfd
*) data
;
2997 h
->root
.u
.def
.value
=
2998 _bfd_merged_section_offset (output_bfd
,
2999 &h
->root
.u
.def
.section
,
3000 elf_section_data (sec
)->sec_info
,
3001 h
->root
.u
.def
.value
);
3007 /* Returns false if the symbol referred to by H should be considered
3008 to resolve local to the current module, and true if it should be
3009 considered to bind dynamically. */
3012 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
3013 struct bfd_link_info
*info
,
3014 bfd_boolean not_local_protected
)
3016 bfd_boolean binding_stays_local_p
;
3017 const struct elf_backend_data
*bed
;
3018 struct elf_link_hash_table
*hash_table
;
3023 while (h
->root
.type
== bfd_link_hash_indirect
3024 || h
->root
.type
== bfd_link_hash_warning
)
3025 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3027 /* If it was forced local, then clearly it's not dynamic. */
3028 if (h
->dynindx
== -1)
3030 if (h
->forced_local
)
3033 /* Identify the cases where name binding rules say that a
3034 visible symbol resolves locally. */
3035 binding_stays_local_p
= (bfd_link_executable (info
)
3036 || SYMBOLIC_BIND (info
, h
));
3038 switch (ELF_ST_VISIBILITY (h
->other
))
3045 hash_table
= elf_hash_table (info
);
3046 if (!is_elf_hash_table (hash_table
))
3049 bed
= get_elf_backend_data (hash_table
->dynobj
);
3051 /* Proper resolution for function pointer equality may require
3052 that these symbols perhaps be resolved dynamically, even though
3053 we should be resolving them to the current module. */
3054 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
3055 binding_stays_local_p
= TRUE
;
3062 /* If it isn't defined locally, then clearly it's dynamic. */
3063 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
3066 /* Otherwise, the symbol is dynamic if binding rules don't tell
3067 us that it remains local. */
3068 return !binding_stays_local_p
;
3071 /* Return true if the symbol referred to by H should be considered
3072 to resolve local to the current module, and false otherwise. Differs
3073 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3074 undefined symbols. The two functions are virtually identical except
3075 for the place where dynindx == -1 is tested. If that test is true,
3076 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3077 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3079 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3080 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3081 treatment of undefined weak symbols. For those that do not make
3082 undefined weak symbols dynamic, both functions may return false. */
3085 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3086 struct bfd_link_info
*info
,
3087 bfd_boolean local_protected
)
3089 const struct elf_backend_data
*bed
;
3090 struct elf_link_hash_table
*hash_table
;
3092 /* If it's a local sym, of course we resolve locally. */
3096 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3097 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3098 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3101 /* Forced local symbols resolve locally. */
3102 if (h
->forced_local
)
3105 /* Common symbols that become definitions don't get the DEF_REGULAR
3106 flag set, so test it first, and don't bail out. */
3107 if (ELF_COMMON_DEF_P (h
))
3109 /* If we don't have a definition in a regular file, then we can't
3110 resolve locally. The sym is either undefined or dynamic. */
3111 else if (!h
->def_regular
)
3114 /* Non-dynamic symbols resolve locally. */
3115 if (h
->dynindx
== -1)
3118 /* At this point, we know the symbol is defined and dynamic. In an
3119 executable it must resolve locally, likewise when building symbolic
3120 shared libraries. */
3121 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3124 /* Now deal with defined dynamic symbols in shared libraries. Ones
3125 with default visibility might not resolve locally. */
3126 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3129 hash_table
= elf_hash_table (info
);
3130 if (!is_elf_hash_table (hash_table
))
3133 bed
= get_elf_backend_data (hash_table
->dynobj
);
3135 /* If extern_protected_data is false, STV_PROTECTED non-function
3136 symbols are local. */
3137 if ((!info
->extern_protected_data
3138 || (info
->extern_protected_data
< 0
3139 && !bed
->extern_protected_data
))
3140 && !bed
->is_function_type (h
->type
))
3143 /* Function pointer equality tests may require that STV_PROTECTED
3144 symbols be treated as dynamic symbols. If the address of a
3145 function not defined in an executable is set to that function's
3146 plt entry in the executable, then the address of the function in
3147 a shared library must also be the plt entry in the executable. */
3148 return local_protected
;
3151 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3152 aligned. Returns the first TLS output section. */
3154 struct bfd_section
*
3155 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3157 struct bfd_section
*sec
, *tls
;
3158 unsigned int align
= 0;
3160 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3161 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3165 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3166 if (sec
->alignment_power
> align
)
3167 align
= sec
->alignment_power
;
3169 elf_hash_table (info
)->tls_sec
= tls
;
3171 /* Ensure the alignment of the first section is the largest alignment,
3172 so that the tls segment starts aligned. */
3174 tls
->alignment_power
= align
;
3179 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3181 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3182 Elf_Internal_Sym
*sym
)
3184 const struct elf_backend_data
*bed
;
3186 /* Local symbols do not count, but target specific ones might. */
3187 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3188 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3191 bed
= get_elf_backend_data (abfd
);
3192 /* Function symbols do not count. */
3193 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3196 /* If the section is undefined, then so is the symbol. */
3197 if (sym
->st_shndx
== SHN_UNDEF
)
3200 /* If the symbol is defined in the common section, then
3201 it is a common definition and so does not count. */
3202 if (bed
->common_definition (sym
))
3205 /* If the symbol is in a target specific section then we
3206 must rely upon the backend to tell us what it is. */
3207 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3208 /* FIXME - this function is not coded yet:
3210 return _bfd_is_global_symbol_definition (abfd, sym);
3212 Instead for now assume that the definition is not global,
3213 Even if this is wrong, at least the linker will behave
3214 in the same way that it used to do. */
3220 /* Search the symbol table of the archive element of the archive ABFD
3221 whose archive map contains a mention of SYMDEF, and determine if
3222 the symbol is defined in this element. */
3224 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3226 Elf_Internal_Shdr
* hdr
;
3230 Elf_Internal_Sym
*isymbuf
;
3231 Elf_Internal_Sym
*isym
;
3232 Elf_Internal_Sym
*isymend
;
3235 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
3239 if (! bfd_check_format (abfd
, bfd_object
))
3242 /* Select the appropriate symbol table. If we don't know if the
3243 object file is an IR object, give linker LTO plugin a chance to
3244 get the correct symbol table. */
3245 if (abfd
->plugin_format
== bfd_plugin_yes
3246 #if BFD_SUPPORTS_PLUGINS
3247 || (abfd
->plugin_format
== bfd_plugin_unknown
3248 && bfd_link_plugin_object_p (abfd
))
3252 /* Use the IR symbol table if the object has been claimed by
3254 abfd
= abfd
->plugin_dummy_bfd
;
3255 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3257 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3258 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3260 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3262 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3264 /* The sh_info field of the symtab header tells us where the
3265 external symbols start. We don't care about the local symbols. */
3266 if (elf_bad_symtab (abfd
))
3268 extsymcount
= symcount
;
3273 extsymcount
= symcount
- hdr
->sh_info
;
3274 extsymoff
= hdr
->sh_info
;
3277 if (extsymcount
== 0)
3280 /* Read in the symbol table. */
3281 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3283 if (isymbuf
== NULL
)
3286 /* Scan the symbol table looking for SYMDEF. */
3288 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3292 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3297 if (strcmp (name
, symdef
->name
) == 0)
3299 result
= is_global_data_symbol_definition (abfd
, isym
);
3309 /* Add an entry to the .dynamic table. */
3312 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3316 struct elf_link_hash_table
*hash_table
;
3317 const struct elf_backend_data
*bed
;
3319 bfd_size_type newsize
;
3320 bfd_byte
*newcontents
;
3321 Elf_Internal_Dyn dyn
;
3323 hash_table
= elf_hash_table (info
);
3324 if (! is_elf_hash_table (hash_table
))
3327 bed
= get_elf_backend_data (hash_table
->dynobj
);
3328 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3329 BFD_ASSERT (s
!= NULL
);
3331 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3332 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3333 if (newcontents
== NULL
)
3337 dyn
.d_un
.d_val
= val
;
3338 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3341 s
->contents
= newcontents
;
3346 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3347 otherwise just check whether one already exists. Returns -1 on error,
3348 1 if a DT_NEEDED tag already exists, and 0 on success. */
3351 elf_add_dt_needed_tag (bfd
*abfd
,
3352 struct bfd_link_info
*info
,
3356 struct elf_link_hash_table
*hash_table
;
3359 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3362 hash_table
= elf_hash_table (info
);
3363 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3364 if (strindex
== (size_t) -1)
3367 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3370 const struct elf_backend_data
*bed
;
3373 bed
= get_elf_backend_data (hash_table
->dynobj
);
3374 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3376 for (extdyn
= sdyn
->contents
;
3377 extdyn
< sdyn
->contents
+ sdyn
->size
;
3378 extdyn
+= bed
->s
->sizeof_dyn
)
3380 Elf_Internal_Dyn dyn
;
3382 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3383 if (dyn
.d_tag
== DT_NEEDED
3384 && dyn
.d_un
.d_val
== strindex
)
3386 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3394 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3397 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3401 /* We were just checking for existence of the tag. */
3402 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3407 /* Return true if SONAME is on the needed list between NEEDED and STOP
3408 (or the end of list if STOP is NULL), and needed by a library that
3412 on_needed_list (const char *soname
,
3413 struct bfd_link_needed_list
*needed
,
3414 struct bfd_link_needed_list
*stop
)
3416 struct bfd_link_needed_list
*look
;
3417 for (look
= needed
; look
!= stop
; look
= look
->next
)
3418 if (strcmp (soname
, look
->name
) == 0
3419 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3420 /* If needed by a library that itself is not directly
3421 needed, recursively check whether that library is
3422 indirectly needed. Since we add DT_NEEDED entries to
3423 the end of the list, library dependencies appear after
3424 the library. Therefore search prior to the current
3425 LOOK, preventing possible infinite recursion. */
3426 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3432 /* Sort symbol by value, section, and size. */
3434 elf_sort_symbol (const void *arg1
, const void *arg2
)
3436 const struct elf_link_hash_entry
*h1
;
3437 const struct elf_link_hash_entry
*h2
;
3438 bfd_signed_vma vdiff
;
3440 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3441 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3442 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3444 return vdiff
> 0 ? 1 : -1;
3447 int sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3449 return sdiff
> 0 ? 1 : -1;
3451 vdiff
= h1
->size
- h2
->size
;
3452 return vdiff
== 0 ? 0 : vdiff
> 0 ? 1 : -1;
3455 /* This function is used to adjust offsets into .dynstr for
3456 dynamic symbols. This is called via elf_link_hash_traverse. */
3459 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3461 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3463 if (h
->dynindx
!= -1)
3464 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3468 /* Assign string offsets in .dynstr, update all structures referencing
3472 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3474 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3475 struct elf_link_local_dynamic_entry
*entry
;
3476 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3477 bfd
*dynobj
= hash_table
->dynobj
;
3480 const struct elf_backend_data
*bed
;
3483 _bfd_elf_strtab_finalize (dynstr
);
3484 size
= _bfd_elf_strtab_size (dynstr
);
3486 bed
= get_elf_backend_data (dynobj
);
3487 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3488 BFD_ASSERT (sdyn
!= NULL
);
3490 /* Update all .dynamic entries referencing .dynstr strings. */
3491 for (extdyn
= sdyn
->contents
;
3492 extdyn
< sdyn
->contents
+ sdyn
->size
;
3493 extdyn
+= bed
->s
->sizeof_dyn
)
3495 Elf_Internal_Dyn dyn
;
3497 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3501 dyn
.d_un
.d_val
= size
;
3511 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3516 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3519 /* Now update local dynamic symbols. */
3520 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3521 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3522 entry
->isym
.st_name
);
3524 /* And the rest of dynamic symbols. */
3525 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3527 /* Adjust version definitions. */
3528 if (elf_tdata (output_bfd
)->cverdefs
)
3533 Elf_Internal_Verdef def
;
3534 Elf_Internal_Verdaux defaux
;
3536 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3540 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3542 p
+= sizeof (Elf_External_Verdef
);
3543 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3545 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3547 _bfd_elf_swap_verdaux_in (output_bfd
,
3548 (Elf_External_Verdaux
*) p
, &defaux
);
3549 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3551 _bfd_elf_swap_verdaux_out (output_bfd
,
3552 &defaux
, (Elf_External_Verdaux
*) p
);
3553 p
+= sizeof (Elf_External_Verdaux
);
3556 while (def
.vd_next
);
3559 /* Adjust version references. */
3560 if (elf_tdata (output_bfd
)->verref
)
3565 Elf_Internal_Verneed need
;
3566 Elf_Internal_Vernaux needaux
;
3568 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
3572 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3574 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3575 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3576 (Elf_External_Verneed
*) p
);
3577 p
+= sizeof (Elf_External_Verneed
);
3578 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3580 _bfd_elf_swap_vernaux_in (output_bfd
,
3581 (Elf_External_Vernaux
*) p
, &needaux
);
3582 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3584 _bfd_elf_swap_vernaux_out (output_bfd
,
3586 (Elf_External_Vernaux
*) p
);
3587 p
+= sizeof (Elf_External_Vernaux
);
3590 while (need
.vn_next
);
3596 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3597 The default is to only match when the INPUT and OUTPUT are exactly
3601 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3602 const bfd_target
*output
)
3604 return input
== output
;
3607 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3608 This version is used when different targets for the same architecture
3609 are virtually identical. */
3612 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3613 const bfd_target
*output
)
3615 const struct elf_backend_data
*obed
, *ibed
;
3617 if (input
== output
)
3620 ibed
= xvec_get_elf_backend_data (input
);
3621 obed
= xvec_get_elf_backend_data (output
);
3623 if (ibed
->arch
!= obed
->arch
)
3626 /* If both backends are using this function, deem them compatible. */
3627 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3630 /* Make a special call to the linker "notice" function to tell it that
3631 we are about to handle an as-needed lib, or have finished
3632 processing the lib. */
3635 _bfd_elf_notice_as_needed (bfd
*ibfd
,
3636 struct bfd_link_info
*info
,
3637 enum notice_asneeded_action act
)
3639 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
3642 /* Check relocations an ELF object file. */
3645 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
3647 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3648 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
3650 /* If this object is the same format as the output object, and it is
3651 not a shared library, then let the backend look through the
3654 This is required to build global offset table entries and to
3655 arrange for dynamic relocs. It is not required for the
3656 particular common case of linking non PIC code, even when linking
3657 against shared libraries, but unfortunately there is no way of
3658 knowing whether an object file has been compiled PIC or not.
3659 Looking through the relocs is not particularly time consuming.
3660 The problem is that we must either (1) keep the relocs in memory,
3661 which causes the linker to require additional runtime memory or
3662 (2) read the relocs twice from the input file, which wastes time.
3663 This would be a good case for using mmap.
3665 I have no idea how to handle linking PIC code into a file of a
3666 different format. It probably can't be done. */
3667 if ((abfd
->flags
& DYNAMIC
) == 0
3668 && is_elf_hash_table (htab
)
3669 && bed
->check_relocs
!= NULL
3670 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
3671 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
3675 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3677 Elf_Internal_Rela
*internal_relocs
;
3680 /* Don't check relocations in excluded sections. */
3681 if ((o
->flags
& SEC_RELOC
) == 0
3682 || (o
->flags
& SEC_EXCLUDE
) != 0
3683 || o
->reloc_count
== 0
3684 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
3685 && (o
->flags
& SEC_DEBUGGING
) != 0)
3686 || bfd_is_abs_section (o
->output_section
))
3689 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
3691 if (internal_relocs
== NULL
)
3694 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
3696 if (elf_section_data (o
)->relocs
!= internal_relocs
)
3697 free (internal_relocs
);
3707 /* Add symbols from an ELF object file to the linker hash table. */
3710 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3712 Elf_Internal_Ehdr
*ehdr
;
3713 Elf_Internal_Shdr
*hdr
;
3717 struct elf_link_hash_entry
**sym_hash
;
3718 bfd_boolean dynamic
;
3719 Elf_External_Versym
*extversym
= NULL
;
3720 Elf_External_Versym
*ever
;
3721 struct elf_link_hash_entry
*weaks
;
3722 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3723 size_t nondeflt_vers_cnt
= 0;
3724 Elf_Internal_Sym
*isymbuf
= NULL
;
3725 Elf_Internal_Sym
*isym
;
3726 Elf_Internal_Sym
*isymend
;
3727 const struct elf_backend_data
*bed
;
3728 bfd_boolean add_needed
;
3729 struct elf_link_hash_table
*htab
;
3731 void *alloc_mark
= NULL
;
3732 struct bfd_hash_entry
**old_table
= NULL
;
3733 unsigned int old_size
= 0;
3734 unsigned int old_count
= 0;
3735 void *old_tab
= NULL
;
3737 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3738 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3739 void *old_strtab
= NULL
;
3742 bfd_boolean just_syms
;
3744 htab
= elf_hash_table (info
);
3745 bed
= get_elf_backend_data (abfd
);
3747 if ((abfd
->flags
& DYNAMIC
) == 0)
3753 /* You can't use -r against a dynamic object. Also, there's no
3754 hope of using a dynamic object which does not exactly match
3755 the format of the output file. */
3756 if (bfd_link_relocatable (info
)
3757 || !is_elf_hash_table (htab
)
3758 || info
->output_bfd
->xvec
!= abfd
->xvec
)
3760 if (bfd_link_relocatable (info
))
3761 bfd_set_error (bfd_error_invalid_operation
);
3763 bfd_set_error (bfd_error_wrong_format
);
3768 ehdr
= elf_elfheader (abfd
);
3769 if (info
->warn_alternate_em
3770 && bed
->elf_machine_code
!= ehdr
->e_machine
3771 && ((bed
->elf_machine_alt1
!= 0
3772 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
3773 || (bed
->elf_machine_alt2
!= 0
3774 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
3775 info
->callbacks
->einfo
3776 /* xgettext:c-format */
3777 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3778 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
3780 /* As a GNU extension, any input sections which are named
3781 .gnu.warning.SYMBOL are treated as warning symbols for the given
3782 symbol. This differs from .gnu.warning sections, which generate
3783 warnings when they are included in an output file. */
3784 /* PR 12761: Also generate this warning when building shared libraries. */
3785 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3789 name
= bfd_get_section_name (abfd
, s
);
3790 if (CONST_STRNEQ (name
, ".gnu.warning."))
3795 name
+= sizeof ".gnu.warning." - 1;
3797 /* If this is a shared object, then look up the symbol
3798 in the hash table. If it is there, and it is already
3799 been defined, then we will not be using the entry
3800 from this shared object, so we don't need to warn.
3801 FIXME: If we see the definition in a regular object
3802 later on, we will warn, but we shouldn't. The only
3803 fix is to keep track of what warnings we are supposed
3804 to emit, and then handle them all at the end of the
3808 struct elf_link_hash_entry
*h
;
3810 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3812 /* FIXME: What about bfd_link_hash_common? */
3814 && (h
->root
.type
== bfd_link_hash_defined
3815 || h
->root
.type
== bfd_link_hash_defweak
))
3820 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
3824 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3829 if (! (_bfd_generic_link_add_one_symbol
3830 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3831 FALSE
, bed
->collect
, NULL
)))
3834 if (bfd_link_executable (info
))
3836 /* Clobber the section size so that the warning does
3837 not get copied into the output file. */
3840 /* Also set SEC_EXCLUDE, so that symbols defined in
3841 the warning section don't get copied to the output. */
3842 s
->flags
|= SEC_EXCLUDE
;
3847 just_syms
= ((s
= abfd
->sections
) != NULL
3848 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
3853 /* If we are creating a shared library, create all the dynamic
3854 sections immediately. We need to attach them to something,
3855 so we attach them to this BFD, provided it is the right
3856 format and is not from ld --just-symbols. Always create the
3857 dynamic sections for -E/--dynamic-list. FIXME: If there
3858 are no input BFD's of the same format as the output, we can't
3859 make a shared library. */
3861 && (bfd_link_pic (info
)
3862 || (!bfd_link_relocatable (info
)
3864 && (info
->export_dynamic
|| info
->dynamic
)))
3865 && is_elf_hash_table (htab
)
3866 && info
->output_bfd
->xvec
== abfd
->xvec
3867 && !htab
->dynamic_sections_created
)
3869 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3873 else if (!is_elf_hash_table (htab
))
3877 const char *soname
= NULL
;
3879 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3880 const Elf_Internal_Phdr
*phdr
;
3883 /* ld --just-symbols and dynamic objects don't mix very well.
3884 ld shouldn't allow it. */
3888 /* If this dynamic lib was specified on the command line with
3889 --as-needed in effect, then we don't want to add a DT_NEEDED
3890 tag unless the lib is actually used. Similary for libs brought
3891 in by another lib's DT_NEEDED. When --no-add-needed is used
3892 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3893 any dynamic library in DT_NEEDED tags in the dynamic lib at
3895 add_needed
= (elf_dyn_lib_class (abfd
)
3896 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3897 | DYN_NO_NEEDED
)) == 0;
3899 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3904 unsigned int elfsec
;
3905 unsigned long shlink
;
3907 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3914 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3915 if (elfsec
== SHN_BAD
)
3916 goto error_free_dyn
;
3917 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3919 for (extdyn
= dynbuf
;
3920 extdyn
< dynbuf
+ s
->size
;
3921 extdyn
+= bed
->s
->sizeof_dyn
)
3923 Elf_Internal_Dyn dyn
;
3925 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3926 if (dyn
.d_tag
== DT_SONAME
)
3928 unsigned int tagv
= dyn
.d_un
.d_val
;
3929 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3931 goto error_free_dyn
;
3933 if (dyn
.d_tag
== DT_NEEDED
)
3935 struct bfd_link_needed_list
*n
, **pn
;
3937 unsigned int tagv
= dyn
.d_un
.d_val
;
3939 amt
= sizeof (struct bfd_link_needed_list
);
3940 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3941 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3942 if (n
== NULL
|| fnm
== NULL
)
3943 goto error_free_dyn
;
3944 amt
= strlen (fnm
) + 1;
3945 anm
= (char *) bfd_alloc (abfd
, amt
);
3947 goto error_free_dyn
;
3948 memcpy (anm
, fnm
, amt
);
3952 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3956 if (dyn
.d_tag
== DT_RUNPATH
)
3958 struct bfd_link_needed_list
*n
, **pn
;
3960 unsigned int tagv
= dyn
.d_un
.d_val
;
3962 amt
= sizeof (struct bfd_link_needed_list
);
3963 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3964 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3965 if (n
== NULL
|| fnm
== NULL
)
3966 goto error_free_dyn
;
3967 amt
= strlen (fnm
) + 1;
3968 anm
= (char *) bfd_alloc (abfd
, amt
);
3970 goto error_free_dyn
;
3971 memcpy (anm
, fnm
, amt
);
3975 for (pn
= & runpath
;
3981 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3982 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3984 struct bfd_link_needed_list
*n
, **pn
;
3986 unsigned int tagv
= dyn
.d_un
.d_val
;
3988 amt
= sizeof (struct bfd_link_needed_list
);
3989 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3990 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3991 if (n
== NULL
|| fnm
== NULL
)
3992 goto error_free_dyn
;
3993 amt
= strlen (fnm
) + 1;
3994 anm
= (char *) bfd_alloc (abfd
, amt
);
3996 goto error_free_dyn
;
3997 memcpy (anm
, fnm
, amt
);
4007 if (dyn
.d_tag
== DT_AUDIT
)
4009 unsigned int tagv
= dyn
.d_un
.d_val
;
4010 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4017 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4018 frees all more recently bfd_alloc'd blocks as well. */
4024 struct bfd_link_needed_list
**pn
;
4025 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4030 /* If we have a PT_GNU_RELRO program header, mark as read-only
4031 all sections contained fully therein. This makes relro
4032 shared library sections appear as they will at run-time. */
4033 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4034 while (--phdr
>= elf_tdata (abfd
)->phdr
)
4035 if (phdr
->p_type
== PT_GNU_RELRO
)
4037 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4038 if ((s
->flags
& SEC_ALLOC
) != 0
4039 && s
->vma
>= phdr
->p_vaddr
4040 && s
->vma
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4041 s
->flags
|= SEC_READONLY
;
4045 /* We do not want to include any of the sections in a dynamic
4046 object in the output file. We hack by simply clobbering the
4047 list of sections in the BFD. This could be handled more
4048 cleanly by, say, a new section flag; the existing
4049 SEC_NEVER_LOAD flag is not the one we want, because that one
4050 still implies that the section takes up space in the output
4052 bfd_section_list_clear (abfd
);
4054 /* Find the name to use in a DT_NEEDED entry that refers to this
4055 object. If the object has a DT_SONAME entry, we use it.
4056 Otherwise, if the generic linker stuck something in
4057 elf_dt_name, we use that. Otherwise, we just use the file
4059 if (soname
== NULL
|| *soname
== '\0')
4061 soname
= elf_dt_name (abfd
);
4062 if (soname
== NULL
|| *soname
== '\0')
4063 soname
= bfd_get_filename (abfd
);
4066 /* Save the SONAME because sometimes the linker emulation code
4067 will need to know it. */
4068 elf_dt_name (abfd
) = soname
;
4070 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4074 /* If we have already included this dynamic object in the
4075 link, just ignore it. There is no reason to include a
4076 particular dynamic object more than once. */
4080 /* Save the DT_AUDIT entry for the linker emulation code. */
4081 elf_dt_audit (abfd
) = audit
;
4084 /* If this is a dynamic object, we always link against the .dynsym
4085 symbol table, not the .symtab symbol table. The dynamic linker
4086 will only see the .dynsym symbol table, so there is no reason to
4087 look at .symtab for a dynamic object. */
4089 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4090 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4092 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4094 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4096 /* The sh_info field of the symtab header tells us where the
4097 external symbols start. We don't care about the local symbols at
4099 if (elf_bad_symtab (abfd
))
4101 extsymcount
= symcount
;
4106 extsymcount
= symcount
- hdr
->sh_info
;
4107 extsymoff
= hdr
->sh_info
;
4110 sym_hash
= elf_sym_hashes (abfd
);
4111 if (extsymcount
!= 0)
4113 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4115 if (isymbuf
== NULL
)
4118 if (sym_hash
== NULL
)
4120 /* We store a pointer to the hash table entry for each
4123 amt
*= sizeof (struct elf_link_hash_entry
*);
4124 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4125 if (sym_hash
== NULL
)
4126 goto error_free_sym
;
4127 elf_sym_hashes (abfd
) = sym_hash
;
4133 /* Read in any version definitions. */
4134 if (!_bfd_elf_slurp_version_tables (abfd
,
4135 info
->default_imported_symver
))
4136 goto error_free_sym
;
4138 /* Read in the symbol versions, but don't bother to convert them
4139 to internal format. */
4140 if (elf_dynversym (abfd
) != 0)
4142 Elf_Internal_Shdr
*versymhdr
;
4144 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4145 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
4146 if (extversym
== NULL
)
4147 goto error_free_sym
;
4148 amt
= versymhdr
->sh_size
;
4149 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
4150 || bfd_bread (extversym
, amt
, abfd
) != amt
)
4151 goto error_free_vers
;
4155 /* If we are loading an as-needed shared lib, save the symbol table
4156 state before we start adding symbols. If the lib turns out
4157 to be unneeded, restore the state. */
4158 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4163 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4165 struct bfd_hash_entry
*p
;
4166 struct elf_link_hash_entry
*h
;
4168 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4170 h
= (struct elf_link_hash_entry
*) p
;
4171 entsize
+= htab
->root
.table
.entsize
;
4172 if (h
->root
.type
== bfd_link_hash_warning
)
4173 entsize
+= htab
->root
.table
.entsize
;
4177 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4178 old_tab
= bfd_malloc (tabsize
+ entsize
);
4179 if (old_tab
== NULL
)
4180 goto error_free_vers
;
4182 /* Remember the current objalloc pointer, so that all mem for
4183 symbols added can later be reclaimed. */
4184 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4185 if (alloc_mark
== NULL
)
4186 goto error_free_vers
;
4188 /* Make a special call to the linker "notice" function to
4189 tell it that we are about to handle an as-needed lib. */
4190 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4191 goto error_free_vers
;
4193 /* Clone the symbol table. Remember some pointers into the
4194 symbol table, and dynamic symbol count. */
4195 old_ent
= (char *) old_tab
+ tabsize
;
4196 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4197 old_undefs
= htab
->root
.undefs
;
4198 old_undefs_tail
= htab
->root
.undefs_tail
;
4199 old_table
= htab
->root
.table
.table
;
4200 old_size
= htab
->root
.table
.size
;
4201 old_count
= htab
->root
.table
.count
;
4202 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4203 if (old_strtab
== NULL
)
4204 goto error_free_vers
;
4206 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4208 struct bfd_hash_entry
*p
;
4209 struct elf_link_hash_entry
*h
;
4211 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4213 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
4214 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4215 h
= (struct elf_link_hash_entry
*) p
;
4216 if (h
->root
.type
== bfd_link_hash_warning
)
4218 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
4219 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4226 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
4227 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
4229 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4233 asection
*sec
, *new_sec
;
4236 struct elf_link_hash_entry
*h
;
4237 struct elf_link_hash_entry
*hi
;
4238 bfd_boolean definition
;
4239 bfd_boolean size_change_ok
;
4240 bfd_boolean type_change_ok
;
4241 bfd_boolean new_weakdef
;
4242 bfd_boolean new_weak
;
4243 bfd_boolean old_weak
;
4244 bfd_boolean override
;
4246 bfd_boolean discarded
;
4247 unsigned int old_alignment
;
4249 bfd_boolean matched
;
4253 flags
= BSF_NO_FLAGS
;
4255 value
= isym
->st_value
;
4256 common
= bed
->common_definition (isym
);
4257 if (common
&& info
->inhibit_common_definition
)
4259 /* Treat common symbol as undefined for --no-define-common. */
4260 isym
->st_shndx
= SHN_UNDEF
;
4265 bind
= ELF_ST_BIND (isym
->st_info
);
4269 /* This should be impossible, since ELF requires that all
4270 global symbols follow all local symbols, and that sh_info
4271 point to the first global symbol. Unfortunately, Irix 5
4276 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4284 case STB_GNU_UNIQUE
:
4285 flags
= BSF_GNU_UNIQUE
;
4289 /* Leave it up to the processor backend. */
4293 if (isym
->st_shndx
== SHN_UNDEF
)
4294 sec
= bfd_und_section_ptr
;
4295 else if (isym
->st_shndx
== SHN_ABS
)
4296 sec
= bfd_abs_section_ptr
;
4297 else if (isym
->st_shndx
== SHN_COMMON
)
4299 sec
= bfd_com_section_ptr
;
4300 /* What ELF calls the size we call the value. What ELF
4301 calls the value we call the alignment. */
4302 value
= isym
->st_size
;
4306 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4308 sec
= bfd_abs_section_ptr
;
4309 else if (discarded_section (sec
))
4311 /* Symbols from discarded section are undefined. We keep
4313 sec
= bfd_und_section_ptr
;
4315 isym
->st_shndx
= SHN_UNDEF
;
4317 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4321 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4324 goto error_free_vers
;
4326 if (isym
->st_shndx
== SHN_COMMON
4327 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4329 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4333 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4335 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4337 goto error_free_vers
;
4341 else if (isym
->st_shndx
== SHN_COMMON
4342 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4343 && !bfd_link_relocatable (info
))
4345 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4349 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4350 | SEC_LINKER_CREATED
);
4351 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4353 goto error_free_vers
;
4357 else if (bed
->elf_add_symbol_hook
)
4359 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4361 goto error_free_vers
;
4363 /* The hook function sets the name to NULL if this symbol
4364 should be skipped for some reason. */
4369 /* Sanity check that all possibilities were handled. */
4372 bfd_set_error (bfd_error_bad_value
);
4373 goto error_free_vers
;
4376 /* Silently discard TLS symbols from --just-syms. There's
4377 no way to combine a static TLS block with a new TLS block
4378 for this executable. */
4379 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4380 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4383 if (bfd_is_und_section (sec
)
4384 || bfd_is_com_section (sec
))
4389 size_change_ok
= FALSE
;
4390 type_change_ok
= bed
->type_change_ok
;
4397 if (is_elf_hash_table (htab
))
4399 Elf_Internal_Versym iver
;
4400 unsigned int vernum
= 0;
4405 if (info
->default_imported_symver
)
4406 /* Use the default symbol version created earlier. */
4407 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4412 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4414 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4416 /* If this is a hidden symbol, or if it is not version
4417 1, we append the version name to the symbol name.
4418 However, we do not modify a non-hidden absolute symbol
4419 if it is not a function, because it might be the version
4420 symbol itself. FIXME: What if it isn't? */
4421 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4423 && (!bfd_is_abs_section (sec
)
4424 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4427 size_t namelen
, verlen
, newlen
;
4430 if (isym
->st_shndx
!= SHN_UNDEF
)
4432 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4434 else if (vernum
> 1)
4436 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4443 /* xgettext:c-format */
4444 (_("%B: %s: invalid version %u (max %d)"),
4446 elf_tdata (abfd
)->cverdefs
);
4447 bfd_set_error (bfd_error_bad_value
);
4448 goto error_free_vers
;
4453 /* We cannot simply test for the number of
4454 entries in the VERNEED section since the
4455 numbers for the needed versions do not start
4457 Elf_Internal_Verneed
*t
;
4460 for (t
= elf_tdata (abfd
)->verref
;
4464 Elf_Internal_Vernaux
*a
;
4466 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4468 if (a
->vna_other
== vernum
)
4470 verstr
= a
->vna_nodename
;
4480 /* xgettext:c-format */
4481 (_("%B: %s: invalid needed version %d"),
4482 abfd
, name
, vernum
);
4483 bfd_set_error (bfd_error_bad_value
);
4484 goto error_free_vers
;
4488 namelen
= strlen (name
);
4489 verlen
= strlen (verstr
);
4490 newlen
= namelen
+ verlen
+ 2;
4491 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4492 && isym
->st_shndx
!= SHN_UNDEF
)
4495 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4496 if (newname
== NULL
)
4497 goto error_free_vers
;
4498 memcpy (newname
, name
, namelen
);
4499 p
= newname
+ namelen
;
4501 /* If this is a defined non-hidden version symbol,
4502 we add another @ to the name. This indicates the
4503 default version of the symbol. */
4504 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4505 && isym
->st_shndx
!= SHN_UNDEF
)
4507 memcpy (p
, verstr
, verlen
+ 1);
4512 /* If this symbol has default visibility and the user has
4513 requested we not re-export it, then mark it as hidden. */
4514 if (!bfd_is_und_section (sec
)
4517 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4518 isym
->st_other
= (STV_HIDDEN
4519 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4521 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4522 sym_hash
, &old_bfd
, &old_weak
,
4523 &old_alignment
, &skip
, &override
,
4524 &type_change_ok
, &size_change_ok
,
4526 goto error_free_vers
;
4531 /* Override a definition only if the new symbol matches the
4533 if (override
&& matched
)
4537 while (h
->root
.type
== bfd_link_hash_indirect
4538 || h
->root
.type
== bfd_link_hash_warning
)
4539 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4541 if (elf_tdata (abfd
)->verdef
!= NULL
4544 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4547 if (! (_bfd_generic_link_add_one_symbol
4548 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4549 (struct bfd_link_hash_entry
**) sym_hash
)))
4550 goto error_free_vers
;
4552 if ((flags
& BSF_GNU_UNIQUE
)
4553 && (abfd
->flags
& DYNAMIC
) == 0
4554 && bfd_get_flavour (info
->output_bfd
) == bfd_target_elf_flavour
)
4555 elf_tdata (info
->output_bfd
)->has_gnu_symbols
|= elf_gnu_symbol_unique
;
4558 /* We need to make sure that indirect symbol dynamic flags are
4561 while (h
->root
.type
== bfd_link_hash_indirect
4562 || h
->root
.type
== bfd_link_hash_warning
)
4563 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4565 /* Setting the index to -3 tells elf_link_output_extsym that
4566 this symbol is defined in a discarded section. */
4572 new_weak
= (flags
& BSF_WEAK
) != 0;
4573 new_weakdef
= FALSE
;
4577 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4578 && is_elf_hash_table (htab
)
4579 && h
->u
.weakdef
== NULL
)
4581 /* Keep a list of all weak defined non function symbols from
4582 a dynamic object, using the weakdef field. Later in this
4583 function we will set the weakdef field to the correct
4584 value. We only put non-function symbols from dynamic
4585 objects on this list, because that happens to be the only
4586 time we need to know the normal symbol corresponding to a
4587 weak symbol, and the information is time consuming to
4588 figure out. If the weakdef field is not already NULL,
4589 then this symbol was already defined by some previous
4590 dynamic object, and we will be using that previous
4591 definition anyhow. */
4593 h
->u
.weakdef
= weaks
;
4598 /* Set the alignment of a common symbol. */
4599 if ((common
|| bfd_is_com_section (sec
))
4600 && h
->root
.type
== bfd_link_hash_common
)
4605 align
= bfd_log2 (isym
->st_value
);
4608 /* The new symbol is a common symbol in a shared object.
4609 We need to get the alignment from the section. */
4610 align
= new_sec
->alignment_power
;
4612 if (align
> old_alignment
)
4613 h
->root
.u
.c
.p
->alignment_power
= align
;
4615 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4618 if (is_elf_hash_table (htab
))
4620 /* Set a flag in the hash table entry indicating the type of
4621 reference or definition we just found. A dynamic symbol
4622 is one which is referenced or defined by both a regular
4623 object and a shared object. */
4624 bfd_boolean dynsym
= FALSE
;
4626 /* Plugin symbols aren't normal. Don't set def_regular or
4627 ref_regular for them, or make them dynamic. */
4628 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4635 if (bind
!= STB_WEAK
)
4636 h
->ref_regular_nonweak
= 1;
4648 /* If the indirect symbol has been forced local, don't
4649 make the real symbol dynamic. */
4650 if ((h
== hi
|| !hi
->forced_local
)
4651 && (bfd_link_dll (info
)
4661 hi
->ref_dynamic
= 1;
4666 hi
->def_dynamic
= 1;
4669 /* If the indirect symbol has been forced local, don't
4670 make the real symbol dynamic. */
4671 if ((h
== hi
|| !hi
->forced_local
)
4674 || (h
->u
.weakdef
!= NULL
4676 && h
->u
.weakdef
->dynindx
!= -1)))
4680 /* Check to see if we need to add an indirect symbol for
4681 the default name. */
4683 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
4684 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4685 sec
, value
, &old_bfd
, &dynsym
))
4686 goto error_free_vers
;
4688 /* Check the alignment when a common symbol is involved. This
4689 can change when a common symbol is overridden by a normal
4690 definition or a common symbol is ignored due to the old
4691 normal definition. We need to make sure the maximum
4692 alignment is maintained. */
4693 if ((old_alignment
|| common
)
4694 && h
->root
.type
!= bfd_link_hash_common
)
4696 unsigned int common_align
;
4697 unsigned int normal_align
;
4698 unsigned int symbol_align
;
4702 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
4703 || h
->root
.type
== bfd_link_hash_defweak
);
4705 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4706 if (h
->root
.u
.def
.section
->owner
!= NULL
4707 && (h
->root
.u
.def
.section
->owner
->flags
4708 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
4710 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4711 if (normal_align
> symbol_align
)
4712 normal_align
= symbol_align
;
4715 normal_align
= symbol_align
;
4719 common_align
= old_alignment
;
4720 common_bfd
= old_bfd
;
4725 common_align
= bfd_log2 (isym
->st_value
);
4727 normal_bfd
= old_bfd
;
4730 if (normal_align
< common_align
)
4732 /* PR binutils/2735 */
4733 if (normal_bfd
== NULL
)
4735 /* xgettext:c-format */
4736 (_("Warning: alignment %u of common symbol `%s' in %B is"
4737 " greater than the alignment (%u) of its section %A"),
4738 1 << common_align
, name
, common_bfd
,
4739 1 << normal_align
, h
->root
.u
.def
.section
);
4742 /* xgettext:c-format */
4743 (_("Warning: alignment %u of symbol `%s' in %B"
4744 " is smaller than %u in %B"),
4745 1 << normal_align
, name
, normal_bfd
,
4746 1 << common_align
, common_bfd
);
4750 /* Remember the symbol size if it isn't undefined. */
4751 if (isym
->st_size
!= 0
4752 && isym
->st_shndx
!= SHN_UNDEF
4753 && (definition
|| h
->size
== 0))
4756 && h
->size
!= isym
->st_size
4757 && ! size_change_ok
)
4759 /* xgettext:c-format */
4760 (_("Warning: size of symbol `%s' changed"
4761 " from %Lu in %B to %Lu in %B"),
4762 name
, h
->size
, old_bfd
, isym
->st_size
, abfd
);
4764 h
->size
= isym
->st_size
;
4767 /* If this is a common symbol, then we always want H->SIZE
4768 to be the size of the common symbol. The code just above
4769 won't fix the size if a common symbol becomes larger. We
4770 don't warn about a size change here, because that is
4771 covered by --warn-common. Allow changes between different
4773 if (h
->root
.type
== bfd_link_hash_common
)
4774 h
->size
= h
->root
.u
.c
.size
;
4776 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4777 && ((definition
&& !new_weak
)
4778 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
4779 || h
->type
== STT_NOTYPE
))
4781 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
4783 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4785 if (type
== STT_GNU_IFUNC
4786 && (abfd
->flags
& DYNAMIC
) != 0)
4789 if (h
->type
!= type
)
4791 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
4792 /* xgettext:c-format */
4794 (_("Warning: type of symbol `%s' changed"
4795 " from %d to %d in %B"),
4796 name
, h
->type
, type
, abfd
);
4802 /* Merge st_other field. */
4803 elf_merge_st_other (abfd
, h
, isym
, sec
, definition
, dynamic
);
4805 /* We don't want to make debug symbol dynamic. */
4807 && (sec
->flags
& SEC_DEBUGGING
)
4808 && !bfd_link_relocatable (info
))
4811 /* Nor should we make plugin symbols dynamic. */
4812 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4817 h
->target_internal
= isym
->st_target_internal
;
4818 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
4821 if (definition
&& !dynamic
)
4823 char *p
= strchr (name
, ELF_VER_CHR
);
4824 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4826 /* Queue non-default versions so that .symver x, x@FOO
4827 aliases can be checked. */
4830 amt
= ((isymend
- isym
+ 1)
4831 * sizeof (struct elf_link_hash_entry
*));
4833 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
4835 goto error_free_vers
;
4837 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4841 if (dynsym
&& h
->dynindx
== -1)
4843 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4844 goto error_free_vers
;
4845 if (h
->u
.weakdef
!= NULL
4847 && h
->u
.weakdef
->dynindx
== -1)
4849 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4850 goto error_free_vers
;
4853 else if (h
->dynindx
!= -1)
4854 /* If the symbol already has a dynamic index, but
4855 visibility says it should not be visible, turn it into
4857 switch (ELF_ST_VISIBILITY (h
->other
))
4861 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4866 /* Don't add DT_NEEDED for references from the dummy bfd nor
4867 for unmatched symbol. */
4872 && h
->ref_regular_nonweak
4874 || (old_bfd
->flags
& BFD_PLUGIN
) == 0))
4875 || (h
->ref_dynamic_nonweak
4876 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
4877 && !on_needed_list (elf_dt_name (abfd
),
4878 htab
->needed
, NULL
))))
4881 const char *soname
= elf_dt_name (abfd
);
4883 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
4884 h
->root
.root
.string
);
4886 /* A symbol from a library loaded via DT_NEEDED of some
4887 other library is referenced by a regular object.
4888 Add a DT_NEEDED entry for it. Issue an error if
4889 --no-add-needed is used and the reference was not
4892 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4895 /* xgettext:c-format */
4896 (_("%B: undefined reference to symbol '%s'"),
4898 bfd_set_error (bfd_error_missing_dso
);
4899 goto error_free_vers
;
4902 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
4903 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
4906 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4908 goto error_free_vers
;
4910 BFD_ASSERT (ret
== 0);
4915 if (extversym
!= NULL
)
4921 if (isymbuf
!= NULL
)
4927 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4931 /* Restore the symbol table. */
4932 old_ent
= (char *) old_tab
+ tabsize
;
4933 memset (elf_sym_hashes (abfd
), 0,
4934 extsymcount
* sizeof (struct elf_link_hash_entry
*));
4935 htab
->root
.table
.table
= old_table
;
4936 htab
->root
.table
.size
= old_size
;
4937 htab
->root
.table
.count
= old_count
;
4938 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4939 htab
->root
.undefs
= old_undefs
;
4940 htab
->root
.undefs_tail
= old_undefs_tail
;
4941 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
4944 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4946 struct bfd_hash_entry
*p
;
4947 struct elf_link_hash_entry
*h
;
4949 unsigned int alignment_power
;
4950 unsigned int non_ir_ref_dynamic
;
4952 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4954 h
= (struct elf_link_hash_entry
*) p
;
4955 if (h
->root
.type
== bfd_link_hash_warning
)
4956 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4958 /* Preserve the maximum alignment and size for common
4959 symbols even if this dynamic lib isn't on DT_NEEDED
4960 since it can still be loaded at run time by another
4962 if (h
->root
.type
== bfd_link_hash_common
)
4964 size
= h
->root
.u
.c
.size
;
4965 alignment_power
= h
->root
.u
.c
.p
->alignment_power
;
4970 alignment_power
= 0;
4972 /* Preserve non_ir_ref_dynamic so that this symbol
4973 will be exported when the dynamic lib becomes needed
4974 in the second pass. */
4975 non_ir_ref_dynamic
= h
->root
.non_ir_ref_dynamic
;
4976 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4977 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4978 h
= (struct elf_link_hash_entry
*) p
;
4979 if (h
->root
.type
== bfd_link_hash_warning
)
4981 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4982 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4983 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4985 if (h
->root
.type
== bfd_link_hash_common
)
4987 if (size
> h
->root
.u
.c
.size
)
4988 h
->root
.u
.c
.size
= size
;
4989 if (alignment_power
> h
->root
.u
.c
.p
->alignment_power
)
4990 h
->root
.u
.c
.p
->alignment_power
= alignment_power
;
4992 h
->root
.non_ir_ref_dynamic
= non_ir_ref_dynamic
;
4996 /* Make a special call to the linker "notice" function to
4997 tell it that symbols added for crefs may need to be removed. */
4998 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
4999 goto error_free_vers
;
5002 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
5004 if (nondeflt_vers
!= NULL
)
5005 free (nondeflt_vers
);
5009 if (old_tab
!= NULL
)
5011 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
5012 goto error_free_vers
;
5017 /* Now that all the symbols from this input file are created, if
5018 not performing a relocatable link, handle .symver foo, foo@BAR
5019 such that any relocs against foo become foo@BAR. */
5020 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5024 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5026 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5027 char *shortname
, *p
;
5029 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5031 || (h
->root
.type
!= bfd_link_hash_defined
5032 && h
->root
.type
!= bfd_link_hash_defweak
))
5035 amt
= p
- h
->root
.root
.string
;
5036 shortname
= (char *) bfd_malloc (amt
+ 1);
5038 goto error_free_vers
;
5039 memcpy (shortname
, h
->root
.root
.string
, amt
);
5040 shortname
[amt
] = '\0';
5042 hi
= (struct elf_link_hash_entry
*)
5043 bfd_link_hash_lookup (&htab
->root
, shortname
,
5044 FALSE
, FALSE
, FALSE
);
5046 && hi
->root
.type
== h
->root
.type
5047 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5048 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5050 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
5051 hi
->root
.type
= bfd_link_hash_indirect
;
5052 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5053 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5054 sym_hash
= elf_sym_hashes (abfd
);
5056 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5057 if (sym_hash
[symidx
] == hi
)
5059 sym_hash
[symidx
] = h
;
5065 free (nondeflt_vers
);
5066 nondeflt_vers
= NULL
;
5069 /* Now set the weakdefs field correctly for all the weak defined
5070 symbols we found. The only way to do this is to search all the
5071 symbols. Since we only need the information for non functions in
5072 dynamic objects, that's the only time we actually put anything on
5073 the list WEAKS. We need this information so that if a regular
5074 object refers to a symbol defined weakly in a dynamic object, the
5075 real symbol in the dynamic object is also put in the dynamic
5076 symbols; we also must arrange for both symbols to point to the
5077 same memory location. We could handle the general case of symbol
5078 aliasing, but a general symbol alias can only be generated in
5079 assembler code, handling it correctly would be very time
5080 consuming, and other ELF linkers don't handle general aliasing
5084 struct elf_link_hash_entry
**hpp
;
5085 struct elf_link_hash_entry
**hppend
;
5086 struct elf_link_hash_entry
**sorted_sym_hash
;
5087 struct elf_link_hash_entry
*h
;
5090 /* Since we have to search the whole symbol list for each weak
5091 defined symbol, search time for N weak defined symbols will be
5092 O(N^2). Binary search will cut it down to O(NlogN). */
5094 amt
*= sizeof (struct elf_link_hash_entry
*);
5095 sorted_sym_hash
= (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5096 if (sorted_sym_hash
== NULL
)
5098 sym_hash
= sorted_sym_hash
;
5099 hpp
= elf_sym_hashes (abfd
);
5100 hppend
= hpp
+ extsymcount
;
5102 for (; hpp
< hppend
; hpp
++)
5106 && h
->root
.type
== bfd_link_hash_defined
5107 && !bed
->is_function_type (h
->type
))
5115 qsort (sorted_sym_hash
, sym_count
,
5116 sizeof (struct elf_link_hash_entry
*),
5119 while (weaks
!= NULL
)
5121 struct elf_link_hash_entry
*hlook
;
5124 size_t i
, j
, idx
= 0;
5127 weaks
= hlook
->u
.weakdef
;
5128 hlook
->u
.weakdef
= NULL
;
5130 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
5131 || hlook
->root
.type
== bfd_link_hash_defweak
5132 || hlook
->root
.type
== bfd_link_hash_common
5133 || hlook
->root
.type
== bfd_link_hash_indirect
);
5134 slook
= hlook
->root
.u
.def
.section
;
5135 vlook
= hlook
->root
.u
.def
.value
;
5141 bfd_signed_vma vdiff
;
5143 h
= sorted_sym_hash
[idx
];
5144 vdiff
= vlook
- h
->root
.u
.def
.value
;
5151 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5161 /* We didn't find a value/section match. */
5165 /* With multiple aliases, or when the weak symbol is already
5166 strongly defined, we have multiple matching symbols and
5167 the binary search above may land on any of them. Step
5168 one past the matching symbol(s). */
5171 h
= sorted_sym_hash
[idx
];
5172 if (h
->root
.u
.def
.section
!= slook
5173 || h
->root
.u
.def
.value
!= vlook
)
5177 /* Now look back over the aliases. Since we sorted by size
5178 as well as value and section, we'll choose the one with
5179 the largest size. */
5182 h
= sorted_sym_hash
[idx
];
5184 /* Stop if value or section doesn't match. */
5185 if (h
->root
.u
.def
.section
!= slook
5186 || h
->root
.u
.def
.value
!= vlook
)
5188 else if (h
!= hlook
)
5190 hlook
->u
.weakdef
= h
;
5192 /* If the weak definition is in the list of dynamic
5193 symbols, make sure the real definition is put
5195 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5197 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5200 free (sorted_sym_hash
);
5205 /* If the real definition is in the list of dynamic
5206 symbols, make sure the weak definition is put
5207 there as well. If we don't do this, then the
5208 dynamic loader might not merge the entries for the
5209 real definition and the weak definition. */
5210 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5212 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5213 goto err_free_sym_hash
;
5220 free (sorted_sym_hash
);
5223 if (bed
->check_directives
5224 && !(*bed
->check_directives
) (abfd
, info
))
5227 if (!info
->check_relocs_after_open_input
5228 && !_bfd_elf_link_check_relocs (abfd
, info
))
5231 /* If this is a non-traditional link, try to optimize the handling
5232 of the .stab/.stabstr sections. */
5234 && ! info
->traditional_format
5235 && is_elf_hash_table (htab
)
5236 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5240 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5241 if (stabstr
!= NULL
)
5243 bfd_size_type string_offset
= 0;
5246 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5247 if (CONST_STRNEQ (stab
->name
, ".stab")
5248 && (!stab
->name
[5] ||
5249 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5250 && (stab
->flags
& SEC_MERGE
) == 0
5251 && !bfd_is_abs_section (stab
->output_section
))
5253 struct bfd_elf_section_data
*secdata
;
5255 secdata
= elf_section_data (stab
);
5256 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5257 stabstr
, &secdata
->sec_info
,
5260 if (secdata
->sec_info
)
5261 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5266 if (is_elf_hash_table (htab
) && add_needed
)
5268 /* Add this bfd to the loaded list. */
5269 struct elf_link_loaded_list
*n
;
5271 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5275 n
->next
= htab
->loaded
;
5282 if (old_tab
!= NULL
)
5284 if (old_strtab
!= NULL
)
5286 if (nondeflt_vers
!= NULL
)
5287 free (nondeflt_vers
);
5288 if (extversym
!= NULL
)
5291 if (isymbuf
!= NULL
)
5297 /* Return the linker hash table entry of a symbol that might be
5298 satisfied by an archive symbol. Return -1 on error. */
5300 struct elf_link_hash_entry
*
5301 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5302 struct bfd_link_info
*info
,
5305 struct elf_link_hash_entry
*h
;
5309 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, TRUE
);
5313 /* If this is a default version (the name contains @@), look up the
5314 symbol again with only one `@' as well as without the version.
5315 The effect is that references to the symbol with and without the
5316 version will be matched by the default symbol in the archive. */
5318 p
= strchr (name
, ELF_VER_CHR
);
5319 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5322 /* First check with only one `@'. */
5323 len
= strlen (name
);
5324 copy
= (char *) bfd_alloc (abfd
, len
);
5326 return (struct elf_link_hash_entry
*) 0 - 1;
5328 first
= p
- name
+ 1;
5329 memcpy (copy
, name
, first
);
5330 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5332 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, TRUE
);
5335 /* We also need to check references to the symbol without the
5337 copy
[first
- 1] = '\0';
5338 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
5339 FALSE
, FALSE
, TRUE
);
5342 bfd_release (abfd
, copy
);
5346 /* Add symbols from an ELF archive file to the linker hash table. We
5347 don't use _bfd_generic_link_add_archive_symbols because we need to
5348 handle versioned symbols.
5350 Fortunately, ELF archive handling is simpler than that done by
5351 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5352 oddities. In ELF, if we find a symbol in the archive map, and the
5353 symbol is currently undefined, we know that we must pull in that
5356 Unfortunately, we do have to make multiple passes over the symbol
5357 table until nothing further is resolved. */
5360 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5363 unsigned char *included
= NULL
;
5367 const struct elf_backend_data
*bed
;
5368 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
5369 (bfd
*, struct bfd_link_info
*, const char *);
5371 if (! bfd_has_map (abfd
))
5373 /* An empty archive is a special case. */
5374 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5376 bfd_set_error (bfd_error_no_armap
);
5380 /* Keep track of all symbols we know to be already defined, and all
5381 files we know to be already included. This is to speed up the
5382 second and subsequent passes. */
5383 c
= bfd_ardata (abfd
)->symdef_count
;
5387 amt
*= sizeof (*included
);
5388 included
= (unsigned char *) bfd_zmalloc (amt
);
5389 if (included
== NULL
)
5392 symdefs
= bfd_ardata (abfd
)->symdefs
;
5393 bed
= get_elf_backend_data (abfd
);
5394 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5407 symdefend
= symdef
+ c
;
5408 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5410 struct elf_link_hash_entry
*h
;
5412 struct bfd_link_hash_entry
*undefs_tail
;
5417 if (symdef
->file_offset
== last
)
5423 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5424 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
5430 if (h
->root
.type
== bfd_link_hash_common
)
5432 /* We currently have a common symbol. The archive map contains
5433 a reference to this symbol, so we may want to include it. We
5434 only want to include it however, if this archive element
5435 contains a definition of the symbol, not just another common
5438 Unfortunately some archivers (including GNU ar) will put
5439 declarations of common symbols into their archive maps, as
5440 well as real definitions, so we cannot just go by the archive
5441 map alone. Instead we must read in the element's symbol
5442 table and check that to see what kind of symbol definition
5444 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5447 else if (h
->root
.type
!= bfd_link_hash_undefined
)
5449 if (h
->root
.type
!= bfd_link_hash_undefweak
)
5450 /* Symbol must be defined. Don't check it again. */
5455 /* We need to include this archive member. */
5456 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5457 if (element
== NULL
)
5460 if (! bfd_check_format (element
, bfd_object
))
5463 undefs_tail
= info
->hash
->undefs_tail
;
5465 if (!(*info
->callbacks
5466 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5468 if (!bfd_link_add_symbols (element
, info
))
5471 /* If there are any new undefined symbols, we need to make
5472 another pass through the archive in order to see whether
5473 they can be defined. FIXME: This isn't perfect, because
5474 common symbols wind up on undefs_tail and because an
5475 undefined symbol which is defined later on in this pass
5476 does not require another pass. This isn't a bug, but it
5477 does make the code less efficient than it could be. */
5478 if (undefs_tail
!= info
->hash
->undefs_tail
)
5481 /* Look backward to mark all symbols from this object file
5482 which we have already seen in this pass. */
5486 included
[mark
] = TRUE
;
5491 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5493 /* We mark subsequent symbols from this object file as we go
5494 on through the loop. */
5495 last
= symdef
->file_offset
;
5505 if (included
!= NULL
)
5510 /* Given an ELF BFD, add symbols to the global hash table as
5514 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5516 switch (bfd_get_format (abfd
))
5519 return elf_link_add_object_symbols (abfd
, info
);
5521 return elf_link_add_archive_symbols (abfd
, info
);
5523 bfd_set_error (bfd_error_wrong_format
);
5528 struct hash_codes_info
5530 unsigned long *hashcodes
;
5534 /* This function will be called though elf_link_hash_traverse to store
5535 all hash value of the exported symbols in an array. */
5538 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5540 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
5545 /* Ignore indirect symbols. These are added by the versioning code. */
5546 if (h
->dynindx
== -1)
5549 name
= h
->root
.root
.string
;
5550 if (h
->versioned
>= versioned
)
5552 char *p
= strchr (name
, ELF_VER_CHR
);
5555 alc
= (char *) bfd_malloc (p
- name
+ 1);
5561 memcpy (alc
, name
, p
- name
);
5562 alc
[p
- name
] = '\0';
5567 /* Compute the hash value. */
5568 ha
= bfd_elf_hash (name
);
5570 /* Store the found hash value in the array given as the argument. */
5571 *(inf
->hashcodes
)++ = ha
;
5573 /* And store it in the struct so that we can put it in the hash table
5575 h
->u
.elf_hash_value
= ha
;
5583 struct collect_gnu_hash_codes
5586 const struct elf_backend_data
*bed
;
5587 unsigned long int nsyms
;
5588 unsigned long int maskbits
;
5589 unsigned long int *hashcodes
;
5590 unsigned long int *hashval
;
5591 unsigned long int *indx
;
5592 unsigned long int *counts
;
5595 long int min_dynindx
;
5596 unsigned long int bucketcount
;
5597 unsigned long int symindx
;
5598 long int local_indx
;
5599 long int shift1
, shift2
;
5600 unsigned long int mask
;
5604 /* This function will be called though elf_link_hash_traverse to store
5605 all hash value of the exported symbols in an array. */
5608 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5610 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5615 /* Ignore indirect symbols. These are added by the versioning code. */
5616 if (h
->dynindx
== -1)
5619 /* Ignore also local symbols and undefined symbols. */
5620 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5623 name
= h
->root
.root
.string
;
5624 if (h
->versioned
>= versioned
)
5626 char *p
= strchr (name
, ELF_VER_CHR
);
5629 alc
= (char *) bfd_malloc (p
- name
+ 1);
5635 memcpy (alc
, name
, p
- name
);
5636 alc
[p
- name
] = '\0';
5641 /* Compute the hash value. */
5642 ha
= bfd_elf_gnu_hash (name
);
5644 /* Store the found hash value in the array for compute_bucket_count,
5645 and also for .dynsym reordering purposes. */
5646 s
->hashcodes
[s
->nsyms
] = ha
;
5647 s
->hashval
[h
->dynindx
] = ha
;
5649 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5650 s
->min_dynindx
= h
->dynindx
;
5658 /* This function will be called though elf_link_hash_traverse to do
5659 final dynaminc symbol renumbering. */
5662 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5664 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5665 unsigned long int bucket
;
5666 unsigned long int val
;
5668 /* Ignore indirect symbols. */
5669 if (h
->dynindx
== -1)
5672 /* Ignore also local symbols and undefined symbols. */
5673 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5675 if (h
->dynindx
>= s
->min_dynindx
)
5676 h
->dynindx
= s
->local_indx
++;
5680 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5681 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5682 & ((s
->maskbits
>> s
->shift1
) - 1);
5683 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5685 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5686 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5687 if (s
->counts
[bucket
] == 1)
5688 /* Last element terminates the chain. */
5690 bfd_put_32 (s
->output_bfd
, val
,
5691 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5692 --s
->counts
[bucket
];
5693 h
->dynindx
= s
->indx
[bucket
]++;
5697 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5700 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5702 return !(h
->forced_local
5703 || h
->root
.type
== bfd_link_hash_undefined
5704 || h
->root
.type
== bfd_link_hash_undefweak
5705 || ((h
->root
.type
== bfd_link_hash_defined
5706 || h
->root
.type
== bfd_link_hash_defweak
)
5707 && h
->root
.u
.def
.section
->output_section
== NULL
));
5710 /* Array used to determine the number of hash table buckets to use
5711 based on the number of symbols there are. If there are fewer than
5712 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5713 fewer than 37 we use 17 buckets, and so forth. We never use more
5714 than 32771 buckets. */
5716 static const size_t elf_buckets
[] =
5718 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5722 /* Compute bucket count for hashing table. We do not use a static set
5723 of possible tables sizes anymore. Instead we determine for all
5724 possible reasonable sizes of the table the outcome (i.e., the
5725 number of collisions etc) and choose the best solution. The
5726 weighting functions are not too simple to allow the table to grow
5727 without bounds. Instead one of the weighting factors is the size.
5728 Therefore the result is always a good payoff between few collisions
5729 (= short chain lengths) and table size. */
5731 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
5732 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
5733 unsigned long int nsyms
,
5736 size_t best_size
= 0;
5737 unsigned long int i
;
5739 /* We have a problem here. The following code to optimize the table
5740 size requires an integer type with more the 32 bits. If
5741 BFD_HOST_U_64_BIT is set we know about such a type. */
5742 #ifdef BFD_HOST_U_64_BIT
5747 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5748 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5749 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5750 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5751 unsigned long int *counts
;
5753 unsigned int no_improvement_count
= 0;
5755 /* Possible optimization parameters: if we have NSYMS symbols we say
5756 that the hashing table must at least have NSYMS/4 and at most
5758 minsize
= nsyms
/ 4;
5761 best_size
= maxsize
= nsyms
* 2;
5766 if ((best_size
& 31) == 0)
5770 /* Create array where we count the collisions in. We must use bfd_malloc
5771 since the size could be large. */
5773 amt
*= sizeof (unsigned long int);
5774 counts
= (unsigned long int *) bfd_malloc (amt
);
5778 /* Compute the "optimal" size for the hash table. The criteria is a
5779 minimal chain length. The minor criteria is (of course) the size
5781 for (i
= minsize
; i
< maxsize
; ++i
)
5783 /* Walk through the array of hashcodes and count the collisions. */
5784 BFD_HOST_U_64_BIT max
;
5785 unsigned long int j
;
5786 unsigned long int fact
;
5788 if (gnu_hash
&& (i
& 31) == 0)
5791 memset (counts
, '\0', i
* sizeof (unsigned long int));
5793 /* Determine how often each hash bucket is used. */
5794 for (j
= 0; j
< nsyms
; ++j
)
5795 ++counts
[hashcodes
[j
] % i
];
5797 /* For the weight function we need some information about the
5798 pagesize on the target. This is information need not be 100%
5799 accurate. Since this information is not available (so far) we
5800 define it here to a reasonable default value. If it is crucial
5801 to have a better value some day simply define this value. */
5802 # ifndef BFD_TARGET_PAGESIZE
5803 # define BFD_TARGET_PAGESIZE (4096)
5806 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5808 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5811 /* Variant 1: optimize for short chains. We add the squares
5812 of all the chain lengths (which favors many small chain
5813 over a few long chains). */
5814 for (j
= 0; j
< i
; ++j
)
5815 max
+= counts
[j
] * counts
[j
];
5817 /* This adds penalties for the overall size of the table. */
5818 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5821 /* Variant 2: Optimize a lot more for small table. Here we
5822 also add squares of the size but we also add penalties for
5823 empty slots (the +1 term). */
5824 for (j
= 0; j
< i
; ++j
)
5825 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5827 /* The overall size of the table is considered, but not as
5828 strong as in variant 1, where it is squared. */
5829 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5833 /* Compare with current best results. */
5834 if (max
< best_chlen
)
5838 no_improvement_count
= 0;
5840 /* PR 11843: Avoid futile long searches for the best bucket size
5841 when there are a large number of symbols. */
5842 else if (++no_improvement_count
== 100)
5849 #endif /* defined (BFD_HOST_U_64_BIT) */
5851 /* This is the fallback solution if no 64bit type is available or if we
5852 are not supposed to spend much time on optimizations. We select the
5853 bucket count using a fixed set of numbers. */
5854 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5856 best_size
= elf_buckets
[i
];
5857 if (nsyms
< elf_buckets
[i
+ 1])
5860 if (gnu_hash
&& best_size
< 2)
5867 /* Size any SHT_GROUP section for ld -r. */
5870 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
5875 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
5876 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
5877 && (s
= ibfd
->sections
) != NULL
5878 && s
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
5879 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
5884 /* Set a default stack segment size. The value in INFO wins. If it
5885 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5886 undefined it is initialized. */
5889 bfd_elf_stack_segment_size (bfd
*output_bfd
,
5890 struct bfd_link_info
*info
,
5891 const char *legacy_symbol
,
5892 bfd_vma default_size
)
5894 struct elf_link_hash_entry
*h
= NULL
;
5896 /* Look for legacy symbol. */
5898 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
5899 FALSE
, FALSE
, FALSE
);
5900 if (h
&& (h
->root
.type
== bfd_link_hash_defined
5901 || h
->root
.type
== bfd_link_hash_defweak
)
5903 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
5905 /* The symbol has no type if specified on the command line. */
5906 h
->type
= STT_OBJECT
;
5907 if (info
->stacksize
)
5908 /* xgettext:c-format */
5909 _bfd_error_handler (_("%B: stack size specified and %s set"),
5910 output_bfd
, legacy_symbol
);
5911 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
5912 /* xgettext:c-format */
5913 _bfd_error_handler (_("%B: %s not absolute"),
5914 output_bfd
, legacy_symbol
);
5916 info
->stacksize
= h
->root
.u
.def
.value
;
5919 if (!info
->stacksize
)
5920 /* If the user didn't set a size, or explicitly inhibit the
5921 size, set it now. */
5922 info
->stacksize
= default_size
;
5924 /* Provide the legacy symbol, if it is referenced. */
5925 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
5926 || h
->root
.type
== bfd_link_hash_undefweak
))
5928 struct bfd_link_hash_entry
*bh
= NULL
;
5930 if (!(_bfd_generic_link_add_one_symbol
5931 (info
, output_bfd
, legacy_symbol
,
5932 BSF_GLOBAL
, bfd_abs_section_ptr
,
5933 info
->stacksize
>= 0 ? info
->stacksize
: 0,
5934 NULL
, FALSE
, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
5937 h
= (struct elf_link_hash_entry
*) bh
;
5939 h
->type
= STT_OBJECT
;
5945 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5947 struct elf_gc_sweep_symbol_info
5949 struct bfd_link_info
*info
;
5950 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
5955 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
5958 && (((h
->root
.type
== bfd_link_hash_defined
5959 || h
->root
.type
== bfd_link_hash_defweak
)
5960 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
5961 && h
->root
.u
.def
.section
->gc_mark
))
5962 || h
->root
.type
== bfd_link_hash_undefined
5963 || h
->root
.type
== bfd_link_hash_undefweak
))
5965 struct elf_gc_sweep_symbol_info
*inf
;
5967 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
5968 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
5971 h
->ref_regular_nonweak
= 0;
5977 /* Set up the sizes and contents of the ELF dynamic sections. This is
5978 called by the ELF linker emulation before_allocation routine. We
5979 must set the sizes of the sections before the linker sets the
5980 addresses of the various sections. */
5983 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5986 const char *filter_shlib
,
5988 const char *depaudit
,
5989 const char * const *auxiliary_filters
,
5990 struct bfd_link_info
*info
,
5991 asection
**sinterpptr
)
5994 const struct elf_backend_data
*bed
;
5998 if (!is_elf_hash_table (info
->hash
))
6001 dynobj
= elf_hash_table (info
)->dynobj
;
6003 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6005 struct bfd_elf_version_tree
*verdefs
;
6006 struct elf_info_failed asvinfo
;
6007 struct bfd_elf_version_tree
*t
;
6008 struct bfd_elf_version_expr
*d
;
6012 /* If we are supposed to export all symbols into the dynamic symbol
6013 table (this is not the normal case), then do so. */
6014 if (info
->export_dynamic
6015 || (bfd_link_executable (info
) && info
->dynamic
))
6017 struct elf_info_failed eif
;
6021 elf_link_hash_traverse (elf_hash_table (info
),
6022 _bfd_elf_export_symbol
,
6030 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6032 if (soname_indx
== (size_t) -1
6033 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6037 soname_indx
= (size_t) -1;
6039 /* Make all global versions with definition. */
6040 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6041 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6042 if (!d
->symver
&& d
->literal
)
6044 const char *verstr
, *name
;
6045 size_t namelen
, verlen
, newlen
;
6046 char *newname
, *p
, leading_char
;
6047 struct elf_link_hash_entry
*newh
;
6049 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6051 namelen
= strlen (name
) + (leading_char
!= '\0');
6053 verlen
= strlen (verstr
);
6054 newlen
= namelen
+ verlen
+ 3;
6056 newname
= (char *) bfd_malloc (newlen
);
6057 if (newname
== NULL
)
6059 newname
[0] = leading_char
;
6060 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6062 /* Check the hidden versioned definition. */
6063 p
= newname
+ namelen
;
6065 memcpy (p
, verstr
, verlen
+ 1);
6066 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6067 newname
, FALSE
, FALSE
,
6070 || (newh
->root
.type
!= bfd_link_hash_defined
6071 && newh
->root
.type
!= bfd_link_hash_defweak
))
6073 /* Check the default versioned definition. */
6075 memcpy (p
, verstr
, verlen
+ 1);
6076 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6077 newname
, FALSE
, FALSE
,
6082 /* Mark this version if there is a definition and it is
6083 not defined in a shared object. */
6085 && !newh
->def_dynamic
6086 && (newh
->root
.type
== bfd_link_hash_defined
6087 || newh
->root
.type
== bfd_link_hash_defweak
))
6091 /* Attach all the symbols to their version information. */
6092 asvinfo
.info
= info
;
6093 asvinfo
.failed
= FALSE
;
6095 elf_link_hash_traverse (elf_hash_table (info
),
6096 _bfd_elf_link_assign_sym_version
,
6101 if (!info
->allow_undefined_version
)
6103 /* Check if all global versions have a definition. */
6104 bfd_boolean all_defined
= TRUE
;
6105 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6106 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6107 if (d
->literal
&& !d
->symver
&& !d
->script
)
6110 (_("%s: undefined version: %s"),
6111 d
->pattern
, t
->name
);
6112 all_defined
= FALSE
;
6117 bfd_set_error (bfd_error_bad_value
);
6122 /* Set up the version definition section. */
6123 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6124 BFD_ASSERT (s
!= NULL
);
6126 /* We may have created additional version definitions if we are
6127 just linking a regular application. */
6128 verdefs
= info
->version_info
;
6130 /* Skip anonymous version tag. */
6131 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6132 verdefs
= verdefs
->next
;
6134 if (verdefs
== NULL
&& !info
->create_default_symver
)
6135 s
->flags
|= SEC_EXCLUDE
;
6141 Elf_Internal_Verdef def
;
6142 Elf_Internal_Verdaux defaux
;
6143 struct bfd_link_hash_entry
*bh
;
6144 struct elf_link_hash_entry
*h
;
6150 /* Make space for the base version. */
6151 size
+= sizeof (Elf_External_Verdef
);
6152 size
+= sizeof (Elf_External_Verdaux
);
6155 /* Make space for the default version. */
6156 if (info
->create_default_symver
)
6158 size
+= sizeof (Elf_External_Verdef
);
6162 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6164 struct bfd_elf_version_deps
*n
;
6166 /* Don't emit base version twice. */
6170 size
+= sizeof (Elf_External_Verdef
);
6171 size
+= sizeof (Elf_External_Verdaux
);
6174 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6175 size
+= sizeof (Elf_External_Verdaux
);
6179 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6180 if (s
->contents
== NULL
&& s
->size
!= 0)
6183 /* Fill in the version definition section. */
6187 def
.vd_version
= VER_DEF_CURRENT
;
6188 def
.vd_flags
= VER_FLG_BASE
;
6191 if (info
->create_default_symver
)
6193 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6194 def
.vd_next
= sizeof (Elf_External_Verdef
);
6198 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6199 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6200 + sizeof (Elf_External_Verdaux
));
6203 if (soname_indx
!= (size_t) -1)
6205 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6207 def
.vd_hash
= bfd_elf_hash (soname
);
6208 defaux
.vda_name
= soname_indx
;
6215 name
= lbasename (output_bfd
->filename
);
6216 def
.vd_hash
= bfd_elf_hash (name
);
6217 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6219 if (indx
== (size_t) -1)
6221 defaux
.vda_name
= indx
;
6223 defaux
.vda_next
= 0;
6225 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6226 (Elf_External_Verdef
*) p
);
6227 p
+= sizeof (Elf_External_Verdef
);
6228 if (info
->create_default_symver
)
6230 /* Add a symbol representing this version. */
6232 if (! (_bfd_generic_link_add_one_symbol
6233 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6235 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6237 h
= (struct elf_link_hash_entry
*) bh
;
6240 h
->type
= STT_OBJECT
;
6241 h
->verinfo
.vertree
= NULL
;
6243 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6246 /* Create a duplicate of the base version with the same
6247 aux block, but different flags. */
6250 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6252 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6253 + sizeof (Elf_External_Verdaux
));
6256 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6257 (Elf_External_Verdef
*) p
);
6258 p
+= sizeof (Elf_External_Verdef
);
6260 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6261 (Elf_External_Verdaux
*) p
);
6262 p
+= sizeof (Elf_External_Verdaux
);
6264 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6267 struct bfd_elf_version_deps
*n
;
6269 /* Don't emit the base version twice. */
6274 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6277 /* Add a symbol representing this version. */
6279 if (! (_bfd_generic_link_add_one_symbol
6280 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6282 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6284 h
= (struct elf_link_hash_entry
*) bh
;
6287 h
->type
= STT_OBJECT
;
6288 h
->verinfo
.vertree
= t
;
6290 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6293 def
.vd_version
= VER_DEF_CURRENT
;
6295 if (t
->globals
.list
== NULL
6296 && t
->locals
.list
== NULL
6298 def
.vd_flags
|= VER_FLG_WEAK
;
6299 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6300 def
.vd_cnt
= cdeps
+ 1;
6301 def
.vd_hash
= bfd_elf_hash (t
->name
);
6302 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6305 /* If a basever node is next, it *must* be the last node in
6306 the chain, otherwise Verdef construction breaks. */
6307 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6308 BFD_ASSERT (t
->next
->next
== NULL
);
6310 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6311 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6312 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6314 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6315 (Elf_External_Verdef
*) p
);
6316 p
+= sizeof (Elf_External_Verdef
);
6318 defaux
.vda_name
= h
->dynstr_index
;
6319 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6321 defaux
.vda_next
= 0;
6322 if (t
->deps
!= NULL
)
6323 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6324 t
->name_indx
= defaux
.vda_name
;
6326 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6327 (Elf_External_Verdaux
*) p
);
6328 p
+= sizeof (Elf_External_Verdaux
);
6330 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6332 if (n
->version_needed
== NULL
)
6334 /* This can happen if there was an error in the
6336 defaux
.vda_name
= 0;
6340 defaux
.vda_name
= n
->version_needed
->name_indx
;
6341 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6344 if (n
->next
== NULL
)
6345 defaux
.vda_next
= 0;
6347 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6349 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6350 (Elf_External_Verdaux
*) p
);
6351 p
+= sizeof (Elf_External_Verdaux
);
6355 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6359 bed
= get_elf_backend_data (output_bfd
);
6361 if (info
->gc_sections
&& bed
->can_gc_sections
)
6363 struct elf_gc_sweep_symbol_info sweep_info
;
6365 /* Remove the symbols that were in the swept sections from the
6366 dynamic symbol table. */
6367 sweep_info
.info
= info
;
6368 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
6369 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
6373 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6376 struct elf_find_verdep_info sinfo
;
6378 /* Work out the size of the version reference section. */
6380 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6381 BFD_ASSERT (s
!= NULL
);
6384 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6385 if (sinfo
.vers
== 0)
6387 sinfo
.failed
= FALSE
;
6389 elf_link_hash_traverse (elf_hash_table (info
),
6390 _bfd_elf_link_find_version_dependencies
,
6395 if (elf_tdata (output_bfd
)->verref
== NULL
)
6396 s
->flags
|= SEC_EXCLUDE
;
6399 Elf_Internal_Verneed
*vn
;
6404 /* Build the version dependency section. */
6407 for (vn
= elf_tdata (output_bfd
)->verref
;
6409 vn
= vn
->vn_nextref
)
6411 Elf_Internal_Vernaux
*a
;
6413 size
+= sizeof (Elf_External_Verneed
);
6415 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6416 size
+= sizeof (Elf_External_Vernaux
);
6420 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6421 if (s
->contents
== NULL
)
6425 for (vn
= elf_tdata (output_bfd
)->verref
;
6427 vn
= vn
->vn_nextref
)
6430 Elf_Internal_Vernaux
*a
;
6434 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6437 vn
->vn_version
= VER_NEED_CURRENT
;
6439 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6440 elf_dt_name (vn
->vn_bfd
) != NULL
6441 ? elf_dt_name (vn
->vn_bfd
)
6442 : lbasename (vn
->vn_bfd
->filename
),
6444 if (indx
== (size_t) -1)
6447 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
6448 if (vn
->vn_nextref
== NULL
)
6451 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
6452 + caux
* sizeof (Elf_External_Vernaux
));
6454 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
6455 (Elf_External_Verneed
*) p
);
6456 p
+= sizeof (Elf_External_Verneed
);
6458 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6460 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6461 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6462 a
->vna_nodename
, FALSE
);
6463 if (indx
== (size_t) -1)
6466 if (a
->vna_nextptr
== NULL
)
6469 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6471 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6472 (Elf_External_Vernaux
*) p
);
6473 p
+= sizeof (Elf_External_Vernaux
);
6477 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6481 /* Any syms created from now on start with -1 in
6482 got.refcount/offset and plt.refcount/offset. */
6483 elf_hash_table (info
)->init_got_refcount
6484 = elf_hash_table (info
)->init_got_offset
;
6485 elf_hash_table (info
)->init_plt_refcount
6486 = elf_hash_table (info
)->init_plt_offset
;
6488 if (bfd_link_relocatable (info
)
6489 && !_bfd_elf_size_group_sections (info
))
6492 /* The backend may have to create some sections regardless of whether
6493 we're dynamic or not. */
6494 if (bed
->elf_backend_always_size_sections
6495 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
6498 /* Determine any GNU_STACK segment requirements, after the backend
6499 has had a chance to set a default segment size. */
6500 if (info
->execstack
)
6501 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
6502 else if (info
->noexecstack
)
6503 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
6507 asection
*notesec
= NULL
;
6510 for (inputobj
= info
->input_bfds
;
6512 inputobj
= inputobj
->link
.next
)
6517 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
6519 s
= inputobj
->sections
;
6520 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
6523 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
6526 if (s
->flags
& SEC_CODE
)
6530 else if (bed
->default_execstack
)
6533 if (notesec
|| info
->stacksize
> 0)
6534 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
6535 if (notesec
&& exec
&& bfd_link_relocatable (info
)
6536 && notesec
->output_section
!= bfd_abs_section_ptr
)
6537 notesec
->output_section
->flags
|= SEC_CODE
;
6540 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6542 struct elf_info_failed eif
;
6543 struct elf_link_hash_entry
*h
;
6547 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
6548 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
6552 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
6554 info
->flags
|= DF_SYMBOLIC
;
6562 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
6564 if (indx
== (size_t) -1)
6567 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
6568 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
6572 if (filter_shlib
!= NULL
)
6576 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6577 filter_shlib
, TRUE
);
6578 if (indx
== (size_t) -1
6579 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
6583 if (auxiliary_filters
!= NULL
)
6585 const char * const *p
;
6587 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
6591 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6593 if (indx
== (size_t) -1
6594 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
6603 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
6605 if (indx
== (size_t) -1
6606 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
6610 if (depaudit
!= NULL
)
6614 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
6616 if (indx
== (size_t) -1
6617 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
6624 /* Find all symbols which were defined in a dynamic object and make
6625 the backend pick a reasonable value for them. */
6626 elf_link_hash_traverse (elf_hash_table (info
),
6627 _bfd_elf_adjust_dynamic_symbol
,
6632 /* Add some entries to the .dynamic section. We fill in some of the
6633 values later, in bfd_elf_final_link, but we must add the entries
6634 now so that we know the final size of the .dynamic section. */
6636 /* If there are initialization and/or finalization functions to
6637 call then add the corresponding DT_INIT/DT_FINI entries. */
6638 h
= (info
->init_function
6639 ? elf_link_hash_lookup (elf_hash_table (info
),
6640 info
->init_function
, FALSE
,
6647 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
6650 h
= (info
->fini_function
6651 ? elf_link_hash_lookup (elf_hash_table (info
),
6652 info
->fini_function
, FALSE
,
6659 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
6663 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
6664 if (s
!= NULL
&& s
->linker_has_input
)
6666 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6667 if (! bfd_link_executable (info
))
6672 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
6673 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
6674 && (o
= sub
->sections
) != NULL
6675 && o
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
)
6676 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
6677 if (elf_section_data (o
)->this_hdr
.sh_type
6678 == SHT_PREINIT_ARRAY
)
6681 (_("%B: .preinit_array section is not allowed in DSO"),
6686 bfd_set_error (bfd_error_nonrepresentable_section
);
6690 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
6691 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
6694 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
6695 if (s
!= NULL
&& s
->linker_has_input
)
6697 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
6698 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
6701 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
6702 if (s
!= NULL
&& s
->linker_has_input
)
6704 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
6705 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
6709 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
6710 /* If .dynstr is excluded from the link, we don't want any of
6711 these tags. Strictly, we should be checking each section
6712 individually; This quick check covers for the case where
6713 someone does a /DISCARD/ : { *(*) }. */
6714 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
6716 bfd_size_type strsize
;
6718 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6719 if ((info
->emit_hash
6720 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
6721 || (info
->emit_gnu_hash
6722 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
6723 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
6724 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
6725 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
6726 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
6727 bed
->s
->sizeof_sym
))
6732 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
6735 /* The backend must work out the sizes of all the other dynamic
6738 && bed
->elf_backend_size_dynamic_sections
!= NULL
6739 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
6742 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6744 unsigned long section_sym_count
;
6746 if (elf_tdata (output_bfd
)->cverdefs
)
6748 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
6750 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
6751 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
6755 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
6757 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
6760 else if (info
->flags
& DF_BIND_NOW
)
6762 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
6768 if (bfd_link_executable (info
))
6769 info
->flags_1
&= ~ (DF_1_INITFIRST
6772 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
6776 if (elf_tdata (output_bfd
)->cverrefs
)
6778 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
6780 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
6781 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
6785 if ((elf_tdata (output_bfd
)->cverrefs
== 0
6786 && elf_tdata (output_bfd
)->cverdefs
== 0)
6787 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6788 §ion_sym_count
) <= 1)
6792 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6793 s
->flags
|= SEC_EXCLUDE
;
6799 /* Find the first non-excluded output section. We'll use its
6800 section symbol for some emitted relocs. */
6802 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
6806 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6807 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
6808 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6810 elf_hash_table (info
)->text_index_section
= s
;
6815 /* Find two non-excluded output sections, one for code, one for data.
6816 We'll use their section symbols for some emitted relocs. */
6818 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6822 /* Data first, since setting text_index_section changes
6823 _bfd_elf_link_omit_section_dynsym. */
6824 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6825 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
6826 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6828 elf_hash_table (info
)->data_index_section
= s
;
6832 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6833 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
6834 == (SEC_ALLOC
| SEC_READONLY
))
6835 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6837 elf_hash_table (info
)->text_index_section
= s
;
6841 if (elf_hash_table (info
)->text_index_section
== NULL
)
6842 elf_hash_table (info
)->text_index_section
6843 = elf_hash_table (info
)->data_index_section
;
6847 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6849 const struct elf_backend_data
*bed
;
6850 unsigned long section_sym_count
;
6851 bfd_size_type dynsymcount
= 0;
6853 if (!is_elf_hash_table (info
->hash
))
6856 bed
= get_elf_backend_data (output_bfd
);
6857 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6859 /* Assign dynsym indices. In a shared library we generate a section
6860 symbol for each output section, which come first. Next come all
6861 of the back-end allocated local dynamic syms, followed by the rest
6862 of the global symbols.
6864 This is usually not needed for static binaries, however backends
6865 can request to always do it, e.g. the MIPS backend uses dynamic
6866 symbol counts to lay out GOT, which will be produced in the
6867 presence of GOT relocations even in static binaries (holding fixed
6868 data in that case, to satisfy those relocations). */
6870 if (elf_hash_table (info
)->dynamic_sections_created
6871 || bed
->always_renumber_dynsyms
)
6872 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6873 §ion_sym_count
);
6875 if (elf_hash_table (info
)->dynamic_sections_created
)
6879 unsigned int dtagcount
;
6881 dynobj
= elf_hash_table (info
)->dynobj
;
6883 /* Work out the size of the symbol version section. */
6884 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6885 BFD_ASSERT (s
!= NULL
);
6886 if ((s
->flags
& SEC_EXCLUDE
) == 0)
6888 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6889 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6890 if (s
->contents
== NULL
)
6893 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
6897 /* Set the size of the .dynsym and .hash sections. We counted
6898 the number of dynamic symbols in elf_link_add_object_symbols.
6899 We will build the contents of .dynsym and .hash when we build
6900 the final symbol table, because until then we do not know the
6901 correct value to give the symbols. We built the .dynstr
6902 section as we went along in elf_link_add_object_symbols. */
6903 s
= elf_hash_table (info
)->dynsym
;
6904 BFD_ASSERT (s
!= NULL
);
6905 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
6907 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6908 if (s
->contents
== NULL
)
6911 /* The first entry in .dynsym is a dummy symbol. Clear all the
6912 section syms, in case we don't output them all. */
6913 ++section_sym_count
;
6914 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
6916 elf_hash_table (info
)->bucketcount
= 0;
6918 /* Compute the size of the hashing table. As a side effect this
6919 computes the hash values for all the names we export. */
6920 if (info
->emit_hash
)
6922 unsigned long int *hashcodes
;
6923 struct hash_codes_info hashinf
;
6925 unsigned long int nsyms
;
6927 size_t hash_entry_size
;
6929 /* Compute the hash values for all exported symbols. At the same
6930 time store the values in an array so that we could use them for
6932 amt
= dynsymcount
* sizeof (unsigned long int);
6933 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
6934 if (hashcodes
== NULL
)
6936 hashinf
.hashcodes
= hashcodes
;
6937 hashinf
.error
= FALSE
;
6939 /* Put all hash values in HASHCODES. */
6940 elf_link_hash_traverse (elf_hash_table (info
),
6941 elf_collect_hash_codes
, &hashinf
);
6948 nsyms
= hashinf
.hashcodes
- hashcodes
;
6950 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6953 if (bucketcount
== 0 && nsyms
> 0)
6956 elf_hash_table (info
)->bucketcount
= bucketcount
;
6958 s
= bfd_get_linker_section (dynobj
, ".hash");
6959 BFD_ASSERT (s
!= NULL
);
6960 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6961 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6962 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6963 if (s
->contents
== NULL
)
6966 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6967 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6968 s
->contents
+ hash_entry_size
);
6971 if (info
->emit_gnu_hash
)
6974 unsigned char *contents
;
6975 struct collect_gnu_hash_codes cinfo
;
6979 memset (&cinfo
, 0, sizeof (cinfo
));
6981 /* Compute the hash values for all exported symbols. At the same
6982 time store the values in an array so that we could use them for
6984 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6985 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
6986 if (cinfo
.hashcodes
== NULL
)
6989 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6990 cinfo
.min_dynindx
= -1;
6991 cinfo
.output_bfd
= output_bfd
;
6994 /* Put all hash values in HASHCODES. */
6995 elf_link_hash_traverse (elf_hash_table (info
),
6996 elf_collect_gnu_hash_codes
, &cinfo
);
6999 free (cinfo
.hashcodes
);
7004 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
7006 if (bucketcount
== 0)
7008 free (cinfo
.hashcodes
);
7012 s
= bfd_get_linker_section (dynobj
, ".gnu.hash");
7013 BFD_ASSERT (s
!= NULL
);
7015 if (cinfo
.nsyms
== 0)
7017 /* Empty .gnu.hash section is special. */
7018 BFD_ASSERT (cinfo
.min_dynindx
== -1);
7019 free (cinfo
.hashcodes
);
7020 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
7021 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7022 if (contents
== NULL
)
7024 s
->contents
= contents
;
7025 /* 1 empty bucket. */
7026 bfd_put_32 (output_bfd
, 1, contents
);
7027 /* SYMIDX above the special symbol 0. */
7028 bfd_put_32 (output_bfd
, 1, contents
+ 4);
7029 /* Just one word for bitmask. */
7030 bfd_put_32 (output_bfd
, 1, contents
+ 8);
7031 /* Only hash fn bloom filter. */
7032 bfd_put_32 (output_bfd
, 0, contents
+ 12);
7033 /* No hashes are valid - empty bitmask. */
7034 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
7035 /* No hashes in the only bucket. */
7036 bfd_put_32 (output_bfd
, 0,
7037 contents
+ 16 + bed
->s
->arch_size
/ 8);
7041 unsigned long int maskwords
, maskbitslog2
, x
;
7042 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
7046 while ((x
>>= 1) != 0)
7048 if (maskbitslog2
< 3)
7050 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
7051 maskbitslog2
= maskbitslog2
+ 3;
7053 maskbitslog2
= maskbitslog2
+ 2;
7054 if (bed
->s
->arch_size
== 64)
7056 if (maskbitslog2
== 5)
7062 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
7063 cinfo
.shift2
= maskbitslog2
;
7064 cinfo
.maskbits
= 1 << maskbitslog2
;
7065 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
7066 amt
= bucketcount
* sizeof (unsigned long int) * 2;
7067 amt
+= maskwords
* sizeof (bfd_vma
);
7068 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
7069 if (cinfo
.bitmask
== NULL
)
7071 free (cinfo
.hashcodes
);
7075 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
7076 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
7077 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
7078 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
7080 /* Determine how often each hash bucket is used. */
7081 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
7082 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
7083 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
7085 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
7086 if (cinfo
.counts
[i
] != 0)
7088 cinfo
.indx
[i
] = cnt
;
7089 cnt
+= cinfo
.counts
[i
];
7091 BFD_ASSERT (cnt
== dynsymcount
);
7092 cinfo
.bucketcount
= bucketcount
;
7093 cinfo
.local_indx
= cinfo
.min_dynindx
;
7095 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
7096 s
->size
+= cinfo
.maskbits
/ 8;
7097 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7098 if (contents
== NULL
)
7100 free (cinfo
.bitmask
);
7101 free (cinfo
.hashcodes
);
7105 s
->contents
= contents
;
7106 bfd_put_32 (output_bfd
, bucketcount
, contents
);
7107 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
7108 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
7109 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
7110 contents
+= 16 + cinfo
.maskbits
/ 8;
7112 for (i
= 0; i
< bucketcount
; ++i
)
7114 if (cinfo
.counts
[i
] == 0)
7115 bfd_put_32 (output_bfd
, 0, contents
);
7117 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
7121 cinfo
.contents
= contents
;
7123 /* Renumber dynamic symbols, populate .gnu.hash section. */
7124 elf_link_hash_traverse (elf_hash_table (info
),
7125 elf_renumber_gnu_hash_syms
, &cinfo
);
7127 contents
= s
->contents
+ 16;
7128 for (i
= 0; i
< maskwords
; ++i
)
7130 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
7132 contents
+= bed
->s
->arch_size
/ 8;
7135 free (cinfo
.bitmask
);
7136 free (cinfo
.hashcodes
);
7140 s
= bfd_get_linker_section (dynobj
, ".dynstr");
7141 BFD_ASSERT (s
!= NULL
);
7143 elf_finalize_dynstr (output_bfd
, info
);
7145 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7147 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
7148 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
7155 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7158 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
7161 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
7162 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
7165 /* Finish SHF_MERGE section merging. */
7168 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
7173 if (!is_elf_hash_table (info
->hash
))
7176 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
7177 if ((ibfd
->flags
& DYNAMIC
) == 0
7178 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
7179 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
7180 == get_elf_backend_data (obfd
)->s
->elfclass
))
7181 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7182 if ((sec
->flags
& SEC_MERGE
) != 0
7183 && !bfd_is_abs_section (sec
->output_section
))
7185 struct bfd_elf_section_data
*secdata
;
7187 secdata
= elf_section_data (sec
);
7188 if (! _bfd_add_merge_section (obfd
,
7189 &elf_hash_table (info
)->merge_info
,
7190 sec
, &secdata
->sec_info
))
7192 else if (secdata
->sec_info
)
7193 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7196 if (elf_hash_table (info
)->merge_info
!= NULL
)
7197 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7198 merge_sections_remove_hook
);
7202 /* Create an entry in an ELF linker hash table. */
7204 struct bfd_hash_entry
*
7205 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7206 struct bfd_hash_table
*table
,
7209 /* Allocate the structure if it has not already been allocated by a
7213 entry
= (struct bfd_hash_entry
*)
7214 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7219 /* Call the allocation method of the superclass. */
7220 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7223 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7224 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7226 /* Set local fields. */
7229 ret
->got
= htab
->init_got_refcount
;
7230 ret
->plt
= htab
->init_plt_refcount
;
7231 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7232 - offsetof (struct elf_link_hash_entry
, size
)));
7233 /* Assume that we have been called by a non-ELF symbol reader.
7234 This flag is then reset by the code which reads an ELF input
7235 file. This ensures that a symbol created by a non-ELF symbol
7236 reader will have the flag set correctly. */
7243 /* Copy data from an indirect symbol to its direct symbol, hiding the
7244 old indirect symbol. Also used for copying flags to a weakdef. */
7247 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7248 struct elf_link_hash_entry
*dir
,
7249 struct elf_link_hash_entry
*ind
)
7251 struct elf_link_hash_table
*htab
;
7253 /* Copy down any references that we may have already seen to the
7254 symbol which just became indirect. */
7256 if (dir
->versioned
!= versioned_hidden
)
7257 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7258 dir
->ref_regular
|= ind
->ref_regular
;
7259 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7260 dir
->non_got_ref
|= ind
->non_got_ref
;
7261 dir
->needs_plt
|= ind
->needs_plt
;
7262 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7264 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7267 /* Copy over the global and procedure linkage table refcount entries.
7268 These may have been already set up by a check_relocs routine. */
7269 htab
= elf_hash_table (info
);
7270 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7272 if (dir
->got
.refcount
< 0)
7273 dir
->got
.refcount
= 0;
7274 dir
->got
.refcount
+= ind
->got
.refcount
;
7275 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7278 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7280 if (dir
->plt
.refcount
< 0)
7281 dir
->plt
.refcount
= 0;
7282 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7283 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7286 if (ind
->dynindx
!= -1)
7288 if (dir
->dynindx
!= -1)
7289 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7290 dir
->dynindx
= ind
->dynindx
;
7291 dir
->dynstr_index
= ind
->dynstr_index
;
7293 ind
->dynstr_index
= 0;
7298 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7299 struct elf_link_hash_entry
*h
,
7300 bfd_boolean force_local
)
7302 /* STT_GNU_IFUNC symbol must go through PLT. */
7303 if (h
->type
!= STT_GNU_IFUNC
)
7305 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7310 h
->forced_local
= 1;
7311 if (h
->dynindx
!= -1)
7313 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7316 h
->dynstr_index
= 0;
7321 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7325 _bfd_elf_link_hash_table_init
7326 (struct elf_link_hash_table
*table
,
7328 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
7329 struct bfd_hash_table
*,
7331 unsigned int entsize
,
7332 enum elf_target_id target_id
)
7335 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
7337 table
->init_got_refcount
.refcount
= can_refcount
- 1;
7338 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
7339 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
7340 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
7341 /* The first dynamic symbol is a dummy. */
7342 table
->dynsymcount
= 1;
7344 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
7346 table
->root
.type
= bfd_link_elf_hash_table
;
7347 table
->hash_table_id
= target_id
;
7352 /* Create an ELF linker hash table. */
7354 struct bfd_link_hash_table
*
7355 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7357 struct elf_link_hash_table
*ret
;
7358 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
7360 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7364 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7365 sizeof (struct elf_link_hash_entry
),
7371 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7376 /* Destroy an ELF linker hash table. */
7379 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7381 struct elf_link_hash_table
*htab
;
7383 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7384 if (htab
->dynstr
!= NULL
)
7385 _bfd_elf_strtab_free (htab
->dynstr
);
7386 _bfd_merge_sections_free (htab
->merge_info
);
7387 _bfd_generic_link_hash_table_free (obfd
);
7390 /* This is a hook for the ELF emulation code in the generic linker to
7391 tell the backend linker what file name to use for the DT_NEEDED
7392 entry for a dynamic object. */
7395 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7397 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7398 && bfd_get_format (abfd
) == bfd_object
)
7399 elf_dt_name (abfd
) = name
;
7403 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
7406 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7407 && bfd_get_format (abfd
) == bfd_object
)
7408 lib_class
= elf_dyn_lib_class (abfd
);
7415 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
7417 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7418 && bfd_get_format (abfd
) == bfd_object
)
7419 elf_dyn_lib_class (abfd
) = lib_class
;
7422 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7423 the linker ELF emulation code. */
7425 struct bfd_link_needed_list
*
7426 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7427 struct bfd_link_info
*info
)
7429 if (! is_elf_hash_table (info
->hash
))
7431 return elf_hash_table (info
)->needed
;
7434 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7435 hook for the linker ELF emulation code. */
7437 struct bfd_link_needed_list
*
7438 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7439 struct bfd_link_info
*info
)
7441 if (! is_elf_hash_table (info
->hash
))
7443 return elf_hash_table (info
)->runpath
;
7446 /* Get the name actually used for a dynamic object for a link. This
7447 is the SONAME entry if there is one. Otherwise, it is the string
7448 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7451 bfd_elf_get_dt_soname (bfd
*abfd
)
7453 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7454 && bfd_get_format (abfd
) == bfd_object
)
7455 return elf_dt_name (abfd
);
7459 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7460 the ELF linker emulation code. */
7463 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
7464 struct bfd_link_needed_list
**pneeded
)
7467 bfd_byte
*dynbuf
= NULL
;
7468 unsigned int elfsec
;
7469 unsigned long shlink
;
7470 bfd_byte
*extdyn
, *extdynend
;
7472 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
7476 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
7477 || bfd_get_format (abfd
) != bfd_object
)
7480 s
= bfd_get_section_by_name (abfd
, ".dynamic");
7481 if (s
== NULL
|| s
->size
== 0)
7484 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
7487 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
7488 if (elfsec
== SHN_BAD
)
7491 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
7493 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
7494 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
7497 extdynend
= extdyn
+ s
->size
;
7498 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
7500 Elf_Internal_Dyn dyn
;
7502 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
7504 if (dyn
.d_tag
== DT_NULL
)
7507 if (dyn
.d_tag
== DT_NEEDED
)
7510 struct bfd_link_needed_list
*l
;
7511 unsigned int tagv
= dyn
.d_un
.d_val
;
7514 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
7519 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
7540 struct elf_symbuf_symbol
7542 unsigned long st_name
; /* Symbol name, index in string tbl */
7543 unsigned char st_info
; /* Type and binding attributes */
7544 unsigned char st_other
; /* Visibilty, and target specific */
7547 struct elf_symbuf_head
7549 struct elf_symbuf_symbol
*ssym
;
7551 unsigned int st_shndx
;
7558 Elf_Internal_Sym
*isym
;
7559 struct elf_symbuf_symbol
*ssym
;
7564 /* Sort references to symbols by ascending section number. */
7567 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
7569 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
7570 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
7572 return s1
->st_shndx
- s2
->st_shndx
;
7576 elf_sym_name_compare (const void *arg1
, const void *arg2
)
7578 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
7579 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
7580 return strcmp (s1
->name
, s2
->name
);
7583 static struct elf_symbuf_head
*
7584 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
7586 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
7587 struct elf_symbuf_symbol
*ssym
;
7588 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
7589 size_t i
, shndx_count
, total_size
;
7591 indbuf
= (Elf_Internal_Sym
**) bfd_malloc2 (symcount
, sizeof (*indbuf
));
7595 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
7596 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
7597 *ind
++ = &isymbuf
[i
];
7600 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
7601 elf_sort_elf_symbol
);
7604 if (indbufend
> indbuf
)
7605 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
7606 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
7609 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
7610 + (indbufend
- indbuf
) * sizeof (*ssym
));
7611 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
7612 if (ssymbuf
== NULL
)
7618 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
7619 ssymbuf
->ssym
= NULL
;
7620 ssymbuf
->count
= shndx_count
;
7621 ssymbuf
->st_shndx
= 0;
7622 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
7624 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
7627 ssymhead
->ssym
= ssym
;
7628 ssymhead
->count
= 0;
7629 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
7631 ssym
->st_name
= (*ind
)->st_name
;
7632 ssym
->st_info
= (*ind
)->st_info
;
7633 ssym
->st_other
= (*ind
)->st_other
;
7636 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
7637 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
7644 /* Check if 2 sections define the same set of local and global
7648 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
7649 struct bfd_link_info
*info
)
7652 const struct elf_backend_data
*bed1
, *bed2
;
7653 Elf_Internal_Shdr
*hdr1
, *hdr2
;
7654 size_t symcount1
, symcount2
;
7655 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
7656 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
7657 Elf_Internal_Sym
*isym
, *isymend
;
7658 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
7659 size_t count1
, count2
, i
;
7660 unsigned int shndx1
, shndx2
;
7666 /* Both sections have to be in ELF. */
7667 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
7668 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
7671 if (elf_section_type (sec1
) != elf_section_type (sec2
))
7674 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
7675 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
7676 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
7679 bed1
= get_elf_backend_data (bfd1
);
7680 bed2
= get_elf_backend_data (bfd2
);
7681 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
7682 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
7683 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
7684 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
7686 if (symcount1
== 0 || symcount2
== 0)
7692 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
7693 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
7695 if (ssymbuf1
== NULL
)
7697 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
7699 if (isymbuf1
== NULL
)
7702 if (!info
->reduce_memory_overheads
)
7703 elf_tdata (bfd1
)->symbuf
= ssymbuf1
7704 = elf_create_symbuf (symcount1
, isymbuf1
);
7707 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
7709 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
7711 if (isymbuf2
== NULL
)
7714 if (ssymbuf1
!= NULL
&& !info
->reduce_memory_overheads
)
7715 elf_tdata (bfd2
)->symbuf
= ssymbuf2
7716 = elf_create_symbuf (symcount2
, isymbuf2
);
7719 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
7721 /* Optimized faster version. */
7723 struct elf_symbol
*symp
;
7724 struct elf_symbuf_symbol
*ssym
, *ssymend
;
7727 hi
= ssymbuf1
->count
;
7732 mid
= (lo
+ hi
) / 2;
7733 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
7735 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
7739 count1
= ssymbuf1
[mid
].count
;
7746 hi
= ssymbuf2
->count
;
7751 mid
= (lo
+ hi
) / 2;
7752 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
7754 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
7758 count2
= ssymbuf2
[mid
].count
;
7764 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7768 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
7770 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
7771 if (symtable1
== NULL
|| symtable2
== NULL
)
7775 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
7776 ssym
< ssymend
; ssym
++, symp
++)
7778 symp
->u
.ssym
= ssym
;
7779 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
7785 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
7786 ssym
< ssymend
; ssym
++, symp
++)
7788 symp
->u
.ssym
= ssym
;
7789 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
7794 /* Sort symbol by name. */
7795 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7796 elf_sym_name_compare
);
7797 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7798 elf_sym_name_compare
);
7800 for (i
= 0; i
< count1
; i
++)
7801 /* Two symbols must have the same binding, type and name. */
7802 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
7803 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
7804 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7811 symtable1
= (struct elf_symbol
*)
7812 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
7813 symtable2
= (struct elf_symbol
*)
7814 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
7815 if (symtable1
== NULL
|| symtable2
== NULL
)
7818 /* Count definitions in the section. */
7820 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
7821 if (isym
->st_shndx
== shndx1
)
7822 symtable1
[count1
++].u
.isym
= isym
;
7825 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
7826 if (isym
->st_shndx
== shndx2
)
7827 symtable2
[count2
++].u
.isym
= isym
;
7829 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7832 for (i
= 0; i
< count1
; i
++)
7834 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
7835 symtable1
[i
].u
.isym
->st_name
);
7837 for (i
= 0; i
< count2
; i
++)
7839 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
7840 symtable2
[i
].u
.isym
->st_name
);
7842 /* Sort symbol by name. */
7843 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7844 elf_sym_name_compare
);
7845 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7846 elf_sym_name_compare
);
7848 for (i
= 0; i
< count1
; i
++)
7849 /* Two symbols must have the same binding, type and name. */
7850 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
7851 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
7852 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7870 /* Return TRUE if 2 section types are compatible. */
7873 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
7874 bfd
*bbfd
, const asection
*bsec
)
7878 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
7879 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7882 return elf_section_type (asec
) == elf_section_type (bsec
);
7885 /* Final phase of ELF linker. */
7887 /* A structure we use to avoid passing large numbers of arguments. */
7889 struct elf_final_link_info
7891 /* General link information. */
7892 struct bfd_link_info
*info
;
7895 /* Symbol string table. */
7896 struct elf_strtab_hash
*symstrtab
;
7897 /* .hash section. */
7899 /* symbol version section (.gnu.version). */
7900 asection
*symver_sec
;
7901 /* Buffer large enough to hold contents of any section. */
7903 /* Buffer large enough to hold external relocs of any section. */
7904 void *external_relocs
;
7905 /* Buffer large enough to hold internal relocs of any section. */
7906 Elf_Internal_Rela
*internal_relocs
;
7907 /* Buffer large enough to hold external local symbols of any input
7909 bfd_byte
*external_syms
;
7910 /* And a buffer for symbol section indices. */
7911 Elf_External_Sym_Shndx
*locsym_shndx
;
7912 /* Buffer large enough to hold internal local symbols of any input
7914 Elf_Internal_Sym
*internal_syms
;
7915 /* Array large enough to hold a symbol index for each local symbol
7916 of any input BFD. */
7918 /* Array large enough to hold a section pointer for each local
7919 symbol of any input BFD. */
7920 asection
**sections
;
7921 /* Buffer for SHT_SYMTAB_SHNDX section. */
7922 Elf_External_Sym_Shndx
*symshndxbuf
;
7923 /* Number of STT_FILE syms seen. */
7924 size_t filesym_count
;
7927 /* This struct is used to pass information to elf_link_output_extsym. */
7929 struct elf_outext_info
7932 bfd_boolean localsyms
;
7933 bfd_boolean file_sym_done
;
7934 struct elf_final_link_info
*flinfo
;
7938 /* Support for evaluating a complex relocation.
7940 Complex relocations are generalized, self-describing relocations. The
7941 implementation of them consists of two parts: complex symbols, and the
7942 relocations themselves.
7944 The relocations are use a reserved elf-wide relocation type code (R_RELC
7945 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7946 information (start bit, end bit, word width, etc) into the addend. This
7947 information is extracted from CGEN-generated operand tables within gas.
7949 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7950 internal) representing prefix-notation expressions, including but not
7951 limited to those sorts of expressions normally encoded as addends in the
7952 addend field. The symbol mangling format is:
7955 | <unary-operator> ':' <node>
7956 | <binary-operator> ':' <node> ':' <node>
7959 <literal> := 's' <digits=N> ':' <N character symbol name>
7960 | 'S' <digits=N> ':' <N character section name>
7964 <binary-operator> := as in C
7965 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7968 set_symbol_value (bfd
*bfd_with_globals
,
7969 Elf_Internal_Sym
*isymbuf
,
7974 struct elf_link_hash_entry
**sym_hashes
;
7975 struct elf_link_hash_entry
*h
;
7976 size_t extsymoff
= locsymcount
;
7978 if (symidx
< locsymcount
)
7980 Elf_Internal_Sym
*sym
;
7982 sym
= isymbuf
+ symidx
;
7983 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
7985 /* It is a local symbol: move it to the
7986 "absolute" section and give it a value. */
7987 sym
->st_shndx
= SHN_ABS
;
7988 sym
->st_value
= val
;
7991 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
7995 /* It is a global symbol: set its link type
7996 to "defined" and give it a value. */
7998 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
7999 h
= sym_hashes
[symidx
- extsymoff
];
8000 while (h
->root
.type
== bfd_link_hash_indirect
8001 || h
->root
.type
== bfd_link_hash_warning
)
8002 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8003 h
->root
.type
= bfd_link_hash_defined
;
8004 h
->root
.u
.def
.value
= val
;
8005 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
8009 resolve_symbol (const char *name
,
8011 struct elf_final_link_info
*flinfo
,
8013 Elf_Internal_Sym
*isymbuf
,
8016 Elf_Internal_Sym
*sym
;
8017 struct bfd_link_hash_entry
*global_entry
;
8018 const char *candidate
= NULL
;
8019 Elf_Internal_Shdr
*symtab_hdr
;
8022 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
8024 for (i
= 0; i
< locsymcount
; ++ i
)
8028 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
8031 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
8032 symtab_hdr
->sh_link
,
8035 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8036 name
, candidate
, (unsigned long) sym
->st_value
);
8038 if (candidate
&& strcmp (candidate
, name
) == 0)
8040 asection
*sec
= flinfo
->sections
[i
];
8042 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
8043 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
8045 printf ("Found symbol with value %8.8lx\n",
8046 (unsigned long) *result
);
8052 /* Hmm, haven't found it yet. perhaps it is a global. */
8053 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
8054 FALSE
, FALSE
, TRUE
);
8058 if (global_entry
->type
== bfd_link_hash_defined
8059 || global_entry
->type
== bfd_link_hash_defweak
)
8061 *result
= (global_entry
->u
.def
.value
8062 + global_entry
->u
.def
.section
->output_section
->vma
8063 + global_entry
->u
.def
.section
->output_offset
);
8065 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8066 global_entry
->root
.string
, (unsigned long) *result
);
8074 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8075 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8076 names like "foo.end" which is the end address of section "foo". */
8079 resolve_section (const char *name
,
8087 for (curr
= sections
; curr
; curr
= curr
->next
)
8088 if (strcmp (curr
->name
, name
) == 0)
8090 *result
= curr
->vma
;
8094 /* Hmm. still haven't found it. try pseudo-section names. */
8095 /* FIXME: This could be coded more efficiently... */
8096 for (curr
= sections
; curr
; curr
= curr
->next
)
8098 len
= strlen (curr
->name
);
8099 if (len
> strlen (name
))
8102 if (strncmp (curr
->name
, name
, len
) == 0)
8104 if (strncmp (".end", name
+ len
, 4) == 0)
8106 *result
= curr
->vma
+ curr
->size
/ bfd_octets_per_byte (abfd
);
8110 /* Insert more pseudo-section names here, if you like. */
8118 undefined_reference (const char *reftype
, const char *name
)
8120 /* xgettext:c-format */
8121 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8126 eval_symbol (bfd_vma
*result
,
8129 struct elf_final_link_info
*flinfo
,
8131 Elf_Internal_Sym
*isymbuf
,
8140 const char *sym
= *symp
;
8142 bfd_boolean symbol_is_section
= FALSE
;
8147 if (len
< 1 || len
> sizeof (symbuf
))
8149 bfd_set_error (bfd_error_invalid_operation
);
8162 *result
= strtoul (sym
, (char **) symp
, 16);
8166 symbol_is_section
= TRUE
;
8170 symlen
= strtol (sym
, (char **) symp
, 10);
8171 sym
= *symp
+ 1; /* Skip the trailing ':'. */
8173 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
8175 bfd_set_error (bfd_error_invalid_operation
);
8179 memcpy (symbuf
, sym
, symlen
);
8180 symbuf
[symlen
] = '\0';
8181 *symp
= sym
+ symlen
;
8183 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8184 the symbol as a section, or vice-versa. so we're pretty liberal in our
8185 interpretation here; section means "try section first", not "must be a
8186 section", and likewise with symbol. */
8188 if (symbol_is_section
)
8190 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
8191 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8192 isymbuf
, locsymcount
))
8194 undefined_reference ("section", symbuf
);
8200 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8201 isymbuf
, locsymcount
)
8202 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8205 undefined_reference ("symbol", symbuf
);
8212 /* All that remains are operators. */
8214 #define UNARY_OP(op) \
8215 if (strncmp (sym, #op, strlen (#op)) == 0) \
8217 sym += strlen (#op); \
8221 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8222 isymbuf, locsymcount, signed_p)) \
8225 *result = op ((bfd_signed_vma) a); \
8231 #define BINARY_OP(op) \
8232 if (strncmp (sym, #op, strlen (#op)) == 0) \
8234 sym += strlen (#op); \
8238 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8239 isymbuf, locsymcount, signed_p)) \
8242 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8243 isymbuf, locsymcount, signed_p)) \
8246 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8276 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
8277 bfd_set_error (bfd_error_invalid_operation
);
8283 put_value (bfd_vma size
,
8284 unsigned long chunksz
,
8289 location
+= (size
- chunksz
);
8291 for (; size
; size
-= chunksz
, location
-= chunksz
)
8296 bfd_put_8 (input_bfd
, x
, location
);
8300 bfd_put_16 (input_bfd
, x
, location
);
8304 bfd_put_32 (input_bfd
, x
, location
);
8305 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8311 bfd_put_64 (input_bfd
, x
, location
);
8312 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8325 get_value (bfd_vma size
,
8326 unsigned long chunksz
,
8333 /* Sanity checks. */
8334 BFD_ASSERT (chunksz
<= sizeof (x
)
8337 && (size
% chunksz
) == 0
8338 && input_bfd
!= NULL
8339 && location
!= NULL
);
8341 if (chunksz
== sizeof (x
))
8343 BFD_ASSERT (size
== chunksz
);
8345 /* Make sure that we do not perform an undefined shift operation.
8346 We know that size == chunksz so there will only be one iteration
8347 of the loop below. */
8351 shift
= 8 * chunksz
;
8353 for (; size
; size
-= chunksz
, location
+= chunksz
)
8358 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
8361 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
8364 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
8368 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
8379 decode_complex_addend (unsigned long *start
, /* in bits */
8380 unsigned long *oplen
, /* in bits */
8381 unsigned long *len
, /* in bits */
8382 unsigned long *wordsz
, /* in bytes */
8383 unsigned long *chunksz
, /* in bytes */
8384 unsigned long *lsb0_p
,
8385 unsigned long *signed_p
,
8386 unsigned long *trunc_p
,
8387 unsigned long encoded
)
8389 * start
= encoded
& 0x3F;
8390 * len
= (encoded
>> 6) & 0x3F;
8391 * oplen
= (encoded
>> 12) & 0x3F;
8392 * wordsz
= (encoded
>> 18) & 0xF;
8393 * chunksz
= (encoded
>> 22) & 0xF;
8394 * lsb0_p
= (encoded
>> 27) & 1;
8395 * signed_p
= (encoded
>> 28) & 1;
8396 * trunc_p
= (encoded
>> 29) & 1;
8399 bfd_reloc_status_type
8400 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
8401 asection
*input_section ATTRIBUTE_UNUSED
,
8403 Elf_Internal_Rela
*rel
,
8406 bfd_vma shift
, x
, mask
;
8407 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
8408 bfd_reloc_status_type r
;
8410 /* Perform this reloc, since it is complex.
8411 (this is not to say that it necessarily refers to a complex
8412 symbol; merely that it is a self-describing CGEN based reloc.
8413 i.e. the addend has the complete reloc information (bit start, end,
8414 word size, etc) encoded within it.). */
8416 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
8417 &chunksz
, &lsb0_p
, &signed_p
,
8418 &trunc_p
, rel
->r_addend
);
8420 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
8423 shift
= (start
+ 1) - len
;
8425 shift
= (8 * wordsz
) - (start
+ len
);
8427 x
= get_value (wordsz
, chunksz
, input_bfd
,
8428 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8431 printf ("Doing complex reloc: "
8432 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8433 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8434 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8435 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
8436 oplen
, (unsigned long) x
, (unsigned long) mask
,
8437 (unsigned long) relocation
);
8442 /* Now do an overflow check. */
8443 r
= bfd_check_overflow ((signed_p
8444 ? complain_overflow_signed
8445 : complain_overflow_unsigned
),
8446 len
, 0, (8 * wordsz
),
8450 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
8453 printf (" relocation: %8.8lx\n"
8454 " shifted mask: %8.8lx\n"
8455 " shifted/masked reloc: %8.8lx\n"
8456 " result: %8.8lx\n",
8457 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
8458 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
8460 put_value (wordsz
, chunksz
, input_bfd
, x
,
8461 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8465 /* Functions to read r_offset from external (target order) reloc
8466 entry. Faster than bfd_getl32 et al, because we let the compiler
8467 know the value is aligned. */
8470 ext32l_r_offset (const void *p
)
8477 const union aligned32
*a
8478 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8480 uint32_t aval
= ( (uint32_t) a
->c
[0]
8481 | (uint32_t) a
->c
[1] << 8
8482 | (uint32_t) a
->c
[2] << 16
8483 | (uint32_t) a
->c
[3] << 24);
8488 ext32b_r_offset (const void *p
)
8495 const union aligned32
*a
8496 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8498 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
8499 | (uint32_t) a
->c
[1] << 16
8500 | (uint32_t) a
->c
[2] << 8
8501 | (uint32_t) a
->c
[3]);
8505 #ifdef BFD_HOST_64_BIT
8507 ext64l_r_offset (const void *p
)
8514 const union aligned64
*a
8515 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8517 uint64_t aval
= ( (uint64_t) a
->c
[0]
8518 | (uint64_t) a
->c
[1] << 8
8519 | (uint64_t) a
->c
[2] << 16
8520 | (uint64_t) a
->c
[3] << 24
8521 | (uint64_t) a
->c
[4] << 32
8522 | (uint64_t) a
->c
[5] << 40
8523 | (uint64_t) a
->c
[6] << 48
8524 | (uint64_t) a
->c
[7] << 56);
8529 ext64b_r_offset (const void *p
)
8536 const union aligned64
*a
8537 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8539 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
8540 | (uint64_t) a
->c
[1] << 48
8541 | (uint64_t) a
->c
[2] << 40
8542 | (uint64_t) a
->c
[3] << 32
8543 | (uint64_t) a
->c
[4] << 24
8544 | (uint64_t) a
->c
[5] << 16
8545 | (uint64_t) a
->c
[6] << 8
8546 | (uint64_t) a
->c
[7]);
8551 /* When performing a relocatable link, the input relocations are
8552 preserved. But, if they reference global symbols, the indices
8553 referenced must be updated. Update all the relocations found in
8557 elf_link_adjust_relocs (bfd
*abfd
,
8559 struct bfd_elf_section_reloc_data
*reldata
,
8561 struct bfd_link_info
*info
)
8564 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8566 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8567 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8568 bfd_vma r_type_mask
;
8570 unsigned int count
= reldata
->count
;
8571 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
8573 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
8575 swap_in
= bed
->s
->swap_reloc_in
;
8576 swap_out
= bed
->s
->swap_reloc_out
;
8578 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
8580 swap_in
= bed
->s
->swap_reloca_in
;
8581 swap_out
= bed
->s
->swap_reloca_out
;
8586 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
8589 if (bed
->s
->arch_size
== 32)
8596 r_type_mask
= 0xffffffff;
8600 erela
= reldata
->hdr
->contents
;
8601 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
8603 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
8606 if (*rel_hash
== NULL
)
8609 if ((*rel_hash
)->indx
== -2
8610 && info
->gc_sections
8611 && ! info
->gc_keep_exported
)
8613 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8614 _bfd_error_handler (_("%B:%A: error: relocation references symbol %s which was removed by garbage collection."),
8616 (*rel_hash
)->root
.root
.string
);
8617 _bfd_error_handler (_("%B:%A: error: try relinking with --gc-keep-exported enabled."),
8619 bfd_set_error (bfd_error_invalid_operation
);
8622 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
8624 (*swap_in
) (abfd
, erela
, irela
);
8625 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
8626 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
8627 | (irela
[j
].r_info
& r_type_mask
));
8628 (*swap_out
) (abfd
, irela
, erela
);
8631 if (bed
->elf_backend_update_relocs
)
8632 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
8634 if (sort
&& count
!= 0)
8636 bfd_vma (*ext_r_off
) (const void *);
8639 bfd_byte
*base
, *end
, *p
, *loc
;
8640 bfd_byte
*buf
= NULL
;
8642 if (bed
->s
->arch_size
== 32)
8644 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8645 ext_r_off
= ext32l_r_offset
;
8646 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8647 ext_r_off
= ext32b_r_offset
;
8653 #ifdef BFD_HOST_64_BIT
8654 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8655 ext_r_off
= ext64l_r_offset
;
8656 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8657 ext_r_off
= ext64b_r_offset
;
8663 /* Must use a stable sort here. A modified insertion sort,
8664 since the relocs are mostly sorted already. */
8665 elt_size
= reldata
->hdr
->sh_entsize
;
8666 base
= reldata
->hdr
->contents
;
8667 end
= base
+ count
* elt_size
;
8668 if (elt_size
> sizeof (Elf64_External_Rela
))
8671 /* Ensure the first element is lowest. This acts as a sentinel,
8672 speeding the main loop below. */
8673 r_off
= (*ext_r_off
) (base
);
8674 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
8676 bfd_vma r_off2
= (*ext_r_off
) (p
);
8685 /* Don't just swap *base and *loc as that changes the order
8686 of the original base[0] and base[1] if they happen to
8687 have the same r_offset. */
8688 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
8689 memcpy (onebuf
, loc
, elt_size
);
8690 memmove (base
+ elt_size
, base
, loc
- base
);
8691 memcpy (base
, onebuf
, elt_size
);
8694 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
8696 /* base to p is sorted, *p is next to insert. */
8697 r_off
= (*ext_r_off
) (p
);
8698 /* Search the sorted region for location to insert. */
8700 while (r_off
< (*ext_r_off
) (loc
))
8705 /* Chances are there is a run of relocs to insert here,
8706 from one of more input files. Files are not always
8707 linked in order due to the way elf_link_input_bfd is
8708 called. See pr17666. */
8709 size_t sortlen
= p
- loc
;
8710 bfd_vma r_off2
= (*ext_r_off
) (loc
);
8711 size_t runlen
= elt_size
;
8712 size_t buf_size
= 96 * 1024;
8713 while (p
+ runlen
< end
8714 && (sortlen
<= buf_size
8715 || runlen
+ elt_size
<= buf_size
)
8716 && r_off2
> (*ext_r_off
) (p
+ runlen
))
8720 buf
= bfd_malloc (buf_size
);
8724 if (runlen
< sortlen
)
8726 memcpy (buf
, p
, runlen
);
8727 memmove (loc
+ runlen
, loc
, sortlen
);
8728 memcpy (loc
, buf
, runlen
);
8732 memcpy (buf
, loc
, sortlen
);
8733 memmove (loc
, p
, runlen
);
8734 memcpy (loc
+ runlen
, buf
, sortlen
);
8736 p
+= runlen
- elt_size
;
8739 /* Hashes are no longer valid. */
8740 free (reldata
->hashes
);
8741 reldata
->hashes
= NULL
;
8747 struct elf_link_sort_rela
8753 enum elf_reloc_type_class type
;
8754 /* We use this as an array of size int_rels_per_ext_rel. */
8755 Elf_Internal_Rela rela
[1];
8759 elf_link_sort_cmp1 (const void *A
, const void *B
)
8761 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8762 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8763 int relativea
, relativeb
;
8765 relativea
= a
->type
== reloc_class_relative
;
8766 relativeb
= b
->type
== reloc_class_relative
;
8768 if (relativea
< relativeb
)
8770 if (relativea
> relativeb
)
8772 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
8774 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
8776 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8778 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8784 elf_link_sort_cmp2 (const void *A
, const void *B
)
8786 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8787 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8789 if (a
->type
< b
->type
)
8791 if (a
->type
> b
->type
)
8793 if (a
->u
.offset
< b
->u
.offset
)
8795 if (a
->u
.offset
> b
->u
.offset
)
8797 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8799 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8805 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
8807 asection
*dynamic_relocs
;
8810 bfd_size_type count
, size
;
8811 size_t i
, ret
, sort_elt
, ext_size
;
8812 bfd_byte
*sort
, *s_non_relative
, *p
;
8813 struct elf_link_sort_rela
*sq
;
8814 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8815 int i2e
= bed
->s
->int_rels_per_ext_rel
;
8816 unsigned int opb
= bfd_octets_per_byte (abfd
);
8817 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8818 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8819 struct bfd_link_order
*lo
;
8821 bfd_boolean use_rela
;
8823 /* Find a dynamic reloc section. */
8824 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
8825 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
8826 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
8827 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8829 bfd_boolean use_rela_initialised
= FALSE
;
8831 /* This is just here to stop gcc from complaining.
8832 Its initialization checking code is not perfect. */
8835 /* Both sections are present. Examine the sizes
8836 of the indirect sections to help us choose. */
8837 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8838 if (lo
->type
== bfd_indirect_link_order
)
8840 asection
*o
= lo
->u
.indirect
.section
;
8842 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8844 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8845 /* Section size is divisible by both rel and rela sizes.
8846 It is of no help to us. */
8850 /* Section size is only divisible by rela. */
8851 if (use_rela_initialised
&& !use_rela
)
8853 _bfd_error_handler (_("%B: Unable to sort relocs - "
8854 "they are in more than one size"),
8856 bfd_set_error (bfd_error_invalid_operation
);
8862 use_rela_initialised
= TRUE
;
8866 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8868 /* Section size is only divisible by rel. */
8869 if (use_rela_initialised
&& use_rela
)
8871 _bfd_error_handler (_("%B: Unable to sort relocs - "
8872 "they are in more than one size"),
8874 bfd_set_error (bfd_error_invalid_operation
);
8880 use_rela_initialised
= TRUE
;
8885 /* The section size is not divisible by either -
8886 something is wrong. */
8887 _bfd_error_handler (_("%B: Unable to sort relocs - "
8888 "they are of an unknown size"), abfd
);
8889 bfd_set_error (bfd_error_invalid_operation
);
8894 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8895 if (lo
->type
== bfd_indirect_link_order
)
8897 asection
*o
= lo
->u
.indirect
.section
;
8899 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8901 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8902 /* Section size is divisible by both rel and rela sizes.
8903 It is of no help to us. */
8907 /* Section size is only divisible by rela. */
8908 if (use_rela_initialised
&& !use_rela
)
8910 _bfd_error_handler (_("%B: Unable to sort relocs - "
8911 "they are in more than one size"),
8913 bfd_set_error (bfd_error_invalid_operation
);
8919 use_rela_initialised
= TRUE
;
8923 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8925 /* Section size is only divisible by rel. */
8926 if (use_rela_initialised
&& use_rela
)
8928 _bfd_error_handler (_("%B: Unable to sort relocs - "
8929 "they are in more than one size"),
8931 bfd_set_error (bfd_error_invalid_operation
);
8937 use_rela_initialised
= TRUE
;
8942 /* The section size is not divisible by either -
8943 something is wrong. */
8944 _bfd_error_handler (_("%B: Unable to sort relocs - "
8945 "they are of an unknown size"), abfd
);
8946 bfd_set_error (bfd_error_invalid_operation
);
8951 if (! use_rela_initialised
)
8955 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
8957 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8964 dynamic_relocs
= rela_dyn
;
8965 ext_size
= bed
->s
->sizeof_rela
;
8966 swap_in
= bed
->s
->swap_reloca_in
;
8967 swap_out
= bed
->s
->swap_reloca_out
;
8971 dynamic_relocs
= rel_dyn
;
8972 ext_size
= bed
->s
->sizeof_rel
;
8973 swap_in
= bed
->s
->swap_reloc_in
;
8974 swap_out
= bed
->s
->swap_reloc_out
;
8978 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8979 if (lo
->type
== bfd_indirect_link_order
)
8980 size
+= lo
->u
.indirect
.section
->size
;
8982 if (size
!= dynamic_relocs
->size
)
8985 sort_elt
= (sizeof (struct elf_link_sort_rela
)
8986 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
8988 count
= dynamic_relocs
->size
/ ext_size
;
8991 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
8995 (*info
->callbacks
->warning
)
8996 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
9000 if (bed
->s
->arch_size
== 32)
9001 r_sym_mask
= ~(bfd_vma
) 0xff;
9003 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
9005 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9006 if (lo
->type
== bfd_indirect_link_order
)
9008 bfd_byte
*erel
, *erelend
;
9009 asection
*o
= lo
->u
.indirect
.section
;
9011 if (o
->contents
== NULL
&& o
->size
!= 0)
9013 /* This is a reloc section that is being handled as a normal
9014 section. See bfd_section_from_shdr. We can't combine
9015 relocs in this case. */
9020 erelend
= o
->contents
+ o
->size
;
9021 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
9023 while (erel
< erelend
)
9025 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9027 (*swap_in
) (abfd
, erel
, s
->rela
);
9028 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
9029 s
->u
.sym_mask
= r_sym_mask
;
9035 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
9037 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
9039 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9040 if (s
->type
!= reloc_class_relative
)
9046 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
9047 for (; i
< count
; i
++, p
+= sort_elt
)
9049 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
9050 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
9052 sp
->u
.offset
= sq
->rela
->r_offset
;
9055 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
9057 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
9058 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
9060 /* We have plt relocs in .rela.dyn. */
9061 sq
= (struct elf_link_sort_rela
*) sort
;
9062 for (i
= 0; i
< count
; i
++)
9063 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
9065 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
9067 struct bfd_link_order
**plo
;
9068 /* Put srelplt link_order last. This is so the output_offset
9069 set in the next loop is correct for DT_JMPREL. */
9070 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
9071 if ((*plo
)->type
== bfd_indirect_link_order
9072 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
9078 plo
= &(*plo
)->next
;
9081 dynamic_relocs
->map_tail
.link_order
= lo
;
9086 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9087 if (lo
->type
== bfd_indirect_link_order
)
9089 bfd_byte
*erel
, *erelend
;
9090 asection
*o
= lo
->u
.indirect
.section
;
9093 erelend
= o
->contents
+ o
->size
;
9094 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
9095 while (erel
< erelend
)
9097 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9098 (*swap_out
) (abfd
, s
->rela
, erel
);
9105 *psec
= dynamic_relocs
;
9109 /* Add a symbol to the output symbol string table. */
9112 elf_link_output_symstrtab (struct elf_final_link_info
*flinfo
,
9114 Elf_Internal_Sym
*elfsym
,
9115 asection
*input_sec
,
9116 struct elf_link_hash_entry
*h
)
9118 int (*output_symbol_hook
)
9119 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
9120 struct elf_link_hash_entry
*);
9121 struct elf_link_hash_table
*hash_table
;
9122 const struct elf_backend_data
*bed
;
9123 bfd_size_type strtabsize
;
9125 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9127 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9128 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
9129 if (output_symbol_hook
!= NULL
)
9131 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
9138 || (input_sec
->flags
& SEC_EXCLUDE
))
9139 elfsym
->st_name
= (unsigned long) -1;
9142 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9143 to get the final offset for st_name. */
9145 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
9147 if (elfsym
->st_name
== (unsigned long) -1)
9151 hash_table
= elf_hash_table (flinfo
->info
);
9152 strtabsize
= hash_table
->strtabsize
;
9153 if (strtabsize
<= hash_table
->strtabcount
)
9155 strtabsize
+= strtabsize
;
9156 hash_table
->strtabsize
= strtabsize
;
9157 strtabsize
*= sizeof (*hash_table
->strtab
);
9159 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
9161 if (hash_table
->strtab
== NULL
)
9164 hash_table
->strtab
[hash_table
->strtabcount
].sym
= *elfsym
;
9165 hash_table
->strtab
[hash_table
->strtabcount
].dest_index
9166 = hash_table
->strtabcount
;
9167 hash_table
->strtab
[hash_table
->strtabcount
].destshndx_index
9168 = flinfo
->symshndxbuf
? bfd_get_symcount (flinfo
->output_bfd
) : 0;
9170 bfd_get_symcount (flinfo
->output_bfd
) += 1;
9171 hash_table
->strtabcount
+= 1;
9176 /* Swap symbols out to the symbol table and flush the output symbols to
9180 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
9182 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
9185 const struct elf_backend_data
*bed
;
9187 Elf_Internal_Shdr
*hdr
;
9191 if (!hash_table
->strtabcount
)
9194 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9196 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9198 amt
= bed
->s
->sizeof_sym
* hash_table
->strtabcount
;
9199 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
9203 if (flinfo
->symshndxbuf
)
9205 amt
= sizeof (Elf_External_Sym_Shndx
);
9206 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
9207 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
9208 if (flinfo
->symshndxbuf
== NULL
)
9215 for (i
= 0; i
< hash_table
->strtabcount
; i
++)
9217 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
9218 if (elfsym
->sym
.st_name
== (unsigned long) -1)
9219 elfsym
->sym
.st_name
= 0;
9222 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
9223 elfsym
->sym
.st_name
);
9224 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
9225 ((bfd_byte
*) symbuf
9226 + (elfsym
->dest_index
9227 * bed
->s
->sizeof_sym
)),
9228 (flinfo
->symshndxbuf
9229 + elfsym
->destshndx_index
));
9232 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
9233 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
9234 amt
= hash_table
->strtabcount
* bed
->s
->sizeof_sym
;
9235 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
9236 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
9238 hdr
->sh_size
+= amt
;
9246 free (hash_table
->strtab
);
9247 hash_table
->strtab
= NULL
;
9252 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9255 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
9257 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
9258 && sym
->st_shndx
< SHN_LORESERVE
)
9260 /* The gABI doesn't support dynamic symbols in output sections
9263 /* xgettext:c-format */
9264 (_("%B: Too many sections: %d (>= %d)"),
9265 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
9266 bfd_set_error (bfd_error_nonrepresentable_section
);
9272 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9273 allowing an unsatisfied unversioned symbol in the DSO to match a
9274 versioned symbol that would normally require an explicit version.
9275 We also handle the case that a DSO references a hidden symbol
9276 which may be satisfied by a versioned symbol in another DSO. */
9279 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
9280 const struct elf_backend_data
*bed
,
9281 struct elf_link_hash_entry
*h
)
9284 struct elf_link_loaded_list
*loaded
;
9286 if (!is_elf_hash_table (info
->hash
))
9289 /* Check indirect symbol. */
9290 while (h
->root
.type
== bfd_link_hash_indirect
)
9291 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9293 switch (h
->root
.type
)
9299 case bfd_link_hash_undefined
:
9300 case bfd_link_hash_undefweak
:
9301 abfd
= h
->root
.u
.undef
.abfd
;
9303 || (abfd
->flags
& DYNAMIC
) == 0
9304 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
9308 case bfd_link_hash_defined
:
9309 case bfd_link_hash_defweak
:
9310 abfd
= h
->root
.u
.def
.section
->owner
;
9313 case bfd_link_hash_common
:
9314 abfd
= h
->root
.u
.c
.p
->section
->owner
;
9317 BFD_ASSERT (abfd
!= NULL
);
9319 for (loaded
= elf_hash_table (info
)->loaded
;
9321 loaded
= loaded
->next
)
9324 Elf_Internal_Shdr
*hdr
;
9328 Elf_Internal_Shdr
*versymhdr
;
9329 Elf_Internal_Sym
*isym
;
9330 Elf_Internal_Sym
*isymend
;
9331 Elf_Internal_Sym
*isymbuf
;
9332 Elf_External_Versym
*ever
;
9333 Elf_External_Versym
*extversym
;
9335 input
= loaded
->abfd
;
9337 /* We check each DSO for a possible hidden versioned definition. */
9339 || (input
->flags
& DYNAMIC
) == 0
9340 || elf_dynversym (input
) == 0)
9343 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
9345 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9346 if (elf_bad_symtab (input
))
9348 extsymcount
= symcount
;
9353 extsymcount
= symcount
- hdr
->sh_info
;
9354 extsymoff
= hdr
->sh_info
;
9357 if (extsymcount
== 0)
9360 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
9362 if (isymbuf
== NULL
)
9365 /* Read in any version definitions. */
9366 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
9367 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
9368 if (extversym
== NULL
)
9371 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
9372 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
9373 != versymhdr
->sh_size
))
9381 ever
= extversym
+ extsymoff
;
9382 isymend
= isymbuf
+ extsymcount
;
9383 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
9386 Elf_Internal_Versym iver
;
9387 unsigned short version_index
;
9389 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
9390 || isym
->st_shndx
== SHN_UNDEF
)
9393 name
= bfd_elf_string_from_elf_section (input
,
9396 if (strcmp (name
, h
->root
.root
.string
) != 0)
9399 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
9401 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
9403 && h
->forced_local
))
9405 /* If we have a non-hidden versioned sym, then it should
9406 have provided a definition for the undefined sym unless
9407 it is defined in a non-shared object and forced local.
9412 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
9413 if (version_index
== 1 || version_index
== 2)
9415 /* This is the base or first version. We can use it. */
9429 /* Convert ELF common symbol TYPE. */
9432 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
9434 /* Commom symbol can only appear in relocatable link. */
9435 if (!bfd_link_relocatable (info
))
9437 switch (info
->elf_stt_common
)
9441 case elf_stt_common
:
9444 case no_elf_stt_common
:
9451 /* Add an external symbol to the symbol table. This is called from
9452 the hash table traversal routine. When generating a shared object,
9453 we go through the symbol table twice. The first time we output
9454 anything that might have been forced to local scope in a version
9455 script. The second time we output the symbols that are still
9459 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
9461 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
9462 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
9463 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
9465 Elf_Internal_Sym sym
;
9466 asection
*input_sec
;
9467 const struct elf_backend_data
*bed
;
9472 if (h
->root
.type
== bfd_link_hash_warning
)
9474 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9475 if (h
->root
.type
== bfd_link_hash_new
)
9479 /* Decide whether to output this symbol in this pass. */
9480 if (eoinfo
->localsyms
)
9482 if (!h
->forced_local
)
9487 if (h
->forced_local
)
9491 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9493 if (h
->root
.type
== bfd_link_hash_undefined
)
9495 /* If we have an undefined symbol reference here then it must have
9496 come from a shared library that is being linked in. (Undefined
9497 references in regular files have already been handled unless
9498 they are in unreferenced sections which are removed by garbage
9500 bfd_boolean ignore_undef
= FALSE
;
9502 /* Some symbols may be special in that the fact that they're
9503 undefined can be safely ignored - let backend determine that. */
9504 if (bed
->elf_backend_ignore_undef_symbol
)
9505 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
9507 /* If we are reporting errors for this situation then do so now. */
9510 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
9511 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
9512 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
9513 (*flinfo
->info
->callbacks
->undefined_symbol
)
9514 (flinfo
->info
, h
->root
.root
.string
,
9515 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
,
9517 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
);
9519 /* Strip a global symbol defined in a discarded section. */
9524 /* We should also warn if a forced local symbol is referenced from
9525 shared libraries. */
9526 if (bfd_link_executable (flinfo
->info
)
9531 && h
->ref_dynamic_nonweak
9532 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
9536 struct elf_link_hash_entry
*hi
= h
;
9538 /* Check indirect symbol. */
9539 while (hi
->root
.type
== bfd_link_hash_indirect
)
9540 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
9542 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
9543 /* xgettext:c-format */
9544 msg
= _("%B: internal symbol `%s' in %B is referenced by DSO");
9545 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
9546 /* xgettext:c-format */
9547 msg
= _("%B: hidden symbol `%s' in %B is referenced by DSO");
9549 /* xgettext:c-format */
9550 msg
= _("%B: local symbol `%s' in %B is referenced by DSO");
9551 def_bfd
= flinfo
->output_bfd
;
9552 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
9553 def_bfd
= hi
->root
.u
.def
.section
->owner
;
9554 _bfd_error_handler (msg
, flinfo
->output_bfd
,
9555 h
->root
.root
.string
, def_bfd
);
9556 bfd_set_error (bfd_error_bad_value
);
9557 eoinfo
->failed
= TRUE
;
9561 /* We don't want to output symbols that have never been mentioned by
9562 a regular file, or that we have been told to strip. However, if
9563 h->indx is set to -2, the symbol is used by a reloc and we must
9568 else if ((h
->def_dynamic
9570 || h
->root
.type
== bfd_link_hash_new
)
9574 else if (flinfo
->info
->strip
== strip_all
)
9576 else if (flinfo
->info
->strip
== strip_some
9577 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
9578 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
9580 else if ((h
->root
.type
== bfd_link_hash_defined
9581 || h
->root
.type
== bfd_link_hash_defweak
)
9582 && ((flinfo
->info
->strip_discarded
9583 && discarded_section (h
->root
.u
.def
.section
))
9584 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
9585 && h
->root
.u
.def
.section
->owner
!= NULL
9586 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
9588 else if ((h
->root
.type
== bfd_link_hash_undefined
9589 || h
->root
.type
== bfd_link_hash_undefweak
)
9590 && h
->root
.u
.undef
.abfd
!= NULL
9591 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
9596 /* If we're stripping it, and it's not a dynamic symbol, there's
9597 nothing else to do. However, if it is a forced local symbol or
9598 an ifunc symbol we need to give the backend finish_dynamic_symbol
9599 function a chance to make it dynamic. */
9602 && type
!= STT_GNU_IFUNC
9603 && !h
->forced_local
)
9607 sym
.st_size
= h
->size
;
9608 sym
.st_other
= h
->other
;
9609 switch (h
->root
.type
)
9612 case bfd_link_hash_new
:
9613 case bfd_link_hash_warning
:
9617 case bfd_link_hash_undefined
:
9618 case bfd_link_hash_undefweak
:
9619 input_sec
= bfd_und_section_ptr
;
9620 sym
.st_shndx
= SHN_UNDEF
;
9623 case bfd_link_hash_defined
:
9624 case bfd_link_hash_defweak
:
9626 input_sec
= h
->root
.u
.def
.section
;
9627 if (input_sec
->output_section
!= NULL
)
9630 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
9631 input_sec
->output_section
);
9632 if (sym
.st_shndx
== SHN_BAD
)
9635 /* xgettext:c-format */
9636 (_("%B: could not find output section %A for input section %A"),
9637 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
9638 bfd_set_error (bfd_error_nonrepresentable_section
);
9639 eoinfo
->failed
= TRUE
;
9643 /* ELF symbols in relocatable files are section relative,
9644 but in nonrelocatable files they are virtual
9646 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
9647 if (!bfd_link_relocatable (flinfo
->info
))
9649 sym
.st_value
+= input_sec
->output_section
->vma
;
9650 if (h
->type
== STT_TLS
)
9652 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
9653 if (tls_sec
!= NULL
)
9654 sym
.st_value
-= tls_sec
->vma
;
9660 BFD_ASSERT (input_sec
->owner
== NULL
9661 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
9662 sym
.st_shndx
= SHN_UNDEF
;
9663 input_sec
= bfd_und_section_ptr
;
9668 case bfd_link_hash_common
:
9669 input_sec
= h
->root
.u
.c
.p
->section
;
9670 sym
.st_shndx
= bed
->common_section_index (input_sec
);
9671 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
9674 case bfd_link_hash_indirect
:
9675 /* These symbols are created by symbol versioning. They point
9676 to the decorated version of the name. For example, if the
9677 symbol foo@@GNU_1.2 is the default, which should be used when
9678 foo is used with no version, then we add an indirect symbol
9679 foo which points to foo@@GNU_1.2. We ignore these symbols,
9680 since the indirected symbol is already in the hash table. */
9684 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
9685 switch (h
->root
.type
)
9687 case bfd_link_hash_common
:
9688 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9690 case bfd_link_hash_defined
:
9691 case bfd_link_hash_defweak
:
9692 if (bed
->common_definition (&sym
))
9693 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9697 case bfd_link_hash_undefined
:
9698 case bfd_link_hash_undefweak
:
9704 if (h
->forced_local
)
9706 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
9707 /* Turn off visibility on local symbol. */
9708 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
9710 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9711 else if (h
->unique_global
&& h
->def_regular
)
9712 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
9713 else if (h
->root
.type
== bfd_link_hash_undefweak
9714 || h
->root
.type
== bfd_link_hash_defweak
)
9715 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
9717 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
9718 sym
.st_target_internal
= h
->target_internal
;
9720 /* Give the processor backend a chance to tweak the symbol value,
9721 and also to finish up anything that needs to be done for this
9722 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9723 forced local syms when non-shared is due to a historical quirk.
9724 STT_GNU_IFUNC symbol must go through PLT. */
9725 if ((h
->type
== STT_GNU_IFUNC
9727 && !bfd_link_relocatable (flinfo
->info
))
9728 || ((h
->dynindx
!= -1
9730 && ((bfd_link_pic (flinfo
->info
)
9731 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
9732 || h
->root
.type
!= bfd_link_hash_undefweak
))
9733 || !h
->forced_local
)
9734 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
9736 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
9737 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
9739 eoinfo
->failed
= TRUE
;
9744 /* If we are marking the symbol as undefined, and there are no
9745 non-weak references to this symbol from a regular object, then
9746 mark the symbol as weak undefined; if there are non-weak
9747 references, mark the symbol as strong. We can't do this earlier,
9748 because it might not be marked as undefined until the
9749 finish_dynamic_symbol routine gets through with it. */
9750 if (sym
.st_shndx
== SHN_UNDEF
9752 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
9753 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
9756 type
= ELF_ST_TYPE (sym
.st_info
);
9758 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9759 if (type
== STT_GNU_IFUNC
)
9762 if (h
->ref_regular_nonweak
)
9763 bindtype
= STB_GLOBAL
;
9765 bindtype
= STB_WEAK
;
9766 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
9769 /* If this is a symbol defined in a dynamic library, don't use the
9770 symbol size from the dynamic library. Relinking an executable
9771 against a new library may introduce gratuitous changes in the
9772 executable's symbols if we keep the size. */
9773 if (sym
.st_shndx
== SHN_UNDEF
9778 /* If a non-weak symbol with non-default visibility is not defined
9779 locally, it is a fatal error. */
9780 if (!bfd_link_relocatable (flinfo
->info
)
9781 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
9782 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
9783 && h
->root
.type
== bfd_link_hash_undefined
9788 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
9789 /* xgettext:c-format */
9790 msg
= _("%B: protected symbol `%s' isn't defined");
9791 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
9792 /* xgettext:c-format */
9793 msg
= _("%B: internal symbol `%s' isn't defined");
9795 /* xgettext:c-format */
9796 msg
= _("%B: hidden symbol `%s' isn't defined");
9797 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
9798 bfd_set_error (bfd_error_bad_value
);
9799 eoinfo
->failed
= TRUE
;
9803 /* If this symbol should be put in the .dynsym section, then put it
9804 there now. We already know the symbol index. We also fill in
9805 the entry in the .hash section. */
9806 if (elf_hash_table (flinfo
->info
)->dynsym
!= NULL
9808 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
)
9812 /* Since there is no version information in the dynamic string,
9813 if there is no version info in symbol version section, we will
9814 have a run-time problem if not linking executable, referenced
9815 by shared library, or not bound locally. */
9816 if (h
->verinfo
.verdef
== NULL
9817 && (!bfd_link_executable (flinfo
->info
)
9819 || !h
->def_regular
))
9821 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
9823 if (p
&& p
[1] != '\0')
9826 /* xgettext:c-format */
9827 (_("%B: No symbol version section for versioned symbol `%s'"),
9828 flinfo
->output_bfd
, h
->root
.root
.string
);
9829 eoinfo
->failed
= TRUE
;
9834 sym
.st_name
= h
->dynstr_index
;
9835 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
9836 + h
->dynindx
* bed
->s
->sizeof_sym
);
9837 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
9839 eoinfo
->failed
= TRUE
;
9842 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
9844 if (flinfo
->hash_sec
!= NULL
)
9846 size_t hash_entry_size
;
9847 bfd_byte
*bucketpos
;
9852 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
9853 bucket
= h
->u
.elf_hash_value
% bucketcount
;
9856 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
9857 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
9858 + (bucket
+ 2) * hash_entry_size
);
9859 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
9860 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
9862 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
9863 ((bfd_byte
*) flinfo
->hash_sec
->contents
9864 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
9867 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
9869 Elf_Internal_Versym iversym
;
9870 Elf_External_Versym
*eversym
;
9872 if (!h
->def_regular
)
9874 if (h
->verinfo
.verdef
== NULL
9875 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
9876 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
9877 iversym
.vs_vers
= 0;
9879 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
9883 if (h
->verinfo
.vertree
== NULL
)
9884 iversym
.vs_vers
= 1;
9886 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
9887 if (flinfo
->info
->create_default_symver
)
9891 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9893 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
9894 iversym
.vs_vers
|= VERSYM_HIDDEN
;
9896 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
9897 eversym
+= h
->dynindx
;
9898 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
9902 /* If the symbol is undefined, and we didn't output it to .dynsym,
9903 strip it from .symtab too. Obviously we can't do this for
9904 relocatable output or when needed for --emit-relocs. */
9905 else if (input_sec
== bfd_und_section_ptr
9907 && !bfd_link_relocatable (flinfo
->info
))
9909 /* Also strip others that we couldn't earlier due to dynamic symbol
9913 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
9916 /* Output a FILE symbol so that following locals are not associated
9917 with the wrong input file. We need one for forced local symbols
9918 if we've seen more than one FILE symbol or when we have exactly
9919 one FILE symbol but global symbols are present in a file other
9920 than the one with the FILE symbol. We also need one if linker
9921 defined symbols are present. In practice these conditions are
9922 always met, so just emit the FILE symbol unconditionally. */
9923 if (eoinfo
->localsyms
9924 && !eoinfo
->file_sym_done
9925 && eoinfo
->flinfo
->filesym_count
!= 0)
9927 Elf_Internal_Sym fsym
;
9929 memset (&fsym
, 0, sizeof (fsym
));
9930 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
9931 fsym
.st_shndx
= SHN_ABS
;
9932 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
9933 bfd_und_section_ptr
, NULL
))
9936 eoinfo
->file_sym_done
= TRUE
;
9939 indx
= bfd_get_symcount (flinfo
->output_bfd
);
9940 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
9944 eoinfo
->failed
= TRUE
;
9949 else if (h
->indx
== -2)
9955 /* Return TRUE if special handling is done for relocs in SEC against
9956 symbols defined in discarded sections. */
9959 elf_section_ignore_discarded_relocs (asection
*sec
)
9961 const struct elf_backend_data
*bed
;
9963 switch (sec
->sec_info_type
)
9965 case SEC_INFO_TYPE_STABS
:
9966 case SEC_INFO_TYPE_EH_FRAME
:
9967 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
9973 bed
= get_elf_backend_data (sec
->owner
);
9974 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
9975 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
9981 /* Return a mask saying how ld should treat relocations in SEC against
9982 symbols defined in discarded sections. If this function returns
9983 COMPLAIN set, ld will issue a warning message. If this function
9984 returns PRETEND set, and the discarded section was link-once and the
9985 same size as the kept link-once section, ld will pretend that the
9986 symbol was actually defined in the kept section. Otherwise ld will
9987 zero the reloc (at least that is the intent, but some cooperation by
9988 the target dependent code is needed, particularly for REL targets). */
9991 _bfd_elf_default_action_discarded (asection
*sec
)
9993 if (sec
->flags
& SEC_DEBUGGING
)
9996 if (strcmp (".eh_frame", sec
->name
) == 0)
9999 if (strcmp (".gcc_except_table", sec
->name
) == 0)
10002 return COMPLAIN
| PRETEND
;
10005 /* Find a match between a section and a member of a section group. */
10008 match_group_member (asection
*sec
, asection
*group
,
10009 struct bfd_link_info
*info
)
10011 asection
*first
= elf_next_in_group (group
);
10012 asection
*s
= first
;
10016 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
10019 s
= elf_next_in_group (s
);
10027 /* Check if the kept section of a discarded section SEC can be used
10028 to replace it. Return the replacement if it is OK. Otherwise return
10032 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
10036 kept
= sec
->kept_section
;
10039 if ((kept
->flags
& SEC_GROUP
) != 0)
10040 kept
= match_group_member (sec
, kept
, info
);
10042 && ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
10043 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
)))
10045 sec
->kept_section
= kept
;
10050 /* Link an input file into the linker output file. This function
10051 handles all the sections and relocations of the input file at once.
10052 This is so that we only have to read the local symbols once, and
10053 don't have to keep them in memory. */
10056 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
10058 int (*relocate_section
)
10059 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
10060 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
10062 Elf_Internal_Shdr
*symtab_hdr
;
10063 size_t locsymcount
;
10065 Elf_Internal_Sym
*isymbuf
;
10066 Elf_Internal_Sym
*isym
;
10067 Elf_Internal_Sym
*isymend
;
10069 asection
**ppsection
;
10071 const struct elf_backend_data
*bed
;
10072 struct elf_link_hash_entry
**sym_hashes
;
10073 bfd_size_type address_size
;
10074 bfd_vma r_type_mask
;
10076 bfd_boolean have_file_sym
= FALSE
;
10078 output_bfd
= flinfo
->output_bfd
;
10079 bed
= get_elf_backend_data (output_bfd
);
10080 relocate_section
= bed
->elf_backend_relocate_section
;
10082 /* If this is a dynamic object, we don't want to do anything here:
10083 we don't want the local symbols, and we don't want the section
10085 if ((input_bfd
->flags
& DYNAMIC
) != 0)
10088 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
10089 if (elf_bad_symtab (input_bfd
))
10091 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10096 locsymcount
= symtab_hdr
->sh_info
;
10097 extsymoff
= symtab_hdr
->sh_info
;
10100 /* Read the local symbols. */
10101 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10102 if (isymbuf
== NULL
&& locsymcount
!= 0)
10104 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
10105 flinfo
->internal_syms
,
10106 flinfo
->external_syms
,
10107 flinfo
->locsym_shndx
);
10108 if (isymbuf
== NULL
)
10112 /* Find local symbol sections and adjust values of symbols in
10113 SEC_MERGE sections. Write out those local symbols we know are
10114 going into the output file. */
10115 isymend
= isymbuf
+ locsymcount
;
10116 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
10118 isym
++, pindex
++, ppsection
++)
10122 Elf_Internal_Sym osym
;
10128 if (elf_bad_symtab (input_bfd
))
10130 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
10137 if (isym
->st_shndx
== SHN_UNDEF
)
10138 isec
= bfd_und_section_ptr
;
10139 else if (isym
->st_shndx
== SHN_ABS
)
10140 isec
= bfd_abs_section_ptr
;
10141 else if (isym
->st_shndx
== SHN_COMMON
)
10142 isec
= bfd_com_section_ptr
;
10145 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
10148 /* Don't attempt to output symbols with st_shnx in the
10149 reserved range other than SHN_ABS and SHN_COMMON. */
10153 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
10154 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
10156 _bfd_merged_section_offset (output_bfd
, &isec
,
10157 elf_section_data (isec
)->sec_info
,
10163 /* Don't output the first, undefined, symbol. In fact, don't
10164 output any undefined local symbol. */
10165 if (isec
== bfd_und_section_ptr
)
10168 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
10170 /* We never output section symbols. Instead, we use the
10171 section symbol of the corresponding section in the output
10176 /* If we are stripping all symbols, we don't want to output this
10178 if (flinfo
->info
->strip
== strip_all
)
10181 /* If we are discarding all local symbols, we don't want to
10182 output this one. If we are generating a relocatable output
10183 file, then some of the local symbols may be required by
10184 relocs; we output them below as we discover that they are
10186 if (flinfo
->info
->discard
== discard_all
)
10189 /* If this symbol is defined in a section which we are
10190 discarding, we don't need to keep it. */
10191 if (isym
->st_shndx
!= SHN_UNDEF
10192 && isym
->st_shndx
< SHN_LORESERVE
10193 && bfd_section_removed_from_list (output_bfd
,
10194 isec
->output_section
))
10197 /* Get the name of the symbol. */
10198 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
10203 /* See if we are discarding symbols with this name. */
10204 if ((flinfo
->info
->strip
== strip_some
10205 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
10207 || (((flinfo
->info
->discard
== discard_sec_merge
10208 && (isec
->flags
& SEC_MERGE
)
10209 && !bfd_link_relocatable (flinfo
->info
))
10210 || flinfo
->info
->discard
== discard_l
)
10211 && bfd_is_local_label_name (input_bfd
, name
)))
10214 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
10216 if (input_bfd
->lto_output
)
10217 /* -flto puts a temp file name here. This means builds
10218 are not reproducible. Discard the symbol. */
10220 have_file_sym
= TRUE
;
10221 flinfo
->filesym_count
+= 1;
10223 if (!have_file_sym
)
10225 /* In the absence of debug info, bfd_find_nearest_line uses
10226 FILE symbols to determine the source file for local
10227 function symbols. Provide a FILE symbol here if input
10228 files lack such, so that their symbols won't be
10229 associated with a previous input file. It's not the
10230 source file, but the best we can do. */
10231 have_file_sym
= TRUE
;
10232 flinfo
->filesym_count
+= 1;
10233 memset (&osym
, 0, sizeof (osym
));
10234 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10235 osym
.st_shndx
= SHN_ABS
;
10236 if (!elf_link_output_symstrtab (flinfo
,
10237 (input_bfd
->lto_output
? NULL
10238 : input_bfd
->filename
),
10239 &osym
, bfd_abs_section_ptr
,
10246 /* Adjust the section index for the output file. */
10247 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10248 isec
->output_section
);
10249 if (osym
.st_shndx
== SHN_BAD
)
10252 /* ELF symbols in relocatable files are section relative, but
10253 in executable files they are virtual addresses. Note that
10254 this code assumes that all ELF sections have an associated
10255 BFD section with a reasonable value for output_offset; below
10256 we assume that they also have a reasonable value for
10257 output_section. Any special sections must be set up to meet
10258 these requirements. */
10259 osym
.st_value
+= isec
->output_offset
;
10260 if (!bfd_link_relocatable (flinfo
->info
))
10262 osym
.st_value
+= isec
->output_section
->vma
;
10263 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
10265 /* STT_TLS symbols are relative to PT_TLS segment base. */
10266 BFD_ASSERT (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
);
10267 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
10271 indx
= bfd_get_symcount (output_bfd
);
10272 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
10279 if (bed
->s
->arch_size
== 32)
10281 r_type_mask
= 0xff;
10287 r_type_mask
= 0xffffffff;
10292 /* Relocate the contents of each section. */
10293 sym_hashes
= elf_sym_hashes (input_bfd
);
10294 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
10296 bfd_byte
*contents
;
10298 if (! o
->linker_mark
)
10300 /* This section was omitted from the link. */
10304 if (!flinfo
->info
->resolve_section_groups
10305 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
10307 /* Deal with the group signature symbol. */
10308 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
10309 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
10310 asection
*osec
= o
->output_section
;
10312 BFD_ASSERT (bfd_link_relocatable (flinfo
->info
));
10313 if (symndx
>= locsymcount
10314 || (elf_bad_symtab (input_bfd
)
10315 && flinfo
->sections
[symndx
] == NULL
))
10317 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
10318 while (h
->root
.type
== bfd_link_hash_indirect
10319 || h
->root
.type
== bfd_link_hash_warning
)
10320 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10321 /* Arrange for symbol to be output. */
10323 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
10325 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
10327 /* We'll use the output section target_index. */
10328 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10329 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
10333 if (flinfo
->indices
[symndx
] == -1)
10335 /* Otherwise output the local symbol now. */
10336 Elf_Internal_Sym sym
= isymbuf
[symndx
];
10337 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10342 name
= bfd_elf_string_from_elf_section (input_bfd
,
10343 symtab_hdr
->sh_link
,
10348 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10350 if (sym
.st_shndx
== SHN_BAD
)
10353 sym
.st_value
+= o
->output_offset
;
10355 indx
= bfd_get_symcount (output_bfd
);
10356 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
10361 flinfo
->indices
[symndx
] = indx
;
10365 elf_section_data (osec
)->this_hdr
.sh_info
10366 = flinfo
->indices
[symndx
];
10370 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
10371 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
10374 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
10376 /* Section was created by _bfd_elf_link_create_dynamic_sections
10381 /* Get the contents of the section. They have been cached by a
10382 relaxation routine. Note that o is a section in an input
10383 file, so the contents field will not have been set by any of
10384 the routines which work on output files. */
10385 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
10387 contents
= elf_section_data (o
)->this_hdr
.contents
;
10388 if (bed
->caches_rawsize
10390 && o
->rawsize
< o
->size
)
10392 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
10393 contents
= flinfo
->contents
;
10398 contents
= flinfo
->contents
;
10399 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
10403 if ((o
->flags
& SEC_RELOC
) != 0)
10405 Elf_Internal_Rela
*internal_relocs
;
10406 Elf_Internal_Rela
*rel
, *relend
;
10407 int action_discarded
;
10410 /* Get the swapped relocs. */
10412 = _bfd_elf_link_read_relocs (input_bfd
, o
, flinfo
->external_relocs
,
10413 flinfo
->internal_relocs
, FALSE
);
10414 if (internal_relocs
== NULL
10415 && o
->reloc_count
> 0)
10418 /* We need to reverse-copy input .ctors/.dtors sections if
10419 they are placed in .init_array/.finit_array for output. */
10420 if (o
->size
> address_size
10421 && ((strncmp (o
->name
, ".ctors", 6) == 0
10422 && strcmp (o
->output_section
->name
,
10423 ".init_array") == 0)
10424 || (strncmp (o
->name
, ".dtors", 6) == 0
10425 && strcmp (o
->output_section
->name
,
10426 ".fini_array") == 0))
10427 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
10429 if (o
->size
* bed
->s
->int_rels_per_ext_rel
10430 != o
->reloc_count
* address_size
)
10433 /* xgettext:c-format */
10434 (_("error: %B: size of section %A is not "
10435 "multiple of address size"),
10437 bfd_set_error (bfd_error_on_input
);
10440 o
->flags
|= SEC_ELF_REVERSE_COPY
;
10443 action_discarded
= -1;
10444 if (!elf_section_ignore_discarded_relocs (o
))
10445 action_discarded
= (*bed
->action_discarded
) (o
);
10447 /* Run through the relocs evaluating complex reloc symbols and
10448 looking for relocs against symbols from discarded sections
10449 or section symbols from removed link-once sections.
10450 Complain about relocs against discarded sections. Zero
10451 relocs against removed link-once sections. */
10453 rel
= internal_relocs
;
10454 relend
= rel
+ o
->reloc_count
;
10455 for ( ; rel
< relend
; rel
++)
10457 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
10458 unsigned int s_type
;
10459 asection
**ps
, *sec
;
10460 struct elf_link_hash_entry
*h
= NULL
;
10461 const char *sym_name
;
10463 if (r_symndx
== STN_UNDEF
)
10466 if (r_symndx
>= locsymcount
10467 || (elf_bad_symtab (input_bfd
)
10468 && flinfo
->sections
[r_symndx
] == NULL
))
10470 h
= sym_hashes
[r_symndx
- extsymoff
];
10472 /* Badly formatted input files can contain relocs that
10473 reference non-existant symbols. Check here so that
10474 we do not seg fault. */
10478 /* xgettext:c-format */
10479 (_("error: %B contains a reloc (%#Lx) for section %A "
10480 "that references a non-existent global symbol"),
10481 input_bfd
, rel
->r_info
, o
);
10482 bfd_set_error (bfd_error_bad_value
);
10486 while (h
->root
.type
== bfd_link_hash_indirect
10487 || h
->root
.type
== bfd_link_hash_warning
)
10488 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10492 /* If a plugin symbol is referenced from a non-IR file,
10493 mark the symbol as undefined. Note that the
10494 linker may attach linker created dynamic sections
10495 to the plugin bfd. Symbols defined in linker
10496 created sections are not plugin symbols. */
10497 if ((h
->root
.non_ir_ref_regular
10498 || h
->root
.non_ir_ref_dynamic
)
10499 && (h
->root
.type
== bfd_link_hash_defined
10500 || h
->root
.type
== bfd_link_hash_defweak
)
10501 && (h
->root
.u
.def
.section
->flags
10502 & SEC_LINKER_CREATED
) == 0
10503 && h
->root
.u
.def
.section
->owner
!= NULL
10504 && (h
->root
.u
.def
.section
->owner
->flags
10505 & BFD_PLUGIN
) != 0)
10507 h
->root
.type
= bfd_link_hash_undefined
;
10508 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
10512 if (h
->root
.type
== bfd_link_hash_defined
10513 || h
->root
.type
== bfd_link_hash_defweak
)
10514 ps
= &h
->root
.u
.def
.section
;
10516 sym_name
= h
->root
.root
.string
;
10520 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
10522 s_type
= ELF_ST_TYPE (sym
->st_info
);
10523 ps
= &flinfo
->sections
[r_symndx
];
10524 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
10528 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
10529 && !bfd_link_relocatable (flinfo
->info
))
10532 bfd_vma dot
= (rel
->r_offset
10533 + o
->output_offset
+ o
->output_section
->vma
);
10535 printf ("Encountered a complex symbol!");
10536 printf (" (input_bfd %s, section %s, reloc %ld\n",
10537 input_bfd
->filename
, o
->name
,
10538 (long) (rel
- internal_relocs
));
10539 printf (" symbol: idx %8.8lx, name %s\n",
10540 r_symndx
, sym_name
);
10541 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10542 (unsigned long) rel
->r_info
,
10543 (unsigned long) rel
->r_offset
);
10545 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
10546 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
10549 /* Symbol evaluated OK. Update to absolute value. */
10550 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
10555 if (action_discarded
!= -1 && ps
!= NULL
)
10557 /* Complain if the definition comes from a
10558 discarded section. */
10559 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
10561 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
10562 if (action_discarded
& COMPLAIN
)
10563 (*flinfo
->info
->callbacks
->einfo
)
10564 /* xgettext:c-format */
10565 (_("%X`%s' referenced in section `%A' of %B: "
10566 "defined in discarded section `%A' of %B\n"),
10567 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
10569 /* Try to do the best we can to support buggy old
10570 versions of gcc. Pretend that the symbol is
10571 really defined in the kept linkonce section.
10572 FIXME: This is quite broken. Modifying the
10573 symbol here means we will be changing all later
10574 uses of the symbol, not just in this section. */
10575 if (action_discarded
& PRETEND
)
10579 kept
= _bfd_elf_check_kept_section (sec
,
10591 /* Relocate the section by invoking a back end routine.
10593 The back end routine is responsible for adjusting the
10594 section contents as necessary, and (if using Rela relocs
10595 and generating a relocatable output file) adjusting the
10596 reloc addend as necessary.
10598 The back end routine does not have to worry about setting
10599 the reloc address or the reloc symbol index.
10601 The back end routine is given a pointer to the swapped in
10602 internal symbols, and can access the hash table entries
10603 for the external symbols via elf_sym_hashes (input_bfd).
10605 When generating relocatable output, the back end routine
10606 must handle STB_LOCAL/STT_SECTION symbols specially. The
10607 output symbol is going to be a section symbol
10608 corresponding to the output section, which will require
10609 the addend to be adjusted. */
10611 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
10612 input_bfd
, o
, contents
,
10620 || bfd_link_relocatable (flinfo
->info
)
10621 || flinfo
->info
->emitrelocations
)
10623 Elf_Internal_Rela
*irela
;
10624 Elf_Internal_Rela
*irelaend
, *irelamid
;
10625 bfd_vma last_offset
;
10626 struct elf_link_hash_entry
**rel_hash
;
10627 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
10628 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
10629 unsigned int next_erel
;
10630 bfd_boolean rela_normal
;
10631 struct bfd_elf_section_data
*esdi
, *esdo
;
10633 esdi
= elf_section_data (o
);
10634 esdo
= elf_section_data (o
->output_section
);
10635 rela_normal
= FALSE
;
10637 /* Adjust the reloc addresses and symbol indices. */
10639 irela
= internal_relocs
;
10640 irelaend
= irela
+ o
->reloc_count
;
10641 rel_hash
= esdo
->rel
.hashes
+ esdo
->rel
.count
;
10642 /* We start processing the REL relocs, if any. When we reach
10643 IRELAMID in the loop, we switch to the RELA relocs. */
10645 if (esdi
->rel
.hdr
!= NULL
)
10646 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
10647 * bed
->s
->int_rels_per_ext_rel
);
10648 rel_hash_list
= rel_hash
;
10649 rela_hash_list
= NULL
;
10650 last_offset
= o
->output_offset
;
10651 if (!bfd_link_relocatable (flinfo
->info
))
10652 last_offset
+= o
->output_section
->vma
;
10653 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
10655 unsigned long r_symndx
;
10657 Elf_Internal_Sym sym
;
10659 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
10665 if (irela
== irelamid
)
10667 rel_hash
= esdo
->rela
.hashes
+ esdo
->rela
.count
;
10668 rela_hash_list
= rel_hash
;
10669 rela_normal
= bed
->rela_normal
;
10672 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
10675 if (irela
->r_offset
>= (bfd_vma
) -2)
10677 /* This is a reloc for a deleted entry or somesuch.
10678 Turn it into an R_*_NONE reloc, at the same
10679 offset as the last reloc. elf_eh_frame.c and
10680 bfd_elf_discard_info rely on reloc offsets
10682 irela
->r_offset
= last_offset
;
10684 irela
->r_addend
= 0;
10688 irela
->r_offset
+= o
->output_offset
;
10690 /* Relocs in an executable have to be virtual addresses. */
10691 if (!bfd_link_relocatable (flinfo
->info
))
10692 irela
->r_offset
+= o
->output_section
->vma
;
10694 last_offset
= irela
->r_offset
;
10696 r_symndx
= irela
->r_info
>> r_sym_shift
;
10697 if (r_symndx
== STN_UNDEF
)
10700 if (r_symndx
>= locsymcount
10701 || (elf_bad_symtab (input_bfd
)
10702 && flinfo
->sections
[r_symndx
] == NULL
))
10704 struct elf_link_hash_entry
*rh
;
10705 unsigned long indx
;
10707 /* This is a reloc against a global symbol. We
10708 have not yet output all the local symbols, so
10709 we do not know the symbol index of any global
10710 symbol. We set the rel_hash entry for this
10711 reloc to point to the global hash table entry
10712 for this symbol. The symbol index is then
10713 set at the end of bfd_elf_final_link. */
10714 indx
= r_symndx
- extsymoff
;
10715 rh
= elf_sym_hashes (input_bfd
)[indx
];
10716 while (rh
->root
.type
== bfd_link_hash_indirect
10717 || rh
->root
.type
== bfd_link_hash_warning
)
10718 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
10720 /* Setting the index to -2 tells
10721 elf_link_output_extsym that this symbol is
10722 used by a reloc. */
10723 BFD_ASSERT (rh
->indx
< 0);
10730 /* This is a reloc against a local symbol. */
10733 sym
= isymbuf
[r_symndx
];
10734 sec
= flinfo
->sections
[r_symndx
];
10735 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
10737 /* I suppose the backend ought to fill in the
10738 section of any STT_SECTION symbol against a
10739 processor specific section. */
10740 r_symndx
= STN_UNDEF
;
10741 if (bfd_is_abs_section (sec
))
10743 else if (sec
== NULL
|| sec
->owner
== NULL
)
10745 bfd_set_error (bfd_error_bad_value
);
10750 asection
*osec
= sec
->output_section
;
10752 /* If we have discarded a section, the output
10753 section will be the absolute section. In
10754 case of discarded SEC_MERGE sections, use
10755 the kept section. relocate_section should
10756 have already handled discarded linkonce
10758 if (bfd_is_abs_section (osec
)
10759 && sec
->kept_section
!= NULL
10760 && sec
->kept_section
->output_section
!= NULL
)
10762 osec
= sec
->kept_section
->output_section
;
10763 irela
->r_addend
-= osec
->vma
;
10766 if (!bfd_is_abs_section (osec
))
10768 r_symndx
= osec
->target_index
;
10769 if (r_symndx
== STN_UNDEF
)
10771 irela
->r_addend
+= osec
->vma
;
10772 osec
= _bfd_nearby_section (output_bfd
, osec
,
10774 irela
->r_addend
-= osec
->vma
;
10775 r_symndx
= osec
->target_index
;
10780 /* Adjust the addend according to where the
10781 section winds up in the output section. */
10783 irela
->r_addend
+= sec
->output_offset
;
10787 if (flinfo
->indices
[r_symndx
] == -1)
10789 unsigned long shlink
;
10794 if (flinfo
->info
->strip
== strip_all
)
10796 /* You can't do ld -r -s. */
10797 bfd_set_error (bfd_error_invalid_operation
);
10801 /* This symbol was skipped earlier, but
10802 since it is needed by a reloc, we
10803 must output it now. */
10804 shlink
= symtab_hdr
->sh_link
;
10805 name
= (bfd_elf_string_from_elf_section
10806 (input_bfd
, shlink
, sym
.st_name
));
10810 osec
= sec
->output_section
;
10812 _bfd_elf_section_from_bfd_section (output_bfd
,
10814 if (sym
.st_shndx
== SHN_BAD
)
10817 sym
.st_value
+= sec
->output_offset
;
10818 if (!bfd_link_relocatable (flinfo
->info
))
10820 sym
.st_value
+= osec
->vma
;
10821 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
10823 /* STT_TLS symbols are relative to PT_TLS
10825 BFD_ASSERT (elf_hash_table (flinfo
->info
)
10826 ->tls_sec
!= NULL
);
10827 sym
.st_value
-= (elf_hash_table (flinfo
->info
)
10832 indx
= bfd_get_symcount (output_bfd
);
10833 ret
= elf_link_output_symstrtab (flinfo
, name
,
10839 flinfo
->indices
[r_symndx
] = indx
;
10844 r_symndx
= flinfo
->indices
[r_symndx
];
10847 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
10848 | (irela
->r_info
& r_type_mask
));
10851 /* Swap out the relocs. */
10852 input_rel_hdr
= esdi
->rel
.hdr
;
10853 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
10855 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10860 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
10861 * bed
->s
->int_rels_per_ext_rel
);
10862 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
10865 input_rela_hdr
= esdi
->rela
.hdr
;
10866 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
10868 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10877 /* Write out the modified section contents. */
10878 if (bed
->elf_backend_write_section
10879 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
10882 /* Section written out. */
10884 else switch (o
->sec_info_type
)
10886 case SEC_INFO_TYPE_STABS
:
10887 if (! (_bfd_write_section_stabs
10889 &elf_hash_table (flinfo
->info
)->stab_info
,
10890 o
, &elf_section_data (o
)->sec_info
, contents
)))
10893 case SEC_INFO_TYPE_MERGE
:
10894 if (! _bfd_write_merged_section (output_bfd
, o
,
10895 elf_section_data (o
)->sec_info
))
10898 case SEC_INFO_TYPE_EH_FRAME
:
10900 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
10905 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10907 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
10915 if (! (o
->flags
& SEC_EXCLUDE
))
10917 file_ptr offset
= (file_ptr
) o
->output_offset
;
10918 bfd_size_type todo
= o
->size
;
10920 offset
*= bfd_octets_per_byte (output_bfd
);
10922 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
10924 /* Reverse-copy input section to output. */
10927 todo
-= address_size
;
10928 if (! bfd_set_section_contents (output_bfd
,
10936 offset
+= address_size
;
10940 else if (! bfd_set_section_contents (output_bfd
,
10954 /* Generate a reloc when linking an ELF file. This is a reloc
10955 requested by the linker, and does not come from any input file. This
10956 is used to build constructor and destructor tables when linking
10960 elf_reloc_link_order (bfd
*output_bfd
,
10961 struct bfd_link_info
*info
,
10962 asection
*output_section
,
10963 struct bfd_link_order
*link_order
)
10965 reloc_howto_type
*howto
;
10969 struct bfd_elf_section_reloc_data
*reldata
;
10970 struct elf_link_hash_entry
**rel_hash_ptr
;
10971 Elf_Internal_Shdr
*rel_hdr
;
10972 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
10973 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
10976 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
10978 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
10981 bfd_set_error (bfd_error_bad_value
);
10985 addend
= link_order
->u
.reloc
.p
->addend
;
10988 reldata
= &esdo
->rel
;
10989 else if (esdo
->rela
.hdr
)
10990 reldata
= &esdo
->rela
;
10997 /* Figure out the symbol index. */
10998 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
10999 if (link_order
->type
== bfd_section_reloc_link_order
)
11001 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
11002 BFD_ASSERT (indx
!= 0);
11003 *rel_hash_ptr
= NULL
;
11007 struct elf_link_hash_entry
*h
;
11009 /* Treat a reloc against a defined symbol as though it were
11010 actually against the section. */
11011 h
= ((struct elf_link_hash_entry
*)
11012 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
11013 link_order
->u
.reloc
.p
->u
.name
,
11014 FALSE
, FALSE
, TRUE
));
11016 && (h
->root
.type
== bfd_link_hash_defined
11017 || h
->root
.type
== bfd_link_hash_defweak
))
11021 section
= h
->root
.u
.def
.section
;
11022 indx
= section
->output_section
->target_index
;
11023 *rel_hash_ptr
= NULL
;
11024 /* It seems that we ought to add the symbol value to the
11025 addend here, but in practice it has already been added
11026 because it was passed to constructor_callback. */
11027 addend
+= section
->output_section
->vma
+ section
->output_offset
;
11029 else if (h
!= NULL
)
11031 /* Setting the index to -2 tells elf_link_output_extsym that
11032 this symbol is used by a reloc. */
11039 (*info
->callbacks
->unattached_reloc
)
11040 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
11045 /* If this is an inplace reloc, we must write the addend into the
11047 if (howto
->partial_inplace
&& addend
!= 0)
11049 bfd_size_type size
;
11050 bfd_reloc_status_type rstat
;
11053 const char *sym_name
;
11055 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
11056 buf
= (bfd_byte
*) bfd_zmalloc (size
);
11057 if (buf
== NULL
&& size
!= 0)
11059 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
11066 case bfd_reloc_outofrange
:
11069 case bfd_reloc_overflow
:
11070 if (link_order
->type
== bfd_section_reloc_link_order
)
11071 sym_name
= bfd_section_name (output_bfd
,
11072 link_order
->u
.reloc
.p
->u
.section
);
11074 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
11075 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
11076 howto
->name
, addend
, NULL
, NULL
,
11081 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
11083 * bfd_octets_per_byte (output_bfd
),
11090 /* The address of a reloc is relative to the section in a
11091 relocatable file, and is a virtual address in an executable
11093 offset
= link_order
->offset
;
11094 if (! bfd_link_relocatable (info
))
11095 offset
+= output_section
->vma
;
11097 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
11099 irel
[i
].r_offset
= offset
;
11100 irel
[i
].r_info
= 0;
11101 irel
[i
].r_addend
= 0;
11103 if (bed
->s
->arch_size
== 32)
11104 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
11106 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
11108 rel_hdr
= reldata
->hdr
;
11109 erel
= rel_hdr
->contents
;
11110 if (rel_hdr
->sh_type
== SHT_REL
)
11112 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
11113 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
11117 irel
[0].r_addend
= addend
;
11118 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
11119 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
11128 /* Get the output vma of the section pointed to by the sh_link field. */
11131 elf_get_linked_section_vma (struct bfd_link_order
*p
)
11133 Elf_Internal_Shdr
**elf_shdrp
;
11137 s
= p
->u
.indirect
.section
;
11138 elf_shdrp
= elf_elfsections (s
->owner
);
11139 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
11140 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
11142 The Intel C compiler generates SHT_IA_64_UNWIND with
11143 SHF_LINK_ORDER. But it doesn't set the sh_link or
11144 sh_info fields. Hence we could get the situation
11145 where elfsec is 0. */
11148 const struct elf_backend_data
*bed
11149 = get_elf_backend_data (s
->owner
);
11150 if (bed
->link_order_error_handler
)
11151 bed
->link_order_error_handler
11152 /* xgettext:c-format */
11153 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
11158 s
= elf_shdrp
[elfsec
]->bfd_section
;
11159 return s
->output_section
->vma
+ s
->output_offset
;
11164 /* Compare two sections based on the locations of the sections they are
11165 linked to. Used by elf_fixup_link_order. */
11168 compare_link_order (const void * a
, const void * b
)
11173 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
11174 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
11177 return apos
> bpos
;
11181 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11182 order as their linked sections. Returns false if this could not be done
11183 because an output section includes both ordered and unordered
11184 sections. Ideally we'd do this in the linker proper. */
11187 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
11189 int seen_linkorder
;
11192 struct bfd_link_order
*p
;
11194 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11196 struct bfd_link_order
**sections
;
11197 asection
*s
, *other_sec
, *linkorder_sec
;
11201 linkorder_sec
= NULL
;
11203 seen_linkorder
= 0;
11204 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11206 if (p
->type
== bfd_indirect_link_order
)
11208 s
= p
->u
.indirect
.section
;
11210 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
11211 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
11212 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
11213 && elfsec
< elf_numsections (sub
)
11214 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
11215 && elf_elfsections (sub
)[elfsec
]->sh_link
< elf_numsections (sub
))
11229 if (seen_other
&& seen_linkorder
)
11231 if (other_sec
&& linkorder_sec
)
11233 /* xgettext:c-format */
11234 (_("%A has both ordered [`%A' in %B] "
11235 "and unordered [`%A' in %B] sections"),
11236 o
, linkorder_sec
, linkorder_sec
->owner
,
11237 other_sec
, other_sec
->owner
);
11240 (_("%A has both ordered and unordered sections"), o
);
11241 bfd_set_error (bfd_error_bad_value
);
11246 if (!seen_linkorder
)
11249 sections
= (struct bfd_link_order
**)
11250 bfd_malloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
11251 if (sections
== NULL
)
11253 seen_linkorder
= 0;
11255 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11257 sections
[seen_linkorder
++] = p
;
11259 /* Sort the input sections in the order of their linked section. */
11260 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
11261 compare_link_order
);
11263 /* Change the offsets of the sections. */
11265 for (n
= 0; n
< seen_linkorder
; n
++)
11267 s
= sections
[n
]->u
.indirect
.section
;
11268 offset
&= ~(bfd_vma
) 0 << s
->alignment_power
;
11269 s
->output_offset
= offset
/ bfd_octets_per_byte (abfd
);
11270 sections
[n
]->offset
= offset
;
11271 offset
+= sections
[n
]->size
;
11278 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11279 Returns TRUE upon success, FALSE otherwise. */
11282 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
11284 bfd_boolean ret
= FALSE
;
11286 const struct elf_backend_data
*bed
;
11288 enum bfd_architecture arch
;
11290 asymbol
**sympp
= NULL
;
11294 elf_symbol_type
*osymbuf
;
11296 implib_bfd
= info
->out_implib_bfd
;
11297 bed
= get_elf_backend_data (abfd
);
11299 if (!bfd_set_format (implib_bfd
, bfd_object
))
11302 /* Use flag from executable but make it a relocatable object. */
11303 flags
= bfd_get_file_flags (abfd
);
11304 flags
&= ~HAS_RELOC
;
11305 if (!bfd_set_start_address (implib_bfd
, 0)
11306 || !bfd_set_file_flags (implib_bfd
, flags
& ~EXEC_P
))
11309 /* Copy architecture of output file to import library file. */
11310 arch
= bfd_get_arch (abfd
);
11311 mach
= bfd_get_mach (abfd
);
11312 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
11313 && (abfd
->target_defaulted
11314 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
11317 /* Get symbol table size. */
11318 symsize
= bfd_get_symtab_upper_bound (abfd
);
11322 /* Read in the symbol table. */
11323 sympp
= (asymbol
**) xmalloc (symsize
);
11324 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
11328 /* Allow the BFD backend to copy any private header data it
11329 understands from the output BFD to the import library BFD. */
11330 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
11333 /* Filter symbols to appear in the import library. */
11334 if (bed
->elf_backend_filter_implib_symbols
)
11335 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
11338 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
11341 bfd_set_error (bfd_error_no_symbols
);
11342 _bfd_error_handler (_("%B: no symbol found for import library"),
11348 /* Make symbols absolute. */
11349 osymbuf
= (elf_symbol_type
*) bfd_alloc2 (implib_bfd
, symcount
,
11350 sizeof (*osymbuf
));
11351 for (src_count
= 0; src_count
< symcount
; src_count
++)
11353 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
11354 sizeof (*osymbuf
));
11355 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
11356 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
11357 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
11358 osymbuf
[src_count
].internal_elf_sym
.st_value
=
11359 osymbuf
[src_count
].symbol
.value
;
11360 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
11363 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
11365 /* Allow the BFD backend to copy any private data it understands
11366 from the output BFD to the import library BFD. This is done last
11367 to permit the routine to look at the filtered symbol table. */
11368 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
11371 if (!bfd_close (implib_bfd
))
11382 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
11386 if (flinfo
->symstrtab
!= NULL
)
11387 _bfd_elf_strtab_free (flinfo
->symstrtab
);
11388 if (flinfo
->contents
!= NULL
)
11389 free (flinfo
->contents
);
11390 if (flinfo
->external_relocs
!= NULL
)
11391 free (flinfo
->external_relocs
);
11392 if (flinfo
->internal_relocs
!= NULL
)
11393 free (flinfo
->internal_relocs
);
11394 if (flinfo
->external_syms
!= NULL
)
11395 free (flinfo
->external_syms
);
11396 if (flinfo
->locsym_shndx
!= NULL
)
11397 free (flinfo
->locsym_shndx
);
11398 if (flinfo
->internal_syms
!= NULL
)
11399 free (flinfo
->internal_syms
);
11400 if (flinfo
->indices
!= NULL
)
11401 free (flinfo
->indices
);
11402 if (flinfo
->sections
!= NULL
)
11403 free (flinfo
->sections
);
11404 if (flinfo
->symshndxbuf
!= NULL
)
11405 free (flinfo
->symshndxbuf
);
11406 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
11408 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11409 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rel
.hashes
!= NULL
)
11410 free (esdo
->rel
.hashes
);
11411 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rela
.hashes
!= NULL
)
11412 free (esdo
->rela
.hashes
);
11416 /* Do the final step of an ELF link. */
11419 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
11421 bfd_boolean dynamic
;
11422 bfd_boolean emit_relocs
;
11424 struct elf_final_link_info flinfo
;
11426 struct bfd_link_order
*p
;
11428 bfd_size_type max_contents_size
;
11429 bfd_size_type max_external_reloc_size
;
11430 bfd_size_type max_internal_reloc_count
;
11431 bfd_size_type max_sym_count
;
11432 bfd_size_type max_sym_shndx_count
;
11433 Elf_Internal_Sym elfsym
;
11435 Elf_Internal_Shdr
*symtab_hdr
;
11436 Elf_Internal_Shdr
*symtab_shndx_hdr
;
11437 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11438 struct elf_outext_info eoinfo
;
11439 bfd_boolean merged
;
11440 size_t relativecount
= 0;
11441 asection
*reldyn
= 0;
11443 asection
*attr_section
= NULL
;
11444 bfd_vma attr_size
= 0;
11445 const char *std_attrs_section
;
11446 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
11448 if (!is_elf_hash_table (htab
))
11451 if (bfd_link_pic (info
))
11452 abfd
->flags
|= DYNAMIC
;
11454 dynamic
= htab
->dynamic_sections_created
;
11455 dynobj
= htab
->dynobj
;
11457 emit_relocs
= (bfd_link_relocatable (info
)
11458 || info
->emitrelocations
);
11460 flinfo
.info
= info
;
11461 flinfo
.output_bfd
= abfd
;
11462 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
11463 if (flinfo
.symstrtab
== NULL
)
11468 flinfo
.hash_sec
= NULL
;
11469 flinfo
.symver_sec
= NULL
;
11473 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
11474 /* Note that dynsym_sec can be NULL (on VMS). */
11475 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
11476 /* Note that it is OK if symver_sec is NULL. */
11479 flinfo
.contents
= NULL
;
11480 flinfo
.external_relocs
= NULL
;
11481 flinfo
.internal_relocs
= NULL
;
11482 flinfo
.external_syms
= NULL
;
11483 flinfo
.locsym_shndx
= NULL
;
11484 flinfo
.internal_syms
= NULL
;
11485 flinfo
.indices
= NULL
;
11486 flinfo
.sections
= NULL
;
11487 flinfo
.symshndxbuf
= NULL
;
11488 flinfo
.filesym_count
= 0;
11490 /* The object attributes have been merged. Remove the input
11491 sections from the link, and set the contents of the output
11493 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
11494 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11496 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
11497 || strcmp (o
->name
, ".gnu.attributes") == 0)
11499 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11501 asection
*input_section
;
11503 if (p
->type
!= bfd_indirect_link_order
)
11505 input_section
= p
->u
.indirect
.section
;
11506 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11507 elf_link_input_bfd ignores this section. */
11508 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
11511 attr_size
= bfd_elf_obj_attr_size (abfd
);
11514 bfd_set_section_size (abfd
, o
, attr_size
);
11516 /* Skip this section later on. */
11517 o
->map_head
.link_order
= NULL
;
11520 o
->flags
|= SEC_EXCLUDE
;
11524 /* Count up the number of relocations we will output for each output
11525 section, so that we know the sizes of the reloc sections. We
11526 also figure out some maximum sizes. */
11527 max_contents_size
= 0;
11528 max_external_reloc_size
= 0;
11529 max_internal_reloc_count
= 0;
11531 max_sym_shndx_count
= 0;
11533 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11535 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11536 o
->reloc_count
= 0;
11538 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11540 unsigned int reloc_count
= 0;
11541 unsigned int additional_reloc_count
= 0;
11542 struct bfd_elf_section_data
*esdi
= NULL
;
11544 if (p
->type
== bfd_section_reloc_link_order
11545 || p
->type
== bfd_symbol_reloc_link_order
)
11547 else if (p
->type
== bfd_indirect_link_order
)
11551 sec
= p
->u
.indirect
.section
;
11553 /* Mark all sections which are to be included in the
11554 link. This will normally be every section. We need
11555 to do this so that we can identify any sections which
11556 the linker has decided to not include. */
11557 sec
->linker_mark
= TRUE
;
11559 if (sec
->flags
& SEC_MERGE
)
11562 if (sec
->rawsize
> max_contents_size
)
11563 max_contents_size
= sec
->rawsize
;
11564 if (sec
->size
> max_contents_size
)
11565 max_contents_size
= sec
->size
;
11567 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
11568 && (sec
->owner
->flags
& DYNAMIC
) == 0)
11572 /* We are interested in just local symbols, not all
11574 if (elf_bad_symtab (sec
->owner
))
11575 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
11576 / bed
->s
->sizeof_sym
);
11578 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
11580 if (sym_count
> max_sym_count
)
11581 max_sym_count
= sym_count
;
11583 if (sym_count
> max_sym_shndx_count
11584 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
11585 max_sym_shndx_count
= sym_count
;
11587 if (esdo
->this_hdr
.sh_type
== SHT_REL
11588 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
11589 /* Some backends use reloc_count in relocation sections
11590 to count particular types of relocs. Of course,
11591 reloc sections themselves can't have relocations. */
11593 else if (emit_relocs
)
11595 reloc_count
= sec
->reloc_count
;
11596 if (bed
->elf_backend_count_additional_relocs
)
11599 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
11600 additional_reloc_count
+= c
;
11603 else if (bed
->elf_backend_count_relocs
)
11604 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
11606 esdi
= elf_section_data (sec
);
11608 if ((sec
->flags
& SEC_RELOC
) != 0)
11610 size_t ext_size
= 0;
11612 if (esdi
->rel
.hdr
!= NULL
)
11613 ext_size
= esdi
->rel
.hdr
->sh_size
;
11614 if (esdi
->rela
.hdr
!= NULL
)
11615 ext_size
+= esdi
->rela
.hdr
->sh_size
;
11617 if (ext_size
> max_external_reloc_size
)
11618 max_external_reloc_size
= ext_size
;
11619 if (sec
->reloc_count
> max_internal_reloc_count
)
11620 max_internal_reloc_count
= sec
->reloc_count
;
11625 if (reloc_count
== 0)
11628 reloc_count
+= additional_reloc_count
;
11629 o
->reloc_count
+= reloc_count
;
11631 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
11635 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
11636 esdo
->rel
.count
+= additional_reloc_count
;
11638 if (esdi
->rela
.hdr
)
11640 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
11641 esdo
->rela
.count
+= additional_reloc_count
;
11647 esdo
->rela
.count
+= reloc_count
;
11649 esdo
->rel
.count
+= reloc_count
;
11653 if (o
->reloc_count
> 0)
11654 o
->flags
|= SEC_RELOC
;
11657 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11658 set it (this is probably a bug) and if it is set
11659 assign_section_numbers will create a reloc section. */
11660 o
->flags
&=~ SEC_RELOC
;
11663 /* If the SEC_ALLOC flag is not set, force the section VMA to
11664 zero. This is done in elf_fake_sections as well, but forcing
11665 the VMA to 0 here will ensure that relocs against these
11666 sections are handled correctly. */
11667 if ((o
->flags
& SEC_ALLOC
) == 0
11668 && ! o
->user_set_vma
)
11672 if (! bfd_link_relocatable (info
) && merged
)
11673 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
11675 /* Figure out the file positions for everything but the symbol table
11676 and the relocs. We set symcount to force assign_section_numbers
11677 to create a symbol table. */
11678 bfd_get_symcount (abfd
) = info
->strip
!= strip_all
|| emit_relocs
;
11679 BFD_ASSERT (! abfd
->output_has_begun
);
11680 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
11683 /* Set sizes, and assign file positions for reloc sections. */
11684 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11686 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11687 if ((o
->flags
& SEC_RELOC
) != 0)
11690 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
11694 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
11698 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11699 to count upwards while actually outputting the relocations. */
11700 esdo
->rel
.count
= 0;
11701 esdo
->rela
.count
= 0;
11703 if (esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
11705 /* Cache the section contents so that they can be compressed
11706 later. Use bfd_malloc since it will be freed by
11707 bfd_compress_section_contents. */
11708 unsigned char *contents
= esdo
->this_hdr
.contents
;
11709 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
11712 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
11713 if (contents
== NULL
)
11715 esdo
->this_hdr
.contents
= contents
;
11719 /* We have now assigned file positions for all the sections except
11720 .symtab, .strtab, and non-loaded reloc sections. We start the
11721 .symtab section at the current file position, and write directly
11722 to it. We build the .strtab section in memory. */
11723 bfd_get_symcount (abfd
) = 0;
11724 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11725 /* sh_name is set in prep_headers. */
11726 symtab_hdr
->sh_type
= SHT_SYMTAB
;
11727 /* sh_flags, sh_addr and sh_size all start off zero. */
11728 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
11729 /* sh_link is set in assign_section_numbers. */
11730 /* sh_info is set below. */
11731 /* sh_offset is set just below. */
11732 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
11734 if (max_sym_count
< 20)
11735 max_sym_count
= 20;
11736 htab
->strtabsize
= max_sym_count
;
11737 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
11738 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
11739 if (htab
->strtab
== NULL
)
11741 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11743 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
11744 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
11746 if (info
->strip
!= strip_all
|| emit_relocs
)
11748 file_ptr off
= elf_next_file_pos (abfd
);
11750 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
11752 /* Note that at this point elf_next_file_pos (abfd) is
11753 incorrect. We do not yet know the size of the .symtab section.
11754 We correct next_file_pos below, after we do know the size. */
11756 /* Start writing out the symbol table. The first symbol is always a
11758 elfsym
.st_value
= 0;
11759 elfsym
.st_size
= 0;
11760 elfsym
.st_info
= 0;
11761 elfsym
.st_other
= 0;
11762 elfsym
.st_shndx
= SHN_UNDEF
;
11763 elfsym
.st_target_internal
= 0;
11764 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
11765 bfd_und_section_ptr
, NULL
) != 1)
11768 /* Output a symbol for each section. We output these even if we are
11769 discarding local symbols, since they are used for relocs. These
11770 symbols have no names. We store the index of each one in the
11771 index field of the section, so that we can find it again when
11772 outputting relocs. */
11774 elfsym
.st_size
= 0;
11775 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
11776 elfsym
.st_other
= 0;
11777 elfsym
.st_value
= 0;
11778 elfsym
.st_target_internal
= 0;
11779 for (i
= 1; i
< elf_numsections (abfd
); i
++)
11781 o
= bfd_section_from_elf_index (abfd
, i
);
11784 o
->target_index
= bfd_get_symcount (abfd
);
11785 elfsym
.st_shndx
= i
;
11786 if (!bfd_link_relocatable (info
))
11787 elfsym
.st_value
= o
->vma
;
11788 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
, o
,
11795 /* Allocate some memory to hold information read in from the input
11797 if (max_contents_size
!= 0)
11799 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
11800 if (flinfo
.contents
== NULL
)
11804 if (max_external_reloc_size
!= 0)
11806 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
11807 if (flinfo
.external_relocs
== NULL
)
11811 if (max_internal_reloc_count
!= 0)
11813 amt
= max_internal_reloc_count
* sizeof (Elf_Internal_Rela
);
11814 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
11815 if (flinfo
.internal_relocs
== NULL
)
11819 if (max_sym_count
!= 0)
11821 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
11822 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
11823 if (flinfo
.external_syms
== NULL
)
11826 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
11827 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
11828 if (flinfo
.internal_syms
== NULL
)
11831 amt
= max_sym_count
* sizeof (long);
11832 flinfo
.indices
= (long int *) bfd_malloc (amt
);
11833 if (flinfo
.indices
== NULL
)
11836 amt
= max_sym_count
* sizeof (asection
*);
11837 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
11838 if (flinfo
.sections
== NULL
)
11842 if (max_sym_shndx_count
!= 0)
11844 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
11845 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
11846 if (flinfo
.locsym_shndx
== NULL
)
11852 bfd_vma base
, end
= 0;
11855 for (sec
= htab
->tls_sec
;
11856 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
11859 bfd_size_type size
= sec
->size
;
11862 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
11864 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
11867 size
= ord
->offset
+ ord
->size
;
11869 end
= sec
->vma
+ size
;
11871 base
= htab
->tls_sec
->vma
;
11872 /* Only align end of TLS section if static TLS doesn't have special
11873 alignment requirements. */
11874 if (bed
->static_tls_alignment
== 1)
11875 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
11876 htab
->tls_size
= end
- base
;
11879 /* Reorder SHF_LINK_ORDER sections. */
11880 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11882 if (!elf_fixup_link_order (abfd
, o
))
11886 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
11889 /* Since ELF permits relocations to be against local symbols, we
11890 must have the local symbols available when we do the relocations.
11891 Since we would rather only read the local symbols once, and we
11892 would rather not keep them in memory, we handle all the
11893 relocations for a single input file at the same time.
11895 Unfortunately, there is no way to know the total number of local
11896 symbols until we have seen all of them, and the local symbol
11897 indices precede the global symbol indices. This means that when
11898 we are generating relocatable output, and we see a reloc against
11899 a global symbol, we can not know the symbol index until we have
11900 finished examining all the local symbols to see which ones we are
11901 going to output. To deal with this, we keep the relocations in
11902 memory, and don't output them until the end of the link. This is
11903 an unfortunate waste of memory, but I don't see a good way around
11904 it. Fortunately, it only happens when performing a relocatable
11905 link, which is not the common case. FIXME: If keep_memory is set
11906 we could write the relocs out and then read them again; I don't
11907 know how bad the memory loss will be. */
11909 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11910 sub
->output_has_begun
= FALSE
;
11911 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11913 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11915 if (p
->type
== bfd_indirect_link_order
11916 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
11917 == bfd_target_elf_flavour
)
11918 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
11920 if (! sub
->output_has_begun
)
11922 if (! elf_link_input_bfd (&flinfo
, sub
))
11924 sub
->output_has_begun
= TRUE
;
11927 else if (p
->type
== bfd_section_reloc_link_order
11928 || p
->type
== bfd_symbol_reloc_link_order
)
11930 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
11935 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
11937 if (p
->type
== bfd_indirect_link_order
11938 && (bfd_get_flavour (sub
)
11939 == bfd_target_elf_flavour
)
11940 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
11941 != bed
->s
->elfclass
))
11943 const char *iclass
, *oclass
;
11945 switch (bed
->s
->elfclass
)
11947 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
11948 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
11949 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
11953 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
11955 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
11956 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
11957 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
11961 bfd_set_error (bfd_error_wrong_format
);
11963 /* xgettext:c-format */
11964 (_("%B: file class %s incompatible with %s"),
11965 sub
, iclass
, oclass
);
11974 /* Free symbol buffer if needed. */
11975 if (!info
->reduce_memory_overheads
)
11977 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11978 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
11979 && elf_tdata (sub
)->symbuf
)
11981 free (elf_tdata (sub
)->symbuf
);
11982 elf_tdata (sub
)->symbuf
= NULL
;
11986 /* Output any global symbols that got converted to local in a
11987 version script or due to symbol visibility. We do this in a
11988 separate step since ELF requires all local symbols to appear
11989 prior to any global symbols. FIXME: We should only do this if
11990 some global symbols were, in fact, converted to become local.
11991 FIXME: Will this work correctly with the Irix 5 linker? */
11992 eoinfo
.failed
= FALSE
;
11993 eoinfo
.flinfo
= &flinfo
;
11994 eoinfo
.localsyms
= TRUE
;
11995 eoinfo
.file_sym_done
= FALSE
;
11996 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12000 /* If backend needs to output some local symbols not present in the hash
12001 table, do it now. */
12002 if (bed
->elf_backend_output_arch_local_syms
12003 && (info
->strip
!= strip_all
|| emit_relocs
))
12005 typedef int (*out_sym_func
)
12006 (void *, const char *, Elf_Internal_Sym
*, asection
*,
12007 struct elf_link_hash_entry
*);
12009 if (! ((*bed
->elf_backend_output_arch_local_syms
)
12010 (abfd
, info
, &flinfo
,
12011 (out_sym_func
) elf_link_output_symstrtab
)))
12015 /* That wrote out all the local symbols. Finish up the symbol table
12016 with the global symbols. Even if we want to strip everything we
12017 can, we still need to deal with those global symbols that got
12018 converted to local in a version script. */
12020 /* The sh_info field records the index of the first non local symbol. */
12021 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12024 && htab
->dynsym
!= NULL
12025 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
12027 Elf_Internal_Sym sym
;
12028 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
12030 o
= htab
->dynsym
->output_section
;
12031 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
12033 /* Write out the section symbols for the output sections. */
12034 if (bfd_link_pic (info
)
12035 || htab
->is_relocatable_executable
)
12041 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12043 sym
.st_target_internal
= 0;
12045 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
12051 dynindx
= elf_section_data (s
)->dynindx
;
12054 indx
= elf_section_data (s
)->this_idx
;
12055 BFD_ASSERT (indx
> 0);
12056 sym
.st_shndx
= indx
;
12057 if (! check_dynsym (abfd
, &sym
))
12059 sym
.st_value
= s
->vma
;
12060 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
12061 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12065 /* Write out the local dynsyms. */
12066 if (htab
->dynlocal
)
12068 struct elf_link_local_dynamic_entry
*e
;
12069 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
12074 /* Copy the internal symbol and turn off visibility.
12075 Note that we saved a word of storage and overwrote
12076 the original st_name with the dynstr_index. */
12078 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
12080 s
= bfd_section_from_elf_index (e
->input_bfd
,
12085 elf_section_data (s
->output_section
)->this_idx
;
12086 if (! check_dynsym (abfd
, &sym
))
12088 sym
.st_value
= (s
->output_section
->vma
12090 + e
->isym
.st_value
);
12093 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
12094 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12099 /* We get the global symbols from the hash table. */
12100 eoinfo
.failed
= FALSE
;
12101 eoinfo
.localsyms
= FALSE
;
12102 eoinfo
.flinfo
= &flinfo
;
12103 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12107 /* If backend needs to output some symbols not present in the hash
12108 table, do it now. */
12109 if (bed
->elf_backend_output_arch_syms
12110 && (info
->strip
!= strip_all
|| emit_relocs
))
12112 typedef int (*out_sym_func
)
12113 (void *, const char *, Elf_Internal_Sym
*, asection
*,
12114 struct elf_link_hash_entry
*);
12116 if (! ((*bed
->elf_backend_output_arch_syms
)
12117 (abfd
, info
, &flinfo
,
12118 (out_sym_func
) elf_link_output_symstrtab
)))
12122 /* Finalize the .strtab section. */
12123 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
12125 /* Swap out the .strtab section. */
12126 if (!elf_link_swap_symbols_out (&flinfo
))
12129 /* Now we know the size of the symtab section. */
12130 if (bfd_get_symcount (abfd
) > 0)
12132 /* Finish up and write out the symbol string table (.strtab)
12134 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
12135 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
12137 if (elf_symtab_shndx_list (abfd
))
12139 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
12141 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
12143 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
12144 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
12145 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
12146 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
12147 symtab_shndx_hdr
->sh_size
= amt
;
12149 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
12152 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
12153 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
12158 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
12159 /* sh_name was set in prep_headers. */
12160 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
12161 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
12162 symstrtab_hdr
->sh_addr
= 0;
12163 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
12164 symstrtab_hdr
->sh_entsize
= 0;
12165 symstrtab_hdr
->sh_link
= 0;
12166 symstrtab_hdr
->sh_info
= 0;
12167 /* sh_offset is set just below. */
12168 symstrtab_hdr
->sh_addralign
= 1;
12170 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
12172 elf_next_file_pos (abfd
) = off
;
12174 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
12175 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
12179 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
12181 _bfd_error_handler (_("%B: failed to generate import library"),
12182 info
->out_implib_bfd
);
12186 /* Adjust the relocs to have the correct symbol indices. */
12187 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12189 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12192 if ((o
->flags
& SEC_RELOC
) == 0)
12195 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
12196 if (esdo
->rel
.hdr
!= NULL
12197 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
, info
))
12199 if (esdo
->rela
.hdr
!= NULL
12200 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
, info
))
12203 /* Set the reloc_count field to 0 to prevent write_relocs from
12204 trying to swap the relocs out itself. */
12205 o
->reloc_count
= 0;
12208 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
12209 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
12211 /* If we are linking against a dynamic object, or generating a
12212 shared library, finish up the dynamic linking information. */
12215 bfd_byte
*dyncon
, *dynconend
;
12217 /* Fix up .dynamic entries. */
12218 o
= bfd_get_linker_section (dynobj
, ".dynamic");
12219 BFD_ASSERT (o
!= NULL
);
12221 dyncon
= o
->contents
;
12222 dynconend
= o
->contents
+ o
->size
;
12223 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12225 Elf_Internal_Dyn dyn
;
12228 bfd_size_type sh_size
;
12231 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12238 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
12240 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
12242 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
12243 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
12246 dyn
.d_un
.d_val
= relativecount
;
12253 name
= info
->init_function
;
12256 name
= info
->fini_function
;
12259 struct elf_link_hash_entry
*h
;
12261 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
12263 && (h
->root
.type
== bfd_link_hash_defined
12264 || h
->root
.type
== bfd_link_hash_defweak
))
12266 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
12267 o
= h
->root
.u
.def
.section
;
12268 if (o
->output_section
!= NULL
)
12269 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
12270 + o
->output_offset
);
12273 /* The symbol is imported from another shared
12274 library and does not apply to this one. */
12275 dyn
.d_un
.d_ptr
= 0;
12282 case DT_PREINIT_ARRAYSZ
:
12283 name
= ".preinit_array";
12285 case DT_INIT_ARRAYSZ
:
12286 name
= ".init_array";
12288 case DT_FINI_ARRAYSZ
:
12289 name
= ".fini_array";
12291 o
= bfd_get_section_by_name (abfd
, name
);
12295 (_("could not find section %s"), name
);
12300 (_("warning: %s section has zero size"), name
);
12301 dyn
.d_un
.d_val
= o
->size
;
12304 case DT_PREINIT_ARRAY
:
12305 name
= ".preinit_array";
12307 case DT_INIT_ARRAY
:
12308 name
= ".init_array";
12310 case DT_FINI_ARRAY
:
12311 name
= ".fini_array";
12313 o
= bfd_get_section_by_name (abfd
, name
);
12320 name
= ".gnu.hash";
12329 name
= ".gnu.version_d";
12332 name
= ".gnu.version_r";
12335 name
= ".gnu.version";
12337 o
= bfd_get_linker_section (dynobj
, name
);
12339 if (o
== NULL
|| bfd_is_abs_section (o
->output_section
))
12342 (_("could not find section %s"), name
);
12345 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
12348 (_("warning: section '%s' is being made into a note"), name
);
12349 bfd_set_error (bfd_error_nonrepresentable_section
);
12352 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
12359 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
12365 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12367 Elf_Internal_Shdr
*hdr
;
12369 hdr
= elf_elfsections (abfd
)[i
];
12370 if (hdr
->sh_type
== type
12371 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
12373 sh_size
+= hdr
->sh_size
;
12375 || sh_addr
> hdr
->sh_addr
)
12376 sh_addr
= hdr
->sh_addr
;
12380 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
12382 /* Don't count procedure linkage table relocs in the
12383 overall reloc count. */
12384 sh_size
-= htab
->srelplt
->size
;
12386 /* If the size is zero, make the address zero too.
12387 This is to avoid a glibc bug. If the backend
12388 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12389 zero, then we'll put DT_RELA at the end of
12390 DT_JMPREL. glibc will interpret the end of
12391 DT_RELA matching the end of DT_JMPREL as the
12392 case where DT_RELA includes DT_JMPREL, and for
12393 LD_BIND_NOW will decide that processing DT_RELA
12394 will process the PLT relocs too. Net result:
12395 No PLT relocs applied. */
12398 /* If .rela.plt is the first .rela section, exclude
12399 it from DT_RELA. */
12400 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
12401 + htab
->srelplt
->output_offset
))
12402 sh_addr
+= htab
->srelplt
->size
;
12405 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
12406 dyn
.d_un
.d_val
= sh_size
;
12408 dyn
.d_un
.d_ptr
= sh_addr
;
12411 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
12415 /* If we have created any dynamic sections, then output them. */
12416 if (dynobj
!= NULL
)
12418 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
12421 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12422 if (((info
->warn_shared_textrel
&& bfd_link_pic (info
))
12423 || info
->error_textrel
)
12424 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
)
12426 bfd_byte
*dyncon
, *dynconend
;
12428 dyncon
= o
->contents
;
12429 dynconend
= o
->contents
+ o
->size
;
12430 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12432 Elf_Internal_Dyn dyn
;
12434 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12436 if (dyn
.d_tag
== DT_TEXTREL
)
12438 if (info
->error_textrel
)
12439 info
->callbacks
->einfo
12440 (_("%P%X: read-only segment has dynamic relocations.\n"));
12442 info
->callbacks
->einfo
12443 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12449 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
12451 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
12453 || o
->output_section
== bfd_abs_section_ptr
)
12455 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
12457 /* At this point, we are only interested in sections
12458 created by _bfd_elf_link_create_dynamic_sections. */
12461 if (htab
->stab_info
.stabstr
== o
)
12463 if (htab
->eh_info
.hdr_sec
== o
)
12465 if (strcmp (o
->name
, ".dynstr") != 0)
12467 if (! bfd_set_section_contents (abfd
, o
->output_section
,
12469 (file_ptr
) o
->output_offset
12470 * bfd_octets_per_byte (abfd
),
12476 /* The contents of the .dynstr section are actually in a
12480 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
12481 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
12482 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
12488 if (!info
->resolve_section_groups
)
12490 bfd_boolean failed
= FALSE
;
12492 BFD_ASSERT (bfd_link_relocatable (info
));
12493 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
12498 /* If we have optimized stabs strings, output them. */
12499 if (htab
->stab_info
.stabstr
!= NULL
)
12501 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
12505 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
12508 elf_final_link_free (abfd
, &flinfo
);
12510 elf_linker (abfd
) = TRUE
;
12514 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
12515 if (contents
== NULL
)
12516 return FALSE
; /* Bail out and fail. */
12517 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
12518 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
12525 elf_final_link_free (abfd
, &flinfo
);
12529 /* Initialize COOKIE for input bfd ABFD. */
12532 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
12533 struct bfd_link_info
*info
, bfd
*abfd
)
12535 Elf_Internal_Shdr
*symtab_hdr
;
12536 const struct elf_backend_data
*bed
;
12538 bed
= get_elf_backend_data (abfd
);
12539 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12541 cookie
->abfd
= abfd
;
12542 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
12543 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
12544 if (cookie
->bad_symtab
)
12546 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
12547 cookie
->extsymoff
= 0;
12551 cookie
->locsymcount
= symtab_hdr
->sh_info
;
12552 cookie
->extsymoff
= symtab_hdr
->sh_info
;
12555 if (bed
->s
->arch_size
== 32)
12556 cookie
->r_sym_shift
= 8;
12558 cookie
->r_sym_shift
= 32;
12560 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
12561 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
12563 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
12564 cookie
->locsymcount
, 0,
12566 if (cookie
->locsyms
== NULL
)
12568 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
12571 if (info
->keep_memory
)
12572 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
12577 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12580 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
12582 Elf_Internal_Shdr
*symtab_hdr
;
12584 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12585 if (cookie
->locsyms
!= NULL
12586 && symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
12587 free (cookie
->locsyms
);
12590 /* Initialize the relocation information in COOKIE for input section SEC
12591 of input bfd ABFD. */
12594 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12595 struct bfd_link_info
*info
, bfd
*abfd
,
12598 if (sec
->reloc_count
== 0)
12600 cookie
->rels
= NULL
;
12601 cookie
->relend
= NULL
;
12605 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
12606 info
->keep_memory
);
12607 if (cookie
->rels
== NULL
)
12609 cookie
->rel
= cookie
->rels
;
12610 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
12612 cookie
->rel
= cookie
->rels
;
12616 /* Free the memory allocated by init_reloc_cookie_rels,
12620 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12623 if (cookie
->rels
&& elf_section_data (sec
)->relocs
!= cookie
->rels
)
12624 free (cookie
->rels
);
12627 /* Initialize the whole of COOKIE for input section SEC. */
12630 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12631 struct bfd_link_info
*info
,
12634 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
12636 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
12641 fini_reloc_cookie (cookie
, sec
->owner
);
12646 /* Free the memory allocated by init_reloc_cookie_for_section,
12650 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12653 fini_reloc_cookie_rels (cookie
, sec
);
12654 fini_reloc_cookie (cookie
, sec
->owner
);
12657 /* Garbage collect unused sections. */
12659 /* Default gc_mark_hook. */
12662 _bfd_elf_gc_mark_hook (asection
*sec
,
12663 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12664 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
12665 struct elf_link_hash_entry
*h
,
12666 Elf_Internal_Sym
*sym
)
12670 switch (h
->root
.type
)
12672 case bfd_link_hash_defined
:
12673 case bfd_link_hash_defweak
:
12674 return h
->root
.u
.def
.section
;
12676 case bfd_link_hash_common
:
12677 return h
->root
.u
.c
.p
->section
;
12684 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
12689 /* Return the global debug definition section. */
12692 elf_gc_mark_debug_section (asection
*sec ATTRIBUTE_UNUSED
,
12693 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12694 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
12695 struct elf_link_hash_entry
*h
,
12696 Elf_Internal_Sym
*sym ATTRIBUTE_UNUSED
)
12699 && (h
->root
.type
== bfd_link_hash_defined
12700 || h
->root
.type
== bfd_link_hash_defweak
)
12701 && (h
->root
.u
.def
.section
->flags
& SEC_DEBUGGING
) != 0)
12702 return h
->root
.u
.def
.section
;
12707 /* COOKIE->rel describes a relocation against section SEC, which is
12708 a section we've decided to keep. Return the section that contains
12709 the relocation symbol, or NULL if no section contains it. */
12712 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
12713 elf_gc_mark_hook_fn gc_mark_hook
,
12714 struct elf_reloc_cookie
*cookie
,
12715 bfd_boolean
*start_stop
)
12717 unsigned long r_symndx
;
12718 struct elf_link_hash_entry
*h
;
12720 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
12721 if (r_symndx
== STN_UNDEF
)
12724 if (r_symndx
>= cookie
->locsymcount
12725 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
12727 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
12730 info
->callbacks
->einfo (_("%F%P: corrupt input: %B\n"),
12734 while (h
->root
.type
== bfd_link_hash_indirect
12735 || h
->root
.type
== bfd_link_hash_warning
)
12736 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
12738 /* If this symbol is weak and there is a non-weak definition, we
12739 keep the non-weak definition because many backends put
12740 dynamic reloc info on the non-weak definition for code
12741 handling copy relocs. */
12742 if (h
->u
.weakdef
!= NULL
)
12743 h
->u
.weakdef
->mark
= 1;
12745 if (start_stop
!= NULL
)
12747 /* To work around a glibc bug, mark XXX input sections
12748 when there is a reference to __start_XXX or __stop_XXX
12752 asection
*s
= h
->u2
.start_stop_section
;
12753 *start_stop
= !s
->gc_mark
;
12758 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
12761 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
12762 &cookie
->locsyms
[r_symndx
]);
12765 /* COOKIE->rel describes a relocation against section SEC, which is
12766 a section we've decided to keep. Mark the section that contains
12767 the relocation symbol. */
12770 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
12772 elf_gc_mark_hook_fn gc_mark_hook
,
12773 struct elf_reloc_cookie
*cookie
)
12776 bfd_boolean start_stop
= FALSE
;
12778 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
12779 while (rsec
!= NULL
)
12781 if (!rsec
->gc_mark
)
12783 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
12784 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
12786 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
12791 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
12796 /* The mark phase of garbage collection. For a given section, mark
12797 it and any sections in this section's group, and all the sections
12798 which define symbols to which it refers. */
12801 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
12803 elf_gc_mark_hook_fn gc_mark_hook
)
12806 asection
*group_sec
, *eh_frame
;
12810 /* Mark all the sections in the group. */
12811 group_sec
= elf_section_data (sec
)->next_in_group
;
12812 if (group_sec
&& !group_sec
->gc_mark
)
12813 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
12816 /* Look through the section relocs. */
12818 eh_frame
= elf_eh_frame_section (sec
->owner
);
12819 if ((sec
->flags
& SEC_RELOC
) != 0
12820 && sec
->reloc_count
> 0
12821 && sec
!= eh_frame
)
12823 struct elf_reloc_cookie cookie
;
12825 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
12829 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
12830 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
12835 fini_reloc_cookie_for_section (&cookie
, sec
);
12839 if (ret
&& eh_frame
&& elf_fde_list (sec
))
12841 struct elf_reloc_cookie cookie
;
12843 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
12847 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
12848 gc_mark_hook
, &cookie
))
12850 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
12854 eh_frame
= elf_section_eh_frame_entry (sec
);
12855 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
12856 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
12862 /* Scan and mark sections in a special or debug section group. */
12865 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
12867 /* Point to first section of section group. */
12869 /* Used to iterate the section group. */
12872 bfd_boolean is_special_grp
= TRUE
;
12873 bfd_boolean is_debug_grp
= TRUE
;
12875 /* First scan to see if group contains any section other than debug
12876 and special section. */
12877 ssec
= msec
= elf_next_in_group (grp
);
12880 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
12881 is_debug_grp
= FALSE
;
12883 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
12884 is_special_grp
= FALSE
;
12886 msec
= elf_next_in_group (msec
);
12888 while (msec
!= ssec
);
12890 /* If this is a pure debug section group or pure special section group,
12891 keep all sections in this group. */
12892 if (is_debug_grp
|| is_special_grp
)
12897 msec
= elf_next_in_group (msec
);
12899 while (msec
!= ssec
);
12903 /* Keep debug and special sections. */
12906 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
12907 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED
)
12911 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
12914 bfd_boolean some_kept
;
12915 bfd_boolean debug_frag_seen
;
12916 bfd_boolean has_kept_debug_info
;
12918 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
12920 isec
= ibfd
->sections
;
12921 if (isec
== NULL
|| isec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
12924 /* Ensure all linker created sections are kept,
12925 see if any other section is already marked,
12926 and note if we have any fragmented debug sections. */
12927 debug_frag_seen
= some_kept
= has_kept_debug_info
= FALSE
;
12928 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12930 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
12932 else if (isec
->gc_mark
12933 && (isec
->flags
& SEC_ALLOC
) != 0
12934 && elf_section_type (isec
) != SHT_NOTE
)
12937 if (!debug_frag_seen
12938 && (isec
->flags
& SEC_DEBUGGING
)
12939 && CONST_STRNEQ (isec
->name
, ".debug_line."))
12940 debug_frag_seen
= TRUE
;
12943 /* If no non-note alloc section in this file will be kept, then
12944 we can toss out the debug and special sections. */
12948 /* Keep debug and special sections like .comment when they are
12949 not part of a group. Also keep section groups that contain
12950 just debug sections or special sections. */
12951 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12953 if ((isec
->flags
& SEC_GROUP
) != 0)
12954 _bfd_elf_gc_mark_debug_special_section_group (isec
);
12955 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
12956 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
12957 && elf_next_in_group (isec
) == NULL
)
12959 if (isec
->gc_mark
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
12960 has_kept_debug_info
= TRUE
;
12963 /* Look for CODE sections which are going to be discarded,
12964 and find and discard any fragmented debug sections which
12965 are associated with that code section. */
12966 if (debug_frag_seen
)
12967 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12968 if ((isec
->flags
& SEC_CODE
) != 0
12969 && isec
->gc_mark
== 0)
12974 ilen
= strlen (isec
->name
);
12976 /* Association is determined by the name of the debug
12977 section containing the name of the code section as
12978 a suffix. For example .debug_line.text.foo is a
12979 debug section associated with .text.foo. */
12980 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
12984 if (dsec
->gc_mark
== 0
12985 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
12988 dlen
= strlen (dsec
->name
);
12991 && strncmp (dsec
->name
+ (dlen
- ilen
),
12992 isec
->name
, ilen
) == 0)
12997 /* Mark debug sections referenced by kept debug sections. */
12998 if (has_kept_debug_info
)
12999 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13001 && (isec
->flags
& SEC_DEBUGGING
) != 0)
13002 if (!_bfd_elf_gc_mark (info
, isec
,
13003 elf_gc_mark_debug_section
))
13009 /* The sweep phase of garbage collection. Remove all garbage sections. */
13011 typedef bfd_boolean (*gc_sweep_hook_fn
)
13012 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
13015 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
13018 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13019 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
13021 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13025 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13026 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13029 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13032 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13034 /* When any section in a section group is kept, we keep all
13035 sections in the section group. If the first member of
13036 the section group is excluded, we will also exclude the
13038 if (o
->flags
& SEC_GROUP
)
13040 asection
*first
= elf_next_in_group (o
);
13041 o
->gc_mark
= first
->gc_mark
;
13047 /* Skip sweeping sections already excluded. */
13048 if (o
->flags
& SEC_EXCLUDE
)
13051 /* Since this is early in the link process, it is simple
13052 to remove a section from the output. */
13053 o
->flags
|= SEC_EXCLUDE
;
13055 if (info
->print_gc_sections
&& o
->size
!= 0)
13056 /* xgettext:c-format */
13057 _bfd_error_handler (_("Removing unused section '%A' in file '%B'"),
13060 /* But we also have to update some of the relocation
13061 info we collected before. */
13063 && (o
->flags
& SEC_RELOC
) != 0
13064 && o
->reloc_count
!= 0
13065 && !((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
13066 && (o
->flags
& SEC_DEBUGGING
) != 0)
13067 && !bfd_is_abs_section (o
->output_section
))
13069 Elf_Internal_Rela
*internal_relocs
;
13073 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
13074 info
->keep_memory
);
13075 if (internal_relocs
== NULL
)
13078 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
13080 if (elf_section_data (o
)->relocs
!= internal_relocs
)
13081 free (internal_relocs
);
13092 /* Propagate collected vtable information. This is called through
13093 elf_link_hash_traverse. */
13096 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
13098 /* Those that are not vtables. */
13100 || h
->u2
.vtable
== NULL
13101 || h
->u2
.vtable
->parent
== NULL
)
13104 /* Those vtables that do not have parents, we cannot merge. */
13105 if (h
->u2
.vtable
->parent
== (struct elf_link_hash_entry
*) -1)
13108 /* If we've already been done, exit. */
13109 if (h
->u2
.vtable
->used
&& h
->u2
.vtable
->used
[-1])
13112 /* Make sure the parent's table is up to date. */
13113 elf_gc_propagate_vtable_entries_used (h
->u2
.vtable
->parent
, okp
);
13115 if (h
->u2
.vtable
->used
== NULL
)
13117 /* None of this table's entries were referenced. Re-use the
13119 h
->u2
.vtable
->used
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13120 h
->u2
.vtable
->size
= h
->u2
.vtable
->parent
->u2
.vtable
->size
;
13125 bfd_boolean
*cu
, *pu
;
13127 /* Or the parent's entries into ours. */
13128 cu
= h
->u2
.vtable
->used
;
13130 pu
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13133 const struct elf_backend_data
*bed
;
13134 unsigned int log_file_align
;
13136 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
13137 log_file_align
= bed
->s
->log_file_align
;
13138 n
= h
->u2
.vtable
->parent
->u2
.vtable
->size
>> log_file_align
;
13153 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
13156 bfd_vma hstart
, hend
;
13157 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
13158 const struct elf_backend_data
*bed
;
13159 unsigned int log_file_align
;
13161 /* Take care of both those symbols that do not describe vtables as
13162 well as those that are not loaded. */
13164 || h
->u2
.vtable
== NULL
13165 || h
->u2
.vtable
->parent
== NULL
)
13168 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
13169 || h
->root
.type
== bfd_link_hash_defweak
);
13171 sec
= h
->root
.u
.def
.section
;
13172 hstart
= h
->root
.u
.def
.value
;
13173 hend
= hstart
+ h
->size
;
13175 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
13177 return *(bfd_boolean
*) okp
= FALSE
;
13178 bed
= get_elf_backend_data (sec
->owner
);
13179 log_file_align
= bed
->s
->log_file_align
;
13181 relend
= relstart
+ sec
->reloc_count
;
13183 for (rel
= relstart
; rel
< relend
; ++rel
)
13184 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
13186 /* If the entry is in use, do nothing. */
13187 if (h
->u2
.vtable
->used
13188 && (rel
->r_offset
- hstart
) < h
->u2
.vtable
->size
)
13190 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
13191 if (h
->u2
.vtable
->used
[entry
])
13194 /* Otherwise, kill it. */
13195 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
13201 /* Mark sections containing dynamically referenced symbols. When
13202 building shared libraries, we must assume that any visible symbol is
13206 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
13208 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
13209 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
13211 if ((h
->root
.type
== bfd_link_hash_defined
13212 || h
->root
.type
== bfd_link_hash_defweak
)
13214 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
13215 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
13216 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
13217 && (!bfd_link_executable (info
)
13218 || info
->gc_keep_exported
13219 || info
->export_dynamic
13222 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
13223 && (h
->versioned
>= versioned
13224 || !bfd_hide_sym_by_version (info
->version_info
,
13225 h
->root
.root
.string
)))))
13226 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13231 /* Keep all sections containing symbols undefined on the command-line,
13232 and the section containing the entry symbol. */
13235 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
13237 struct bfd_sym_chain
*sym
;
13239 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
13241 struct elf_link_hash_entry
*h
;
13243 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
13244 FALSE
, FALSE
, FALSE
);
13247 && (h
->root
.type
== bfd_link_hash_defined
13248 || h
->root
.type
== bfd_link_hash_defweak
)
13249 && !bfd_is_abs_section (h
->root
.u
.def
.section
)
13250 && !bfd_is_und_section (h
->root
.u
.def
.section
))
13251 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13256 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
13257 struct bfd_link_info
*info
)
13259 bfd
*ibfd
= info
->input_bfds
;
13261 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13264 struct elf_reloc_cookie cookie
;
13266 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13268 sec
= ibfd
->sections
;
13269 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13272 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
13275 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
13277 if (CONST_STRNEQ (bfd_section_name (ibfd
, sec
), ".eh_frame_entry")
13278 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
13280 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
13281 fini_reloc_cookie_rels (&cookie
, sec
);
13288 /* Do mark and sweep of unused sections. */
13291 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
13293 bfd_boolean ok
= TRUE
;
13295 elf_gc_mark_hook_fn gc_mark_hook
;
13296 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13297 struct elf_link_hash_table
*htab
;
13299 if (!bed
->can_gc_sections
13300 || !is_elf_hash_table (info
->hash
))
13302 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13306 bed
->gc_keep (info
);
13307 htab
= elf_hash_table (info
);
13309 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13310 at the .eh_frame section if we can mark the FDEs individually. */
13311 for (sub
= info
->input_bfds
;
13312 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
13313 sub
= sub
->link
.next
)
13316 struct elf_reloc_cookie cookie
;
13318 sec
= sub
->sections
;
13319 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13321 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
13322 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
13324 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
13325 if (elf_section_data (sec
)->sec_info
13326 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
13327 elf_eh_frame_section (sub
) = sec
;
13328 fini_reloc_cookie_for_section (&cookie
, sec
);
13329 sec
= bfd_get_next_section_by_name (NULL
, sec
);
13333 /* Apply transitive closure to the vtable entry usage info. */
13334 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
13338 /* Kill the vtable relocations that were not used. */
13339 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &ok
);
13343 /* Mark dynamically referenced symbols. */
13344 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
13345 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
13347 /* Grovel through relocs to find out who stays ... */
13348 gc_mark_hook
= bed
->gc_mark_hook
;
13349 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13353 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13354 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13358 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13361 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13362 Also treat note sections as a root, if the section is not part
13364 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13366 && (o
->flags
& SEC_EXCLUDE
) == 0
13367 && ((o
->flags
& SEC_KEEP
) != 0
13368 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
13369 && elf_next_in_group (o
) == NULL
)))
13371 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
13376 /* Allow the backend to mark additional target specific sections. */
13377 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
13379 /* ... and mark SEC_EXCLUDE for those that go. */
13380 return elf_gc_sweep (abfd
, info
);
13383 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13386 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
13388 struct elf_link_hash_entry
*h
,
13391 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
13392 struct elf_link_hash_entry
**search
, *child
;
13393 size_t extsymcount
;
13394 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13396 /* The sh_info field of the symtab header tells us where the
13397 external symbols start. We don't care about the local symbols at
13399 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
13400 if (!elf_bad_symtab (abfd
))
13401 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
13403 sym_hashes
= elf_sym_hashes (abfd
);
13404 sym_hashes_end
= sym_hashes
+ extsymcount
;
13406 /* Hunt down the child symbol, which is in this section at the same
13407 offset as the relocation. */
13408 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
13410 if ((child
= *search
) != NULL
13411 && (child
->root
.type
== bfd_link_hash_defined
13412 || child
->root
.type
== bfd_link_hash_defweak
)
13413 && child
->root
.u
.def
.section
== sec
13414 && child
->root
.u
.def
.value
== offset
)
13418 /* xgettext:c-format */
13419 _bfd_error_handler (_("%B: %A+%#Lx: No symbol found for INHERIT"),
13420 abfd
, sec
, offset
);
13421 bfd_set_error (bfd_error_invalid_operation
);
13425 if (!child
->u2
.vtable
)
13427 child
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
13428 bfd_zalloc (abfd
, sizeof (*child
->u2
.vtable
)));
13429 if (!child
->u2
.vtable
)
13434 /* This *should* only be the absolute section. It could potentially
13435 be that someone has defined a non-global vtable though, which
13436 would be bad. It isn't worth paging in the local symbols to be
13437 sure though; that case should simply be handled by the assembler. */
13439 child
->u2
.vtable
->parent
= (struct elf_link_hash_entry
*) -1;
13442 child
->u2
.vtable
->parent
= h
;
13447 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13450 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
13451 asection
*sec ATTRIBUTE_UNUSED
,
13452 struct elf_link_hash_entry
*h
,
13455 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13456 unsigned int log_file_align
= bed
->s
->log_file_align
;
13460 h
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
13461 bfd_zalloc (abfd
, sizeof (*h
->u2
.vtable
)));
13466 if (addend
>= h
->u2
.vtable
->size
)
13468 size_t size
, bytes
, file_align
;
13469 bfd_boolean
*ptr
= h
->u2
.vtable
->used
;
13471 /* While the symbol is undefined, we have to be prepared to handle
13473 file_align
= 1 << log_file_align
;
13474 if (h
->root
.type
== bfd_link_hash_undefined
)
13475 size
= addend
+ file_align
;
13479 if (addend
>= size
)
13481 /* Oops! We've got a reference past the defined end of
13482 the table. This is probably a bug -- shall we warn? */
13483 size
= addend
+ file_align
;
13486 size
= (size
+ file_align
- 1) & -file_align
;
13488 /* Allocate one extra entry for use as a "done" flag for the
13489 consolidation pass. */
13490 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
13494 ptr
= (bfd_boolean
*) bfd_realloc (ptr
- 1, bytes
);
13500 oldbytes
= (((h
->u2
.vtable
->size
>> log_file_align
) + 1)
13501 * sizeof (bfd_boolean
));
13502 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
13506 ptr
= (bfd_boolean
*) bfd_zmalloc (bytes
);
13511 /* And arrange for that done flag to be at index -1. */
13512 h
->u2
.vtable
->used
= ptr
+ 1;
13513 h
->u2
.vtable
->size
= size
;
13516 h
->u2
.vtable
->used
[addend
>> log_file_align
] = TRUE
;
13521 /* Map an ELF section header flag to its corresponding string. */
13525 flagword flag_value
;
13526 } elf_flags_to_name_table
;
13528 static elf_flags_to_name_table elf_flags_to_names
[] =
13530 { "SHF_WRITE", SHF_WRITE
},
13531 { "SHF_ALLOC", SHF_ALLOC
},
13532 { "SHF_EXECINSTR", SHF_EXECINSTR
},
13533 { "SHF_MERGE", SHF_MERGE
},
13534 { "SHF_STRINGS", SHF_STRINGS
},
13535 { "SHF_INFO_LINK", SHF_INFO_LINK
},
13536 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
13537 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
13538 { "SHF_GROUP", SHF_GROUP
},
13539 { "SHF_TLS", SHF_TLS
},
13540 { "SHF_MASKOS", SHF_MASKOS
},
13541 { "SHF_EXCLUDE", SHF_EXCLUDE
},
13544 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13546 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
13547 struct flag_info
*flaginfo
,
13550 const bfd_vma sh_flags
= elf_section_flags (section
);
13552 if (!flaginfo
->flags_initialized
)
13554 bfd
*obfd
= info
->output_bfd
;
13555 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13556 struct flag_info_list
*tf
= flaginfo
->flag_list
;
13558 int without_hex
= 0;
13560 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
13563 flagword (*lookup
) (char *);
13565 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
13566 if (lookup
!= NULL
)
13568 flagword hexval
= (*lookup
) ((char *) tf
->name
);
13572 if (tf
->with
== with_flags
)
13573 with_hex
|= hexval
;
13574 else if (tf
->with
== without_flags
)
13575 without_hex
|= hexval
;
13580 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
13582 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
13584 if (tf
->with
== with_flags
)
13585 with_hex
|= elf_flags_to_names
[i
].flag_value
;
13586 else if (tf
->with
== without_flags
)
13587 without_hex
|= elf_flags_to_names
[i
].flag_value
;
13594 info
->callbacks
->einfo
13595 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
13599 flaginfo
->flags_initialized
= TRUE
;
13600 flaginfo
->only_with_flags
|= with_hex
;
13601 flaginfo
->not_with_flags
|= without_hex
;
13604 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
13607 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
13613 struct alloc_got_off_arg
{
13615 struct bfd_link_info
*info
;
13618 /* We need a special top-level link routine to convert got reference counts
13619 to real got offsets. */
13622 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
13624 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
13625 bfd
*obfd
= gofarg
->info
->output_bfd
;
13626 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13628 if (h
->got
.refcount
> 0)
13630 h
->got
.offset
= gofarg
->gotoff
;
13631 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
13634 h
->got
.offset
= (bfd_vma
) -1;
13639 /* And an accompanying bit to work out final got entry offsets once
13640 we're done. Should be called from final_link. */
13643 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
13644 struct bfd_link_info
*info
)
13647 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13649 struct alloc_got_off_arg gofarg
;
13651 BFD_ASSERT (abfd
== info
->output_bfd
);
13653 if (! is_elf_hash_table (info
->hash
))
13656 /* The GOT offset is relative to the .got section, but the GOT header is
13657 put into the .got.plt section, if the backend uses it. */
13658 if (bed
->want_got_plt
)
13661 gotoff
= bed
->got_header_size
;
13663 /* Do the local .got entries first. */
13664 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
13666 bfd_signed_vma
*local_got
;
13667 size_t j
, locsymcount
;
13668 Elf_Internal_Shdr
*symtab_hdr
;
13670 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
13673 local_got
= elf_local_got_refcounts (i
);
13677 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
13678 if (elf_bad_symtab (i
))
13679 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13681 locsymcount
= symtab_hdr
->sh_info
;
13683 for (j
= 0; j
< locsymcount
; ++j
)
13685 if (local_got
[j
] > 0)
13687 local_got
[j
] = gotoff
;
13688 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
13691 local_got
[j
] = (bfd_vma
) -1;
13695 /* Then the global .got entries. .plt refcounts are handled by
13696 adjust_dynamic_symbol */
13697 gofarg
.gotoff
= gotoff
;
13698 gofarg
.info
= info
;
13699 elf_link_hash_traverse (elf_hash_table (info
),
13700 elf_gc_allocate_got_offsets
,
13705 /* Many folk need no more in the way of final link than this, once
13706 got entry reference counting is enabled. */
13709 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
13711 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
13714 /* Invoke the regular ELF backend linker to do all the work. */
13715 return bfd_elf_final_link (abfd
, info
);
13719 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
13721 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
13723 if (rcookie
->bad_symtab
)
13724 rcookie
->rel
= rcookie
->rels
;
13726 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
13728 unsigned long r_symndx
;
13730 if (! rcookie
->bad_symtab
)
13731 if (rcookie
->rel
->r_offset
> offset
)
13733 if (rcookie
->rel
->r_offset
!= offset
)
13736 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
13737 if (r_symndx
== STN_UNDEF
)
13740 if (r_symndx
>= rcookie
->locsymcount
13741 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13743 struct elf_link_hash_entry
*h
;
13745 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
13747 while (h
->root
.type
== bfd_link_hash_indirect
13748 || h
->root
.type
== bfd_link_hash_warning
)
13749 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13751 if ((h
->root
.type
== bfd_link_hash_defined
13752 || h
->root
.type
== bfd_link_hash_defweak
)
13753 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
13754 || h
->root
.u
.def
.section
->kept_section
!= NULL
13755 || discarded_section (h
->root
.u
.def
.section
)))
13760 /* It's not a relocation against a global symbol,
13761 but it could be a relocation against a local
13762 symbol for a discarded section. */
13764 Elf_Internal_Sym
*isym
;
13766 /* Need to: get the symbol; get the section. */
13767 isym
= &rcookie
->locsyms
[r_symndx
];
13768 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
13770 && (isec
->kept_section
!= NULL
13771 || discarded_section (isec
)))
13779 /* Discard unneeded references to discarded sections.
13780 Returns -1 on error, 1 if any section's size was changed, 0 if
13781 nothing changed. This function assumes that the relocations are in
13782 sorted order, which is true for all known assemblers. */
13785 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
13787 struct elf_reloc_cookie cookie
;
13792 if (info
->traditional_format
13793 || !is_elf_hash_table (info
->hash
))
13796 o
= bfd_get_section_by_name (output_bfd
, ".stab");
13801 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13804 || i
->reloc_count
== 0
13805 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
13809 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13812 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13815 if (_bfd_discard_section_stabs (abfd
, i
,
13816 elf_section_data (i
)->sec_info
,
13817 bfd_elf_reloc_symbol_deleted_p
,
13821 fini_reloc_cookie_for_section (&cookie
, i
);
13826 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
13827 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
13831 int eh_changed
= 0;
13832 unsigned int eh_alignment
;
13834 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13840 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13843 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13846 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
13847 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
13848 bfd_elf_reloc_symbol_deleted_p
,
13852 if (i
->size
!= i
->rawsize
)
13856 fini_reloc_cookie_for_section (&cookie
, i
);
13859 eh_alignment
= 1 << o
->alignment_power
;
13860 /* Skip over zero terminator, and prevent empty sections from
13861 adding alignment padding at the end. */
13862 for (i
= o
->map_tail
.s
; i
!= NULL
; i
= i
->map_tail
.s
)
13864 i
->flags
|= SEC_EXCLUDE
;
13865 else if (i
->size
> 4)
13867 /* The last non-empty eh_frame section doesn't need padding. */
13870 /* Any prior sections must pad the last FDE out to the output
13871 section alignment. Otherwise we might have zero padding
13872 between sections, which would be seen as a terminator. */
13873 for (; i
!= NULL
; i
= i
->map_tail
.s
)
13875 /* All but the last zero terminator should have been removed. */
13880 = (i
->size
+ eh_alignment
- 1) & -eh_alignment
;
13881 if (i
->size
!= size
)
13889 elf_link_hash_traverse (elf_hash_table (info
),
13890 _bfd_elf_adjust_eh_frame_global_symbol
, NULL
);
13893 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
13895 const struct elf_backend_data
*bed
;
13898 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13900 s
= abfd
->sections
;
13901 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13904 bed
= get_elf_backend_data (abfd
);
13906 if (bed
->elf_backend_discard_info
!= NULL
)
13908 if (!init_reloc_cookie (&cookie
, info
, abfd
))
13911 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
13914 fini_reloc_cookie (&cookie
, abfd
);
13918 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
13919 _bfd_elf_end_eh_frame_parsing (info
);
13921 if (info
->eh_frame_hdr_type
13922 && !bfd_link_relocatable (info
)
13923 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
13930 _bfd_elf_section_already_linked (bfd
*abfd
,
13932 struct bfd_link_info
*info
)
13935 const char *name
, *key
;
13936 struct bfd_section_already_linked
*l
;
13937 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
13939 if (sec
->output_section
== bfd_abs_section_ptr
)
13942 flags
= sec
->flags
;
13944 /* Return if it isn't a linkonce section. A comdat group section
13945 also has SEC_LINK_ONCE set. */
13946 if ((flags
& SEC_LINK_ONCE
) == 0)
13949 /* Don't put group member sections on our list of already linked
13950 sections. They are handled as a group via their group section. */
13951 if (elf_sec_group (sec
) != NULL
)
13954 /* For a SHT_GROUP section, use the group signature as the key. */
13956 if ((flags
& SEC_GROUP
) != 0
13957 && elf_next_in_group (sec
) != NULL
13958 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
13959 key
= elf_group_name (elf_next_in_group (sec
));
13962 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13963 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
13964 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
13967 /* Must be a user linkonce section that doesn't follow gcc's
13968 naming convention. In this case we won't be matching
13969 single member groups. */
13973 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
13975 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13977 /* We may have 2 different types of sections on the list: group
13978 sections with a signature of <key> (<key> is some string),
13979 and linkonce sections named .gnu.linkonce.<type>.<key>.
13980 Match like sections. LTO plugin sections are an exception.
13981 They are always named .gnu.linkonce.t.<key> and match either
13982 type of section. */
13983 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
13984 && ((flags
& SEC_GROUP
) != 0
13985 || strcmp (name
, l
->sec
->name
) == 0))
13986 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0)
13988 /* The section has already been linked. See if we should
13989 issue a warning. */
13990 if (!_bfd_handle_already_linked (sec
, l
, info
))
13993 if (flags
& SEC_GROUP
)
13995 asection
*first
= elf_next_in_group (sec
);
13996 asection
*s
= first
;
14000 s
->output_section
= bfd_abs_section_ptr
;
14001 /* Record which group discards it. */
14002 s
->kept_section
= l
->sec
;
14003 s
= elf_next_in_group (s
);
14004 /* These lists are circular. */
14014 /* A single member comdat group section may be discarded by a
14015 linkonce section and vice versa. */
14016 if ((flags
& SEC_GROUP
) != 0)
14018 asection
*first
= elf_next_in_group (sec
);
14020 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
14021 /* Check this single member group against linkonce sections. */
14022 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14023 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14024 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
14026 first
->output_section
= bfd_abs_section_ptr
;
14027 first
->kept_section
= l
->sec
;
14028 sec
->output_section
= bfd_abs_section_ptr
;
14033 /* Check this linkonce section against single member groups. */
14034 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14035 if (l
->sec
->flags
& SEC_GROUP
)
14037 asection
*first
= elf_next_in_group (l
->sec
);
14040 && elf_next_in_group (first
) == first
14041 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
14043 sec
->output_section
= bfd_abs_section_ptr
;
14044 sec
->kept_section
= first
;
14049 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14050 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14051 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14052 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14053 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14054 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14055 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14056 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14057 The reverse order cannot happen as there is never a bfd with only the
14058 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14059 matter as here were are looking only for cross-bfd sections. */
14061 if ((flags
& SEC_GROUP
) == 0 && CONST_STRNEQ (name
, ".gnu.linkonce.r."))
14062 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14063 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14064 && CONST_STRNEQ (l
->sec
->name
, ".gnu.linkonce.t."))
14066 if (abfd
!= l
->sec
->owner
)
14067 sec
->output_section
= bfd_abs_section_ptr
;
14071 /* This is the first section with this name. Record it. */
14072 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
14073 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
14074 return sec
->output_section
== bfd_abs_section_ptr
;
14078 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
14080 return sym
->st_shndx
== SHN_COMMON
;
14084 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
14090 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
14092 return bfd_com_section_ptr
;
14096 _bfd_elf_default_got_elt_size (bfd
*abfd
,
14097 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
14098 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
14099 bfd
*ibfd ATTRIBUTE_UNUSED
,
14100 unsigned long symndx ATTRIBUTE_UNUSED
)
14102 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14103 return bed
->s
->arch_size
/ 8;
14106 /* Routines to support the creation of dynamic relocs. */
14108 /* Returns the name of the dynamic reloc section associated with SEC. */
14110 static const char *
14111 get_dynamic_reloc_section_name (bfd
* abfd
,
14113 bfd_boolean is_rela
)
14116 const char *old_name
= bfd_get_section_name (NULL
, sec
);
14117 const char *prefix
= is_rela
? ".rela" : ".rel";
14119 if (old_name
== NULL
)
14122 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
14123 sprintf (name
, "%s%s", prefix
, old_name
);
14128 /* Returns the dynamic reloc section associated with SEC.
14129 If necessary compute the name of the dynamic reloc section based
14130 on SEC's name (looked up in ABFD's string table) and the setting
14134 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
14136 bfd_boolean is_rela
)
14138 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14140 if (reloc_sec
== NULL
)
14142 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14146 reloc_sec
= bfd_get_linker_section (abfd
, name
);
14148 if (reloc_sec
!= NULL
)
14149 elf_section_data (sec
)->sreloc
= reloc_sec
;
14156 /* Returns the dynamic reloc section associated with SEC. If the
14157 section does not exist it is created and attached to the DYNOBJ
14158 bfd and stored in the SRELOC field of SEC's elf_section_data
14161 ALIGNMENT is the alignment for the newly created section and
14162 IS_RELA defines whether the name should be .rela.<SEC's name>
14163 or .rel.<SEC's name>. The section name is looked up in the
14164 string table associated with ABFD. */
14167 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
14169 unsigned int alignment
,
14171 bfd_boolean is_rela
)
14173 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14175 if (reloc_sec
== NULL
)
14177 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14182 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
14184 if (reloc_sec
== NULL
)
14186 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
14187 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
14188 if ((sec
->flags
& SEC_ALLOC
) != 0)
14189 flags
|= SEC_ALLOC
| SEC_LOAD
;
14191 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
14192 if (reloc_sec
!= NULL
)
14194 /* _bfd_elf_get_sec_type_attr chooses a section type by
14195 name. Override as it may be wrong, eg. for a user
14196 section named "auto" we'll get ".relauto" which is
14197 seen to be a .rela section. */
14198 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
14199 if (! bfd_set_section_alignment (dynobj
, reloc_sec
, alignment
))
14204 elf_section_data (sec
)->sreloc
= reloc_sec
;
14210 /* Copy the ELF symbol type and other attributes for a linker script
14211 assignment from HSRC to HDEST. Generally this should be treated as
14212 if we found a strong non-dynamic definition for HDEST (except that
14213 ld ignores multiple definition errors). */
14215 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
14216 struct bfd_link_hash_entry
*hdest
,
14217 struct bfd_link_hash_entry
*hsrc
)
14219 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
14220 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
14221 Elf_Internal_Sym isym
;
14223 ehdest
->type
= ehsrc
->type
;
14224 ehdest
->target_internal
= ehsrc
->target_internal
;
14226 isym
.st_other
= ehsrc
->other
;
14227 elf_merge_st_other (abfd
, ehdest
, &isym
, NULL
, TRUE
, FALSE
);
14230 /* Append a RELA relocation REL to section S in BFD. */
14233 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14235 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14236 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
14237 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
14238 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
14241 /* Append a REL relocation REL to section S in BFD. */
14244 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14246 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14247 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
14248 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
14249 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);
14252 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14254 struct bfd_link_hash_entry
*
14255 bfd_elf_define_start_stop (struct bfd_link_info
*info
,
14256 const char *symbol
, asection
*sec
)
14258 struct elf_link_hash_entry
*h
;
14260 h
= elf_link_hash_lookup (elf_hash_table (info
), symbol
,
14261 FALSE
, FALSE
, TRUE
);
14263 && (h
->root
.type
== bfd_link_hash_undefined
14264 || h
->root
.type
== bfd_link_hash_undefweak
14265 || (h
->ref_regular
&& !h
->def_regular
)))
14267 h
->root
.type
= bfd_link_hash_defined
;
14268 h
->root
.u
.def
.section
= sec
;
14269 h
->root
.u
.def
.value
= 0;
14270 h
->def_regular
= 1;
14271 h
->def_dynamic
= 0;
14273 h
->u2
.start_stop_section
= sec
;
14274 if (symbol
[0] == '.')
14276 /* .startof. and .sizeof. symbols are local. */
14277 _bfd_elf_link_hash_hide_symbol (info
, h
, TRUE
);
14279 else if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
14280 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED
;