1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2020 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
27 #include "safe-ctype.h"
28 #include "libiberty.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
42 /* This struct is used to pass information to routines called via
43 elf_link_hash_traverse which must return failure. */
45 struct elf_info_failed
47 struct bfd_link_info
*info
;
51 /* This structure is used to pass information to
52 _bfd_elf_link_find_version_dependencies. */
54 struct elf_find_verdep_info
56 /* General link information. */
57 struct bfd_link_info
*info
;
58 /* The number of dependencies. */
60 /* Whether we had a failure. */
64 static bfd_boolean _bfd_elf_fix_symbol_flags
65 (struct elf_link_hash_entry
*, struct elf_info_failed
*);
68 _bfd_elf_section_for_symbol (struct elf_reloc_cookie
*cookie
,
69 unsigned long r_symndx
,
72 if (r_symndx
>= cookie
->locsymcount
73 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
75 struct elf_link_hash_entry
*h
;
77 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
79 while (h
->root
.type
== bfd_link_hash_indirect
80 || h
->root
.type
== bfd_link_hash_warning
)
81 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
83 if ((h
->root
.type
== bfd_link_hash_defined
84 || h
->root
.type
== bfd_link_hash_defweak
)
85 && discarded_section (h
->root
.u
.def
.section
))
86 return h
->root
.u
.def
.section
;
92 /* It's not a relocation against a global symbol,
93 but it could be a relocation against a local
94 symbol for a discarded section. */
96 Elf_Internal_Sym
*isym
;
98 /* Need to: get the symbol; get the section. */
99 isym
= &cookie
->locsyms
[r_symndx
];
100 isec
= bfd_section_from_elf_index (cookie
->abfd
, isym
->st_shndx
);
102 && discard
? discarded_section (isec
) : 1)
108 /* Define a symbol in a dynamic linkage section. */
110 struct elf_link_hash_entry
*
111 _bfd_elf_define_linkage_sym (bfd
*abfd
,
112 struct bfd_link_info
*info
,
116 struct elf_link_hash_entry
*h
;
117 struct bfd_link_hash_entry
*bh
;
118 const struct elf_backend_data
*bed
;
120 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
123 /* Zap symbol defined in an as-needed lib that wasn't linked.
124 This is a symptom of a larger problem: Absolute symbols
125 defined in shared libraries can't be overridden, because we
126 lose the link to the bfd which is via the symbol section. */
127 h
->root
.type
= bfd_link_hash_new
;
133 bed
= get_elf_backend_data (abfd
);
134 if (!_bfd_generic_link_add_one_symbol (info
, abfd
, name
, BSF_GLOBAL
,
135 sec
, 0, NULL
, FALSE
, bed
->collect
,
138 h
= (struct elf_link_hash_entry
*) bh
;
139 BFD_ASSERT (h
!= NULL
);
142 h
->root
.linker_def
= 1;
143 h
->type
= STT_OBJECT
;
144 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
145 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
147 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
152 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
156 struct elf_link_hash_entry
*h
;
157 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
158 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
160 /* This function may be called more than once. */
161 if (htab
->sgot
!= NULL
)
164 flags
= bed
->dynamic_sec_flags
;
166 s
= bfd_make_section_anyway_with_flags (abfd
,
167 (bed
->rela_plts_and_copies_p
168 ? ".rela.got" : ".rel.got"),
169 (bed
->dynamic_sec_flags
172 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
176 s
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
178 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
182 if (bed
->want_got_plt
)
184 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
186 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
191 /* The first bit of the global offset table is the header. */
192 s
->size
+= bed
->got_header_size
;
194 if (bed
->want_got_sym
)
196 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
197 (or .got.plt) section. We don't do this in the linker script
198 because we don't want to define the symbol if we are not creating
199 a global offset table. */
200 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
201 "_GLOBAL_OFFSET_TABLE_");
202 elf_hash_table (info
)->hgot
= h
;
210 /* Create a strtab to hold the dynamic symbol names. */
212 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
214 struct elf_link_hash_table
*hash_table
;
216 hash_table
= elf_hash_table (info
);
217 if (hash_table
->dynobj
== NULL
)
219 /* We may not set dynobj, an input file holding linker created
220 dynamic sections to abfd, which may be a dynamic object with
221 its own dynamic sections. We need to find a normal input file
222 to hold linker created sections if possible. */
223 if ((abfd
->flags
& (DYNAMIC
| BFD_PLUGIN
)) != 0)
227 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link
.next
)
229 & (DYNAMIC
| BFD_LINKER_CREATED
| BFD_PLUGIN
)) == 0
230 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
231 && elf_object_id (ibfd
) == elf_hash_table_id (hash_table
)
232 && !((s
= ibfd
->sections
) != NULL
233 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
))
239 hash_table
->dynobj
= abfd
;
242 if (hash_table
->dynstr
== NULL
)
244 hash_table
->dynstr
= _bfd_elf_strtab_init ();
245 if (hash_table
->dynstr
== NULL
)
251 /* Create some sections which will be filled in with dynamic linking
252 information. ABFD is an input file which requires dynamic sections
253 to be created. The dynamic sections take up virtual memory space
254 when the final executable is run, so we need to create them before
255 addresses are assigned to the output sections. We work out the
256 actual contents and size of these sections later. */
259 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
263 const struct elf_backend_data
*bed
;
264 struct elf_link_hash_entry
*h
;
266 if (! is_elf_hash_table (info
->hash
))
269 if (elf_hash_table (info
)->dynamic_sections_created
)
272 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
275 abfd
= elf_hash_table (info
)->dynobj
;
276 bed
= get_elf_backend_data (abfd
);
278 flags
= bed
->dynamic_sec_flags
;
280 /* A dynamically linked executable has a .interp section, but a
281 shared library does not. */
282 if (bfd_link_executable (info
) && !info
->nointerp
)
284 s
= bfd_make_section_anyway_with_flags (abfd
, ".interp",
285 flags
| SEC_READONLY
);
290 /* Create sections to hold version informations. These are removed
291 if they are not needed. */
292 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_d",
293 flags
| SEC_READONLY
);
295 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
298 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version",
299 flags
| SEC_READONLY
);
301 || !bfd_set_section_alignment (s
, 1))
304 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_r",
305 flags
| SEC_READONLY
);
307 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
310 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynsym",
311 flags
| SEC_READONLY
);
313 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
315 elf_hash_table (info
)->dynsym
= s
;
317 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynstr",
318 flags
| SEC_READONLY
);
322 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynamic", flags
);
324 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
327 /* The special symbol _DYNAMIC is always set to the start of the
328 .dynamic section. We could set _DYNAMIC in a linker script, but we
329 only want to define it if we are, in fact, creating a .dynamic
330 section. We don't want to define it if there is no .dynamic
331 section, since on some ELF platforms the start up code examines it
332 to decide how to initialize the process. */
333 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC");
334 elf_hash_table (info
)->hdynamic
= h
;
340 s
= bfd_make_section_anyway_with_flags (abfd
, ".hash",
341 flags
| SEC_READONLY
);
343 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
345 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
348 if (info
->emit_gnu_hash
&& bed
->record_xhash_symbol
== NULL
)
350 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.hash",
351 flags
| SEC_READONLY
);
353 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
355 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
356 4 32-bit words followed by variable count of 64-bit words, then
357 variable count of 32-bit words. */
358 if (bed
->s
->arch_size
== 64)
359 elf_section_data (s
)->this_hdr
.sh_entsize
= 0;
361 elf_section_data (s
)->this_hdr
.sh_entsize
= 4;
364 /* Let the backend create the rest of the sections. This lets the
365 backend set the right flags. The backend will normally create
366 the .got and .plt sections. */
367 if (bed
->elf_backend_create_dynamic_sections
== NULL
368 || ! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
371 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
376 /* Create dynamic sections when linking against a dynamic object. */
379 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
381 flagword flags
, pltflags
;
382 struct elf_link_hash_entry
*h
;
384 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
385 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
387 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
388 .rel[a].bss sections. */
389 flags
= bed
->dynamic_sec_flags
;
392 if (bed
->plt_not_loaded
)
393 /* We do not clear SEC_ALLOC here because we still want the OS to
394 allocate space for the section; it's just that there's nothing
395 to read in from the object file. */
396 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
398 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
399 if (bed
->plt_readonly
)
400 pltflags
|= SEC_READONLY
;
402 s
= bfd_make_section_anyway_with_flags (abfd
, ".plt", pltflags
);
404 || !bfd_set_section_alignment (s
, bed
->plt_alignment
))
408 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
410 if (bed
->want_plt_sym
)
412 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
413 "_PROCEDURE_LINKAGE_TABLE_");
414 elf_hash_table (info
)->hplt
= h
;
419 s
= bfd_make_section_anyway_with_flags (abfd
,
420 (bed
->rela_plts_and_copies_p
421 ? ".rela.plt" : ".rel.plt"),
422 flags
| SEC_READONLY
);
424 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
428 if (! _bfd_elf_create_got_section (abfd
, info
))
431 if (bed
->want_dynbss
)
433 /* The .dynbss section is a place to put symbols which are defined
434 by dynamic objects, are referenced by regular objects, and are
435 not functions. We must allocate space for them in the process
436 image and use a R_*_COPY reloc to tell the dynamic linker to
437 initialize them at run time. The linker script puts the .dynbss
438 section into the .bss section of the final image. */
439 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynbss",
440 SEC_ALLOC
| SEC_LINKER_CREATED
);
445 if (bed
->want_dynrelro
)
447 /* Similarly, but for symbols that were originally in read-only
448 sections. This section doesn't really need to have contents,
449 but make it like other .data.rel.ro sections. */
450 s
= bfd_make_section_anyway_with_flags (abfd
, ".data.rel.ro",
457 /* The .rel[a].bss section holds copy relocs. This section is not
458 normally needed. We need to create it here, though, so that the
459 linker will map it to an output section. We can't just create it
460 only if we need it, because we will not know whether we need it
461 until we have seen all the input files, and the first time the
462 main linker code calls BFD after examining all the input files
463 (size_dynamic_sections) the input sections have already been
464 mapped to the output sections. If the section turns out not to
465 be needed, we can discard it later. We will never need this
466 section when generating a shared object, since they do not use
468 if (bfd_link_executable (info
))
470 s
= bfd_make_section_anyway_with_flags (abfd
,
471 (bed
->rela_plts_and_copies_p
472 ? ".rela.bss" : ".rel.bss"),
473 flags
| SEC_READONLY
);
475 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
479 if (bed
->want_dynrelro
)
481 s
= (bfd_make_section_anyway_with_flags
482 (abfd
, (bed
->rela_plts_and_copies_p
483 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
484 flags
| SEC_READONLY
));
486 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
488 htab
->sreldynrelro
= s
;
496 /* Record a new dynamic symbol. We record the dynamic symbols as we
497 read the input files, since we need to have a list of all of them
498 before we can determine the final sizes of the output sections.
499 Note that we may actually call this function even though we are not
500 going to output any dynamic symbols; in some cases we know that a
501 symbol should be in the dynamic symbol table, but only if there is
505 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
506 struct elf_link_hash_entry
*h
)
508 if (h
->dynindx
== -1)
510 struct elf_strtab_hash
*dynstr
;
515 if (h
->root
.type
== bfd_link_hash_defined
516 || h
->root
.type
== bfd_link_hash_defweak
)
518 /* An IR symbol should not be made dynamic. */
519 if (h
->root
.u
.def
.section
!= NULL
520 && h
->root
.u
.def
.section
->owner
!= NULL
521 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
525 /* XXX: The ABI draft says the linker must turn hidden and
526 internal symbols into STB_LOCAL symbols when producing the
527 DSO. However, if ld.so honors st_other in the dynamic table,
528 this would not be necessary. */
529 switch (ELF_ST_VISIBILITY (h
->other
))
533 if (h
->root
.type
!= bfd_link_hash_undefined
534 && h
->root
.type
!= bfd_link_hash_undefweak
)
537 if (!elf_hash_table (info
)->is_relocatable_executable
)
545 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
546 ++elf_hash_table (info
)->dynsymcount
;
548 dynstr
= elf_hash_table (info
)->dynstr
;
551 /* Create a strtab to hold the dynamic symbol names. */
552 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
557 /* We don't put any version information in the dynamic string
559 name
= h
->root
.root
.string
;
560 p
= strchr (name
, ELF_VER_CHR
);
562 /* We know that the p points into writable memory. In fact,
563 there are only a few symbols that have read-only names, being
564 those like _GLOBAL_OFFSET_TABLE_ that are created specially
565 by the backends. Most symbols will have names pointing into
566 an ELF string table read from a file, or to objalloc memory. */
569 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
574 if (indx
== (size_t) -1)
576 h
->dynstr_index
= indx
;
582 /* Mark a symbol dynamic. */
585 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
586 struct elf_link_hash_entry
*h
,
587 Elf_Internal_Sym
*sym
)
589 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
591 /* It may be called more than once on the same H. */
592 if(h
->dynamic
|| bfd_link_relocatable (info
))
595 if ((info
->dynamic_data
596 && (h
->type
== STT_OBJECT
597 || h
->type
== STT_COMMON
599 && (ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
600 || ELF_ST_TYPE (sym
->st_info
) == STT_COMMON
))))
603 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
606 /* NB: If a symbol is made dynamic by --dynamic-list, it has
608 h
->root
.non_ir_ref_dynamic
= 1;
612 /* Record an assignment to a symbol made by a linker script. We need
613 this in case some dynamic object refers to this symbol. */
616 bfd_elf_record_link_assignment (bfd
*output_bfd
,
617 struct bfd_link_info
*info
,
622 struct elf_link_hash_entry
*h
, *hv
;
623 struct elf_link_hash_table
*htab
;
624 const struct elf_backend_data
*bed
;
626 if (!is_elf_hash_table (info
->hash
))
629 htab
= elf_hash_table (info
);
630 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
634 if (h
->root
.type
== bfd_link_hash_warning
)
635 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
637 if (h
->versioned
== unknown
)
639 /* Set versioned if symbol version is unknown. */
640 char *version
= strrchr (name
, ELF_VER_CHR
);
643 if (version
> name
&& version
[-1] != ELF_VER_CHR
)
644 h
->versioned
= versioned_hidden
;
646 h
->versioned
= versioned
;
650 /* Symbols defined in a linker script but not referenced anywhere
651 else will have non_elf set. */
654 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
658 switch (h
->root
.type
)
660 case bfd_link_hash_defined
:
661 case bfd_link_hash_defweak
:
662 case bfd_link_hash_common
:
664 case bfd_link_hash_undefweak
:
665 case bfd_link_hash_undefined
:
666 /* Since we're defining the symbol, don't let it seem to have not
667 been defined. record_dynamic_symbol and size_dynamic_sections
668 may depend on this. */
669 h
->root
.type
= bfd_link_hash_new
;
670 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
671 bfd_link_repair_undef_list (&htab
->root
);
673 case bfd_link_hash_new
:
675 case bfd_link_hash_indirect
:
676 /* We had a versioned symbol in a dynamic library. We make the
677 the versioned symbol point to this one. */
678 bed
= get_elf_backend_data (output_bfd
);
680 while (hv
->root
.type
== bfd_link_hash_indirect
681 || hv
->root
.type
== bfd_link_hash_warning
)
682 hv
= (struct elf_link_hash_entry
*) hv
->root
.u
.i
.link
;
683 /* We don't need to update h->root.u since linker will set them
685 h
->root
.type
= bfd_link_hash_undefined
;
686 hv
->root
.type
= bfd_link_hash_indirect
;
687 hv
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
688 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hv
);
695 /* If this symbol is being provided by the linker script, and it is
696 currently defined by a dynamic object, but not by a regular
697 object, then mark it as undefined so that the generic linker will
698 force the correct value. */
702 h
->root
.type
= bfd_link_hash_undefined
;
704 /* If this symbol is currently defined by a dynamic object, but not
705 by a regular object, then clear out any version information because
706 the symbol will not be associated with the dynamic object any
708 if (h
->def_dynamic
&& !h
->def_regular
)
709 h
->verinfo
.verdef
= NULL
;
711 /* Make sure this symbol is not garbage collected. */
718 bed
= get_elf_backend_data (output_bfd
);
719 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
720 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
721 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
724 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
726 if (!bfd_link_relocatable (info
)
728 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
729 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
734 || bfd_link_dll (info
)
735 || elf_hash_table (info
)->is_relocatable_executable
)
739 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
742 /* If this is a weak defined symbol, and we know a corresponding
743 real symbol from the same dynamic object, make sure the real
744 symbol is also made into a dynamic symbol. */
747 struct elf_link_hash_entry
*def
= weakdef (h
);
749 if (def
->dynindx
== -1
750 && !bfd_elf_link_record_dynamic_symbol (info
, def
))
758 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
759 success, and 2 on a failure caused by attempting to record a symbol
760 in a discarded section, eg. a discarded link-once section symbol. */
763 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
768 struct elf_link_local_dynamic_entry
*entry
;
769 struct elf_link_hash_table
*eht
;
770 struct elf_strtab_hash
*dynstr
;
773 Elf_External_Sym_Shndx eshndx
;
774 char esym
[sizeof (Elf64_External_Sym
)];
776 if (! is_elf_hash_table (info
->hash
))
779 /* See if the entry exists already. */
780 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
781 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
784 amt
= sizeof (*entry
);
785 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
789 /* Go find the symbol, so that we can find it's name. */
790 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
791 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
793 bfd_release (input_bfd
, entry
);
797 if (entry
->isym
.st_shndx
!= SHN_UNDEF
798 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
802 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
803 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
805 /* We can still bfd_release here as nothing has done another
806 bfd_alloc. We can't do this later in this function. */
807 bfd_release (input_bfd
, entry
);
812 name
= (bfd_elf_string_from_elf_section
813 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
814 entry
->isym
.st_name
));
816 dynstr
= elf_hash_table (info
)->dynstr
;
819 /* Create a strtab to hold the dynamic symbol names. */
820 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
825 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
826 if (dynstr_index
== (size_t) -1)
828 entry
->isym
.st_name
= dynstr_index
;
830 eht
= elf_hash_table (info
);
832 entry
->next
= eht
->dynlocal
;
833 eht
->dynlocal
= entry
;
834 entry
->input_bfd
= input_bfd
;
835 entry
->input_indx
= input_indx
;
838 /* Whatever binding the symbol had before, it's now local. */
840 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
842 /* The dynindx will be set at the end of size_dynamic_sections. */
847 /* Return the dynindex of a local dynamic symbol. */
850 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
854 struct elf_link_local_dynamic_entry
*e
;
856 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
857 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
862 /* This function is used to renumber the dynamic symbols, if some of
863 them are removed because they are marked as local. This is called
864 via elf_link_hash_traverse. */
867 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
870 size_t *count
= (size_t *) data
;
875 if (h
->dynindx
!= -1)
876 h
->dynindx
= ++(*count
);
882 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
883 STB_LOCAL binding. */
886 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
889 size_t *count
= (size_t *) data
;
891 if (!h
->forced_local
)
894 if (h
->dynindx
!= -1)
895 h
->dynindx
= ++(*count
);
900 /* Return true if the dynamic symbol for a given section should be
901 omitted when creating a shared library. */
903 _bfd_elf_omit_section_dynsym_default (bfd
*output_bfd ATTRIBUTE_UNUSED
,
904 struct bfd_link_info
*info
,
907 struct elf_link_hash_table
*htab
;
910 switch (elf_section_data (p
)->this_hdr
.sh_type
)
914 /* If sh_type is yet undecided, assume it could be
915 SHT_PROGBITS/SHT_NOBITS. */
917 htab
= elf_hash_table (info
);
918 if (htab
->text_index_section
!= NULL
)
919 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
921 return (htab
->dynobj
!= NULL
922 && (ip
= bfd_get_linker_section (htab
->dynobj
, p
->name
)) != NULL
923 && ip
->output_section
== p
);
925 /* There shouldn't be section relative relocations
926 against any other section. */
933 _bfd_elf_omit_section_dynsym_all
934 (bfd
*output_bfd ATTRIBUTE_UNUSED
,
935 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
936 asection
*p ATTRIBUTE_UNUSED
)
941 /* Assign dynsym indices. In a shared library we generate a section
942 symbol for each output section, which come first. Next come symbols
943 which have been forced to local binding. Then all of the back-end
944 allocated local dynamic syms, followed by the rest of the global
945 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
946 (This prevents the early call before elf_backend_init_index_section
947 and strip_excluded_output_sections setting dynindx for sections
948 that are stripped.) */
951 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
952 struct bfd_link_info
*info
,
953 unsigned long *section_sym_count
)
955 unsigned long dynsymcount
= 0;
956 bfd_boolean do_sec
= section_sym_count
!= NULL
;
958 if (bfd_link_pic (info
)
959 || elf_hash_table (info
)->is_relocatable_executable
)
961 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
963 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
964 if ((p
->flags
& SEC_EXCLUDE
) == 0
965 && (p
->flags
& SEC_ALLOC
) != 0
966 && elf_hash_table (info
)->dynamic_relocs
967 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
971 elf_section_data (p
)->dynindx
= dynsymcount
;
974 elf_section_data (p
)->dynindx
= 0;
977 *section_sym_count
= dynsymcount
;
979 elf_link_hash_traverse (elf_hash_table (info
),
980 elf_link_renumber_local_hash_table_dynsyms
,
983 if (elf_hash_table (info
)->dynlocal
)
985 struct elf_link_local_dynamic_entry
*p
;
986 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
987 p
->dynindx
= ++dynsymcount
;
989 elf_hash_table (info
)->local_dynsymcount
= dynsymcount
;
991 elf_link_hash_traverse (elf_hash_table (info
),
992 elf_link_renumber_hash_table_dynsyms
,
995 /* There is an unused NULL entry at the head of the table which we
996 must account for in our count even if the table is empty since it
997 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1001 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
1005 /* Merge st_other field. */
1008 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
1009 unsigned int st_other
, asection
*sec
,
1010 bfd_boolean definition
, bfd_boolean dynamic
)
1012 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1014 /* If st_other has a processor-specific meaning, specific
1015 code might be needed here. */
1016 if (bed
->elf_backend_merge_symbol_attribute
)
1017 (*bed
->elf_backend_merge_symbol_attribute
) (h
, st_other
, definition
,
1022 unsigned symvis
= ELF_ST_VISIBILITY (st_other
);
1023 unsigned hvis
= ELF_ST_VISIBILITY (h
->other
);
1025 /* Keep the most constraining visibility. Leave the remainder
1026 of the st_other field to elf_backend_merge_symbol_attribute. */
1027 if (symvis
- 1 < hvis
- 1)
1028 h
->other
= symvis
| (h
->other
& ~ELF_ST_VISIBILITY (-1));
1031 && ELF_ST_VISIBILITY (st_other
) != STV_DEFAULT
1032 && (sec
->flags
& SEC_READONLY
) == 0)
1033 h
->protected_def
= 1;
1036 /* This function is called when we want to merge a new symbol with an
1037 existing symbol. It handles the various cases which arise when we
1038 find a definition in a dynamic object, or when there is already a
1039 definition in a dynamic object. The new symbol is described by
1040 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1041 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1042 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1043 of an old common symbol. We set OVERRIDE if the old symbol is
1044 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1045 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1046 to change. By OK to change, we mean that we shouldn't warn if the
1047 type or size does change. */
1050 _bfd_elf_merge_symbol (bfd
*abfd
,
1051 struct bfd_link_info
*info
,
1053 Elf_Internal_Sym
*sym
,
1056 struct elf_link_hash_entry
**sym_hash
,
1058 bfd_boolean
*pold_weak
,
1059 unsigned int *pold_alignment
,
1062 bfd_boolean
*type_change_ok
,
1063 bfd_boolean
*size_change_ok
,
1064 bfd_boolean
*matched
)
1066 asection
*sec
, *oldsec
;
1067 struct elf_link_hash_entry
*h
;
1068 struct elf_link_hash_entry
*hi
;
1069 struct elf_link_hash_entry
*flip
;
1072 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
1073 bfd_boolean newweak
, oldweak
, newfunc
, oldfunc
;
1074 const struct elf_backend_data
*bed
;
1076 bfd_boolean default_sym
= *matched
;
1082 bind
= ELF_ST_BIND (sym
->st_info
);
1084 if (! bfd_is_und_section (sec
))
1085 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
1087 h
= ((struct elf_link_hash_entry
*)
1088 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
1093 bed
= get_elf_backend_data (abfd
);
1095 /* NEW_VERSION is the symbol version of the new symbol. */
1096 if (h
->versioned
!= unversioned
)
1098 /* Symbol version is unknown or versioned. */
1099 new_version
= strrchr (name
, ELF_VER_CHR
);
1102 if (h
->versioned
== unknown
)
1104 if (new_version
> name
&& new_version
[-1] != ELF_VER_CHR
)
1105 h
->versioned
= versioned_hidden
;
1107 h
->versioned
= versioned
;
1110 if (new_version
[0] == '\0')
1114 h
->versioned
= unversioned
;
1119 /* For merging, we only care about real symbols. But we need to make
1120 sure that indirect symbol dynamic flags are updated. */
1122 while (h
->root
.type
== bfd_link_hash_indirect
1123 || h
->root
.type
== bfd_link_hash_warning
)
1124 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1128 if (hi
== h
|| h
->root
.type
== bfd_link_hash_new
)
1132 /* OLD_HIDDEN is true if the existing symbol is only visible
1133 to the symbol with the same symbol version. NEW_HIDDEN is
1134 true if the new symbol is only visible to the symbol with
1135 the same symbol version. */
1136 bfd_boolean old_hidden
= h
->versioned
== versioned_hidden
;
1137 bfd_boolean new_hidden
= hi
->versioned
== versioned_hidden
;
1138 if (!old_hidden
&& !new_hidden
)
1139 /* The new symbol matches the existing symbol if both
1144 /* OLD_VERSION is the symbol version of the existing
1148 if (h
->versioned
>= versioned
)
1149 old_version
= strrchr (h
->root
.root
.string
,
1154 /* The new symbol matches the existing symbol if they
1155 have the same symbol version. */
1156 *matched
= (old_version
== new_version
1157 || (old_version
!= NULL
1158 && new_version
!= NULL
1159 && strcmp (old_version
, new_version
) == 0));
1164 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1169 switch (h
->root
.type
)
1174 case bfd_link_hash_undefined
:
1175 case bfd_link_hash_undefweak
:
1176 oldbfd
= h
->root
.u
.undef
.abfd
;
1179 case bfd_link_hash_defined
:
1180 case bfd_link_hash_defweak
:
1181 oldbfd
= h
->root
.u
.def
.section
->owner
;
1182 oldsec
= h
->root
.u
.def
.section
;
1185 case bfd_link_hash_common
:
1186 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1187 oldsec
= h
->root
.u
.c
.p
->section
;
1189 *pold_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1192 if (poldbfd
&& *poldbfd
== NULL
)
1195 /* Differentiate strong and weak symbols. */
1196 newweak
= bind
== STB_WEAK
;
1197 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1198 || h
->root
.type
== bfd_link_hash_undefweak
);
1200 *pold_weak
= oldweak
;
1202 /* We have to check it for every instance since the first few may be
1203 references and not all compilers emit symbol type for undefined
1205 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1207 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1208 respectively, is from a dynamic object. */
1210 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1212 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1213 syms and defined syms in dynamic libraries respectively.
1214 ref_dynamic on the other hand can be set for a symbol defined in
1215 a dynamic library, and def_dynamic may not be set; When the
1216 definition in a dynamic lib is overridden by a definition in the
1217 executable use of the symbol in the dynamic lib becomes a
1218 reference to the executable symbol. */
1221 if (bfd_is_und_section (sec
))
1223 if (bind
!= STB_WEAK
)
1225 h
->ref_dynamic_nonweak
= 1;
1226 hi
->ref_dynamic_nonweak
= 1;
1231 /* Update the existing symbol only if they match. */
1234 hi
->dynamic_def
= 1;
1238 /* If we just created the symbol, mark it as being an ELF symbol.
1239 Other than that, there is nothing to do--there is no merge issue
1240 with a newly defined symbol--so we just return. */
1242 if (h
->root
.type
== bfd_link_hash_new
)
1248 /* In cases involving weak versioned symbols, we may wind up trying
1249 to merge a symbol with itself. Catch that here, to avoid the
1250 confusion that results if we try to override a symbol with
1251 itself. The additional tests catch cases like
1252 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1253 dynamic object, which we do want to handle here. */
1255 && (newweak
|| oldweak
)
1256 && ((abfd
->flags
& DYNAMIC
) == 0
1257 || !h
->def_regular
))
1262 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1263 else if (oldsec
!= NULL
)
1265 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1266 indices used by MIPS ELF. */
1267 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1270 /* Handle a case where plugin_notice won't be called and thus won't
1271 set the non_ir_ref flags on the first pass over symbols. */
1273 && (oldbfd
->flags
& BFD_PLUGIN
) != (abfd
->flags
& BFD_PLUGIN
)
1274 && newdyn
!= olddyn
)
1276 h
->root
.non_ir_ref_dynamic
= TRUE
;
1277 hi
->root
.non_ir_ref_dynamic
= TRUE
;
1280 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1281 respectively, appear to be a definition rather than reference. */
1283 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1285 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1286 && h
->root
.type
!= bfd_link_hash_undefweak
1287 && h
->root
.type
!= bfd_link_hash_common
);
1289 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1290 respectively, appear to be a function. */
1292 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1293 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1295 oldfunc
= (h
->type
!= STT_NOTYPE
1296 && bed
->is_function_type (h
->type
));
1298 if (!(newfunc
&& oldfunc
)
1299 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1300 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1301 && h
->type
!= STT_NOTYPE
1302 && (newdef
|| bfd_is_com_section (sec
))
1303 && (olddef
|| h
->root
.type
== bfd_link_hash_common
))
1305 /* If creating a default indirect symbol ("foo" or "foo@") from
1306 a dynamic versioned definition ("foo@@") skip doing so if
1307 there is an existing regular definition with a different
1308 type. We don't want, for example, a "time" variable in the
1309 executable overriding a "time" function in a shared library. */
1317 /* When adding a symbol from a regular object file after we have
1318 created indirect symbols, undo the indirection and any
1325 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1326 h
->forced_local
= 0;
1330 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1332 h
->root
.type
= bfd_link_hash_undefined
;
1333 h
->root
.u
.undef
.abfd
= abfd
;
1337 h
->root
.type
= bfd_link_hash_new
;
1338 h
->root
.u
.undef
.abfd
= NULL
;
1344 /* Check TLS symbols. We don't check undefined symbols introduced
1345 by "ld -u" which have no type (and oldbfd NULL), and we don't
1346 check symbols from plugins because they also have no type. */
1348 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1349 && (abfd
->flags
& BFD_PLUGIN
) == 0
1350 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1351 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1354 bfd_boolean ntdef
, tdef
;
1355 asection
*ntsec
, *tsec
;
1357 if (h
->type
== STT_TLS
)
1378 /* xgettext:c-format */
1379 (_("%s: TLS definition in %pB section %pA "
1380 "mismatches non-TLS definition in %pB section %pA"),
1381 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
, ntsec
);
1382 else if (!tdef
&& !ntdef
)
1384 /* xgettext:c-format */
1385 (_("%s: TLS reference in %pB "
1386 "mismatches non-TLS reference in %pB"),
1387 h
->root
.root
.string
, tbfd
, ntbfd
);
1390 /* xgettext:c-format */
1391 (_("%s: TLS definition in %pB section %pA "
1392 "mismatches non-TLS reference in %pB"),
1393 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
);
1396 /* xgettext:c-format */
1397 (_("%s: TLS reference in %pB "
1398 "mismatches non-TLS definition in %pB section %pA"),
1399 h
->root
.root
.string
, tbfd
, ntbfd
, ntsec
);
1401 bfd_set_error (bfd_error_bad_value
);
1405 /* If the old symbol has non-default visibility, we ignore the new
1406 definition from a dynamic object. */
1408 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1409 && !bfd_is_und_section (sec
))
1412 /* Make sure this symbol is dynamic. */
1414 hi
->ref_dynamic
= 1;
1415 /* A protected symbol has external availability. Make sure it is
1416 recorded as dynamic.
1418 FIXME: Should we check type and size for protected symbol? */
1419 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1420 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1425 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1428 /* If the new symbol with non-default visibility comes from a
1429 relocatable file and the old definition comes from a dynamic
1430 object, we remove the old definition. */
1431 if (hi
->root
.type
== bfd_link_hash_indirect
)
1433 /* Handle the case where the old dynamic definition is
1434 default versioned. We need to copy the symbol info from
1435 the symbol with default version to the normal one if it
1436 was referenced before. */
1439 hi
->root
.type
= h
->root
.type
;
1440 h
->root
.type
= bfd_link_hash_indirect
;
1441 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1443 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1444 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1446 /* If the new symbol is hidden or internal, completely undo
1447 any dynamic link state. */
1448 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1449 h
->forced_local
= 0;
1456 /* FIXME: Should we check type and size for protected symbol? */
1466 /* If the old symbol was undefined before, then it will still be
1467 on the undefs list. If the new symbol is undefined or
1468 common, we can't make it bfd_link_hash_new here, because new
1469 undefined or common symbols will be added to the undefs list
1470 by _bfd_generic_link_add_one_symbol. Symbols may not be
1471 added twice to the undefs list. Also, if the new symbol is
1472 undefweak then we don't want to lose the strong undef. */
1473 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1475 h
->root
.type
= bfd_link_hash_undefined
;
1476 h
->root
.u
.undef
.abfd
= abfd
;
1480 h
->root
.type
= bfd_link_hash_new
;
1481 h
->root
.u
.undef
.abfd
= NULL
;
1484 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1486 /* If the new symbol is hidden or internal, completely undo
1487 any dynamic link state. */
1488 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1489 h
->forced_local
= 0;
1495 /* FIXME: Should we check type and size for protected symbol? */
1501 /* If a new weak symbol definition comes from a regular file and the
1502 old symbol comes from a dynamic library, we treat the new one as
1503 strong. Similarly, an old weak symbol definition from a regular
1504 file is treated as strong when the new symbol comes from a dynamic
1505 library. Further, an old weak symbol from a dynamic library is
1506 treated as strong if the new symbol is from a dynamic library.
1507 This reflects the way glibc's ld.so works.
1509 Also allow a weak symbol to override a linker script symbol
1510 defined by an early pass over the script. This is done so the
1511 linker knows the symbol is defined in an object file, for the
1512 DEFINED script function.
1514 Do this before setting *type_change_ok or *size_change_ok so that
1515 we warn properly when dynamic library symbols are overridden. */
1517 if (newdef
&& !newdyn
&& (olddyn
|| h
->root
.ldscript_def
))
1519 if (olddef
&& newdyn
)
1522 /* Allow changes between different types of function symbol. */
1523 if (newfunc
&& oldfunc
)
1524 *type_change_ok
= TRUE
;
1526 /* It's OK to change the type if either the existing symbol or the
1527 new symbol is weak. A type change is also OK if the old symbol
1528 is undefined and the new symbol is defined. */
1533 && h
->root
.type
== bfd_link_hash_undefined
))
1534 *type_change_ok
= TRUE
;
1536 /* It's OK to change the size if either the existing symbol or the
1537 new symbol is weak, or if the old symbol is undefined. */
1540 || h
->root
.type
== bfd_link_hash_undefined
)
1541 *size_change_ok
= TRUE
;
1543 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1544 symbol, respectively, appears to be a common symbol in a dynamic
1545 object. If a symbol appears in an uninitialized section, and is
1546 not weak, and is not a function, then it may be a common symbol
1547 which was resolved when the dynamic object was created. We want
1548 to treat such symbols specially, because they raise special
1549 considerations when setting the symbol size: if the symbol
1550 appears as a common symbol in a regular object, and the size in
1551 the regular object is larger, we must make sure that we use the
1552 larger size. This problematic case can always be avoided in C,
1553 but it must be handled correctly when using Fortran shared
1556 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1557 likewise for OLDDYNCOMMON and OLDDEF.
1559 Note that this test is just a heuristic, and that it is quite
1560 possible to have an uninitialized symbol in a shared object which
1561 is really a definition, rather than a common symbol. This could
1562 lead to some minor confusion when the symbol really is a common
1563 symbol in some regular object. However, I think it will be
1569 && (sec
->flags
& SEC_ALLOC
) != 0
1570 && (sec
->flags
& SEC_LOAD
) == 0
1573 newdyncommon
= TRUE
;
1575 newdyncommon
= FALSE
;
1579 && h
->root
.type
== bfd_link_hash_defined
1581 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1582 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1585 olddyncommon
= TRUE
;
1587 olddyncommon
= FALSE
;
1589 /* We now know everything about the old and new symbols. We ask the
1590 backend to check if we can merge them. */
1591 if (bed
->merge_symbol
!= NULL
)
1593 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1598 /* There are multiple definitions of a normal symbol. Skip the
1599 default symbol as well as definition from an IR object. */
1600 if (olddef
&& !olddyn
&& !oldweak
&& newdef
&& !newdyn
&& !newweak
1601 && !default_sym
&& h
->def_regular
1603 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1604 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1606 /* Handle a multiple definition. */
1607 (*info
->callbacks
->multiple_definition
) (info
, &h
->root
,
1608 abfd
, sec
, *pvalue
);
1613 /* If both the old and the new symbols look like common symbols in a
1614 dynamic object, set the size of the symbol to the larger of the
1619 && sym
->st_size
!= h
->size
)
1621 /* Since we think we have two common symbols, issue a multiple
1622 common warning if desired. Note that we only warn if the
1623 size is different. If the size is the same, we simply let
1624 the old symbol override the new one as normally happens with
1625 symbols defined in dynamic objects. */
1627 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1628 bfd_link_hash_common
, sym
->st_size
);
1629 if (sym
->st_size
> h
->size
)
1630 h
->size
= sym
->st_size
;
1632 *size_change_ok
= TRUE
;
1635 /* If we are looking at a dynamic object, and we have found a
1636 definition, we need to see if the symbol was already defined by
1637 some other object. If so, we want to use the existing
1638 definition, and we do not want to report a multiple symbol
1639 definition error; we do this by clobbering *PSEC to be
1640 bfd_und_section_ptr.
1642 We treat a common symbol as a definition if the symbol in the
1643 shared library is a function, since common symbols always
1644 represent variables; this can cause confusion in principle, but
1645 any such confusion would seem to indicate an erroneous program or
1646 shared library. We also permit a common symbol in a regular
1647 object to override a weak symbol in a shared object. */
1652 || (h
->root
.type
== bfd_link_hash_common
1653 && (newweak
|| newfunc
))))
1657 newdyncommon
= FALSE
;
1659 *psec
= sec
= bfd_und_section_ptr
;
1660 *size_change_ok
= TRUE
;
1662 /* If we get here when the old symbol is a common symbol, then
1663 we are explicitly letting it override a weak symbol or
1664 function in a dynamic object, and we don't want to warn about
1665 a type change. If the old symbol is a defined symbol, a type
1666 change warning may still be appropriate. */
1668 if (h
->root
.type
== bfd_link_hash_common
)
1669 *type_change_ok
= TRUE
;
1672 /* Handle the special case of an old common symbol merging with a
1673 new symbol which looks like a common symbol in a shared object.
1674 We change *PSEC and *PVALUE to make the new symbol look like a
1675 common symbol, and let _bfd_generic_link_add_one_symbol do the
1679 && h
->root
.type
== bfd_link_hash_common
)
1683 newdyncommon
= FALSE
;
1684 *pvalue
= sym
->st_size
;
1685 *psec
= sec
= bed
->common_section (oldsec
);
1686 *size_change_ok
= TRUE
;
1689 /* Skip weak definitions of symbols that are already defined. */
1690 if (newdef
&& olddef
&& newweak
)
1692 /* Don't skip new non-IR weak syms. */
1693 if (!(oldbfd
!= NULL
1694 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1695 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1701 /* Merge st_other. If the symbol already has a dynamic index,
1702 but visibility says it should not be visible, turn it into a
1704 elf_merge_st_other (abfd
, h
, sym
->st_other
, sec
, newdef
, newdyn
);
1705 if (h
->dynindx
!= -1)
1706 switch (ELF_ST_VISIBILITY (h
->other
))
1710 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1715 /* If the old symbol is from a dynamic object, and the new symbol is
1716 a definition which is not from a dynamic object, then the new
1717 symbol overrides the old symbol. Symbols from regular files
1718 always take precedence over symbols from dynamic objects, even if
1719 they are defined after the dynamic object in the link.
1721 As above, we again permit a common symbol in a regular object to
1722 override a definition in a shared object if the shared object
1723 symbol is a function or is weak. */
1728 || (bfd_is_com_section (sec
)
1729 && (oldweak
|| oldfunc
)))
1734 /* Change the hash table entry to undefined, and let
1735 _bfd_generic_link_add_one_symbol do the right thing with the
1738 h
->root
.type
= bfd_link_hash_undefined
;
1739 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1740 *size_change_ok
= TRUE
;
1743 olddyncommon
= FALSE
;
1745 /* We again permit a type change when a common symbol may be
1746 overriding a function. */
1748 if (bfd_is_com_section (sec
))
1752 /* If a common symbol overrides a function, make sure
1753 that it isn't defined dynamically nor has type
1756 h
->type
= STT_NOTYPE
;
1758 *type_change_ok
= TRUE
;
1761 if (hi
->root
.type
== bfd_link_hash_indirect
)
1764 /* This union may have been set to be non-NULL when this symbol
1765 was seen in a dynamic object. We must force the union to be
1766 NULL, so that it is correct for a regular symbol. */
1767 h
->verinfo
.vertree
= NULL
;
1770 /* Handle the special case of a new common symbol merging with an
1771 old symbol that looks like it might be a common symbol defined in
1772 a shared object. Note that we have already handled the case in
1773 which a new common symbol should simply override the definition
1774 in the shared library. */
1777 && bfd_is_com_section (sec
)
1780 /* It would be best if we could set the hash table entry to a
1781 common symbol, but we don't know what to use for the section
1782 or the alignment. */
1783 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1784 bfd_link_hash_common
, sym
->st_size
);
1786 /* If the presumed common symbol in the dynamic object is
1787 larger, pretend that the new symbol has its size. */
1789 if (h
->size
> *pvalue
)
1792 /* We need to remember the alignment required by the symbol
1793 in the dynamic object. */
1794 BFD_ASSERT (pold_alignment
);
1795 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1798 olddyncommon
= FALSE
;
1800 h
->root
.type
= bfd_link_hash_undefined
;
1801 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1803 *size_change_ok
= TRUE
;
1804 *type_change_ok
= TRUE
;
1806 if (hi
->root
.type
== bfd_link_hash_indirect
)
1809 h
->verinfo
.vertree
= NULL
;
1814 /* Handle the case where we had a versioned symbol in a dynamic
1815 library and now find a definition in a normal object. In this
1816 case, we make the versioned symbol point to the normal one. */
1817 flip
->root
.type
= h
->root
.type
;
1818 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1819 h
->root
.type
= bfd_link_hash_indirect
;
1820 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1821 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1825 flip
->ref_dynamic
= 1;
1832 /* This function is called to create an indirect symbol from the
1833 default for the symbol with the default version if needed. The
1834 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1835 set DYNSYM if the new indirect symbol is dynamic. */
1838 _bfd_elf_add_default_symbol (bfd
*abfd
,
1839 struct bfd_link_info
*info
,
1840 struct elf_link_hash_entry
*h
,
1842 Elf_Internal_Sym
*sym
,
1846 bfd_boolean
*dynsym
)
1848 bfd_boolean type_change_ok
;
1849 bfd_boolean size_change_ok
;
1852 struct elf_link_hash_entry
*hi
;
1853 struct bfd_link_hash_entry
*bh
;
1854 const struct elf_backend_data
*bed
;
1855 bfd_boolean collect
;
1856 bfd_boolean dynamic
;
1859 size_t len
, shortlen
;
1861 bfd_boolean matched
;
1863 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1866 /* If this symbol has a version, and it is the default version, we
1867 create an indirect symbol from the default name to the fully
1868 decorated name. This will cause external references which do not
1869 specify a version to be bound to this version of the symbol. */
1870 p
= strchr (name
, ELF_VER_CHR
);
1871 if (h
->versioned
== unknown
)
1875 h
->versioned
= unversioned
;
1880 if (p
[1] != ELF_VER_CHR
)
1882 h
->versioned
= versioned_hidden
;
1886 h
->versioned
= versioned
;
1891 /* PR ld/19073: We may see an unversioned definition after the
1897 bed
= get_elf_backend_data (abfd
);
1898 collect
= bed
->collect
;
1899 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1901 shortlen
= p
- name
;
1902 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1903 if (shortname
== NULL
)
1905 memcpy (shortname
, name
, shortlen
);
1906 shortname
[shortlen
] = '\0';
1908 /* We are going to create a new symbol. Merge it with any existing
1909 symbol with this name. For the purposes of the merge, act as
1910 though we were defining the symbol we just defined, although we
1911 actually going to define an indirect symbol. */
1912 type_change_ok
= FALSE
;
1913 size_change_ok
= FALSE
;
1916 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1917 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1918 &type_change_ok
, &size_change_ok
, &matched
))
1924 if (hi
->def_regular
|| ELF_COMMON_DEF_P (hi
))
1926 /* If the undecorated symbol will have a version added by a
1927 script different to H, then don't indirect to/from the
1928 undecorated symbol. This isn't ideal because we may not yet
1929 have seen symbol versions, if given by a script on the
1930 command line rather than via --version-script. */
1931 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
1936 = bfd_find_version_for_sym (info
->version_info
,
1937 hi
->root
.root
.string
, &hide
);
1938 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
1940 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
1944 if (hi
->verinfo
.vertree
!= NULL
1945 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
1951 /* Add the default symbol if not performing a relocatable link. */
1952 if (! bfd_link_relocatable (info
))
1955 if (bh
->type
== bfd_link_hash_defined
1956 && bh
->u
.def
.section
->owner
!= NULL
1957 && (bh
->u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
1959 /* Mark the previous definition from IR object as
1960 undefined so that the generic linker will override
1962 bh
->type
= bfd_link_hash_undefined
;
1963 bh
->u
.undef
.abfd
= bh
->u
.def
.section
->owner
;
1965 if (! (_bfd_generic_link_add_one_symbol
1966 (info
, abfd
, shortname
, BSF_INDIRECT
,
1967 bfd_ind_section_ptr
,
1968 0, name
, FALSE
, collect
, &bh
)))
1970 hi
= (struct elf_link_hash_entry
*) bh
;
1975 /* In this case the symbol named SHORTNAME is overriding the
1976 indirect symbol we want to add. We were planning on making
1977 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1978 is the name without a version. NAME is the fully versioned
1979 name, and it is the default version.
1981 Overriding means that we already saw a definition for the
1982 symbol SHORTNAME in a regular object, and it is overriding
1983 the symbol defined in the dynamic object.
1985 When this happens, we actually want to change NAME, the
1986 symbol we just added, to refer to SHORTNAME. This will cause
1987 references to NAME in the shared object to become references
1988 to SHORTNAME in the regular object. This is what we expect
1989 when we override a function in a shared object: that the
1990 references in the shared object will be mapped to the
1991 definition in the regular object. */
1993 while (hi
->root
.type
== bfd_link_hash_indirect
1994 || hi
->root
.type
== bfd_link_hash_warning
)
1995 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1997 h
->root
.type
= bfd_link_hash_indirect
;
1998 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
2002 hi
->ref_dynamic
= 1;
2006 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
2011 /* Now set HI to H, so that the following code will set the
2012 other fields correctly. */
2016 /* Check if HI is a warning symbol. */
2017 if (hi
->root
.type
== bfd_link_hash_warning
)
2018 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2020 /* If there is a duplicate definition somewhere, then HI may not
2021 point to an indirect symbol. We will have reported an error to
2022 the user in that case. */
2024 if (hi
->root
.type
== bfd_link_hash_indirect
)
2026 struct elf_link_hash_entry
*ht
;
2028 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2029 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
2031 /* If we first saw a reference to SHORTNAME with non-default
2032 visibility, merge that visibility to the @@VER symbol. */
2033 elf_merge_st_other (abfd
, ht
, hi
->other
, sec
, TRUE
, dynamic
);
2035 /* A reference to the SHORTNAME symbol from a dynamic library
2036 will be satisfied by the versioned symbol at runtime. In
2037 effect, we have a reference to the versioned symbol. */
2038 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2039 hi
->dynamic_def
|= ht
->dynamic_def
;
2041 /* See if the new flags lead us to realize that the symbol must
2047 if (! bfd_link_executable (info
)
2054 if (hi
->ref_regular
)
2060 /* We also need to define an indirection from the nondefault version
2064 len
= strlen (name
);
2065 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
2066 if (shortname
== NULL
)
2068 memcpy (shortname
, name
, shortlen
);
2069 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
2071 /* Once again, merge with any existing symbol. */
2072 type_change_ok
= FALSE
;
2073 size_change_ok
= FALSE
;
2075 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
2076 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
2077 &type_change_ok
, &size_change_ok
, &matched
))
2083 && h
->root
.type
== bfd_link_hash_defweak
2084 && hi
->root
.type
== bfd_link_hash_defined
)
2086 /* We are handling a weak sym@@ver and attempting to define
2087 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2088 new weak sym@ver because there is already a strong sym@ver.
2089 However, sym@ver and sym@@ver are really the same symbol.
2090 The existing strong sym@ver ought to override sym@@ver. */
2091 h
->root
.type
= bfd_link_hash_defined
;
2092 h
->root
.u
.def
.section
= hi
->root
.u
.def
.section
;
2093 h
->root
.u
.def
.value
= hi
->root
.u
.def
.value
;
2094 hi
->root
.type
= bfd_link_hash_indirect
;
2095 hi
->root
.u
.i
.link
= &h
->root
;
2102 /* Here SHORTNAME is a versioned name, so we don't expect to see
2103 the type of override we do in the case above unless it is
2104 overridden by a versioned definition. */
2105 if (hi
->root
.type
!= bfd_link_hash_defined
2106 && hi
->root
.type
!= bfd_link_hash_defweak
)
2108 /* xgettext:c-format */
2109 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2116 if (! (_bfd_generic_link_add_one_symbol
2117 (info
, abfd
, shortname
, BSF_INDIRECT
,
2118 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
2120 hi
= (struct elf_link_hash_entry
*) bh
;
2123 /* If there is a duplicate definition somewhere, then HI may not
2124 point to an indirect symbol. We will have reported an error
2125 to the user in that case. */
2126 if (hi
->root
.type
== bfd_link_hash_indirect
)
2128 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
2129 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2130 hi
->dynamic_def
|= h
->dynamic_def
;
2132 /* If we first saw a reference to @VER symbol with
2133 non-default visibility, merge that visibility to the
2135 elf_merge_st_other (abfd
, h
, hi
->other
, sec
, TRUE
, dynamic
);
2137 /* See if the new flags lead us to realize that the symbol
2143 if (! bfd_link_executable (info
)
2149 if (hi
->ref_regular
)
2158 /* This routine is used to export all defined symbols into the dynamic
2159 symbol table. It is called via elf_link_hash_traverse. */
2162 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
2164 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2166 /* Ignore indirect symbols. These are added by the versioning code. */
2167 if (h
->root
.type
== bfd_link_hash_indirect
)
2170 /* Ignore this if we won't export it. */
2171 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2174 if (h
->dynindx
== -1
2175 && (h
->def_regular
|| h
->ref_regular
)
2176 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2177 h
->root
.root
.string
))
2179 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2189 /* Look through the symbols which are defined in other shared
2190 libraries and referenced here. Update the list of version
2191 dependencies. This will be put into the .gnu.version_r section.
2192 This function is called via elf_link_hash_traverse. */
2195 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2198 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2199 Elf_Internal_Verneed
*t
;
2200 Elf_Internal_Vernaux
*a
;
2203 /* We only care about symbols defined in shared objects with version
2208 || h
->verinfo
.verdef
== NULL
2209 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2210 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2213 /* See if we already know about this version. */
2214 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2218 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2221 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2222 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2228 /* This is a new version. Add it to tree we are building. */
2233 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2236 rinfo
->failed
= TRUE
;
2240 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2241 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2242 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2246 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2249 rinfo
->failed
= TRUE
;
2253 /* Note that we are copying a string pointer here, and testing it
2254 above. If bfd_elf_string_from_elf_section is ever changed to
2255 discard the string data when low in memory, this will have to be
2257 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2259 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2260 a
->vna_nextptr
= t
->vn_auxptr
;
2262 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2265 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2272 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2273 hidden. Set *T_P to NULL if there is no match. */
2276 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info
*info
,
2277 struct elf_link_hash_entry
*h
,
2278 const char *version_p
,
2279 struct bfd_elf_version_tree
**t_p
,
2282 struct bfd_elf_version_tree
*t
;
2284 /* Look for the version. If we find it, it is no longer weak. */
2285 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
2287 if (strcmp (t
->name
, version_p
) == 0)
2291 struct bfd_elf_version_expr
*d
;
2293 len
= version_p
- h
->root
.root
.string
;
2294 alc
= (char *) bfd_malloc (len
);
2297 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2298 alc
[len
- 1] = '\0';
2299 if (alc
[len
- 2] == ELF_VER_CHR
)
2300 alc
[len
- 2] = '\0';
2302 h
->verinfo
.vertree
= t
;
2306 if (t
->globals
.list
!= NULL
)
2307 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2309 /* See if there is anything to force this symbol to
2311 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2313 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2316 && ! info
->export_dynamic
)
2330 /* Return TRUE if the symbol H is hidden by version script. */
2333 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info
*info
,
2334 struct elf_link_hash_entry
*h
)
2337 bfd_boolean hide
= FALSE
;
2338 const struct elf_backend_data
*bed
2339 = get_elf_backend_data (info
->output_bfd
);
2341 /* Version script only hides symbols defined in regular objects. */
2342 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2345 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2346 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2348 struct bfd_elf_version_tree
*t
;
2351 if (*p
== ELF_VER_CHR
)
2355 && _bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
)
2359 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2364 /* If we don't have a version for this symbol, see if we can find
2366 if (h
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
2369 = bfd_find_version_for_sym (info
->version_info
,
2370 h
->root
.root
.string
, &hide
);
2371 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2373 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2381 /* Figure out appropriate versions for all the symbols. We may not
2382 have the version number script until we have read all of the input
2383 files, so until that point we don't know which symbols should be
2384 local. This function is called via elf_link_hash_traverse. */
2387 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2389 struct elf_info_failed
*sinfo
;
2390 struct bfd_link_info
*info
;
2391 const struct elf_backend_data
*bed
;
2392 struct elf_info_failed eif
;
2396 sinfo
= (struct elf_info_failed
*) data
;
2399 /* Fix the symbol flags. */
2402 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2405 sinfo
->failed
= TRUE
;
2409 bed
= get_elf_backend_data (info
->output_bfd
);
2411 /* We only need version numbers for symbols defined in regular
2413 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2415 /* Hide symbols defined in discarded input sections. */
2416 if ((h
->root
.type
== bfd_link_hash_defined
2417 || h
->root
.type
== bfd_link_hash_defweak
)
2418 && discarded_section (h
->root
.u
.def
.section
))
2419 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2424 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2425 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2427 struct bfd_elf_version_tree
*t
;
2430 if (*p
== ELF_VER_CHR
)
2433 /* If there is no version string, we can just return out. */
2437 if (!_bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
))
2439 sinfo
->failed
= TRUE
;
2444 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2446 /* If we are building an application, we need to create a
2447 version node for this version. */
2448 if (t
== NULL
&& bfd_link_executable (info
))
2450 struct bfd_elf_version_tree
**pp
;
2453 /* If we aren't going to export this symbol, we don't need
2454 to worry about it. */
2455 if (h
->dynindx
== -1)
2458 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2462 sinfo
->failed
= TRUE
;
2467 t
->name_indx
= (unsigned int) -1;
2471 /* Don't count anonymous version tag. */
2472 if (sinfo
->info
->version_info
!= NULL
2473 && sinfo
->info
->version_info
->vernum
== 0)
2475 for (pp
= &sinfo
->info
->version_info
;
2479 t
->vernum
= version_index
;
2483 h
->verinfo
.vertree
= t
;
2487 /* We could not find the version for a symbol when
2488 generating a shared archive. Return an error. */
2490 /* xgettext:c-format */
2491 (_("%pB: version node not found for symbol %s"),
2492 info
->output_bfd
, h
->root
.root
.string
);
2493 bfd_set_error (bfd_error_bad_value
);
2494 sinfo
->failed
= TRUE
;
2499 /* If we don't have a version for this symbol, see if we can find
2502 && h
->verinfo
.vertree
== NULL
2503 && sinfo
->info
->version_info
!= NULL
)
2506 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2507 h
->root
.root
.string
, &hide
);
2508 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2509 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2515 /* Read and swap the relocs from the section indicated by SHDR. This
2516 may be either a REL or a RELA section. The relocations are
2517 translated into RELA relocations and stored in INTERNAL_RELOCS,
2518 which should have already been allocated to contain enough space.
2519 The EXTERNAL_RELOCS are a buffer where the external form of the
2520 relocations should be stored.
2522 Returns FALSE if something goes wrong. */
2525 elf_link_read_relocs_from_section (bfd
*abfd
,
2527 Elf_Internal_Shdr
*shdr
,
2528 void *external_relocs
,
2529 Elf_Internal_Rela
*internal_relocs
)
2531 const struct elf_backend_data
*bed
;
2532 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2533 const bfd_byte
*erela
;
2534 const bfd_byte
*erelaend
;
2535 Elf_Internal_Rela
*irela
;
2536 Elf_Internal_Shdr
*symtab_hdr
;
2539 /* Position ourselves at the start of the section. */
2540 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2543 /* Read the relocations. */
2544 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2547 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2548 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2550 bed
= get_elf_backend_data (abfd
);
2552 /* Convert the external relocations to the internal format. */
2553 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2554 swap_in
= bed
->s
->swap_reloc_in
;
2555 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2556 swap_in
= bed
->s
->swap_reloca_in
;
2559 bfd_set_error (bfd_error_wrong_format
);
2563 erela
= (const bfd_byte
*) external_relocs
;
2564 /* Setting erelaend like this and comparing with <= handles case of
2565 a fuzzed object with sh_size not a multiple of sh_entsize. */
2566 erelaend
= erela
+ shdr
->sh_size
- shdr
->sh_entsize
;
2567 irela
= internal_relocs
;
2568 while (erela
<= erelaend
)
2572 (*swap_in
) (abfd
, erela
, irela
);
2573 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2574 if (bed
->s
->arch_size
== 64)
2578 if ((size_t) r_symndx
>= nsyms
)
2581 /* xgettext:c-format */
2582 (_("%pB: bad reloc symbol index (%#" PRIx64
" >= %#lx)"
2583 " for offset %#" PRIx64
" in section `%pA'"),
2584 abfd
, (uint64_t) r_symndx
, (unsigned long) nsyms
,
2585 (uint64_t) irela
->r_offset
, sec
);
2586 bfd_set_error (bfd_error_bad_value
);
2590 else if (r_symndx
!= STN_UNDEF
)
2593 /* xgettext:c-format */
2594 (_("%pB: non-zero symbol index (%#" PRIx64
")"
2595 " for offset %#" PRIx64
" in section `%pA'"
2596 " when the object file has no symbol table"),
2597 abfd
, (uint64_t) r_symndx
,
2598 (uint64_t) irela
->r_offset
, sec
);
2599 bfd_set_error (bfd_error_bad_value
);
2602 irela
+= bed
->s
->int_rels_per_ext_rel
;
2603 erela
+= shdr
->sh_entsize
;
2609 /* Read and swap the relocs for a section O. They may have been
2610 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2611 not NULL, they are used as buffers to read into. They are known to
2612 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2613 the return value is allocated using either malloc or bfd_alloc,
2614 according to the KEEP_MEMORY argument. If O has two relocation
2615 sections (both REL and RELA relocations), then the REL_HDR
2616 relocations will appear first in INTERNAL_RELOCS, followed by the
2617 RELA_HDR relocations. */
2620 _bfd_elf_link_read_relocs (bfd
*abfd
,
2622 void *external_relocs
,
2623 Elf_Internal_Rela
*internal_relocs
,
2624 bfd_boolean keep_memory
)
2626 void *alloc1
= NULL
;
2627 Elf_Internal_Rela
*alloc2
= NULL
;
2628 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2629 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2630 Elf_Internal_Rela
*internal_rela_relocs
;
2632 if (esdo
->relocs
!= NULL
)
2633 return esdo
->relocs
;
2635 if (o
->reloc_count
== 0)
2638 if (internal_relocs
== NULL
)
2642 size
= (bfd_size_type
) o
->reloc_count
* sizeof (Elf_Internal_Rela
);
2644 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2646 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2647 if (internal_relocs
== NULL
)
2651 if (external_relocs
== NULL
)
2653 bfd_size_type size
= 0;
2656 size
+= esdo
->rel
.hdr
->sh_size
;
2658 size
+= esdo
->rela
.hdr
->sh_size
;
2660 alloc1
= bfd_malloc (size
);
2663 external_relocs
= alloc1
;
2666 internal_rela_relocs
= internal_relocs
;
2669 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2673 external_relocs
= (((bfd_byte
*) external_relocs
)
2674 + esdo
->rel
.hdr
->sh_size
);
2675 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2676 * bed
->s
->int_rels_per_ext_rel
);
2680 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2682 internal_rela_relocs
)))
2685 /* Cache the results for next time, if we can. */
2687 esdo
->relocs
= internal_relocs
;
2691 /* Don't free alloc2, since if it was allocated we are passing it
2692 back (under the name of internal_relocs). */
2694 return internal_relocs
;
2701 bfd_release (abfd
, alloc2
);
2708 /* Compute the size of, and allocate space for, REL_HDR which is the
2709 section header for a section containing relocations for O. */
2712 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2713 struct bfd_elf_section_reloc_data
*reldata
)
2715 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2717 /* That allows us to calculate the size of the section. */
2718 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2720 /* The contents field must last into write_object_contents, so we
2721 allocate it with bfd_alloc rather than malloc. Also since we
2722 cannot be sure that the contents will actually be filled in,
2723 we zero the allocated space. */
2724 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2725 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2728 if (reldata
->hashes
== NULL
&& reldata
->count
)
2730 struct elf_link_hash_entry
**p
;
2732 p
= ((struct elf_link_hash_entry
**)
2733 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2737 reldata
->hashes
= p
;
2743 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2744 originated from the section given by INPUT_REL_HDR) to the
2748 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2749 asection
*input_section
,
2750 Elf_Internal_Shdr
*input_rel_hdr
,
2751 Elf_Internal_Rela
*internal_relocs
,
2752 struct elf_link_hash_entry
**rel_hash
2755 Elf_Internal_Rela
*irela
;
2756 Elf_Internal_Rela
*irelaend
;
2758 struct bfd_elf_section_reloc_data
*output_reldata
;
2759 asection
*output_section
;
2760 const struct elf_backend_data
*bed
;
2761 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2762 struct bfd_elf_section_data
*esdo
;
2764 output_section
= input_section
->output_section
;
2766 bed
= get_elf_backend_data (output_bfd
);
2767 esdo
= elf_section_data (output_section
);
2768 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2770 output_reldata
= &esdo
->rel
;
2771 swap_out
= bed
->s
->swap_reloc_out
;
2773 else if (esdo
->rela
.hdr
2774 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2776 output_reldata
= &esdo
->rela
;
2777 swap_out
= bed
->s
->swap_reloca_out
;
2782 /* xgettext:c-format */
2783 (_("%pB: relocation size mismatch in %pB section %pA"),
2784 output_bfd
, input_section
->owner
, input_section
);
2785 bfd_set_error (bfd_error_wrong_format
);
2789 erel
= output_reldata
->hdr
->contents
;
2790 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2791 irela
= internal_relocs
;
2792 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2793 * bed
->s
->int_rels_per_ext_rel
);
2794 while (irela
< irelaend
)
2796 (*swap_out
) (output_bfd
, irela
, erel
);
2797 irela
+= bed
->s
->int_rels_per_ext_rel
;
2798 erel
+= input_rel_hdr
->sh_entsize
;
2801 /* Bump the counter, so that we know where to add the next set of
2803 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2808 /* Make weak undefined symbols in PIE dynamic. */
2811 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2812 struct elf_link_hash_entry
*h
)
2814 if (bfd_link_pie (info
)
2816 && h
->root
.type
== bfd_link_hash_undefweak
)
2817 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2822 /* Fix up the flags for a symbol. This handles various cases which
2823 can only be fixed after all the input files are seen. This is
2824 currently called by both adjust_dynamic_symbol and
2825 assign_sym_version, which is unnecessary but perhaps more robust in
2826 the face of future changes. */
2829 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2830 struct elf_info_failed
*eif
)
2832 const struct elf_backend_data
*bed
;
2834 /* If this symbol was mentioned in a non-ELF file, try to set
2835 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2836 permit a non-ELF file to correctly refer to a symbol defined in
2837 an ELF dynamic object. */
2840 while (h
->root
.type
== bfd_link_hash_indirect
)
2841 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2843 if (h
->root
.type
!= bfd_link_hash_defined
2844 && h
->root
.type
!= bfd_link_hash_defweak
)
2847 h
->ref_regular_nonweak
= 1;
2851 if (h
->root
.u
.def
.section
->owner
!= NULL
2852 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2853 == bfd_target_elf_flavour
))
2856 h
->ref_regular_nonweak
= 1;
2862 if (h
->dynindx
== -1
2866 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2875 /* Unfortunately, NON_ELF is only correct if the symbol
2876 was first seen in a non-ELF file. Fortunately, if the symbol
2877 was first seen in an ELF file, we're probably OK unless the
2878 symbol was defined in a non-ELF file. Catch that case here.
2879 FIXME: We're still in trouble if the symbol was first seen in
2880 a dynamic object, and then later in a non-ELF regular object. */
2881 if ((h
->root
.type
== bfd_link_hash_defined
2882 || h
->root
.type
== bfd_link_hash_defweak
)
2884 && (h
->root
.u
.def
.section
->owner
!= NULL
2885 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2886 != bfd_target_elf_flavour
)
2887 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2888 && !h
->def_dynamic
)))
2892 /* Backend specific symbol fixup. */
2893 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2894 if (bed
->elf_backend_fixup_symbol
2895 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2898 /* If this is a final link, and the symbol was defined as a common
2899 symbol in a regular object file, and there was no definition in
2900 any dynamic object, then the linker will have allocated space for
2901 the symbol in a common section but the DEF_REGULAR
2902 flag will not have been set. */
2903 if (h
->root
.type
== bfd_link_hash_defined
2907 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
2910 /* Symbols defined in discarded sections shouldn't be dynamic. */
2911 if (h
->root
.type
== bfd_link_hash_undefined
&& h
->indx
== -3)
2912 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2914 /* If a weak undefined symbol has non-default visibility, we also
2915 hide it from the dynamic linker. */
2916 else if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2917 && h
->root
.type
== bfd_link_hash_undefweak
)
2918 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2920 /* A hidden versioned symbol in executable should be forced local if
2921 it is is locally defined, not referenced by shared library and not
2923 else if (bfd_link_executable (eif
->info
)
2924 && h
->versioned
== versioned_hidden
2925 && !eif
->info
->export_dynamic
2929 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2931 /* If -Bsymbolic was used (which means to bind references to global
2932 symbols to the definition within the shared object), and this
2933 symbol was defined in a regular object, then it actually doesn't
2934 need a PLT entry. Likewise, if the symbol has non-default
2935 visibility. If the symbol has hidden or internal visibility, we
2936 will force it local. */
2937 else if (h
->needs_plt
2938 && bfd_link_pic (eif
->info
)
2939 && is_elf_hash_table (eif
->info
->hash
)
2940 && (SYMBOLIC_BIND (eif
->info
, h
)
2941 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2944 bfd_boolean force_local
;
2946 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2947 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2948 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2951 /* If this is a weak defined symbol in a dynamic object, and we know
2952 the real definition in the dynamic object, copy interesting flags
2953 over to the real definition. */
2954 if (h
->is_weakalias
)
2956 struct elf_link_hash_entry
*def
= weakdef (h
);
2958 /* If the real definition is defined by a regular object file,
2959 don't do anything special. See the longer description in
2960 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2961 bfd_link_hash_defined as it was when put on the alias list
2962 then it must have originally been a versioned symbol (for
2963 which a non-versioned indirect symbol is created) and later
2964 a definition for the non-versioned symbol is found. In that
2965 case the indirection is flipped with the versioned symbol
2966 becoming an indirect pointing at the non-versioned symbol.
2967 Thus, not an alias any more. */
2968 if (def
->def_regular
2969 || def
->root
.type
!= bfd_link_hash_defined
)
2972 while ((h
= h
->u
.alias
) != def
)
2973 h
->is_weakalias
= 0;
2977 while (h
->root
.type
== bfd_link_hash_indirect
)
2978 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2979 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2980 || h
->root
.type
== bfd_link_hash_defweak
);
2981 BFD_ASSERT (def
->def_dynamic
);
2982 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, def
, h
);
2989 /* Make the backend pick a good value for a dynamic symbol. This is
2990 called via elf_link_hash_traverse, and also calls itself
2994 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2996 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2997 struct elf_link_hash_table
*htab
;
2998 const struct elf_backend_data
*bed
;
3000 if (! is_elf_hash_table (eif
->info
->hash
))
3003 /* Ignore indirect symbols. These are added by the versioning code. */
3004 if (h
->root
.type
== bfd_link_hash_indirect
)
3007 /* Fix the symbol flags. */
3008 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
3011 htab
= elf_hash_table (eif
->info
);
3012 bed
= get_elf_backend_data (htab
->dynobj
);
3014 if (h
->root
.type
== bfd_link_hash_undefweak
)
3016 if (eif
->info
->dynamic_undefined_weak
== 0)
3017 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
3018 else if (eif
->info
->dynamic_undefined_weak
> 0
3020 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3021 && !bfd_hide_sym_by_version (eif
->info
->version_info
,
3022 h
->root
.root
.string
))
3024 if (!bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3032 /* If this symbol does not require a PLT entry, and it is not
3033 defined by a dynamic object, or is not referenced by a regular
3034 object, ignore it. We do have to handle a weak defined symbol,
3035 even if no regular object refers to it, if we decided to add it
3036 to the dynamic symbol table. FIXME: Do we normally need to worry
3037 about symbols which are defined by one dynamic object and
3038 referenced by another one? */
3040 && h
->type
!= STT_GNU_IFUNC
3044 && (!h
->is_weakalias
|| weakdef (h
)->dynindx
== -1))))
3046 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
3050 /* If we've already adjusted this symbol, don't do it again. This
3051 can happen via a recursive call. */
3052 if (h
->dynamic_adjusted
)
3055 /* Don't look at this symbol again. Note that we must set this
3056 after checking the above conditions, because we may look at a
3057 symbol once, decide not to do anything, and then get called
3058 recursively later after REF_REGULAR is set below. */
3059 h
->dynamic_adjusted
= 1;
3061 /* If this is a weak definition, and we know a real definition, and
3062 the real symbol is not itself defined by a regular object file,
3063 then get a good value for the real definition. We handle the
3064 real symbol first, for the convenience of the backend routine.
3066 Note that there is a confusing case here. If the real definition
3067 is defined by a regular object file, we don't get the real symbol
3068 from the dynamic object, but we do get the weak symbol. If the
3069 processor backend uses a COPY reloc, then if some routine in the
3070 dynamic object changes the real symbol, we will not see that
3071 change in the corresponding weak symbol. This is the way other
3072 ELF linkers work as well, and seems to be a result of the shared
3075 I will clarify this issue. Most SVR4 shared libraries define the
3076 variable _timezone and define timezone as a weak synonym. The
3077 tzset call changes _timezone. If you write
3078 extern int timezone;
3080 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3081 you might expect that, since timezone is a synonym for _timezone,
3082 the same number will print both times. However, if the processor
3083 backend uses a COPY reloc, then actually timezone will be copied
3084 into your process image, and, since you define _timezone
3085 yourself, _timezone will not. Thus timezone and _timezone will
3086 wind up at different memory locations. The tzset call will set
3087 _timezone, leaving timezone unchanged. */
3089 if (h
->is_weakalias
)
3091 struct elf_link_hash_entry
*def
= weakdef (h
);
3093 /* If we get to this point, there is an implicit reference to
3094 the alias by a regular object file via the weak symbol H. */
3095 def
->ref_regular
= 1;
3097 /* Ensure that the backend adjust_dynamic_symbol function sees
3098 the strong alias before H by recursively calling ourselves. */
3099 if (!_bfd_elf_adjust_dynamic_symbol (def
, eif
))
3103 /* If a symbol has no type and no size and does not require a PLT
3104 entry, then we are probably about to do the wrong thing here: we
3105 are probably going to create a COPY reloc for an empty object.
3106 This case can arise when a shared object is built with assembly
3107 code, and the assembly code fails to set the symbol type. */
3109 && h
->type
== STT_NOTYPE
3112 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3113 h
->root
.root
.string
);
3115 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3124 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3128 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
3129 struct elf_link_hash_entry
*h
,
3132 unsigned int power_of_two
;
3134 asection
*sec
= h
->root
.u
.def
.section
;
3136 /* The section alignment of the definition is the maximum alignment
3137 requirement of symbols defined in the section. Since we don't
3138 know the symbol alignment requirement, we start with the
3139 maximum alignment and check low bits of the symbol address
3140 for the minimum alignment. */
3141 power_of_two
= bfd_section_alignment (sec
);
3142 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
3143 while ((h
->root
.u
.def
.value
& mask
) != 0)
3149 if (power_of_two
> bfd_section_alignment (dynbss
))
3151 /* Adjust the section alignment if needed. */
3152 if (!bfd_set_section_alignment (dynbss
, power_of_two
))
3156 /* We make sure that the symbol will be aligned properly. */
3157 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
3159 /* Define the symbol as being at this point in DYNBSS. */
3160 h
->root
.u
.def
.section
= dynbss
;
3161 h
->root
.u
.def
.value
= dynbss
->size
;
3163 /* Increment the size of DYNBSS to make room for the symbol. */
3164 dynbss
->size
+= h
->size
;
3166 /* No error if extern_protected_data is true. */
3167 if (h
->protected_def
3168 && (!info
->extern_protected_data
3169 || (info
->extern_protected_data
< 0
3170 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
3171 info
->callbacks
->einfo
3172 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3173 h
->root
.root
.string
);
3178 /* Adjust all external symbols pointing into SEC_MERGE sections
3179 to reflect the object merging within the sections. */
3182 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
3186 if ((h
->root
.type
== bfd_link_hash_defined
3187 || h
->root
.type
== bfd_link_hash_defweak
)
3188 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
3189 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
3191 bfd
*output_bfd
= (bfd
*) data
;
3193 h
->root
.u
.def
.value
=
3194 _bfd_merged_section_offset (output_bfd
,
3195 &h
->root
.u
.def
.section
,
3196 elf_section_data (sec
)->sec_info
,
3197 h
->root
.u
.def
.value
);
3203 /* Returns false if the symbol referred to by H should be considered
3204 to resolve local to the current module, and true if it should be
3205 considered to bind dynamically. */
3208 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
3209 struct bfd_link_info
*info
,
3210 bfd_boolean not_local_protected
)
3212 bfd_boolean binding_stays_local_p
;
3213 const struct elf_backend_data
*bed
;
3214 struct elf_link_hash_table
*hash_table
;
3219 while (h
->root
.type
== bfd_link_hash_indirect
3220 || h
->root
.type
== bfd_link_hash_warning
)
3221 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3223 /* If it was forced local, then clearly it's not dynamic. */
3224 if (h
->dynindx
== -1)
3226 if (h
->forced_local
)
3229 /* Identify the cases where name binding rules say that a
3230 visible symbol resolves locally. */
3231 binding_stays_local_p
= (bfd_link_executable (info
)
3232 || SYMBOLIC_BIND (info
, h
));
3234 switch (ELF_ST_VISIBILITY (h
->other
))
3241 hash_table
= elf_hash_table (info
);
3242 if (!is_elf_hash_table (hash_table
))
3245 bed
= get_elf_backend_data (hash_table
->dynobj
);
3247 /* Proper resolution for function pointer equality may require
3248 that these symbols perhaps be resolved dynamically, even though
3249 we should be resolving them to the current module. */
3250 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
3251 binding_stays_local_p
= TRUE
;
3258 /* If it isn't defined locally, then clearly it's dynamic. */
3259 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
3262 /* Otherwise, the symbol is dynamic if binding rules don't tell
3263 us that it remains local. */
3264 return !binding_stays_local_p
;
3267 /* Return true if the symbol referred to by H should be considered
3268 to resolve local to the current module, and false otherwise. Differs
3269 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3270 undefined symbols. The two functions are virtually identical except
3271 for the place where dynindx == -1 is tested. If that test is true,
3272 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3273 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3275 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3276 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3277 treatment of undefined weak symbols. For those that do not make
3278 undefined weak symbols dynamic, both functions may return false. */
3281 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3282 struct bfd_link_info
*info
,
3283 bfd_boolean local_protected
)
3285 const struct elf_backend_data
*bed
;
3286 struct elf_link_hash_table
*hash_table
;
3288 /* If it's a local sym, of course we resolve locally. */
3292 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3293 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3294 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3297 /* Forced local symbols resolve locally. */
3298 if (h
->forced_local
)
3301 /* Common symbols that become definitions don't get the DEF_REGULAR
3302 flag set, so test it first, and don't bail out. */
3303 if (ELF_COMMON_DEF_P (h
))
3305 /* If we don't have a definition in a regular file, then we can't
3306 resolve locally. The sym is either undefined or dynamic. */
3307 else if (!h
->def_regular
)
3310 /* Non-dynamic symbols resolve locally. */
3311 if (h
->dynindx
== -1)
3314 /* At this point, we know the symbol is defined and dynamic. In an
3315 executable it must resolve locally, likewise when building symbolic
3316 shared libraries. */
3317 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3320 /* Now deal with defined dynamic symbols in shared libraries. Ones
3321 with default visibility might not resolve locally. */
3322 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3325 hash_table
= elf_hash_table (info
);
3326 if (!is_elf_hash_table (hash_table
))
3329 bed
= get_elf_backend_data (hash_table
->dynobj
);
3331 /* If extern_protected_data is false, STV_PROTECTED non-function
3332 symbols are local. */
3333 if ((!info
->extern_protected_data
3334 || (info
->extern_protected_data
< 0
3335 && !bed
->extern_protected_data
))
3336 && !bed
->is_function_type (h
->type
))
3339 /* Function pointer equality tests may require that STV_PROTECTED
3340 symbols be treated as dynamic symbols. If the address of a
3341 function not defined in an executable is set to that function's
3342 plt entry in the executable, then the address of the function in
3343 a shared library must also be the plt entry in the executable. */
3344 return local_protected
;
3347 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3348 aligned. Returns the first TLS output section. */
3350 struct bfd_section
*
3351 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3353 struct bfd_section
*sec
, *tls
;
3354 unsigned int align
= 0;
3356 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3357 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3361 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3362 if (sec
->alignment_power
> align
)
3363 align
= sec
->alignment_power
;
3365 elf_hash_table (info
)->tls_sec
= tls
;
3367 /* Ensure the alignment of the first section (usually .tdata) is the largest
3368 alignment, so that the tls segment starts aligned. */
3370 tls
->alignment_power
= align
;
3375 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3377 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3378 Elf_Internal_Sym
*sym
)
3380 const struct elf_backend_data
*bed
;
3382 /* Local symbols do not count, but target specific ones might. */
3383 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3384 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3387 bed
= get_elf_backend_data (abfd
);
3388 /* Function symbols do not count. */
3389 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3392 /* If the section is undefined, then so is the symbol. */
3393 if (sym
->st_shndx
== SHN_UNDEF
)
3396 /* If the symbol is defined in the common section, then
3397 it is a common definition and so does not count. */
3398 if (bed
->common_definition (sym
))
3401 /* If the symbol is in a target specific section then we
3402 must rely upon the backend to tell us what it is. */
3403 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3404 /* FIXME - this function is not coded yet:
3406 return _bfd_is_global_symbol_definition (abfd, sym);
3408 Instead for now assume that the definition is not global,
3409 Even if this is wrong, at least the linker will behave
3410 in the same way that it used to do. */
3416 /* Search the symbol table of the archive element of the archive ABFD
3417 whose archive map contains a mention of SYMDEF, and determine if
3418 the symbol is defined in this element. */
3420 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3422 Elf_Internal_Shdr
* hdr
;
3426 Elf_Internal_Sym
*isymbuf
;
3427 Elf_Internal_Sym
*isym
;
3428 Elf_Internal_Sym
*isymend
;
3431 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
3435 if (! bfd_check_format (abfd
, bfd_object
))
3438 /* Select the appropriate symbol table. If we don't know if the
3439 object file is an IR object, give linker LTO plugin a chance to
3440 get the correct symbol table. */
3441 if (abfd
->plugin_format
== bfd_plugin_yes
3442 #if BFD_SUPPORTS_PLUGINS
3443 || (abfd
->plugin_format
== bfd_plugin_unknown
3444 && bfd_link_plugin_object_p (abfd
))
3448 /* Use the IR symbol table if the object has been claimed by
3450 abfd
= abfd
->plugin_dummy_bfd
;
3451 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3453 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3454 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3456 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3458 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3460 /* The sh_info field of the symtab header tells us where the
3461 external symbols start. We don't care about the local symbols. */
3462 if (elf_bad_symtab (abfd
))
3464 extsymcount
= symcount
;
3469 extsymcount
= symcount
- hdr
->sh_info
;
3470 extsymoff
= hdr
->sh_info
;
3473 if (extsymcount
== 0)
3476 /* Read in the symbol table. */
3477 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3479 if (isymbuf
== NULL
)
3482 /* Scan the symbol table looking for SYMDEF. */
3484 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3488 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3493 if (strcmp (name
, symdef
->name
) == 0)
3495 result
= is_global_data_symbol_definition (abfd
, isym
);
3505 /* Add an entry to the .dynamic table. */
3508 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3512 struct elf_link_hash_table
*hash_table
;
3513 const struct elf_backend_data
*bed
;
3515 bfd_size_type newsize
;
3516 bfd_byte
*newcontents
;
3517 Elf_Internal_Dyn dyn
;
3519 hash_table
= elf_hash_table (info
);
3520 if (! is_elf_hash_table (hash_table
))
3523 if (tag
== DT_RELA
|| tag
== DT_REL
)
3524 hash_table
->dynamic_relocs
= TRUE
;
3526 bed
= get_elf_backend_data (hash_table
->dynobj
);
3527 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3528 BFD_ASSERT (s
!= NULL
);
3530 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3531 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3532 if (newcontents
== NULL
)
3536 dyn
.d_un
.d_val
= val
;
3537 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3540 s
->contents
= newcontents
;
3545 /* Strip zero-sized dynamic sections. */
3548 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info
*info
)
3550 struct elf_link_hash_table
*hash_table
;
3551 const struct elf_backend_data
*bed
;
3552 asection
*s
, *sdynamic
, **pp
;
3553 asection
*rela_dyn
, *rel_dyn
;
3554 Elf_Internal_Dyn dyn
;
3555 bfd_byte
*extdyn
, *next
;
3556 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
3557 bfd_boolean strip_zero_sized
;
3558 bfd_boolean strip_zero_sized_plt
;
3560 if (bfd_link_relocatable (info
))
3563 hash_table
= elf_hash_table (info
);
3564 if (!is_elf_hash_table (hash_table
))
3567 if (!hash_table
->dynobj
)
3570 sdynamic
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3574 bed
= get_elf_backend_data (hash_table
->dynobj
);
3575 swap_dyn_in
= bed
->s
->swap_dyn_in
;
3577 strip_zero_sized
= FALSE
;
3578 strip_zero_sized_plt
= FALSE
;
3580 /* Strip zero-sized dynamic sections. */
3581 rela_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rela.dyn");
3582 rel_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rel.dyn");
3583 for (pp
= &info
->output_bfd
->sections
; (s
= *pp
) != NULL
;)
3587 || s
== hash_table
->srelplt
->output_section
3588 || s
== hash_table
->splt
->output_section
))
3591 info
->output_bfd
->section_count
--;
3592 strip_zero_sized
= TRUE
;
3597 else if (s
== hash_table
->splt
->output_section
)
3599 s
= hash_table
->splt
;
3600 strip_zero_sized_plt
= TRUE
;
3603 s
= hash_table
->srelplt
;
3604 s
->flags
|= SEC_EXCLUDE
;
3605 s
->output_section
= bfd_abs_section_ptr
;
3610 if (strip_zero_sized_plt
)
3611 for (extdyn
= sdynamic
->contents
;
3612 extdyn
< sdynamic
->contents
+ sdynamic
->size
;
3615 next
= extdyn
+ bed
->s
->sizeof_dyn
;
3616 swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3624 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3625 the procedure linkage table (the .plt section) has been
3627 memmove (extdyn
, next
,
3628 sdynamic
->size
- (next
- sdynamic
->contents
));
3633 if (strip_zero_sized
)
3635 /* Regenerate program headers. */
3636 elf_seg_map (info
->output_bfd
) = NULL
;
3637 return _bfd_elf_map_sections_to_segments (info
->output_bfd
, info
);
3643 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3644 1 if a DT_NEEDED tag already exists, and 0 on success. */
3647 bfd_elf_add_dt_needed_tag (bfd
*abfd
, struct bfd_link_info
*info
)
3649 struct elf_link_hash_table
*hash_table
;
3653 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3656 hash_table
= elf_hash_table (info
);
3657 soname
= elf_dt_name (abfd
);
3658 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3659 if (strindex
== (size_t) -1)
3662 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3665 const struct elf_backend_data
*bed
;
3668 bed
= get_elf_backend_data (hash_table
->dynobj
);
3669 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3671 for (extdyn
= sdyn
->contents
;
3672 extdyn
< sdyn
->contents
+ sdyn
->size
;
3673 extdyn
+= bed
->s
->sizeof_dyn
)
3675 Elf_Internal_Dyn dyn
;
3677 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3678 if (dyn
.d_tag
== DT_NEEDED
3679 && dyn
.d_un
.d_val
== strindex
)
3681 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3687 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3690 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3696 /* Return true if SONAME is on the needed list between NEEDED and STOP
3697 (or the end of list if STOP is NULL), and needed by a library that
3701 on_needed_list (const char *soname
,
3702 struct bfd_link_needed_list
*needed
,
3703 struct bfd_link_needed_list
*stop
)
3705 struct bfd_link_needed_list
*look
;
3706 for (look
= needed
; look
!= stop
; look
= look
->next
)
3707 if (strcmp (soname
, look
->name
) == 0
3708 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3709 /* If needed by a library that itself is not directly
3710 needed, recursively check whether that library is
3711 indirectly needed. Since we add DT_NEEDED entries to
3712 the end of the list, library dependencies appear after
3713 the library. Therefore search prior to the current
3714 LOOK, preventing possible infinite recursion. */
3715 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3721 /* Sort symbol by value, section, size, and type. */
3723 elf_sort_symbol (const void *arg1
, const void *arg2
)
3725 const struct elf_link_hash_entry
*h1
;
3726 const struct elf_link_hash_entry
*h2
;
3727 bfd_signed_vma vdiff
;
3732 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3733 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3734 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3736 return vdiff
> 0 ? 1 : -1;
3738 sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3742 /* Sort so that sized symbols are selected over zero size symbols. */
3743 vdiff
= h1
->size
- h2
->size
;
3745 return vdiff
> 0 ? 1 : -1;
3747 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3748 if (h1
->type
!= h2
->type
)
3749 return h1
->type
- h2
->type
;
3751 /* If symbols are properly sized and typed, and multiple strong
3752 aliases are not defined in a shared library by the user we
3753 shouldn't get here. Unfortunately linker script symbols like
3754 __bss_start sometimes match a user symbol defined at the start of
3755 .bss without proper size and type. We'd like to preference the
3756 user symbol over reserved system symbols. Sort on leading
3758 n1
= h1
->root
.root
.string
;
3759 n2
= h2
->root
.root
.string
;
3772 /* Final sort on name selects user symbols like '_u' over reserved
3773 system symbols like '_Z' and also will avoid qsort instability. */
3777 /* This function is used to adjust offsets into .dynstr for
3778 dynamic symbols. This is called via elf_link_hash_traverse. */
3781 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3783 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3785 if (h
->dynindx
!= -1)
3786 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3790 /* Assign string offsets in .dynstr, update all structures referencing
3794 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3796 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3797 struct elf_link_local_dynamic_entry
*entry
;
3798 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3799 bfd
*dynobj
= hash_table
->dynobj
;
3802 const struct elf_backend_data
*bed
;
3805 _bfd_elf_strtab_finalize (dynstr
);
3806 size
= _bfd_elf_strtab_size (dynstr
);
3808 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3810 if (info
->callbacks
->examine_strtab
)
3811 info
->callbacks
->examine_strtab (dynstr
);
3813 bed
= get_elf_backend_data (dynobj
);
3814 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3815 BFD_ASSERT (sdyn
!= NULL
);
3817 /* Update all .dynamic entries referencing .dynstr strings. */
3818 for (extdyn
= sdyn
->contents
;
3819 extdyn
< sdyn
->contents
+ sdyn
->size
;
3820 extdyn
+= bed
->s
->sizeof_dyn
)
3822 Elf_Internal_Dyn dyn
;
3824 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3828 dyn
.d_un
.d_val
= size
;
3838 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3843 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3846 /* Now update local dynamic symbols. */
3847 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3848 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3849 entry
->isym
.st_name
);
3851 /* And the rest of dynamic symbols. */
3852 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3854 /* Adjust version definitions. */
3855 if (elf_tdata (output_bfd
)->cverdefs
)
3860 Elf_Internal_Verdef def
;
3861 Elf_Internal_Verdaux defaux
;
3863 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3867 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3869 p
+= sizeof (Elf_External_Verdef
);
3870 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3872 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3874 _bfd_elf_swap_verdaux_in (output_bfd
,
3875 (Elf_External_Verdaux
*) p
, &defaux
);
3876 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3878 _bfd_elf_swap_verdaux_out (output_bfd
,
3879 &defaux
, (Elf_External_Verdaux
*) p
);
3880 p
+= sizeof (Elf_External_Verdaux
);
3883 while (def
.vd_next
);
3886 /* Adjust version references. */
3887 if (elf_tdata (output_bfd
)->verref
)
3892 Elf_Internal_Verneed need
;
3893 Elf_Internal_Vernaux needaux
;
3895 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
3899 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3901 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3902 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3903 (Elf_External_Verneed
*) p
);
3904 p
+= sizeof (Elf_External_Verneed
);
3905 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3907 _bfd_elf_swap_vernaux_in (output_bfd
,
3908 (Elf_External_Vernaux
*) p
, &needaux
);
3909 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3911 _bfd_elf_swap_vernaux_out (output_bfd
,
3913 (Elf_External_Vernaux
*) p
);
3914 p
+= sizeof (Elf_External_Vernaux
);
3917 while (need
.vn_next
);
3923 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3924 The default is to only match when the INPUT and OUTPUT are exactly
3928 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3929 const bfd_target
*output
)
3931 return input
== output
;
3934 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3935 This version is used when different targets for the same architecture
3936 are virtually identical. */
3939 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3940 const bfd_target
*output
)
3942 const struct elf_backend_data
*obed
, *ibed
;
3944 if (input
== output
)
3947 ibed
= xvec_get_elf_backend_data (input
);
3948 obed
= xvec_get_elf_backend_data (output
);
3950 if (ibed
->arch
!= obed
->arch
)
3953 /* If both backends are using this function, deem them compatible. */
3954 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3957 /* Make a special call to the linker "notice" function to tell it that
3958 we are about to handle an as-needed lib, or have finished
3959 processing the lib. */
3962 _bfd_elf_notice_as_needed (bfd
*ibfd
,
3963 struct bfd_link_info
*info
,
3964 enum notice_asneeded_action act
)
3966 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
3969 /* Check relocations an ELF object file. */
3972 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
3974 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3975 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
3977 /* If this object is the same format as the output object, and it is
3978 not a shared library, then let the backend look through the
3981 This is required to build global offset table entries and to
3982 arrange for dynamic relocs. It is not required for the
3983 particular common case of linking non PIC code, even when linking
3984 against shared libraries, but unfortunately there is no way of
3985 knowing whether an object file has been compiled PIC or not.
3986 Looking through the relocs is not particularly time consuming.
3987 The problem is that we must either (1) keep the relocs in memory,
3988 which causes the linker to require additional runtime memory or
3989 (2) read the relocs twice from the input file, which wastes time.
3990 This would be a good case for using mmap.
3992 I have no idea how to handle linking PIC code into a file of a
3993 different format. It probably can't be done. */
3994 if ((abfd
->flags
& DYNAMIC
) == 0
3995 && is_elf_hash_table (htab
)
3996 && bed
->check_relocs
!= NULL
3997 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
3998 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
4002 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4004 Elf_Internal_Rela
*internal_relocs
;
4007 /* Don't check relocations in excluded sections. Don't do
4008 anything special with non-loaded, non-alloced sections.
4009 In particular, any relocs in such sections should not
4010 affect GOT and PLT reference counting (ie. we don't
4011 allow them to create GOT or PLT entries), there's no
4012 possibility or desire to optimize TLS relocs, and
4013 there's not much point in propagating relocs to shared
4014 libs that the dynamic linker won't relocate. */
4015 if ((o
->flags
& SEC_ALLOC
) == 0
4016 || (o
->flags
& SEC_RELOC
) == 0
4017 || (o
->flags
& SEC_EXCLUDE
) != 0
4018 || o
->reloc_count
== 0
4019 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4020 && (o
->flags
& SEC_DEBUGGING
) != 0)
4021 || bfd_is_abs_section (o
->output_section
))
4024 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4026 if (internal_relocs
== NULL
)
4029 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
4031 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4032 free (internal_relocs
);
4042 /* Add symbols from an ELF object file to the linker hash table. */
4045 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4047 Elf_Internal_Ehdr
*ehdr
;
4048 Elf_Internal_Shdr
*hdr
;
4052 struct elf_link_hash_entry
**sym_hash
;
4053 bfd_boolean dynamic
;
4054 Elf_External_Versym
*extversym
= NULL
;
4055 Elf_External_Versym
*extversym_end
= NULL
;
4056 Elf_External_Versym
*ever
;
4057 struct elf_link_hash_entry
*weaks
;
4058 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
4059 size_t nondeflt_vers_cnt
= 0;
4060 Elf_Internal_Sym
*isymbuf
= NULL
;
4061 Elf_Internal_Sym
*isym
;
4062 Elf_Internal_Sym
*isymend
;
4063 const struct elf_backend_data
*bed
;
4064 bfd_boolean add_needed
;
4065 struct elf_link_hash_table
*htab
;
4066 void *alloc_mark
= NULL
;
4067 struct bfd_hash_entry
**old_table
= NULL
;
4068 unsigned int old_size
= 0;
4069 unsigned int old_count
= 0;
4070 void *old_tab
= NULL
;
4072 struct bfd_link_hash_entry
*old_undefs
= NULL
;
4073 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
4074 void *old_strtab
= NULL
;
4077 bfd_boolean just_syms
;
4079 htab
= elf_hash_table (info
);
4080 bed
= get_elf_backend_data (abfd
);
4082 if ((abfd
->flags
& DYNAMIC
) == 0)
4088 /* You can't use -r against a dynamic object. Also, there's no
4089 hope of using a dynamic object which does not exactly match
4090 the format of the output file. */
4091 if (bfd_link_relocatable (info
)
4092 || !is_elf_hash_table (htab
)
4093 || info
->output_bfd
->xvec
!= abfd
->xvec
)
4095 if (bfd_link_relocatable (info
))
4096 bfd_set_error (bfd_error_invalid_operation
);
4098 bfd_set_error (bfd_error_wrong_format
);
4103 ehdr
= elf_elfheader (abfd
);
4104 if (info
->warn_alternate_em
4105 && bed
->elf_machine_code
!= ehdr
->e_machine
4106 && ((bed
->elf_machine_alt1
!= 0
4107 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
4108 || (bed
->elf_machine_alt2
!= 0
4109 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
4111 /* xgettext:c-format */
4112 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4113 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
4115 /* As a GNU extension, any input sections which are named
4116 .gnu.warning.SYMBOL are treated as warning symbols for the given
4117 symbol. This differs from .gnu.warning sections, which generate
4118 warnings when they are included in an output file. */
4119 /* PR 12761: Also generate this warning when building shared libraries. */
4120 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4124 name
= bfd_section_name (s
);
4125 if (CONST_STRNEQ (name
, ".gnu.warning."))
4130 name
+= sizeof ".gnu.warning." - 1;
4132 /* If this is a shared object, then look up the symbol
4133 in the hash table. If it is there, and it is already
4134 been defined, then we will not be using the entry
4135 from this shared object, so we don't need to warn.
4136 FIXME: If we see the definition in a regular object
4137 later on, we will warn, but we shouldn't. The only
4138 fix is to keep track of what warnings we are supposed
4139 to emit, and then handle them all at the end of the
4143 struct elf_link_hash_entry
*h
;
4145 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
4147 /* FIXME: What about bfd_link_hash_common? */
4149 && (h
->root
.type
== bfd_link_hash_defined
4150 || h
->root
.type
== bfd_link_hash_defweak
))
4155 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
4159 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
4164 if (! (_bfd_generic_link_add_one_symbol
4165 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
4166 FALSE
, bed
->collect
, NULL
)))
4169 if (bfd_link_executable (info
))
4171 /* Clobber the section size so that the warning does
4172 not get copied into the output file. */
4175 /* Also set SEC_EXCLUDE, so that symbols defined in
4176 the warning section don't get copied to the output. */
4177 s
->flags
|= SEC_EXCLUDE
;
4182 just_syms
= ((s
= abfd
->sections
) != NULL
4183 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
4188 /* If we are creating a shared library, create all the dynamic
4189 sections immediately. We need to attach them to something,
4190 so we attach them to this BFD, provided it is the right
4191 format and is not from ld --just-symbols. Always create the
4192 dynamic sections for -E/--dynamic-list. FIXME: If there
4193 are no input BFD's of the same format as the output, we can't
4194 make a shared library. */
4196 && (bfd_link_pic (info
)
4197 || (!bfd_link_relocatable (info
)
4199 && (info
->export_dynamic
|| info
->dynamic
)))
4200 && is_elf_hash_table (htab
)
4201 && info
->output_bfd
->xvec
== abfd
->xvec
4202 && !htab
->dynamic_sections_created
)
4204 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
4208 else if (!is_elf_hash_table (htab
))
4212 const char *soname
= NULL
;
4214 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
4215 const Elf_Internal_Phdr
*phdr
;
4216 struct elf_link_loaded_list
*loaded_lib
;
4218 /* ld --just-symbols and dynamic objects don't mix very well.
4219 ld shouldn't allow it. */
4223 /* If this dynamic lib was specified on the command line with
4224 --as-needed in effect, then we don't want to add a DT_NEEDED
4225 tag unless the lib is actually used. Similary for libs brought
4226 in by another lib's DT_NEEDED. When --no-add-needed is used
4227 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4228 any dynamic library in DT_NEEDED tags in the dynamic lib at
4230 add_needed
= (elf_dyn_lib_class (abfd
)
4231 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
4232 | DYN_NO_NEEDED
)) == 0;
4234 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4239 unsigned int elfsec
;
4240 unsigned long shlink
;
4242 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
4249 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
4250 if (elfsec
== SHN_BAD
)
4251 goto error_free_dyn
;
4252 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
4254 for (extdyn
= dynbuf
;
4255 extdyn
<= dynbuf
+ s
->size
- bed
->s
->sizeof_dyn
;
4256 extdyn
+= bed
->s
->sizeof_dyn
)
4258 Elf_Internal_Dyn dyn
;
4260 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
4261 if (dyn
.d_tag
== DT_SONAME
)
4263 unsigned int tagv
= dyn
.d_un
.d_val
;
4264 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4266 goto error_free_dyn
;
4268 if (dyn
.d_tag
== DT_NEEDED
)
4270 struct bfd_link_needed_list
*n
, **pn
;
4272 unsigned int tagv
= dyn
.d_un
.d_val
;
4273 size_t amt
= sizeof (struct bfd_link_needed_list
);
4275 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4276 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4277 if (n
== NULL
|| fnm
== NULL
)
4278 goto error_free_dyn
;
4279 amt
= strlen (fnm
) + 1;
4280 anm
= (char *) bfd_alloc (abfd
, amt
);
4282 goto error_free_dyn
;
4283 memcpy (anm
, fnm
, amt
);
4287 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4291 if (dyn
.d_tag
== DT_RUNPATH
)
4293 struct bfd_link_needed_list
*n
, **pn
;
4295 unsigned int tagv
= dyn
.d_un
.d_val
;
4296 size_t amt
= sizeof (struct bfd_link_needed_list
);
4298 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4299 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4300 if (n
== NULL
|| fnm
== NULL
)
4301 goto error_free_dyn
;
4302 amt
= strlen (fnm
) + 1;
4303 anm
= (char *) bfd_alloc (abfd
, amt
);
4305 goto error_free_dyn
;
4306 memcpy (anm
, fnm
, amt
);
4310 for (pn
= & runpath
;
4316 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4317 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
4319 struct bfd_link_needed_list
*n
, **pn
;
4321 unsigned int tagv
= dyn
.d_un
.d_val
;
4322 size_t amt
= sizeof (struct bfd_link_needed_list
);
4324 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4325 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4326 if (n
== NULL
|| fnm
== NULL
)
4327 goto error_free_dyn
;
4328 amt
= strlen (fnm
) + 1;
4329 anm
= (char *) bfd_alloc (abfd
, amt
);
4331 goto error_free_dyn
;
4332 memcpy (anm
, fnm
, amt
);
4342 if (dyn
.d_tag
== DT_AUDIT
)
4344 unsigned int tagv
= dyn
.d_un
.d_val
;
4345 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4352 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4353 frees all more recently bfd_alloc'd blocks as well. */
4359 struct bfd_link_needed_list
**pn
;
4360 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4365 /* If we have a PT_GNU_RELRO program header, mark as read-only
4366 all sections contained fully therein. This makes relro
4367 shared library sections appear as they will at run-time. */
4368 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4369 while (phdr
-- > elf_tdata (abfd
)->phdr
)
4370 if (phdr
->p_type
== PT_GNU_RELRO
)
4372 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4374 unsigned int opb
= bfd_octets_per_byte (abfd
, s
);
4376 if ((s
->flags
& SEC_ALLOC
) != 0
4377 && s
->vma
* opb
>= phdr
->p_vaddr
4378 && s
->vma
* opb
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4379 s
->flags
|= SEC_READONLY
;
4384 /* We do not want to include any of the sections in a dynamic
4385 object in the output file. We hack by simply clobbering the
4386 list of sections in the BFD. This could be handled more
4387 cleanly by, say, a new section flag; the existing
4388 SEC_NEVER_LOAD flag is not the one we want, because that one
4389 still implies that the section takes up space in the output
4391 bfd_section_list_clear (abfd
);
4393 /* Find the name to use in a DT_NEEDED entry that refers to this
4394 object. If the object has a DT_SONAME entry, we use it.
4395 Otherwise, if the generic linker stuck something in
4396 elf_dt_name, we use that. Otherwise, we just use the file
4398 if (soname
== NULL
|| *soname
== '\0')
4400 soname
= elf_dt_name (abfd
);
4401 if (soname
== NULL
|| *soname
== '\0')
4402 soname
= bfd_get_filename (abfd
);
4405 /* Save the SONAME because sometimes the linker emulation code
4406 will need to know it. */
4407 elf_dt_name (abfd
) = soname
;
4409 /* If we have already included this dynamic object in the
4410 link, just ignore it. There is no reason to include a
4411 particular dynamic object more than once. */
4412 for (loaded_lib
= htab
->dyn_loaded
;
4414 loaded_lib
= loaded_lib
->next
)
4416 if (strcmp (elf_dt_name (loaded_lib
->abfd
), soname
) == 0)
4420 /* Create dynamic sections for backends that require that be done
4421 before setup_gnu_properties. */
4423 && !_bfd_elf_link_create_dynamic_sections (abfd
, info
))
4426 /* Save the DT_AUDIT entry for the linker emulation code. */
4427 elf_dt_audit (abfd
) = audit
;
4430 /* If this is a dynamic object, we always link against the .dynsym
4431 symbol table, not the .symtab symbol table. The dynamic linker
4432 will only see the .dynsym symbol table, so there is no reason to
4433 look at .symtab for a dynamic object. */
4435 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4436 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4438 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4440 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4442 /* The sh_info field of the symtab header tells us where the
4443 external symbols start. We don't care about the local symbols at
4445 if (elf_bad_symtab (abfd
))
4447 extsymcount
= symcount
;
4452 extsymcount
= symcount
- hdr
->sh_info
;
4453 extsymoff
= hdr
->sh_info
;
4456 sym_hash
= elf_sym_hashes (abfd
);
4457 if (extsymcount
!= 0)
4459 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4461 if (isymbuf
== NULL
)
4464 if (sym_hash
== NULL
)
4466 /* We store a pointer to the hash table entry for each
4468 size_t amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4469 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4470 if (sym_hash
== NULL
)
4471 goto error_free_sym
;
4472 elf_sym_hashes (abfd
) = sym_hash
;
4478 /* Read in any version definitions. */
4479 if (!_bfd_elf_slurp_version_tables (abfd
,
4480 info
->default_imported_symver
))
4481 goto error_free_sym
;
4483 /* Read in the symbol versions, but don't bother to convert them
4484 to internal format. */
4485 if (elf_dynversym (abfd
) != 0)
4487 Elf_Internal_Shdr
*versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4488 bfd_size_type amt
= versymhdr
->sh_size
;
4490 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0)
4491 goto error_free_sym
;
4492 extversym
= (Elf_External_Versym
*)
4493 _bfd_malloc_and_read (abfd
, amt
, amt
);
4494 if (extversym
== NULL
)
4495 goto error_free_sym
;
4496 extversym_end
= extversym
+ amt
/ sizeof (*extversym
);
4500 /* If we are loading an as-needed shared lib, save the symbol table
4501 state before we start adding symbols. If the lib turns out
4502 to be unneeded, restore the state. */
4503 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4508 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4510 struct bfd_hash_entry
*p
;
4511 struct elf_link_hash_entry
*h
;
4513 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4515 h
= (struct elf_link_hash_entry
*) p
;
4516 entsize
+= htab
->root
.table
.entsize
;
4517 if (h
->root
.type
== bfd_link_hash_warning
)
4519 entsize
+= htab
->root
.table
.entsize
;
4520 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4522 if (h
->root
.type
== bfd_link_hash_common
)
4523 entsize
+= sizeof (*h
->root
.u
.c
.p
);
4527 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4528 old_tab
= bfd_malloc (tabsize
+ entsize
);
4529 if (old_tab
== NULL
)
4530 goto error_free_vers
;
4532 /* Remember the current objalloc pointer, so that all mem for
4533 symbols added can later be reclaimed. */
4534 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4535 if (alloc_mark
== NULL
)
4536 goto error_free_vers
;
4538 /* Make a special call to the linker "notice" function to
4539 tell it that we are about to handle an as-needed lib. */
4540 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4541 goto error_free_vers
;
4543 /* Clone the symbol table. Remember some pointers into the
4544 symbol table, and dynamic symbol count. */
4545 old_ent
= (char *) old_tab
+ tabsize
;
4546 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4547 old_undefs
= htab
->root
.undefs
;
4548 old_undefs_tail
= htab
->root
.undefs_tail
;
4549 old_table
= htab
->root
.table
.table
;
4550 old_size
= htab
->root
.table
.size
;
4551 old_count
= htab
->root
.table
.count
;
4553 if (htab
->dynstr
!= NULL
)
4555 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4556 if (old_strtab
== NULL
)
4557 goto error_free_vers
;
4560 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4562 struct bfd_hash_entry
*p
;
4563 struct elf_link_hash_entry
*h
;
4565 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4567 h
= (struct elf_link_hash_entry
*) p
;
4568 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4569 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4570 if (h
->root
.type
== bfd_link_hash_warning
)
4572 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4573 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4574 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4576 if (h
->root
.type
== bfd_link_hash_common
)
4578 memcpy (old_ent
, h
->root
.u
.c
.p
, sizeof (*h
->root
.u
.c
.p
));
4579 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
4586 if (extversym
== NULL
)
4588 else if (extversym
+ extsymoff
< extversym_end
)
4589 ever
= extversym
+ extsymoff
;
4592 /* xgettext:c-format */
4593 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4594 abfd
, (long) extsymoff
,
4595 (long) (extversym_end
- extversym
) / sizeof (* extversym
));
4596 bfd_set_error (bfd_error_bad_value
);
4597 goto error_free_vers
;
4600 if (!bfd_link_relocatable (info
)
4601 && abfd
->lto_slim_object
)
4604 (_("%pB: plugin needed to handle lto object"), abfd
);
4607 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
4609 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4613 asection
*sec
, *new_sec
;
4616 struct elf_link_hash_entry
*h
;
4617 struct elf_link_hash_entry
*hi
;
4618 bfd_boolean definition
;
4619 bfd_boolean size_change_ok
;
4620 bfd_boolean type_change_ok
;
4621 bfd_boolean new_weak
;
4622 bfd_boolean old_weak
;
4625 bfd_boolean discarded
;
4626 unsigned int old_alignment
;
4627 unsigned int shindex
;
4629 bfd_boolean matched
;
4633 flags
= BSF_NO_FLAGS
;
4635 value
= isym
->st_value
;
4636 common
= bed
->common_definition (isym
);
4637 if (common
&& info
->inhibit_common_definition
)
4639 /* Treat common symbol as undefined for --no-define-common. */
4640 isym
->st_shndx
= SHN_UNDEF
;
4645 bind
= ELF_ST_BIND (isym
->st_info
);
4649 /* This should be impossible, since ELF requires that all
4650 global symbols follow all local symbols, and that sh_info
4651 point to the first global symbol. Unfortunately, Irix 5
4653 if (elf_bad_symtab (abfd
))
4656 /* If we aren't prepared to handle locals within the globals
4657 then we'll likely segfault on a NULL symbol hash if the
4658 symbol is ever referenced in relocations. */
4659 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
4660 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, hdr
->sh_name
);
4661 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4662 " (>= sh_info of %lu)"),
4663 abfd
, name
, (long) (isym
- isymbuf
+ extsymoff
),
4666 /* Dynamic object relocations are not processed by ld, so
4667 ld won't run into the problem mentioned above. */
4670 bfd_set_error (bfd_error_bad_value
);
4671 goto error_free_vers
;
4674 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4682 case STB_GNU_UNIQUE
:
4683 flags
= BSF_GNU_UNIQUE
;
4687 /* Leave it up to the processor backend. */
4691 if (isym
->st_shndx
== SHN_UNDEF
)
4692 sec
= bfd_und_section_ptr
;
4693 else if (isym
->st_shndx
== SHN_ABS
)
4694 sec
= bfd_abs_section_ptr
;
4695 else if (isym
->st_shndx
== SHN_COMMON
)
4697 sec
= bfd_com_section_ptr
;
4698 /* What ELF calls the size we call the value. What ELF
4699 calls the value we call the alignment. */
4700 value
= isym
->st_size
;
4704 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4706 sec
= bfd_abs_section_ptr
;
4707 else if (discarded_section (sec
))
4709 /* Symbols from discarded section are undefined. We keep
4711 sec
= bfd_und_section_ptr
;
4713 isym
->st_shndx
= SHN_UNDEF
;
4715 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4719 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4722 goto error_free_vers
;
4724 if (isym
->st_shndx
== SHN_COMMON
4725 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4727 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4731 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4733 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4735 goto error_free_vers
;
4739 else if (isym
->st_shndx
== SHN_COMMON
4740 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4741 && !bfd_link_relocatable (info
))
4743 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4747 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4748 | SEC_LINKER_CREATED
);
4749 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4751 goto error_free_vers
;
4755 else if (bed
->elf_add_symbol_hook
)
4757 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4759 goto error_free_vers
;
4761 /* The hook function sets the name to NULL if this symbol
4762 should be skipped for some reason. */
4767 /* Sanity check that all possibilities were handled. */
4771 /* Silently discard TLS symbols from --just-syms. There's
4772 no way to combine a static TLS block with a new TLS block
4773 for this executable. */
4774 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4775 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4778 if (bfd_is_und_section (sec
)
4779 || bfd_is_com_section (sec
))
4784 size_change_ok
= FALSE
;
4785 type_change_ok
= bed
->type_change_ok
;
4792 if (is_elf_hash_table (htab
))
4794 Elf_Internal_Versym iver
;
4795 unsigned int vernum
= 0;
4800 if (info
->default_imported_symver
)
4801 /* Use the default symbol version created earlier. */
4802 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4806 else if (ever
>= extversym_end
)
4808 /* xgettext:c-format */
4809 _bfd_error_handler (_("%pB: not enough version information"),
4811 bfd_set_error (bfd_error_bad_value
);
4812 goto error_free_vers
;
4815 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4817 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4819 /* If this is a hidden symbol, or if it is not version
4820 1, we append the version name to the symbol name.
4821 However, we do not modify a non-hidden absolute symbol
4822 if it is not a function, because it might be the version
4823 symbol itself. FIXME: What if it isn't? */
4824 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4826 && (!bfd_is_abs_section (sec
)
4827 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4830 size_t namelen
, verlen
, newlen
;
4833 if (isym
->st_shndx
!= SHN_UNDEF
)
4835 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4837 else if (vernum
> 1)
4839 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4846 /* xgettext:c-format */
4847 (_("%pB: %s: invalid version %u (max %d)"),
4849 elf_tdata (abfd
)->cverdefs
);
4850 bfd_set_error (bfd_error_bad_value
);
4851 goto error_free_vers
;
4856 /* We cannot simply test for the number of
4857 entries in the VERNEED section since the
4858 numbers for the needed versions do not start
4860 Elf_Internal_Verneed
*t
;
4863 for (t
= elf_tdata (abfd
)->verref
;
4867 Elf_Internal_Vernaux
*a
;
4869 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4871 if (a
->vna_other
== vernum
)
4873 verstr
= a
->vna_nodename
;
4883 /* xgettext:c-format */
4884 (_("%pB: %s: invalid needed version %d"),
4885 abfd
, name
, vernum
);
4886 bfd_set_error (bfd_error_bad_value
);
4887 goto error_free_vers
;
4891 namelen
= strlen (name
);
4892 verlen
= strlen (verstr
);
4893 newlen
= namelen
+ verlen
+ 2;
4894 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4895 && isym
->st_shndx
!= SHN_UNDEF
)
4898 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4899 if (newname
== NULL
)
4900 goto error_free_vers
;
4901 memcpy (newname
, name
, namelen
);
4902 p
= newname
+ namelen
;
4904 /* If this is a defined non-hidden version symbol,
4905 we add another @ to the name. This indicates the
4906 default version of the symbol. */
4907 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4908 && isym
->st_shndx
!= SHN_UNDEF
)
4910 memcpy (p
, verstr
, verlen
+ 1);
4915 /* If this symbol has default visibility and the user has
4916 requested we not re-export it, then mark it as hidden. */
4917 if (!bfd_is_und_section (sec
)
4920 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4921 isym
->st_other
= (STV_HIDDEN
4922 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4924 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4925 sym_hash
, &old_bfd
, &old_weak
,
4926 &old_alignment
, &skip
, &override
,
4927 &type_change_ok
, &size_change_ok
,
4929 goto error_free_vers
;
4934 /* Override a definition only if the new symbol matches the
4936 if (override
&& matched
)
4940 while (h
->root
.type
== bfd_link_hash_indirect
4941 || h
->root
.type
== bfd_link_hash_warning
)
4942 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4944 if (elf_tdata (abfd
)->verdef
!= NULL
4947 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4950 if (! (_bfd_generic_link_add_one_symbol
4951 (info
, override
? override
: abfd
, name
, flags
, sec
, value
,
4952 NULL
, FALSE
, bed
->collect
,
4953 (struct bfd_link_hash_entry
**) sym_hash
)))
4954 goto error_free_vers
;
4957 /* We need to make sure that indirect symbol dynamic flags are
4960 while (h
->root
.type
== bfd_link_hash_indirect
4961 || h
->root
.type
== bfd_link_hash_warning
)
4962 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4964 /* Setting the index to -3 tells elf_link_output_extsym that
4965 this symbol is defined in a discarded section. */
4971 new_weak
= (flags
& BSF_WEAK
) != 0;
4975 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4976 && is_elf_hash_table (htab
)
4977 && h
->u
.alias
== NULL
)
4979 /* Keep a list of all weak defined non function symbols from
4980 a dynamic object, using the alias field. Later in this
4981 function we will set the alias field to the correct
4982 value. We only put non-function symbols from dynamic
4983 objects on this list, because that happens to be the only
4984 time we need to know the normal symbol corresponding to a
4985 weak symbol, and the information is time consuming to
4986 figure out. If the alias field is not already NULL,
4987 then this symbol was already defined by some previous
4988 dynamic object, and we will be using that previous
4989 definition anyhow. */
4995 /* Set the alignment of a common symbol. */
4996 if ((common
|| bfd_is_com_section (sec
))
4997 && h
->root
.type
== bfd_link_hash_common
)
5002 align
= bfd_log2 (isym
->st_value
);
5005 /* The new symbol is a common symbol in a shared object.
5006 We need to get the alignment from the section. */
5007 align
= new_sec
->alignment_power
;
5009 if (align
> old_alignment
)
5010 h
->root
.u
.c
.p
->alignment_power
= align
;
5012 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
5015 if (is_elf_hash_table (htab
))
5017 /* Set a flag in the hash table entry indicating the type of
5018 reference or definition we just found. A dynamic symbol
5019 is one which is referenced or defined by both a regular
5020 object and a shared object. */
5021 bfd_boolean dynsym
= FALSE
;
5023 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5024 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5031 if (bind
!= STB_WEAK
)
5032 h
->ref_regular_nonweak
= 1;
5049 hi
->ref_dynamic
= 1;
5054 hi
->def_dynamic
= 1;
5058 /* If an indirect symbol has been forced local, don't
5059 make the real symbol dynamic. */
5060 if (h
!= hi
&& hi
->forced_local
)
5064 if (bfd_link_dll (info
)
5074 && weakdef (h
)->dynindx
!= -1))
5078 /* Check to see if we need to add an indirect symbol for
5079 the default name. */
5081 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
5083 && hi
->versioned
== versioned_hidden
))
5084 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
5085 sec
, value
, &old_bfd
, &dynsym
))
5086 goto error_free_vers
;
5088 /* Check the alignment when a common symbol is involved. This
5089 can change when a common symbol is overridden by a normal
5090 definition or a common symbol is ignored due to the old
5091 normal definition. We need to make sure the maximum
5092 alignment is maintained. */
5093 if ((old_alignment
|| common
)
5094 && h
->root
.type
!= bfd_link_hash_common
)
5096 unsigned int common_align
;
5097 unsigned int normal_align
;
5098 unsigned int symbol_align
;
5102 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
5103 || h
->root
.type
== bfd_link_hash_defweak
);
5105 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
5106 if (h
->root
.u
.def
.section
->owner
!= NULL
5107 && (h
->root
.u
.def
.section
->owner
->flags
5108 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
5110 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
5111 if (normal_align
> symbol_align
)
5112 normal_align
= symbol_align
;
5115 normal_align
= symbol_align
;
5119 common_align
= old_alignment
;
5120 common_bfd
= old_bfd
;
5125 common_align
= bfd_log2 (isym
->st_value
);
5127 normal_bfd
= old_bfd
;
5130 if (normal_align
< common_align
)
5132 /* PR binutils/2735 */
5133 if (normal_bfd
== NULL
)
5135 /* xgettext:c-format */
5136 (_("warning: alignment %u of common symbol `%s' in %pB is"
5137 " greater than the alignment (%u) of its section %pA"),
5138 1 << common_align
, name
, common_bfd
,
5139 1 << normal_align
, h
->root
.u
.def
.section
);
5142 /* xgettext:c-format */
5143 (_("warning: alignment %u of symbol `%s' in %pB"
5144 " is smaller than %u in %pB"),
5145 1 << normal_align
, name
, normal_bfd
,
5146 1 << common_align
, common_bfd
);
5150 /* Remember the symbol size if it isn't undefined. */
5151 if (isym
->st_size
!= 0
5152 && isym
->st_shndx
!= SHN_UNDEF
5153 && (definition
|| h
->size
== 0))
5156 && h
->size
!= isym
->st_size
5157 && ! size_change_ok
)
5159 /* xgettext:c-format */
5160 (_("warning: size of symbol `%s' changed"
5161 " from %" PRIu64
" in %pB to %" PRIu64
" in %pB"),
5162 name
, (uint64_t) h
->size
, old_bfd
,
5163 (uint64_t) isym
->st_size
, abfd
);
5165 h
->size
= isym
->st_size
;
5168 /* If this is a common symbol, then we always want H->SIZE
5169 to be the size of the common symbol. The code just above
5170 won't fix the size if a common symbol becomes larger. We
5171 don't warn about a size change here, because that is
5172 covered by --warn-common. Allow changes between different
5174 if (h
->root
.type
== bfd_link_hash_common
)
5175 h
->size
= h
->root
.u
.c
.size
;
5177 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
5178 && ((definition
&& !new_weak
)
5179 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
5180 || h
->type
== STT_NOTYPE
))
5182 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
5184 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5186 if (type
== STT_GNU_IFUNC
5187 && (abfd
->flags
& DYNAMIC
) != 0)
5190 if (h
->type
!= type
)
5192 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
5193 /* xgettext:c-format */
5195 (_("warning: type of symbol `%s' changed"
5196 " from %d to %d in %pB"),
5197 name
, h
->type
, type
, abfd
);
5203 /* Merge st_other field. */
5204 elf_merge_st_other (abfd
, h
, isym
->st_other
, sec
,
5205 definition
, dynamic
);
5207 /* We don't want to make debug symbol dynamic. */
5209 && (sec
->flags
& SEC_DEBUGGING
)
5210 && !bfd_link_relocatable (info
))
5213 /* Nor should we make plugin symbols dynamic. */
5214 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5219 h
->target_internal
= isym
->st_target_internal
;
5220 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
5223 if (definition
&& !dynamic
)
5225 char *p
= strchr (name
, ELF_VER_CHR
);
5226 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
5228 /* Queue non-default versions so that .symver x, x@FOO
5229 aliases can be checked. */
5232 size_t amt
= ((isymend
- isym
+ 1)
5233 * sizeof (struct elf_link_hash_entry
*));
5235 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5237 goto error_free_vers
;
5239 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
5243 if (dynsym
&& h
->dynindx
== -1)
5245 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5246 goto error_free_vers
;
5248 && weakdef (h
)->dynindx
== -1)
5250 if (!bfd_elf_link_record_dynamic_symbol (info
, weakdef (h
)))
5251 goto error_free_vers
;
5254 else if (h
->dynindx
!= -1)
5255 /* If the symbol already has a dynamic index, but
5256 visibility says it should not be visible, turn it into
5258 switch (ELF_ST_VISIBILITY (h
->other
))
5262 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
5271 && h
->ref_regular_nonweak
)
5273 && (old_bfd
->flags
& BFD_PLUGIN
) != 0
5274 && bind
!= STB_WEAK
)
5275 || (h
->ref_dynamic_nonweak
5276 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5277 && !on_needed_list (elf_dt_name (abfd
),
5278 htab
->needed
, NULL
))))
5280 const char *soname
= elf_dt_name (abfd
);
5282 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
5283 h
->root
.root
.string
);
5285 /* A symbol from a library loaded via DT_NEEDED of some
5286 other library is referenced by a regular object.
5287 Add a DT_NEEDED entry for it. Issue an error if
5288 --no-add-needed is used and the reference was not
5291 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
5294 /* xgettext:c-format */
5295 (_("%pB: undefined reference to symbol '%s'"),
5297 bfd_set_error (bfd_error_missing_dso
);
5298 goto error_free_vers
;
5301 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
5302 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
5304 /* Create dynamic sections for backends that require
5305 that be done before setup_gnu_properties. */
5306 if (!_bfd_elf_link_create_dynamic_sections (abfd
, info
))
5313 if (info
->lto_plugin_active
5314 && !bfd_link_relocatable (info
)
5315 && (abfd
->flags
& BFD_PLUGIN
) == 0
5321 if (bed
->s
->arch_size
== 32)
5326 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5327 referenced in regular objects so that linker plugin will get
5328 the correct symbol resolution. */
5330 sym_hash
= elf_sym_hashes (abfd
);
5331 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5333 Elf_Internal_Rela
*internal_relocs
;
5334 Elf_Internal_Rela
*rel
, *relend
;
5336 /* Don't check relocations in excluded sections. */
5337 if ((s
->flags
& SEC_RELOC
) == 0
5338 || s
->reloc_count
== 0
5339 || (s
->flags
& SEC_EXCLUDE
) != 0
5340 || ((info
->strip
== strip_all
5341 || info
->strip
== strip_debugger
)
5342 && (s
->flags
& SEC_DEBUGGING
) != 0))
5345 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, s
, NULL
,
5348 if (internal_relocs
== NULL
)
5349 goto error_free_vers
;
5351 rel
= internal_relocs
;
5352 relend
= rel
+ s
->reloc_count
;
5353 for ( ; rel
< relend
; rel
++)
5355 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
5356 struct elf_link_hash_entry
*h
;
5358 /* Skip local symbols. */
5359 if (r_symndx
< extsymoff
)
5362 h
= sym_hash
[r_symndx
- extsymoff
];
5364 h
->root
.non_ir_ref_regular
= 1;
5367 if (elf_section_data (s
)->relocs
!= internal_relocs
)
5368 free (internal_relocs
);
5377 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
5381 /* Restore the symbol table. */
5382 old_ent
= (char *) old_tab
+ tabsize
;
5383 memset (elf_sym_hashes (abfd
), 0,
5384 extsymcount
* sizeof (struct elf_link_hash_entry
*));
5385 htab
->root
.table
.table
= old_table
;
5386 htab
->root
.table
.size
= old_size
;
5387 htab
->root
.table
.count
= old_count
;
5388 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
5389 htab
->root
.undefs
= old_undefs
;
5390 htab
->root
.undefs_tail
= old_undefs_tail
;
5391 if (htab
->dynstr
!= NULL
)
5392 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
5395 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
5397 struct bfd_hash_entry
*p
;
5398 struct elf_link_hash_entry
*h
;
5399 unsigned int non_ir_ref_dynamic
;
5401 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
5403 /* Preserve non_ir_ref_dynamic so that this symbol
5404 will be exported when the dynamic lib becomes needed
5405 in the second pass. */
5406 h
= (struct elf_link_hash_entry
*) p
;
5407 if (h
->root
.type
== bfd_link_hash_warning
)
5408 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5409 non_ir_ref_dynamic
= h
->root
.non_ir_ref_dynamic
;
5411 h
= (struct elf_link_hash_entry
*) p
;
5412 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5413 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5414 if (h
->root
.type
== bfd_link_hash_warning
)
5416 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5417 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5418 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5420 if (h
->root
.type
== bfd_link_hash_common
)
5422 memcpy (h
->root
.u
.c
.p
, old_ent
, sizeof (*h
->root
.u
.c
.p
));
5423 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
5425 h
->root
.non_ir_ref_dynamic
= non_ir_ref_dynamic
;
5429 /* Make a special call to the linker "notice" function to
5430 tell it that symbols added for crefs may need to be removed. */
5431 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
5432 goto error_free_vers
;
5435 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
5437 free (nondeflt_vers
);
5441 if (old_tab
!= NULL
)
5443 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
5444 goto error_free_vers
;
5449 /* Now that all the symbols from this input file are created, if
5450 not performing a relocatable link, handle .symver foo, foo@BAR
5451 such that any relocs against foo become foo@BAR. */
5452 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5456 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5458 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5459 char *shortname
, *p
;
5462 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5464 || (h
->root
.type
!= bfd_link_hash_defined
5465 && h
->root
.type
!= bfd_link_hash_defweak
))
5468 amt
= p
- h
->root
.root
.string
;
5469 shortname
= (char *) bfd_malloc (amt
+ 1);
5471 goto error_free_vers
;
5472 memcpy (shortname
, h
->root
.root
.string
, amt
);
5473 shortname
[amt
] = '\0';
5475 hi
= (struct elf_link_hash_entry
*)
5476 bfd_link_hash_lookup (&htab
->root
, shortname
,
5477 FALSE
, FALSE
, FALSE
);
5479 && hi
->root
.type
== h
->root
.type
5480 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5481 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5483 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
5484 hi
->root
.type
= bfd_link_hash_indirect
;
5485 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5486 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5487 sym_hash
= elf_sym_hashes (abfd
);
5489 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5490 if (sym_hash
[symidx
] == hi
)
5492 sym_hash
[symidx
] = h
;
5498 free (nondeflt_vers
);
5499 nondeflt_vers
= NULL
;
5502 /* Now set the alias field correctly for all the weak defined
5503 symbols we found. The only way to do this is to search all the
5504 symbols. Since we only need the information for non functions in
5505 dynamic objects, that's the only time we actually put anything on
5506 the list WEAKS. We need this information so that if a regular
5507 object refers to a symbol defined weakly in a dynamic object, the
5508 real symbol in the dynamic object is also put in the dynamic
5509 symbols; we also must arrange for both symbols to point to the
5510 same memory location. We could handle the general case of symbol
5511 aliasing, but a general symbol alias can only be generated in
5512 assembler code, handling it correctly would be very time
5513 consuming, and other ELF linkers don't handle general aliasing
5517 struct elf_link_hash_entry
**hpp
;
5518 struct elf_link_hash_entry
**hppend
;
5519 struct elf_link_hash_entry
**sorted_sym_hash
;
5520 struct elf_link_hash_entry
*h
;
5521 size_t sym_count
, amt
;
5523 /* Since we have to search the whole symbol list for each weak
5524 defined symbol, search time for N weak defined symbols will be
5525 O(N^2). Binary search will cut it down to O(NlogN). */
5526 amt
= extsymcount
* sizeof (*sorted_sym_hash
);
5527 sorted_sym_hash
= bfd_malloc (amt
);
5528 if (sorted_sym_hash
== NULL
)
5530 sym_hash
= sorted_sym_hash
;
5531 hpp
= elf_sym_hashes (abfd
);
5532 hppend
= hpp
+ extsymcount
;
5534 for (; hpp
< hppend
; hpp
++)
5538 && h
->root
.type
== bfd_link_hash_defined
5539 && !bed
->is_function_type (h
->type
))
5547 qsort (sorted_sym_hash
, sym_count
, sizeof (*sorted_sym_hash
),
5550 while (weaks
!= NULL
)
5552 struct elf_link_hash_entry
*hlook
;
5555 size_t i
, j
, idx
= 0;
5558 weaks
= hlook
->u
.alias
;
5559 hlook
->u
.alias
= NULL
;
5561 if (hlook
->root
.type
!= bfd_link_hash_defined
5562 && hlook
->root
.type
!= bfd_link_hash_defweak
)
5565 slook
= hlook
->root
.u
.def
.section
;
5566 vlook
= hlook
->root
.u
.def
.value
;
5572 bfd_signed_vma vdiff
;
5574 h
= sorted_sym_hash
[idx
];
5575 vdiff
= vlook
- h
->root
.u
.def
.value
;
5582 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5592 /* We didn't find a value/section match. */
5596 /* With multiple aliases, or when the weak symbol is already
5597 strongly defined, we have multiple matching symbols and
5598 the binary search above may land on any of them. Step
5599 one past the matching symbol(s). */
5602 h
= sorted_sym_hash
[idx
];
5603 if (h
->root
.u
.def
.section
!= slook
5604 || h
->root
.u
.def
.value
!= vlook
)
5608 /* Now look back over the aliases. Since we sorted by size
5609 as well as value and section, we'll choose the one with
5610 the largest size. */
5613 h
= sorted_sym_hash
[idx
];
5615 /* Stop if value or section doesn't match. */
5616 if (h
->root
.u
.def
.section
!= slook
5617 || h
->root
.u
.def
.value
!= vlook
)
5619 else if (h
!= hlook
)
5621 struct elf_link_hash_entry
*t
;
5624 hlook
->is_weakalias
= 1;
5626 if (t
->u
.alias
!= NULL
)
5627 while (t
->u
.alias
!= h
)
5631 /* If the weak definition is in the list of dynamic
5632 symbols, make sure the real definition is put
5634 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5636 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5639 free (sorted_sym_hash
);
5644 /* If the real definition is in the list of dynamic
5645 symbols, make sure the weak definition is put
5646 there as well. If we don't do this, then the
5647 dynamic loader might not merge the entries for the
5648 real definition and the weak definition. */
5649 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5651 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5652 goto err_free_sym_hash
;
5659 free (sorted_sym_hash
);
5662 if (bed
->check_directives
5663 && !(*bed
->check_directives
) (abfd
, info
))
5666 /* If this is a non-traditional link, try to optimize the handling
5667 of the .stab/.stabstr sections. */
5669 && ! info
->traditional_format
5670 && is_elf_hash_table (htab
)
5671 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5675 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5676 if (stabstr
!= NULL
)
5678 bfd_size_type string_offset
= 0;
5681 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5682 if (CONST_STRNEQ (stab
->name
, ".stab")
5683 && (!stab
->name
[5] ||
5684 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5685 && (stab
->flags
& SEC_MERGE
) == 0
5686 && !bfd_is_abs_section (stab
->output_section
))
5688 struct bfd_elf_section_data
*secdata
;
5690 secdata
= elf_section_data (stab
);
5691 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5692 stabstr
, &secdata
->sec_info
,
5695 if (secdata
->sec_info
)
5696 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5701 if (dynamic
&& add_needed
)
5703 /* Add this bfd to the loaded list. */
5704 struct elf_link_loaded_list
*n
;
5706 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5710 n
->next
= htab
->dyn_loaded
;
5711 htab
->dyn_loaded
= n
;
5713 if (dynamic
&& !add_needed
5714 && (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) != 0)
5715 elf_dyn_lib_class (abfd
) |= DYN_NO_NEEDED
;
5722 free (nondeflt_vers
);
5730 /* Return the linker hash table entry of a symbol that might be
5731 satisfied by an archive symbol. Return -1 on error. */
5733 struct elf_link_hash_entry
*
5734 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5735 struct bfd_link_info
*info
,
5738 struct elf_link_hash_entry
*h
;
5742 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, TRUE
);
5746 /* If this is a default version (the name contains @@), look up the
5747 symbol again with only one `@' as well as without the version.
5748 The effect is that references to the symbol with and without the
5749 version will be matched by the default symbol in the archive. */
5751 p
= strchr (name
, ELF_VER_CHR
);
5752 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5755 /* First check with only one `@'. */
5756 len
= strlen (name
);
5757 copy
= (char *) bfd_alloc (abfd
, len
);
5759 return (struct elf_link_hash_entry
*) -1;
5761 first
= p
- name
+ 1;
5762 memcpy (copy
, name
, first
);
5763 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5765 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, TRUE
);
5768 /* We also need to check references to the symbol without the
5770 copy
[first
- 1] = '\0';
5771 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
5772 FALSE
, FALSE
, TRUE
);
5775 bfd_release (abfd
, copy
);
5779 /* Add symbols from an ELF archive file to the linker hash table. We
5780 don't use _bfd_generic_link_add_archive_symbols because we need to
5781 handle versioned symbols.
5783 Fortunately, ELF archive handling is simpler than that done by
5784 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5785 oddities. In ELF, if we find a symbol in the archive map, and the
5786 symbol is currently undefined, we know that we must pull in that
5789 Unfortunately, we do have to make multiple passes over the symbol
5790 table until nothing further is resolved. */
5793 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5796 unsigned char *included
= NULL
;
5800 const struct elf_backend_data
*bed
;
5801 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
5802 (bfd
*, struct bfd_link_info
*, const char *);
5804 if (! bfd_has_map (abfd
))
5806 /* An empty archive is a special case. */
5807 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5809 bfd_set_error (bfd_error_no_armap
);
5813 /* Keep track of all symbols we know to be already defined, and all
5814 files we know to be already included. This is to speed up the
5815 second and subsequent passes. */
5816 c
= bfd_ardata (abfd
)->symdef_count
;
5819 amt
= c
* sizeof (*included
);
5820 included
= (unsigned char *) bfd_zmalloc (amt
);
5821 if (included
== NULL
)
5824 symdefs
= bfd_ardata (abfd
)->symdefs
;
5825 bed
= get_elf_backend_data (abfd
);
5826 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5839 symdefend
= symdef
+ c
;
5840 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5842 struct elf_link_hash_entry
*h
;
5844 struct bfd_link_hash_entry
*undefs_tail
;
5849 if (symdef
->file_offset
== last
)
5855 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5856 if (h
== (struct elf_link_hash_entry
*) -1)
5862 if (h
->root
.type
== bfd_link_hash_undefined
)
5864 /* If the archive element has already been loaded then one
5865 of the symbols defined by that element might have been
5866 made undefined due to being in a discarded section. */
5870 else if (h
->root
.type
== bfd_link_hash_common
)
5872 /* We currently have a common symbol. The archive map contains
5873 a reference to this symbol, so we may want to include it. We
5874 only want to include it however, if this archive element
5875 contains a definition of the symbol, not just another common
5878 Unfortunately some archivers (including GNU ar) will put
5879 declarations of common symbols into their archive maps, as
5880 well as real definitions, so we cannot just go by the archive
5881 map alone. Instead we must read in the element's symbol
5882 table and check that to see what kind of symbol definition
5884 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5889 if (h
->root
.type
!= bfd_link_hash_undefweak
)
5890 /* Symbol must be defined. Don't check it again. */
5895 /* We need to include this archive member. */
5896 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5897 if (element
== NULL
)
5900 if (! bfd_check_format (element
, bfd_object
))
5903 undefs_tail
= info
->hash
->undefs_tail
;
5905 if (!(*info
->callbacks
5906 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5908 if (!bfd_link_add_symbols (element
, info
))
5911 /* If there are any new undefined symbols, we need to make
5912 another pass through the archive in order to see whether
5913 they can be defined. FIXME: This isn't perfect, because
5914 common symbols wind up on undefs_tail and because an
5915 undefined symbol which is defined later on in this pass
5916 does not require another pass. This isn't a bug, but it
5917 does make the code less efficient than it could be. */
5918 if (undefs_tail
!= info
->hash
->undefs_tail
)
5921 /* Look backward to mark all symbols from this object file
5922 which we have already seen in this pass. */
5926 included
[mark
] = TRUE
;
5931 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5933 /* We mark subsequent symbols from this object file as we go
5934 on through the loop. */
5935 last
= symdef
->file_offset
;
5948 /* Given an ELF BFD, add symbols to the global hash table as
5952 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5954 switch (bfd_get_format (abfd
))
5957 return elf_link_add_object_symbols (abfd
, info
);
5959 return elf_link_add_archive_symbols (abfd
, info
);
5961 bfd_set_error (bfd_error_wrong_format
);
5966 struct hash_codes_info
5968 unsigned long *hashcodes
;
5972 /* This function will be called though elf_link_hash_traverse to store
5973 all hash value of the exported symbols in an array. */
5976 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5978 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
5983 /* Ignore indirect symbols. These are added by the versioning code. */
5984 if (h
->dynindx
== -1)
5987 name
= h
->root
.root
.string
;
5988 if (h
->versioned
>= versioned
)
5990 char *p
= strchr (name
, ELF_VER_CHR
);
5993 alc
= (char *) bfd_malloc (p
- name
+ 1);
5999 memcpy (alc
, name
, p
- name
);
6000 alc
[p
- name
] = '\0';
6005 /* Compute the hash value. */
6006 ha
= bfd_elf_hash (name
);
6008 /* Store the found hash value in the array given as the argument. */
6009 *(inf
->hashcodes
)++ = ha
;
6011 /* And store it in the struct so that we can put it in the hash table
6013 h
->u
.elf_hash_value
= ha
;
6019 struct collect_gnu_hash_codes
6022 const struct elf_backend_data
*bed
;
6023 unsigned long int nsyms
;
6024 unsigned long int maskbits
;
6025 unsigned long int *hashcodes
;
6026 unsigned long int *hashval
;
6027 unsigned long int *indx
;
6028 unsigned long int *counts
;
6032 long int min_dynindx
;
6033 unsigned long int bucketcount
;
6034 unsigned long int symindx
;
6035 long int local_indx
;
6036 long int shift1
, shift2
;
6037 unsigned long int mask
;
6041 /* This function will be called though elf_link_hash_traverse to store
6042 all hash value of the exported symbols in an array. */
6045 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6047 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6052 /* Ignore indirect symbols. These are added by the versioning code. */
6053 if (h
->dynindx
== -1)
6056 /* Ignore also local symbols and undefined symbols. */
6057 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6060 name
= h
->root
.root
.string
;
6061 if (h
->versioned
>= versioned
)
6063 char *p
= strchr (name
, ELF_VER_CHR
);
6066 alc
= (char *) bfd_malloc (p
- name
+ 1);
6072 memcpy (alc
, name
, p
- name
);
6073 alc
[p
- name
] = '\0';
6078 /* Compute the hash value. */
6079 ha
= bfd_elf_gnu_hash (name
);
6081 /* Store the found hash value in the array for compute_bucket_count,
6082 and also for .dynsym reordering purposes. */
6083 s
->hashcodes
[s
->nsyms
] = ha
;
6084 s
->hashval
[h
->dynindx
] = ha
;
6086 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
6087 s
->min_dynindx
= h
->dynindx
;
6093 /* This function will be called though elf_link_hash_traverse to do
6094 final dynamic symbol renumbering in case of .gnu.hash.
6095 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6096 to the translation table. */
6099 elf_gnu_hash_process_symidx (struct elf_link_hash_entry
*h
, void *data
)
6101 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6102 unsigned long int bucket
;
6103 unsigned long int val
;
6105 /* Ignore indirect symbols. */
6106 if (h
->dynindx
== -1)
6109 /* Ignore also local symbols and undefined symbols. */
6110 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6112 if (h
->dynindx
>= s
->min_dynindx
)
6114 if (s
->bed
->record_xhash_symbol
!= NULL
)
6116 (*s
->bed
->record_xhash_symbol
) (h
, 0);
6120 h
->dynindx
= s
->local_indx
++;
6125 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
6126 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
6127 & ((s
->maskbits
>> s
->shift1
) - 1);
6128 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
6130 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
6131 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
6132 if (s
->counts
[bucket
] == 1)
6133 /* Last element terminates the chain. */
6135 bfd_put_32 (s
->output_bfd
, val
,
6136 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
6137 --s
->counts
[bucket
];
6138 if (s
->bed
->record_xhash_symbol
!= NULL
)
6140 bfd_vma xlat_loc
= s
->xlat
+ (s
->indx
[bucket
]++ - s
->symindx
) * 4;
6142 (*s
->bed
->record_xhash_symbol
) (h
, xlat_loc
);
6145 h
->dynindx
= s
->indx
[bucket
]++;
6149 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6152 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
6154 return !(h
->forced_local
6155 || h
->root
.type
== bfd_link_hash_undefined
6156 || h
->root
.type
== bfd_link_hash_undefweak
6157 || ((h
->root
.type
== bfd_link_hash_defined
6158 || h
->root
.type
== bfd_link_hash_defweak
)
6159 && h
->root
.u
.def
.section
->output_section
== NULL
));
6162 /* Array used to determine the number of hash table buckets to use
6163 based on the number of symbols there are. If there are fewer than
6164 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6165 fewer than 37 we use 17 buckets, and so forth. We never use more
6166 than 32771 buckets. */
6168 static const size_t elf_buckets
[] =
6170 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6174 /* Compute bucket count for hashing table. We do not use a static set
6175 of possible tables sizes anymore. Instead we determine for all
6176 possible reasonable sizes of the table the outcome (i.e., the
6177 number of collisions etc) and choose the best solution. The
6178 weighting functions are not too simple to allow the table to grow
6179 without bounds. Instead one of the weighting factors is the size.
6180 Therefore the result is always a good payoff between few collisions
6181 (= short chain lengths) and table size. */
6183 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6184 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
6185 unsigned long int nsyms
,
6188 size_t best_size
= 0;
6189 unsigned long int i
;
6191 /* We have a problem here. The following code to optimize the table
6192 size requires an integer type with more the 32 bits. If
6193 BFD_HOST_U_64_BIT is set we know about such a type. */
6194 #ifdef BFD_HOST_U_64_BIT
6199 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
6200 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
6201 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
6202 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
6203 unsigned long int *counts
;
6205 unsigned int no_improvement_count
= 0;
6207 /* Possible optimization parameters: if we have NSYMS symbols we say
6208 that the hashing table must at least have NSYMS/4 and at most
6210 minsize
= nsyms
/ 4;
6213 best_size
= maxsize
= nsyms
* 2;
6218 if ((best_size
& 31) == 0)
6222 /* Create array where we count the collisions in. We must use bfd_malloc
6223 since the size could be large. */
6225 amt
*= sizeof (unsigned long int);
6226 counts
= (unsigned long int *) bfd_malloc (amt
);
6230 /* Compute the "optimal" size for the hash table. The criteria is a
6231 minimal chain length. The minor criteria is (of course) the size
6233 for (i
= minsize
; i
< maxsize
; ++i
)
6235 /* Walk through the array of hashcodes and count the collisions. */
6236 BFD_HOST_U_64_BIT max
;
6237 unsigned long int j
;
6238 unsigned long int fact
;
6240 if (gnu_hash
&& (i
& 31) == 0)
6243 memset (counts
, '\0', i
* sizeof (unsigned long int));
6245 /* Determine how often each hash bucket is used. */
6246 for (j
= 0; j
< nsyms
; ++j
)
6247 ++counts
[hashcodes
[j
] % i
];
6249 /* For the weight function we need some information about the
6250 pagesize on the target. This is information need not be 100%
6251 accurate. Since this information is not available (so far) we
6252 define it here to a reasonable default value. If it is crucial
6253 to have a better value some day simply define this value. */
6254 # ifndef BFD_TARGET_PAGESIZE
6255 # define BFD_TARGET_PAGESIZE (4096)
6258 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6260 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
6263 /* Variant 1: optimize for short chains. We add the squares
6264 of all the chain lengths (which favors many small chain
6265 over a few long chains). */
6266 for (j
= 0; j
< i
; ++j
)
6267 max
+= counts
[j
] * counts
[j
];
6269 /* This adds penalties for the overall size of the table. */
6270 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6273 /* Variant 2: Optimize a lot more for small table. Here we
6274 also add squares of the size but we also add penalties for
6275 empty slots (the +1 term). */
6276 for (j
= 0; j
< i
; ++j
)
6277 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
6279 /* The overall size of the table is considered, but not as
6280 strong as in variant 1, where it is squared. */
6281 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6285 /* Compare with current best results. */
6286 if (max
< best_chlen
)
6290 no_improvement_count
= 0;
6292 /* PR 11843: Avoid futile long searches for the best bucket size
6293 when there are a large number of symbols. */
6294 else if (++no_improvement_count
== 100)
6301 #endif /* defined (BFD_HOST_U_64_BIT) */
6303 /* This is the fallback solution if no 64bit type is available or if we
6304 are not supposed to spend much time on optimizations. We select the
6305 bucket count using a fixed set of numbers. */
6306 for (i
= 0; elf_buckets
[i
] != 0; i
++)
6308 best_size
= elf_buckets
[i
];
6309 if (nsyms
< elf_buckets
[i
+ 1])
6312 if (gnu_hash
&& best_size
< 2)
6319 /* Size any SHT_GROUP section for ld -r. */
6322 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
6327 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6328 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6329 && (s
= ibfd
->sections
) != NULL
6330 && s
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
6331 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
6336 /* Set a default stack segment size. The value in INFO wins. If it
6337 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6338 undefined it is initialized. */
6341 bfd_elf_stack_segment_size (bfd
*output_bfd
,
6342 struct bfd_link_info
*info
,
6343 const char *legacy_symbol
,
6344 bfd_vma default_size
)
6346 struct elf_link_hash_entry
*h
= NULL
;
6348 /* Look for legacy symbol. */
6350 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
6351 FALSE
, FALSE
, FALSE
);
6352 if (h
&& (h
->root
.type
== bfd_link_hash_defined
6353 || h
->root
.type
== bfd_link_hash_defweak
)
6355 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
6357 /* The symbol has no type if specified on the command line. */
6358 h
->type
= STT_OBJECT
;
6359 if (info
->stacksize
)
6360 /* xgettext:c-format */
6361 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6362 output_bfd
, legacy_symbol
);
6363 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
6364 /* xgettext:c-format */
6365 _bfd_error_handler (_("%pB: %s not absolute"),
6366 output_bfd
, legacy_symbol
);
6368 info
->stacksize
= h
->root
.u
.def
.value
;
6371 if (!info
->stacksize
)
6372 /* If the user didn't set a size, or explicitly inhibit the
6373 size, set it now. */
6374 info
->stacksize
= default_size
;
6376 /* Provide the legacy symbol, if it is referenced. */
6377 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
6378 || h
->root
.type
== bfd_link_hash_undefweak
))
6380 struct bfd_link_hash_entry
*bh
= NULL
;
6382 if (!(_bfd_generic_link_add_one_symbol
6383 (info
, output_bfd
, legacy_symbol
,
6384 BSF_GLOBAL
, bfd_abs_section_ptr
,
6385 info
->stacksize
>= 0 ? info
->stacksize
: 0,
6386 NULL
, FALSE
, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
6389 h
= (struct elf_link_hash_entry
*) bh
;
6391 h
->type
= STT_OBJECT
;
6397 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6399 struct elf_gc_sweep_symbol_info
6401 struct bfd_link_info
*info
;
6402 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
6407 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
6410 && (((h
->root
.type
== bfd_link_hash_defined
6411 || h
->root
.type
== bfd_link_hash_defweak
)
6412 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
6413 && h
->root
.u
.def
.section
->gc_mark
))
6414 || h
->root
.type
== bfd_link_hash_undefined
6415 || h
->root
.type
== bfd_link_hash_undefweak
))
6417 struct elf_gc_sweep_symbol_info
*inf
;
6419 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
6420 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
6423 h
->ref_regular_nonweak
= 0;
6429 /* Set up the sizes and contents of the ELF dynamic sections. This is
6430 called by the ELF linker emulation before_allocation routine. We
6431 must set the sizes of the sections before the linker sets the
6432 addresses of the various sections. */
6435 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
6438 const char *filter_shlib
,
6440 const char *depaudit
,
6441 const char * const *auxiliary_filters
,
6442 struct bfd_link_info
*info
,
6443 asection
**sinterpptr
)
6446 const struct elf_backend_data
*bed
;
6450 if (!is_elf_hash_table (info
->hash
))
6453 dynobj
= elf_hash_table (info
)->dynobj
;
6455 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6457 struct bfd_elf_version_tree
*verdefs
;
6458 struct elf_info_failed asvinfo
;
6459 struct bfd_elf_version_tree
*t
;
6460 struct bfd_elf_version_expr
*d
;
6464 /* If we are supposed to export all symbols into the dynamic symbol
6465 table (this is not the normal case), then do so. */
6466 if (info
->export_dynamic
6467 || (bfd_link_executable (info
) && info
->dynamic
))
6469 struct elf_info_failed eif
;
6473 elf_link_hash_traverse (elf_hash_table (info
),
6474 _bfd_elf_export_symbol
,
6482 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6484 if (soname_indx
== (size_t) -1
6485 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6489 soname_indx
= (size_t) -1;
6491 /* Make all global versions with definition. */
6492 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6493 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6494 if (!d
->symver
&& d
->literal
)
6496 const char *verstr
, *name
;
6497 size_t namelen
, verlen
, newlen
;
6498 char *newname
, *p
, leading_char
;
6499 struct elf_link_hash_entry
*newh
;
6501 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6503 namelen
= strlen (name
) + (leading_char
!= '\0');
6505 verlen
= strlen (verstr
);
6506 newlen
= namelen
+ verlen
+ 3;
6508 newname
= (char *) bfd_malloc (newlen
);
6509 if (newname
== NULL
)
6511 newname
[0] = leading_char
;
6512 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6514 /* Check the hidden versioned definition. */
6515 p
= newname
+ namelen
;
6517 memcpy (p
, verstr
, verlen
+ 1);
6518 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6519 newname
, FALSE
, FALSE
,
6522 || (newh
->root
.type
!= bfd_link_hash_defined
6523 && newh
->root
.type
!= bfd_link_hash_defweak
))
6525 /* Check the default versioned definition. */
6527 memcpy (p
, verstr
, verlen
+ 1);
6528 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6529 newname
, FALSE
, FALSE
,
6534 /* Mark this version if there is a definition and it is
6535 not defined in a shared object. */
6537 && !newh
->def_dynamic
6538 && (newh
->root
.type
== bfd_link_hash_defined
6539 || newh
->root
.type
== bfd_link_hash_defweak
))
6543 /* Attach all the symbols to their version information. */
6544 asvinfo
.info
= info
;
6545 asvinfo
.failed
= FALSE
;
6547 elf_link_hash_traverse (elf_hash_table (info
),
6548 _bfd_elf_link_assign_sym_version
,
6553 if (!info
->allow_undefined_version
)
6555 /* Check if all global versions have a definition. */
6556 bfd_boolean all_defined
= TRUE
;
6557 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6558 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6559 if (d
->literal
&& !d
->symver
&& !d
->script
)
6562 (_("%s: undefined version: %s"),
6563 d
->pattern
, t
->name
);
6564 all_defined
= FALSE
;
6569 bfd_set_error (bfd_error_bad_value
);
6574 /* Set up the version definition section. */
6575 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6576 BFD_ASSERT (s
!= NULL
);
6578 /* We may have created additional version definitions if we are
6579 just linking a regular application. */
6580 verdefs
= info
->version_info
;
6582 /* Skip anonymous version tag. */
6583 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6584 verdefs
= verdefs
->next
;
6586 if (verdefs
== NULL
&& !info
->create_default_symver
)
6587 s
->flags
|= SEC_EXCLUDE
;
6593 Elf_Internal_Verdef def
;
6594 Elf_Internal_Verdaux defaux
;
6595 struct bfd_link_hash_entry
*bh
;
6596 struct elf_link_hash_entry
*h
;
6602 /* Make space for the base version. */
6603 size
+= sizeof (Elf_External_Verdef
);
6604 size
+= sizeof (Elf_External_Verdaux
);
6607 /* Make space for the default version. */
6608 if (info
->create_default_symver
)
6610 size
+= sizeof (Elf_External_Verdef
);
6614 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6616 struct bfd_elf_version_deps
*n
;
6618 /* Don't emit base version twice. */
6622 size
+= sizeof (Elf_External_Verdef
);
6623 size
+= sizeof (Elf_External_Verdaux
);
6626 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6627 size
+= sizeof (Elf_External_Verdaux
);
6631 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6632 if (s
->contents
== NULL
&& s
->size
!= 0)
6635 /* Fill in the version definition section. */
6639 def
.vd_version
= VER_DEF_CURRENT
;
6640 def
.vd_flags
= VER_FLG_BASE
;
6643 if (info
->create_default_symver
)
6645 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6646 def
.vd_next
= sizeof (Elf_External_Verdef
);
6650 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6651 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6652 + sizeof (Elf_External_Verdaux
));
6655 if (soname_indx
!= (size_t) -1)
6657 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6659 def
.vd_hash
= bfd_elf_hash (soname
);
6660 defaux
.vda_name
= soname_indx
;
6667 name
= lbasename (bfd_get_filename (output_bfd
));
6668 def
.vd_hash
= bfd_elf_hash (name
);
6669 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6671 if (indx
== (size_t) -1)
6673 defaux
.vda_name
= indx
;
6675 defaux
.vda_next
= 0;
6677 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6678 (Elf_External_Verdef
*) p
);
6679 p
+= sizeof (Elf_External_Verdef
);
6680 if (info
->create_default_symver
)
6682 /* Add a symbol representing this version. */
6684 if (! (_bfd_generic_link_add_one_symbol
6685 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6687 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6689 h
= (struct elf_link_hash_entry
*) bh
;
6692 h
->type
= STT_OBJECT
;
6693 h
->verinfo
.vertree
= NULL
;
6695 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6698 /* Create a duplicate of the base version with the same
6699 aux block, but different flags. */
6702 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6704 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6705 + sizeof (Elf_External_Verdaux
));
6708 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6709 (Elf_External_Verdef
*) p
);
6710 p
+= sizeof (Elf_External_Verdef
);
6712 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6713 (Elf_External_Verdaux
*) p
);
6714 p
+= sizeof (Elf_External_Verdaux
);
6716 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6719 struct bfd_elf_version_deps
*n
;
6721 /* Don't emit the base version twice. */
6726 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6729 /* Add a symbol representing this version. */
6731 if (! (_bfd_generic_link_add_one_symbol
6732 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6734 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6736 h
= (struct elf_link_hash_entry
*) bh
;
6739 h
->type
= STT_OBJECT
;
6740 h
->verinfo
.vertree
= t
;
6742 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6745 def
.vd_version
= VER_DEF_CURRENT
;
6747 if (t
->globals
.list
== NULL
6748 && t
->locals
.list
== NULL
6750 def
.vd_flags
|= VER_FLG_WEAK
;
6751 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6752 def
.vd_cnt
= cdeps
+ 1;
6753 def
.vd_hash
= bfd_elf_hash (t
->name
);
6754 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6757 /* If a basever node is next, it *must* be the last node in
6758 the chain, otherwise Verdef construction breaks. */
6759 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6760 BFD_ASSERT (t
->next
->next
== NULL
);
6762 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6763 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6764 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6766 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6767 (Elf_External_Verdef
*) p
);
6768 p
+= sizeof (Elf_External_Verdef
);
6770 defaux
.vda_name
= h
->dynstr_index
;
6771 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6773 defaux
.vda_next
= 0;
6774 if (t
->deps
!= NULL
)
6775 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6776 t
->name_indx
= defaux
.vda_name
;
6778 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6779 (Elf_External_Verdaux
*) p
);
6780 p
+= sizeof (Elf_External_Verdaux
);
6782 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6784 if (n
->version_needed
== NULL
)
6786 /* This can happen if there was an error in the
6788 defaux
.vda_name
= 0;
6792 defaux
.vda_name
= n
->version_needed
->name_indx
;
6793 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6796 if (n
->next
== NULL
)
6797 defaux
.vda_next
= 0;
6799 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6801 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6802 (Elf_External_Verdaux
*) p
);
6803 p
+= sizeof (Elf_External_Verdaux
);
6807 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6811 bed
= get_elf_backend_data (output_bfd
);
6813 if (info
->gc_sections
&& bed
->can_gc_sections
)
6815 struct elf_gc_sweep_symbol_info sweep_info
;
6817 /* Remove the symbols that were in the swept sections from the
6818 dynamic symbol table. */
6819 sweep_info
.info
= info
;
6820 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
6821 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
6825 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6828 struct elf_find_verdep_info sinfo
;
6830 /* Work out the size of the version reference section. */
6832 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6833 BFD_ASSERT (s
!= NULL
);
6836 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6837 if (sinfo
.vers
== 0)
6839 sinfo
.failed
= FALSE
;
6841 elf_link_hash_traverse (elf_hash_table (info
),
6842 _bfd_elf_link_find_version_dependencies
,
6847 if (elf_tdata (output_bfd
)->verref
== NULL
)
6848 s
->flags
|= SEC_EXCLUDE
;
6851 Elf_Internal_Verneed
*vn
;
6856 /* Build the version dependency section. */
6859 for (vn
= elf_tdata (output_bfd
)->verref
;
6861 vn
= vn
->vn_nextref
)
6863 Elf_Internal_Vernaux
*a
;
6865 size
+= sizeof (Elf_External_Verneed
);
6867 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6868 size
+= sizeof (Elf_External_Vernaux
);
6872 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6873 if (s
->contents
== NULL
)
6877 for (vn
= elf_tdata (output_bfd
)->verref
;
6879 vn
= vn
->vn_nextref
)
6882 Elf_Internal_Vernaux
*a
;
6886 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6889 vn
->vn_version
= VER_NEED_CURRENT
;
6891 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6892 elf_dt_name (vn
->vn_bfd
) != NULL
6893 ? elf_dt_name (vn
->vn_bfd
)
6894 : lbasename (bfd_get_filename
6897 if (indx
== (size_t) -1)
6900 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
6901 if (vn
->vn_nextref
== NULL
)
6904 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
6905 + caux
* sizeof (Elf_External_Vernaux
));
6907 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
6908 (Elf_External_Verneed
*) p
);
6909 p
+= sizeof (Elf_External_Verneed
);
6911 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6913 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6914 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6915 a
->vna_nodename
, FALSE
);
6916 if (indx
== (size_t) -1)
6919 if (a
->vna_nextptr
== NULL
)
6922 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6924 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6925 (Elf_External_Vernaux
*) p
);
6926 p
+= sizeof (Elf_External_Vernaux
);
6930 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6934 /* Any syms created from now on start with -1 in
6935 got.refcount/offset and plt.refcount/offset. */
6936 elf_hash_table (info
)->init_got_refcount
6937 = elf_hash_table (info
)->init_got_offset
;
6938 elf_hash_table (info
)->init_plt_refcount
6939 = elf_hash_table (info
)->init_plt_offset
;
6941 if (bfd_link_relocatable (info
)
6942 && !_bfd_elf_size_group_sections (info
))
6945 /* The backend may have to create some sections regardless of whether
6946 we're dynamic or not. */
6947 if (bed
->elf_backend_always_size_sections
6948 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
6951 /* Determine any GNU_STACK segment requirements, after the backend
6952 has had a chance to set a default segment size. */
6953 if (info
->execstack
)
6954 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
6955 else if (info
->noexecstack
)
6956 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
6960 asection
*notesec
= NULL
;
6963 for (inputobj
= info
->input_bfds
;
6965 inputobj
= inputobj
->link
.next
)
6970 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
6972 s
= inputobj
->sections
;
6973 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
6976 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
6979 if (s
->flags
& SEC_CODE
)
6983 else if (bed
->default_execstack
)
6986 if (notesec
|| info
->stacksize
> 0)
6987 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
6988 if (notesec
&& exec
&& bfd_link_relocatable (info
)
6989 && notesec
->output_section
!= bfd_abs_section_ptr
)
6990 notesec
->output_section
->flags
|= SEC_CODE
;
6993 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6995 struct elf_info_failed eif
;
6996 struct elf_link_hash_entry
*h
;
7000 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
7001 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
7005 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
7007 info
->flags
|= DF_SYMBOLIC
;
7015 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
7017 if (indx
== (size_t) -1)
7020 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
7021 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
7025 if (filter_shlib
!= NULL
)
7029 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7030 filter_shlib
, TRUE
);
7031 if (indx
== (size_t) -1
7032 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
7036 if (auxiliary_filters
!= NULL
)
7038 const char * const *p
;
7040 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
7044 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7046 if (indx
== (size_t) -1
7047 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
7056 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
7058 if (indx
== (size_t) -1
7059 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
7063 if (depaudit
!= NULL
)
7067 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
7069 if (indx
== (size_t) -1
7070 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
7077 /* Find all symbols which were defined in a dynamic object and make
7078 the backend pick a reasonable value for them. */
7079 elf_link_hash_traverse (elf_hash_table (info
),
7080 _bfd_elf_adjust_dynamic_symbol
,
7085 /* Add some entries to the .dynamic section. We fill in some of the
7086 values later, in bfd_elf_final_link, but we must add the entries
7087 now so that we know the final size of the .dynamic section. */
7089 /* If there are initialization and/or finalization functions to
7090 call then add the corresponding DT_INIT/DT_FINI entries. */
7091 h
= (info
->init_function
7092 ? elf_link_hash_lookup (elf_hash_table (info
),
7093 info
->init_function
, FALSE
,
7100 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
7103 h
= (info
->fini_function
7104 ? elf_link_hash_lookup (elf_hash_table (info
),
7105 info
->fini_function
, FALSE
,
7112 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
7116 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
7117 if (s
!= NULL
&& s
->linker_has_input
)
7119 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7120 if (! bfd_link_executable (info
))
7125 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
7126 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
7127 && (o
= sub
->sections
) != NULL
7128 && o
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
)
7129 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7130 if (elf_section_data (o
)->this_hdr
.sh_type
7131 == SHT_PREINIT_ARRAY
)
7134 (_("%pB: .preinit_array section is not allowed in DSO"),
7139 bfd_set_error (bfd_error_nonrepresentable_section
);
7143 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
7144 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
7147 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
7148 if (s
!= NULL
&& s
->linker_has_input
)
7150 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
7151 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
7154 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
7155 if (s
!= NULL
&& s
->linker_has_input
)
7157 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
7158 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
7162 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
7163 /* If .dynstr is excluded from the link, we don't want any of
7164 these tags. Strictly, we should be checking each section
7165 individually; This quick check covers for the case where
7166 someone does a /DISCARD/ : { *(*) }. */
7167 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
7169 bfd_size_type strsize
;
7171 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7172 if ((info
->emit_hash
7173 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
7174 || (info
->emit_gnu_hash
7175 && (bed
->record_xhash_symbol
== NULL
7176 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0)))
7177 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
7178 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
7179 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
7180 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
7181 bed
->s
->sizeof_sym
))
7186 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
7189 /* The backend must work out the sizes of all the other dynamic
7192 && bed
->elf_backend_size_dynamic_sections
!= NULL
7193 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
7196 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7198 if (elf_tdata (output_bfd
)->cverdefs
)
7200 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
7202 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
7203 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
7207 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
7209 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
7212 else if (info
->flags
& DF_BIND_NOW
)
7214 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
7220 if (bfd_link_executable (info
))
7221 info
->flags_1
&= ~ (DF_1_INITFIRST
7224 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
7228 if (elf_tdata (output_bfd
)->cverrefs
)
7230 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
7232 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
7233 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
7237 if ((elf_tdata (output_bfd
)->cverrefs
== 0
7238 && elf_tdata (output_bfd
)->cverdefs
== 0)
7239 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, NULL
) <= 1)
7243 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7244 s
->flags
|= SEC_EXCLUDE
;
7250 /* Find the first non-excluded output section. We'll use its
7251 section symbol for some emitted relocs. */
7253 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
7256 asection
*found
= NULL
;
7258 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7259 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7260 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7263 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7266 elf_hash_table (info
)->text_index_section
= found
;
7269 /* Find two non-excluded output sections, one for code, one for data.
7270 We'll use their section symbols for some emitted relocs. */
7272 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
7275 asection
*found
= NULL
;
7277 /* Data first, since setting text_index_section changes
7278 _bfd_elf_omit_section_dynsym_default. */
7279 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7280 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7281 && !(s
->flags
& SEC_READONLY
)
7282 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7285 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7288 elf_hash_table (info
)->data_index_section
= found
;
7290 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7291 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7292 && (s
->flags
& SEC_READONLY
)
7293 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7298 elf_hash_table (info
)->text_index_section
= found
;
7301 #define GNU_HASH_SECTION_NAME(bed) \
7302 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7305 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
7307 const struct elf_backend_data
*bed
;
7308 unsigned long section_sym_count
;
7309 bfd_size_type dynsymcount
= 0;
7311 if (!is_elf_hash_table (info
->hash
))
7314 bed
= get_elf_backend_data (output_bfd
);
7315 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
7317 /* Assign dynsym indices. In a shared library we generate a section
7318 symbol for each output section, which come first. Next come all
7319 of the back-end allocated local dynamic syms, followed by the rest
7320 of the global symbols.
7322 This is usually not needed for static binaries, however backends
7323 can request to always do it, e.g. the MIPS backend uses dynamic
7324 symbol counts to lay out GOT, which will be produced in the
7325 presence of GOT relocations even in static binaries (holding fixed
7326 data in that case, to satisfy those relocations). */
7328 if (elf_hash_table (info
)->dynamic_sections_created
7329 || bed
->always_renumber_dynsyms
)
7330 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
7331 §ion_sym_count
);
7333 if (elf_hash_table (info
)->dynamic_sections_created
)
7337 unsigned int dtagcount
;
7339 dynobj
= elf_hash_table (info
)->dynobj
;
7341 /* Work out the size of the symbol version section. */
7342 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7343 BFD_ASSERT (s
!= NULL
);
7344 if ((s
->flags
& SEC_EXCLUDE
) == 0)
7346 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
7347 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7348 if (s
->contents
== NULL
)
7351 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
7355 /* Set the size of the .dynsym and .hash sections. We counted
7356 the number of dynamic symbols in elf_link_add_object_symbols.
7357 We will build the contents of .dynsym and .hash when we build
7358 the final symbol table, because until then we do not know the
7359 correct value to give the symbols. We built the .dynstr
7360 section as we went along in elf_link_add_object_symbols. */
7361 s
= elf_hash_table (info
)->dynsym
;
7362 BFD_ASSERT (s
!= NULL
);
7363 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
7365 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7366 if (s
->contents
== NULL
)
7369 /* The first entry in .dynsym is a dummy symbol. Clear all the
7370 section syms, in case we don't output them all. */
7371 ++section_sym_count
;
7372 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
7374 elf_hash_table (info
)->bucketcount
= 0;
7376 /* Compute the size of the hashing table. As a side effect this
7377 computes the hash values for all the names we export. */
7378 if (info
->emit_hash
)
7380 unsigned long int *hashcodes
;
7381 struct hash_codes_info hashinf
;
7383 unsigned long int nsyms
;
7385 size_t hash_entry_size
;
7387 /* Compute the hash values for all exported symbols. At the same
7388 time store the values in an array so that we could use them for
7390 amt
= dynsymcount
* sizeof (unsigned long int);
7391 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
7392 if (hashcodes
== NULL
)
7394 hashinf
.hashcodes
= hashcodes
;
7395 hashinf
.error
= FALSE
;
7397 /* Put all hash values in HASHCODES. */
7398 elf_link_hash_traverse (elf_hash_table (info
),
7399 elf_collect_hash_codes
, &hashinf
);
7406 nsyms
= hashinf
.hashcodes
- hashcodes
;
7408 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
7411 if (bucketcount
== 0 && nsyms
> 0)
7414 elf_hash_table (info
)->bucketcount
= bucketcount
;
7416 s
= bfd_get_linker_section (dynobj
, ".hash");
7417 BFD_ASSERT (s
!= NULL
);
7418 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
7419 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
7420 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7421 if (s
->contents
== NULL
)
7424 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
7425 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
7426 s
->contents
+ hash_entry_size
);
7429 if (info
->emit_gnu_hash
)
7432 unsigned char *contents
;
7433 struct collect_gnu_hash_codes cinfo
;
7437 memset (&cinfo
, 0, sizeof (cinfo
));
7439 /* Compute the hash values for all exported symbols. At the same
7440 time store the values in an array so that we could use them for
7442 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
7443 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
7444 if (cinfo
.hashcodes
== NULL
)
7447 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
7448 cinfo
.min_dynindx
= -1;
7449 cinfo
.output_bfd
= output_bfd
;
7452 /* Put all hash values in HASHCODES. */
7453 elf_link_hash_traverse (elf_hash_table (info
),
7454 elf_collect_gnu_hash_codes
, &cinfo
);
7457 free (cinfo
.hashcodes
);
7462 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
7464 if (bucketcount
== 0)
7466 free (cinfo
.hashcodes
);
7470 s
= bfd_get_linker_section (dynobj
, GNU_HASH_SECTION_NAME (bed
));
7471 BFD_ASSERT (s
!= NULL
);
7473 if (cinfo
.nsyms
== 0)
7475 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7476 BFD_ASSERT (cinfo
.min_dynindx
== -1);
7477 free (cinfo
.hashcodes
);
7478 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
7479 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7480 if (contents
== NULL
)
7482 s
->contents
= contents
;
7483 /* 1 empty bucket. */
7484 bfd_put_32 (output_bfd
, 1, contents
);
7485 /* SYMIDX above the special symbol 0. */
7486 bfd_put_32 (output_bfd
, 1, contents
+ 4);
7487 /* Just one word for bitmask. */
7488 bfd_put_32 (output_bfd
, 1, contents
+ 8);
7489 /* Only hash fn bloom filter. */
7490 bfd_put_32 (output_bfd
, 0, contents
+ 12);
7491 /* No hashes are valid - empty bitmask. */
7492 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
7493 /* No hashes in the only bucket. */
7494 bfd_put_32 (output_bfd
, 0,
7495 contents
+ 16 + bed
->s
->arch_size
/ 8);
7499 unsigned long int maskwords
, maskbitslog2
, x
;
7500 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
7504 while ((x
>>= 1) != 0)
7506 if (maskbitslog2
< 3)
7508 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
7509 maskbitslog2
= maskbitslog2
+ 3;
7511 maskbitslog2
= maskbitslog2
+ 2;
7512 if (bed
->s
->arch_size
== 64)
7514 if (maskbitslog2
== 5)
7520 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
7521 cinfo
.shift2
= maskbitslog2
;
7522 cinfo
.maskbits
= 1 << maskbitslog2
;
7523 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
7524 amt
= bucketcount
* sizeof (unsigned long int) * 2;
7525 amt
+= maskwords
* sizeof (bfd_vma
);
7526 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
7527 if (cinfo
.bitmask
== NULL
)
7529 free (cinfo
.hashcodes
);
7533 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
7534 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
7535 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
7536 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
7538 /* Determine how often each hash bucket is used. */
7539 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
7540 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
7541 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
7543 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
7544 if (cinfo
.counts
[i
] != 0)
7546 cinfo
.indx
[i
] = cnt
;
7547 cnt
+= cinfo
.counts
[i
];
7549 BFD_ASSERT (cnt
== dynsymcount
);
7550 cinfo
.bucketcount
= bucketcount
;
7551 cinfo
.local_indx
= cinfo
.min_dynindx
;
7553 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
7554 s
->size
+= cinfo
.maskbits
/ 8;
7555 if (bed
->record_xhash_symbol
!= NULL
)
7556 s
->size
+= cinfo
.nsyms
* 4;
7557 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7558 if (contents
== NULL
)
7560 free (cinfo
.bitmask
);
7561 free (cinfo
.hashcodes
);
7565 s
->contents
= contents
;
7566 bfd_put_32 (output_bfd
, bucketcount
, contents
);
7567 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
7568 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
7569 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
7570 contents
+= 16 + cinfo
.maskbits
/ 8;
7572 for (i
= 0; i
< bucketcount
; ++i
)
7574 if (cinfo
.counts
[i
] == 0)
7575 bfd_put_32 (output_bfd
, 0, contents
);
7577 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
7581 cinfo
.contents
= contents
;
7583 cinfo
.xlat
= contents
+ cinfo
.nsyms
* 4 - s
->contents
;
7584 /* Renumber dynamic symbols, if populating .gnu.hash section.
7585 If using .MIPS.xhash, populate the translation table. */
7586 elf_link_hash_traverse (elf_hash_table (info
),
7587 elf_gnu_hash_process_symidx
, &cinfo
);
7589 contents
= s
->contents
+ 16;
7590 for (i
= 0; i
< maskwords
; ++i
)
7592 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
7594 contents
+= bed
->s
->arch_size
/ 8;
7597 free (cinfo
.bitmask
);
7598 free (cinfo
.hashcodes
);
7602 s
= bfd_get_linker_section (dynobj
, ".dynstr");
7603 BFD_ASSERT (s
!= NULL
);
7605 elf_finalize_dynstr (output_bfd
, info
);
7607 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7609 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
7610 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
7617 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7620 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
7623 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
7624 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
7627 /* Finish SHF_MERGE section merging. */
7630 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
7635 if (!is_elf_hash_table (info
->hash
))
7638 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
7639 if ((ibfd
->flags
& DYNAMIC
) == 0
7640 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
7641 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
7642 == get_elf_backend_data (obfd
)->s
->elfclass
))
7643 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7644 if ((sec
->flags
& SEC_MERGE
) != 0
7645 && !bfd_is_abs_section (sec
->output_section
))
7647 struct bfd_elf_section_data
*secdata
;
7649 secdata
= elf_section_data (sec
);
7650 if (! _bfd_add_merge_section (obfd
,
7651 &elf_hash_table (info
)->merge_info
,
7652 sec
, &secdata
->sec_info
))
7654 else if (secdata
->sec_info
)
7655 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7658 if (elf_hash_table (info
)->merge_info
!= NULL
)
7659 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7660 merge_sections_remove_hook
);
7664 /* Create an entry in an ELF linker hash table. */
7666 struct bfd_hash_entry
*
7667 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7668 struct bfd_hash_table
*table
,
7671 /* Allocate the structure if it has not already been allocated by a
7675 entry
= (struct bfd_hash_entry
*)
7676 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7681 /* Call the allocation method of the superclass. */
7682 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7685 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7686 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7688 /* Set local fields. */
7691 ret
->got
= htab
->init_got_refcount
;
7692 ret
->plt
= htab
->init_plt_refcount
;
7693 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7694 - offsetof (struct elf_link_hash_entry
, size
)));
7695 /* Assume that we have been called by a non-ELF symbol reader.
7696 This flag is then reset by the code which reads an ELF input
7697 file. This ensures that a symbol created by a non-ELF symbol
7698 reader will have the flag set correctly. */
7705 /* Copy data from an indirect symbol to its direct symbol, hiding the
7706 old indirect symbol. Also used for copying flags to a weakdef. */
7709 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7710 struct elf_link_hash_entry
*dir
,
7711 struct elf_link_hash_entry
*ind
)
7713 struct elf_link_hash_table
*htab
;
7715 if (ind
->dyn_relocs
!= NULL
)
7717 if (dir
->dyn_relocs
!= NULL
)
7719 struct elf_dyn_relocs
**pp
;
7720 struct elf_dyn_relocs
*p
;
7722 /* Add reloc counts against the indirect sym to the direct sym
7723 list. Merge any entries against the same section. */
7724 for (pp
= &ind
->dyn_relocs
; (p
= *pp
) != NULL
; )
7726 struct elf_dyn_relocs
*q
;
7728 for (q
= dir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
7729 if (q
->sec
== p
->sec
)
7731 q
->pc_count
+= p
->pc_count
;
7732 q
->count
+= p
->count
;
7739 *pp
= dir
->dyn_relocs
;
7742 dir
->dyn_relocs
= ind
->dyn_relocs
;
7743 ind
->dyn_relocs
= NULL
;
7746 /* Copy down any references that we may have already seen to the
7747 symbol which just became indirect. */
7749 if (dir
->versioned
!= versioned_hidden
)
7750 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7751 dir
->ref_regular
|= ind
->ref_regular
;
7752 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7753 dir
->non_got_ref
|= ind
->non_got_ref
;
7754 dir
->needs_plt
|= ind
->needs_plt
;
7755 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7757 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7760 /* Copy over the global and procedure linkage table refcount entries.
7761 These may have been already set up by a check_relocs routine. */
7762 htab
= elf_hash_table (info
);
7763 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7765 if (dir
->got
.refcount
< 0)
7766 dir
->got
.refcount
= 0;
7767 dir
->got
.refcount
+= ind
->got
.refcount
;
7768 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7771 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7773 if (dir
->plt
.refcount
< 0)
7774 dir
->plt
.refcount
= 0;
7775 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7776 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7779 if (ind
->dynindx
!= -1)
7781 if (dir
->dynindx
!= -1)
7782 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7783 dir
->dynindx
= ind
->dynindx
;
7784 dir
->dynstr_index
= ind
->dynstr_index
;
7786 ind
->dynstr_index
= 0;
7791 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7792 struct elf_link_hash_entry
*h
,
7793 bfd_boolean force_local
)
7795 /* STT_GNU_IFUNC symbol must go through PLT. */
7796 if (h
->type
!= STT_GNU_IFUNC
)
7798 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7803 h
->forced_local
= 1;
7804 if (h
->dynindx
!= -1)
7806 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7809 h
->dynstr_index
= 0;
7814 /* Hide a symbol. */
7817 _bfd_elf_link_hide_symbol (bfd
*output_bfd
,
7818 struct bfd_link_info
*info
,
7819 struct bfd_link_hash_entry
*h
)
7821 if (is_elf_hash_table (info
->hash
))
7823 const struct elf_backend_data
*bed
7824 = get_elf_backend_data (output_bfd
);
7825 struct elf_link_hash_entry
*eh
7826 = (struct elf_link_hash_entry
*) h
;
7827 bed
->elf_backend_hide_symbol (info
, eh
, TRUE
);
7828 eh
->def_dynamic
= 0;
7829 eh
->ref_dynamic
= 0;
7830 eh
->dynamic_def
= 0;
7834 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7838 _bfd_elf_link_hash_table_init
7839 (struct elf_link_hash_table
*table
,
7841 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
7842 struct bfd_hash_table
*,
7844 unsigned int entsize
,
7845 enum elf_target_id target_id
)
7848 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
7850 table
->init_got_refcount
.refcount
= can_refcount
- 1;
7851 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
7852 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
7853 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
7854 /* The first dynamic symbol is a dummy. */
7855 table
->dynsymcount
= 1;
7857 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
7859 table
->root
.type
= bfd_link_elf_hash_table
;
7860 table
->hash_table_id
= target_id
;
7861 table
->target_os
= get_elf_backend_data (abfd
)->target_os
;
7866 /* Create an ELF linker hash table. */
7868 struct bfd_link_hash_table
*
7869 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7871 struct elf_link_hash_table
*ret
;
7872 size_t amt
= sizeof (struct elf_link_hash_table
);
7874 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7878 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7879 sizeof (struct elf_link_hash_entry
),
7885 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7890 /* Destroy an ELF linker hash table. */
7893 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7895 struct elf_link_hash_table
*htab
;
7897 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7898 if (htab
->dynstr
!= NULL
)
7899 _bfd_elf_strtab_free (htab
->dynstr
);
7900 _bfd_merge_sections_free (htab
->merge_info
);
7901 _bfd_generic_link_hash_table_free (obfd
);
7904 /* This is a hook for the ELF emulation code in the generic linker to
7905 tell the backend linker what file name to use for the DT_NEEDED
7906 entry for a dynamic object. */
7909 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7911 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7912 && bfd_get_format (abfd
) == bfd_object
)
7913 elf_dt_name (abfd
) = name
;
7917 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
7920 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7921 && bfd_get_format (abfd
) == bfd_object
)
7922 lib_class
= elf_dyn_lib_class (abfd
);
7929 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
7931 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7932 && bfd_get_format (abfd
) == bfd_object
)
7933 elf_dyn_lib_class (abfd
) = lib_class
;
7936 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7937 the linker ELF emulation code. */
7939 struct bfd_link_needed_list
*
7940 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7941 struct bfd_link_info
*info
)
7943 if (! is_elf_hash_table (info
->hash
))
7945 return elf_hash_table (info
)->needed
;
7948 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7949 hook for the linker ELF emulation code. */
7951 struct bfd_link_needed_list
*
7952 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7953 struct bfd_link_info
*info
)
7955 if (! is_elf_hash_table (info
->hash
))
7957 return elf_hash_table (info
)->runpath
;
7960 /* Get the name actually used for a dynamic object for a link. This
7961 is the SONAME entry if there is one. Otherwise, it is the string
7962 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7965 bfd_elf_get_dt_soname (bfd
*abfd
)
7967 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7968 && bfd_get_format (abfd
) == bfd_object
)
7969 return elf_dt_name (abfd
);
7973 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7974 the ELF linker emulation code. */
7977 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
7978 struct bfd_link_needed_list
**pneeded
)
7981 bfd_byte
*dynbuf
= NULL
;
7982 unsigned int elfsec
;
7983 unsigned long shlink
;
7984 bfd_byte
*extdyn
, *extdynend
;
7986 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
7990 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
7991 || bfd_get_format (abfd
) != bfd_object
)
7994 s
= bfd_get_section_by_name (abfd
, ".dynamic");
7995 if (s
== NULL
|| s
->size
== 0)
7998 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
8001 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
8002 if (elfsec
== SHN_BAD
)
8005 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
8007 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
8008 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
8011 extdynend
= extdyn
+ s
->size
;
8012 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
8014 Elf_Internal_Dyn dyn
;
8016 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
8018 if (dyn
.d_tag
== DT_NULL
)
8021 if (dyn
.d_tag
== DT_NEEDED
)
8024 struct bfd_link_needed_list
*l
;
8025 unsigned int tagv
= dyn
.d_un
.d_val
;
8028 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
8033 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
8053 struct elf_symbuf_symbol
8055 unsigned long st_name
; /* Symbol name, index in string tbl */
8056 unsigned char st_info
; /* Type and binding attributes */
8057 unsigned char st_other
; /* Visibilty, and target specific */
8060 struct elf_symbuf_head
8062 struct elf_symbuf_symbol
*ssym
;
8064 unsigned int st_shndx
;
8071 Elf_Internal_Sym
*isym
;
8072 struct elf_symbuf_symbol
*ssym
;
8078 /* Sort references to symbols by ascending section number. */
8081 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
8083 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
8084 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
8086 if (s1
->st_shndx
!= s2
->st_shndx
)
8087 return s1
->st_shndx
> s2
->st_shndx
? 1 : -1;
8088 /* Final sort by the address of the sym in the symbuf ensures
8091 return s1
> s2
? 1 : -1;
8096 elf_sym_name_compare (const void *arg1
, const void *arg2
)
8098 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
8099 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
8100 int ret
= strcmp (s1
->name
, s2
->name
);
8103 if (s1
->u
.p
!= s2
->u
.p
)
8104 return s1
->u
.p
> s2
->u
.p
? 1 : -1;
8108 static struct elf_symbuf_head
*
8109 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
8111 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
8112 struct elf_symbuf_symbol
*ssym
;
8113 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
8114 size_t i
, shndx_count
, total_size
, amt
;
8116 amt
= symcount
* sizeof (*indbuf
);
8117 indbuf
= (Elf_Internal_Sym
**) bfd_malloc (amt
);
8121 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
8122 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
8123 *ind
++ = &isymbuf
[i
];
8126 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
8127 elf_sort_elf_symbol
);
8130 if (indbufend
> indbuf
)
8131 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
8132 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
8135 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
8136 + (indbufend
- indbuf
) * sizeof (*ssym
));
8137 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
8138 if (ssymbuf
== NULL
)
8144 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
8145 ssymbuf
->ssym
= NULL
;
8146 ssymbuf
->count
= shndx_count
;
8147 ssymbuf
->st_shndx
= 0;
8148 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
8150 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
8153 ssymhead
->ssym
= ssym
;
8154 ssymhead
->count
= 0;
8155 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
8157 ssym
->st_name
= (*ind
)->st_name
;
8158 ssym
->st_info
= (*ind
)->st_info
;
8159 ssym
->st_other
= (*ind
)->st_other
;
8162 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
8163 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
8170 /* Check if 2 sections define the same set of local and global
8174 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
8175 struct bfd_link_info
*info
)
8178 const struct elf_backend_data
*bed1
, *bed2
;
8179 Elf_Internal_Shdr
*hdr1
, *hdr2
;
8180 size_t symcount1
, symcount2
;
8181 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
8182 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
8183 Elf_Internal_Sym
*isym
, *isymend
;
8184 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
8185 size_t count1
, count2
, i
;
8186 unsigned int shndx1
, shndx2
;
8192 /* Both sections have to be in ELF. */
8193 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
8194 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
8197 if (elf_section_type (sec1
) != elf_section_type (sec2
))
8200 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
8201 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
8202 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
8205 bed1
= get_elf_backend_data (bfd1
);
8206 bed2
= get_elf_backend_data (bfd2
);
8207 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
8208 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
8209 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
8210 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
8212 if (symcount1
== 0 || symcount2
== 0)
8218 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
8219 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
8221 if (ssymbuf1
== NULL
)
8223 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
8225 if (isymbuf1
== NULL
)
8228 if (info
!= NULL
&& !info
->reduce_memory_overheads
)
8230 ssymbuf1
= elf_create_symbuf (symcount1
, isymbuf1
);
8231 elf_tdata (bfd1
)->symbuf
= ssymbuf1
;
8235 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
8237 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
8239 if (isymbuf2
== NULL
)
8242 if (ssymbuf1
!= NULL
&& info
!= NULL
&& !info
->reduce_memory_overheads
)
8244 ssymbuf2
= elf_create_symbuf (symcount2
, isymbuf2
);
8245 elf_tdata (bfd2
)->symbuf
= ssymbuf2
;
8249 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
8251 /* Optimized faster version. */
8253 struct elf_symbol
*symp
;
8254 struct elf_symbuf_symbol
*ssym
, *ssymend
;
8257 hi
= ssymbuf1
->count
;
8262 mid
= (lo
+ hi
) / 2;
8263 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
8265 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
8269 count1
= ssymbuf1
[mid
].count
;
8276 hi
= ssymbuf2
->count
;
8281 mid
= (lo
+ hi
) / 2;
8282 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
8284 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
8288 count2
= ssymbuf2
[mid
].count
;
8294 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8298 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
8300 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
8301 if (symtable1
== NULL
|| symtable2
== NULL
)
8305 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
8306 ssym
< ssymend
; ssym
++, symp
++)
8308 symp
->u
.ssym
= ssym
;
8309 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
8315 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
8316 ssym
< ssymend
; ssym
++, symp
++)
8318 symp
->u
.ssym
= ssym
;
8319 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
8324 /* Sort symbol by name. */
8325 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8326 elf_sym_name_compare
);
8327 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8328 elf_sym_name_compare
);
8330 for (i
= 0; i
< count1
; i
++)
8331 /* Two symbols must have the same binding, type and name. */
8332 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
8333 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
8334 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8341 symtable1
= (struct elf_symbol
*)
8342 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
8343 symtable2
= (struct elf_symbol
*)
8344 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
8345 if (symtable1
== NULL
|| symtable2
== NULL
)
8348 /* Count definitions in the section. */
8350 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
8351 if (isym
->st_shndx
== shndx1
)
8352 symtable1
[count1
++].u
.isym
= isym
;
8355 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
8356 if (isym
->st_shndx
== shndx2
)
8357 symtable2
[count2
++].u
.isym
= isym
;
8359 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8362 for (i
= 0; i
< count1
; i
++)
8364 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
8365 symtable1
[i
].u
.isym
->st_name
);
8367 for (i
= 0; i
< count2
; i
++)
8369 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
8370 symtable2
[i
].u
.isym
->st_name
);
8372 /* Sort symbol by name. */
8373 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8374 elf_sym_name_compare
);
8375 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8376 elf_sym_name_compare
);
8378 for (i
= 0; i
< count1
; i
++)
8379 /* Two symbols must have the same binding, type and name. */
8380 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
8381 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
8382 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8396 /* Return TRUE if 2 section types are compatible. */
8399 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
8400 bfd
*bbfd
, const asection
*bsec
)
8404 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
8405 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8408 return elf_section_type (asec
) == elf_section_type (bsec
);
8411 /* Final phase of ELF linker. */
8413 /* A structure we use to avoid passing large numbers of arguments. */
8415 struct elf_final_link_info
8417 /* General link information. */
8418 struct bfd_link_info
*info
;
8421 /* Symbol string table. */
8422 struct elf_strtab_hash
*symstrtab
;
8423 /* .hash section. */
8425 /* symbol version section (.gnu.version). */
8426 asection
*symver_sec
;
8427 /* Buffer large enough to hold contents of any section. */
8429 /* Buffer large enough to hold external relocs of any section. */
8430 void *external_relocs
;
8431 /* Buffer large enough to hold internal relocs of any section. */
8432 Elf_Internal_Rela
*internal_relocs
;
8433 /* Buffer large enough to hold external local symbols of any input
8435 bfd_byte
*external_syms
;
8436 /* And a buffer for symbol section indices. */
8437 Elf_External_Sym_Shndx
*locsym_shndx
;
8438 /* Buffer large enough to hold internal local symbols of any input
8440 Elf_Internal_Sym
*internal_syms
;
8441 /* Array large enough to hold a symbol index for each local symbol
8442 of any input BFD. */
8444 /* Array large enough to hold a section pointer for each local
8445 symbol of any input BFD. */
8446 asection
**sections
;
8447 /* Buffer for SHT_SYMTAB_SHNDX section. */
8448 Elf_External_Sym_Shndx
*symshndxbuf
;
8449 /* Number of STT_FILE syms seen. */
8450 size_t filesym_count
;
8451 /* Local symbol hash table. */
8452 struct bfd_hash_table local_hash_table
;
8455 struct local_hash_entry
8457 /* Base hash table entry structure. */
8458 struct bfd_hash_entry root
;
8459 /* Size of the local symbol name. */
8461 /* Number of the duplicated local symbol names. */
8465 /* Create an entry in the local symbol hash table. */
8467 static struct bfd_hash_entry
*
8468 local_hash_newfunc (struct bfd_hash_entry
*entry
,
8469 struct bfd_hash_table
*table
,
8473 /* Allocate the structure if it has not already been allocated by a
8477 entry
= bfd_hash_allocate (table
,
8478 sizeof (struct local_hash_entry
));
8483 /* Call the allocation method of the superclass. */
8484 entry
= bfd_hash_newfunc (entry
, table
, string
);
8487 ((struct local_hash_entry
*) entry
)->count
= 0;
8488 ((struct local_hash_entry
*) entry
)->size
= 0;
8494 /* This struct is used to pass information to elf_link_output_extsym. */
8496 struct elf_outext_info
8499 bfd_boolean localsyms
;
8500 bfd_boolean file_sym_done
;
8501 struct elf_final_link_info
*flinfo
;
8505 /* Support for evaluating a complex relocation.
8507 Complex relocations are generalized, self-describing relocations. The
8508 implementation of them consists of two parts: complex symbols, and the
8509 relocations themselves.
8511 The relocations use a reserved elf-wide relocation type code (R_RELC
8512 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8513 information (start bit, end bit, word width, etc) into the addend. This
8514 information is extracted from CGEN-generated operand tables within gas.
8516 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8517 internal) representing prefix-notation expressions, including but not
8518 limited to those sorts of expressions normally encoded as addends in the
8519 addend field. The symbol mangling format is:
8522 | <unary-operator> ':' <node>
8523 | <binary-operator> ':' <node> ':' <node>
8526 <literal> := 's' <digits=N> ':' <N character symbol name>
8527 | 'S' <digits=N> ':' <N character section name>
8531 <binary-operator> := as in C
8532 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8535 set_symbol_value (bfd
*bfd_with_globals
,
8536 Elf_Internal_Sym
*isymbuf
,
8541 struct elf_link_hash_entry
**sym_hashes
;
8542 struct elf_link_hash_entry
*h
;
8543 size_t extsymoff
= locsymcount
;
8545 if (symidx
< locsymcount
)
8547 Elf_Internal_Sym
*sym
;
8549 sym
= isymbuf
+ symidx
;
8550 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
8552 /* It is a local symbol: move it to the
8553 "absolute" section and give it a value. */
8554 sym
->st_shndx
= SHN_ABS
;
8555 sym
->st_value
= val
;
8558 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
8562 /* It is a global symbol: set its link type
8563 to "defined" and give it a value. */
8565 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
8566 h
= sym_hashes
[symidx
- extsymoff
];
8567 while (h
->root
.type
== bfd_link_hash_indirect
8568 || h
->root
.type
== bfd_link_hash_warning
)
8569 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8570 h
->root
.type
= bfd_link_hash_defined
;
8571 h
->root
.u
.def
.value
= val
;
8572 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
8576 resolve_symbol (const char *name
,
8578 struct elf_final_link_info
*flinfo
,
8580 Elf_Internal_Sym
*isymbuf
,
8583 Elf_Internal_Sym
*sym
;
8584 struct bfd_link_hash_entry
*global_entry
;
8585 const char *candidate
= NULL
;
8586 Elf_Internal_Shdr
*symtab_hdr
;
8589 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
8591 for (i
= 0; i
< locsymcount
; ++ i
)
8595 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
8598 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
8599 symtab_hdr
->sh_link
,
8602 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8603 name
, candidate
, (unsigned long) sym
->st_value
);
8605 if (candidate
&& strcmp (candidate
, name
) == 0)
8607 asection
*sec
= flinfo
->sections
[i
];
8609 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
8610 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
8612 printf ("Found symbol with value %8.8lx\n",
8613 (unsigned long) *result
);
8619 /* Hmm, haven't found it yet. perhaps it is a global. */
8620 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
8621 FALSE
, FALSE
, TRUE
);
8625 if (global_entry
->type
== bfd_link_hash_defined
8626 || global_entry
->type
== bfd_link_hash_defweak
)
8628 *result
= (global_entry
->u
.def
.value
8629 + global_entry
->u
.def
.section
->output_section
->vma
8630 + global_entry
->u
.def
.section
->output_offset
);
8632 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8633 global_entry
->root
.string
, (unsigned long) *result
);
8641 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8642 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8643 names like "foo.end" which is the end address of section "foo". */
8646 resolve_section (const char *name
,
8654 for (curr
= sections
; curr
; curr
= curr
->next
)
8655 if (strcmp (curr
->name
, name
) == 0)
8657 *result
= curr
->vma
;
8661 /* Hmm. still haven't found it. try pseudo-section names. */
8662 /* FIXME: This could be coded more efficiently... */
8663 for (curr
= sections
; curr
; curr
= curr
->next
)
8665 len
= strlen (curr
->name
);
8666 if (len
> strlen (name
))
8669 if (strncmp (curr
->name
, name
, len
) == 0)
8671 if (strncmp (".end", name
+ len
, 4) == 0)
8673 *result
= (curr
->vma
8674 + curr
->size
/ bfd_octets_per_byte (abfd
, curr
));
8678 /* Insert more pseudo-section names here, if you like. */
8686 undefined_reference (const char *reftype
, const char *name
)
8688 /* xgettext:c-format */
8689 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8691 bfd_set_error (bfd_error_bad_value
);
8695 eval_symbol (bfd_vma
*result
,
8698 struct elf_final_link_info
*flinfo
,
8700 Elf_Internal_Sym
*isymbuf
,
8709 const char *sym
= *symp
;
8711 bfd_boolean symbol_is_section
= FALSE
;
8716 if (len
< 1 || len
> sizeof (symbuf
))
8718 bfd_set_error (bfd_error_invalid_operation
);
8731 *result
= strtoul (sym
, (char **) symp
, 16);
8735 symbol_is_section
= TRUE
;
8739 symlen
= strtol (sym
, (char **) symp
, 10);
8740 sym
= *symp
+ 1; /* Skip the trailing ':'. */
8742 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
8744 bfd_set_error (bfd_error_invalid_operation
);
8748 memcpy (symbuf
, sym
, symlen
);
8749 symbuf
[symlen
] = '\0';
8750 *symp
= sym
+ symlen
;
8752 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8753 the symbol as a section, or vice-versa. so we're pretty liberal in our
8754 interpretation here; section means "try section first", not "must be a
8755 section", and likewise with symbol. */
8757 if (symbol_is_section
)
8759 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
8760 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8761 isymbuf
, locsymcount
))
8763 undefined_reference ("section", symbuf
);
8769 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8770 isymbuf
, locsymcount
)
8771 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8774 undefined_reference ("symbol", symbuf
);
8781 /* All that remains are operators. */
8783 #define UNARY_OP(op) \
8784 if (strncmp (sym, #op, strlen (#op)) == 0) \
8786 sym += strlen (#op); \
8790 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8791 isymbuf, locsymcount, signed_p)) \
8794 *result = op ((bfd_signed_vma) a); \
8800 #define BINARY_OP_HEAD(op) \
8801 if (strncmp (sym, #op, strlen (#op)) == 0) \
8803 sym += strlen (#op); \
8807 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8808 isymbuf, locsymcount, signed_p)) \
8811 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8812 isymbuf, locsymcount, signed_p)) \
8814 #define BINARY_OP_TAIL(op) \
8816 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8821 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
8825 BINARY_OP_HEAD (<<);
8826 if (b
>= sizeof (a
) * CHAR_BIT
)
8832 BINARY_OP_TAIL (<<);
8833 BINARY_OP_HEAD (>>);
8834 if (b
>= sizeof (a
) * CHAR_BIT
)
8836 *result
= signed_p
&& (bfd_signed_vma
) a
< 0 ? -1 : 0;
8839 BINARY_OP_TAIL (>>);
8852 _bfd_error_handler (_("division by zero"));
8853 bfd_set_error (bfd_error_bad_value
);
8860 _bfd_error_handler (_("division by zero"));
8861 bfd_set_error (bfd_error_bad_value
);
8874 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
8875 bfd_set_error (bfd_error_invalid_operation
);
8881 put_value (bfd_vma size
,
8882 unsigned long chunksz
,
8887 location
+= (size
- chunksz
);
8889 for (; size
; size
-= chunksz
, location
-= chunksz
)
8894 bfd_put_8 (input_bfd
, x
, location
);
8898 bfd_put_16 (input_bfd
, x
, location
);
8902 bfd_put_32 (input_bfd
, x
, location
);
8903 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8909 bfd_put_64 (input_bfd
, x
, location
);
8910 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8923 get_value (bfd_vma size
,
8924 unsigned long chunksz
,
8931 /* Sanity checks. */
8932 BFD_ASSERT (chunksz
<= sizeof (x
)
8935 && (size
% chunksz
) == 0
8936 && input_bfd
!= NULL
8937 && location
!= NULL
);
8939 if (chunksz
== sizeof (x
))
8941 BFD_ASSERT (size
== chunksz
);
8943 /* Make sure that we do not perform an undefined shift operation.
8944 We know that size == chunksz so there will only be one iteration
8945 of the loop below. */
8949 shift
= 8 * chunksz
;
8951 for (; size
; size
-= chunksz
, location
+= chunksz
)
8956 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
8959 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
8962 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
8966 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
8977 decode_complex_addend (unsigned long *start
, /* in bits */
8978 unsigned long *oplen
, /* in bits */
8979 unsigned long *len
, /* in bits */
8980 unsigned long *wordsz
, /* in bytes */
8981 unsigned long *chunksz
, /* in bytes */
8982 unsigned long *lsb0_p
,
8983 unsigned long *signed_p
,
8984 unsigned long *trunc_p
,
8985 unsigned long encoded
)
8987 * start
= encoded
& 0x3F;
8988 * len
= (encoded
>> 6) & 0x3F;
8989 * oplen
= (encoded
>> 12) & 0x3F;
8990 * wordsz
= (encoded
>> 18) & 0xF;
8991 * chunksz
= (encoded
>> 22) & 0xF;
8992 * lsb0_p
= (encoded
>> 27) & 1;
8993 * signed_p
= (encoded
>> 28) & 1;
8994 * trunc_p
= (encoded
>> 29) & 1;
8997 bfd_reloc_status_type
8998 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
8999 asection
*input_section
,
9001 Elf_Internal_Rela
*rel
,
9004 bfd_vma shift
, x
, mask
;
9005 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
9006 bfd_reloc_status_type r
;
9007 bfd_size_type octets
;
9009 /* Perform this reloc, since it is complex.
9010 (this is not to say that it necessarily refers to a complex
9011 symbol; merely that it is a self-describing CGEN based reloc.
9012 i.e. the addend has the complete reloc information (bit start, end,
9013 word size, etc) encoded within it.). */
9015 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
9016 &chunksz
, &lsb0_p
, &signed_p
,
9017 &trunc_p
, rel
->r_addend
);
9019 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
9022 shift
= (start
+ 1) - len
;
9024 shift
= (8 * wordsz
) - (start
+ len
);
9026 octets
= rel
->r_offset
* bfd_octets_per_byte (input_bfd
, input_section
);
9027 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ octets
);
9030 printf ("Doing complex reloc: "
9031 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9032 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9033 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9034 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
9035 oplen
, (unsigned long) x
, (unsigned long) mask
,
9036 (unsigned long) relocation
);
9041 /* Now do an overflow check. */
9042 r
= bfd_check_overflow ((signed_p
9043 ? complain_overflow_signed
9044 : complain_overflow_unsigned
),
9045 len
, 0, (8 * wordsz
),
9049 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
9052 printf (" relocation: %8.8lx\n"
9053 " shifted mask: %8.8lx\n"
9054 " shifted/masked reloc: %8.8lx\n"
9055 " result: %8.8lx\n",
9056 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
9057 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
9059 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ octets
);
9063 /* Functions to read r_offset from external (target order) reloc
9064 entry. Faster than bfd_getl32 et al, because we let the compiler
9065 know the value is aligned. */
9068 ext32l_r_offset (const void *p
)
9075 const union aligned32
*a
9076 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9078 uint32_t aval
= ( (uint32_t) a
->c
[0]
9079 | (uint32_t) a
->c
[1] << 8
9080 | (uint32_t) a
->c
[2] << 16
9081 | (uint32_t) a
->c
[3] << 24);
9086 ext32b_r_offset (const void *p
)
9093 const union aligned32
*a
9094 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9096 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
9097 | (uint32_t) a
->c
[1] << 16
9098 | (uint32_t) a
->c
[2] << 8
9099 | (uint32_t) a
->c
[3]);
9103 #ifdef BFD_HOST_64_BIT
9105 ext64l_r_offset (const void *p
)
9112 const union aligned64
*a
9113 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9115 uint64_t aval
= ( (uint64_t) a
->c
[0]
9116 | (uint64_t) a
->c
[1] << 8
9117 | (uint64_t) a
->c
[2] << 16
9118 | (uint64_t) a
->c
[3] << 24
9119 | (uint64_t) a
->c
[4] << 32
9120 | (uint64_t) a
->c
[5] << 40
9121 | (uint64_t) a
->c
[6] << 48
9122 | (uint64_t) a
->c
[7] << 56);
9127 ext64b_r_offset (const void *p
)
9134 const union aligned64
*a
9135 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9137 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
9138 | (uint64_t) a
->c
[1] << 48
9139 | (uint64_t) a
->c
[2] << 40
9140 | (uint64_t) a
->c
[3] << 32
9141 | (uint64_t) a
->c
[4] << 24
9142 | (uint64_t) a
->c
[5] << 16
9143 | (uint64_t) a
->c
[6] << 8
9144 | (uint64_t) a
->c
[7]);
9149 /* When performing a relocatable link, the input relocations are
9150 preserved. But, if they reference global symbols, the indices
9151 referenced must be updated. Update all the relocations found in
9155 elf_link_adjust_relocs (bfd
*abfd
,
9157 struct bfd_elf_section_reloc_data
*reldata
,
9159 struct bfd_link_info
*info
)
9162 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9164 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9165 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9166 bfd_vma r_type_mask
;
9168 unsigned int count
= reldata
->count
;
9169 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
9171 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
9173 swap_in
= bed
->s
->swap_reloc_in
;
9174 swap_out
= bed
->s
->swap_reloc_out
;
9176 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
9178 swap_in
= bed
->s
->swap_reloca_in
;
9179 swap_out
= bed
->s
->swap_reloca_out
;
9184 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
9187 if (bed
->s
->arch_size
== 32)
9194 r_type_mask
= 0xffffffff;
9198 erela
= reldata
->hdr
->contents
;
9199 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
9201 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
9204 if (*rel_hash
== NULL
)
9207 if ((*rel_hash
)->indx
== -2
9208 && info
->gc_sections
9209 && ! info
->gc_keep_exported
)
9211 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9212 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9214 (*rel_hash
)->root
.root
.string
);
9215 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9217 bfd_set_error (bfd_error_invalid_operation
);
9220 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
9222 (*swap_in
) (abfd
, erela
, irela
);
9223 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
9224 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
9225 | (irela
[j
].r_info
& r_type_mask
));
9226 (*swap_out
) (abfd
, irela
, erela
);
9229 if (bed
->elf_backend_update_relocs
)
9230 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
9232 if (sort
&& count
!= 0)
9234 bfd_vma (*ext_r_off
) (const void *);
9237 bfd_byte
*base
, *end
, *p
, *loc
;
9238 bfd_byte
*buf
= NULL
;
9240 if (bed
->s
->arch_size
== 32)
9242 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9243 ext_r_off
= ext32l_r_offset
;
9244 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9245 ext_r_off
= ext32b_r_offset
;
9251 #ifdef BFD_HOST_64_BIT
9252 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9253 ext_r_off
= ext64l_r_offset
;
9254 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9255 ext_r_off
= ext64b_r_offset
;
9261 /* Must use a stable sort here. A modified insertion sort,
9262 since the relocs are mostly sorted already. */
9263 elt_size
= reldata
->hdr
->sh_entsize
;
9264 base
= reldata
->hdr
->contents
;
9265 end
= base
+ count
* elt_size
;
9266 if (elt_size
> sizeof (Elf64_External_Rela
))
9269 /* Ensure the first element is lowest. This acts as a sentinel,
9270 speeding the main loop below. */
9271 r_off
= (*ext_r_off
) (base
);
9272 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
9274 bfd_vma r_off2
= (*ext_r_off
) (p
);
9283 /* Don't just swap *base and *loc as that changes the order
9284 of the original base[0] and base[1] if they happen to
9285 have the same r_offset. */
9286 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
9287 memcpy (onebuf
, loc
, elt_size
);
9288 memmove (base
+ elt_size
, base
, loc
- base
);
9289 memcpy (base
, onebuf
, elt_size
);
9292 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
9294 /* base to p is sorted, *p is next to insert. */
9295 r_off
= (*ext_r_off
) (p
);
9296 /* Search the sorted region for location to insert. */
9298 while (r_off
< (*ext_r_off
) (loc
))
9303 /* Chances are there is a run of relocs to insert here,
9304 from one of more input files. Files are not always
9305 linked in order due to the way elf_link_input_bfd is
9306 called. See pr17666. */
9307 size_t sortlen
= p
- loc
;
9308 bfd_vma r_off2
= (*ext_r_off
) (loc
);
9309 size_t runlen
= elt_size
;
9310 size_t buf_size
= 96 * 1024;
9311 while (p
+ runlen
< end
9312 && (sortlen
<= buf_size
9313 || runlen
+ elt_size
<= buf_size
)
9314 && r_off2
> (*ext_r_off
) (p
+ runlen
))
9318 buf
= bfd_malloc (buf_size
);
9322 if (runlen
< sortlen
)
9324 memcpy (buf
, p
, runlen
);
9325 memmove (loc
+ runlen
, loc
, sortlen
);
9326 memcpy (loc
, buf
, runlen
);
9330 memcpy (buf
, loc
, sortlen
);
9331 memmove (loc
, p
, runlen
);
9332 memcpy (loc
+ runlen
, buf
, sortlen
);
9334 p
+= runlen
- elt_size
;
9337 /* Hashes are no longer valid. */
9338 free (reldata
->hashes
);
9339 reldata
->hashes
= NULL
;
9345 struct elf_link_sort_rela
9351 enum elf_reloc_type_class type
;
9352 /* We use this as an array of size int_rels_per_ext_rel. */
9353 Elf_Internal_Rela rela
[1];
9356 /* qsort stability here and for cmp2 is only an issue if multiple
9357 dynamic relocations are emitted at the same address. But targets
9358 that apply a series of dynamic relocations each operating on the
9359 result of the prior relocation can't use -z combreloc as
9360 implemented anyway. Such schemes tend to be broken by sorting on
9361 symbol index. That leaves dynamic NONE relocs as the only other
9362 case where ld might emit multiple relocs at the same address, and
9363 those are only emitted due to target bugs. */
9366 elf_link_sort_cmp1 (const void *A
, const void *B
)
9368 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9369 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9370 int relativea
, relativeb
;
9372 relativea
= a
->type
== reloc_class_relative
;
9373 relativeb
= b
->type
== reloc_class_relative
;
9375 if (relativea
< relativeb
)
9377 if (relativea
> relativeb
)
9379 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
9381 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
9383 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9385 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9391 elf_link_sort_cmp2 (const void *A
, const void *B
)
9393 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9394 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9396 if (a
->type
< b
->type
)
9398 if (a
->type
> b
->type
)
9400 if (a
->u
.offset
< b
->u
.offset
)
9402 if (a
->u
.offset
> b
->u
.offset
)
9404 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9406 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9412 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
9414 asection
*dynamic_relocs
;
9417 bfd_size_type count
, size
;
9418 size_t i
, ret
, sort_elt
, ext_size
;
9419 bfd_byte
*sort
, *s_non_relative
, *p
;
9420 struct elf_link_sort_rela
*sq
;
9421 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9422 int i2e
= bed
->s
->int_rels_per_ext_rel
;
9423 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
9424 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9425 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9426 struct bfd_link_order
*lo
;
9428 bfd_boolean use_rela
;
9430 /* Find a dynamic reloc section. */
9431 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
9432 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
9433 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
9434 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9436 bfd_boolean use_rela_initialised
= FALSE
;
9438 /* This is just here to stop gcc from complaining.
9439 Its initialization checking code is not perfect. */
9442 /* Both sections are present. Examine the sizes
9443 of the indirect sections to help us choose. */
9444 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9445 if (lo
->type
== bfd_indirect_link_order
)
9447 asection
*o
= lo
->u
.indirect
.section
;
9449 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9451 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9452 /* Section size is divisible by both rel and rela sizes.
9453 It is of no help to us. */
9457 /* Section size is only divisible by rela. */
9458 if (use_rela_initialised
&& !use_rela
)
9460 _bfd_error_handler (_("%pB: unable to sort relocs - "
9461 "they are in more than one size"),
9463 bfd_set_error (bfd_error_invalid_operation
);
9469 use_rela_initialised
= TRUE
;
9473 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9475 /* Section size is only divisible by rel. */
9476 if (use_rela_initialised
&& use_rela
)
9478 _bfd_error_handler (_("%pB: unable to sort relocs - "
9479 "they are in more than one size"),
9481 bfd_set_error (bfd_error_invalid_operation
);
9487 use_rela_initialised
= TRUE
;
9492 /* The section size is not divisible by either -
9493 something is wrong. */
9494 _bfd_error_handler (_("%pB: unable to sort relocs - "
9495 "they are of an unknown size"), abfd
);
9496 bfd_set_error (bfd_error_invalid_operation
);
9501 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9502 if (lo
->type
== bfd_indirect_link_order
)
9504 asection
*o
= lo
->u
.indirect
.section
;
9506 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9508 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9509 /* Section size is divisible by both rel and rela sizes.
9510 It is of no help to us. */
9514 /* Section size is only divisible by rela. */
9515 if (use_rela_initialised
&& !use_rela
)
9517 _bfd_error_handler (_("%pB: unable to sort relocs - "
9518 "they are in more than one size"),
9520 bfd_set_error (bfd_error_invalid_operation
);
9526 use_rela_initialised
= TRUE
;
9530 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9532 /* Section size is only divisible by rel. */
9533 if (use_rela_initialised
&& use_rela
)
9535 _bfd_error_handler (_("%pB: unable to sort relocs - "
9536 "they are in more than one size"),
9538 bfd_set_error (bfd_error_invalid_operation
);
9544 use_rela_initialised
= TRUE
;
9549 /* The section size is not divisible by either -
9550 something is wrong. */
9551 _bfd_error_handler (_("%pB: unable to sort relocs - "
9552 "they are of an unknown size"), abfd
);
9553 bfd_set_error (bfd_error_invalid_operation
);
9558 if (! use_rela_initialised
)
9562 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
9564 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9571 dynamic_relocs
= rela_dyn
;
9572 ext_size
= bed
->s
->sizeof_rela
;
9573 swap_in
= bed
->s
->swap_reloca_in
;
9574 swap_out
= bed
->s
->swap_reloca_out
;
9578 dynamic_relocs
= rel_dyn
;
9579 ext_size
= bed
->s
->sizeof_rel
;
9580 swap_in
= bed
->s
->swap_reloc_in
;
9581 swap_out
= bed
->s
->swap_reloc_out
;
9585 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9586 if (lo
->type
== bfd_indirect_link_order
)
9587 size
+= lo
->u
.indirect
.section
->size
;
9589 if (size
!= dynamic_relocs
->size
)
9592 sort_elt
= (sizeof (struct elf_link_sort_rela
)
9593 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
9595 count
= dynamic_relocs
->size
/ ext_size
;
9598 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
9602 (*info
->callbacks
->warning
)
9603 (info
, _("not enough memory to sort relocations"), 0, abfd
, 0, 0);
9607 if (bed
->s
->arch_size
== 32)
9608 r_sym_mask
= ~(bfd_vma
) 0xff;
9610 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
9612 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9613 if (lo
->type
== bfd_indirect_link_order
)
9615 bfd_byte
*erel
, *erelend
;
9616 asection
*o
= lo
->u
.indirect
.section
;
9618 if (o
->contents
== NULL
&& o
->size
!= 0)
9620 /* This is a reloc section that is being handled as a normal
9621 section. See bfd_section_from_shdr. We can't combine
9622 relocs in this case. */
9627 erelend
= o
->contents
+ o
->size
;
9628 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
9630 while (erel
< erelend
)
9632 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9634 (*swap_in
) (abfd
, erel
, s
->rela
);
9635 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
9636 s
->u
.sym_mask
= r_sym_mask
;
9642 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
9644 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
9646 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9647 if (s
->type
!= reloc_class_relative
)
9653 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
9654 for (; i
< count
; i
++, p
+= sort_elt
)
9656 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
9657 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
9659 sp
->u
.offset
= sq
->rela
->r_offset
;
9662 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
9664 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
9665 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
9667 /* We have plt relocs in .rela.dyn. */
9668 sq
= (struct elf_link_sort_rela
*) sort
;
9669 for (i
= 0; i
< count
; i
++)
9670 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
9672 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
9674 struct bfd_link_order
**plo
;
9675 /* Put srelplt link_order last. This is so the output_offset
9676 set in the next loop is correct for DT_JMPREL. */
9677 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
9678 if ((*plo
)->type
== bfd_indirect_link_order
9679 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
9685 plo
= &(*plo
)->next
;
9688 dynamic_relocs
->map_tail
.link_order
= lo
;
9693 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9694 if (lo
->type
== bfd_indirect_link_order
)
9696 bfd_byte
*erel
, *erelend
;
9697 asection
*o
= lo
->u
.indirect
.section
;
9700 erelend
= o
->contents
+ o
->size
;
9701 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
9702 while (erel
< erelend
)
9704 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9705 (*swap_out
) (abfd
, s
->rela
, erel
);
9712 *psec
= dynamic_relocs
;
9716 /* Add a symbol to the output symbol string table. */
9719 elf_link_output_symstrtab (struct elf_final_link_info
*flinfo
,
9721 Elf_Internal_Sym
*elfsym
,
9722 asection
*input_sec
,
9723 struct elf_link_hash_entry
*h
)
9725 int (*output_symbol_hook
)
9726 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
9727 struct elf_link_hash_entry
*);
9728 struct elf_link_hash_table
*hash_table
;
9729 const struct elf_backend_data
*bed
;
9730 bfd_size_type strtabsize
;
9732 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9734 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9735 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
9736 if (output_symbol_hook
!= NULL
)
9738 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
9743 if (ELF_ST_TYPE (elfsym
->st_info
) == STT_GNU_IFUNC
)
9744 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_ifunc
;
9745 if (ELF_ST_BIND (elfsym
->st_info
) == STB_GNU_UNIQUE
)
9746 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_unique
;
9750 || (input_sec
->flags
& SEC_EXCLUDE
))
9751 elfsym
->st_name
= (unsigned long) -1;
9754 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9755 to get the final offset for st_name. */
9756 char *versioned_name
= (char *) name
;
9759 if (h
->versioned
== versioned
&& h
->def_dynamic
)
9761 /* Keep only one '@' for versioned symbols defined in
9763 char *version
= strrchr (name
, ELF_VER_CHR
);
9764 char *base_end
= strchr (name
, ELF_VER_CHR
);
9765 if (version
!= base_end
)
9768 size_t len
= strlen (name
);
9769 versioned_name
= bfd_alloc (flinfo
->output_bfd
, len
);
9770 if (versioned_name
== NULL
)
9772 base_len
= base_end
- name
;
9773 memcpy (versioned_name
, name
, base_len
);
9774 memcpy (versioned_name
+ base_len
, version
,
9779 else if (flinfo
->info
->unique_symbol
9780 && ELF_ST_BIND (elfsym
->st_info
) == STB_LOCAL
)
9782 struct local_hash_entry
*lh
;
9783 switch (ELF_ST_TYPE (elfsym
->st_info
))
9789 lh
= (struct local_hash_entry
*) bfd_hash_lookup
9790 (&flinfo
->local_hash_table
, name
, TRUE
, FALSE
);
9795 /* Append ".COUNT" to duplicated local symbols. */
9797 size_t base_len
= lh
->size
;
9799 sprintf (buf
, "%lx", lh
->count
);
9802 base_len
= strlen (name
);
9803 lh
->size
= base_len
;
9805 count_len
= strlen (buf
);
9806 versioned_name
= bfd_alloc (flinfo
->output_bfd
,
9807 base_len
+ count_len
+ 2);
9808 if (versioned_name
== NULL
)
9810 memcpy (versioned_name
, name
, base_len
);
9811 versioned_name
[base_len
] = '.';
9812 memcpy (versioned_name
+ base_len
+ 1, buf
,
9820 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
9821 versioned_name
, FALSE
);
9822 if (elfsym
->st_name
== (unsigned long) -1)
9826 hash_table
= elf_hash_table (flinfo
->info
);
9827 strtabsize
= hash_table
->strtabsize
;
9828 if (strtabsize
<= hash_table
->strtabcount
)
9830 strtabsize
+= strtabsize
;
9831 hash_table
->strtabsize
= strtabsize
;
9832 strtabsize
*= sizeof (*hash_table
->strtab
);
9834 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
9836 if (hash_table
->strtab
== NULL
)
9839 hash_table
->strtab
[hash_table
->strtabcount
].sym
= *elfsym
;
9840 hash_table
->strtab
[hash_table
->strtabcount
].dest_index
9841 = hash_table
->strtabcount
;
9842 hash_table
->strtab
[hash_table
->strtabcount
].destshndx_index
9843 = flinfo
->symshndxbuf
? bfd_get_symcount (flinfo
->output_bfd
) : 0;
9845 flinfo
->output_bfd
->symcount
+= 1;
9846 hash_table
->strtabcount
+= 1;
9851 /* Swap symbols out to the symbol table and flush the output symbols to
9855 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
9857 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
9860 const struct elf_backend_data
*bed
;
9862 Elf_Internal_Shdr
*hdr
;
9866 if (!hash_table
->strtabcount
)
9869 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9871 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9873 amt
= bed
->s
->sizeof_sym
* hash_table
->strtabcount
;
9874 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
9878 if (flinfo
->symshndxbuf
)
9880 amt
= sizeof (Elf_External_Sym_Shndx
);
9881 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
9882 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
9883 if (flinfo
->symshndxbuf
== NULL
)
9890 /* Now swap out the symbols. */
9891 for (i
= 0; i
< hash_table
->strtabcount
; i
++)
9893 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
9894 if (elfsym
->sym
.st_name
== (unsigned long) -1)
9895 elfsym
->sym
.st_name
= 0;
9898 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
9899 elfsym
->sym
.st_name
);
9901 /* Inform the linker of the addition of this symbol. */
9903 if (flinfo
->info
->callbacks
->ctf_new_symbol
)
9904 flinfo
->info
->callbacks
->ctf_new_symbol (elfsym
->dest_index
,
9907 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
9908 ((bfd_byte
*) symbuf
9909 + (elfsym
->dest_index
9910 * bed
->s
->sizeof_sym
)),
9911 (flinfo
->symshndxbuf
9912 + elfsym
->destshndx_index
));
9915 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
9916 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
9917 amt
= hash_table
->strtabcount
* bed
->s
->sizeof_sym
;
9918 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
9919 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
9921 hdr
->sh_size
+= amt
;
9929 free (hash_table
->strtab
);
9930 hash_table
->strtab
= NULL
;
9935 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9938 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
9940 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
9941 && sym
->st_shndx
< SHN_LORESERVE
)
9943 /* The gABI doesn't support dynamic symbols in output sections
9946 /* xgettext:c-format */
9947 (_("%pB: too many sections: %d (>= %d)"),
9948 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
9949 bfd_set_error (bfd_error_nonrepresentable_section
);
9955 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9956 allowing an unsatisfied unversioned symbol in the DSO to match a
9957 versioned symbol that would normally require an explicit version.
9958 We also handle the case that a DSO references a hidden symbol
9959 which may be satisfied by a versioned symbol in another DSO. */
9962 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
9963 const struct elf_backend_data
*bed
,
9964 struct elf_link_hash_entry
*h
)
9967 struct elf_link_loaded_list
*loaded
;
9969 if (!is_elf_hash_table (info
->hash
))
9972 /* Check indirect symbol. */
9973 while (h
->root
.type
== bfd_link_hash_indirect
)
9974 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9976 switch (h
->root
.type
)
9982 case bfd_link_hash_undefined
:
9983 case bfd_link_hash_undefweak
:
9984 abfd
= h
->root
.u
.undef
.abfd
;
9986 || (abfd
->flags
& DYNAMIC
) == 0
9987 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
9991 case bfd_link_hash_defined
:
9992 case bfd_link_hash_defweak
:
9993 abfd
= h
->root
.u
.def
.section
->owner
;
9996 case bfd_link_hash_common
:
9997 abfd
= h
->root
.u
.c
.p
->section
->owner
;
10000 BFD_ASSERT (abfd
!= NULL
);
10002 for (loaded
= elf_hash_table (info
)->dyn_loaded
;
10004 loaded
= loaded
->next
)
10007 Elf_Internal_Shdr
*hdr
;
10009 size_t extsymcount
;
10011 Elf_Internal_Shdr
*versymhdr
;
10012 Elf_Internal_Sym
*isym
;
10013 Elf_Internal_Sym
*isymend
;
10014 Elf_Internal_Sym
*isymbuf
;
10015 Elf_External_Versym
*ever
;
10016 Elf_External_Versym
*extversym
;
10018 input
= loaded
->abfd
;
10020 /* We check each DSO for a possible hidden versioned definition. */
10022 || elf_dynversym (input
) == 0)
10025 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
10027 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10028 if (elf_bad_symtab (input
))
10030 extsymcount
= symcount
;
10035 extsymcount
= symcount
- hdr
->sh_info
;
10036 extsymoff
= hdr
->sh_info
;
10039 if (extsymcount
== 0)
10042 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
10044 if (isymbuf
== NULL
)
10047 /* Read in any version definitions. */
10048 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
10049 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
10050 || (extversym
= (Elf_External_Versym
*)
10051 _bfd_malloc_and_read (input
, versymhdr
->sh_size
,
10052 versymhdr
->sh_size
)) == NULL
)
10058 ever
= extversym
+ extsymoff
;
10059 isymend
= isymbuf
+ extsymcount
;
10060 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
10063 Elf_Internal_Versym iver
;
10064 unsigned short version_index
;
10066 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
10067 || isym
->st_shndx
== SHN_UNDEF
)
10070 name
= bfd_elf_string_from_elf_section (input
,
10073 if (strcmp (name
, h
->root
.root
.string
) != 0)
10076 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
10078 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
10079 && !(h
->def_regular
10080 && h
->forced_local
))
10082 /* If we have a non-hidden versioned sym, then it should
10083 have provided a definition for the undefined sym unless
10084 it is defined in a non-shared object and forced local.
10089 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
10090 if (version_index
== 1 || version_index
== 2)
10092 /* This is the base or first version. We can use it. */
10106 /* Convert ELF common symbol TYPE. */
10109 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
10111 /* Commom symbol can only appear in relocatable link. */
10112 if (!bfd_link_relocatable (info
))
10114 switch (info
->elf_stt_common
)
10118 case elf_stt_common
:
10121 case no_elf_stt_common
:
10128 /* Add an external symbol to the symbol table. This is called from
10129 the hash table traversal routine. When generating a shared object,
10130 we go through the symbol table twice. The first time we output
10131 anything that might have been forced to local scope in a version
10132 script. The second time we output the symbols that are still
10136 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
10138 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
10139 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
10140 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
10142 Elf_Internal_Sym sym
;
10143 asection
*input_sec
;
10144 const struct elf_backend_data
*bed
;
10149 if (h
->root
.type
== bfd_link_hash_warning
)
10151 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10152 if (h
->root
.type
== bfd_link_hash_new
)
10156 /* Decide whether to output this symbol in this pass. */
10157 if (eoinfo
->localsyms
)
10159 if (!h
->forced_local
)
10164 if (h
->forced_local
)
10168 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10170 if (h
->root
.type
== bfd_link_hash_undefined
)
10172 /* If we have an undefined symbol reference here then it must have
10173 come from a shared library that is being linked in. (Undefined
10174 references in regular files have already been handled unless
10175 they are in unreferenced sections which are removed by garbage
10177 bfd_boolean ignore_undef
= FALSE
;
10179 /* Some symbols may be special in that the fact that they're
10180 undefined can be safely ignored - let backend determine that. */
10181 if (bed
->elf_backend_ignore_undef_symbol
)
10182 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
10184 /* If we are reporting errors for this situation then do so now. */
10186 && h
->ref_dynamic_nonweak
10187 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
10188 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
10189 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
10191 flinfo
->info
->callbacks
->undefined_symbol
10192 (flinfo
->info
, h
->root
.root
.string
,
10193 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
, NULL
, 0,
10194 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_DIAGNOSE
10195 && !flinfo
->info
->warn_unresolved_syms
);
10198 /* Strip a global symbol defined in a discarded section. */
10203 /* We should also warn if a forced local symbol is referenced from
10204 shared libraries. */
10205 if (bfd_link_executable (flinfo
->info
)
10210 && h
->ref_dynamic_nonweak
10211 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
10215 struct elf_link_hash_entry
*hi
= h
;
10217 /* Check indirect symbol. */
10218 while (hi
->root
.type
== bfd_link_hash_indirect
)
10219 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
10221 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
10222 /* xgettext:c-format */
10223 msg
= _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10224 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
10225 /* xgettext:c-format */
10226 msg
= _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10228 /* xgettext:c-format */
10229 msg
= _("%pB: local symbol `%s' in %pB is referenced by DSO");
10230 def_bfd
= flinfo
->output_bfd
;
10231 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
10232 def_bfd
= hi
->root
.u
.def
.section
->owner
;
10233 _bfd_error_handler (msg
, flinfo
->output_bfd
,
10234 h
->root
.root
.string
, def_bfd
);
10235 bfd_set_error (bfd_error_bad_value
);
10236 eoinfo
->failed
= TRUE
;
10240 /* We don't want to output symbols that have never been mentioned by
10241 a regular file, or that we have been told to strip. However, if
10242 h->indx is set to -2, the symbol is used by a reloc and we must
10247 else if ((h
->def_dynamic
10249 || h
->root
.type
== bfd_link_hash_new
)
10251 && !h
->ref_regular
)
10253 else if (flinfo
->info
->strip
== strip_all
)
10255 else if (flinfo
->info
->strip
== strip_some
10256 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
10257 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
10259 else if ((h
->root
.type
== bfd_link_hash_defined
10260 || h
->root
.type
== bfd_link_hash_defweak
)
10261 && ((flinfo
->info
->strip_discarded
10262 && discarded_section (h
->root
.u
.def
.section
))
10263 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
10264 && h
->root
.u
.def
.section
->owner
!= NULL
10265 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
10267 else if ((h
->root
.type
== bfd_link_hash_undefined
10268 || h
->root
.type
== bfd_link_hash_undefweak
)
10269 && h
->root
.u
.undef
.abfd
!= NULL
10270 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
10275 /* If we're stripping it, and it's not a dynamic symbol, there's
10276 nothing else to do. However, if it is a forced local symbol or
10277 an ifunc symbol we need to give the backend finish_dynamic_symbol
10278 function a chance to make it dynamic. */
10280 && h
->dynindx
== -1
10281 && type
!= STT_GNU_IFUNC
10282 && !h
->forced_local
)
10286 sym
.st_size
= h
->size
;
10287 sym
.st_other
= h
->other
;
10288 switch (h
->root
.type
)
10291 case bfd_link_hash_new
:
10292 case bfd_link_hash_warning
:
10296 case bfd_link_hash_undefined
:
10297 case bfd_link_hash_undefweak
:
10298 input_sec
= bfd_und_section_ptr
;
10299 sym
.st_shndx
= SHN_UNDEF
;
10302 case bfd_link_hash_defined
:
10303 case bfd_link_hash_defweak
:
10305 input_sec
= h
->root
.u
.def
.section
;
10306 if (input_sec
->output_section
!= NULL
)
10309 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
10310 input_sec
->output_section
);
10311 if (sym
.st_shndx
== SHN_BAD
)
10314 /* xgettext:c-format */
10315 (_("%pB: could not find output section %pA for input section %pA"),
10316 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
10317 bfd_set_error (bfd_error_nonrepresentable_section
);
10318 eoinfo
->failed
= TRUE
;
10322 /* ELF symbols in relocatable files are section relative,
10323 but in nonrelocatable files they are virtual
10325 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
10326 if (!bfd_link_relocatable (flinfo
->info
))
10328 sym
.st_value
+= input_sec
->output_section
->vma
;
10329 if (h
->type
== STT_TLS
)
10331 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
10332 if (tls_sec
!= NULL
)
10333 sym
.st_value
-= tls_sec
->vma
;
10339 BFD_ASSERT (input_sec
->owner
== NULL
10340 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
10341 sym
.st_shndx
= SHN_UNDEF
;
10342 input_sec
= bfd_und_section_ptr
;
10347 case bfd_link_hash_common
:
10348 input_sec
= h
->root
.u
.c
.p
->section
;
10349 sym
.st_shndx
= bed
->common_section_index (input_sec
);
10350 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
10353 case bfd_link_hash_indirect
:
10354 /* These symbols are created by symbol versioning. They point
10355 to the decorated version of the name. For example, if the
10356 symbol foo@@GNU_1.2 is the default, which should be used when
10357 foo is used with no version, then we add an indirect symbol
10358 foo which points to foo@@GNU_1.2. We ignore these symbols,
10359 since the indirected symbol is already in the hash table. */
10363 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
10364 switch (h
->root
.type
)
10366 case bfd_link_hash_common
:
10367 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10369 case bfd_link_hash_defined
:
10370 case bfd_link_hash_defweak
:
10371 if (bed
->common_definition (&sym
))
10372 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10376 case bfd_link_hash_undefined
:
10377 case bfd_link_hash_undefweak
:
10383 if (h
->forced_local
)
10385 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
10386 /* Turn off visibility on local symbol. */
10387 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
10389 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10390 else if (h
->unique_global
&& h
->def_regular
)
10391 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
10392 else if (h
->root
.type
== bfd_link_hash_undefweak
10393 || h
->root
.type
== bfd_link_hash_defweak
)
10394 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
10396 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
10397 sym
.st_target_internal
= h
->target_internal
;
10399 /* Give the processor backend a chance to tweak the symbol value,
10400 and also to finish up anything that needs to be done for this
10401 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10402 forced local syms when non-shared is due to a historical quirk.
10403 STT_GNU_IFUNC symbol must go through PLT. */
10404 if ((h
->type
== STT_GNU_IFUNC
10406 && !bfd_link_relocatable (flinfo
->info
))
10407 || ((h
->dynindx
!= -1
10408 || h
->forced_local
)
10409 && ((bfd_link_pic (flinfo
->info
)
10410 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
10411 || h
->root
.type
!= bfd_link_hash_undefweak
))
10412 || !h
->forced_local
)
10413 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
10415 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
10416 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
10418 eoinfo
->failed
= TRUE
;
10423 /* If we are marking the symbol as undefined, and there are no
10424 non-weak references to this symbol from a regular object, then
10425 mark the symbol as weak undefined; if there are non-weak
10426 references, mark the symbol as strong. We can't do this earlier,
10427 because it might not be marked as undefined until the
10428 finish_dynamic_symbol routine gets through with it. */
10429 if (sym
.st_shndx
== SHN_UNDEF
10431 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
10432 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
10435 type
= ELF_ST_TYPE (sym
.st_info
);
10437 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10438 if (type
== STT_GNU_IFUNC
)
10441 if (h
->ref_regular_nonweak
)
10442 bindtype
= STB_GLOBAL
;
10444 bindtype
= STB_WEAK
;
10445 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
10448 /* If this is a symbol defined in a dynamic library, don't use the
10449 symbol size from the dynamic library. Relinking an executable
10450 against a new library may introduce gratuitous changes in the
10451 executable's symbols if we keep the size. */
10452 if (sym
.st_shndx
== SHN_UNDEF
10457 /* If a non-weak symbol with non-default visibility is not defined
10458 locally, it is a fatal error. */
10459 if (!bfd_link_relocatable (flinfo
->info
)
10460 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
10461 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
10462 && h
->root
.type
== bfd_link_hash_undefined
10463 && !h
->def_regular
)
10467 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
10468 /* xgettext:c-format */
10469 msg
= _("%pB: protected symbol `%s' isn't defined");
10470 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
10471 /* xgettext:c-format */
10472 msg
= _("%pB: internal symbol `%s' isn't defined");
10474 /* xgettext:c-format */
10475 msg
= _("%pB: hidden symbol `%s' isn't defined");
10476 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
10477 bfd_set_error (bfd_error_bad_value
);
10478 eoinfo
->failed
= TRUE
;
10482 /* If this symbol should be put in the .dynsym section, then put it
10483 there now. We already know the symbol index. We also fill in
10484 the entry in the .hash section. */
10485 if (h
->dynindx
!= -1
10486 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
10487 && elf_hash_table (flinfo
->info
)->dynsym
!= NULL
10488 && !discarded_section (elf_hash_table (flinfo
->info
)->dynsym
))
10492 /* Since there is no version information in the dynamic string,
10493 if there is no version info in symbol version section, we will
10494 have a run-time problem if not linking executable, referenced
10495 by shared library, or not bound locally. */
10496 if (h
->verinfo
.verdef
== NULL
10497 && (!bfd_link_executable (flinfo
->info
)
10499 || !h
->def_regular
))
10501 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
10503 if (p
&& p
[1] != '\0')
10506 /* xgettext:c-format */
10507 (_("%pB: no symbol version section for versioned symbol `%s'"),
10508 flinfo
->output_bfd
, h
->root
.root
.string
);
10509 eoinfo
->failed
= TRUE
;
10514 sym
.st_name
= h
->dynstr_index
;
10515 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
10516 + h
->dynindx
* bed
->s
->sizeof_sym
);
10517 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
10519 eoinfo
->failed
= TRUE
;
10523 /* Inform the linker of the addition of this symbol. */
10525 if (flinfo
->info
->callbacks
->ctf_new_dynsym
)
10526 flinfo
->info
->callbacks
->ctf_new_dynsym (h
->dynindx
, &sym
);
10528 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
10530 if (flinfo
->hash_sec
!= NULL
)
10532 size_t hash_entry_size
;
10533 bfd_byte
*bucketpos
;
10535 size_t bucketcount
;
10538 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
10539 bucket
= h
->u
.elf_hash_value
% bucketcount
;
10542 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
10543 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
10544 + (bucket
+ 2) * hash_entry_size
);
10545 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
10546 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
10548 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
10549 ((bfd_byte
*) flinfo
->hash_sec
->contents
10550 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
10553 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
10555 Elf_Internal_Versym iversym
;
10556 Elf_External_Versym
*eversym
;
10558 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
10560 if (h
->verinfo
.verdef
== NULL
10561 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
10562 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
10563 iversym
.vs_vers
= 0;
10565 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
10569 if (h
->verinfo
.vertree
== NULL
)
10570 iversym
.vs_vers
= 1;
10572 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
10573 if (flinfo
->info
->create_default_symver
)
10577 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10578 defined locally. */
10579 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
10580 iversym
.vs_vers
|= VERSYM_HIDDEN
;
10582 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
10583 eversym
+= h
->dynindx
;
10584 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
10588 /* If the symbol is undefined, and we didn't output it to .dynsym,
10589 strip it from .symtab too. Obviously we can't do this for
10590 relocatable output or when needed for --emit-relocs. */
10591 else if (input_sec
== bfd_und_section_ptr
10593 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10594 && (h
->mark
!= 1 || ELF_ST_BIND (sym
.st_info
) != STB_GLOBAL
)
10595 && !bfd_link_relocatable (flinfo
->info
))
10598 /* Also strip others that we couldn't earlier due to dynamic symbol
10602 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
10605 /* Output a FILE symbol so that following locals are not associated
10606 with the wrong input file. We need one for forced local symbols
10607 if we've seen more than one FILE symbol or when we have exactly
10608 one FILE symbol but global symbols are present in a file other
10609 than the one with the FILE symbol. We also need one if linker
10610 defined symbols are present. In practice these conditions are
10611 always met, so just emit the FILE symbol unconditionally. */
10612 if (eoinfo
->localsyms
10613 && !eoinfo
->file_sym_done
10614 && eoinfo
->flinfo
->filesym_count
!= 0)
10616 Elf_Internal_Sym fsym
;
10618 memset (&fsym
, 0, sizeof (fsym
));
10619 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10620 fsym
.st_shndx
= SHN_ABS
;
10621 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
10622 bfd_und_section_ptr
, NULL
))
10625 eoinfo
->file_sym_done
= TRUE
;
10628 indx
= bfd_get_symcount (flinfo
->output_bfd
);
10629 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
10633 eoinfo
->failed
= TRUE
;
10638 else if (h
->indx
== -2)
10644 /* Return TRUE if special handling is done for relocs in SEC against
10645 symbols defined in discarded sections. */
10648 elf_section_ignore_discarded_relocs (asection
*sec
)
10650 const struct elf_backend_data
*bed
;
10652 switch (sec
->sec_info_type
)
10654 case SEC_INFO_TYPE_STABS
:
10655 case SEC_INFO_TYPE_EH_FRAME
:
10656 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10662 bed
= get_elf_backend_data (sec
->owner
);
10663 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
10664 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
10670 /* Return a mask saying how ld should treat relocations in SEC against
10671 symbols defined in discarded sections. If this function returns
10672 COMPLAIN set, ld will issue a warning message. If this function
10673 returns PRETEND set, and the discarded section was link-once and the
10674 same size as the kept link-once section, ld will pretend that the
10675 symbol was actually defined in the kept section. Otherwise ld will
10676 zero the reloc (at least that is the intent, but some cooperation by
10677 the target dependent code is needed, particularly for REL targets). */
10680 _bfd_elf_default_action_discarded (asection
*sec
)
10682 if (sec
->flags
& SEC_DEBUGGING
)
10685 if (strcmp (".eh_frame", sec
->name
) == 0)
10688 if (strcmp (".gcc_except_table", sec
->name
) == 0)
10691 return COMPLAIN
| PRETEND
;
10694 /* Find a match between a section and a member of a section group. */
10697 match_group_member (asection
*sec
, asection
*group
,
10698 struct bfd_link_info
*info
)
10700 asection
*first
= elf_next_in_group (group
);
10701 asection
*s
= first
;
10705 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
10708 s
= elf_next_in_group (s
);
10716 /* Check if the kept section of a discarded section SEC can be used
10717 to replace it. Return the replacement if it is OK. Otherwise return
10721 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
10725 kept
= sec
->kept_section
;
10728 if ((kept
->flags
& SEC_GROUP
) != 0)
10729 kept
= match_group_member (sec
, kept
, info
);
10732 if ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
10733 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
))
10737 /* Get the real kept section. */
10739 for (next
= kept
->kept_section
;
10741 next
= next
->kept_section
)
10745 sec
->kept_section
= kept
;
10750 /* Link an input file into the linker output file. This function
10751 handles all the sections and relocations of the input file at once.
10752 This is so that we only have to read the local symbols once, and
10753 don't have to keep them in memory. */
10756 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
10758 int (*relocate_section
)
10759 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
10760 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
10762 Elf_Internal_Shdr
*symtab_hdr
;
10763 size_t locsymcount
;
10765 Elf_Internal_Sym
*isymbuf
;
10766 Elf_Internal_Sym
*isym
;
10767 Elf_Internal_Sym
*isymend
;
10769 asection
**ppsection
;
10771 const struct elf_backend_data
*bed
;
10772 struct elf_link_hash_entry
**sym_hashes
;
10773 bfd_size_type address_size
;
10774 bfd_vma r_type_mask
;
10776 bfd_boolean have_file_sym
= FALSE
;
10778 output_bfd
= flinfo
->output_bfd
;
10779 bed
= get_elf_backend_data (output_bfd
);
10780 relocate_section
= bed
->elf_backend_relocate_section
;
10782 /* If this is a dynamic object, we don't want to do anything here:
10783 we don't want the local symbols, and we don't want the section
10785 if ((input_bfd
->flags
& DYNAMIC
) != 0)
10788 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
10789 if (elf_bad_symtab (input_bfd
))
10791 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10796 locsymcount
= symtab_hdr
->sh_info
;
10797 extsymoff
= symtab_hdr
->sh_info
;
10800 /* Enable GNU OSABI features in the output BFD that are used in the input
10802 if (bed
->elf_osabi
== ELFOSABI_NONE
10803 || bed
->elf_osabi
== ELFOSABI_GNU
10804 || bed
->elf_osabi
== ELFOSABI_FREEBSD
)
10805 elf_tdata (output_bfd
)->has_gnu_osabi
10806 |= elf_tdata (input_bfd
)->has_gnu_osabi
;
10808 /* Read the local symbols. */
10809 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10810 if (isymbuf
== NULL
&& locsymcount
!= 0)
10812 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
10813 flinfo
->internal_syms
,
10814 flinfo
->external_syms
,
10815 flinfo
->locsym_shndx
);
10816 if (isymbuf
== NULL
)
10820 /* Find local symbol sections and adjust values of symbols in
10821 SEC_MERGE sections. Write out those local symbols we know are
10822 going into the output file. */
10823 isymend
= isymbuf
+ locsymcount
;
10824 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
10826 isym
++, pindex
++, ppsection
++)
10830 Elf_Internal_Sym osym
;
10836 if (elf_bad_symtab (input_bfd
))
10838 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
10845 if (isym
->st_shndx
== SHN_UNDEF
)
10846 isec
= bfd_und_section_ptr
;
10847 else if (isym
->st_shndx
== SHN_ABS
)
10848 isec
= bfd_abs_section_ptr
;
10849 else if (isym
->st_shndx
== SHN_COMMON
)
10850 isec
= bfd_com_section_ptr
;
10853 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
10856 /* Don't attempt to output symbols with st_shnx in the
10857 reserved range other than SHN_ABS and SHN_COMMON. */
10858 isec
= bfd_und_section_ptr
;
10860 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
10861 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
10863 _bfd_merged_section_offset (output_bfd
, &isec
,
10864 elf_section_data (isec
)->sec_info
,
10870 /* Don't output the first, undefined, symbol. In fact, don't
10871 output any undefined local symbol. */
10872 if (isec
== bfd_und_section_ptr
)
10875 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
10877 /* We never output section symbols. Instead, we use the
10878 section symbol of the corresponding section in the output
10883 /* If we are stripping all symbols, we don't want to output this
10885 if (flinfo
->info
->strip
== strip_all
)
10888 /* If we are discarding all local symbols, we don't want to
10889 output this one. If we are generating a relocatable output
10890 file, then some of the local symbols may be required by
10891 relocs; we output them below as we discover that they are
10893 if (flinfo
->info
->discard
== discard_all
)
10896 /* If this symbol is defined in a section which we are
10897 discarding, we don't need to keep it. */
10898 if (isym
->st_shndx
!= SHN_UNDEF
10899 && isym
->st_shndx
< SHN_LORESERVE
10900 && isec
->output_section
== NULL
10901 && flinfo
->info
->non_contiguous_regions
10902 && flinfo
->info
->non_contiguous_regions_warnings
)
10904 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10905 "discards section `%s' from '%s'\n"),
10906 isec
->name
, bfd_get_filename (isec
->owner
));
10910 if (isym
->st_shndx
!= SHN_UNDEF
10911 && isym
->st_shndx
< SHN_LORESERVE
10912 && bfd_section_removed_from_list (output_bfd
,
10913 isec
->output_section
))
10916 /* Get the name of the symbol. */
10917 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
10922 /* See if we are discarding symbols with this name. */
10923 if ((flinfo
->info
->strip
== strip_some
10924 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
10926 || (((flinfo
->info
->discard
== discard_sec_merge
10927 && (isec
->flags
& SEC_MERGE
)
10928 && !bfd_link_relocatable (flinfo
->info
))
10929 || flinfo
->info
->discard
== discard_l
)
10930 && bfd_is_local_label_name (input_bfd
, name
)))
10933 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
10935 if (input_bfd
->lto_output
)
10936 /* -flto puts a temp file name here. This means builds
10937 are not reproducible. Discard the symbol. */
10939 have_file_sym
= TRUE
;
10940 flinfo
->filesym_count
+= 1;
10942 if (!have_file_sym
)
10944 /* In the absence of debug info, bfd_find_nearest_line uses
10945 FILE symbols to determine the source file for local
10946 function symbols. Provide a FILE symbol here if input
10947 files lack such, so that their symbols won't be
10948 associated with a previous input file. It's not the
10949 source file, but the best we can do. */
10950 have_file_sym
= TRUE
;
10951 flinfo
->filesym_count
+= 1;
10952 memset (&osym
, 0, sizeof (osym
));
10953 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10954 osym
.st_shndx
= SHN_ABS
;
10955 if (!elf_link_output_symstrtab (flinfo
,
10956 (input_bfd
->lto_output
? NULL
10957 : bfd_get_filename (input_bfd
)),
10958 &osym
, bfd_abs_section_ptr
,
10965 /* Adjust the section index for the output file. */
10966 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10967 isec
->output_section
);
10968 if (osym
.st_shndx
== SHN_BAD
)
10971 /* ELF symbols in relocatable files are section relative, but
10972 in executable files they are virtual addresses. Note that
10973 this code assumes that all ELF sections have an associated
10974 BFD section with a reasonable value for output_offset; below
10975 we assume that they also have a reasonable value for
10976 output_section. Any special sections must be set up to meet
10977 these requirements. */
10978 osym
.st_value
+= isec
->output_offset
;
10979 if (!bfd_link_relocatable (flinfo
->info
))
10981 osym
.st_value
+= isec
->output_section
->vma
;
10982 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
10984 /* STT_TLS symbols are relative to PT_TLS segment base. */
10985 if (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
)
10986 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
10988 osym
.st_info
= ELF_ST_INFO (ELF_ST_BIND (osym
.st_info
),
10993 indx
= bfd_get_symcount (output_bfd
);
10994 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
11001 if (bed
->s
->arch_size
== 32)
11003 r_type_mask
= 0xff;
11009 r_type_mask
= 0xffffffff;
11014 /* Relocate the contents of each section. */
11015 sym_hashes
= elf_sym_hashes (input_bfd
);
11016 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
11018 bfd_byte
*contents
;
11020 if (! o
->linker_mark
)
11022 /* This section was omitted from the link. */
11026 if (!flinfo
->info
->resolve_section_groups
11027 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
11029 /* Deal with the group signature symbol. */
11030 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
11031 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
11032 asection
*osec
= o
->output_section
;
11034 BFD_ASSERT (bfd_link_relocatable (flinfo
->info
));
11035 if (symndx
>= locsymcount
11036 || (elf_bad_symtab (input_bfd
)
11037 && flinfo
->sections
[symndx
] == NULL
))
11039 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
11040 while (h
->root
.type
== bfd_link_hash_indirect
11041 || h
->root
.type
== bfd_link_hash_warning
)
11042 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11043 /* Arrange for symbol to be output. */
11045 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
11047 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
11049 /* We'll use the output section target_index. */
11050 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11051 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
11055 if (flinfo
->indices
[symndx
] == -1)
11057 /* Otherwise output the local symbol now. */
11058 Elf_Internal_Sym sym
= isymbuf
[symndx
];
11059 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11064 name
= bfd_elf_string_from_elf_section (input_bfd
,
11065 symtab_hdr
->sh_link
,
11070 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11072 if (sym
.st_shndx
== SHN_BAD
)
11075 sym
.st_value
+= o
->output_offset
;
11077 indx
= bfd_get_symcount (output_bfd
);
11078 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
11083 flinfo
->indices
[symndx
] = indx
;
11087 elf_section_data (osec
)->this_hdr
.sh_info
11088 = flinfo
->indices
[symndx
];
11092 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
11093 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
11096 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
11098 /* Section was created by _bfd_elf_link_create_dynamic_sections
11103 /* Get the contents of the section. They have been cached by a
11104 relaxation routine. Note that o is a section in an input
11105 file, so the contents field will not have been set by any of
11106 the routines which work on output files. */
11107 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
11109 contents
= elf_section_data (o
)->this_hdr
.contents
;
11110 if (bed
->caches_rawsize
11112 && o
->rawsize
< o
->size
)
11114 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
11115 contents
= flinfo
->contents
;
11120 contents
= flinfo
->contents
;
11121 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
11125 if ((o
->flags
& SEC_RELOC
) != 0)
11127 Elf_Internal_Rela
*internal_relocs
;
11128 Elf_Internal_Rela
*rel
, *relend
;
11129 int action_discarded
;
11132 /* Get the swapped relocs. */
11134 = _bfd_elf_link_read_relocs (input_bfd
, o
, flinfo
->external_relocs
,
11135 flinfo
->internal_relocs
, FALSE
);
11136 if (internal_relocs
== NULL
11137 && o
->reloc_count
> 0)
11140 /* We need to reverse-copy input .ctors/.dtors sections if
11141 they are placed in .init_array/.finit_array for output. */
11142 if (o
->size
> address_size
11143 && ((strncmp (o
->name
, ".ctors", 6) == 0
11144 && strcmp (o
->output_section
->name
,
11145 ".init_array") == 0)
11146 || (strncmp (o
->name
, ".dtors", 6) == 0
11147 && strcmp (o
->output_section
->name
,
11148 ".fini_array") == 0))
11149 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
11151 if (o
->size
* bed
->s
->int_rels_per_ext_rel
11152 != o
->reloc_count
* address_size
)
11155 /* xgettext:c-format */
11156 (_("error: %pB: size of section %pA is not "
11157 "multiple of address size"),
11159 bfd_set_error (bfd_error_bad_value
);
11162 o
->flags
|= SEC_ELF_REVERSE_COPY
;
11165 action_discarded
= -1;
11166 if (!elf_section_ignore_discarded_relocs (o
))
11167 action_discarded
= (*bed
->action_discarded
) (o
);
11169 /* Run through the relocs evaluating complex reloc symbols and
11170 looking for relocs against symbols from discarded sections
11171 or section symbols from removed link-once sections.
11172 Complain about relocs against discarded sections. Zero
11173 relocs against removed link-once sections. */
11175 rel
= internal_relocs
;
11176 relend
= rel
+ o
->reloc_count
;
11177 for ( ; rel
< relend
; rel
++)
11179 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
11180 unsigned int s_type
;
11181 asection
**ps
, *sec
;
11182 struct elf_link_hash_entry
*h
= NULL
;
11183 const char *sym_name
;
11185 if (r_symndx
== STN_UNDEF
)
11188 if (r_symndx
>= locsymcount
11189 || (elf_bad_symtab (input_bfd
)
11190 && flinfo
->sections
[r_symndx
] == NULL
))
11192 h
= sym_hashes
[r_symndx
- extsymoff
];
11194 /* Badly formatted input files can contain relocs that
11195 reference non-existant symbols. Check here so that
11196 we do not seg fault. */
11200 /* xgettext:c-format */
11201 (_("error: %pB contains a reloc (%#" PRIx64
") for section %pA "
11202 "that references a non-existent global symbol"),
11203 input_bfd
, (uint64_t) rel
->r_info
, o
);
11204 bfd_set_error (bfd_error_bad_value
);
11208 while (h
->root
.type
== bfd_link_hash_indirect
11209 || h
->root
.type
== bfd_link_hash_warning
)
11210 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11214 /* If a plugin symbol is referenced from a non-IR file,
11215 mark the symbol as undefined. Note that the
11216 linker may attach linker created dynamic sections
11217 to the plugin bfd. Symbols defined in linker
11218 created sections are not plugin symbols. */
11219 if ((h
->root
.non_ir_ref_regular
11220 || h
->root
.non_ir_ref_dynamic
)
11221 && (h
->root
.type
== bfd_link_hash_defined
11222 || h
->root
.type
== bfd_link_hash_defweak
)
11223 && (h
->root
.u
.def
.section
->flags
11224 & SEC_LINKER_CREATED
) == 0
11225 && h
->root
.u
.def
.section
->owner
!= NULL
11226 && (h
->root
.u
.def
.section
->owner
->flags
11227 & BFD_PLUGIN
) != 0)
11229 h
->root
.type
= bfd_link_hash_undefined
;
11230 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
11234 if (h
->root
.type
== bfd_link_hash_defined
11235 || h
->root
.type
== bfd_link_hash_defweak
)
11236 ps
= &h
->root
.u
.def
.section
;
11238 sym_name
= h
->root
.root
.string
;
11242 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
11244 s_type
= ELF_ST_TYPE (sym
->st_info
);
11245 ps
= &flinfo
->sections
[r_symndx
];
11246 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
11250 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
11251 && !bfd_link_relocatable (flinfo
->info
))
11254 bfd_vma dot
= (rel
->r_offset
11255 + o
->output_offset
+ o
->output_section
->vma
);
11257 printf ("Encountered a complex symbol!");
11258 printf (" (input_bfd %s, section %s, reloc %ld\n",
11259 bfd_get_filename (input_bfd
), o
->name
,
11260 (long) (rel
- internal_relocs
));
11261 printf (" symbol: idx %8.8lx, name %s\n",
11262 r_symndx
, sym_name
);
11263 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11264 (unsigned long) rel
->r_info
,
11265 (unsigned long) rel
->r_offset
);
11267 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
11268 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
11271 /* Symbol evaluated OK. Update to absolute value. */
11272 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
11277 if (action_discarded
!= -1 && ps
!= NULL
)
11279 /* Complain if the definition comes from a
11280 discarded section. */
11281 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
11283 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
11284 if (action_discarded
& COMPLAIN
)
11285 (*flinfo
->info
->callbacks
->einfo
)
11286 /* xgettext:c-format */
11287 (_("%X`%s' referenced in section `%pA' of %pB: "
11288 "defined in discarded section `%pA' of %pB\n"),
11289 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
11291 /* Try to do the best we can to support buggy old
11292 versions of gcc. Pretend that the symbol is
11293 really defined in the kept linkonce section.
11294 FIXME: This is quite broken. Modifying the
11295 symbol here means we will be changing all later
11296 uses of the symbol, not just in this section. */
11297 if (action_discarded
& PRETEND
)
11301 kept
= _bfd_elf_check_kept_section (sec
,
11313 /* Relocate the section by invoking a back end routine.
11315 The back end routine is responsible for adjusting the
11316 section contents as necessary, and (if using Rela relocs
11317 and generating a relocatable output file) adjusting the
11318 reloc addend as necessary.
11320 The back end routine does not have to worry about setting
11321 the reloc address or the reloc symbol index.
11323 The back end routine is given a pointer to the swapped in
11324 internal symbols, and can access the hash table entries
11325 for the external symbols via elf_sym_hashes (input_bfd).
11327 When generating relocatable output, the back end routine
11328 must handle STB_LOCAL/STT_SECTION symbols specially. The
11329 output symbol is going to be a section symbol
11330 corresponding to the output section, which will require
11331 the addend to be adjusted. */
11333 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
11334 input_bfd
, o
, contents
,
11342 || bfd_link_relocatable (flinfo
->info
)
11343 || flinfo
->info
->emitrelocations
)
11345 Elf_Internal_Rela
*irela
;
11346 Elf_Internal_Rela
*irelaend
, *irelamid
;
11347 bfd_vma last_offset
;
11348 struct elf_link_hash_entry
**rel_hash
;
11349 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
11350 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
11351 unsigned int next_erel
;
11352 bfd_boolean rela_normal
;
11353 struct bfd_elf_section_data
*esdi
, *esdo
;
11355 esdi
= elf_section_data (o
);
11356 esdo
= elf_section_data (o
->output_section
);
11357 rela_normal
= FALSE
;
11359 /* Adjust the reloc addresses and symbol indices. */
11361 irela
= internal_relocs
;
11362 irelaend
= irela
+ o
->reloc_count
;
11363 rel_hash
= esdo
->rel
.hashes
+ esdo
->rel
.count
;
11364 /* We start processing the REL relocs, if any. When we reach
11365 IRELAMID in the loop, we switch to the RELA relocs. */
11367 if (esdi
->rel
.hdr
!= NULL
)
11368 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
11369 * bed
->s
->int_rels_per_ext_rel
);
11370 rel_hash_list
= rel_hash
;
11371 rela_hash_list
= NULL
;
11372 last_offset
= o
->output_offset
;
11373 if (!bfd_link_relocatable (flinfo
->info
))
11374 last_offset
+= o
->output_section
->vma
;
11375 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
11377 unsigned long r_symndx
;
11379 Elf_Internal_Sym sym
;
11381 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
11387 if (irela
== irelamid
)
11389 rel_hash
= esdo
->rela
.hashes
+ esdo
->rela
.count
;
11390 rela_hash_list
= rel_hash
;
11391 rela_normal
= bed
->rela_normal
;
11394 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
11397 if (irela
->r_offset
>= (bfd_vma
) -2)
11399 /* This is a reloc for a deleted entry or somesuch.
11400 Turn it into an R_*_NONE reloc, at the same
11401 offset as the last reloc. elf_eh_frame.c and
11402 bfd_elf_discard_info rely on reloc offsets
11404 irela
->r_offset
= last_offset
;
11406 irela
->r_addend
= 0;
11410 irela
->r_offset
+= o
->output_offset
;
11412 /* Relocs in an executable have to be virtual addresses. */
11413 if (!bfd_link_relocatable (flinfo
->info
))
11414 irela
->r_offset
+= o
->output_section
->vma
;
11416 last_offset
= irela
->r_offset
;
11418 r_symndx
= irela
->r_info
>> r_sym_shift
;
11419 if (r_symndx
== STN_UNDEF
)
11422 if (r_symndx
>= locsymcount
11423 || (elf_bad_symtab (input_bfd
)
11424 && flinfo
->sections
[r_symndx
] == NULL
))
11426 struct elf_link_hash_entry
*rh
;
11427 unsigned long indx
;
11429 /* This is a reloc against a global symbol. We
11430 have not yet output all the local symbols, so
11431 we do not know the symbol index of any global
11432 symbol. We set the rel_hash entry for this
11433 reloc to point to the global hash table entry
11434 for this symbol. The symbol index is then
11435 set at the end of bfd_elf_final_link. */
11436 indx
= r_symndx
- extsymoff
;
11437 rh
= elf_sym_hashes (input_bfd
)[indx
];
11438 while (rh
->root
.type
== bfd_link_hash_indirect
11439 || rh
->root
.type
== bfd_link_hash_warning
)
11440 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
11442 /* Setting the index to -2 tells
11443 elf_link_output_extsym that this symbol is
11444 used by a reloc. */
11445 BFD_ASSERT (rh
->indx
< 0);
11452 /* This is a reloc against a local symbol. */
11455 sym
= isymbuf
[r_symndx
];
11456 sec
= flinfo
->sections
[r_symndx
];
11457 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
11459 /* I suppose the backend ought to fill in the
11460 section of any STT_SECTION symbol against a
11461 processor specific section. */
11462 r_symndx
= STN_UNDEF
;
11463 if (bfd_is_abs_section (sec
))
11465 else if (sec
== NULL
|| sec
->owner
== NULL
)
11467 bfd_set_error (bfd_error_bad_value
);
11472 asection
*osec
= sec
->output_section
;
11474 /* If we have discarded a section, the output
11475 section will be the absolute section. In
11476 case of discarded SEC_MERGE sections, use
11477 the kept section. relocate_section should
11478 have already handled discarded linkonce
11480 if (bfd_is_abs_section (osec
)
11481 && sec
->kept_section
!= NULL
11482 && sec
->kept_section
->output_section
!= NULL
)
11484 osec
= sec
->kept_section
->output_section
;
11485 irela
->r_addend
-= osec
->vma
;
11488 if (!bfd_is_abs_section (osec
))
11490 r_symndx
= osec
->target_index
;
11491 if (r_symndx
== STN_UNDEF
)
11493 irela
->r_addend
+= osec
->vma
;
11494 osec
= _bfd_nearby_section (output_bfd
, osec
,
11496 irela
->r_addend
-= osec
->vma
;
11497 r_symndx
= osec
->target_index
;
11502 /* Adjust the addend according to where the
11503 section winds up in the output section. */
11505 irela
->r_addend
+= sec
->output_offset
;
11509 if (flinfo
->indices
[r_symndx
] == -1)
11511 unsigned long shlink
;
11516 if (flinfo
->info
->strip
== strip_all
)
11518 /* You can't do ld -r -s. */
11519 bfd_set_error (bfd_error_invalid_operation
);
11523 /* This symbol was skipped earlier, but
11524 since it is needed by a reloc, we
11525 must output it now. */
11526 shlink
= symtab_hdr
->sh_link
;
11527 name
= (bfd_elf_string_from_elf_section
11528 (input_bfd
, shlink
, sym
.st_name
));
11532 osec
= sec
->output_section
;
11534 _bfd_elf_section_from_bfd_section (output_bfd
,
11536 if (sym
.st_shndx
== SHN_BAD
)
11539 sym
.st_value
+= sec
->output_offset
;
11540 if (!bfd_link_relocatable (flinfo
->info
))
11542 sym
.st_value
+= osec
->vma
;
11543 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
11545 struct elf_link_hash_table
*htab
11546 = elf_hash_table (flinfo
->info
);
11548 /* STT_TLS symbols are relative to PT_TLS
11550 if (htab
->tls_sec
!= NULL
)
11551 sym
.st_value
-= htab
->tls_sec
->vma
;
11554 = ELF_ST_INFO (ELF_ST_BIND (sym
.st_info
),
11559 indx
= bfd_get_symcount (output_bfd
);
11560 ret
= elf_link_output_symstrtab (flinfo
, name
,
11566 flinfo
->indices
[r_symndx
] = indx
;
11571 r_symndx
= flinfo
->indices
[r_symndx
];
11574 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
11575 | (irela
->r_info
& r_type_mask
));
11578 /* Swap out the relocs. */
11579 input_rel_hdr
= esdi
->rel
.hdr
;
11580 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
11582 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11587 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
11588 * bed
->s
->int_rels_per_ext_rel
);
11589 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
11592 input_rela_hdr
= esdi
->rela
.hdr
;
11593 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
11595 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11604 /* Write out the modified section contents. */
11605 if (bed
->elf_backend_write_section
11606 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
11609 /* Section written out. */
11611 else switch (o
->sec_info_type
)
11613 case SEC_INFO_TYPE_STABS
:
11614 if (! (_bfd_write_section_stabs
11616 &elf_hash_table (flinfo
->info
)->stab_info
,
11617 o
, &elf_section_data (o
)->sec_info
, contents
)))
11620 case SEC_INFO_TYPE_MERGE
:
11621 if (! _bfd_write_merged_section (output_bfd
, o
,
11622 elf_section_data (o
)->sec_info
))
11625 case SEC_INFO_TYPE_EH_FRAME
:
11627 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
11632 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
11634 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
11642 if (! (o
->flags
& SEC_EXCLUDE
))
11644 file_ptr offset
= (file_ptr
) o
->output_offset
;
11645 bfd_size_type todo
= o
->size
;
11647 offset
*= bfd_octets_per_byte (output_bfd
, o
);
11649 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
11651 /* Reverse-copy input section to output. */
11654 todo
-= address_size
;
11655 if (! bfd_set_section_contents (output_bfd
,
11663 offset
+= address_size
;
11667 else if (! bfd_set_section_contents (output_bfd
,
11681 /* Generate a reloc when linking an ELF file. This is a reloc
11682 requested by the linker, and does not come from any input file. This
11683 is used to build constructor and destructor tables when linking
11687 elf_reloc_link_order (bfd
*output_bfd
,
11688 struct bfd_link_info
*info
,
11689 asection
*output_section
,
11690 struct bfd_link_order
*link_order
)
11692 reloc_howto_type
*howto
;
11696 struct bfd_elf_section_reloc_data
*reldata
;
11697 struct elf_link_hash_entry
**rel_hash_ptr
;
11698 Elf_Internal_Shdr
*rel_hdr
;
11699 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
11700 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
11703 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
11705 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
11708 bfd_set_error (bfd_error_bad_value
);
11712 addend
= link_order
->u
.reloc
.p
->addend
;
11715 reldata
= &esdo
->rel
;
11716 else if (esdo
->rela
.hdr
)
11717 reldata
= &esdo
->rela
;
11724 /* Figure out the symbol index. */
11725 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
11726 if (link_order
->type
== bfd_section_reloc_link_order
)
11728 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
11729 BFD_ASSERT (indx
!= 0);
11730 *rel_hash_ptr
= NULL
;
11734 struct elf_link_hash_entry
*h
;
11736 /* Treat a reloc against a defined symbol as though it were
11737 actually against the section. */
11738 h
= ((struct elf_link_hash_entry
*)
11739 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
11740 link_order
->u
.reloc
.p
->u
.name
,
11741 FALSE
, FALSE
, TRUE
));
11743 && (h
->root
.type
== bfd_link_hash_defined
11744 || h
->root
.type
== bfd_link_hash_defweak
))
11748 section
= h
->root
.u
.def
.section
;
11749 indx
= section
->output_section
->target_index
;
11750 *rel_hash_ptr
= NULL
;
11751 /* It seems that we ought to add the symbol value to the
11752 addend here, but in practice it has already been added
11753 because it was passed to constructor_callback. */
11754 addend
+= section
->output_section
->vma
+ section
->output_offset
;
11756 else if (h
!= NULL
)
11758 /* Setting the index to -2 tells elf_link_output_extsym that
11759 this symbol is used by a reloc. */
11766 (*info
->callbacks
->unattached_reloc
)
11767 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
11772 /* If this is an inplace reloc, we must write the addend into the
11774 if (howto
->partial_inplace
&& addend
!= 0)
11776 bfd_size_type size
;
11777 bfd_reloc_status_type rstat
;
11780 const char *sym_name
;
11781 bfd_size_type octets
;
11783 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
11784 buf
= (bfd_byte
*) bfd_zmalloc (size
);
11785 if (buf
== NULL
&& size
!= 0)
11787 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
11794 case bfd_reloc_outofrange
:
11797 case bfd_reloc_overflow
:
11798 if (link_order
->type
== bfd_section_reloc_link_order
)
11799 sym_name
= bfd_section_name (link_order
->u
.reloc
.p
->u
.section
);
11801 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
11802 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
11803 howto
->name
, addend
, NULL
, NULL
,
11808 octets
= link_order
->offset
* bfd_octets_per_byte (output_bfd
,
11810 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
11817 /* The address of a reloc is relative to the section in a
11818 relocatable file, and is a virtual address in an executable
11820 offset
= link_order
->offset
;
11821 if (! bfd_link_relocatable (info
))
11822 offset
+= output_section
->vma
;
11824 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
11826 irel
[i
].r_offset
= offset
;
11827 irel
[i
].r_info
= 0;
11828 irel
[i
].r_addend
= 0;
11830 if (bed
->s
->arch_size
== 32)
11831 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
11833 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
11835 rel_hdr
= reldata
->hdr
;
11836 erel
= rel_hdr
->contents
;
11837 if (rel_hdr
->sh_type
== SHT_REL
)
11839 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
11840 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
11844 irel
[0].r_addend
= addend
;
11845 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
11846 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
11855 /* Compare two sections based on the locations of the sections they are
11856 linked to. Used by elf_fixup_link_order. */
11859 compare_link_order (const void *a
, const void *b
)
11861 const struct bfd_link_order
*alo
= *(const struct bfd_link_order
**) a
;
11862 const struct bfd_link_order
*blo
= *(const struct bfd_link_order
**) b
;
11863 asection
*asec
= elf_linked_to_section (alo
->u
.indirect
.section
);
11864 asection
*bsec
= elf_linked_to_section (blo
->u
.indirect
.section
);
11865 bfd_vma apos
= asec
->output_section
->lma
+ asec
->output_offset
;
11866 bfd_vma bpos
= bsec
->output_section
->lma
+ bsec
->output_offset
;
11873 /* The only way we should get matching LMAs is when the first of two
11874 sections has zero size. */
11875 if (asec
->size
< bsec
->size
)
11877 if (asec
->size
> bsec
->size
)
11880 /* If they are both zero size then they almost certainly have the same
11881 VMA and thus are not ordered with respect to each other. Test VMA
11882 anyway, and fall back to id to make the result reproducible across
11883 qsort implementations. */
11884 apos
= asec
->output_section
->vma
+ asec
->output_offset
;
11885 bpos
= bsec
->output_section
->vma
+ bsec
->output_offset
;
11891 return asec
->id
- bsec
->id
;
11895 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11896 order as their linked sections. Returns false if this could not be done
11897 because an output section includes both ordered and unordered
11898 sections. Ideally we'd do this in the linker proper. */
11901 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
11903 size_t seen_linkorder
;
11906 struct bfd_link_order
*p
;
11908 struct bfd_link_order
**sections
;
11909 asection
*other_sec
, *linkorder_sec
;
11910 bfd_vma offset
; /* Octets. */
11913 linkorder_sec
= NULL
;
11915 seen_linkorder
= 0;
11916 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11918 if (p
->type
== bfd_indirect_link_order
)
11920 asection
*s
= p
->u
.indirect
.section
;
11922 if ((s
->flags
& SEC_LINKER_CREATED
) == 0
11923 && bfd_get_flavour (sub
) == bfd_target_elf_flavour
11924 && elf_section_data (s
) != NULL
11925 && elf_linked_to_section (s
) != NULL
)
11939 if (seen_other
&& seen_linkorder
)
11941 if (other_sec
&& linkorder_sec
)
11943 /* xgettext:c-format */
11944 (_("%pA has both ordered [`%pA' in %pB] "
11945 "and unordered [`%pA' in %pB] sections"),
11946 o
, linkorder_sec
, linkorder_sec
->owner
,
11947 other_sec
, other_sec
->owner
);
11950 (_("%pA has both ordered and unordered sections"), o
);
11951 bfd_set_error (bfd_error_bad_value
);
11956 if (!seen_linkorder
)
11959 sections
= bfd_malloc (seen_linkorder
* sizeof (*sections
));
11960 if (sections
== NULL
)
11963 seen_linkorder
= 0;
11964 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11965 sections
[seen_linkorder
++] = p
;
11967 /* Sort the input sections in the order of their linked section. */
11968 qsort (sections
, seen_linkorder
, sizeof (*sections
), compare_link_order
);
11970 /* Change the offsets of the sections. */
11972 for (n
= 0; n
< seen_linkorder
; n
++)
11975 asection
*s
= sections
[n
]->u
.indirect
.section
;
11976 unsigned int opb
= bfd_octets_per_byte (abfd
, s
);
11978 mask
= ~(bfd_vma
) 0 << s
->alignment_power
* opb
;
11979 offset
= (offset
+ ~mask
) & mask
;
11980 sections
[n
]->offset
= s
->output_offset
= offset
/ opb
;
11981 offset
+= sections
[n
]->size
;
11988 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11989 Returns TRUE upon success, FALSE otherwise. */
11992 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
11994 bfd_boolean ret
= FALSE
;
11996 const struct elf_backend_data
*bed
;
11998 enum bfd_architecture arch
;
12000 asymbol
**sympp
= NULL
;
12004 elf_symbol_type
*osymbuf
;
12007 implib_bfd
= info
->out_implib_bfd
;
12008 bed
= get_elf_backend_data (abfd
);
12010 if (!bfd_set_format (implib_bfd
, bfd_object
))
12013 /* Use flag from executable but make it a relocatable object. */
12014 flags
= bfd_get_file_flags (abfd
);
12015 flags
&= ~HAS_RELOC
;
12016 if (!bfd_set_start_address (implib_bfd
, 0)
12017 || !bfd_set_file_flags (implib_bfd
, flags
& ~EXEC_P
))
12020 /* Copy architecture of output file to import library file. */
12021 arch
= bfd_get_arch (abfd
);
12022 mach
= bfd_get_mach (abfd
);
12023 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
12024 && (abfd
->target_defaulted
12025 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
12028 /* Get symbol table size. */
12029 symsize
= bfd_get_symtab_upper_bound (abfd
);
12033 /* Read in the symbol table. */
12034 sympp
= (asymbol
**) bfd_malloc (symsize
);
12038 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
12042 /* Allow the BFD backend to copy any private header data it
12043 understands from the output BFD to the import library BFD. */
12044 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
12047 /* Filter symbols to appear in the import library. */
12048 if (bed
->elf_backend_filter_implib_symbols
)
12049 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
12052 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
12055 bfd_set_error (bfd_error_no_symbols
);
12056 _bfd_error_handler (_("%pB: no symbol found for import library"),
12062 /* Make symbols absolute. */
12063 amt
= symcount
* sizeof (*osymbuf
);
12064 osymbuf
= (elf_symbol_type
*) bfd_alloc (implib_bfd
, amt
);
12065 if (osymbuf
== NULL
)
12068 for (src_count
= 0; src_count
< symcount
; src_count
++)
12070 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
12071 sizeof (*osymbuf
));
12072 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
12073 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
12074 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
12075 osymbuf
[src_count
].internal_elf_sym
.st_value
=
12076 osymbuf
[src_count
].symbol
.value
;
12077 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
12080 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
12082 /* Allow the BFD backend to copy any private data it understands
12083 from the output BFD to the import library BFD. This is done last
12084 to permit the routine to look at the filtered symbol table. */
12085 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
12088 if (!bfd_close (implib_bfd
))
12099 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
12103 if (flinfo
->symstrtab
!= NULL
)
12104 _bfd_elf_strtab_free (flinfo
->symstrtab
);
12105 free (flinfo
->contents
);
12106 free (flinfo
->external_relocs
);
12107 free (flinfo
->internal_relocs
);
12108 free (flinfo
->external_syms
);
12109 free (flinfo
->locsym_shndx
);
12110 free (flinfo
->internal_syms
);
12111 free (flinfo
->indices
);
12112 free (flinfo
->sections
);
12113 if (flinfo
->symshndxbuf
!= (Elf_External_Sym_Shndx
*) -1)
12114 free (flinfo
->symshndxbuf
);
12115 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
12117 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12118 free (esdo
->rel
.hashes
);
12119 free (esdo
->rela
.hashes
);
12123 /* Do the final step of an ELF link. */
12126 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
12128 bfd_boolean dynamic
;
12129 bfd_boolean emit_relocs
;
12131 struct elf_final_link_info flinfo
;
12133 struct bfd_link_order
*p
;
12135 bfd_size_type max_contents_size
;
12136 bfd_size_type max_external_reloc_size
;
12137 bfd_size_type max_internal_reloc_count
;
12138 bfd_size_type max_sym_count
;
12139 bfd_size_type max_sym_shndx_count
;
12140 Elf_Internal_Sym elfsym
;
12142 Elf_Internal_Shdr
*symtab_hdr
;
12143 Elf_Internal_Shdr
*symtab_shndx_hdr
;
12144 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12145 struct elf_outext_info eoinfo
;
12146 bfd_boolean merged
;
12147 size_t relativecount
= 0;
12148 asection
*reldyn
= 0;
12150 asection
*attr_section
= NULL
;
12151 bfd_vma attr_size
= 0;
12152 const char *std_attrs_section
;
12153 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
12154 bfd_boolean sections_removed
;
12157 if (!is_elf_hash_table (htab
))
12160 if (bfd_link_pic (info
))
12161 abfd
->flags
|= DYNAMIC
;
12163 dynamic
= htab
->dynamic_sections_created
;
12164 dynobj
= htab
->dynobj
;
12166 emit_relocs
= (bfd_link_relocatable (info
)
12167 || info
->emitrelocations
);
12169 memset (&flinfo
, 0, sizeof (flinfo
));
12170 flinfo
.info
= info
;
12171 flinfo
.output_bfd
= abfd
;
12172 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
12173 if (flinfo
.symstrtab
== NULL
)
12178 flinfo
.hash_sec
= NULL
;
12179 flinfo
.symver_sec
= NULL
;
12183 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
12184 /* Note that dynsym_sec can be NULL (on VMS). */
12185 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
12186 /* Note that it is OK if symver_sec is NULL. */
12189 if (info
->unique_symbol
12190 && !bfd_hash_table_init (&flinfo
.local_hash_table
,
12191 local_hash_newfunc
,
12192 sizeof (struct local_hash_entry
)))
12195 /* The object attributes have been merged. Remove the input
12196 sections from the link, and set the contents of the output
12198 sections_removed
= FALSE
;
12199 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
12200 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12202 bfd_boolean remove_section
= FALSE
;
12204 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
12205 || strcmp (o
->name
, ".gnu.attributes") == 0)
12207 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12209 asection
*input_section
;
12211 if (p
->type
!= bfd_indirect_link_order
)
12213 input_section
= p
->u
.indirect
.section
;
12214 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12215 elf_link_input_bfd ignores this section. */
12216 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
12219 attr_size
= bfd_elf_obj_attr_size (abfd
);
12220 bfd_set_section_size (o
, attr_size
);
12221 /* Skip this section later on. */
12222 o
->map_head
.link_order
= NULL
;
12226 remove_section
= TRUE
;
12228 else if ((o
->flags
& SEC_GROUP
) != 0 && o
->size
== 0)
12230 /* Remove empty group section from linker output. */
12231 remove_section
= TRUE
;
12233 if (remove_section
)
12235 o
->flags
|= SEC_EXCLUDE
;
12236 bfd_section_list_remove (abfd
, o
);
12237 abfd
->section_count
--;
12238 sections_removed
= TRUE
;
12241 if (sections_removed
)
12242 _bfd_fix_excluded_sec_syms (abfd
, info
);
12244 /* Count up the number of relocations we will output for each output
12245 section, so that we know the sizes of the reloc sections. We
12246 also figure out some maximum sizes. */
12247 max_contents_size
= 0;
12248 max_external_reloc_size
= 0;
12249 max_internal_reloc_count
= 0;
12251 max_sym_shndx_count
= 0;
12253 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12255 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12256 o
->reloc_count
= 0;
12258 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12260 unsigned int reloc_count
= 0;
12261 unsigned int additional_reloc_count
= 0;
12262 struct bfd_elf_section_data
*esdi
= NULL
;
12264 if (p
->type
== bfd_section_reloc_link_order
12265 || p
->type
== bfd_symbol_reloc_link_order
)
12267 else if (p
->type
== bfd_indirect_link_order
)
12271 sec
= p
->u
.indirect
.section
;
12273 /* Mark all sections which are to be included in the
12274 link. This will normally be every section. We need
12275 to do this so that we can identify any sections which
12276 the linker has decided to not include. */
12277 sec
->linker_mark
= TRUE
;
12279 if (sec
->flags
& SEC_MERGE
)
12282 if (sec
->rawsize
> max_contents_size
)
12283 max_contents_size
= sec
->rawsize
;
12284 if (sec
->size
> max_contents_size
)
12285 max_contents_size
= sec
->size
;
12287 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
12288 && (sec
->owner
->flags
& DYNAMIC
) == 0)
12292 /* We are interested in just local symbols, not all
12294 if (elf_bad_symtab (sec
->owner
))
12295 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
12296 / bed
->s
->sizeof_sym
);
12298 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
12300 if (sym_count
> max_sym_count
)
12301 max_sym_count
= sym_count
;
12303 if (sym_count
> max_sym_shndx_count
12304 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
12305 max_sym_shndx_count
= sym_count
;
12307 if (esdo
->this_hdr
.sh_type
== SHT_REL
12308 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
12309 /* Some backends use reloc_count in relocation sections
12310 to count particular types of relocs. Of course,
12311 reloc sections themselves can't have relocations. */
12313 else if (emit_relocs
)
12315 reloc_count
= sec
->reloc_count
;
12316 if (bed
->elf_backend_count_additional_relocs
)
12319 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
12320 additional_reloc_count
+= c
;
12323 else if (bed
->elf_backend_count_relocs
)
12324 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
12326 esdi
= elf_section_data (sec
);
12328 if ((sec
->flags
& SEC_RELOC
) != 0)
12330 size_t ext_size
= 0;
12332 if (esdi
->rel
.hdr
!= NULL
)
12333 ext_size
= esdi
->rel
.hdr
->sh_size
;
12334 if (esdi
->rela
.hdr
!= NULL
)
12335 ext_size
+= esdi
->rela
.hdr
->sh_size
;
12337 if (ext_size
> max_external_reloc_size
)
12338 max_external_reloc_size
= ext_size
;
12339 if (sec
->reloc_count
> max_internal_reloc_count
)
12340 max_internal_reloc_count
= sec
->reloc_count
;
12345 if (reloc_count
== 0)
12348 reloc_count
+= additional_reloc_count
;
12349 o
->reloc_count
+= reloc_count
;
12351 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
12355 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
12356 esdo
->rel
.count
+= additional_reloc_count
;
12358 if (esdi
->rela
.hdr
)
12360 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
12361 esdo
->rela
.count
+= additional_reloc_count
;
12367 esdo
->rela
.count
+= reloc_count
;
12369 esdo
->rel
.count
+= reloc_count
;
12373 if (o
->reloc_count
> 0)
12374 o
->flags
|= SEC_RELOC
;
12377 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12378 set it (this is probably a bug) and if it is set
12379 assign_section_numbers will create a reloc section. */
12380 o
->flags
&=~ SEC_RELOC
;
12383 /* If the SEC_ALLOC flag is not set, force the section VMA to
12384 zero. This is done in elf_fake_sections as well, but forcing
12385 the VMA to 0 here will ensure that relocs against these
12386 sections are handled correctly. */
12387 if ((o
->flags
& SEC_ALLOC
) == 0
12388 && ! o
->user_set_vma
)
12392 if (! bfd_link_relocatable (info
) && merged
)
12393 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
12395 /* Figure out the file positions for everything but the symbol table
12396 and the relocs. We set symcount to force assign_section_numbers
12397 to create a symbol table. */
12398 abfd
->symcount
= info
->strip
!= strip_all
|| emit_relocs
;
12399 BFD_ASSERT (! abfd
->output_has_begun
);
12400 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
12403 /* Set sizes, and assign file positions for reloc sections. */
12404 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12406 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12407 if ((o
->flags
& SEC_RELOC
) != 0)
12410 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
12414 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
12418 /* _bfd_elf_compute_section_file_positions makes temporary use
12419 of target_index. Reset it. */
12420 o
->target_index
= 0;
12422 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12423 to count upwards while actually outputting the relocations. */
12424 esdo
->rel
.count
= 0;
12425 esdo
->rela
.count
= 0;
12427 if ((esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
12428 && !bfd_section_is_ctf (o
))
12430 /* Cache the section contents so that they can be compressed
12431 later. Use bfd_malloc since it will be freed by
12432 bfd_compress_section_contents. */
12433 unsigned char *contents
= esdo
->this_hdr
.contents
;
12434 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
12437 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
12438 if (contents
== NULL
)
12440 esdo
->this_hdr
.contents
= contents
;
12444 /* We have now assigned file positions for all the sections except .symtab,
12445 .strtab, and non-loaded reloc and compressed debugging sections. We start
12446 the .symtab section at the current file position, and write directly to it.
12447 We build the .strtab section in memory. */
12448 abfd
->symcount
= 0;
12449 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12450 /* sh_name is set in prep_headers. */
12451 symtab_hdr
->sh_type
= SHT_SYMTAB
;
12452 /* sh_flags, sh_addr and sh_size all start off zero. */
12453 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
12454 /* sh_link is set in assign_section_numbers. */
12455 /* sh_info is set below. */
12456 /* sh_offset is set just below. */
12457 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
12459 if (max_sym_count
< 20)
12460 max_sym_count
= 20;
12461 htab
->strtabsize
= max_sym_count
;
12462 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
12463 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
12464 if (htab
->strtab
== NULL
)
12466 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12468 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
12469 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
12471 if (info
->strip
!= strip_all
|| emit_relocs
)
12473 bfd_boolean name_local_sections
;
12476 file_ptr off
= elf_next_file_pos (abfd
);
12478 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
12480 /* Note that at this point elf_next_file_pos (abfd) is
12481 incorrect. We do not yet know the size of the .symtab section.
12482 We correct next_file_pos below, after we do know the size. */
12484 /* Start writing out the symbol table. The first symbol is always a
12486 elfsym
.st_value
= 0;
12487 elfsym
.st_size
= 0;
12488 elfsym
.st_info
= 0;
12489 elfsym
.st_other
= 0;
12490 elfsym
.st_shndx
= SHN_UNDEF
;
12491 elfsym
.st_target_internal
= 0;
12492 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
12493 bfd_und_section_ptr
, NULL
) != 1)
12496 /* Output a symbol for each section. We output these even if we are
12497 discarding local symbols, since they are used for relocs. These
12498 symbols usually have no names. We store the index of each one in
12499 the index field of the section, so that we can find it again when
12500 outputting relocs. */
12502 name_local_sections
12503 = (bed
->elf_backend_name_local_section_symbols
12504 && bed
->elf_backend_name_local_section_symbols (abfd
));
12507 elfsym
.st_size
= 0;
12508 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12509 elfsym
.st_other
= 0;
12510 elfsym
.st_value
= 0;
12511 elfsym
.st_target_internal
= 0;
12512 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12514 o
= bfd_section_from_elf_index (abfd
, i
);
12517 o
->target_index
= bfd_get_symcount (abfd
);
12518 elfsym
.st_shndx
= i
;
12519 if (!bfd_link_relocatable (info
))
12520 elfsym
.st_value
= o
->vma
;
12521 if (name_local_sections
)
12523 if (elf_link_output_symstrtab (&flinfo
, name
, &elfsym
, o
,
12530 /* On some targets like Irix 5 the symbol split between local and global
12531 ones recorded in the sh_info field needs to be done between section
12532 and all other symbols. */
12533 if (bed
->elf_backend_elfsym_local_is_section
12534 && bed
->elf_backend_elfsym_local_is_section (abfd
))
12535 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12537 /* Allocate some memory to hold information read in from the input
12539 if (max_contents_size
!= 0)
12541 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
12542 if (flinfo
.contents
== NULL
)
12546 if (max_external_reloc_size
!= 0)
12548 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
12549 if (flinfo
.external_relocs
== NULL
)
12553 if (max_internal_reloc_count
!= 0)
12555 amt
= max_internal_reloc_count
* sizeof (Elf_Internal_Rela
);
12556 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
12557 if (flinfo
.internal_relocs
== NULL
)
12561 if (max_sym_count
!= 0)
12563 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
12564 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
12565 if (flinfo
.external_syms
== NULL
)
12568 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
12569 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
12570 if (flinfo
.internal_syms
== NULL
)
12573 amt
= max_sym_count
* sizeof (long);
12574 flinfo
.indices
= (long int *) bfd_malloc (amt
);
12575 if (flinfo
.indices
== NULL
)
12578 amt
= max_sym_count
* sizeof (asection
*);
12579 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
12580 if (flinfo
.sections
== NULL
)
12584 if (max_sym_shndx_count
!= 0)
12586 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
12587 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
12588 if (flinfo
.locsym_shndx
== NULL
)
12594 bfd_vma base
, end
= 0; /* Both bytes. */
12597 for (sec
= htab
->tls_sec
;
12598 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
12601 bfd_size_type size
= sec
->size
;
12602 unsigned int opb
= bfd_octets_per_byte (abfd
, sec
);
12605 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
12607 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
12610 size
= ord
->offset
* opb
+ ord
->size
;
12612 end
= sec
->vma
+ size
/ opb
;
12614 base
= htab
->tls_sec
->vma
;
12615 /* Only align end of TLS section if static TLS doesn't have special
12616 alignment requirements. */
12617 if (bed
->static_tls_alignment
== 1)
12618 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
12619 htab
->tls_size
= end
- base
;
12622 /* Reorder SHF_LINK_ORDER sections. */
12623 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12625 if (!elf_fixup_link_order (abfd
, o
))
12629 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
12632 /* Since ELF permits relocations to be against local symbols, we
12633 must have the local symbols available when we do the relocations.
12634 Since we would rather only read the local symbols once, and we
12635 would rather not keep them in memory, we handle all the
12636 relocations for a single input file at the same time.
12638 Unfortunately, there is no way to know the total number of local
12639 symbols until we have seen all of them, and the local symbol
12640 indices precede the global symbol indices. This means that when
12641 we are generating relocatable output, and we see a reloc against
12642 a global symbol, we can not know the symbol index until we have
12643 finished examining all the local symbols to see which ones we are
12644 going to output. To deal with this, we keep the relocations in
12645 memory, and don't output them until the end of the link. This is
12646 an unfortunate waste of memory, but I don't see a good way around
12647 it. Fortunately, it only happens when performing a relocatable
12648 link, which is not the common case. FIXME: If keep_memory is set
12649 we could write the relocs out and then read them again; I don't
12650 know how bad the memory loss will be. */
12652 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12653 sub
->output_has_begun
= FALSE
;
12654 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12656 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12658 if (p
->type
== bfd_indirect_link_order
12659 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
12660 == bfd_target_elf_flavour
)
12661 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
12663 if (! sub
->output_has_begun
)
12665 if (! elf_link_input_bfd (&flinfo
, sub
))
12667 sub
->output_has_begun
= TRUE
;
12670 else if (p
->type
== bfd_section_reloc_link_order
12671 || p
->type
== bfd_symbol_reloc_link_order
)
12673 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
12678 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
12680 if (p
->type
== bfd_indirect_link_order
12681 && (bfd_get_flavour (sub
)
12682 == bfd_target_elf_flavour
)
12683 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
12684 != bed
->s
->elfclass
))
12686 const char *iclass
, *oclass
;
12688 switch (bed
->s
->elfclass
)
12690 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
12691 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
12692 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
12696 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
12698 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
12699 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
12700 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
12704 bfd_set_error (bfd_error_wrong_format
);
12706 /* xgettext:c-format */
12707 (_("%pB: file class %s incompatible with %s"),
12708 sub
, iclass
, oclass
);
12717 /* Free symbol buffer if needed. */
12718 if (!info
->reduce_memory_overheads
)
12720 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12721 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
12723 free (elf_tdata (sub
)->symbuf
);
12724 elf_tdata (sub
)->symbuf
= NULL
;
12730 /* Output any global symbols that got converted to local in a
12731 version script or due to symbol visibility. We do this in a
12732 separate step since ELF requires all local symbols to appear
12733 prior to any global symbols. FIXME: We should only do this if
12734 some global symbols were, in fact, converted to become local.
12735 FIXME: Will this work correctly with the Irix 5 linker? */
12736 eoinfo
.failed
= FALSE
;
12737 eoinfo
.flinfo
= &flinfo
;
12738 eoinfo
.localsyms
= TRUE
;
12739 eoinfo
.file_sym_done
= FALSE
;
12740 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12744 goto return_local_hash_table
;
12747 /* If backend needs to output some local symbols not present in the hash
12748 table, do it now. */
12749 if (bed
->elf_backend_output_arch_local_syms
12750 && (info
->strip
!= strip_all
|| emit_relocs
))
12752 typedef int (*out_sym_func
)
12753 (void *, const char *, Elf_Internal_Sym
*, asection
*,
12754 struct elf_link_hash_entry
*);
12756 if (! ((*bed
->elf_backend_output_arch_local_syms
)
12757 (abfd
, info
, &flinfo
,
12758 (out_sym_func
) elf_link_output_symstrtab
)))
12761 goto return_local_hash_table
;
12765 /* That wrote out all the local symbols. Finish up the symbol table
12766 with the global symbols. Even if we want to strip everything we
12767 can, we still need to deal with those global symbols that got
12768 converted to local in a version script. */
12770 /* The sh_info field records the index of the first non local symbol. */
12771 if (!symtab_hdr
->sh_info
)
12772 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12775 && htab
->dynsym
!= NULL
12776 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
12778 Elf_Internal_Sym sym
;
12779 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
12781 o
= htab
->dynsym
->output_section
;
12782 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
12784 /* Write out the section symbols for the output sections. */
12785 if (bfd_link_pic (info
)
12786 || htab
->is_relocatable_executable
)
12792 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12794 sym
.st_target_internal
= 0;
12796 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
12802 dynindx
= elf_section_data (s
)->dynindx
;
12805 indx
= elf_section_data (s
)->this_idx
;
12806 BFD_ASSERT (indx
> 0);
12807 sym
.st_shndx
= indx
;
12808 if (! check_dynsym (abfd
, &sym
))
12811 goto return_local_hash_table
;
12813 sym
.st_value
= s
->vma
;
12814 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
12816 /* Inform the linker of the addition of this symbol. */
12818 if (info
->callbacks
->ctf_new_dynsym
)
12819 info
->callbacks
->ctf_new_dynsym (dynindx
, &sym
);
12821 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12825 /* Write out the local dynsyms. */
12826 if (htab
->dynlocal
)
12828 struct elf_link_local_dynamic_entry
*e
;
12829 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
12834 /* Copy the internal symbol and turn off visibility.
12835 Note that we saved a word of storage and overwrote
12836 the original st_name with the dynstr_index. */
12838 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
12839 sym
.st_shndx
= SHN_UNDEF
;
12841 s
= bfd_section_from_elf_index (e
->input_bfd
,
12844 && s
->output_section
!= NULL
12845 && elf_section_data (s
->output_section
) != NULL
)
12848 elf_section_data (s
->output_section
)->this_idx
;
12849 if (! check_dynsym (abfd
, &sym
))
12852 goto return_local_hash_table
;
12854 sym
.st_value
= (s
->output_section
->vma
12856 + e
->isym
.st_value
);
12859 /* Inform the linker of the addition of this symbol. */
12861 if (info
->callbacks
->ctf_new_dynsym
)
12862 info
->callbacks
->ctf_new_dynsym (e
->dynindx
, &sym
);
12864 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
12865 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12870 /* We get the global symbols from the hash table. */
12871 eoinfo
.failed
= FALSE
;
12872 eoinfo
.localsyms
= FALSE
;
12873 eoinfo
.flinfo
= &flinfo
;
12874 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12878 goto return_local_hash_table
;
12881 /* If backend needs to output some symbols not present in the hash
12882 table, do it now. */
12883 if (bed
->elf_backend_output_arch_syms
12884 && (info
->strip
!= strip_all
|| emit_relocs
))
12886 typedef int (*out_sym_func
)
12887 (void *, const char *, Elf_Internal_Sym
*, asection
*,
12888 struct elf_link_hash_entry
*);
12890 if (! ((*bed
->elf_backend_output_arch_syms
)
12891 (abfd
, info
, &flinfo
,
12892 (out_sym_func
) elf_link_output_symstrtab
)))
12895 goto return_local_hash_table
;
12899 /* Finalize the .strtab section. */
12900 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
12902 /* Swap out the .strtab section. */
12903 if (!elf_link_swap_symbols_out (&flinfo
))
12906 goto return_local_hash_table
;
12909 /* Now we know the size of the symtab section. */
12910 if (bfd_get_symcount (abfd
) > 0)
12912 /* Finish up and write out the symbol string table (.strtab)
12914 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
12915 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
12917 if (elf_symtab_shndx_list (abfd
))
12919 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
12921 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
12923 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
12924 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
12925 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
12926 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
12927 symtab_shndx_hdr
->sh_size
= amt
;
12929 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
12932 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
12933 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
12936 goto return_local_hash_table
;
12941 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
12942 /* sh_name was set in prep_headers. */
12943 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
12944 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
12945 symstrtab_hdr
->sh_addr
= 0;
12946 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
12947 symstrtab_hdr
->sh_entsize
= 0;
12948 symstrtab_hdr
->sh_link
= 0;
12949 symstrtab_hdr
->sh_info
= 0;
12950 /* sh_offset is set just below. */
12951 symstrtab_hdr
->sh_addralign
= 1;
12953 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
12955 elf_next_file_pos (abfd
) = off
;
12957 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
12958 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
12961 goto return_local_hash_table
;
12965 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
12967 _bfd_error_handler (_("%pB: failed to generate import library"),
12968 info
->out_implib_bfd
);
12970 goto return_local_hash_table
;
12973 /* Adjust the relocs to have the correct symbol indices. */
12974 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12976 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12979 if ((o
->flags
& SEC_RELOC
) == 0)
12982 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
12983 if (esdo
->rel
.hdr
!= NULL
12984 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
, info
))
12987 goto return_local_hash_table
;
12989 if (esdo
->rela
.hdr
!= NULL
12990 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
, info
))
12993 goto return_local_hash_table
;
12996 /* Set the reloc_count field to 0 to prevent write_relocs from
12997 trying to swap the relocs out itself. */
12998 o
->reloc_count
= 0;
13001 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
13002 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
13004 /* If we are linking against a dynamic object, or generating a
13005 shared library, finish up the dynamic linking information. */
13008 bfd_byte
*dyncon
, *dynconend
;
13010 /* Fix up .dynamic entries. */
13011 o
= bfd_get_linker_section (dynobj
, ".dynamic");
13012 BFD_ASSERT (o
!= NULL
);
13014 dyncon
= o
->contents
;
13015 dynconend
= o
->contents
+ o
->size
;
13016 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13018 Elf_Internal_Dyn dyn
;
13021 bfd_size_type sh_size
;
13024 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13031 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
13033 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
13035 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
13036 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
13039 dyn
.d_un
.d_val
= relativecount
;
13046 name
= info
->init_function
;
13049 name
= info
->fini_function
;
13052 struct elf_link_hash_entry
*h
;
13054 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
13056 && (h
->root
.type
== bfd_link_hash_defined
13057 || h
->root
.type
== bfd_link_hash_defweak
))
13059 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
13060 o
= h
->root
.u
.def
.section
;
13061 if (o
->output_section
!= NULL
)
13062 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
13063 + o
->output_offset
);
13066 /* The symbol is imported from another shared
13067 library and does not apply to this one. */
13068 dyn
.d_un
.d_ptr
= 0;
13075 case DT_PREINIT_ARRAYSZ
:
13076 name
= ".preinit_array";
13078 case DT_INIT_ARRAYSZ
:
13079 name
= ".init_array";
13081 case DT_FINI_ARRAYSZ
:
13082 name
= ".fini_array";
13084 o
= bfd_get_section_by_name (abfd
, name
);
13088 (_("could not find section %s"), name
);
13093 (_("warning: %s section has zero size"), name
);
13094 dyn
.d_un
.d_val
= o
->size
;
13097 case DT_PREINIT_ARRAY
:
13098 name
= ".preinit_array";
13100 case DT_INIT_ARRAY
:
13101 name
= ".init_array";
13103 case DT_FINI_ARRAY
:
13104 name
= ".fini_array";
13106 o
= bfd_get_section_by_name (abfd
, name
);
13113 name
= ".gnu.hash";
13122 name
= ".gnu.version_d";
13125 name
= ".gnu.version_r";
13128 name
= ".gnu.version";
13130 o
= bfd_get_linker_section (dynobj
, name
);
13132 if (o
== NULL
|| bfd_is_abs_section (o
->output_section
))
13135 (_("could not find section %s"), name
);
13138 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
13141 (_("warning: section '%s' is being made into a note"), name
);
13142 bfd_set_error (bfd_error_nonrepresentable_section
);
13145 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
13152 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
13158 for (i
= 1; i
< elf_numsections (abfd
); i
++)
13160 Elf_Internal_Shdr
*hdr
;
13162 hdr
= elf_elfsections (abfd
)[i
];
13163 if (hdr
->sh_type
== type
13164 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
13166 sh_size
+= hdr
->sh_size
;
13168 || sh_addr
> hdr
->sh_addr
)
13169 sh_addr
= hdr
->sh_addr
;
13173 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
13175 unsigned int opb
= bfd_octets_per_byte (abfd
, o
);
13177 /* Don't count procedure linkage table relocs in the
13178 overall reloc count. */
13179 sh_size
-= htab
->srelplt
->size
;
13181 /* If the size is zero, make the address zero too.
13182 This is to avoid a glibc bug. If the backend
13183 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13184 zero, then we'll put DT_RELA at the end of
13185 DT_JMPREL. glibc will interpret the end of
13186 DT_RELA matching the end of DT_JMPREL as the
13187 case where DT_RELA includes DT_JMPREL, and for
13188 LD_BIND_NOW will decide that processing DT_RELA
13189 will process the PLT relocs too. Net result:
13190 No PLT relocs applied. */
13193 /* If .rela.plt is the first .rela section, exclude
13194 it from DT_RELA. */
13195 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
13196 + htab
->srelplt
->output_offset
) * opb
)
13197 sh_addr
+= htab
->srelplt
->size
;
13200 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
13201 dyn
.d_un
.d_val
= sh_size
;
13203 dyn
.d_un
.d_ptr
= sh_addr
;
13206 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13210 /* If we have created any dynamic sections, then output them. */
13211 if (dynobj
!= NULL
)
13213 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
13216 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13217 if (bfd_link_textrel_check (info
)
13218 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
)
13220 bfd_byte
*dyncon
, *dynconend
;
13222 dyncon
= o
->contents
;
13223 dynconend
= o
->contents
+ o
->size
;
13224 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13226 Elf_Internal_Dyn dyn
;
13228 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13230 if (dyn
.d_tag
== DT_TEXTREL
)
13232 if (info
->textrel_check
== textrel_check_error
)
13233 info
->callbacks
->einfo
13234 (_("%P%X: read-only segment has dynamic relocations\n"));
13235 else if (bfd_link_dll (info
))
13236 info
->callbacks
->einfo
13237 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13239 info
->callbacks
->einfo
13240 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13246 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
13248 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
13250 || o
->output_section
== bfd_abs_section_ptr
)
13252 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
13254 /* At this point, we are only interested in sections
13255 created by _bfd_elf_link_create_dynamic_sections. */
13258 if (htab
->stab_info
.stabstr
== o
)
13260 if (htab
->eh_info
.hdr_sec
== o
)
13262 if (strcmp (o
->name
, ".dynstr") != 0)
13264 bfd_size_type octets
= ((file_ptr
) o
->output_offset
13265 * bfd_octets_per_byte (abfd
, o
));
13266 if (!bfd_set_section_contents (abfd
, o
->output_section
,
13267 o
->contents
, octets
, o
->size
))
13272 /* The contents of the .dynstr section are actually in a
13276 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
13277 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
13278 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
13284 if (!info
->resolve_section_groups
)
13286 bfd_boolean failed
= FALSE
;
13288 BFD_ASSERT (bfd_link_relocatable (info
));
13289 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
13294 /* If we have optimized stabs strings, output them. */
13295 if (htab
->stab_info
.stabstr
!= NULL
)
13297 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
13301 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
13304 if (info
->callbacks
->emit_ctf
)
13305 info
->callbacks
->emit_ctf ();
13307 elf_final_link_free (abfd
, &flinfo
);
13311 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
13312 if (contents
== NULL
)
13314 /* Bail out and fail. */
13316 goto return_local_hash_table
;
13318 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
13319 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
13323 return_local_hash_table
:
13324 if (info
->unique_symbol
)
13325 bfd_hash_table_free (&flinfo
.local_hash_table
);
13329 elf_final_link_free (abfd
, &flinfo
);
13331 goto return_local_hash_table
;
13334 /* Initialize COOKIE for input bfd ABFD. */
13337 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
13338 struct bfd_link_info
*info
, bfd
*abfd
)
13340 Elf_Internal_Shdr
*symtab_hdr
;
13341 const struct elf_backend_data
*bed
;
13343 bed
= get_elf_backend_data (abfd
);
13344 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13346 cookie
->abfd
= abfd
;
13347 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
13348 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
13349 if (cookie
->bad_symtab
)
13351 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13352 cookie
->extsymoff
= 0;
13356 cookie
->locsymcount
= symtab_hdr
->sh_info
;
13357 cookie
->extsymoff
= symtab_hdr
->sh_info
;
13360 if (bed
->s
->arch_size
== 32)
13361 cookie
->r_sym_shift
= 8;
13363 cookie
->r_sym_shift
= 32;
13365 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
13366 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
13368 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
13369 cookie
->locsymcount
, 0,
13371 if (cookie
->locsyms
== NULL
)
13373 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
13376 if (info
->keep_memory
)
13377 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
13382 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13385 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
13387 Elf_Internal_Shdr
*symtab_hdr
;
13389 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13390 if (symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
13391 free (cookie
->locsyms
);
13394 /* Initialize the relocation information in COOKIE for input section SEC
13395 of input bfd ABFD. */
13398 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13399 struct bfd_link_info
*info
, bfd
*abfd
,
13402 if (sec
->reloc_count
== 0)
13404 cookie
->rels
= NULL
;
13405 cookie
->relend
= NULL
;
13409 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
13410 info
->keep_memory
);
13411 if (cookie
->rels
== NULL
)
13413 cookie
->rel
= cookie
->rels
;
13414 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
13416 cookie
->rel
= cookie
->rels
;
13420 /* Free the memory allocated by init_reloc_cookie_rels,
13424 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13427 if (elf_section_data (sec
)->relocs
!= cookie
->rels
)
13428 free (cookie
->rels
);
13431 /* Initialize the whole of COOKIE for input section SEC. */
13434 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13435 struct bfd_link_info
*info
,
13438 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
13440 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
13445 fini_reloc_cookie (cookie
, sec
->owner
);
13450 /* Free the memory allocated by init_reloc_cookie_for_section,
13454 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13457 fini_reloc_cookie_rels (cookie
, sec
);
13458 fini_reloc_cookie (cookie
, sec
->owner
);
13461 /* Garbage collect unused sections. */
13463 /* Default gc_mark_hook. */
13466 _bfd_elf_gc_mark_hook (asection
*sec
,
13467 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13468 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13469 struct elf_link_hash_entry
*h
,
13470 Elf_Internal_Sym
*sym
)
13474 switch (h
->root
.type
)
13476 case bfd_link_hash_defined
:
13477 case bfd_link_hash_defweak
:
13478 return h
->root
.u
.def
.section
;
13480 case bfd_link_hash_common
:
13481 return h
->root
.u
.c
.p
->section
;
13488 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
13493 /* Return the debug definition section. */
13496 elf_gc_mark_debug_section (asection
*sec ATTRIBUTE_UNUSED
,
13497 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13498 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13499 struct elf_link_hash_entry
*h
,
13500 Elf_Internal_Sym
*sym
)
13504 /* Return the global debug definition section. */
13505 if ((h
->root
.type
== bfd_link_hash_defined
13506 || h
->root
.type
== bfd_link_hash_defweak
)
13507 && (h
->root
.u
.def
.section
->flags
& SEC_DEBUGGING
) != 0)
13508 return h
->root
.u
.def
.section
;
13512 /* Return the local debug definition section. */
13513 asection
*isec
= bfd_section_from_elf_index (sec
->owner
,
13515 if ((isec
->flags
& SEC_DEBUGGING
) != 0)
13522 /* COOKIE->rel describes a relocation against section SEC, which is
13523 a section we've decided to keep. Return the section that contains
13524 the relocation symbol, or NULL if no section contains it. */
13527 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
13528 elf_gc_mark_hook_fn gc_mark_hook
,
13529 struct elf_reloc_cookie
*cookie
,
13530 bfd_boolean
*start_stop
)
13532 unsigned long r_symndx
;
13533 struct elf_link_hash_entry
*h
, *hw
;
13535 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
13536 if (r_symndx
== STN_UNDEF
)
13539 if (r_symndx
>= cookie
->locsymcount
13540 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13542 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
13545 info
->callbacks
->einfo (_("%F%P: corrupt input: %pB\n"),
13549 while (h
->root
.type
== bfd_link_hash_indirect
13550 || h
->root
.type
== bfd_link_hash_warning
)
13551 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13553 /* Keep all aliases of the symbol too. If an object symbol
13554 needs to be copied into .dynbss then all of its aliases
13555 should be present as dynamic symbols, not just the one used
13556 on the copy relocation. */
13558 while (hw
->is_weakalias
)
13564 if (start_stop
!= NULL
)
13566 /* To work around a glibc bug, mark XXX input sections
13567 when there is a reference to __start_XXX or __stop_XXX
13571 asection
*s
= h
->u2
.start_stop_section
;
13572 *start_stop
= !s
->gc_mark
;
13577 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
13580 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
13581 &cookie
->locsyms
[r_symndx
]);
13584 /* COOKIE->rel describes a relocation against section SEC, which is
13585 a section we've decided to keep. Mark the section that contains
13586 the relocation symbol. */
13589 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
13591 elf_gc_mark_hook_fn gc_mark_hook
,
13592 struct elf_reloc_cookie
*cookie
)
13595 bfd_boolean start_stop
= FALSE
;
13597 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
13598 while (rsec
!= NULL
)
13600 if (!rsec
->gc_mark
)
13602 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
13603 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
13605 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
13610 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
13615 /* The mark phase of garbage collection. For a given section, mark
13616 it and any sections in this section's group, and all the sections
13617 which define symbols to which it refers. */
13620 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
13622 elf_gc_mark_hook_fn gc_mark_hook
)
13625 asection
*group_sec
, *eh_frame
;
13629 /* Mark all the sections in the group. */
13630 group_sec
= elf_section_data (sec
)->next_in_group
;
13631 if (group_sec
&& !group_sec
->gc_mark
)
13632 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
13635 /* Look through the section relocs. */
13637 eh_frame
= elf_eh_frame_section (sec
->owner
);
13638 if ((sec
->flags
& SEC_RELOC
) != 0
13639 && sec
->reloc_count
> 0
13640 && sec
!= eh_frame
)
13642 struct elf_reloc_cookie cookie
;
13644 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
13648 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
13649 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
13654 fini_reloc_cookie_for_section (&cookie
, sec
);
13658 if (ret
&& eh_frame
&& elf_fde_list (sec
))
13660 struct elf_reloc_cookie cookie
;
13662 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
13666 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
13667 gc_mark_hook
, &cookie
))
13669 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
13673 eh_frame
= elf_section_eh_frame_entry (sec
);
13674 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
13675 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
13681 /* Scan and mark sections in a special or debug section group. */
13684 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
13686 /* Point to first section of section group. */
13688 /* Used to iterate the section group. */
13691 bfd_boolean is_special_grp
= TRUE
;
13692 bfd_boolean is_debug_grp
= TRUE
;
13694 /* First scan to see if group contains any section other than debug
13695 and special section. */
13696 ssec
= msec
= elf_next_in_group (grp
);
13699 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
13700 is_debug_grp
= FALSE
;
13702 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
13703 is_special_grp
= FALSE
;
13705 msec
= elf_next_in_group (msec
);
13707 while (msec
!= ssec
);
13709 /* If this is a pure debug section group or pure special section group,
13710 keep all sections in this group. */
13711 if (is_debug_grp
|| is_special_grp
)
13716 msec
= elf_next_in_group (msec
);
13718 while (msec
!= ssec
);
13722 /* Keep debug and special sections. */
13725 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
13726 elf_gc_mark_hook_fn mark_hook
)
13730 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13733 bfd_boolean some_kept
;
13734 bfd_boolean debug_frag_seen
;
13735 bfd_boolean has_kept_debug_info
;
13737 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13739 isec
= ibfd
->sections
;
13740 if (isec
== NULL
|| isec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13743 /* Ensure all linker created sections are kept,
13744 see if any other section is already marked,
13745 and note if we have any fragmented debug sections. */
13746 debug_frag_seen
= some_kept
= has_kept_debug_info
= FALSE
;
13747 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13749 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
13751 else if (isec
->gc_mark
13752 && (isec
->flags
& SEC_ALLOC
) != 0
13753 && elf_section_type (isec
) != SHT_NOTE
)
13757 /* Since all sections, except for backend specific ones,
13758 have been garbage collected, call mark_hook on this
13759 section if any of its linked-to sections is marked. */
13760 asection
*linked_to_sec
= elf_linked_to_section (isec
);
13761 for (; linked_to_sec
!= NULL
;
13762 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
13763 if (linked_to_sec
->gc_mark
)
13765 if (!_bfd_elf_gc_mark (info
, isec
, mark_hook
))
13771 if (!debug_frag_seen
13772 && (isec
->flags
& SEC_DEBUGGING
)
13773 && CONST_STRNEQ (isec
->name
, ".debug_line."))
13774 debug_frag_seen
= TRUE
;
13775 else if (strcmp (bfd_section_name (isec
),
13776 "__patchable_function_entries") == 0
13777 && elf_linked_to_section (isec
) == NULL
)
13778 info
->callbacks
->einfo (_("%F%P: %pB(%pA): error: "
13779 "need linked-to section "
13780 "for --gc-sections\n"),
13781 isec
->owner
, isec
);
13784 /* If no non-note alloc section in this file will be kept, then
13785 we can toss out the debug and special sections. */
13789 /* Keep debug and special sections like .comment when they are
13790 not part of a group. Also keep section groups that contain
13791 just debug sections or special sections. NB: Sections with
13792 linked-to section has been handled above. */
13793 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13795 if ((isec
->flags
& SEC_GROUP
) != 0)
13796 _bfd_elf_gc_mark_debug_special_section_group (isec
);
13797 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
13798 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
13799 && elf_next_in_group (isec
) == NULL
13800 && elf_linked_to_section (isec
) == NULL
)
13802 if (isec
->gc_mark
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
13803 has_kept_debug_info
= TRUE
;
13806 /* Look for CODE sections which are going to be discarded,
13807 and find and discard any fragmented debug sections which
13808 are associated with that code section. */
13809 if (debug_frag_seen
)
13810 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13811 if ((isec
->flags
& SEC_CODE
) != 0
13812 && isec
->gc_mark
== 0)
13817 ilen
= strlen (isec
->name
);
13819 /* Association is determined by the name of the debug
13820 section containing the name of the code section as
13821 a suffix. For example .debug_line.text.foo is a
13822 debug section associated with .text.foo. */
13823 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
13827 if (dsec
->gc_mark
== 0
13828 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
13831 dlen
= strlen (dsec
->name
);
13834 && strncmp (dsec
->name
+ (dlen
- ilen
),
13835 isec
->name
, ilen
) == 0)
13840 /* Mark debug sections referenced by kept debug sections. */
13841 if (has_kept_debug_info
)
13842 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13844 && (isec
->flags
& SEC_DEBUGGING
) != 0)
13845 if (!_bfd_elf_gc_mark (info
, isec
,
13846 elf_gc_mark_debug_section
))
13853 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
13856 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13858 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13862 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13863 || elf_object_id (sub
) != elf_hash_table_id (elf_hash_table (info
))
13864 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13867 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13870 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13872 /* When any section in a section group is kept, we keep all
13873 sections in the section group. If the first member of
13874 the section group is excluded, we will also exclude the
13876 if (o
->flags
& SEC_GROUP
)
13878 asection
*first
= elf_next_in_group (o
);
13879 o
->gc_mark
= first
->gc_mark
;
13885 /* Skip sweeping sections already excluded. */
13886 if (o
->flags
& SEC_EXCLUDE
)
13889 /* Since this is early in the link process, it is simple
13890 to remove a section from the output. */
13891 o
->flags
|= SEC_EXCLUDE
;
13893 if (info
->print_gc_sections
&& o
->size
!= 0)
13894 /* xgettext:c-format */
13895 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13903 /* Propagate collected vtable information. This is called through
13904 elf_link_hash_traverse. */
13907 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
13909 /* Those that are not vtables. */
13911 || h
->u2
.vtable
== NULL
13912 || h
->u2
.vtable
->parent
== NULL
)
13915 /* Those vtables that do not have parents, we cannot merge. */
13916 if (h
->u2
.vtable
->parent
== (struct elf_link_hash_entry
*) -1)
13919 /* If we've already been done, exit. */
13920 if (h
->u2
.vtable
->used
&& h
->u2
.vtable
->used
[-1])
13923 /* Make sure the parent's table is up to date. */
13924 elf_gc_propagate_vtable_entries_used (h
->u2
.vtable
->parent
, okp
);
13926 if (h
->u2
.vtable
->used
== NULL
)
13928 /* None of this table's entries were referenced. Re-use the
13930 h
->u2
.vtable
->used
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13931 h
->u2
.vtable
->size
= h
->u2
.vtable
->parent
->u2
.vtable
->size
;
13936 bfd_boolean
*cu
, *pu
;
13938 /* Or the parent's entries into ours. */
13939 cu
= h
->u2
.vtable
->used
;
13941 pu
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13944 const struct elf_backend_data
*bed
;
13945 unsigned int log_file_align
;
13947 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
13948 log_file_align
= bed
->s
->log_file_align
;
13949 n
= h
->u2
.vtable
->parent
->u2
.vtable
->size
>> log_file_align
;
13964 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
13967 bfd_vma hstart
, hend
;
13968 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
13969 const struct elf_backend_data
*bed
;
13970 unsigned int log_file_align
;
13972 /* Take care of both those symbols that do not describe vtables as
13973 well as those that are not loaded. */
13975 || h
->u2
.vtable
== NULL
13976 || h
->u2
.vtable
->parent
== NULL
)
13979 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
13980 || h
->root
.type
== bfd_link_hash_defweak
);
13982 sec
= h
->root
.u
.def
.section
;
13983 hstart
= h
->root
.u
.def
.value
;
13984 hend
= hstart
+ h
->size
;
13986 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
13988 return *(bfd_boolean
*) okp
= FALSE
;
13989 bed
= get_elf_backend_data (sec
->owner
);
13990 log_file_align
= bed
->s
->log_file_align
;
13992 relend
= relstart
+ sec
->reloc_count
;
13994 for (rel
= relstart
; rel
< relend
; ++rel
)
13995 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
13997 /* If the entry is in use, do nothing. */
13998 if (h
->u2
.vtable
->used
13999 && (rel
->r_offset
- hstart
) < h
->u2
.vtable
->size
)
14001 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
14002 if (h
->u2
.vtable
->used
[entry
])
14005 /* Otherwise, kill it. */
14006 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
14012 /* Mark sections containing dynamically referenced symbols. When
14013 building shared libraries, we must assume that any visible symbol is
14017 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
14019 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
14020 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
14022 if ((h
->root
.type
== bfd_link_hash_defined
14023 || h
->root
.type
== bfd_link_hash_defweak
)
14024 && ((h
->ref_dynamic
&& !h
->forced_local
)
14025 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
14026 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
14027 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
14028 && (!bfd_link_executable (info
)
14029 || info
->gc_keep_exported
14030 || info
->export_dynamic
14033 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
14034 && (h
->versioned
>= versioned
14035 || !bfd_hide_sym_by_version (info
->version_info
,
14036 h
->root
.root
.string
)))))
14037 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14042 /* Keep all sections containing symbols undefined on the command-line,
14043 and the section containing the entry symbol. */
14046 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
14048 struct bfd_sym_chain
*sym
;
14050 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
14052 struct elf_link_hash_entry
*h
;
14054 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
14055 FALSE
, FALSE
, FALSE
);
14058 && (h
->root
.type
== bfd_link_hash_defined
14059 || h
->root
.type
== bfd_link_hash_defweak
)
14060 && !bfd_is_const_section (h
->root
.u
.def
.section
))
14061 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14066 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
14067 struct bfd_link_info
*info
)
14069 bfd
*ibfd
= info
->input_bfds
;
14071 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
14074 struct elf_reloc_cookie cookie
;
14076 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
14078 sec
= ibfd
->sections
;
14079 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14082 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
14085 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
14087 if (CONST_STRNEQ (bfd_section_name (sec
), ".eh_frame_entry")
14088 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
14090 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
14091 fini_reloc_cookie_rels (&cookie
, sec
);
14098 /* Do mark and sweep of unused sections. */
14101 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
14103 bfd_boolean ok
= TRUE
;
14105 elf_gc_mark_hook_fn gc_mark_hook
;
14106 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14107 struct elf_link_hash_table
*htab
;
14109 if (!bed
->can_gc_sections
14110 || !is_elf_hash_table (info
->hash
))
14112 _bfd_error_handler(_("warning: gc-sections option ignored"));
14116 bed
->gc_keep (info
);
14117 htab
= elf_hash_table (info
);
14119 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14120 at the .eh_frame section if we can mark the FDEs individually. */
14121 for (sub
= info
->input_bfds
;
14122 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
14123 sub
= sub
->link
.next
)
14126 struct elf_reloc_cookie cookie
;
14128 sec
= sub
->sections
;
14129 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14131 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
14132 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
14134 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
14135 if (elf_section_data (sec
)->sec_info
14136 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
14137 elf_eh_frame_section (sub
) = sec
;
14138 fini_reloc_cookie_for_section (&cookie
, sec
);
14139 sec
= bfd_get_next_section_by_name (NULL
, sec
);
14143 /* Apply transitive closure to the vtable entry usage info. */
14144 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
14148 /* Kill the vtable relocations that were not used. */
14149 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &ok
);
14153 /* Mark dynamically referenced symbols. */
14154 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
14155 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
14157 /* Grovel through relocs to find out who stays ... */
14158 gc_mark_hook
= bed
->gc_mark_hook
;
14159 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
14163 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
14164 || elf_object_id (sub
) != elf_hash_table_id (htab
)
14165 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
14169 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14172 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14173 Also treat note sections as a root, if the section is not part
14174 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14175 well as FINI_ARRAY sections for ld -r. */
14176 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
14178 && (o
->flags
& SEC_EXCLUDE
) == 0
14179 && ((o
->flags
& SEC_KEEP
) != 0
14180 || (bfd_link_relocatable (info
)
14181 && ((elf_section_data (o
)->this_hdr
.sh_type
14182 == SHT_PREINIT_ARRAY
)
14183 || (elf_section_data (o
)->this_hdr
.sh_type
14185 || (elf_section_data (o
)->this_hdr
.sh_type
14186 == SHT_FINI_ARRAY
)))
14187 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
14188 && elf_next_in_group (o
) == NULL
14189 && elf_linked_to_section (o
) == NULL
)
14190 || ((elf_tdata (sub
)->has_gnu_osabi
& elf_gnu_osabi_retain
)
14191 && (elf_section_flags (o
) & SHF_GNU_RETAIN
))))
14193 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
14198 /* Allow the backend to mark additional target specific sections. */
14199 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
14201 /* ... and mark SEC_EXCLUDE for those that go. */
14202 return elf_gc_sweep (abfd
, info
);
14205 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14208 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
14210 struct elf_link_hash_entry
*h
,
14213 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
14214 struct elf_link_hash_entry
**search
, *child
;
14215 size_t extsymcount
;
14216 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14218 /* The sh_info field of the symtab header tells us where the
14219 external symbols start. We don't care about the local symbols at
14221 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
14222 if (!elf_bad_symtab (abfd
))
14223 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
14225 sym_hashes
= elf_sym_hashes (abfd
);
14226 sym_hashes_end
= sym_hashes
+ extsymcount
;
14228 /* Hunt down the child symbol, which is in this section at the same
14229 offset as the relocation. */
14230 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
14232 if ((child
= *search
) != NULL
14233 && (child
->root
.type
== bfd_link_hash_defined
14234 || child
->root
.type
== bfd_link_hash_defweak
)
14235 && child
->root
.u
.def
.section
== sec
14236 && child
->root
.u
.def
.value
== offset
)
14240 /* xgettext:c-format */
14241 _bfd_error_handler (_("%pB: %pA+%#" PRIx64
": no symbol found for INHERIT"),
14242 abfd
, sec
, (uint64_t) offset
);
14243 bfd_set_error (bfd_error_invalid_operation
);
14247 if (!child
->u2
.vtable
)
14249 child
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14250 bfd_zalloc (abfd
, sizeof (*child
->u2
.vtable
)));
14251 if (!child
->u2
.vtable
)
14256 /* This *should* only be the absolute section. It could potentially
14257 be that someone has defined a non-global vtable though, which
14258 would be bad. It isn't worth paging in the local symbols to be
14259 sure though; that case should simply be handled by the assembler. */
14261 child
->u2
.vtable
->parent
= (struct elf_link_hash_entry
*) -1;
14264 child
->u2
.vtable
->parent
= h
;
14269 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14272 bfd_elf_gc_record_vtentry (bfd
*abfd
, asection
*sec
,
14273 struct elf_link_hash_entry
*h
,
14276 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14277 unsigned int log_file_align
= bed
->s
->log_file_align
;
14281 /* xgettext:c-format */
14282 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14284 bfd_set_error (bfd_error_bad_value
);
14290 h
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14291 bfd_zalloc (abfd
, sizeof (*h
->u2
.vtable
)));
14296 if (addend
>= h
->u2
.vtable
->size
)
14298 size_t size
, bytes
, file_align
;
14299 bfd_boolean
*ptr
= h
->u2
.vtable
->used
;
14301 /* While the symbol is undefined, we have to be prepared to handle
14303 file_align
= 1 << log_file_align
;
14304 if (h
->root
.type
== bfd_link_hash_undefined
)
14305 size
= addend
+ file_align
;
14309 if (addend
>= size
)
14311 /* Oops! We've got a reference past the defined end of
14312 the table. This is probably a bug -- shall we warn? */
14313 size
= addend
+ file_align
;
14316 size
= (size
+ file_align
- 1) & -file_align
;
14318 /* Allocate one extra entry for use as a "done" flag for the
14319 consolidation pass. */
14320 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
14324 ptr
= (bfd_boolean
*) bfd_realloc (ptr
- 1, bytes
);
14330 oldbytes
= (((h
->u2
.vtable
->size
>> log_file_align
) + 1)
14331 * sizeof (bfd_boolean
));
14332 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
14336 ptr
= (bfd_boolean
*) bfd_zmalloc (bytes
);
14341 /* And arrange for that done flag to be at index -1. */
14342 h
->u2
.vtable
->used
= ptr
+ 1;
14343 h
->u2
.vtable
->size
= size
;
14346 h
->u2
.vtable
->used
[addend
>> log_file_align
] = TRUE
;
14351 /* Map an ELF section header flag to its corresponding string. */
14355 flagword flag_value
;
14356 } elf_flags_to_name_table
;
14358 static elf_flags_to_name_table elf_flags_to_names
[] =
14360 { "SHF_WRITE", SHF_WRITE
},
14361 { "SHF_ALLOC", SHF_ALLOC
},
14362 { "SHF_EXECINSTR", SHF_EXECINSTR
},
14363 { "SHF_MERGE", SHF_MERGE
},
14364 { "SHF_STRINGS", SHF_STRINGS
},
14365 { "SHF_INFO_LINK", SHF_INFO_LINK
},
14366 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
14367 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
14368 { "SHF_GROUP", SHF_GROUP
},
14369 { "SHF_TLS", SHF_TLS
},
14370 { "SHF_MASKOS", SHF_MASKOS
},
14371 { "SHF_EXCLUDE", SHF_EXCLUDE
},
14374 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14376 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
14377 struct flag_info
*flaginfo
,
14380 const bfd_vma sh_flags
= elf_section_flags (section
);
14382 if (!flaginfo
->flags_initialized
)
14384 bfd
*obfd
= info
->output_bfd
;
14385 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14386 struct flag_info_list
*tf
= flaginfo
->flag_list
;
14388 int without_hex
= 0;
14390 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
14393 flagword (*lookup
) (char *);
14395 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
14396 if (lookup
!= NULL
)
14398 flagword hexval
= (*lookup
) ((char *) tf
->name
);
14402 if (tf
->with
== with_flags
)
14403 with_hex
|= hexval
;
14404 else if (tf
->with
== without_flags
)
14405 without_hex
|= hexval
;
14410 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
14412 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
14414 if (tf
->with
== with_flags
)
14415 with_hex
|= elf_flags_to_names
[i
].flag_value
;
14416 else if (tf
->with
== without_flags
)
14417 without_hex
|= elf_flags_to_names
[i
].flag_value
;
14424 info
->callbacks
->einfo
14425 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
14429 flaginfo
->flags_initialized
= TRUE
;
14430 flaginfo
->only_with_flags
|= with_hex
;
14431 flaginfo
->not_with_flags
|= without_hex
;
14434 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
14437 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
14443 struct alloc_got_off_arg
{
14445 struct bfd_link_info
*info
;
14448 /* We need a special top-level link routine to convert got reference counts
14449 to real got offsets. */
14452 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
14454 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
14455 bfd
*obfd
= gofarg
->info
->output_bfd
;
14456 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14458 if (h
->got
.refcount
> 0)
14460 h
->got
.offset
= gofarg
->gotoff
;
14461 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
14464 h
->got
.offset
= (bfd_vma
) -1;
14469 /* And an accompanying bit to work out final got entry offsets once
14470 we're done. Should be called from final_link. */
14473 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
14474 struct bfd_link_info
*info
)
14477 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14479 struct alloc_got_off_arg gofarg
;
14481 BFD_ASSERT (abfd
== info
->output_bfd
);
14483 if (! is_elf_hash_table (info
->hash
))
14486 /* The GOT offset is relative to the .got section, but the GOT header is
14487 put into the .got.plt section, if the backend uses it. */
14488 if (bed
->want_got_plt
)
14491 gotoff
= bed
->got_header_size
;
14493 /* Do the local .got entries first. */
14494 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
14496 bfd_signed_vma
*local_got
;
14497 size_t j
, locsymcount
;
14498 Elf_Internal_Shdr
*symtab_hdr
;
14500 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
14503 local_got
= elf_local_got_refcounts (i
);
14507 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
14508 if (elf_bad_symtab (i
))
14509 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
14511 locsymcount
= symtab_hdr
->sh_info
;
14513 for (j
= 0; j
< locsymcount
; ++j
)
14515 if (local_got
[j
] > 0)
14517 local_got
[j
] = gotoff
;
14518 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
14521 local_got
[j
] = (bfd_vma
) -1;
14525 /* Then the global .got entries. .plt refcounts are handled by
14526 adjust_dynamic_symbol */
14527 gofarg
.gotoff
= gotoff
;
14528 gofarg
.info
= info
;
14529 elf_link_hash_traverse (elf_hash_table (info
),
14530 elf_gc_allocate_got_offsets
,
14535 /* Many folk need no more in the way of final link than this, once
14536 got entry reference counting is enabled. */
14539 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
14541 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
14544 /* Invoke the regular ELF backend linker to do all the work. */
14545 return bfd_elf_final_link (abfd
, info
);
14549 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
14551 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
14553 if (rcookie
->bad_symtab
)
14554 rcookie
->rel
= rcookie
->rels
;
14556 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
14558 unsigned long r_symndx
;
14560 if (! rcookie
->bad_symtab
)
14561 if (rcookie
->rel
->r_offset
> offset
)
14563 if (rcookie
->rel
->r_offset
!= offset
)
14566 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
14567 if (r_symndx
== STN_UNDEF
)
14570 if (r_symndx
>= rcookie
->locsymcount
14571 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
14573 struct elf_link_hash_entry
*h
;
14575 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
14577 while (h
->root
.type
== bfd_link_hash_indirect
14578 || h
->root
.type
== bfd_link_hash_warning
)
14579 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
14581 if ((h
->root
.type
== bfd_link_hash_defined
14582 || h
->root
.type
== bfd_link_hash_defweak
)
14583 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
14584 || h
->root
.u
.def
.section
->kept_section
!= NULL
14585 || discarded_section (h
->root
.u
.def
.section
)))
14590 /* It's not a relocation against a global symbol,
14591 but it could be a relocation against a local
14592 symbol for a discarded section. */
14594 Elf_Internal_Sym
*isym
;
14596 /* Need to: get the symbol; get the section. */
14597 isym
= &rcookie
->locsyms
[r_symndx
];
14598 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
14600 && (isec
->kept_section
!= NULL
14601 || discarded_section (isec
)))
14609 /* Discard unneeded references to discarded sections.
14610 Returns -1 on error, 1 if any section's size was changed, 0 if
14611 nothing changed. This function assumes that the relocations are in
14612 sorted order, which is true for all known assemblers. */
14615 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
14617 struct elf_reloc_cookie cookie
;
14622 if (info
->traditional_format
14623 || !is_elf_hash_table (info
->hash
))
14626 o
= bfd_get_section_by_name (output_bfd
, ".stab");
14631 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14634 || i
->reloc_count
== 0
14635 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
14639 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14642 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14645 if (_bfd_discard_section_stabs (abfd
, i
,
14646 elf_section_data (i
)->sec_info
,
14647 bfd_elf_reloc_symbol_deleted_p
,
14651 fini_reloc_cookie_for_section (&cookie
, i
);
14656 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
14657 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
14661 int eh_changed
= 0;
14662 unsigned int eh_alignment
; /* Octets. */
14664 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14670 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14673 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14676 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
14677 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
14678 bfd_elf_reloc_symbol_deleted_p
,
14682 if (i
->size
!= i
->rawsize
)
14686 fini_reloc_cookie_for_section (&cookie
, i
);
14689 eh_alignment
= ((1 << o
->alignment_power
)
14690 * bfd_octets_per_byte (output_bfd
, o
));
14691 /* Skip over zero terminator, and prevent empty sections from
14692 adding alignment padding at the end. */
14693 for (i
= o
->map_tail
.s
; i
!= NULL
; i
= i
->map_tail
.s
)
14695 i
->flags
|= SEC_EXCLUDE
;
14696 else if (i
->size
> 4)
14698 /* The last non-empty eh_frame section doesn't need padding. */
14701 /* Any prior sections must pad the last FDE out to the output
14702 section alignment. Otherwise we might have zero padding
14703 between sections, which would be seen as a terminator. */
14704 for (; i
!= NULL
; i
= i
->map_tail
.s
)
14706 /* All but the last zero terminator should have been removed. */
14711 = (i
->size
+ eh_alignment
- 1) & -eh_alignment
;
14712 if (i
->size
!= size
)
14720 elf_link_hash_traverse (elf_hash_table (info
),
14721 _bfd_elf_adjust_eh_frame_global_symbol
, NULL
);
14724 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
14726 const struct elf_backend_data
*bed
;
14729 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14731 s
= abfd
->sections
;
14732 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14735 bed
= get_elf_backend_data (abfd
);
14737 if (bed
->elf_backend_discard_info
!= NULL
)
14739 if (!init_reloc_cookie (&cookie
, info
, abfd
))
14742 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
14745 fini_reloc_cookie (&cookie
, abfd
);
14749 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
14750 _bfd_elf_end_eh_frame_parsing (info
);
14752 if (info
->eh_frame_hdr_type
14753 && !bfd_link_relocatable (info
)
14754 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
14761 _bfd_elf_section_already_linked (bfd
*abfd
,
14763 struct bfd_link_info
*info
)
14766 const char *name
, *key
;
14767 struct bfd_section_already_linked
*l
;
14768 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
14770 if (sec
->output_section
== bfd_abs_section_ptr
)
14773 flags
= sec
->flags
;
14775 /* Return if it isn't a linkonce section. A comdat group section
14776 also has SEC_LINK_ONCE set. */
14777 if ((flags
& SEC_LINK_ONCE
) == 0)
14780 /* Don't put group member sections on our list of already linked
14781 sections. They are handled as a group via their group section. */
14782 if (elf_sec_group (sec
) != NULL
)
14785 /* For a SHT_GROUP section, use the group signature as the key. */
14787 if ((flags
& SEC_GROUP
) != 0
14788 && elf_next_in_group (sec
) != NULL
14789 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
14790 key
= elf_group_name (elf_next_in_group (sec
));
14793 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14794 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
14795 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
14798 /* Must be a user linkonce section that doesn't follow gcc's
14799 naming convention. In this case we won't be matching
14800 single member groups. */
14804 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
14806 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14808 /* We may have 2 different types of sections on the list: group
14809 sections with a signature of <key> (<key> is some string),
14810 and linkonce sections named .gnu.linkonce.<type>.<key>.
14811 Match like sections. LTO plugin sections are an exception.
14812 They are always named .gnu.linkonce.t.<key> and match either
14813 type of section. */
14814 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
14815 && ((flags
& SEC_GROUP
) != 0
14816 || strcmp (name
, l
->sec
->name
) == 0))
14817 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0
14818 || (sec
->owner
->flags
& BFD_PLUGIN
) != 0)
14820 /* The section has already been linked. See if we should
14821 issue a warning. */
14822 if (!_bfd_handle_already_linked (sec
, l
, info
))
14825 if (flags
& SEC_GROUP
)
14827 asection
*first
= elf_next_in_group (sec
);
14828 asection
*s
= first
;
14832 s
->output_section
= bfd_abs_section_ptr
;
14833 /* Record which group discards it. */
14834 s
->kept_section
= l
->sec
;
14835 s
= elf_next_in_group (s
);
14836 /* These lists are circular. */
14846 /* A single member comdat group section may be discarded by a
14847 linkonce section and vice versa. */
14848 if ((flags
& SEC_GROUP
) != 0)
14850 asection
*first
= elf_next_in_group (sec
);
14852 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
14853 /* Check this single member group against linkonce sections. */
14854 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14855 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14856 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
14858 first
->output_section
= bfd_abs_section_ptr
;
14859 first
->kept_section
= l
->sec
;
14860 sec
->output_section
= bfd_abs_section_ptr
;
14865 /* Check this linkonce section against single member groups. */
14866 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14867 if (l
->sec
->flags
& SEC_GROUP
)
14869 asection
*first
= elf_next_in_group (l
->sec
);
14872 && elf_next_in_group (first
) == first
14873 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
14875 sec
->output_section
= bfd_abs_section_ptr
;
14876 sec
->kept_section
= first
;
14881 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14882 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14883 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14884 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14885 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14886 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14887 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14888 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14889 The reverse order cannot happen as there is never a bfd with only the
14890 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14891 matter as here were are looking only for cross-bfd sections. */
14893 if ((flags
& SEC_GROUP
) == 0 && CONST_STRNEQ (name
, ".gnu.linkonce.r."))
14894 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14895 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14896 && CONST_STRNEQ (l
->sec
->name
, ".gnu.linkonce.t."))
14898 if (abfd
!= l
->sec
->owner
)
14899 sec
->output_section
= bfd_abs_section_ptr
;
14903 /* This is the first section with this name. Record it. */
14904 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
14905 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
14906 return sec
->output_section
== bfd_abs_section_ptr
;
14910 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
14912 return sym
->st_shndx
== SHN_COMMON
;
14916 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
14922 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
14924 return bfd_com_section_ptr
;
14928 _bfd_elf_default_got_elt_size (bfd
*abfd
,
14929 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
14930 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
14931 bfd
*ibfd ATTRIBUTE_UNUSED
,
14932 unsigned long symndx ATTRIBUTE_UNUSED
)
14934 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14935 return bed
->s
->arch_size
/ 8;
14938 /* Routines to support the creation of dynamic relocs. */
14940 /* Returns the name of the dynamic reloc section associated with SEC. */
14942 static const char *
14943 get_dynamic_reloc_section_name (bfd
* abfd
,
14945 bfd_boolean is_rela
)
14948 const char *old_name
= bfd_section_name (sec
);
14949 const char *prefix
= is_rela
? ".rela" : ".rel";
14951 if (old_name
== NULL
)
14954 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
14955 sprintf (name
, "%s%s", prefix
, old_name
);
14960 /* Returns the dynamic reloc section associated with SEC.
14961 If necessary compute the name of the dynamic reloc section based
14962 on SEC's name (looked up in ABFD's string table) and the setting
14966 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
14968 bfd_boolean is_rela
)
14970 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14972 if (reloc_sec
== NULL
)
14974 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14978 reloc_sec
= bfd_get_linker_section (abfd
, name
);
14980 if (reloc_sec
!= NULL
)
14981 elf_section_data (sec
)->sreloc
= reloc_sec
;
14988 /* Returns the dynamic reloc section associated with SEC. If the
14989 section does not exist it is created and attached to the DYNOBJ
14990 bfd and stored in the SRELOC field of SEC's elf_section_data
14993 ALIGNMENT is the alignment for the newly created section and
14994 IS_RELA defines whether the name should be .rela.<SEC's name>
14995 or .rel.<SEC's name>. The section name is looked up in the
14996 string table associated with ABFD. */
14999 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
15001 unsigned int alignment
,
15003 bfd_boolean is_rela
)
15005 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
15007 if (reloc_sec
== NULL
)
15009 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
15014 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
15016 if (reloc_sec
== NULL
)
15018 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
15019 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
15020 if ((sec
->flags
& SEC_ALLOC
) != 0)
15021 flags
|= SEC_ALLOC
| SEC_LOAD
;
15023 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
15024 if (reloc_sec
!= NULL
)
15026 /* _bfd_elf_get_sec_type_attr chooses a section type by
15027 name. Override as it may be wrong, eg. for a user
15028 section named "auto" we'll get ".relauto" which is
15029 seen to be a .rela section. */
15030 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
15031 if (!bfd_set_section_alignment (reloc_sec
, alignment
))
15036 elf_section_data (sec
)->sreloc
= reloc_sec
;
15042 /* Copy the ELF symbol type and other attributes for a linker script
15043 assignment from HSRC to HDEST. Generally this should be treated as
15044 if we found a strong non-dynamic definition for HDEST (except that
15045 ld ignores multiple definition errors). */
15047 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
15048 struct bfd_link_hash_entry
*hdest
,
15049 struct bfd_link_hash_entry
*hsrc
)
15051 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
15052 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
15053 Elf_Internal_Sym isym
;
15055 ehdest
->type
= ehsrc
->type
;
15056 ehdest
->target_internal
= ehsrc
->target_internal
;
15058 isym
.st_other
= ehsrc
->other
;
15059 elf_merge_st_other (abfd
, ehdest
, isym
.st_other
, NULL
, TRUE
, FALSE
);
15062 /* Append a RELA relocation REL to section S in BFD. */
15065 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15067 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15068 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
15069 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
15070 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
15073 /* Append a REL relocation REL to section S in BFD. */
15076 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15078 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15079 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
15080 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
15081 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);
15084 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15086 struct bfd_link_hash_entry
*
15087 bfd_elf_define_start_stop (struct bfd_link_info
*info
,
15088 const char *symbol
, asection
*sec
)
15090 struct elf_link_hash_entry
*h
;
15092 h
= elf_link_hash_lookup (elf_hash_table (info
), symbol
,
15093 FALSE
, FALSE
, TRUE
);
15094 /* NB: Common symbols will be turned into definition later. */
15096 && (h
->root
.type
== bfd_link_hash_undefined
15097 || h
->root
.type
== bfd_link_hash_undefweak
15098 || ((h
->ref_regular
|| h
->def_dynamic
)
15100 && h
->root
.type
!= bfd_link_hash_common
)))
15102 bfd_boolean was_dynamic
= h
->ref_dynamic
|| h
->def_dynamic
;
15103 h
->verinfo
.verdef
= NULL
;
15104 h
->root
.type
= bfd_link_hash_defined
;
15105 h
->root
.u
.def
.section
= sec
;
15106 h
->root
.u
.def
.value
= 0;
15107 h
->def_regular
= 1;
15108 h
->def_dynamic
= 0;
15110 h
->u2
.start_stop_section
= sec
;
15111 if (symbol
[0] == '.')
15113 /* .startof. and .sizeof. symbols are local. */
15114 const struct elf_backend_data
*bed
;
15115 bed
= get_elf_backend_data (info
->output_bfd
);
15116 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
15120 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
15121 h
->other
= ((h
->other
& ~ELF_ST_VISIBILITY (-1))
15122 | info
->start_stop_visibility
);
15124 bfd_elf_link_record_dynamic_symbol (info
, h
);
15131 /* Find dynamic relocs for H that apply to read-only sections. */
15134 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry
*h
)
15136 struct elf_dyn_relocs
*p
;
15138 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
15140 asection
*s
= p
->sec
->output_section
;
15142 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
15148 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15149 read-only sections. */
15152 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry
*h
, void *inf
)
15156 if (h
->root
.type
== bfd_link_hash_indirect
)
15159 sec
= _bfd_elf_readonly_dynrelocs (h
);
15162 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
15164 info
->flags
|= DF_TEXTREL
;
15165 /* xgettext:c-format */
15166 info
->callbacks
->minfo (_("%pB: dynamic relocation against `%pT' "
15167 "in read-only section `%pA'\n"),
15168 sec
->owner
, h
->root
.root
.string
, sec
);
15170 if (bfd_link_textrel_check (info
))
15171 /* xgettext:c-format */
15172 info
->callbacks
->einfo (_("%P: %pB: warning: relocation against `%s' "
15173 "in read-only section `%pA'\n"),
15174 sec
->owner
, h
->root
.root
.string
, sec
);
15176 /* Not an error, just cut short the traversal. */
15182 /* Add dynamic tags. */
15185 _bfd_elf_add_dynamic_tags (bfd
*output_bfd
, struct bfd_link_info
*info
,
15186 bfd_boolean need_dynamic_reloc
)
15188 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
15190 if (htab
->dynamic_sections_created
)
15192 /* Add some entries to the .dynamic section. We fill in the
15193 values later, in finish_dynamic_sections, but we must add
15194 the entries now so that we get the correct size for the
15195 .dynamic section. The DT_DEBUG entry is filled in by the
15196 dynamic linker and used by the debugger. */
15197 #define add_dynamic_entry(TAG, VAL) \
15198 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15200 const struct elf_backend_data
*bed
15201 = get_elf_backend_data (output_bfd
);
15203 if (bfd_link_executable (info
))
15205 if (!add_dynamic_entry (DT_DEBUG
, 0))
15209 if (htab
->dt_pltgot_required
|| htab
->splt
->size
!= 0)
15211 /* DT_PLTGOT is used by prelink even if there is no PLT
15213 if (!add_dynamic_entry (DT_PLTGOT
, 0))
15217 if (htab
->dt_jmprel_required
|| htab
->srelplt
->size
!= 0)
15219 if (!add_dynamic_entry (DT_PLTRELSZ
, 0)
15220 || !add_dynamic_entry (DT_PLTREL
,
15221 (bed
->rela_plts_and_copies_p
15222 ? DT_RELA
: DT_REL
))
15223 || !add_dynamic_entry (DT_JMPREL
, 0))
15227 if (htab
->tlsdesc_plt
15228 && (!add_dynamic_entry (DT_TLSDESC_PLT
, 0)
15229 || !add_dynamic_entry (DT_TLSDESC_GOT
, 0)))
15232 if (need_dynamic_reloc
)
15234 if (bed
->rela_plts_and_copies_p
)
15236 if (!add_dynamic_entry (DT_RELA
, 0)
15237 || !add_dynamic_entry (DT_RELASZ
, 0)
15238 || !add_dynamic_entry (DT_RELAENT
,
15239 bed
->s
->sizeof_rela
))
15244 if (!add_dynamic_entry (DT_REL
, 0)
15245 || !add_dynamic_entry (DT_RELSZ
, 0)
15246 || !add_dynamic_entry (DT_RELENT
,
15247 bed
->s
->sizeof_rel
))
15251 /* If any dynamic relocs apply to a read-only section,
15252 then we need a DT_TEXTREL entry. */
15253 if ((info
->flags
& DF_TEXTREL
) == 0)
15254 elf_link_hash_traverse (htab
, _bfd_elf_maybe_set_textrel
,
15257 if ((info
->flags
& DF_TEXTREL
) != 0)
15259 if (htab
->ifunc_resolvers
)
15260 info
->callbacks
->einfo
15261 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15262 "may result in a segfault at runtime; recompile with %s\n"),
15263 bfd_link_dll (info
) ? "-fPIC" : "-fPIE");
15265 if (!add_dynamic_entry (DT_TEXTREL
, 0))
15270 #undef add_dynamic_entry