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 const Elf_Internal_Sym
*isym
, 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
, isym
, definition
,
1022 unsigned symvis
= ELF_ST_VISIBILITY (isym
->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 (isym
->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
, 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 /* A reference to the SHORTNAME symbol from a dynamic library
2032 will be satisfied by the versioned symbol at runtime. In
2033 effect, we have a reference to the versioned symbol. */
2034 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2035 hi
->dynamic_def
|= ht
->dynamic_def
;
2037 /* See if the new flags lead us to realize that the symbol must
2043 if (! bfd_link_executable (info
)
2050 if (hi
->ref_regular
)
2056 /* We also need to define an indirection from the nondefault version
2060 len
= strlen (name
);
2061 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
2062 if (shortname
== NULL
)
2064 memcpy (shortname
, name
, shortlen
);
2065 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
2067 /* Once again, merge with any existing symbol. */
2068 type_change_ok
= FALSE
;
2069 size_change_ok
= FALSE
;
2071 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
2072 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
2073 &type_change_ok
, &size_change_ok
, &matched
))
2081 /* Here SHORTNAME is a versioned name, so we don't expect to see
2082 the type of override we do in the case above unless it is
2083 overridden by a versioned definition. */
2084 if (hi
->root
.type
!= bfd_link_hash_defined
2085 && hi
->root
.type
!= bfd_link_hash_defweak
)
2087 /* xgettext:c-format */
2088 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2094 if (! (_bfd_generic_link_add_one_symbol
2095 (info
, abfd
, shortname
, BSF_INDIRECT
,
2096 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
2098 hi
= (struct elf_link_hash_entry
*) bh
;
2100 /* If there is a duplicate definition somewhere, then HI may not
2101 point to an indirect symbol. We will have reported an error
2102 to the user in that case. */
2104 if (hi
->root
.type
== bfd_link_hash_indirect
)
2106 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
2107 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2108 hi
->dynamic_def
|= h
->dynamic_def
;
2110 /* See if the new flags lead us to realize that the symbol
2116 if (! bfd_link_executable (info
)
2122 if (hi
->ref_regular
)
2132 /* This routine is used to export all defined symbols into the dynamic
2133 symbol table. It is called via elf_link_hash_traverse. */
2136 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
2138 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2140 /* Ignore indirect symbols. These are added by the versioning code. */
2141 if (h
->root
.type
== bfd_link_hash_indirect
)
2144 /* Ignore this if we won't export it. */
2145 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2148 if (h
->dynindx
== -1
2149 && (h
->def_regular
|| h
->ref_regular
)
2150 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2151 h
->root
.root
.string
))
2153 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2163 /* Look through the symbols which are defined in other shared
2164 libraries and referenced here. Update the list of version
2165 dependencies. This will be put into the .gnu.version_r section.
2166 This function is called via elf_link_hash_traverse. */
2169 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2172 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2173 Elf_Internal_Verneed
*t
;
2174 Elf_Internal_Vernaux
*a
;
2177 /* We only care about symbols defined in shared objects with version
2182 || h
->verinfo
.verdef
== NULL
2183 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2184 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2187 /* See if we already know about this version. */
2188 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2192 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2195 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2196 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2202 /* This is a new version. Add it to tree we are building. */
2207 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2210 rinfo
->failed
= TRUE
;
2214 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2215 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2216 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2220 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2223 rinfo
->failed
= TRUE
;
2227 /* Note that we are copying a string pointer here, and testing it
2228 above. If bfd_elf_string_from_elf_section is ever changed to
2229 discard the string data when low in memory, this will have to be
2231 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2233 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2234 a
->vna_nextptr
= t
->vn_auxptr
;
2236 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2239 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2246 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2247 hidden. Set *T_P to NULL if there is no match. */
2250 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info
*info
,
2251 struct elf_link_hash_entry
*h
,
2252 const char *version_p
,
2253 struct bfd_elf_version_tree
**t_p
,
2256 struct bfd_elf_version_tree
*t
;
2258 /* Look for the version. If we find it, it is no longer weak. */
2259 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
2261 if (strcmp (t
->name
, version_p
) == 0)
2265 struct bfd_elf_version_expr
*d
;
2267 len
= version_p
- h
->root
.root
.string
;
2268 alc
= (char *) bfd_malloc (len
);
2271 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2272 alc
[len
- 1] = '\0';
2273 if (alc
[len
- 2] == ELF_VER_CHR
)
2274 alc
[len
- 2] = '\0';
2276 h
->verinfo
.vertree
= t
;
2280 if (t
->globals
.list
!= NULL
)
2281 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2283 /* See if there is anything to force this symbol to
2285 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2287 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2290 && ! info
->export_dynamic
)
2304 /* Return TRUE if the symbol H is hidden by version script. */
2307 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info
*info
,
2308 struct elf_link_hash_entry
*h
)
2311 bfd_boolean hide
= FALSE
;
2312 const struct elf_backend_data
*bed
2313 = get_elf_backend_data (info
->output_bfd
);
2315 /* Version script only hides symbols defined in regular objects. */
2316 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2319 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2320 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2322 struct bfd_elf_version_tree
*t
;
2325 if (*p
== ELF_VER_CHR
)
2329 && _bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
)
2333 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2338 /* If we don't have a version for this symbol, see if we can find
2340 if (h
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
2343 = bfd_find_version_for_sym (info
->version_info
,
2344 h
->root
.root
.string
, &hide
);
2345 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2347 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2355 /* Figure out appropriate versions for all the symbols. We may not
2356 have the version number script until we have read all of the input
2357 files, so until that point we don't know which symbols should be
2358 local. This function is called via elf_link_hash_traverse. */
2361 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2363 struct elf_info_failed
*sinfo
;
2364 struct bfd_link_info
*info
;
2365 const struct elf_backend_data
*bed
;
2366 struct elf_info_failed eif
;
2370 sinfo
= (struct elf_info_failed
*) data
;
2373 /* Fix the symbol flags. */
2376 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2379 sinfo
->failed
= TRUE
;
2383 bed
= get_elf_backend_data (info
->output_bfd
);
2385 /* We only need version numbers for symbols defined in regular
2387 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2389 /* Hide symbols defined in discarded input sections. */
2390 if ((h
->root
.type
== bfd_link_hash_defined
2391 || h
->root
.type
== bfd_link_hash_defweak
)
2392 && discarded_section (h
->root
.u
.def
.section
))
2393 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2398 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2399 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2401 struct bfd_elf_version_tree
*t
;
2404 if (*p
== ELF_VER_CHR
)
2407 /* If there is no version string, we can just return out. */
2411 if (!_bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
))
2413 sinfo
->failed
= TRUE
;
2418 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2420 /* If we are building an application, we need to create a
2421 version node for this version. */
2422 if (t
== NULL
&& bfd_link_executable (info
))
2424 struct bfd_elf_version_tree
**pp
;
2427 /* If we aren't going to export this symbol, we don't need
2428 to worry about it. */
2429 if (h
->dynindx
== -1)
2432 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2436 sinfo
->failed
= TRUE
;
2441 t
->name_indx
= (unsigned int) -1;
2445 /* Don't count anonymous version tag. */
2446 if (sinfo
->info
->version_info
!= NULL
2447 && sinfo
->info
->version_info
->vernum
== 0)
2449 for (pp
= &sinfo
->info
->version_info
;
2453 t
->vernum
= version_index
;
2457 h
->verinfo
.vertree
= t
;
2461 /* We could not find the version for a symbol when
2462 generating a shared archive. Return an error. */
2464 /* xgettext:c-format */
2465 (_("%pB: version node not found for symbol %s"),
2466 info
->output_bfd
, h
->root
.root
.string
);
2467 bfd_set_error (bfd_error_bad_value
);
2468 sinfo
->failed
= TRUE
;
2473 /* If we don't have a version for this symbol, see if we can find
2476 && h
->verinfo
.vertree
== NULL
2477 && sinfo
->info
->version_info
!= NULL
)
2480 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2481 h
->root
.root
.string
, &hide
);
2482 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2483 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2489 /* Read and swap the relocs from the section indicated by SHDR. This
2490 may be either a REL or a RELA section. The relocations are
2491 translated into RELA relocations and stored in INTERNAL_RELOCS,
2492 which should have already been allocated to contain enough space.
2493 The EXTERNAL_RELOCS are a buffer where the external form of the
2494 relocations should be stored.
2496 Returns FALSE if something goes wrong. */
2499 elf_link_read_relocs_from_section (bfd
*abfd
,
2501 Elf_Internal_Shdr
*shdr
,
2502 void *external_relocs
,
2503 Elf_Internal_Rela
*internal_relocs
)
2505 const struct elf_backend_data
*bed
;
2506 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2507 const bfd_byte
*erela
;
2508 const bfd_byte
*erelaend
;
2509 Elf_Internal_Rela
*irela
;
2510 Elf_Internal_Shdr
*symtab_hdr
;
2513 /* Position ourselves at the start of the section. */
2514 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2517 /* Read the relocations. */
2518 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2521 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2522 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2524 bed
= get_elf_backend_data (abfd
);
2526 /* Convert the external relocations to the internal format. */
2527 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2528 swap_in
= bed
->s
->swap_reloc_in
;
2529 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2530 swap_in
= bed
->s
->swap_reloca_in
;
2533 bfd_set_error (bfd_error_wrong_format
);
2537 erela
= (const bfd_byte
*) external_relocs
;
2538 /* Setting erelaend like this and comparing with <= handles case of
2539 a fuzzed object with sh_size not a multiple of sh_entsize. */
2540 erelaend
= erela
+ shdr
->sh_size
- shdr
->sh_entsize
;
2541 irela
= internal_relocs
;
2542 while (erela
<= erelaend
)
2546 (*swap_in
) (abfd
, erela
, irela
);
2547 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2548 if (bed
->s
->arch_size
== 64)
2552 if ((size_t) r_symndx
>= nsyms
)
2555 /* xgettext:c-format */
2556 (_("%pB: bad reloc symbol index (%#" PRIx64
" >= %#lx)"
2557 " for offset %#" PRIx64
" in section `%pA'"),
2558 abfd
, (uint64_t) r_symndx
, (unsigned long) nsyms
,
2559 (uint64_t) irela
->r_offset
, sec
);
2560 bfd_set_error (bfd_error_bad_value
);
2564 else if (r_symndx
!= STN_UNDEF
)
2567 /* xgettext:c-format */
2568 (_("%pB: non-zero symbol index (%#" PRIx64
")"
2569 " for offset %#" PRIx64
" in section `%pA'"
2570 " when the object file has no symbol table"),
2571 abfd
, (uint64_t) r_symndx
,
2572 (uint64_t) irela
->r_offset
, sec
);
2573 bfd_set_error (bfd_error_bad_value
);
2576 irela
+= bed
->s
->int_rels_per_ext_rel
;
2577 erela
+= shdr
->sh_entsize
;
2583 /* Read and swap the relocs for a section O. They may have been
2584 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2585 not NULL, they are used as buffers to read into. They are known to
2586 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2587 the return value is allocated using either malloc or bfd_alloc,
2588 according to the KEEP_MEMORY argument. If O has two relocation
2589 sections (both REL and RELA relocations), then the REL_HDR
2590 relocations will appear first in INTERNAL_RELOCS, followed by the
2591 RELA_HDR relocations. */
2594 _bfd_elf_link_read_relocs (bfd
*abfd
,
2596 void *external_relocs
,
2597 Elf_Internal_Rela
*internal_relocs
,
2598 bfd_boolean keep_memory
)
2600 void *alloc1
= NULL
;
2601 Elf_Internal_Rela
*alloc2
= NULL
;
2602 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2603 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2604 Elf_Internal_Rela
*internal_rela_relocs
;
2606 if (esdo
->relocs
!= NULL
)
2607 return esdo
->relocs
;
2609 if (o
->reloc_count
== 0)
2612 if (internal_relocs
== NULL
)
2616 size
= (bfd_size_type
) o
->reloc_count
* sizeof (Elf_Internal_Rela
);
2618 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2620 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2621 if (internal_relocs
== NULL
)
2625 if (external_relocs
== NULL
)
2627 bfd_size_type size
= 0;
2630 size
+= esdo
->rel
.hdr
->sh_size
;
2632 size
+= esdo
->rela
.hdr
->sh_size
;
2634 alloc1
= bfd_malloc (size
);
2637 external_relocs
= alloc1
;
2640 internal_rela_relocs
= internal_relocs
;
2643 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2647 external_relocs
= (((bfd_byte
*) external_relocs
)
2648 + esdo
->rel
.hdr
->sh_size
);
2649 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2650 * bed
->s
->int_rels_per_ext_rel
);
2654 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2656 internal_rela_relocs
)))
2659 /* Cache the results for next time, if we can. */
2661 esdo
->relocs
= internal_relocs
;
2665 /* Don't free alloc2, since if it was allocated we are passing it
2666 back (under the name of internal_relocs). */
2668 return internal_relocs
;
2675 bfd_release (abfd
, alloc2
);
2682 /* Compute the size of, and allocate space for, REL_HDR which is the
2683 section header for a section containing relocations for O. */
2686 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2687 struct bfd_elf_section_reloc_data
*reldata
)
2689 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2691 /* That allows us to calculate the size of the section. */
2692 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2694 /* The contents field must last into write_object_contents, so we
2695 allocate it with bfd_alloc rather than malloc. Also since we
2696 cannot be sure that the contents will actually be filled in,
2697 we zero the allocated space. */
2698 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2699 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2702 if (reldata
->hashes
== NULL
&& reldata
->count
)
2704 struct elf_link_hash_entry
**p
;
2706 p
= ((struct elf_link_hash_entry
**)
2707 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2711 reldata
->hashes
= p
;
2717 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2718 originated from the section given by INPUT_REL_HDR) to the
2722 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2723 asection
*input_section
,
2724 Elf_Internal_Shdr
*input_rel_hdr
,
2725 Elf_Internal_Rela
*internal_relocs
,
2726 struct elf_link_hash_entry
**rel_hash
2729 Elf_Internal_Rela
*irela
;
2730 Elf_Internal_Rela
*irelaend
;
2732 struct bfd_elf_section_reloc_data
*output_reldata
;
2733 asection
*output_section
;
2734 const struct elf_backend_data
*bed
;
2735 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2736 struct bfd_elf_section_data
*esdo
;
2738 output_section
= input_section
->output_section
;
2740 bed
= get_elf_backend_data (output_bfd
);
2741 esdo
= elf_section_data (output_section
);
2742 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2744 output_reldata
= &esdo
->rel
;
2745 swap_out
= bed
->s
->swap_reloc_out
;
2747 else if (esdo
->rela
.hdr
2748 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2750 output_reldata
= &esdo
->rela
;
2751 swap_out
= bed
->s
->swap_reloca_out
;
2756 /* xgettext:c-format */
2757 (_("%pB: relocation size mismatch in %pB section %pA"),
2758 output_bfd
, input_section
->owner
, input_section
);
2759 bfd_set_error (bfd_error_wrong_format
);
2763 erel
= output_reldata
->hdr
->contents
;
2764 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2765 irela
= internal_relocs
;
2766 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2767 * bed
->s
->int_rels_per_ext_rel
);
2768 while (irela
< irelaend
)
2770 (*swap_out
) (output_bfd
, irela
, erel
);
2771 irela
+= bed
->s
->int_rels_per_ext_rel
;
2772 erel
+= input_rel_hdr
->sh_entsize
;
2775 /* Bump the counter, so that we know where to add the next set of
2777 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2782 /* Make weak undefined symbols in PIE dynamic. */
2785 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2786 struct elf_link_hash_entry
*h
)
2788 if (bfd_link_pie (info
)
2790 && h
->root
.type
== bfd_link_hash_undefweak
)
2791 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2796 /* Fix up the flags for a symbol. This handles various cases which
2797 can only be fixed after all the input files are seen. This is
2798 currently called by both adjust_dynamic_symbol and
2799 assign_sym_version, which is unnecessary but perhaps more robust in
2800 the face of future changes. */
2803 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2804 struct elf_info_failed
*eif
)
2806 const struct elf_backend_data
*bed
;
2808 /* If this symbol was mentioned in a non-ELF file, try to set
2809 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2810 permit a non-ELF file to correctly refer to a symbol defined in
2811 an ELF dynamic object. */
2814 while (h
->root
.type
== bfd_link_hash_indirect
)
2815 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2817 if (h
->root
.type
!= bfd_link_hash_defined
2818 && h
->root
.type
!= bfd_link_hash_defweak
)
2821 h
->ref_regular_nonweak
= 1;
2825 if (h
->root
.u
.def
.section
->owner
!= NULL
2826 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2827 == bfd_target_elf_flavour
))
2830 h
->ref_regular_nonweak
= 1;
2836 if (h
->dynindx
== -1
2840 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2849 /* Unfortunately, NON_ELF is only correct if the symbol
2850 was first seen in a non-ELF file. Fortunately, if the symbol
2851 was first seen in an ELF file, we're probably OK unless the
2852 symbol was defined in a non-ELF file. Catch that case here.
2853 FIXME: We're still in trouble if the symbol was first seen in
2854 a dynamic object, and then later in a non-ELF regular object. */
2855 if ((h
->root
.type
== bfd_link_hash_defined
2856 || h
->root
.type
== bfd_link_hash_defweak
)
2858 && (h
->root
.u
.def
.section
->owner
!= NULL
2859 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2860 != bfd_target_elf_flavour
)
2861 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2862 && !h
->def_dynamic
)))
2866 /* Backend specific symbol fixup. */
2867 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2868 if (bed
->elf_backend_fixup_symbol
2869 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2872 /* If this is a final link, and the symbol was defined as a common
2873 symbol in a regular object file, and there was no definition in
2874 any dynamic object, then the linker will have allocated space for
2875 the symbol in a common section but the DEF_REGULAR
2876 flag will not have been set. */
2877 if (h
->root
.type
== bfd_link_hash_defined
2881 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
2884 /* Symbols defined in discarded sections shouldn't be dynamic. */
2885 if (h
->root
.type
== bfd_link_hash_undefined
&& h
->indx
== -3)
2886 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2888 /* If a weak undefined symbol has non-default visibility, we also
2889 hide it from the dynamic linker. */
2890 else if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2891 && h
->root
.type
== bfd_link_hash_undefweak
)
2892 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2894 /* A hidden versioned symbol in executable should be forced local if
2895 it is is locally defined, not referenced by shared library and not
2897 else if (bfd_link_executable (eif
->info
)
2898 && h
->versioned
== versioned_hidden
2899 && !eif
->info
->export_dynamic
2903 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2905 /* If -Bsymbolic was used (which means to bind references to global
2906 symbols to the definition within the shared object), and this
2907 symbol was defined in a regular object, then it actually doesn't
2908 need a PLT entry. Likewise, if the symbol has non-default
2909 visibility. If the symbol has hidden or internal visibility, we
2910 will force it local. */
2911 else if (h
->needs_plt
2912 && bfd_link_pic (eif
->info
)
2913 && is_elf_hash_table (eif
->info
->hash
)
2914 && (SYMBOLIC_BIND (eif
->info
, h
)
2915 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2918 bfd_boolean force_local
;
2920 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2921 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2922 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2925 /* If this is a weak defined symbol in a dynamic object, and we know
2926 the real definition in the dynamic object, copy interesting flags
2927 over to the real definition. */
2928 if (h
->is_weakalias
)
2930 struct elf_link_hash_entry
*def
= weakdef (h
);
2932 /* If the real definition is defined by a regular object file,
2933 don't do anything special. See the longer description in
2934 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2935 bfd_link_hash_defined as it was when put on the alias list
2936 then it must have originally been a versioned symbol (for
2937 which a non-versioned indirect symbol is created) and later
2938 a definition for the non-versioned symbol is found. In that
2939 case the indirection is flipped with the versioned symbol
2940 becoming an indirect pointing at the non-versioned symbol.
2941 Thus, not an alias any more. */
2942 if (def
->def_regular
2943 || def
->root
.type
!= bfd_link_hash_defined
)
2946 while ((h
= h
->u
.alias
) != def
)
2947 h
->is_weakalias
= 0;
2951 while (h
->root
.type
== bfd_link_hash_indirect
)
2952 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2953 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2954 || h
->root
.type
== bfd_link_hash_defweak
);
2955 BFD_ASSERT (def
->def_dynamic
);
2956 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, def
, h
);
2963 /* Make the backend pick a good value for a dynamic symbol. This is
2964 called via elf_link_hash_traverse, and also calls itself
2968 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2970 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2971 struct elf_link_hash_table
*htab
;
2972 const struct elf_backend_data
*bed
;
2974 if (! is_elf_hash_table (eif
->info
->hash
))
2977 /* Ignore indirect symbols. These are added by the versioning code. */
2978 if (h
->root
.type
== bfd_link_hash_indirect
)
2981 /* Fix the symbol flags. */
2982 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2985 htab
= elf_hash_table (eif
->info
);
2986 bed
= get_elf_backend_data (htab
->dynobj
);
2988 if (h
->root
.type
== bfd_link_hash_undefweak
)
2990 if (eif
->info
->dynamic_undefined_weak
== 0)
2991 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2992 else if (eif
->info
->dynamic_undefined_weak
> 0
2994 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2995 && !bfd_hide_sym_by_version (eif
->info
->version_info
,
2996 h
->root
.root
.string
))
2998 if (!bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3006 /* If this symbol does not require a PLT entry, and it is not
3007 defined by a dynamic object, or is not referenced by a regular
3008 object, ignore it. We do have to handle a weak defined symbol,
3009 even if no regular object refers to it, if we decided to add it
3010 to the dynamic symbol table. FIXME: Do we normally need to worry
3011 about symbols which are defined by one dynamic object and
3012 referenced by another one? */
3014 && h
->type
!= STT_GNU_IFUNC
3018 && (!h
->is_weakalias
|| weakdef (h
)->dynindx
== -1))))
3020 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
3024 /* If we've already adjusted this symbol, don't do it again. This
3025 can happen via a recursive call. */
3026 if (h
->dynamic_adjusted
)
3029 /* Don't look at this symbol again. Note that we must set this
3030 after checking the above conditions, because we may look at a
3031 symbol once, decide not to do anything, and then get called
3032 recursively later after REF_REGULAR is set below. */
3033 h
->dynamic_adjusted
= 1;
3035 /* If this is a weak definition, and we know a real definition, and
3036 the real symbol is not itself defined by a regular object file,
3037 then get a good value for the real definition. We handle the
3038 real symbol first, for the convenience of the backend routine.
3040 Note that there is a confusing case here. If the real definition
3041 is defined by a regular object file, we don't get the real symbol
3042 from the dynamic object, but we do get the weak symbol. If the
3043 processor backend uses a COPY reloc, then if some routine in the
3044 dynamic object changes the real symbol, we will not see that
3045 change in the corresponding weak symbol. This is the way other
3046 ELF linkers work as well, and seems to be a result of the shared
3049 I will clarify this issue. Most SVR4 shared libraries define the
3050 variable _timezone and define timezone as a weak synonym. The
3051 tzset call changes _timezone. If you write
3052 extern int timezone;
3054 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3055 you might expect that, since timezone is a synonym for _timezone,
3056 the same number will print both times. However, if the processor
3057 backend uses a COPY reloc, then actually timezone will be copied
3058 into your process image, and, since you define _timezone
3059 yourself, _timezone will not. Thus timezone and _timezone will
3060 wind up at different memory locations. The tzset call will set
3061 _timezone, leaving timezone unchanged. */
3063 if (h
->is_weakalias
)
3065 struct elf_link_hash_entry
*def
= weakdef (h
);
3067 /* If we get to this point, there is an implicit reference to
3068 the alias by a regular object file via the weak symbol H. */
3069 def
->ref_regular
= 1;
3071 /* Ensure that the backend adjust_dynamic_symbol function sees
3072 the strong alias before H by recursively calling ourselves. */
3073 if (!_bfd_elf_adjust_dynamic_symbol (def
, eif
))
3077 /* If a symbol has no type and no size and does not require a PLT
3078 entry, then we are probably about to do the wrong thing here: we
3079 are probably going to create a COPY reloc for an empty object.
3080 This case can arise when a shared object is built with assembly
3081 code, and the assembly code fails to set the symbol type. */
3083 && h
->type
== STT_NOTYPE
3086 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3087 h
->root
.root
.string
);
3089 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3098 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3102 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
3103 struct elf_link_hash_entry
*h
,
3106 unsigned int power_of_two
;
3108 asection
*sec
= h
->root
.u
.def
.section
;
3110 /* The section alignment of the definition is the maximum alignment
3111 requirement of symbols defined in the section. Since we don't
3112 know the symbol alignment requirement, we start with the
3113 maximum alignment and check low bits of the symbol address
3114 for the minimum alignment. */
3115 power_of_two
= bfd_section_alignment (sec
);
3116 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
3117 while ((h
->root
.u
.def
.value
& mask
) != 0)
3123 if (power_of_two
> bfd_section_alignment (dynbss
))
3125 /* Adjust the section alignment if needed. */
3126 if (!bfd_set_section_alignment (dynbss
, power_of_two
))
3130 /* We make sure that the symbol will be aligned properly. */
3131 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
3133 /* Define the symbol as being at this point in DYNBSS. */
3134 h
->root
.u
.def
.section
= dynbss
;
3135 h
->root
.u
.def
.value
= dynbss
->size
;
3137 /* Increment the size of DYNBSS to make room for the symbol. */
3138 dynbss
->size
+= h
->size
;
3140 /* No error if extern_protected_data is true. */
3141 if (h
->protected_def
3142 && (!info
->extern_protected_data
3143 || (info
->extern_protected_data
< 0
3144 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
3145 info
->callbacks
->einfo
3146 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3147 h
->root
.root
.string
);
3152 /* Adjust all external symbols pointing into SEC_MERGE sections
3153 to reflect the object merging within the sections. */
3156 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
3160 if ((h
->root
.type
== bfd_link_hash_defined
3161 || h
->root
.type
== bfd_link_hash_defweak
)
3162 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
3163 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
3165 bfd
*output_bfd
= (bfd
*) data
;
3167 h
->root
.u
.def
.value
=
3168 _bfd_merged_section_offset (output_bfd
,
3169 &h
->root
.u
.def
.section
,
3170 elf_section_data (sec
)->sec_info
,
3171 h
->root
.u
.def
.value
);
3177 /* Returns false if the symbol referred to by H should be considered
3178 to resolve local to the current module, and true if it should be
3179 considered to bind dynamically. */
3182 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
3183 struct bfd_link_info
*info
,
3184 bfd_boolean not_local_protected
)
3186 bfd_boolean binding_stays_local_p
;
3187 const struct elf_backend_data
*bed
;
3188 struct elf_link_hash_table
*hash_table
;
3193 while (h
->root
.type
== bfd_link_hash_indirect
3194 || h
->root
.type
== bfd_link_hash_warning
)
3195 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3197 /* If it was forced local, then clearly it's not dynamic. */
3198 if (h
->dynindx
== -1)
3200 if (h
->forced_local
)
3203 /* Identify the cases where name binding rules say that a
3204 visible symbol resolves locally. */
3205 binding_stays_local_p
= (bfd_link_executable (info
)
3206 || SYMBOLIC_BIND (info
, h
));
3208 switch (ELF_ST_VISIBILITY (h
->other
))
3215 hash_table
= elf_hash_table (info
);
3216 if (!is_elf_hash_table (hash_table
))
3219 bed
= get_elf_backend_data (hash_table
->dynobj
);
3221 /* Proper resolution for function pointer equality may require
3222 that these symbols perhaps be resolved dynamically, even though
3223 we should be resolving them to the current module. */
3224 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
3225 binding_stays_local_p
= TRUE
;
3232 /* If it isn't defined locally, then clearly it's dynamic. */
3233 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
3236 /* Otherwise, the symbol is dynamic if binding rules don't tell
3237 us that it remains local. */
3238 return !binding_stays_local_p
;
3241 /* Return true if the symbol referred to by H should be considered
3242 to resolve local to the current module, and false otherwise. Differs
3243 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3244 undefined symbols. The two functions are virtually identical except
3245 for the place where dynindx == -1 is tested. If that test is true,
3246 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3247 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3249 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3250 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3251 treatment of undefined weak symbols. For those that do not make
3252 undefined weak symbols dynamic, both functions may return false. */
3255 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3256 struct bfd_link_info
*info
,
3257 bfd_boolean local_protected
)
3259 const struct elf_backend_data
*bed
;
3260 struct elf_link_hash_table
*hash_table
;
3262 /* If it's a local sym, of course we resolve locally. */
3266 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3267 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3268 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3271 /* Forced local symbols resolve locally. */
3272 if (h
->forced_local
)
3275 /* Common symbols that become definitions don't get the DEF_REGULAR
3276 flag set, so test it first, and don't bail out. */
3277 if (ELF_COMMON_DEF_P (h
))
3279 /* If we don't have a definition in a regular file, then we can't
3280 resolve locally. The sym is either undefined or dynamic. */
3281 else if (!h
->def_regular
)
3284 /* Non-dynamic symbols resolve locally. */
3285 if (h
->dynindx
== -1)
3288 /* At this point, we know the symbol is defined and dynamic. In an
3289 executable it must resolve locally, likewise when building symbolic
3290 shared libraries. */
3291 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3294 /* Now deal with defined dynamic symbols in shared libraries. Ones
3295 with default visibility might not resolve locally. */
3296 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3299 hash_table
= elf_hash_table (info
);
3300 if (!is_elf_hash_table (hash_table
))
3303 bed
= get_elf_backend_data (hash_table
->dynobj
);
3305 /* If extern_protected_data is false, STV_PROTECTED non-function
3306 symbols are local. */
3307 if ((!info
->extern_protected_data
3308 || (info
->extern_protected_data
< 0
3309 && !bed
->extern_protected_data
))
3310 && !bed
->is_function_type (h
->type
))
3313 /* Function pointer equality tests may require that STV_PROTECTED
3314 symbols be treated as dynamic symbols. If the address of a
3315 function not defined in an executable is set to that function's
3316 plt entry in the executable, then the address of the function in
3317 a shared library must also be the plt entry in the executable. */
3318 return local_protected
;
3321 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3322 aligned. Returns the first TLS output section. */
3324 struct bfd_section
*
3325 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3327 struct bfd_section
*sec
, *tls
;
3328 unsigned int align
= 0;
3330 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3331 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3335 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3336 if (sec
->alignment_power
> align
)
3337 align
= sec
->alignment_power
;
3339 elf_hash_table (info
)->tls_sec
= tls
;
3341 /* Ensure the alignment of the first section (usually .tdata) is the largest
3342 alignment, so that the tls segment starts aligned. */
3344 tls
->alignment_power
= align
;
3349 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3351 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3352 Elf_Internal_Sym
*sym
)
3354 const struct elf_backend_data
*bed
;
3356 /* Local symbols do not count, but target specific ones might. */
3357 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3358 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3361 bed
= get_elf_backend_data (abfd
);
3362 /* Function symbols do not count. */
3363 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3366 /* If the section is undefined, then so is the symbol. */
3367 if (sym
->st_shndx
== SHN_UNDEF
)
3370 /* If the symbol is defined in the common section, then
3371 it is a common definition and so does not count. */
3372 if (bed
->common_definition (sym
))
3375 /* If the symbol is in a target specific section then we
3376 must rely upon the backend to tell us what it is. */
3377 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3378 /* FIXME - this function is not coded yet:
3380 return _bfd_is_global_symbol_definition (abfd, sym);
3382 Instead for now assume that the definition is not global,
3383 Even if this is wrong, at least the linker will behave
3384 in the same way that it used to do. */
3390 /* Search the symbol table of the archive element of the archive ABFD
3391 whose archive map contains a mention of SYMDEF, and determine if
3392 the symbol is defined in this element. */
3394 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3396 Elf_Internal_Shdr
* hdr
;
3400 Elf_Internal_Sym
*isymbuf
;
3401 Elf_Internal_Sym
*isym
;
3402 Elf_Internal_Sym
*isymend
;
3405 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
3409 if (! bfd_check_format (abfd
, bfd_object
))
3412 /* Select the appropriate symbol table. If we don't know if the
3413 object file is an IR object, give linker LTO plugin a chance to
3414 get the correct symbol table. */
3415 if (abfd
->plugin_format
== bfd_plugin_yes
3416 #if BFD_SUPPORTS_PLUGINS
3417 || (abfd
->plugin_format
== bfd_plugin_unknown
3418 && bfd_link_plugin_object_p (abfd
))
3422 /* Use the IR symbol table if the object has been claimed by
3424 abfd
= abfd
->plugin_dummy_bfd
;
3425 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3427 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3428 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3430 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3432 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3434 /* The sh_info field of the symtab header tells us where the
3435 external symbols start. We don't care about the local symbols. */
3436 if (elf_bad_symtab (abfd
))
3438 extsymcount
= symcount
;
3443 extsymcount
= symcount
- hdr
->sh_info
;
3444 extsymoff
= hdr
->sh_info
;
3447 if (extsymcount
== 0)
3450 /* Read in the symbol table. */
3451 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3453 if (isymbuf
== NULL
)
3456 /* Scan the symbol table looking for SYMDEF. */
3458 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3462 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3467 if (strcmp (name
, symdef
->name
) == 0)
3469 result
= is_global_data_symbol_definition (abfd
, isym
);
3479 /* Add an entry to the .dynamic table. */
3482 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3486 struct elf_link_hash_table
*hash_table
;
3487 const struct elf_backend_data
*bed
;
3489 bfd_size_type newsize
;
3490 bfd_byte
*newcontents
;
3491 Elf_Internal_Dyn dyn
;
3493 hash_table
= elf_hash_table (info
);
3494 if (! is_elf_hash_table (hash_table
))
3497 if (tag
== DT_RELA
|| tag
== DT_REL
)
3498 hash_table
->dynamic_relocs
= TRUE
;
3500 bed
= get_elf_backend_data (hash_table
->dynobj
);
3501 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3502 BFD_ASSERT (s
!= NULL
);
3504 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3505 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3506 if (newcontents
== NULL
)
3510 dyn
.d_un
.d_val
= val
;
3511 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3514 s
->contents
= newcontents
;
3519 /* Strip zero-sized dynamic sections. */
3522 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info
*info
)
3524 struct elf_link_hash_table
*hash_table
;
3525 const struct elf_backend_data
*bed
;
3526 asection
*s
, *sdynamic
, **pp
;
3527 asection
*rela_dyn
, *rel_dyn
;
3528 Elf_Internal_Dyn dyn
;
3529 bfd_byte
*extdyn
, *next
;
3530 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
3531 bfd_boolean strip_zero_sized
;
3532 bfd_boolean strip_zero_sized_plt
;
3534 if (bfd_link_relocatable (info
))
3537 hash_table
= elf_hash_table (info
);
3538 if (!is_elf_hash_table (hash_table
))
3541 if (!hash_table
->dynobj
)
3544 sdynamic
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3548 bed
= get_elf_backend_data (hash_table
->dynobj
);
3549 swap_dyn_in
= bed
->s
->swap_dyn_in
;
3551 strip_zero_sized
= FALSE
;
3552 strip_zero_sized_plt
= FALSE
;
3554 /* Strip zero-sized dynamic sections. */
3555 rela_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rela.dyn");
3556 rel_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rel.dyn");
3557 for (pp
= &info
->output_bfd
->sections
; (s
= *pp
) != NULL
;)
3561 || s
== hash_table
->srelplt
->output_section
3562 || s
== hash_table
->splt
->output_section
))
3565 info
->output_bfd
->section_count
--;
3566 strip_zero_sized
= TRUE
;
3571 else if (s
== hash_table
->splt
->output_section
)
3573 s
= hash_table
->splt
;
3574 strip_zero_sized_plt
= TRUE
;
3577 s
= hash_table
->srelplt
;
3578 s
->flags
|= SEC_EXCLUDE
;
3579 s
->output_section
= bfd_abs_section_ptr
;
3584 if (strip_zero_sized_plt
)
3585 for (extdyn
= sdynamic
->contents
;
3586 extdyn
< sdynamic
->contents
+ sdynamic
->size
;
3589 next
= extdyn
+ bed
->s
->sizeof_dyn
;
3590 swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3598 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3599 the procedure linkage table (the .plt section) has been
3601 memmove (extdyn
, next
,
3602 sdynamic
->size
- (next
- sdynamic
->contents
));
3607 if (strip_zero_sized
)
3609 /* Regenerate program headers. */
3610 elf_seg_map (info
->output_bfd
) = NULL
;
3611 return _bfd_elf_map_sections_to_segments (info
->output_bfd
, info
);
3617 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3618 1 if a DT_NEEDED tag already exists, and 0 on success. */
3621 bfd_elf_add_dt_needed_tag (bfd
*abfd
, struct bfd_link_info
*info
)
3623 struct elf_link_hash_table
*hash_table
;
3627 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3630 hash_table
= elf_hash_table (info
);
3631 soname
= elf_dt_name (abfd
);
3632 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3633 if (strindex
== (size_t) -1)
3636 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3639 const struct elf_backend_data
*bed
;
3642 bed
= get_elf_backend_data (hash_table
->dynobj
);
3643 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3645 for (extdyn
= sdyn
->contents
;
3646 extdyn
< sdyn
->contents
+ sdyn
->size
;
3647 extdyn
+= bed
->s
->sizeof_dyn
)
3649 Elf_Internal_Dyn dyn
;
3651 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3652 if (dyn
.d_tag
== DT_NEEDED
3653 && dyn
.d_un
.d_val
== strindex
)
3655 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3661 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3664 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3670 /* Return true if SONAME is on the needed list between NEEDED and STOP
3671 (or the end of list if STOP is NULL), and needed by a library that
3675 on_needed_list (const char *soname
,
3676 struct bfd_link_needed_list
*needed
,
3677 struct bfd_link_needed_list
*stop
)
3679 struct bfd_link_needed_list
*look
;
3680 for (look
= needed
; look
!= stop
; look
= look
->next
)
3681 if (strcmp (soname
, look
->name
) == 0
3682 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3683 /* If needed by a library that itself is not directly
3684 needed, recursively check whether that library is
3685 indirectly needed. Since we add DT_NEEDED entries to
3686 the end of the list, library dependencies appear after
3687 the library. Therefore search prior to the current
3688 LOOK, preventing possible infinite recursion. */
3689 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3695 /* Sort symbol by value, section, size, and type. */
3697 elf_sort_symbol (const void *arg1
, const void *arg2
)
3699 const struct elf_link_hash_entry
*h1
;
3700 const struct elf_link_hash_entry
*h2
;
3701 bfd_signed_vma vdiff
;
3706 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3707 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3708 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3710 return vdiff
> 0 ? 1 : -1;
3712 sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3716 /* Sort so that sized symbols are selected over zero size symbols. */
3717 vdiff
= h1
->size
- h2
->size
;
3719 return vdiff
> 0 ? 1 : -1;
3721 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3722 if (h1
->type
!= h2
->type
)
3723 return h1
->type
- h2
->type
;
3725 /* If symbols are properly sized and typed, and multiple strong
3726 aliases are not defined in a shared library by the user we
3727 shouldn't get here. Unfortunately linker script symbols like
3728 __bss_start sometimes match a user symbol defined at the start of
3729 .bss without proper size and type. We'd like to preference the
3730 user symbol over reserved system symbols. Sort on leading
3732 n1
= h1
->root
.root
.string
;
3733 n2
= h2
->root
.root
.string
;
3746 /* Final sort on name selects user symbols like '_u' over reserved
3747 system symbols like '_Z' and also will avoid qsort instability. */
3751 /* This function is used to adjust offsets into .dynstr for
3752 dynamic symbols. This is called via elf_link_hash_traverse. */
3755 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3757 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3759 if (h
->dynindx
!= -1)
3760 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3764 /* Assign string offsets in .dynstr, update all structures referencing
3768 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3770 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3771 struct elf_link_local_dynamic_entry
*entry
;
3772 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3773 bfd
*dynobj
= hash_table
->dynobj
;
3776 const struct elf_backend_data
*bed
;
3779 _bfd_elf_strtab_finalize (dynstr
);
3780 size
= _bfd_elf_strtab_size (dynstr
);
3782 bed
= get_elf_backend_data (dynobj
);
3783 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3784 BFD_ASSERT (sdyn
!= NULL
);
3786 /* Update all .dynamic entries referencing .dynstr strings. */
3787 for (extdyn
= sdyn
->contents
;
3788 extdyn
< sdyn
->contents
+ sdyn
->size
;
3789 extdyn
+= bed
->s
->sizeof_dyn
)
3791 Elf_Internal_Dyn dyn
;
3793 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3797 dyn
.d_un
.d_val
= size
;
3807 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3812 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3815 /* Now update local dynamic symbols. */
3816 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3817 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3818 entry
->isym
.st_name
);
3820 /* And the rest of dynamic symbols. */
3821 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3823 /* Adjust version definitions. */
3824 if (elf_tdata (output_bfd
)->cverdefs
)
3829 Elf_Internal_Verdef def
;
3830 Elf_Internal_Verdaux defaux
;
3832 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3836 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3838 p
+= sizeof (Elf_External_Verdef
);
3839 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3841 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3843 _bfd_elf_swap_verdaux_in (output_bfd
,
3844 (Elf_External_Verdaux
*) p
, &defaux
);
3845 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3847 _bfd_elf_swap_verdaux_out (output_bfd
,
3848 &defaux
, (Elf_External_Verdaux
*) p
);
3849 p
+= sizeof (Elf_External_Verdaux
);
3852 while (def
.vd_next
);
3855 /* Adjust version references. */
3856 if (elf_tdata (output_bfd
)->verref
)
3861 Elf_Internal_Verneed need
;
3862 Elf_Internal_Vernaux needaux
;
3864 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
3868 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3870 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3871 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3872 (Elf_External_Verneed
*) p
);
3873 p
+= sizeof (Elf_External_Verneed
);
3874 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3876 _bfd_elf_swap_vernaux_in (output_bfd
,
3877 (Elf_External_Vernaux
*) p
, &needaux
);
3878 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3880 _bfd_elf_swap_vernaux_out (output_bfd
,
3882 (Elf_External_Vernaux
*) p
);
3883 p
+= sizeof (Elf_External_Vernaux
);
3886 while (need
.vn_next
);
3892 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3893 The default is to only match when the INPUT and OUTPUT are exactly
3897 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3898 const bfd_target
*output
)
3900 return input
== output
;
3903 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3904 This version is used when different targets for the same architecture
3905 are virtually identical. */
3908 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3909 const bfd_target
*output
)
3911 const struct elf_backend_data
*obed
, *ibed
;
3913 if (input
== output
)
3916 ibed
= xvec_get_elf_backend_data (input
);
3917 obed
= xvec_get_elf_backend_data (output
);
3919 if (ibed
->arch
!= obed
->arch
)
3922 /* If both backends are using this function, deem them compatible. */
3923 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3926 /* Make a special call to the linker "notice" function to tell it that
3927 we are about to handle an as-needed lib, or have finished
3928 processing the lib. */
3931 _bfd_elf_notice_as_needed (bfd
*ibfd
,
3932 struct bfd_link_info
*info
,
3933 enum notice_asneeded_action act
)
3935 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
3938 /* Check relocations an ELF object file. */
3941 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
3943 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3944 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
3946 /* If this object is the same format as the output object, and it is
3947 not a shared library, then let the backend look through the
3950 This is required to build global offset table entries and to
3951 arrange for dynamic relocs. It is not required for the
3952 particular common case of linking non PIC code, even when linking
3953 against shared libraries, but unfortunately there is no way of
3954 knowing whether an object file has been compiled PIC or not.
3955 Looking through the relocs is not particularly time consuming.
3956 The problem is that we must either (1) keep the relocs in memory,
3957 which causes the linker to require additional runtime memory or
3958 (2) read the relocs twice from the input file, which wastes time.
3959 This would be a good case for using mmap.
3961 I have no idea how to handle linking PIC code into a file of a
3962 different format. It probably can't be done. */
3963 if ((abfd
->flags
& DYNAMIC
) == 0
3964 && is_elf_hash_table (htab
)
3965 && bed
->check_relocs
!= NULL
3966 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
3967 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
3971 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3973 Elf_Internal_Rela
*internal_relocs
;
3976 /* Don't check relocations in excluded sections. Don't do
3977 anything special with non-loaded, non-alloced sections.
3978 In particular, any relocs in such sections should not
3979 affect GOT and PLT reference counting (ie. we don't
3980 allow them to create GOT or PLT entries), there's no
3981 possibility or desire to optimize TLS relocs, and
3982 there's not much point in propagating relocs to shared
3983 libs that the dynamic linker won't relocate. */
3984 if ((o
->flags
& SEC_ALLOC
) == 0
3985 || (o
->flags
& SEC_RELOC
) == 0
3986 || (o
->flags
& SEC_EXCLUDE
) != 0
3987 || o
->reloc_count
== 0
3988 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
3989 && (o
->flags
& SEC_DEBUGGING
) != 0)
3990 || bfd_is_abs_section (o
->output_section
))
3993 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
3995 if (internal_relocs
== NULL
)
3998 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
4000 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4001 free (internal_relocs
);
4011 /* Add symbols from an ELF object file to the linker hash table. */
4014 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4016 Elf_Internal_Ehdr
*ehdr
;
4017 Elf_Internal_Shdr
*hdr
;
4021 struct elf_link_hash_entry
**sym_hash
;
4022 bfd_boolean dynamic
;
4023 Elf_External_Versym
*extversym
= NULL
;
4024 Elf_External_Versym
*extversym_end
= NULL
;
4025 Elf_External_Versym
*ever
;
4026 struct elf_link_hash_entry
*weaks
;
4027 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
4028 size_t nondeflt_vers_cnt
= 0;
4029 Elf_Internal_Sym
*isymbuf
= NULL
;
4030 Elf_Internal_Sym
*isym
;
4031 Elf_Internal_Sym
*isymend
;
4032 const struct elf_backend_data
*bed
;
4033 bfd_boolean add_needed
;
4034 struct elf_link_hash_table
*htab
;
4035 void *alloc_mark
= NULL
;
4036 struct bfd_hash_entry
**old_table
= NULL
;
4037 unsigned int old_size
= 0;
4038 unsigned int old_count
= 0;
4039 void *old_tab
= NULL
;
4041 struct bfd_link_hash_entry
*old_undefs
= NULL
;
4042 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
4043 void *old_strtab
= NULL
;
4046 bfd_boolean just_syms
;
4048 htab
= elf_hash_table (info
);
4049 bed
= get_elf_backend_data (abfd
);
4051 if ((abfd
->flags
& DYNAMIC
) == 0)
4057 /* You can't use -r against a dynamic object. Also, there's no
4058 hope of using a dynamic object which does not exactly match
4059 the format of the output file. */
4060 if (bfd_link_relocatable (info
)
4061 || !is_elf_hash_table (htab
)
4062 || info
->output_bfd
->xvec
!= abfd
->xvec
)
4064 if (bfd_link_relocatable (info
))
4065 bfd_set_error (bfd_error_invalid_operation
);
4067 bfd_set_error (bfd_error_wrong_format
);
4072 ehdr
= elf_elfheader (abfd
);
4073 if (info
->warn_alternate_em
4074 && bed
->elf_machine_code
!= ehdr
->e_machine
4075 && ((bed
->elf_machine_alt1
!= 0
4076 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
4077 || (bed
->elf_machine_alt2
!= 0
4078 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
4080 /* xgettext:c-format */
4081 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4082 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
4084 /* As a GNU extension, any input sections which are named
4085 .gnu.warning.SYMBOL are treated as warning symbols for the given
4086 symbol. This differs from .gnu.warning sections, which generate
4087 warnings when they are included in an output file. */
4088 /* PR 12761: Also generate this warning when building shared libraries. */
4089 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4093 name
= bfd_section_name (s
);
4094 if (CONST_STRNEQ (name
, ".gnu.warning."))
4099 name
+= sizeof ".gnu.warning." - 1;
4101 /* If this is a shared object, then look up the symbol
4102 in the hash table. If it is there, and it is already
4103 been defined, then we will not be using the entry
4104 from this shared object, so we don't need to warn.
4105 FIXME: If we see the definition in a regular object
4106 later on, we will warn, but we shouldn't. The only
4107 fix is to keep track of what warnings we are supposed
4108 to emit, and then handle them all at the end of the
4112 struct elf_link_hash_entry
*h
;
4114 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
4116 /* FIXME: What about bfd_link_hash_common? */
4118 && (h
->root
.type
== bfd_link_hash_defined
4119 || h
->root
.type
== bfd_link_hash_defweak
))
4124 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
4128 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
4133 if (! (_bfd_generic_link_add_one_symbol
4134 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
4135 FALSE
, bed
->collect
, NULL
)))
4138 if (bfd_link_executable (info
))
4140 /* Clobber the section size so that the warning does
4141 not get copied into the output file. */
4144 /* Also set SEC_EXCLUDE, so that symbols defined in
4145 the warning section don't get copied to the output. */
4146 s
->flags
|= SEC_EXCLUDE
;
4151 just_syms
= ((s
= abfd
->sections
) != NULL
4152 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
4157 /* If we are creating a shared library, create all the dynamic
4158 sections immediately. We need to attach them to something,
4159 so we attach them to this BFD, provided it is the right
4160 format and is not from ld --just-symbols. Always create the
4161 dynamic sections for -E/--dynamic-list. FIXME: If there
4162 are no input BFD's of the same format as the output, we can't
4163 make a shared library. */
4165 && (bfd_link_pic (info
)
4166 || (!bfd_link_relocatable (info
)
4168 && (info
->export_dynamic
|| info
->dynamic
)))
4169 && is_elf_hash_table (htab
)
4170 && info
->output_bfd
->xvec
== abfd
->xvec
4171 && !htab
->dynamic_sections_created
)
4173 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
4177 else if (!is_elf_hash_table (htab
))
4181 const char *soname
= NULL
;
4183 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
4184 const Elf_Internal_Phdr
*phdr
;
4185 struct elf_link_loaded_list
*loaded_lib
;
4187 /* ld --just-symbols and dynamic objects don't mix very well.
4188 ld shouldn't allow it. */
4192 /* If this dynamic lib was specified on the command line with
4193 --as-needed in effect, then we don't want to add a DT_NEEDED
4194 tag unless the lib is actually used. Similary for libs brought
4195 in by another lib's DT_NEEDED. When --no-add-needed is used
4196 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4197 any dynamic library in DT_NEEDED tags in the dynamic lib at
4199 add_needed
= (elf_dyn_lib_class (abfd
)
4200 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
4201 | DYN_NO_NEEDED
)) == 0;
4203 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4208 unsigned int elfsec
;
4209 unsigned long shlink
;
4211 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
4218 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
4219 if (elfsec
== SHN_BAD
)
4220 goto error_free_dyn
;
4221 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
4223 for (extdyn
= dynbuf
;
4224 extdyn
<= dynbuf
+ s
->size
- bed
->s
->sizeof_dyn
;
4225 extdyn
+= bed
->s
->sizeof_dyn
)
4227 Elf_Internal_Dyn dyn
;
4229 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
4230 if (dyn
.d_tag
== DT_SONAME
)
4232 unsigned int tagv
= dyn
.d_un
.d_val
;
4233 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4235 goto error_free_dyn
;
4237 if (dyn
.d_tag
== DT_NEEDED
)
4239 struct bfd_link_needed_list
*n
, **pn
;
4241 unsigned int tagv
= dyn
.d_un
.d_val
;
4242 size_t amt
= sizeof (struct bfd_link_needed_list
);
4244 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4245 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4246 if (n
== NULL
|| fnm
== NULL
)
4247 goto error_free_dyn
;
4248 amt
= strlen (fnm
) + 1;
4249 anm
= (char *) bfd_alloc (abfd
, amt
);
4251 goto error_free_dyn
;
4252 memcpy (anm
, fnm
, amt
);
4256 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4260 if (dyn
.d_tag
== DT_RUNPATH
)
4262 struct bfd_link_needed_list
*n
, **pn
;
4264 unsigned int tagv
= dyn
.d_un
.d_val
;
4265 size_t amt
= sizeof (struct bfd_link_needed_list
);
4267 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4268 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4269 if (n
== NULL
|| fnm
== NULL
)
4270 goto error_free_dyn
;
4271 amt
= strlen (fnm
) + 1;
4272 anm
= (char *) bfd_alloc (abfd
, amt
);
4274 goto error_free_dyn
;
4275 memcpy (anm
, fnm
, amt
);
4279 for (pn
= & runpath
;
4285 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4286 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
4288 struct bfd_link_needed_list
*n
, **pn
;
4290 unsigned int tagv
= dyn
.d_un
.d_val
;
4291 size_t amt
= sizeof (struct bfd_link_needed_list
);
4293 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4294 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4295 if (n
== NULL
|| fnm
== NULL
)
4296 goto error_free_dyn
;
4297 amt
= strlen (fnm
) + 1;
4298 anm
= (char *) bfd_alloc (abfd
, amt
);
4300 goto error_free_dyn
;
4301 memcpy (anm
, fnm
, amt
);
4311 if (dyn
.d_tag
== DT_AUDIT
)
4313 unsigned int tagv
= dyn
.d_un
.d_val
;
4314 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4321 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4322 frees all more recently bfd_alloc'd blocks as well. */
4328 struct bfd_link_needed_list
**pn
;
4329 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4334 /* If we have a PT_GNU_RELRO program header, mark as read-only
4335 all sections contained fully therein. This makes relro
4336 shared library sections appear as they will at run-time. */
4337 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4338 while (phdr
-- > elf_tdata (abfd
)->phdr
)
4339 if (phdr
->p_type
== PT_GNU_RELRO
)
4341 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4343 unsigned int opb
= bfd_octets_per_byte (abfd
, s
);
4345 if ((s
->flags
& SEC_ALLOC
) != 0
4346 && s
->vma
* opb
>= phdr
->p_vaddr
4347 && s
->vma
* opb
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4348 s
->flags
|= SEC_READONLY
;
4353 /* We do not want to include any of the sections in a dynamic
4354 object in the output file. We hack by simply clobbering the
4355 list of sections in the BFD. This could be handled more
4356 cleanly by, say, a new section flag; the existing
4357 SEC_NEVER_LOAD flag is not the one we want, because that one
4358 still implies that the section takes up space in the output
4360 bfd_section_list_clear (abfd
);
4362 /* Find the name to use in a DT_NEEDED entry that refers to this
4363 object. If the object has a DT_SONAME entry, we use it.
4364 Otherwise, if the generic linker stuck something in
4365 elf_dt_name, we use that. Otherwise, we just use the file
4367 if (soname
== NULL
|| *soname
== '\0')
4369 soname
= elf_dt_name (abfd
);
4370 if (soname
== NULL
|| *soname
== '\0')
4371 soname
= bfd_get_filename (abfd
);
4374 /* Save the SONAME because sometimes the linker emulation code
4375 will need to know it. */
4376 elf_dt_name (abfd
) = soname
;
4378 /* If we have already included this dynamic object in the
4379 link, just ignore it. There is no reason to include a
4380 particular dynamic object more than once. */
4381 for (loaded_lib
= htab
->dyn_loaded
;
4383 loaded_lib
= loaded_lib
->next
)
4385 if (strcmp (elf_dt_name (loaded_lib
->abfd
), soname
) == 0)
4389 /* Create dynamic sections for backends that require that be done
4390 before setup_gnu_properties. */
4392 && !_bfd_elf_link_create_dynamic_sections (abfd
, info
))
4395 /* Save the DT_AUDIT entry for the linker emulation code. */
4396 elf_dt_audit (abfd
) = audit
;
4399 /* If this is a dynamic object, we always link against the .dynsym
4400 symbol table, not the .symtab symbol table. The dynamic linker
4401 will only see the .dynsym symbol table, so there is no reason to
4402 look at .symtab for a dynamic object. */
4404 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4405 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4407 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4409 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4411 /* The sh_info field of the symtab header tells us where the
4412 external symbols start. We don't care about the local symbols at
4414 if (elf_bad_symtab (abfd
))
4416 extsymcount
= symcount
;
4421 extsymcount
= symcount
- hdr
->sh_info
;
4422 extsymoff
= hdr
->sh_info
;
4425 sym_hash
= elf_sym_hashes (abfd
);
4426 if (extsymcount
!= 0)
4428 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4430 if (isymbuf
== NULL
)
4433 if (sym_hash
== NULL
)
4435 /* We store a pointer to the hash table entry for each
4437 size_t amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4438 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4439 if (sym_hash
== NULL
)
4440 goto error_free_sym
;
4441 elf_sym_hashes (abfd
) = sym_hash
;
4447 /* Read in any version definitions. */
4448 if (!_bfd_elf_slurp_version_tables (abfd
,
4449 info
->default_imported_symver
))
4450 goto error_free_sym
;
4452 /* Read in the symbol versions, but don't bother to convert them
4453 to internal format. */
4454 if (elf_dynversym (abfd
) != 0)
4456 Elf_Internal_Shdr
*versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4457 bfd_size_type amt
= versymhdr
->sh_size
;
4459 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0)
4460 goto error_free_sym
;
4461 extversym
= (Elf_External_Versym
*)
4462 _bfd_malloc_and_read (abfd
, amt
, amt
);
4463 if (extversym
== NULL
)
4464 goto error_free_sym
;
4465 extversym_end
= extversym
+ amt
/ sizeof (*extversym
);
4469 /* If we are loading an as-needed shared lib, save the symbol table
4470 state before we start adding symbols. If the lib turns out
4471 to be unneeded, restore the state. */
4472 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4477 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4479 struct bfd_hash_entry
*p
;
4480 struct elf_link_hash_entry
*h
;
4482 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4484 h
= (struct elf_link_hash_entry
*) p
;
4485 entsize
+= htab
->root
.table
.entsize
;
4486 if (h
->root
.type
== bfd_link_hash_warning
)
4488 entsize
+= htab
->root
.table
.entsize
;
4489 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4491 if (h
->root
.type
== bfd_link_hash_common
)
4492 entsize
+= sizeof (*h
->root
.u
.c
.p
);
4496 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4497 old_tab
= bfd_malloc (tabsize
+ entsize
);
4498 if (old_tab
== NULL
)
4499 goto error_free_vers
;
4501 /* Remember the current objalloc pointer, so that all mem for
4502 symbols added can later be reclaimed. */
4503 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4504 if (alloc_mark
== NULL
)
4505 goto error_free_vers
;
4507 /* Make a special call to the linker "notice" function to
4508 tell it that we are about to handle an as-needed lib. */
4509 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4510 goto error_free_vers
;
4512 /* Clone the symbol table. Remember some pointers into the
4513 symbol table, and dynamic symbol count. */
4514 old_ent
= (char *) old_tab
+ tabsize
;
4515 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4516 old_undefs
= htab
->root
.undefs
;
4517 old_undefs_tail
= htab
->root
.undefs_tail
;
4518 old_table
= htab
->root
.table
.table
;
4519 old_size
= htab
->root
.table
.size
;
4520 old_count
= htab
->root
.table
.count
;
4522 if (htab
->dynstr
!= NULL
)
4524 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4525 if (old_strtab
== NULL
)
4526 goto error_free_vers
;
4529 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4531 struct bfd_hash_entry
*p
;
4532 struct elf_link_hash_entry
*h
;
4534 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4536 h
= (struct elf_link_hash_entry
*) p
;
4537 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4538 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4539 if (h
->root
.type
== bfd_link_hash_warning
)
4541 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4542 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4543 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4545 if (h
->root
.type
== bfd_link_hash_common
)
4547 memcpy (old_ent
, h
->root
.u
.c
.p
, sizeof (*h
->root
.u
.c
.p
));
4548 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
4555 if (extversym
== NULL
)
4557 else if (extversym
+ extsymoff
< extversym_end
)
4558 ever
= extversym
+ extsymoff
;
4561 /* xgettext:c-format */
4562 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4563 abfd
, (long) extsymoff
,
4564 (long) (extversym_end
- extversym
) / sizeof (* extversym
));
4565 bfd_set_error (bfd_error_bad_value
);
4566 goto error_free_vers
;
4569 if (!bfd_link_relocatable (info
)
4570 && abfd
->lto_slim_object
)
4573 (_("%pB: plugin needed to handle lto object"), abfd
);
4576 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
4578 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4582 asection
*sec
, *new_sec
;
4585 struct elf_link_hash_entry
*h
;
4586 struct elf_link_hash_entry
*hi
;
4587 bfd_boolean definition
;
4588 bfd_boolean size_change_ok
;
4589 bfd_boolean type_change_ok
;
4590 bfd_boolean new_weak
;
4591 bfd_boolean old_weak
;
4594 bfd_boolean discarded
;
4595 unsigned int old_alignment
;
4596 unsigned int shindex
;
4598 bfd_boolean matched
;
4602 flags
= BSF_NO_FLAGS
;
4604 value
= isym
->st_value
;
4605 common
= bed
->common_definition (isym
);
4606 if (common
&& info
->inhibit_common_definition
)
4608 /* Treat common symbol as undefined for --no-define-common. */
4609 isym
->st_shndx
= SHN_UNDEF
;
4614 bind
= ELF_ST_BIND (isym
->st_info
);
4618 /* This should be impossible, since ELF requires that all
4619 global symbols follow all local symbols, and that sh_info
4620 point to the first global symbol. Unfortunately, Irix 5
4622 if (elf_bad_symtab (abfd
))
4625 /* If we aren't prepared to handle locals within the globals
4626 then we'll likely segfault on a NULL symbol hash if the
4627 symbol is ever referenced in relocations. */
4628 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
4629 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, hdr
->sh_name
);
4630 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4631 " (>= sh_info of %lu)"),
4632 abfd
, name
, (long) (isym
- isymbuf
+ extsymoff
),
4635 /* Dynamic object relocations are not processed by ld, so
4636 ld won't run into the problem mentioned above. */
4639 bfd_set_error (bfd_error_bad_value
);
4640 goto error_free_vers
;
4643 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4651 case STB_GNU_UNIQUE
:
4652 flags
= BSF_GNU_UNIQUE
;
4656 /* Leave it up to the processor backend. */
4660 if (isym
->st_shndx
== SHN_UNDEF
)
4661 sec
= bfd_und_section_ptr
;
4662 else if (isym
->st_shndx
== SHN_ABS
)
4663 sec
= bfd_abs_section_ptr
;
4664 else if (isym
->st_shndx
== SHN_COMMON
)
4666 sec
= bfd_com_section_ptr
;
4667 /* What ELF calls the size we call the value. What ELF
4668 calls the value we call the alignment. */
4669 value
= isym
->st_size
;
4673 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4675 sec
= bfd_abs_section_ptr
;
4676 else if (discarded_section (sec
))
4678 /* Symbols from discarded section are undefined. We keep
4680 sec
= bfd_und_section_ptr
;
4682 isym
->st_shndx
= SHN_UNDEF
;
4684 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4688 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4691 goto error_free_vers
;
4693 if (isym
->st_shndx
== SHN_COMMON
4694 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4696 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4700 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4702 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4704 goto error_free_vers
;
4708 else if (isym
->st_shndx
== SHN_COMMON
4709 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4710 && !bfd_link_relocatable (info
))
4712 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4716 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4717 | SEC_LINKER_CREATED
);
4718 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4720 goto error_free_vers
;
4724 else if (bed
->elf_add_symbol_hook
)
4726 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4728 goto error_free_vers
;
4730 /* The hook function sets the name to NULL if this symbol
4731 should be skipped for some reason. */
4736 /* Sanity check that all possibilities were handled. */
4740 /* Silently discard TLS symbols from --just-syms. There's
4741 no way to combine a static TLS block with a new TLS block
4742 for this executable. */
4743 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4744 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4747 if (bfd_is_und_section (sec
)
4748 || bfd_is_com_section (sec
))
4753 size_change_ok
= FALSE
;
4754 type_change_ok
= bed
->type_change_ok
;
4761 if (is_elf_hash_table (htab
))
4763 Elf_Internal_Versym iver
;
4764 unsigned int vernum
= 0;
4769 if (info
->default_imported_symver
)
4770 /* Use the default symbol version created earlier. */
4771 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4775 else if (ever
>= extversym_end
)
4777 /* xgettext:c-format */
4778 _bfd_error_handler (_("%pB: not enough version information"),
4780 bfd_set_error (bfd_error_bad_value
);
4781 goto error_free_vers
;
4784 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4786 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4788 /* If this is a hidden symbol, or if it is not version
4789 1, we append the version name to the symbol name.
4790 However, we do not modify a non-hidden absolute symbol
4791 if it is not a function, because it might be the version
4792 symbol itself. FIXME: What if it isn't? */
4793 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4795 && (!bfd_is_abs_section (sec
)
4796 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4799 size_t namelen
, verlen
, newlen
;
4802 if (isym
->st_shndx
!= SHN_UNDEF
)
4804 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4806 else if (vernum
> 1)
4808 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4815 /* xgettext:c-format */
4816 (_("%pB: %s: invalid version %u (max %d)"),
4818 elf_tdata (abfd
)->cverdefs
);
4819 bfd_set_error (bfd_error_bad_value
);
4820 goto error_free_vers
;
4825 /* We cannot simply test for the number of
4826 entries in the VERNEED section since the
4827 numbers for the needed versions do not start
4829 Elf_Internal_Verneed
*t
;
4832 for (t
= elf_tdata (abfd
)->verref
;
4836 Elf_Internal_Vernaux
*a
;
4838 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4840 if (a
->vna_other
== vernum
)
4842 verstr
= a
->vna_nodename
;
4852 /* xgettext:c-format */
4853 (_("%pB: %s: invalid needed version %d"),
4854 abfd
, name
, vernum
);
4855 bfd_set_error (bfd_error_bad_value
);
4856 goto error_free_vers
;
4860 namelen
= strlen (name
);
4861 verlen
= strlen (verstr
);
4862 newlen
= namelen
+ verlen
+ 2;
4863 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4864 && isym
->st_shndx
!= SHN_UNDEF
)
4867 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4868 if (newname
== NULL
)
4869 goto error_free_vers
;
4870 memcpy (newname
, name
, namelen
);
4871 p
= newname
+ namelen
;
4873 /* If this is a defined non-hidden version symbol,
4874 we add another @ to the name. This indicates the
4875 default version of the symbol. */
4876 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4877 && isym
->st_shndx
!= SHN_UNDEF
)
4879 memcpy (p
, verstr
, verlen
+ 1);
4884 /* If this symbol has default visibility and the user has
4885 requested we not re-export it, then mark it as hidden. */
4886 if (!bfd_is_und_section (sec
)
4889 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4890 isym
->st_other
= (STV_HIDDEN
4891 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4893 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4894 sym_hash
, &old_bfd
, &old_weak
,
4895 &old_alignment
, &skip
, &override
,
4896 &type_change_ok
, &size_change_ok
,
4898 goto error_free_vers
;
4903 /* Override a definition only if the new symbol matches the
4905 if (override
&& matched
)
4909 while (h
->root
.type
== bfd_link_hash_indirect
4910 || h
->root
.type
== bfd_link_hash_warning
)
4911 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4913 if (elf_tdata (abfd
)->verdef
!= NULL
4916 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4919 if (! (_bfd_generic_link_add_one_symbol
4920 (info
, override
? override
: abfd
, name
, flags
, sec
, value
,
4921 NULL
, FALSE
, bed
->collect
,
4922 (struct bfd_link_hash_entry
**) sym_hash
)))
4923 goto error_free_vers
;
4926 /* We need to make sure that indirect symbol dynamic flags are
4929 while (h
->root
.type
== bfd_link_hash_indirect
4930 || h
->root
.type
== bfd_link_hash_warning
)
4931 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4933 /* Setting the index to -3 tells elf_link_output_extsym that
4934 this symbol is defined in a discarded section. */
4940 new_weak
= (flags
& BSF_WEAK
) != 0;
4944 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4945 && is_elf_hash_table (htab
)
4946 && h
->u
.alias
== NULL
)
4948 /* Keep a list of all weak defined non function symbols from
4949 a dynamic object, using the alias field. Later in this
4950 function we will set the alias field to the correct
4951 value. We only put non-function symbols from dynamic
4952 objects on this list, because that happens to be the only
4953 time we need to know the normal symbol corresponding to a
4954 weak symbol, and the information is time consuming to
4955 figure out. If the alias field is not already NULL,
4956 then this symbol was already defined by some previous
4957 dynamic object, and we will be using that previous
4958 definition anyhow. */
4964 /* Set the alignment of a common symbol. */
4965 if ((common
|| bfd_is_com_section (sec
))
4966 && h
->root
.type
== bfd_link_hash_common
)
4971 align
= bfd_log2 (isym
->st_value
);
4974 /* The new symbol is a common symbol in a shared object.
4975 We need to get the alignment from the section. */
4976 align
= new_sec
->alignment_power
;
4978 if (align
> old_alignment
)
4979 h
->root
.u
.c
.p
->alignment_power
= align
;
4981 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4984 if (is_elf_hash_table (htab
))
4986 /* Set a flag in the hash table entry indicating the type of
4987 reference or definition we just found. A dynamic symbol
4988 is one which is referenced or defined by both a regular
4989 object and a shared object. */
4990 bfd_boolean dynsym
= FALSE
;
4997 if (bind
!= STB_WEAK
)
4998 h
->ref_regular_nonweak
= 1;
5010 /* If the indirect symbol has been forced local, don't
5011 make the real symbol dynamic. */
5012 if ((h
== hi
|| !hi
->forced_local
)
5013 && (bfd_link_dll (info
)
5023 hi
->ref_dynamic
= 1;
5028 hi
->def_dynamic
= 1;
5031 /* If the indirect symbol has been forced local, don't
5032 make the real symbol dynamic. */
5033 if ((h
== hi
|| !hi
->forced_local
)
5037 && weakdef (h
)->dynindx
!= -1)))
5041 /* Check to see if we need to add an indirect symbol for
5042 the default name. */
5044 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
5045 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
5046 sec
, value
, &old_bfd
, &dynsym
))
5047 goto error_free_vers
;
5049 /* Check the alignment when a common symbol is involved. This
5050 can change when a common symbol is overridden by a normal
5051 definition or a common symbol is ignored due to the old
5052 normal definition. We need to make sure the maximum
5053 alignment is maintained. */
5054 if ((old_alignment
|| common
)
5055 && h
->root
.type
!= bfd_link_hash_common
)
5057 unsigned int common_align
;
5058 unsigned int normal_align
;
5059 unsigned int symbol_align
;
5063 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
5064 || h
->root
.type
== bfd_link_hash_defweak
);
5066 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
5067 if (h
->root
.u
.def
.section
->owner
!= NULL
5068 && (h
->root
.u
.def
.section
->owner
->flags
5069 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
5071 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
5072 if (normal_align
> symbol_align
)
5073 normal_align
= symbol_align
;
5076 normal_align
= symbol_align
;
5080 common_align
= old_alignment
;
5081 common_bfd
= old_bfd
;
5086 common_align
= bfd_log2 (isym
->st_value
);
5088 normal_bfd
= old_bfd
;
5091 if (normal_align
< common_align
)
5093 /* PR binutils/2735 */
5094 if (normal_bfd
== NULL
)
5096 /* xgettext:c-format */
5097 (_("warning: alignment %u of common symbol `%s' in %pB is"
5098 " greater than the alignment (%u) of its section %pA"),
5099 1 << common_align
, name
, common_bfd
,
5100 1 << normal_align
, h
->root
.u
.def
.section
);
5103 /* xgettext:c-format */
5104 (_("warning: alignment %u of symbol `%s' in %pB"
5105 " is smaller than %u in %pB"),
5106 1 << normal_align
, name
, normal_bfd
,
5107 1 << common_align
, common_bfd
);
5111 /* Remember the symbol size if it isn't undefined. */
5112 if (isym
->st_size
!= 0
5113 && isym
->st_shndx
!= SHN_UNDEF
5114 && (definition
|| h
->size
== 0))
5117 && h
->size
!= isym
->st_size
5118 && ! size_change_ok
)
5120 /* xgettext:c-format */
5121 (_("warning: size of symbol `%s' changed"
5122 " from %" PRIu64
" in %pB to %" PRIu64
" in %pB"),
5123 name
, (uint64_t) h
->size
, old_bfd
,
5124 (uint64_t) isym
->st_size
, abfd
);
5126 h
->size
= isym
->st_size
;
5129 /* If this is a common symbol, then we always want H->SIZE
5130 to be the size of the common symbol. The code just above
5131 won't fix the size if a common symbol becomes larger. We
5132 don't warn about a size change here, because that is
5133 covered by --warn-common. Allow changes between different
5135 if (h
->root
.type
== bfd_link_hash_common
)
5136 h
->size
= h
->root
.u
.c
.size
;
5138 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
5139 && ((definition
&& !new_weak
)
5140 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
5141 || h
->type
== STT_NOTYPE
))
5143 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
5145 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5147 if (type
== STT_GNU_IFUNC
5148 && (abfd
->flags
& DYNAMIC
) != 0)
5151 if (h
->type
!= type
)
5153 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
5154 /* xgettext:c-format */
5156 (_("warning: type of symbol `%s' changed"
5157 " from %d to %d in %pB"),
5158 name
, h
->type
, type
, abfd
);
5164 /* Merge st_other field. */
5165 elf_merge_st_other (abfd
, h
, isym
, sec
, definition
, dynamic
);
5167 /* We don't want to make debug symbol dynamic. */
5169 && (sec
->flags
& SEC_DEBUGGING
)
5170 && !bfd_link_relocatable (info
))
5175 h
->target_internal
= isym
->st_target_internal
;
5176 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
5179 if (definition
&& !dynamic
)
5181 char *p
= strchr (name
, ELF_VER_CHR
);
5182 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
5184 /* Queue non-default versions so that .symver x, x@FOO
5185 aliases can be checked. */
5188 size_t amt
= ((isymend
- isym
+ 1)
5189 * sizeof (struct elf_link_hash_entry
*));
5191 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5193 goto error_free_vers
;
5195 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
5199 if (dynsym
&& (abfd
->flags
& BFD_PLUGIN
) == 0 && h
->dynindx
== -1)
5201 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5202 goto error_free_vers
;
5204 && weakdef (h
)->dynindx
== -1)
5206 if (!bfd_elf_link_record_dynamic_symbol (info
, weakdef (h
)))
5207 goto error_free_vers
;
5210 else if (h
->dynindx
!= -1)
5211 /* If the symbol already has a dynamic index, but
5212 visibility says it should not be visible, turn it into
5214 switch (ELF_ST_VISIBILITY (h
->other
))
5218 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
5227 && h
->ref_regular_nonweak
)
5228 || (h
->ref_dynamic_nonweak
5229 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5230 && !on_needed_list (elf_dt_name (abfd
),
5231 htab
->needed
, NULL
))))
5233 const char *soname
= elf_dt_name (abfd
);
5235 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
5236 h
->root
.root
.string
);
5238 /* A symbol from a library loaded via DT_NEEDED of some
5239 other library is referenced by a regular object.
5240 Add a DT_NEEDED entry for it. Issue an error if
5241 --no-add-needed is used and the reference was not
5244 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
5247 /* xgettext:c-format */
5248 (_("%pB: undefined reference to symbol '%s'"),
5250 bfd_set_error (bfd_error_missing_dso
);
5251 goto error_free_vers
;
5254 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
5255 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
5257 /* Create dynamic sections for backends that require
5258 that be done before setup_gnu_properties. */
5259 if (!_bfd_elf_link_create_dynamic_sections (abfd
, info
))
5266 if (info
->lto_plugin_active
5267 && !bfd_link_relocatable (info
)
5268 && (abfd
->flags
& BFD_PLUGIN
) == 0
5274 if (bed
->s
->arch_size
== 32)
5279 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5280 referenced in regular objects so that linker plugin will get
5281 the correct symbol resolution. */
5283 sym_hash
= elf_sym_hashes (abfd
);
5284 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5286 Elf_Internal_Rela
*internal_relocs
;
5287 Elf_Internal_Rela
*rel
, *relend
;
5289 /* Don't check relocations in excluded sections. */
5290 if ((s
->flags
& SEC_RELOC
) == 0
5291 || s
->reloc_count
== 0
5292 || (s
->flags
& SEC_EXCLUDE
) != 0
5293 || ((info
->strip
== strip_all
5294 || info
->strip
== strip_debugger
)
5295 && (s
->flags
& SEC_DEBUGGING
) != 0))
5298 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, s
, NULL
,
5301 if (internal_relocs
== NULL
)
5302 goto error_free_vers
;
5304 rel
= internal_relocs
;
5305 relend
= rel
+ s
->reloc_count
;
5306 for ( ; rel
< relend
; rel
++)
5308 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
5309 struct elf_link_hash_entry
*h
;
5311 /* Skip local symbols. */
5312 if (r_symndx
< extsymoff
)
5315 h
= sym_hash
[r_symndx
- extsymoff
];
5317 h
->root
.non_ir_ref_regular
= 1;
5320 if (elf_section_data (s
)->relocs
!= internal_relocs
)
5321 free (internal_relocs
);
5330 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
5334 /* Restore the symbol table. */
5335 old_ent
= (char *) old_tab
+ tabsize
;
5336 memset (elf_sym_hashes (abfd
), 0,
5337 extsymcount
* sizeof (struct elf_link_hash_entry
*));
5338 htab
->root
.table
.table
= old_table
;
5339 htab
->root
.table
.size
= old_size
;
5340 htab
->root
.table
.count
= old_count
;
5341 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
5342 htab
->root
.undefs
= old_undefs
;
5343 htab
->root
.undefs_tail
= old_undefs_tail
;
5344 if (htab
->dynstr
!= NULL
)
5345 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
5348 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
5350 struct bfd_hash_entry
*p
;
5351 struct elf_link_hash_entry
*h
;
5352 unsigned int non_ir_ref_dynamic
;
5354 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
5356 /* Preserve non_ir_ref_dynamic so that this symbol
5357 will be exported when the dynamic lib becomes needed
5358 in the second pass. */
5359 h
= (struct elf_link_hash_entry
*) p
;
5360 if (h
->root
.type
== bfd_link_hash_warning
)
5361 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5362 non_ir_ref_dynamic
= h
->root
.non_ir_ref_dynamic
;
5364 h
= (struct elf_link_hash_entry
*) p
;
5365 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5366 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5367 if (h
->root
.type
== bfd_link_hash_warning
)
5369 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5370 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5371 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5373 if (h
->root
.type
== bfd_link_hash_common
)
5375 memcpy (h
->root
.u
.c
.p
, old_ent
, sizeof (*h
->root
.u
.c
.p
));
5376 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
5378 h
->root
.non_ir_ref_dynamic
= non_ir_ref_dynamic
;
5382 /* Make a special call to the linker "notice" function to
5383 tell it that symbols added for crefs may need to be removed. */
5384 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
5385 goto error_free_vers
;
5388 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
5390 free (nondeflt_vers
);
5394 if (old_tab
!= NULL
)
5396 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
5397 goto error_free_vers
;
5402 /* Now that all the symbols from this input file are created, if
5403 not performing a relocatable link, handle .symver foo, foo@BAR
5404 such that any relocs against foo become foo@BAR. */
5405 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5409 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5411 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5412 char *shortname
, *p
;
5415 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5417 || (h
->root
.type
!= bfd_link_hash_defined
5418 && h
->root
.type
!= bfd_link_hash_defweak
))
5421 amt
= p
- h
->root
.root
.string
;
5422 shortname
= (char *) bfd_malloc (amt
+ 1);
5424 goto error_free_vers
;
5425 memcpy (shortname
, h
->root
.root
.string
, amt
);
5426 shortname
[amt
] = '\0';
5428 hi
= (struct elf_link_hash_entry
*)
5429 bfd_link_hash_lookup (&htab
->root
, shortname
,
5430 FALSE
, FALSE
, FALSE
);
5432 && hi
->root
.type
== h
->root
.type
5433 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5434 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5436 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
5437 hi
->root
.type
= bfd_link_hash_indirect
;
5438 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5439 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5440 sym_hash
= elf_sym_hashes (abfd
);
5442 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5443 if (sym_hash
[symidx
] == hi
)
5445 sym_hash
[symidx
] = h
;
5451 free (nondeflt_vers
);
5452 nondeflt_vers
= NULL
;
5455 /* Now set the alias field correctly for all the weak defined
5456 symbols we found. The only way to do this is to search all the
5457 symbols. Since we only need the information for non functions in
5458 dynamic objects, that's the only time we actually put anything on
5459 the list WEAKS. We need this information so that if a regular
5460 object refers to a symbol defined weakly in a dynamic object, the
5461 real symbol in the dynamic object is also put in the dynamic
5462 symbols; we also must arrange for both symbols to point to the
5463 same memory location. We could handle the general case of symbol
5464 aliasing, but a general symbol alias can only be generated in
5465 assembler code, handling it correctly would be very time
5466 consuming, and other ELF linkers don't handle general aliasing
5470 struct elf_link_hash_entry
**hpp
;
5471 struct elf_link_hash_entry
**hppend
;
5472 struct elf_link_hash_entry
**sorted_sym_hash
;
5473 struct elf_link_hash_entry
*h
;
5474 size_t sym_count
, amt
;
5476 /* Since we have to search the whole symbol list for each weak
5477 defined symbol, search time for N weak defined symbols will be
5478 O(N^2). Binary search will cut it down to O(NlogN). */
5479 amt
= extsymcount
* sizeof (*sorted_sym_hash
);
5480 sorted_sym_hash
= bfd_malloc (amt
);
5481 if (sorted_sym_hash
== NULL
)
5483 sym_hash
= sorted_sym_hash
;
5484 hpp
= elf_sym_hashes (abfd
);
5485 hppend
= hpp
+ extsymcount
;
5487 for (; hpp
< hppend
; hpp
++)
5491 && h
->root
.type
== bfd_link_hash_defined
5492 && !bed
->is_function_type (h
->type
))
5500 qsort (sorted_sym_hash
, sym_count
, sizeof (*sorted_sym_hash
),
5503 while (weaks
!= NULL
)
5505 struct elf_link_hash_entry
*hlook
;
5508 size_t i
, j
, idx
= 0;
5511 weaks
= hlook
->u
.alias
;
5512 hlook
->u
.alias
= NULL
;
5514 if (hlook
->root
.type
!= bfd_link_hash_defined
5515 && hlook
->root
.type
!= bfd_link_hash_defweak
)
5518 slook
= hlook
->root
.u
.def
.section
;
5519 vlook
= hlook
->root
.u
.def
.value
;
5525 bfd_signed_vma vdiff
;
5527 h
= sorted_sym_hash
[idx
];
5528 vdiff
= vlook
- h
->root
.u
.def
.value
;
5535 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5545 /* We didn't find a value/section match. */
5549 /* With multiple aliases, or when the weak symbol is already
5550 strongly defined, we have multiple matching symbols and
5551 the binary search above may land on any of them. Step
5552 one past the matching symbol(s). */
5555 h
= sorted_sym_hash
[idx
];
5556 if (h
->root
.u
.def
.section
!= slook
5557 || h
->root
.u
.def
.value
!= vlook
)
5561 /* Now look back over the aliases. Since we sorted by size
5562 as well as value and section, we'll choose the one with
5563 the largest size. */
5566 h
= sorted_sym_hash
[idx
];
5568 /* Stop if value or section doesn't match. */
5569 if (h
->root
.u
.def
.section
!= slook
5570 || h
->root
.u
.def
.value
!= vlook
)
5572 else if (h
!= hlook
)
5574 struct elf_link_hash_entry
*t
;
5577 hlook
->is_weakalias
= 1;
5579 if (t
->u
.alias
!= NULL
)
5580 while (t
->u
.alias
!= h
)
5584 /* If the weak definition is in the list of dynamic
5585 symbols, make sure the real definition is put
5587 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5589 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5592 free (sorted_sym_hash
);
5597 /* If the real definition is in the list of dynamic
5598 symbols, make sure the weak definition is put
5599 there as well. If we don't do this, then the
5600 dynamic loader might not merge the entries for the
5601 real definition and the weak definition. */
5602 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5604 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5605 goto err_free_sym_hash
;
5612 free (sorted_sym_hash
);
5615 if (bed
->check_directives
5616 && !(*bed
->check_directives
) (abfd
, info
))
5619 /* If this is a non-traditional link, try to optimize the handling
5620 of the .stab/.stabstr sections. */
5622 && ! info
->traditional_format
5623 && is_elf_hash_table (htab
)
5624 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5628 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5629 if (stabstr
!= NULL
)
5631 bfd_size_type string_offset
= 0;
5634 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5635 if (CONST_STRNEQ (stab
->name
, ".stab")
5636 && (!stab
->name
[5] ||
5637 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5638 && (stab
->flags
& SEC_MERGE
) == 0
5639 && !bfd_is_abs_section (stab
->output_section
))
5641 struct bfd_elf_section_data
*secdata
;
5643 secdata
= elf_section_data (stab
);
5644 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5645 stabstr
, &secdata
->sec_info
,
5648 if (secdata
->sec_info
)
5649 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5654 if (dynamic
&& add_needed
)
5656 /* Add this bfd to the loaded list. */
5657 struct elf_link_loaded_list
*n
;
5659 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5663 n
->next
= htab
->dyn_loaded
;
5664 htab
->dyn_loaded
= n
;
5666 if (dynamic
&& !add_needed
5667 && (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) != 0)
5668 elf_dyn_lib_class (abfd
) |= DYN_NO_NEEDED
;
5675 free (nondeflt_vers
);
5683 /* Return the linker hash table entry of a symbol that might be
5684 satisfied by an archive symbol. Return -1 on error. */
5686 struct elf_link_hash_entry
*
5687 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5688 struct bfd_link_info
*info
,
5691 struct elf_link_hash_entry
*h
;
5695 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, TRUE
);
5699 /* If this is a default version (the name contains @@), look up the
5700 symbol again with only one `@' as well as without the version.
5701 The effect is that references to the symbol with and without the
5702 version will be matched by the default symbol in the archive. */
5704 p
= strchr (name
, ELF_VER_CHR
);
5705 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5708 /* First check with only one `@'. */
5709 len
= strlen (name
);
5710 copy
= (char *) bfd_alloc (abfd
, len
);
5712 return (struct elf_link_hash_entry
*) -1;
5714 first
= p
- name
+ 1;
5715 memcpy (copy
, name
, first
);
5716 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5718 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, TRUE
);
5721 /* We also need to check references to the symbol without the
5723 copy
[first
- 1] = '\0';
5724 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
5725 FALSE
, FALSE
, TRUE
);
5728 bfd_release (abfd
, copy
);
5732 /* Add symbols from an ELF archive file to the linker hash table. We
5733 don't use _bfd_generic_link_add_archive_symbols because we need to
5734 handle versioned symbols.
5736 Fortunately, ELF archive handling is simpler than that done by
5737 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5738 oddities. In ELF, if we find a symbol in the archive map, and the
5739 symbol is currently undefined, we know that we must pull in that
5742 Unfortunately, we do have to make multiple passes over the symbol
5743 table until nothing further is resolved. */
5746 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5749 unsigned char *included
= NULL
;
5753 const struct elf_backend_data
*bed
;
5754 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
5755 (bfd
*, struct bfd_link_info
*, const char *);
5757 if (! bfd_has_map (abfd
))
5759 /* An empty archive is a special case. */
5760 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5762 bfd_set_error (bfd_error_no_armap
);
5766 /* Keep track of all symbols we know to be already defined, and all
5767 files we know to be already included. This is to speed up the
5768 second and subsequent passes. */
5769 c
= bfd_ardata (abfd
)->symdef_count
;
5772 amt
= c
* sizeof (*included
);
5773 included
= (unsigned char *) bfd_zmalloc (amt
);
5774 if (included
== NULL
)
5777 symdefs
= bfd_ardata (abfd
)->symdefs
;
5778 bed
= get_elf_backend_data (abfd
);
5779 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5792 symdefend
= symdef
+ c
;
5793 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5795 struct elf_link_hash_entry
*h
;
5797 struct bfd_link_hash_entry
*undefs_tail
;
5802 if (symdef
->file_offset
== last
)
5808 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5809 if (h
== (struct elf_link_hash_entry
*) -1)
5815 if (h
->root
.type
== bfd_link_hash_undefined
)
5817 /* If the archive element has already been loaded then one
5818 of the symbols defined by that element might have been
5819 made undefined due to being in a discarded section. */
5823 else if (h
->root
.type
== bfd_link_hash_common
)
5825 /* We currently have a common symbol. The archive map contains
5826 a reference to this symbol, so we may want to include it. We
5827 only want to include it however, if this archive element
5828 contains a definition of the symbol, not just another common
5831 Unfortunately some archivers (including GNU ar) will put
5832 declarations of common symbols into their archive maps, as
5833 well as real definitions, so we cannot just go by the archive
5834 map alone. Instead we must read in the element's symbol
5835 table and check that to see what kind of symbol definition
5837 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5842 if (h
->root
.type
!= bfd_link_hash_undefweak
)
5843 /* Symbol must be defined. Don't check it again. */
5848 /* We need to include this archive member. */
5849 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5850 if (element
== NULL
)
5853 if (! bfd_check_format (element
, bfd_object
))
5856 undefs_tail
= info
->hash
->undefs_tail
;
5858 if (!(*info
->callbacks
5859 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5861 if (!bfd_link_add_symbols (element
, info
))
5864 /* If there are any new undefined symbols, we need to make
5865 another pass through the archive in order to see whether
5866 they can be defined. FIXME: This isn't perfect, because
5867 common symbols wind up on undefs_tail and because an
5868 undefined symbol which is defined later on in this pass
5869 does not require another pass. This isn't a bug, but it
5870 does make the code less efficient than it could be. */
5871 if (undefs_tail
!= info
->hash
->undefs_tail
)
5874 /* Look backward to mark all symbols from this object file
5875 which we have already seen in this pass. */
5879 included
[mark
] = TRUE
;
5884 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5886 /* We mark subsequent symbols from this object file as we go
5887 on through the loop. */
5888 last
= symdef
->file_offset
;
5901 /* Given an ELF BFD, add symbols to the global hash table as
5905 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5907 switch (bfd_get_format (abfd
))
5910 return elf_link_add_object_symbols (abfd
, info
);
5912 return elf_link_add_archive_symbols (abfd
, info
);
5914 bfd_set_error (bfd_error_wrong_format
);
5919 struct hash_codes_info
5921 unsigned long *hashcodes
;
5925 /* This function will be called though elf_link_hash_traverse to store
5926 all hash value of the exported symbols in an array. */
5929 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5931 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
5936 /* Ignore indirect symbols. These are added by the versioning code. */
5937 if (h
->dynindx
== -1)
5940 name
= h
->root
.root
.string
;
5941 if (h
->versioned
>= versioned
)
5943 char *p
= strchr (name
, ELF_VER_CHR
);
5946 alc
= (char *) bfd_malloc (p
- name
+ 1);
5952 memcpy (alc
, name
, p
- name
);
5953 alc
[p
- name
] = '\0';
5958 /* Compute the hash value. */
5959 ha
= bfd_elf_hash (name
);
5961 /* Store the found hash value in the array given as the argument. */
5962 *(inf
->hashcodes
)++ = ha
;
5964 /* And store it in the struct so that we can put it in the hash table
5966 h
->u
.elf_hash_value
= ha
;
5972 struct collect_gnu_hash_codes
5975 const struct elf_backend_data
*bed
;
5976 unsigned long int nsyms
;
5977 unsigned long int maskbits
;
5978 unsigned long int *hashcodes
;
5979 unsigned long int *hashval
;
5980 unsigned long int *indx
;
5981 unsigned long int *counts
;
5985 long int min_dynindx
;
5986 unsigned long int bucketcount
;
5987 unsigned long int symindx
;
5988 long int local_indx
;
5989 long int shift1
, shift2
;
5990 unsigned long int mask
;
5994 /* This function will be called though elf_link_hash_traverse to store
5995 all hash value of the exported symbols in an array. */
5998 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6000 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6005 /* Ignore indirect symbols. These are added by the versioning code. */
6006 if (h
->dynindx
== -1)
6009 /* Ignore also local symbols and undefined symbols. */
6010 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6013 name
= h
->root
.root
.string
;
6014 if (h
->versioned
>= versioned
)
6016 char *p
= strchr (name
, ELF_VER_CHR
);
6019 alc
= (char *) bfd_malloc (p
- name
+ 1);
6025 memcpy (alc
, name
, p
- name
);
6026 alc
[p
- name
] = '\0';
6031 /* Compute the hash value. */
6032 ha
= bfd_elf_gnu_hash (name
);
6034 /* Store the found hash value in the array for compute_bucket_count,
6035 and also for .dynsym reordering purposes. */
6036 s
->hashcodes
[s
->nsyms
] = ha
;
6037 s
->hashval
[h
->dynindx
] = ha
;
6039 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
6040 s
->min_dynindx
= h
->dynindx
;
6046 /* This function will be called though elf_link_hash_traverse to do
6047 final dynamic symbol renumbering in case of .gnu.hash.
6048 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6049 to the translation table. */
6052 elf_gnu_hash_process_symidx (struct elf_link_hash_entry
*h
, void *data
)
6054 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6055 unsigned long int bucket
;
6056 unsigned long int val
;
6058 /* Ignore indirect symbols. */
6059 if (h
->dynindx
== -1)
6062 /* Ignore also local symbols and undefined symbols. */
6063 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6065 if (h
->dynindx
>= s
->min_dynindx
)
6067 if (s
->bed
->record_xhash_symbol
!= NULL
)
6069 (*s
->bed
->record_xhash_symbol
) (h
, 0);
6073 h
->dynindx
= s
->local_indx
++;
6078 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
6079 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
6080 & ((s
->maskbits
>> s
->shift1
) - 1);
6081 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
6083 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
6084 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
6085 if (s
->counts
[bucket
] == 1)
6086 /* Last element terminates the chain. */
6088 bfd_put_32 (s
->output_bfd
, val
,
6089 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
6090 --s
->counts
[bucket
];
6091 if (s
->bed
->record_xhash_symbol
!= NULL
)
6093 bfd_vma xlat_loc
= s
->xlat
+ (s
->indx
[bucket
]++ - s
->symindx
) * 4;
6095 (*s
->bed
->record_xhash_symbol
) (h
, xlat_loc
);
6098 h
->dynindx
= s
->indx
[bucket
]++;
6102 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6105 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
6107 return !(h
->forced_local
6108 || h
->root
.type
== bfd_link_hash_undefined
6109 || h
->root
.type
== bfd_link_hash_undefweak
6110 || ((h
->root
.type
== bfd_link_hash_defined
6111 || h
->root
.type
== bfd_link_hash_defweak
)
6112 && h
->root
.u
.def
.section
->output_section
== NULL
));
6115 /* Array used to determine the number of hash table buckets to use
6116 based on the number of symbols there are. If there are fewer than
6117 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6118 fewer than 37 we use 17 buckets, and so forth. We never use more
6119 than 32771 buckets. */
6121 static const size_t elf_buckets
[] =
6123 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6127 /* Compute bucket count for hashing table. We do not use a static set
6128 of possible tables sizes anymore. Instead we determine for all
6129 possible reasonable sizes of the table the outcome (i.e., the
6130 number of collisions etc) and choose the best solution. The
6131 weighting functions are not too simple to allow the table to grow
6132 without bounds. Instead one of the weighting factors is the size.
6133 Therefore the result is always a good payoff between few collisions
6134 (= short chain lengths) and table size. */
6136 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6137 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
6138 unsigned long int nsyms
,
6141 size_t best_size
= 0;
6142 unsigned long int i
;
6144 /* We have a problem here. The following code to optimize the table
6145 size requires an integer type with more the 32 bits. If
6146 BFD_HOST_U_64_BIT is set we know about such a type. */
6147 #ifdef BFD_HOST_U_64_BIT
6152 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
6153 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
6154 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
6155 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
6156 unsigned long int *counts
;
6158 unsigned int no_improvement_count
= 0;
6160 /* Possible optimization parameters: if we have NSYMS symbols we say
6161 that the hashing table must at least have NSYMS/4 and at most
6163 minsize
= nsyms
/ 4;
6166 best_size
= maxsize
= nsyms
* 2;
6171 if ((best_size
& 31) == 0)
6175 /* Create array where we count the collisions in. We must use bfd_malloc
6176 since the size could be large. */
6178 amt
*= sizeof (unsigned long int);
6179 counts
= (unsigned long int *) bfd_malloc (amt
);
6183 /* Compute the "optimal" size for the hash table. The criteria is a
6184 minimal chain length. The minor criteria is (of course) the size
6186 for (i
= minsize
; i
< maxsize
; ++i
)
6188 /* Walk through the array of hashcodes and count the collisions. */
6189 BFD_HOST_U_64_BIT max
;
6190 unsigned long int j
;
6191 unsigned long int fact
;
6193 if (gnu_hash
&& (i
& 31) == 0)
6196 memset (counts
, '\0', i
* sizeof (unsigned long int));
6198 /* Determine how often each hash bucket is used. */
6199 for (j
= 0; j
< nsyms
; ++j
)
6200 ++counts
[hashcodes
[j
] % i
];
6202 /* For the weight function we need some information about the
6203 pagesize on the target. This is information need not be 100%
6204 accurate. Since this information is not available (so far) we
6205 define it here to a reasonable default value. If it is crucial
6206 to have a better value some day simply define this value. */
6207 # ifndef BFD_TARGET_PAGESIZE
6208 # define BFD_TARGET_PAGESIZE (4096)
6211 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6213 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
6216 /* Variant 1: optimize for short chains. We add the squares
6217 of all the chain lengths (which favors many small chain
6218 over a few long chains). */
6219 for (j
= 0; j
< i
; ++j
)
6220 max
+= counts
[j
] * counts
[j
];
6222 /* This adds penalties for the overall size of the table. */
6223 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6226 /* Variant 2: Optimize a lot more for small table. Here we
6227 also add squares of the size but we also add penalties for
6228 empty slots (the +1 term). */
6229 for (j
= 0; j
< i
; ++j
)
6230 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
6232 /* The overall size of the table is considered, but not as
6233 strong as in variant 1, where it is squared. */
6234 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6238 /* Compare with current best results. */
6239 if (max
< best_chlen
)
6243 no_improvement_count
= 0;
6245 /* PR 11843: Avoid futile long searches for the best bucket size
6246 when there are a large number of symbols. */
6247 else if (++no_improvement_count
== 100)
6254 #endif /* defined (BFD_HOST_U_64_BIT) */
6256 /* This is the fallback solution if no 64bit type is available or if we
6257 are not supposed to spend much time on optimizations. We select the
6258 bucket count using a fixed set of numbers. */
6259 for (i
= 0; elf_buckets
[i
] != 0; i
++)
6261 best_size
= elf_buckets
[i
];
6262 if (nsyms
< elf_buckets
[i
+ 1])
6265 if (gnu_hash
&& best_size
< 2)
6272 /* Size any SHT_GROUP section for ld -r. */
6275 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
6280 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6281 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6282 && (s
= ibfd
->sections
) != NULL
6283 && s
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
6284 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
6289 /* Set a default stack segment size. The value in INFO wins. If it
6290 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6291 undefined it is initialized. */
6294 bfd_elf_stack_segment_size (bfd
*output_bfd
,
6295 struct bfd_link_info
*info
,
6296 const char *legacy_symbol
,
6297 bfd_vma default_size
)
6299 struct elf_link_hash_entry
*h
= NULL
;
6301 /* Look for legacy symbol. */
6303 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
6304 FALSE
, FALSE
, FALSE
);
6305 if (h
&& (h
->root
.type
== bfd_link_hash_defined
6306 || h
->root
.type
== bfd_link_hash_defweak
)
6308 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
6310 /* The symbol has no type if specified on the command line. */
6311 h
->type
= STT_OBJECT
;
6312 if (info
->stacksize
)
6313 /* xgettext:c-format */
6314 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6315 output_bfd
, legacy_symbol
);
6316 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
6317 /* xgettext:c-format */
6318 _bfd_error_handler (_("%pB: %s not absolute"),
6319 output_bfd
, legacy_symbol
);
6321 info
->stacksize
= h
->root
.u
.def
.value
;
6324 if (!info
->stacksize
)
6325 /* If the user didn't set a size, or explicitly inhibit the
6326 size, set it now. */
6327 info
->stacksize
= default_size
;
6329 /* Provide the legacy symbol, if it is referenced. */
6330 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
6331 || h
->root
.type
== bfd_link_hash_undefweak
))
6333 struct bfd_link_hash_entry
*bh
= NULL
;
6335 if (!(_bfd_generic_link_add_one_symbol
6336 (info
, output_bfd
, legacy_symbol
,
6337 BSF_GLOBAL
, bfd_abs_section_ptr
,
6338 info
->stacksize
>= 0 ? info
->stacksize
: 0,
6339 NULL
, FALSE
, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
6342 h
= (struct elf_link_hash_entry
*) bh
;
6344 h
->type
= STT_OBJECT
;
6350 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6352 struct elf_gc_sweep_symbol_info
6354 struct bfd_link_info
*info
;
6355 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
6360 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
6363 && (((h
->root
.type
== bfd_link_hash_defined
6364 || h
->root
.type
== bfd_link_hash_defweak
)
6365 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
6366 && h
->root
.u
.def
.section
->gc_mark
))
6367 || h
->root
.type
== bfd_link_hash_undefined
6368 || h
->root
.type
== bfd_link_hash_undefweak
))
6370 struct elf_gc_sweep_symbol_info
*inf
;
6372 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
6373 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
6376 h
->ref_regular_nonweak
= 0;
6382 /* Set up the sizes and contents of the ELF dynamic sections. This is
6383 called by the ELF linker emulation before_allocation routine. We
6384 must set the sizes of the sections before the linker sets the
6385 addresses of the various sections. */
6388 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
6391 const char *filter_shlib
,
6393 const char *depaudit
,
6394 const char * const *auxiliary_filters
,
6395 struct bfd_link_info
*info
,
6396 asection
**sinterpptr
)
6399 const struct elf_backend_data
*bed
;
6403 if (!is_elf_hash_table (info
->hash
))
6406 dynobj
= elf_hash_table (info
)->dynobj
;
6408 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6410 struct bfd_elf_version_tree
*verdefs
;
6411 struct elf_info_failed asvinfo
;
6412 struct bfd_elf_version_tree
*t
;
6413 struct bfd_elf_version_expr
*d
;
6417 /* If we are supposed to export all symbols into the dynamic symbol
6418 table (this is not the normal case), then do so. */
6419 if (info
->export_dynamic
6420 || (bfd_link_executable (info
) && info
->dynamic
))
6422 struct elf_info_failed eif
;
6426 elf_link_hash_traverse (elf_hash_table (info
),
6427 _bfd_elf_export_symbol
,
6435 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6437 if (soname_indx
== (size_t) -1
6438 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6442 soname_indx
= (size_t) -1;
6444 /* Make all global versions with definition. */
6445 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6446 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6447 if (!d
->symver
&& d
->literal
)
6449 const char *verstr
, *name
;
6450 size_t namelen
, verlen
, newlen
;
6451 char *newname
, *p
, leading_char
;
6452 struct elf_link_hash_entry
*newh
;
6454 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6456 namelen
= strlen (name
) + (leading_char
!= '\0');
6458 verlen
= strlen (verstr
);
6459 newlen
= namelen
+ verlen
+ 3;
6461 newname
= (char *) bfd_malloc (newlen
);
6462 if (newname
== NULL
)
6464 newname
[0] = leading_char
;
6465 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6467 /* Check the hidden versioned definition. */
6468 p
= newname
+ namelen
;
6470 memcpy (p
, verstr
, verlen
+ 1);
6471 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6472 newname
, FALSE
, FALSE
,
6475 || (newh
->root
.type
!= bfd_link_hash_defined
6476 && newh
->root
.type
!= bfd_link_hash_defweak
))
6478 /* Check the default versioned definition. */
6480 memcpy (p
, verstr
, verlen
+ 1);
6481 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6482 newname
, FALSE
, FALSE
,
6487 /* Mark this version if there is a definition and it is
6488 not defined in a shared object. */
6490 && !newh
->def_dynamic
6491 && (newh
->root
.type
== bfd_link_hash_defined
6492 || newh
->root
.type
== bfd_link_hash_defweak
))
6496 /* Attach all the symbols to their version information. */
6497 asvinfo
.info
= info
;
6498 asvinfo
.failed
= FALSE
;
6500 elf_link_hash_traverse (elf_hash_table (info
),
6501 _bfd_elf_link_assign_sym_version
,
6506 if (!info
->allow_undefined_version
)
6508 /* Check if all global versions have a definition. */
6509 bfd_boolean all_defined
= TRUE
;
6510 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6511 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6512 if (d
->literal
&& !d
->symver
&& !d
->script
)
6515 (_("%s: undefined version: %s"),
6516 d
->pattern
, t
->name
);
6517 all_defined
= FALSE
;
6522 bfd_set_error (bfd_error_bad_value
);
6527 /* Set up the version definition section. */
6528 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6529 BFD_ASSERT (s
!= NULL
);
6531 /* We may have created additional version definitions if we are
6532 just linking a regular application. */
6533 verdefs
= info
->version_info
;
6535 /* Skip anonymous version tag. */
6536 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6537 verdefs
= verdefs
->next
;
6539 if (verdefs
== NULL
&& !info
->create_default_symver
)
6540 s
->flags
|= SEC_EXCLUDE
;
6546 Elf_Internal_Verdef def
;
6547 Elf_Internal_Verdaux defaux
;
6548 struct bfd_link_hash_entry
*bh
;
6549 struct elf_link_hash_entry
*h
;
6555 /* Make space for the base version. */
6556 size
+= sizeof (Elf_External_Verdef
);
6557 size
+= sizeof (Elf_External_Verdaux
);
6560 /* Make space for the default version. */
6561 if (info
->create_default_symver
)
6563 size
+= sizeof (Elf_External_Verdef
);
6567 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6569 struct bfd_elf_version_deps
*n
;
6571 /* Don't emit base version twice. */
6575 size
+= sizeof (Elf_External_Verdef
);
6576 size
+= sizeof (Elf_External_Verdaux
);
6579 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6580 size
+= sizeof (Elf_External_Verdaux
);
6584 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6585 if (s
->contents
== NULL
&& s
->size
!= 0)
6588 /* Fill in the version definition section. */
6592 def
.vd_version
= VER_DEF_CURRENT
;
6593 def
.vd_flags
= VER_FLG_BASE
;
6596 if (info
->create_default_symver
)
6598 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6599 def
.vd_next
= sizeof (Elf_External_Verdef
);
6603 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6604 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6605 + sizeof (Elf_External_Verdaux
));
6608 if (soname_indx
!= (size_t) -1)
6610 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6612 def
.vd_hash
= bfd_elf_hash (soname
);
6613 defaux
.vda_name
= soname_indx
;
6620 name
= lbasename (bfd_get_filename (output_bfd
));
6621 def
.vd_hash
= bfd_elf_hash (name
);
6622 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6624 if (indx
== (size_t) -1)
6626 defaux
.vda_name
= indx
;
6628 defaux
.vda_next
= 0;
6630 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6631 (Elf_External_Verdef
*) p
);
6632 p
+= sizeof (Elf_External_Verdef
);
6633 if (info
->create_default_symver
)
6635 /* Add a symbol representing this version. */
6637 if (! (_bfd_generic_link_add_one_symbol
6638 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6640 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6642 h
= (struct elf_link_hash_entry
*) bh
;
6645 h
->type
= STT_OBJECT
;
6646 h
->verinfo
.vertree
= NULL
;
6648 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6651 /* Create a duplicate of the base version with the same
6652 aux block, but different flags. */
6655 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6657 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6658 + sizeof (Elf_External_Verdaux
));
6661 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6662 (Elf_External_Verdef
*) p
);
6663 p
+= sizeof (Elf_External_Verdef
);
6665 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6666 (Elf_External_Verdaux
*) p
);
6667 p
+= sizeof (Elf_External_Verdaux
);
6669 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6672 struct bfd_elf_version_deps
*n
;
6674 /* Don't emit the base version twice. */
6679 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6682 /* Add a symbol representing this version. */
6684 if (! (_bfd_generic_link_add_one_symbol
6685 (info
, dynobj
, t
->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
= t
;
6695 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6698 def
.vd_version
= VER_DEF_CURRENT
;
6700 if (t
->globals
.list
== NULL
6701 && t
->locals
.list
== NULL
6703 def
.vd_flags
|= VER_FLG_WEAK
;
6704 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6705 def
.vd_cnt
= cdeps
+ 1;
6706 def
.vd_hash
= bfd_elf_hash (t
->name
);
6707 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6710 /* If a basever node is next, it *must* be the last node in
6711 the chain, otherwise Verdef construction breaks. */
6712 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6713 BFD_ASSERT (t
->next
->next
== NULL
);
6715 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6716 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6717 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6719 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6720 (Elf_External_Verdef
*) p
);
6721 p
+= sizeof (Elf_External_Verdef
);
6723 defaux
.vda_name
= h
->dynstr_index
;
6724 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6726 defaux
.vda_next
= 0;
6727 if (t
->deps
!= NULL
)
6728 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6729 t
->name_indx
= defaux
.vda_name
;
6731 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6732 (Elf_External_Verdaux
*) p
);
6733 p
+= sizeof (Elf_External_Verdaux
);
6735 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6737 if (n
->version_needed
== NULL
)
6739 /* This can happen if there was an error in the
6741 defaux
.vda_name
= 0;
6745 defaux
.vda_name
= n
->version_needed
->name_indx
;
6746 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6749 if (n
->next
== NULL
)
6750 defaux
.vda_next
= 0;
6752 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6754 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6755 (Elf_External_Verdaux
*) p
);
6756 p
+= sizeof (Elf_External_Verdaux
);
6760 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6764 bed
= get_elf_backend_data (output_bfd
);
6766 if (info
->gc_sections
&& bed
->can_gc_sections
)
6768 struct elf_gc_sweep_symbol_info sweep_info
;
6770 /* Remove the symbols that were in the swept sections from the
6771 dynamic symbol table. */
6772 sweep_info
.info
= info
;
6773 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
6774 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
6778 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6781 struct elf_find_verdep_info sinfo
;
6783 /* Work out the size of the version reference section. */
6785 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6786 BFD_ASSERT (s
!= NULL
);
6789 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6790 if (sinfo
.vers
== 0)
6792 sinfo
.failed
= FALSE
;
6794 elf_link_hash_traverse (elf_hash_table (info
),
6795 _bfd_elf_link_find_version_dependencies
,
6800 if (elf_tdata (output_bfd
)->verref
== NULL
)
6801 s
->flags
|= SEC_EXCLUDE
;
6804 Elf_Internal_Verneed
*vn
;
6809 /* Build the version dependency section. */
6812 for (vn
= elf_tdata (output_bfd
)->verref
;
6814 vn
= vn
->vn_nextref
)
6816 Elf_Internal_Vernaux
*a
;
6818 size
+= sizeof (Elf_External_Verneed
);
6820 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6821 size
+= sizeof (Elf_External_Vernaux
);
6825 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6826 if (s
->contents
== NULL
)
6830 for (vn
= elf_tdata (output_bfd
)->verref
;
6832 vn
= vn
->vn_nextref
)
6835 Elf_Internal_Vernaux
*a
;
6839 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6842 vn
->vn_version
= VER_NEED_CURRENT
;
6844 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6845 elf_dt_name (vn
->vn_bfd
) != NULL
6846 ? elf_dt_name (vn
->vn_bfd
)
6847 : lbasename (bfd_get_filename
6850 if (indx
== (size_t) -1)
6853 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
6854 if (vn
->vn_nextref
== NULL
)
6857 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
6858 + caux
* sizeof (Elf_External_Vernaux
));
6860 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
6861 (Elf_External_Verneed
*) p
);
6862 p
+= sizeof (Elf_External_Verneed
);
6864 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6866 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6867 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6868 a
->vna_nodename
, FALSE
);
6869 if (indx
== (size_t) -1)
6872 if (a
->vna_nextptr
== NULL
)
6875 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6877 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6878 (Elf_External_Vernaux
*) p
);
6879 p
+= sizeof (Elf_External_Vernaux
);
6883 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6887 /* Any syms created from now on start with -1 in
6888 got.refcount/offset and plt.refcount/offset. */
6889 elf_hash_table (info
)->init_got_refcount
6890 = elf_hash_table (info
)->init_got_offset
;
6891 elf_hash_table (info
)->init_plt_refcount
6892 = elf_hash_table (info
)->init_plt_offset
;
6894 if (bfd_link_relocatable (info
)
6895 && !_bfd_elf_size_group_sections (info
))
6898 /* The backend may have to create some sections regardless of whether
6899 we're dynamic or not. */
6900 if (bed
->elf_backend_always_size_sections
6901 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
6904 /* Determine any GNU_STACK segment requirements, after the backend
6905 has had a chance to set a default segment size. */
6906 if (info
->execstack
)
6907 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
6908 else if (info
->noexecstack
)
6909 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
6913 asection
*notesec
= NULL
;
6916 for (inputobj
= info
->input_bfds
;
6918 inputobj
= inputobj
->link
.next
)
6923 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
6925 s
= inputobj
->sections
;
6926 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
6929 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
6932 if (s
->flags
& SEC_CODE
)
6936 else if (bed
->default_execstack
)
6939 if (notesec
|| info
->stacksize
> 0)
6940 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
6941 if (notesec
&& exec
&& bfd_link_relocatable (info
)
6942 && notesec
->output_section
!= bfd_abs_section_ptr
)
6943 notesec
->output_section
->flags
|= SEC_CODE
;
6946 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6948 struct elf_info_failed eif
;
6949 struct elf_link_hash_entry
*h
;
6953 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
6954 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
6958 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
6960 info
->flags
|= DF_SYMBOLIC
;
6968 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
6970 if (indx
== (size_t) -1)
6973 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
6974 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
6978 if (filter_shlib
!= NULL
)
6982 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6983 filter_shlib
, TRUE
);
6984 if (indx
== (size_t) -1
6985 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
6989 if (auxiliary_filters
!= NULL
)
6991 const char * const *p
;
6993 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
6997 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6999 if (indx
== (size_t) -1
7000 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
7009 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
7011 if (indx
== (size_t) -1
7012 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
7016 if (depaudit
!= NULL
)
7020 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
7022 if (indx
== (size_t) -1
7023 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
7030 /* Find all symbols which were defined in a dynamic object and make
7031 the backend pick a reasonable value for them. */
7032 elf_link_hash_traverse (elf_hash_table (info
),
7033 _bfd_elf_adjust_dynamic_symbol
,
7038 /* Add some entries to the .dynamic section. We fill in some of the
7039 values later, in bfd_elf_final_link, but we must add the entries
7040 now so that we know the final size of the .dynamic section. */
7042 /* If there are initialization and/or finalization functions to
7043 call then add the corresponding DT_INIT/DT_FINI entries. */
7044 h
= (info
->init_function
7045 ? elf_link_hash_lookup (elf_hash_table (info
),
7046 info
->init_function
, FALSE
,
7053 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
7056 h
= (info
->fini_function
7057 ? elf_link_hash_lookup (elf_hash_table (info
),
7058 info
->fini_function
, FALSE
,
7065 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
7069 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
7070 if (s
!= NULL
&& s
->linker_has_input
)
7072 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7073 if (! bfd_link_executable (info
))
7078 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
7079 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
7080 && (o
= sub
->sections
) != NULL
7081 && o
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
)
7082 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7083 if (elf_section_data (o
)->this_hdr
.sh_type
7084 == SHT_PREINIT_ARRAY
)
7087 (_("%pB: .preinit_array section is not allowed in DSO"),
7092 bfd_set_error (bfd_error_nonrepresentable_section
);
7096 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
7097 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
7100 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
7101 if (s
!= NULL
&& s
->linker_has_input
)
7103 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
7104 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
7107 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
7108 if (s
!= NULL
&& s
->linker_has_input
)
7110 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
7111 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
7115 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
7116 /* If .dynstr is excluded from the link, we don't want any of
7117 these tags. Strictly, we should be checking each section
7118 individually; This quick check covers for the case where
7119 someone does a /DISCARD/ : { *(*) }. */
7120 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
7122 bfd_size_type strsize
;
7124 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7125 if ((info
->emit_hash
7126 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
7127 || (info
->emit_gnu_hash
7128 && (bed
->record_xhash_symbol
== NULL
7129 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0)))
7130 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
7131 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
7132 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
7133 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
7134 bed
->s
->sizeof_sym
))
7139 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
7142 /* The backend must work out the sizes of all the other dynamic
7145 && bed
->elf_backend_size_dynamic_sections
!= NULL
7146 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
7149 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7151 if (elf_tdata (output_bfd
)->cverdefs
)
7153 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
7155 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
7156 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
7160 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
7162 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
7165 else if (info
->flags
& DF_BIND_NOW
)
7167 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
7173 if (bfd_link_executable (info
))
7174 info
->flags_1
&= ~ (DF_1_INITFIRST
7177 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
7181 if (elf_tdata (output_bfd
)->cverrefs
)
7183 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
7185 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
7186 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
7190 if ((elf_tdata (output_bfd
)->cverrefs
== 0
7191 && elf_tdata (output_bfd
)->cverdefs
== 0)
7192 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, NULL
) <= 1)
7196 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7197 s
->flags
|= SEC_EXCLUDE
;
7203 /* Find the first non-excluded output section. We'll use its
7204 section symbol for some emitted relocs. */
7206 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
7209 asection
*found
= NULL
;
7211 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7212 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7213 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7216 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7219 elf_hash_table (info
)->text_index_section
= found
;
7222 /* Find two non-excluded output sections, one for code, one for data.
7223 We'll use their section symbols for some emitted relocs. */
7225 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
7228 asection
*found
= NULL
;
7230 /* Data first, since setting text_index_section changes
7231 _bfd_elf_omit_section_dynsym_default. */
7232 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7233 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7234 && !(s
->flags
& SEC_READONLY
)
7235 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7238 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7241 elf_hash_table (info
)->data_index_section
= found
;
7243 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7244 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7245 && (s
->flags
& SEC_READONLY
)
7246 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7251 elf_hash_table (info
)->text_index_section
= found
;
7254 #define GNU_HASH_SECTION_NAME(bed) \
7255 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7258 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
7260 const struct elf_backend_data
*bed
;
7261 unsigned long section_sym_count
;
7262 bfd_size_type dynsymcount
= 0;
7264 if (!is_elf_hash_table (info
->hash
))
7267 bed
= get_elf_backend_data (output_bfd
);
7268 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
7270 /* Assign dynsym indices. In a shared library we generate a section
7271 symbol for each output section, which come first. Next come all
7272 of the back-end allocated local dynamic syms, followed by the rest
7273 of the global symbols.
7275 This is usually not needed for static binaries, however backends
7276 can request to always do it, e.g. the MIPS backend uses dynamic
7277 symbol counts to lay out GOT, which will be produced in the
7278 presence of GOT relocations even in static binaries (holding fixed
7279 data in that case, to satisfy those relocations). */
7281 if (elf_hash_table (info
)->dynamic_sections_created
7282 || bed
->always_renumber_dynsyms
)
7283 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
7284 §ion_sym_count
);
7286 if (elf_hash_table (info
)->dynamic_sections_created
)
7290 unsigned int dtagcount
;
7292 dynobj
= elf_hash_table (info
)->dynobj
;
7294 /* Work out the size of the symbol version section. */
7295 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7296 BFD_ASSERT (s
!= NULL
);
7297 if ((s
->flags
& SEC_EXCLUDE
) == 0)
7299 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
7300 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7301 if (s
->contents
== NULL
)
7304 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
7308 /* Set the size of the .dynsym and .hash sections. We counted
7309 the number of dynamic symbols in elf_link_add_object_symbols.
7310 We will build the contents of .dynsym and .hash when we build
7311 the final symbol table, because until then we do not know the
7312 correct value to give the symbols. We built the .dynstr
7313 section as we went along in elf_link_add_object_symbols. */
7314 s
= elf_hash_table (info
)->dynsym
;
7315 BFD_ASSERT (s
!= NULL
);
7316 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
7318 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7319 if (s
->contents
== NULL
)
7322 /* The first entry in .dynsym is a dummy symbol. Clear all the
7323 section syms, in case we don't output them all. */
7324 ++section_sym_count
;
7325 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
7327 elf_hash_table (info
)->bucketcount
= 0;
7329 /* Compute the size of the hashing table. As a side effect this
7330 computes the hash values for all the names we export. */
7331 if (info
->emit_hash
)
7333 unsigned long int *hashcodes
;
7334 struct hash_codes_info hashinf
;
7336 unsigned long int nsyms
;
7338 size_t hash_entry_size
;
7340 /* Compute the hash values for all exported symbols. At the same
7341 time store the values in an array so that we could use them for
7343 amt
= dynsymcount
* sizeof (unsigned long int);
7344 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
7345 if (hashcodes
== NULL
)
7347 hashinf
.hashcodes
= hashcodes
;
7348 hashinf
.error
= FALSE
;
7350 /* Put all hash values in HASHCODES. */
7351 elf_link_hash_traverse (elf_hash_table (info
),
7352 elf_collect_hash_codes
, &hashinf
);
7359 nsyms
= hashinf
.hashcodes
- hashcodes
;
7361 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
7364 if (bucketcount
== 0 && nsyms
> 0)
7367 elf_hash_table (info
)->bucketcount
= bucketcount
;
7369 s
= bfd_get_linker_section (dynobj
, ".hash");
7370 BFD_ASSERT (s
!= NULL
);
7371 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
7372 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
7373 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7374 if (s
->contents
== NULL
)
7377 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
7378 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
7379 s
->contents
+ hash_entry_size
);
7382 if (info
->emit_gnu_hash
)
7385 unsigned char *contents
;
7386 struct collect_gnu_hash_codes cinfo
;
7390 memset (&cinfo
, 0, sizeof (cinfo
));
7392 /* Compute the hash values for all exported symbols. At the same
7393 time store the values in an array so that we could use them for
7395 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
7396 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
7397 if (cinfo
.hashcodes
== NULL
)
7400 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
7401 cinfo
.min_dynindx
= -1;
7402 cinfo
.output_bfd
= output_bfd
;
7405 /* Put all hash values in HASHCODES. */
7406 elf_link_hash_traverse (elf_hash_table (info
),
7407 elf_collect_gnu_hash_codes
, &cinfo
);
7410 free (cinfo
.hashcodes
);
7415 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
7417 if (bucketcount
== 0)
7419 free (cinfo
.hashcodes
);
7423 s
= bfd_get_linker_section (dynobj
, GNU_HASH_SECTION_NAME (bed
));
7424 BFD_ASSERT (s
!= NULL
);
7426 if (cinfo
.nsyms
== 0)
7428 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7429 BFD_ASSERT (cinfo
.min_dynindx
== -1);
7430 free (cinfo
.hashcodes
);
7431 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
7432 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7433 if (contents
== NULL
)
7435 s
->contents
= contents
;
7436 /* 1 empty bucket. */
7437 bfd_put_32 (output_bfd
, 1, contents
);
7438 /* SYMIDX above the special symbol 0. */
7439 bfd_put_32 (output_bfd
, 1, contents
+ 4);
7440 /* Just one word for bitmask. */
7441 bfd_put_32 (output_bfd
, 1, contents
+ 8);
7442 /* Only hash fn bloom filter. */
7443 bfd_put_32 (output_bfd
, 0, contents
+ 12);
7444 /* No hashes are valid - empty bitmask. */
7445 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
7446 /* No hashes in the only bucket. */
7447 bfd_put_32 (output_bfd
, 0,
7448 contents
+ 16 + bed
->s
->arch_size
/ 8);
7452 unsigned long int maskwords
, maskbitslog2
, x
;
7453 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
7457 while ((x
>>= 1) != 0)
7459 if (maskbitslog2
< 3)
7461 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
7462 maskbitslog2
= maskbitslog2
+ 3;
7464 maskbitslog2
= maskbitslog2
+ 2;
7465 if (bed
->s
->arch_size
== 64)
7467 if (maskbitslog2
== 5)
7473 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
7474 cinfo
.shift2
= maskbitslog2
;
7475 cinfo
.maskbits
= 1 << maskbitslog2
;
7476 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
7477 amt
= bucketcount
* sizeof (unsigned long int) * 2;
7478 amt
+= maskwords
* sizeof (bfd_vma
);
7479 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
7480 if (cinfo
.bitmask
== NULL
)
7482 free (cinfo
.hashcodes
);
7486 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
7487 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
7488 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
7489 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
7491 /* Determine how often each hash bucket is used. */
7492 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
7493 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
7494 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
7496 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
7497 if (cinfo
.counts
[i
] != 0)
7499 cinfo
.indx
[i
] = cnt
;
7500 cnt
+= cinfo
.counts
[i
];
7502 BFD_ASSERT (cnt
== dynsymcount
);
7503 cinfo
.bucketcount
= bucketcount
;
7504 cinfo
.local_indx
= cinfo
.min_dynindx
;
7506 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
7507 s
->size
+= cinfo
.maskbits
/ 8;
7508 if (bed
->record_xhash_symbol
!= NULL
)
7509 s
->size
+= cinfo
.nsyms
* 4;
7510 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7511 if (contents
== NULL
)
7513 free (cinfo
.bitmask
);
7514 free (cinfo
.hashcodes
);
7518 s
->contents
= contents
;
7519 bfd_put_32 (output_bfd
, bucketcount
, contents
);
7520 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
7521 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
7522 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
7523 contents
+= 16 + cinfo
.maskbits
/ 8;
7525 for (i
= 0; i
< bucketcount
; ++i
)
7527 if (cinfo
.counts
[i
] == 0)
7528 bfd_put_32 (output_bfd
, 0, contents
);
7530 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
7534 cinfo
.contents
= contents
;
7536 cinfo
.xlat
= contents
+ cinfo
.nsyms
* 4 - s
->contents
;
7537 /* Renumber dynamic symbols, if populating .gnu.hash section.
7538 If using .MIPS.xhash, populate the translation table. */
7539 elf_link_hash_traverse (elf_hash_table (info
),
7540 elf_gnu_hash_process_symidx
, &cinfo
);
7542 contents
= s
->contents
+ 16;
7543 for (i
= 0; i
< maskwords
; ++i
)
7545 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
7547 contents
+= bed
->s
->arch_size
/ 8;
7550 free (cinfo
.bitmask
);
7551 free (cinfo
.hashcodes
);
7555 s
= bfd_get_linker_section (dynobj
, ".dynstr");
7556 BFD_ASSERT (s
!= NULL
);
7558 elf_finalize_dynstr (output_bfd
, info
);
7560 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7562 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
7563 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
7570 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7573 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
7576 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
7577 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
7580 /* Finish SHF_MERGE section merging. */
7583 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
7588 if (!is_elf_hash_table (info
->hash
))
7591 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
7592 if ((ibfd
->flags
& DYNAMIC
) == 0
7593 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
7594 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
7595 == get_elf_backend_data (obfd
)->s
->elfclass
))
7596 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7597 if ((sec
->flags
& SEC_MERGE
) != 0
7598 && !bfd_is_abs_section (sec
->output_section
))
7600 struct bfd_elf_section_data
*secdata
;
7602 secdata
= elf_section_data (sec
);
7603 if (! _bfd_add_merge_section (obfd
,
7604 &elf_hash_table (info
)->merge_info
,
7605 sec
, &secdata
->sec_info
))
7607 else if (secdata
->sec_info
)
7608 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7611 if (elf_hash_table (info
)->merge_info
!= NULL
)
7612 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7613 merge_sections_remove_hook
);
7617 /* Create an entry in an ELF linker hash table. */
7619 struct bfd_hash_entry
*
7620 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7621 struct bfd_hash_table
*table
,
7624 /* Allocate the structure if it has not already been allocated by a
7628 entry
= (struct bfd_hash_entry
*)
7629 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7634 /* Call the allocation method of the superclass. */
7635 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7638 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7639 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7641 /* Set local fields. */
7644 ret
->got
= htab
->init_got_refcount
;
7645 ret
->plt
= htab
->init_plt_refcount
;
7646 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7647 - offsetof (struct elf_link_hash_entry
, size
)));
7648 /* Assume that we have been called by a non-ELF symbol reader.
7649 This flag is then reset by the code which reads an ELF input
7650 file. This ensures that a symbol created by a non-ELF symbol
7651 reader will have the flag set correctly. */
7658 /* Copy data from an indirect symbol to its direct symbol, hiding the
7659 old indirect symbol. Also used for copying flags to a weakdef. */
7662 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7663 struct elf_link_hash_entry
*dir
,
7664 struct elf_link_hash_entry
*ind
)
7666 struct elf_link_hash_table
*htab
;
7668 if (ind
->dyn_relocs
!= NULL
)
7670 if (dir
->dyn_relocs
!= NULL
)
7672 struct elf_dyn_relocs
**pp
;
7673 struct elf_dyn_relocs
*p
;
7675 /* Add reloc counts against the indirect sym to the direct sym
7676 list. Merge any entries against the same section. */
7677 for (pp
= &ind
->dyn_relocs
; (p
= *pp
) != NULL
; )
7679 struct elf_dyn_relocs
*q
;
7681 for (q
= dir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
7682 if (q
->sec
== p
->sec
)
7684 q
->pc_count
+= p
->pc_count
;
7685 q
->count
+= p
->count
;
7692 *pp
= dir
->dyn_relocs
;
7695 dir
->dyn_relocs
= ind
->dyn_relocs
;
7696 ind
->dyn_relocs
= NULL
;
7699 /* Copy down any references that we may have already seen to the
7700 symbol which just became indirect. */
7702 if (dir
->versioned
!= versioned_hidden
)
7703 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7704 dir
->ref_regular
|= ind
->ref_regular
;
7705 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7706 dir
->non_got_ref
|= ind
->non_got_ref
;
7707 dir
->needs_plt
|= ind
->needs_plt
;
7708 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7710 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7713 /* Copy over the global and procedure linkage table refcount entries.
7714 These may have been already set up by a check_relocs routine. */
7715 htab
= elf_hash_table (info
);
7716 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7718 if (dir
->got
.refcount
< 0)
7719 dir
->got
.refcount
= 0;
7720 dir
->got
.refcount
+= ind
->got
.refcount
;
7721 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7724 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7726 if (dir
->plt
.refcount
< 0)
7727 dir
->plt
.refcount
= 0;
7728 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7729 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7732 if (ind
->dynindx
!= -1)
7734 if (dir
->dynindx
!= -1)
7735 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7736 dir
->dynindx
= ind
->dynindx
;
7737 dir
->dynstr_index
= ind
->dynstr_index
;
7739 ind
->dynstr_index
= 0;
7744 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7745 struct elf_link_hash_entry
*h
,
7746 bfd_boolean force_local
)
7748 /* STT_GNU_IFUNC symbol must go through PLT. */
7749 if (h
->type
!= STT_GNU_IFUNC
)
7751 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7756 h
->forced_local
= 1;
7757 if (h
->dynindx
!= -1)
7759 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7762 h
->dynstr_index
= 0;
7767 /* Hide a symbol. */
7770 _bfd_elf_link_hide_symbol (bfd
*output_bfd
,
7771 struct bfd_link_info
*info
,
7772 struct bfd_link_hash_entry
*h
)
7774 if (is_elf_hash_table (info
->hash
))
7776 const struct elf_backend_data
*bed
7777 = get_elf_backend_data (output_bfd
);
7778 struct elf_link_hash_entry
*eh
7779 = (struct elf_link_hash_entry
*) h
;
7780 bed
->elf_backend_hide_symbol (info
, eh
, TRUE
);
7781 eh
->def_dynamic
= 0;
7782 eh
->ref_dynamic
= 0;
7783 eh
->dynamic_def
= 0;
7787 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7791 _bfd_elf_link_hash_table_init
7792 (struct elf_link_hash_table
*table
,
7794 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
7795 struct bfd_hash_table
*,
7797 unsigned int entsize
,
7798 enum elf_target_id target_id
)
7801 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
7803 table
->init_got_refcount
.refcount
= can_refcount
- 1;
7804 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
7805 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
7806 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
7807 /* The first dynamic symbol is a dummy. */
7808 table
->dynsymcount
= 1;
7810 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
7812 table
->root
.type
= bfd_link_elf_hash_table
;
7813 table
->hash_table_id
= target_id
;
7814 table
->target_os
= get_elf_backend_data (abfd
)->target_os
;
7819 /* Create an ELF linker hash table. */
7821 struct bfd_link_hash_table
*
7822 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7824 struct elf_link_hash_table
*ret
;
7825 size_t amt
= sizeof (struct elf_link_hash_table
);
7827 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7831 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7832 sizeof (struct elf_link_hash_entry
),
7838 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7843 /* Destroy an ELF linker hash table. */
7846 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7848 struct elf_link_hash_table
*htab
;
7850 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7851 if (htab
->dynstr
!= NULL
)
7852 _bfd_elf_strtab_free (htab
->dynstr
);
7853 _bfd_merge_sections_free (htab
->merge_info
);
7854 _bfd_generic_link_hash_table_free (obfd
);
7857 /* This is a hook for the ELF emulation code in the generic linker to
7858 tell the backend linker what file name to use for the DT_NEEDED
7859 entry for a dynamic object. */
7862 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7864 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7865 && bfd_get_format (abfd
) == bfd_object
)
7866 elf_dt_name (abfd
) = name
;
7870 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
7873 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7874 && bfd_get_format (abfd
) == bfd_object
)
7875 lib_class
= elf_dyn_lib_class (abfd
);
7882 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
7884 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7885 && bfd_get_format (abfd
) == bfd_object
)
7886 elf_dyn_lib_class (abfd
) = lib_class
;
7889 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7890 the linker ELF emulation code. */
7892 struct bfd_link_needed_list
*
7893 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7894 struct bfd_link_info
*info
)
7896 if (! is_elf_hash_table (info
->hash
))
7898 return elf_hash_table (info
)->needed
;
7901 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7902 hook for the linker ELF emulation code. */
7904 struct bfd_link_needed_list
*
7905 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7906 struct bfd_link_info
*info
)
7908 if (! is_elf_hash_table (info
->hash
))
7910 return elf_hash_table (info
)->runpath
;
7913 /* Get the name actually used for a dynamic object for a link. This
7914 is the SONAME entry if there is one. Otherwise, it is the string
7915 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7918 bfd_elf_get_dt_soname (bfd
*abfd
)
7920 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7921 && bfd_get_format (abfd
) == bfd_object
)
7922 return elf_dt_name (abfd
);
7926 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7927 the ELF linker emulation code. */
7930 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
7931 struct bfd_link_needed_list
**pneeded
)
7934 bfd_byte
*dynbuf
= NULL
;
7935 unsigned int elfsec
;
7936 unsigned long shlink
;
7937 bfd_byte
*extdyn
, *extdynend
;
7939 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
7943 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
7944 || bfd_get_format (abfd
) != bfd_object
)
7947 s
= bfd_get_section_by_name (abfd
, ".dynamic");
7948 if (s
== NULL
|| s
->size
== 0)
7951 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
7954 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
7955 if (elfsec
== SHN_BAD
)
7958 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
7960 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
7961 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
7964 extdynend
= extdyn
+ s
->size
;
7965 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
7967 Elf_Internal_Dyn dyn
;
7969 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
7971 if (dyn
.d_tag
== DT_NULL
)
7974 if (dyn
.d_tag
== DT_NEEDED
)
7977 struct bfd_link_needed_list
*l
;
7978 unsigned int tagv
= dyn
.d_un
.d_val
;
7981 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
7986 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
8006 struct elf_symbuf_symbol
8008 unsigned long st_name
; /* Symbol name, index in string tbl */
8009 unsigned char st_info
; /* Type and binding attributes */
8010 unsigned char st_other
; /* Visibilty, and target specific */
8013 struct elf_symbuf_head
8015 struct elf_symbuf_symbol
*ssym
;
8017 unsigned int st_shndx
;
8024 Elf_Internal_Sym
*isym
;
8025 struct elf_symbuf_symbol
*ssym
;
8031 /* Sort references to symbols by ascending section number. */
8034 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
8036 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
8037 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
8039 if (s1
->st_shndx
!= s2
->st_shndx
)
8040 return s1
->st_shndx
> s2
->st_shndx
? 1 : -1;
8041 /* Final sort by the address of the sym in the symbuf ensures
8044 return s1
> s2
? 1 : -1;
8049 elf_sym_name_compare (const void *arg1
, const void *arg2
)
8051 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
8052 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
8053 int ret
= strcmp (s1
->name
, s2
->name
);
8056 if (s1
->u
.p
!= s2
->u
.p
)
8057 return s1
->u
.p
> s2
->u
.p
? 1 : -1;
8061 static struct elf_symbuf_head
*
8062 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
8064 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
8065 struct elf_symbuf_symbol
*ssym
;
8066 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
8067 size_t i
, shndx_count
, total_size
, amt
;
8069 amt
= symcount
* sizeof (*indbuf
);
8070 indbuf
= (Elf_Internal_Sym
**) bfd_malloc (amt
);
8074 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
8075 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
8076 *ind
++ = &isymbuf
[i
];
8079 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
8080 elf_sort_elf_symbol
);
8083 if (indbufend
> indbuf
)
8084 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
8085 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
8088 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
8089 + (indbufend
- indbuf
) * sizeof (*ssym
));
8090 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
8091 if (ssymbuf
== NULL
)
8097 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
8098 ssymbuf
->ssym
= NULL
;
8099 ssymbuf
->count
= shndx_count
;
8100 ssymbuf
->st_shndx
= 0;
8101 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
8103 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
8106 ssymhead
->ssym
= ssym
;
8107 ssymhead
->count
= 0;
8108 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
8110 ssym
->st_name
= (*ind
)->st_name
;
8111 ssym
->st_info
= (*ind
)->st_info
;
8112 ssym
->st_other
= (*ind
)->st_other
;
8115 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
8116 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
8123 /* Check if 2 sections define the same set of local and global
8127 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
8128 struct bfd_link_info
*info
)
8131 const struct elf_backend_data
*bed1
, *bed2
;
8132 Elf_Internal_Shdr
*hdr1
, *hdr2
;
8133 size_t symcount1
, symcount2
;
8134 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
8135 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
8136 Elf_Internal_Sym
*isym
, *isymend
;
8137 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
8138 size_t count1
, count2
, i
;
8139 unsigned int shndx1
, shndx2
;
8145 /* Both sections have to be in ELF. */
8146 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
8147 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
8150 if (elf_section_type (sec1
) != elf_section_type (sec2
))
8153 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
8154 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
8155 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
8158 bed1
= get_elf_backend_data (bfd1
);
8159 bed2
= get_elf_backend_data (bfd2
);
8160 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
8161 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
8162 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
8163 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
8165 if (symcount1
== 0 || symcount2
== 0)
8171 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
8172 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
8174 if (ssymbuf1
== NULL
)
8176 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
8178 if (isymbuf1
== NULL
)
8181 if (info
!= NULL
&& !info
->reduce_memory_overheads
)
8183 ssymbuf1
= elf_create_symbuf (symcount1
, isymbuf1
);
8184 elf_tdata (bfd1
)->symbuf
= ssymbuf1
;
8188 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
8190 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
8192 if (isymbuf2
== NULL
)
8195 if (ssymbuf1
!= NULL
&& info
!= NULL
&& !info
->reduce_memory_overheads
)
8197 ssymbuf2
= elf_create_symbuf (symcount2
, isymbuf2
);
8198 elf_tdata (bfd2
)->symbuf
= ssymbuf2
;
8202 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
8204 /* Optimized faster version. */
8206 struct elf_symbol
*symp
;
8207 struct elf_symbuf_symbol
*ssym
, *ssymend
;
8210 hi
= ssymbuf1
->count
;
8215 mid
= (lo
+ hi
) / 2;
8216 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
8218 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
8222 count1
= ssymbuf1
[mid
].count
;
8229 hi
= ssymbuf2
->count
;
8234 mid
= (lo
+ hi
) / 2;
8235 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
8237 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
8241 count2
= ssymbuf2
[mid
].count
;
8247 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8251 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
8253 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
8254 if (symtable1
== NULL
|| symtable2
== NULL
)
8258 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
8259 ssym
< ssymend
; ssym
++, symp
++)
8261 symp
->u
.ssym
= ssym
;
8262 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
8268 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
8269 ssym
< ssymend
; ssym
++, symp
++)
8271 symp
->u
.ssym
= ssym
;
8272 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
8277 /* Sort symbol by name. */
8278 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8279 elf_sym_name_compare
);
8280 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8281 elf_sym_name_compare
);
8283 for (i
= 0; i
< count1
; i
++)
8284 /* Two symbols must have the same binding, type and name. */
8285 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
8286 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
8287 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8294 symtable1
= (struct elf_symbol
*)
8295 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
8296 symtable2
= (struct elf_symbol
*)
8297 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
8298 if (symtable1
== NULL
|| symtable2
== NULL
)
8301 /* Count definitions in the section. */
8303 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
8304 if (isym
->st_shndx
== shndx1
)
8305 symtable1
[count1
++].u
.isym
= isym
;
8308 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
8309 if (isym
->st_shndx
== shndx2
)
8310 symtable2
[count2
++].u
.isym
= isym
;
8312 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8315 for (i
= 0; i
< count1
; i
++)
8317 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
8318 symtable1
[i
].u
.isym
->st_name
);
8320 for (i
= 0; i
< count2
; i
++)
8322 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
8323 symtable2
[i
].u
.isym
->st_name
);
8325 /* Sort symbol by name. */
8326 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8327 elf_sym_name_compare
);
8328 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8329 elf_sym_name_compare
);
8331 for (i
= 0; i
< count1
; i
++)
8332 /* Two symbols must have the same binding, type and name. */
8333 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
8334 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
8335 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8349 /* Return TRUE if 2 section types are compatible. */
8352 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
8353 bfd
*bbfd
, const asection
*bsec
)
8357 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
8358 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8361 return elf_section_type (asec
) == elf_section_type (bsec
);
8364 /* Final phase of ELF linker. */
8366 /* A structure we use to avoid passing large numbers of arguments. */
8368 struct elf_final_link_info
8370 /* General link information. */
8371 struct bfd_link_info
*info
;
8374 /* Symbol string table. */
8375 struct elf_strtab_hash
*symstrtab
;
8376 /* .hash section. */
8378 /* symbol version section (.gnu.version). */
8379 asection
*symver_sec
;
8380 /* Buffer large enough to hold contents of any section. */
8382 /* Buffer large enough to hold external relocs of any section. */
8383 void *external_relocs
;
8384 /* Buffer large enough to hold internal relocs of any section. */
8385 Elf_Internal_Rela
*internal_relocs
;
8386 /* Buffer large enough to hold external local symbols of any input
8388 bfd_byte
*external_syms
;
8389 /* And a buffer for symbol section indices. */
8390 Elf_External_Sym_Shndx
*locsym_shndx
;
8391 /* Buffer large enough to hold internal local symbols of any input
8393 Elf_Internal_Sym
*internal_syms
;
8394 /* Array large enough to hold a symbol index for each local symbol
8395 of any input BFD. */
8397 /* Array large enough to hold a section pointer for each local
8398 symbol of any input BFD. */
8399 asection
**sections
;
8400 /* Buffer for SHT_SYMTAB_SHNDX section. */
8401 Elf_External_Sym_Shndx
*symshndxbuf
;
8402 /* Number of STT_FILE syms seen. */
8403 size_t filesym_count
;
8404 /* Local symbol hash table. */
8405 struct bfd_hash_table local_hash_table
;
8408 struct local_hash_entry
8410 /* Base hash table entry structure. */
8411 struct bfd_hash_entry root
;
8412 /* Size of the local symbol name. */
8414 /* Number of the duplicated local symbol names. */
8418 /* Create an entry in the local symbol hash table. */
8420 static struct bfd_hash_entry
*
8421 local_hash_newfunc (struct bfd_hash_entry
*entry
,
8422 struct bfd_hash_table
*table
,
8426 /* Allocate the structure if it has not already been allocated by a
8430 entry
= bfd_hash_allocate (table
,
8431 sizeof (struct local_hash_entry
));
8436 /* Call the allocation method of the superclass. */
8437 entry
= bfd_hash_newfunc (entry
, table
, string
);
8440 ((struct local_hash_entry
*) entry
)->count
= 0;
8441 ((struct local_hash_entry
*) entry
)->size
= 0;
8447 /* This struct is used to pass information to elf_link_output_extsym. */
8449 struct elf_outext_info
8452 bfd_boolean localsyms
;
8453 bfd_boolean file_sym_done
;
8454 struct elf_final_link_info
*flinfo
;
8458 /* Support for evaluating a complex relocation.
8460 Complex relocations are generalized, self-describing relocations. The
8461 implementation of them consists of two parts: complex symbols, and the
8462 relocations themselves.
8464 The relocations use a reserved elf-wide relocation type code (R_RELC
8465 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8466 information (start bit, end bit, word width, etc) into the addend. This
8467 information is extracted from CGEN-generated operand tables within gas.
8469 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8470 internal) representing prefix-notation expressions, including but not
8471 limited to those sorts of expressions normally encoded as addends in the
8472 addend field. The symbol mangling format is:
8475 | <unary-operator> ':' <node>
8476 | <binary-operator> ':' <node> ':' <node>
8479 <literal> := 's' <digits=N> ':' <N character symbol name>
8480 | 'S' <digits=N> ':' <N character section name>
8484 <binary-operator> := as in C
8485 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8488 set_symbol_value (bfd
*bfd_with_globals
,
8489 Elf_Internal_Sym
*isymbuf
,
8494 struct elf_link_hash_entry
**sym_hashes
;
8495 struct elf_link_hash_entry
*h
;
8496 size_t extsymoff
= locsymcount
;
8498 if (symidx
< locsymcount
)
8500 Elf_Internal_Sym
*sym
;
8502 sym
= isymbuf
+ symidx
;
8503 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
8505 /* It is a local symbol: move it to the
8506 "absolute" section and give it a value. */
8507 sym
->st_shndx
= SHN_ABS
;
8508 sym
->st_value
= val
;
8511 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
8515 /* It is a global symbol: set its link type
8516 to "defined" and give it a value. */
8518 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
8519 h
= sym_hashes
[symidx
- extsymoff
];
8520 while (h
->root
.type
== bfd_link_hash_indirect
8521 || h
->root
.type
== bfd_link_hash_warning
)
8522 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8523 h
->root
.type
= bfd_link_hash_defined
;
8524 h
->root
.u
.def
.value
= val
;
8525 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
8529 resolve_symbol (const char *name
,
8531 struct elf_final_link_info
*flinfo
,
8533 Elf_Internal_Sym
*isymbuf
,
8536 Elf_Internal_Sym
*sym
;
8537 struct bfd_link_hash_entry
*global_entry
;
8538 const char *candidate
= NULL
;
8539 Elf_Internal_Shdr
*symtab_hdr
;
8542 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
8544 for (i
= 0; i
< locsymcount
; ++ i
)
8548 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
8551 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
8552 symtab_hdr
->sh_link
,
8555 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8556 name
, candidate
, (unsigned long) sym
->st_value
);
8558 if (candidate
&& strcmp (candidate
, name
) == 0)
8560 asection
*sec
= flinfo
->sections
[i
];
8562 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
8563 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
8565 printf ("Found symbol with value %8.8lx\n",
8566 (unsigned long) *result
);
8572 /* Hmm, haven't found it yet. perhaps it is a global. */
8573 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
8574 FALSE
, FALSE
, TRUE
);
8578 if (global_entry
->type
== bfd_link_hash_defined
8579 || global_entry
->type
== bfd_link_hash_defweak
)
8581 *result
= (global_entry
->u
.def
.value
8582 + global_entry
->u
.def
.section
->output_section
->vma
8583 + global_entry
->u
.def
.section
->output_offset
);
8585 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8586 global_entry
->root
.string
, (unsigned long) *result
);
8594 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8595 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8596 names like "foo.end" which is the end address of section "foo". */
8599 resolve_section (const char *name
,
8607 for (curr
= sections
; curr
; curr
= curr
->next
)
8608 if (strcmp (curr
->name
, name
) == 0)
8610 *result
= curr
->vma
;
8614 /* Hmm. still haven't found it. try pseudo-section names. */
8615 /* FIXME: This could be coded more efficiently... */
8616 for (curr
= sections
; curr
; curr
= curr
->next
)
8618 len
= strlen (curr
->name
);
8619 if (len
> strlen (name
))
8622 if (strncmp (curr
->name
, name
, len
) == 0)
8624 if (strncmp (".end", name
+ len
, 4) == 0)
8626 *result
= (curr
->vma
8627 + curr
->size
/ bfd_octets_per_byte (abfd
, curr
));
8631 /* Insert more pseudo-section names here, if you like. */
8639 undefined_reference (const char *reftype
, const char *name
)
8641 /* xgettext:c-format */
8642 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8644 bfd_set_error (bfd_error_bad_value
);
8648 eval_symbol (bfd_vma
*result
,
8651 struct elf_final_link_info
*flinfo
,
8653 Elf_Internal_Sym
*isymbuf
,
8662 const char *sym
= *symp
;
8664 bfd_boolean symbol_is_section
= FALSE
;
8669 if (len
< 1 || len
> sizeof (symbuf
))
8671 bfd_set_error (bfd_error_invalid_operation
);
8684 *result
= strtoul (sym
, (char **) symp
, 16);
8688 symbol_is_section
= TRUE
;
8692 symlen
= strtol (sym
, (char **) symp
, 10);
8693 sym
= *symp
+ 1; /* Skip the trailing ':'. */
8695 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
8697 bfd_set_error (bfd_error_invalid_operation
);
8701 memcpy (symbuf
, sym
, symlen
);
8702 symbuf
[symlen
] = '\0';
8703 *symp
= sym
+ symlen
;
8705 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8706 the symbol as a section, or vice-versa. so we're pretty liberal in our
8707 interpretation here; section means "try section first", not "must be a
8708 section", and likewise with symbol. */
8710 if (symbol_is_section
)
8712 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
8713 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8714 isymbuf
, locsymcount
))
8716 undefined_reference ("section", symbuf
);
8722 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8723 isymbuf
, locsymcount
)
8724 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8727 undefined_reference ("symbol", symbuf
);
8734 /* All that remains are operators. */
8736 #define UNARY_OP(op) \
8737 if (strncmp (sym, #op, strlen (#op)) == 0) \
8739 sym += strlen (#op); \
8743 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8744 isymbuf, locsymcount, signed_p)) \
8747 *result = op ((bfd_signed_vma) a); \
8753 #define BINARY_OP_HEAD(op) \
8754 if (strncmp (sym, #op, strlen (#op)) == 0) \
8756 sym += strlen (#op); \
8760 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8761 isymbuf, locsymcount, signed_p)) \
8764 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8765 isymbuf, locsymcount, signed_p)) \
8767 #define BINARY_OP_TAIL(op) \
8769 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8774 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
8778 BINARY_OP_HEAD (<<);
8779 if (b
>= sizeof (a
) * CHAR_BIT
)
8785 BINARY_OP_TAIL (<<);
8786 BINARY_OP_HEAD (>>);
8787 if (b
>= sizeof (a
) * CHAR_BIT
)
8789 *result
= signed_p
&& (bfd_signed_vma
) a
< 0 ? -1 : 0;
8792 BINARY_OP_TAIL (>>);
8805 _bfd_error_handler (_("division by zero"));
8806 bfd_set_error (bfd_error_bad_value
);
8813 _bfd_error_handler (_("division by zero"));
8814 bfd_set_error (bfd_error_bad_value
);
8827 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
8828 bfd_set_error (bfd_error_invalid_operation
);
8834 put_value (bfd_vma size
,
8835 unsigned long chunksz
,
8840 location
+= (size
- chunksz
);
8842 for (; size
; size
-= chunksz
, location
-= chunksz
)
8847 bfd_put_8 (input_bfd
, x
, location
);
8851 bfd_put_16 (input_bfd
, x
, location
);
8855 bfd_put_32 (input_bfd
, x
, location
);
8856 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8862 bfd_put_64 (input_bfd
, x
, location
);
8863 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8876 get_value (bfd_vma size
,
8877 unsigned long chunksz
,
8884 /* Sanity checks. */
8885 BFD_ASSERT (chunksz
<= sizeof (x
)
8888 && (size
% chunksz
) == 0
8889 && input_bfd
!= NULL
8890 && location
!= NULL
);
8892 if (chunksz
== sizeof (x
))
8894 BFD_ASSERT (size
== chunksz
);
8896 /* Make sure that we do not perform an undefined shift operation.
8897 We know that size == chunksz so there will only be one iteration
8898 of the loop below. */
8902 shift
= 8 * chunksz
;
8904 for (; size
; size
-= chunksz
, location
+= chunksz
)
8909 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
8912 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
8915 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
8919 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
8930 decode_complex_addend (unsigned long *start
, /* in bits */
8931 unsigned long *oplen
, /* in bits */
8932 unsigned long *len
, /* in bits */
8933 unsigned long *wordsz
, /* in bytes */
8934 unsigned long *chunksz
, /* in bytes */
8935 unsigned long *lsb0_p
,
8936 unsigned long *signed_p
,
8937 unsigned long *trunc_p
,
8938 unsigned long encoded
)
8940 * start
= encoded
& 0x3F;
8941 * len
= (encoded
>> 6) & 0x3F;
8942 * oplen
= (encoded
>> 12) & 0x3F;
8943 * wordsz
= (encoded
>> 18) & 0xF;
8944 * chunksz
= (encoded
>> 22) & 0xF;
8945 * lsb0_p
= (encoded
>> 27) & 1;
8946 * signed_p
= (encoded
>> 28) & 1;
8947 * trunc_p
= (encoded
>> 29) & 1;
8950 bfd_reloc_status_type
8951 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
8952 asection
*input_section
,
8954 Elf_Internal_Rela
*rel
,
8957 bfd_vma shift
, x
, mask
;
8958 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
8959 bfd_reloc_status_type r
;
8960 bfd_size_type octets
;
8962 /* Perform this reloc, since it is complex.
8963 (this is not to say that it necessarily refers to a complex
8964 symbol; merely that it is a self-describing CGEN based reloc.
8965 i.e. the addend has the complete reloc information (bit start, end,
8966 word size, etc) encoded within it.). */
8968 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
8969 &chunksz
, &lsb0_p
, &signed_p
,
8970 &trunc_p
, rel
->r_addend
);
8972 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
8975 shift
= (start
+ 1) - len
;
8977 shift
= (8 * wordsz
) - (start
+ len
);
8979 octets
= rel
->r_offset
* bfd_octets_per_byte (input_bfd
, input_section
);
8980 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ octets
);
8983 printf ("Doing complex reloc: "
8984 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8985 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8986 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8987 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
8988 oplen
, (unsigned long) x
, (unsigned long) mask
,
8989 (unsigned long) relocation
);
8994 /* Now do an overflow check. */
8995 r
= bfd_check_overflow ((signed_p
8996 ? complain_overflow_signed
8997 : complain_overflow_unsigned
),
8998 len
, 0, (8 * wordsz
),
9002 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
9005 printf (" relocation: %8.8lx\n"
9006 " shifted mask: %8.8lx\n"
9007 " shifted/masked reloc: %8.8lx\n"
9008 " result: %8.8lx\n",
9009 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
9010 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
9012 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ octets
);
9016 /* Functions to read r_offset from external (target order) reloc
9017 entry. Faster than bfd_getl32 et al, because we let the compiler
9018 know the value is aligned. */
9021 ext32l_r_offset (const void *p
)
9028 const union aligned32
*a
9029 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9031 uint32_t aval
= ( (uint32_t) a
->c
[0]
9032 | (uint32_t) a
->c
[1] << 8
9033 | (uint32_t) a
->c
[2] << 16
9034 | (uint32_t) a
->c
[3] << 24);
9039 ext32b_r_offset (const void *p
)
9046 const union aligned32
*a
9047 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9049 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
9050 | (uint32_t) a
->c
[1] << 16
9051 | (uint32_t) a
->c
[2] << 8
9052 | (uint32_t) a
->c
[3]);
9056 #ifdef BFD_HOST_64_BIT
9058 ext64l_r_offset (const void *p
)
9065 const union aligned64
*a
9066 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9068 uint64_t aval
= ( (uint64_t) a
->c
[0]
9069 | (uint64_t) a
->c
[1] << 8
9070 | (uint64_t) a
->c
[2] << 16
9071 | (uint64_t) a
->c
[3] << 24
9072 | (uint64_t) a
->c
[4] << 32
9073 | (uint64_t) a
->c
[5] << 40
9074 | (uint64_t) a
->c
[6] << 48
9075 | (uint64_t) a
->c
[7] << 56);
9080 ext64b_r_offset (const void *p
)
9087 const union aligned64
*a
9088 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9090 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
9091 | (uint64_t) a
->c
[1] << 48
9092 | (uint64_t) a
->c
[2] << 40
9093 | (uint64_t) a
->c
[3] << 32
9094 | (uint64_t) a
->c
[4] << 24
9095 | (uint64_t) a
->c
[5] << 16
9096 | (uint64_t) a
->c
[6] << 8
9097 | (uint64_t) a
->c
[7]);
9102 /* When performing a relocatable link, the input relocations are
9103 preserved. But, if they reference global symbols, the indices
9104 referenced must be updated. Update all the relocations found in
9108 elf_link_adjust_relocs (bfd
*abfd
,
9110 struct bfd_elf_section_reloc_data
*reldata
,
9112 struct bfd_link_info
*info
)
9115 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9117 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9118 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9119 bfd_vma r_type_mask
;
9121 unsigned int count
= reldata
->count
;
9122 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
9124 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
9126 swap_in
= bed
->s
->swap_reloc_in
;
9127 swap_out
= bed
->s
->swap_reloc_out
;
9129 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
9131 swap_in
= bed
->s
->swap_reloca_in
;
9132 swap_out
= bed
->s
->swap_reloca_out
;
9137 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
9140 if (bed
->s
->arch_size
== 32)
9147 r_type_mask
= 0xffffffff;
9151 erela
= reldata
->hdr
->contents
;
9152 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
9154 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
9157 if (*rel_hash
== NULL
)
9160 if ((*rel_hash
)->indx
== -2
9161 && info
->gc_sections
9162 && ! info
->gc_keep_exported
)
9164 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9165 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9167 (*rel_hash
)->root
.root
.string
);
9168 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9170 bfd_set_error (bfd_error_invalid_operation
);
9173 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
9175 (*swap_in
) (abfd
, erela
, irela
);
9176 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
9177 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
9178 | (irela
[j
].r_info
& r_type_mask
));
9179 (*swap_out
) (abfd
, irela
, erela
);
9182 if (bed
->elf_backend_update_relocs
)
9183 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
9185 if (sort
&& count
!= 0)
9187 bfd_vma (*ext_r_off
) (const void *);
9190 bfd_byte
*base
, *end
, *p
, *loc
;
9191 bfd_byte
*buf
= NULL
;
9193 if (bed
->s
->arch_size
== 32)
9195 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9196 ext_r_off
= ext32l_r_offset
;
9197 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9198 ext_r_off
= ext32b_r_offset
;
9204 #ifdef BFD_HOST_64_BIT
9205 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9206 ext_r_off
= ext64l_r_offset
;
9207 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9208 ext_r_off
= ext64b_r_offset
;
9214 /* Must use a stable sort here. A modified insertion sort,
9215 since the relocs are mostly sorted already. */
9216 elt_size
= reldata
->hdr
->sh_entsize
;
9217 base
= reldata
->hdr
->contents
;
9218 end
= base
+ count
* elt_size
;
9219 if (elt_size
> sizeof (Elf64_External_Rela
))
9222 /* Ensure the first element is lowest. This acts as a sentinel,
9223 speeding the main loop below. */
9224 r_off
= (*ext_r_off
) (base
);
9225 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
9227 bfd_vma r_off2
= (*ext_r_off
) (p
);
9236 /* Don't just swap *base and *loc as that changes the order
9237 of the original base[0] and base[1] if they happen to
9238 have the same r_offset. */
9239 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
9240 memcpy (onebuf
, loc
, elt_size
);
9241 memmove (base
+ elt_size
, base
, loc
- base
);
9242 memcpy (base
, onebuf
, elt_size
);
9245 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
9247 /* base to p is sorted, *p is next to insert. */
9248 r_off
= (*ext_r_off
) (p
);
9249 /* Search the sorted region for location to insert. */
9251 while (r_off
< (*ext_r_off
) (loc
))
9256 /* Chances are there is a run of relocs to insert here,
9257 from one of more input files. Files are not always
9258 linked in order due to the way elf_link_input_bfd is
9259 called. See pr17666. */
9260 size_t sortlen
= p
- loc
;
9261 bfd_vma r_off2
= (*ext_r_off
) (loc
);
9262 size_t runlen
= elt_size
;
9263 size_t buf_size
= 96 * 1024;
9264 while (p
+ runlen
< end
9265 && (sortlen
<= buf_size
9266 || runlen
+ elt_size
<= buf_size
)
9267 && r_off2
> (*ext_r_off
) (p
+ runlen
))
9271 buf
= bfd_malloc (buf_size
);
9275 if (runlen
< sortlen
)
9277 memcpy (buf
, p
, runlen
);
9278 memmove (loc
+ runlen
, loc
, sortlen
);
9279 memcpy (loc
, buf
, runlen
);
9283 memcpy (buf
, loc
, sortlen
);
9284 memmove (loc
, p
, runlen
);
9285 memcpy (loc
+ runlen
, buf
, sortlen
);
9287 p
+= runlen
- elt_size
;
9290 /* Hashes are no longer valid. */
9291 free (reldata
->hashes
);
9292 reldata
->hashes
= NULL
;
9298 struct elf_link_sort_rela
9304 enum elf_reloc_type_class type
;
9305 /* We use this as an array of size int_rels_per_ext_rel. */
9306 Elf_Internal_Rela rela
[1];
9309 /* qsort stability here and for cmp2 is only an issue if multiple
9310 dynamic relocations are emitted at the same address. But targets
9311 that apply a series of dynamic relocations each operating on the
9312 result of the prior relocation can't use -z combreloc as
9313 implemented anyway. Such schemes tend to be broken by sorting on
9314 symbol index. That leaves dynamic NONE relocs as the only other
9315 case where ld might emit multiple relocs at the same address, and
9316 those are only emitted due to target bugs. */
9319 elf_link_sort_cmp1 (const void *A
, const void *B
)
9321 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9322 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9323 int relativea
, relativeb
;
9325 relativea
= a
->type
== reloc_class_relative
;
9326 relativeb
= b
->type
== reloc_class_relative
;
9328 if (relativea
< relativeb
)
9330 if (relativea
> relativeb
)
9332 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
9334 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
9336 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9338 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9344 elf_link_sort_cmp2 (const void *A
, const void *B
)
9346 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9347 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9349 if (a
->type
< b
->type
)
9351 if (a
->type
> b
->type
)
9353 if (a
->u
.offset
< b
->u
.offset
)
9355 if (a
->u
.offset
> b
->u
.offset
)
9357 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9359 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9365 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
9367 asection
*dynamic_relocs
;
9370 bfd_size_type count
, size
;
9371 size_t i
, ret
, sort_elt
, ext_size
;
9372 bfd_byte
*sort
, *s_non_relative
, *p
;
9373 struct elf_link_sort_rela
*sq
;
9374 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9375 int i2e
= bed
->s
->int_rels_per_ext_rel
;
9376 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
9377 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9378 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9379 struct bfd_link_order
*lo
;
9381 bfd_boolean use_rela
;
9383 /* Find a dynamic reloc section. */
9384 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
9385 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
9386 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
9387 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9389 bfd_boolean use_rela_initialised
= FALSE
;
9391 /* This is just here to stop gcc from complaining.
9392 Its initialization checking code is not perfect. */
9395 /* Both sections are present. Examine the sizes
9396 of the indirect sections to help us choose. */
9397 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9398 if (lo
->type
== bfd_indirect_link_order
)
9400 asection
*o
= lo
->u
.indirect
.section
;
9402 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9404 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9405 /* Section size is divisible by both rel and rela sizes.
9406 It is of no help to us. */
9410 /* Section size is only divisible by rela. */
9411 if (use_rela_initialised
&& !use_rela
)
9413 _bfd_error_handler (_("%pB: unable to sort relocs - "
9414 "they are in more than one size"),
9416 bfd_set_error (bfd_error_invalid_operation
);
9422 use_rela_initialised
= TRUE
;
9426 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9428 /* Section size is only divisible by rel. */
9429 if (use_rela_initialised
&& use_rela
)
9431 _bfd_error_handler (_("%pB: unable to sort relocs - "
9432 "they are in more than one size"),
9434 bfd_set_error (bfd_error_invalid_operation
);
9440 use_rela_initialised
= TRUE
;
9445 /* The section size is not divisible by either -
9446 something is wrong. */
9447 _bfd_error_handler (_("%pB: unable to sort relocs - "
9448 "they are of an unknown size"), abfd
);
9449 bfd_set_error (bfd_error_invalid_operation
);
9454 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9455 if (lo
->type
== bfd_indirect_link_order
)
9457 asection
*o
= lo
->u
.indirect
.section
;
9459 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9461 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9462 /* Section size is divisible by both rel and rela sizes.
9463 It is of no help to us. */
9467 /* Section size is only divisible by rela. */
9468 if (use_rela_initialised
&& !use_rela
)
9470 _bfd_error_handler (_("%pB: unable to sort relocs - "
9471 "they are in more than one size"),
9473 bfd_set_error (bfd_error_invalid_operation
);
9479 use_rela_initialised
= TRUE
;
9483 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9485 /* Section size is only divisible by rel. */
9486 if (use_rela_initialised
&& use_rela
)
9488 _bfd_error_handler (_("%pB: unable to sort relocs - "
9489 "they are in more than one size"),
9491 bfd_set_error (bfd_error_invalid_operation
);
9497 use_rela_initialised
= TRUE
;
9502 /* The section size is not divisible by either -
9503 something is wrong. */
9504 _bfd_error_handler (_("%pB: unable to sort relocs - "
9505 "they are of an unknown size"), abfd
);
9506 bfd_set_error (bfd_error_invalid_operation
);
9511 if (! use_rela_initialised
)
9515 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
9517 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9524 dynamic_relocs
= rela_dyn
;
9525 ext_size
= bed
->s
->sizeof_rela
;
9526 swap_in
= bed
->s
->swap_reloca_in
;
9527 swap_out
= bed
->s
->swap_reloca_out
;
9531 dynamic_relocs
= rel_dyn
;
9532 ext_size
= bed
->s
->sizeof_rel
;
9533 swap_in
= bed
->s
->swap_reloc_in
;
9534 swap_out
= bed
->s
->swap_reloc_out
;
9538 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9539 if (lo
->type
== bfd_indirect_link_order
)
9540 size
+= lo
->u
.indirect
.section
->size
;
9542 if (size
!= dynamic_relocs
->size
)
9545 sort_elt
= (sizeof (struct elf_link_sort_rela
)
9546 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
9548 count
= dynamic_relocs
->size
/ ext_size
;
9551 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
9555 (*info
->callbacks
->warning
)
9556 (info
, _("not enough memory to sort relocations"), 0, abfd
, 0, 0);
9560 if (bed
->s
->arch_size
== 32)
9561 r_sym_mask
= ~(bfd_vma
) 0xff;
9563 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
9565 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9566 if (lo
->type
== bfd_indirect_link_order
)
9568 bfd_byte
*erel
, *erelend
;
9569 asection
*o
= lo
->u
.indirect
.section
;
9571 if (o
->contents
== NULL
&& o
->size
!= 0)
9573 /* This is a reloc section that is being handled as a normal
9574 section. See bfd_section_from_shdr. We can't combine
9575 relocs in this case. */
9580 erelend
= o
->contents
+ o
->size
;
9581 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
9583 while (erel
< erelend
)
9585 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9587 (*swap_in
) (abfd
, erel
, s
->rela
);
9588 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
9589 s
->u
.sym_mask
= r_sym_mask
;
9595 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
9597 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
9599 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9600 if (s
->type
!= reloc_class_relative
)
9606 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
9607 for (; i
< count
; i
++, p
+= sort_elt
)
9609 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
9610 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
9612 sp
->u
.offset
= sq
->rela
->r_offset
;
9615 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
9617 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
9618 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
9620 /* We have plt relocs in .rela.dyn. */
9621 sq
= (struct elf_link_sort_rela
*) sort
;
9622 for (i
= 0; i
< count
; i
++)
9623 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
9625 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
9627 struct bfd_link_order
**plo
;
9628 /* Put srelplt link_order last. This is so the output_offset
9629 set in the next loop is correct for DT_JMPREL. */
9630 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
9631 if ((*plo
)->type
== bfd_indirect_link_order
9632 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
9638 plo
= &(*plo
)->next
;
9641 dynamic_relocs
->map_tail
.link_order
= lo
;
9646 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9647 if (lo
->type
== bfd_indirect_link_order
)
9649 bfd_byte
*erel
, *erelend
;
9650 asection
*o
= lo
->u
.indirect
.section
;
9653 erelend
= o
->contents
+ o
->size
;
9654 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
9655 while (erel
< erelend
)
9657 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9658 (*swap_out
) (abfd
, s
->rela
, erel
);
9665 *psec
= dynamic_relocs
;
9669 /* Add a symbol to the output symbol string table. */
9672 elf_link_output_symstrtab (struct elf_final_link_info
*flinfo
,
9674 Elf_Internal_Sym
*elfsym
,
9675 asection
*input_sec
,
9676 struct elf_link_hash_entry
*h
)
9678 int (*output_symbol_hook
)
9679 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
9680 struct elf_link_hash_entry
*);
9681 struct elf_link_hash_table
*hash_table
;
9682 const struct elf_backend_data
*bed
;
9683 bfd_size_type strtabsize
;
9685 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9687 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9688 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
9689 if (output_symbol_hook
!= NULL
)
9691 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
9696 if (ELF_ST_TYPE (elfsym
->st_info
) == STT_GNU_IFUNC
)
9697 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_ifunc
;
9698 if (ELF_ST_BIND (elfsym
->st_info
) == STB_GNU_UNIQUE
)
9699 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_unique
;
9703 || (input_sec
->flags
& SEC_EXCLUDE
))
9704 elfsym
->st_name
= (unsigned long) -1;
9707 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9708 to get the final offset for st_name. */
9709 char *versioned_name
= (char *) name
;
9712 if (h
->versioned
== versioned
&& h
->def_dynamic
)
9714 /* Keep only one '@' for versioned symbols defined in
9716 char *version
= strrchr (name
, ELF_VER_CHR
);
9717 char *base_end
= strchr (name
, ELF_VER_CHR
);
9718 if (version
!= base_end
)
9721 size_t len
= strlen (name
);
9722 versioned_name
= bfd_alloc (flinfo
->output_bfd
, len
);
9723 if (versioned_name
== NULL
)
9725 base_len
= base_end
- name
;
9726 memcpy (versioned_name
, name
, base_len
);
9727 memcpy (versioned_name
+ base_len
, version
,
9732 else if (flinfo
->info
->unique_symbol
9733 && ELF_ST_BIND (elfsym
->st_info
) == STB_LOCAL
)
9735 struct local_hash_entry
*lh
;
9736 switch (ELF_ST_TYPE (elfsym
->st_info
))
9742 lh
= (struct local_hash_entry
*) bfd_hash_lookup
9743 (&flinfo
->local_hash_table
, name
, TRUE
, FALSE
);
9748 /* Append ".COUNT" to duplicated local symbols. */
9750 size_t base_len
= lh
->size
;
9752 sprintf (buf
, "%lx", lh
->count
);
9755 base_len
= strlen (name
);
9756 lh
->size
= base_len
;
9758 count_len
= strlen (buf
);
9759 versioned_name
= bfd_alloc (flinfo
->output_bfd
,
9760 base_len
+ count_len
+ 2);
9761 if (versioned_name
== NULL
)
9763 memcpy (versioned_name
, name
, base_len
);
9764 versioned_name
[base_len
] = '.';
9765 memcpy (versioned_name
+ base_len
+ 1, buf
,
9773 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
9774 versioned_name
, FALSE
);
9775 if (elfsym
->st_name
== (unsigned long) -1)
9779 hash_table
= elf_hash_table (flinfo
->info
);
9780 strtabsize
= hash_table
->strtabsize
;
9781 if (strtabsize
<= hash_table
->strtabcount
)
9783 strtabsize
+= strtabsize
;
9784 hash_table
->strtabsize
= strtabsize
;
9785 strtabsize
*= sizeof (*hash_table
->strtab
);
9787 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
9789 if (hash_table
->strtab
== NULL
)
9792 hash_table
->strtab
[hash_table
->strtabcount
].sym
= *elfsym
;
9793 hash_table
->strtab
[hash_table
->strtabcount
].dest_index
9794 = hash_table
->strtabcount
;
9795 hash_table
->strtab
[hash_table
->strtabcount
].destshndx_index
9796 = flinfo
->symshndxbuf
? bfd_get_symcount (flinfo
->output_bfd
) : 0;
9798 flinfo
->output_bfd
->symcount
+= 1;
9799 hash_table
->strtabcount
+= 1;
9804 /* Swap symbols out to the symbol table and flush the output symbols to
9808 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
9810 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
9813 const struct elf_backend_data
*bed
;
9815 Elf_Internal_Shdr
*hdr
;
9819 if (!hash_table
->strtabcount
)
9822 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9824 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9826 amt
= bed
->s
->sizeof_sym
* hash_table
->strtabcount
;
9827 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
9831 if (flinfo
->symshndxbuf
)
9833 amt
= sizeof (Elf_External_Sym_Shndx
);
9834 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
9835 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
9836 if (flinfo
->symshndxbuf
== NULL
)
9843 for (i
= 0; i
< hash_table
->strtabcount
; i
++)
9845 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
9846 if (elfsym
->sym
.st_name
== (unsigned long) -1)
9847 elfsym
->sym
.st_name
= 0;
9850 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
9851 elfsym
->sym
.st_name
);
9852 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
9853 ((bfd_byte
*) symbuf
9854 + (elfsym
->dest_index
9855 * bed
->s
->sizeof_sym
)),
9856 (flinfo
->symshndxbuf
9857 + elfsym
->destshndx_index
));
9860 /* Allow the linker to examine the strtab and symtab now they are
9863 if (flinfo
->info
->callbacks
->examine_strtab
)
9864 flinfo
->info
->callbacks
->examine_strtab (hash_table
->strtab
,
9865 hash_table
->strtabcount
,
9868 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
9869 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
9870 amt
= hash_table
->strtabcount
* bed
->s
->sizeof_sym
;
9871 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
9872 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
9874 hdr
->sh_size
+= amt
;
9882 free (hash_table
->strtab
);
9883 hash_table
->strtab
= NULL
;
9888 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9891 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
9893 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
9894 && sym
->st_shndx
< SHN_LORESERVE
)
9896 /* The gABI doesn't support dynamic symbols in output sections
9899 /* xgettext:c-format */
9900 (_("%pB: too many sections: %d (>= %d)"),
9901 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
9902 bfd_set_error (bfd_error_nonrepresentable_section
);
9908 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9909 allowing an unsatisfied unversioned symbol in the DSO to match a
9910 versioned symbol that would normally require an explicit version.
9911 We also handle the case that a DSO references a hidden symbol
9912 which may be satisfied by a versioned symbol in another DSO. */
9915 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
9916 const struct elf_backend_data
*bed
,
9917 struct elf_link_hash_entry
*h
)
9920 struct elf_link_loaded_list
*loaded
;
9922 if (!is_elf_hash_table (info
->hash
))
9925 /* Check indirect symbol. */
9926 while (h
->root
.type
== bfd_link_hash_indirect
)
9927 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9929 switch (h
->root
.type
)
9935 case bfd_link_hash_undefined
:
9936 case bfd_link_hash_undefweak
:
9937 abfd
= h
->root
.u
.undef
.abfd
;
9939 || (abfd
->flags
& DYNAMIC
) == 0
9940 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
9944 case bfd_link_hash_defined
:
9945 case bfd_link_hash_defweak
:
9946 abfd
= h
->root
.u
.def
.section
->owner
;
9949 case bfd_link_hash_common
:
9950 abfd
= h
->root
.u
.c
.p
->section
->owner
;
9953 BFD_ASSERT (abfd
!= NULL
);
9955 for (loaded
= elf_hash_table (info
)->dyn_loaded
;
9957 loaded
= loaded
->next
)
9960 Elf_Internal_Shdr
*hdr
;
9964 Elf_Internal_Shdr
*versymhdr
;
9965 Elf_Internal_Sym
*isym
;
9966 Elf_Internal_Sym
*isymend
;
9967 Elf_Internal_Sym
*isymbuf
;
9968 Elf_External_Versym
*ever
;
9969 Elf_External_Versym
*extversym
;
9971 input
= loaded
->abfd
;
9973 /* We check each DSO for a possible hidden versioned definition. */
9975 || elf_dynversym (input
) == 0)
9978 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
9980 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9981 if (elf_bad_symtab (input
))
9983 extsymcount
= symcount
;
9988 extsymcount
= symcount
- hdr
->sh_info
;
9989 extsymoff
= hdr
->sh_info
;
9992 if (extsymcount
== 0)
9995 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
9997 if (isymbuf
== NULL
)
10000 /* Read in any version definitions. */
10001 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
10002 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
10003 || (extversym
= (Elf_External_Versym
*)
10004 _bfd_malloc_and_read (input
, versymhdr
->sh_size
,
10005 versymhdr
->sh_size
)) == NULL
)
10011 ever
= extversym
+ extsymoff
;
10012 isymend
= isymbuf
+ extsymcount
;
10013 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
10016 Elf_Internal_Versym iver
;
10017 unsigned short version_index
;
10019 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
10020 || isym
->st_shndx
== SHN_UNDEF
)
10023 name
= bfd_elf_string_from_elf_section (input
,
10026 if (strcmp (name
, h
->root
.root
.string
) != 0)
10029 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
10031 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
10032 && !(h
->def_regular
10033 && h
->forced_local
))
10035 /* If we have a non-hidden versioned sym, then it should
10036 have provided a definition for the undefined sym unless
10037 it is defined in a non-shared object and forced local.
10042 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
10043 if (version_index
== 1 || version_index
== 2)
10045 /* This is the base or first version. We can use it. */
10059 /* Convert ELF common symbol TYPE. */
10062 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
10064 /* Commom symbol can only appear in relocatable link. */
10065 if (!bfd_link_relocatable (info
))
10067 switch (info
->elf_stt_common
)
10071 case elf_stt_common
:
10074 case no_elf_stt_common
:
10081 /* Add an external symbol to the symbol table. This is called from
10082 the hash table traversal routine. When generating a shared object,
10083 we go through the symbol table twice. The first time we output
10084 anything that might have been forced to local scope in a version
10085 script. The second time we output the symbols that are still
10089 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
10091 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
10092 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
10093 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
10095 Elf_Internal_Sym sym
;
10096 asection
*input_sec
;
10097 const struct elf_backend_data
*bed
;
10102 if (h
->root
.type
== bfd_link_hash_warning
)
10104 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10105 if (h
->root
.type
== bfd_link_hash_new
)
10109 /* Decide whether to output this symbol in this pass. */
10110 if (eoinfo
->localsyms
)
10112 if (!h
->forced_local
)
10117 if (h
->forced_local
)
10121 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10123 if (h
->root
.type
== bfd_link_hash_undefined
)
10125 /* If we have an undefined symbol reference here then it must have
10126 come from a shared library that is being linked in. (Undefined
10127 references in regular files have already been handled unless
10128 they are in unreferenced sections which are removed by garbage
10130 bfd_boolean ignore_undef
= FALSE
;
10132 /* Some symbols may be special in that the fact that they're
10133 undefined can be safely ignored - let backend determine that. */
10134 if (bed
->elf_backend_ignore_undef_symbol
)
10135 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
10137 /* If we are reporting errors for this situation then do so now. */
10139 && h
->ref_dynamic_nonweak
10140 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
10141 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
10142 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
10144 flinfo
->info
->callbacks
->undefined_symbol
10145 (flinfo
->info
, h
->root
.root
.string
,
10146 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
, NULL
, 0,
10147 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_DIAGNOSE
10148 && !flinfo
->info
->warn_unresolved_syms
);
10151 /* Strip a global symbol defined in a discarded section. */
10156 /* We should also warn if a forced local symbol is referenced from
10157 shared libraries. */
10158 if (bfd_link_executable (flinfo
->info
)
10163 && h
->ref_dynamic_nonweak
10164 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
10168 struct elf_link_hash_entry
*hi
= h
;
10170 /* Check indirect symbol. */
10171 while (hi
->root
.type
== bfd_link_hash_indirect
)
10172 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
10174 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
10175 /* xgettext:c-format */
10176 msg
= _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10177 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
10178 /* xgettext:c-format */
10179 msg
= _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10181 /* xgettext:c-format */
10182 msg
= _("%pB: local symbol `%s' in %pB is referenced by DSO");
10183 def_bfd
= flinfo
->output_bfd
;
10184 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
10185 def_bfd
= hi
->root
.u
.def
.section
->owner
;
10186 _bfd_error_handler (msg
, flinfo
->output_bfd
,
10187 h
->root
.root
.string
, def_bfd
);
10188 bfd_set_error (bfd_error_bad_value
);
10189 eoinfo
->failed
= TRUE
;
10193 /* We don't want to output symbols that have never been mentioned by
10194 a regular file, or that we have been told to strip. However, if
10195 h->indx is set to -2, the symbol is used by a reloc and we must
10200 else if ((h
->def_dynamic
10202 || h
->root
.type
== bfd_link_hash_new
)
10204 && !h
->ref_regular
)
10206 else if (flinfo
->info
->strip
== strip_all
)
10208 else if (flinfo
->info
->strip
== strip_some
10209 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
10210 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
10212 else if ((h
->root
.type
== bfd_link_hash_defined
10213 || h
->root
.type
== bfd_link_hash_defweak
)
10214 && ((flinfo
->info
->strip_discarded
10215 && discarded_section (h
->root
.u
.def
.section
))
10216 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
10217 && h
->root
.u
.def
.section
->owner
!= NULL
10218 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
10220 else if ((h
->root
.type
== bfd_link_hash_undefined
10221 || h
->root
.type
== bfd_link_hash_undefweak
)
10222 && h
->root
.u
.undef
.abfd
!= NULL
10223 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
10228 /* If we're stripping it, and it's not a dynamic symbol, there's
10229 nothing else to do. However, if it is a forced local symbol or
10230 an ifunc symbol we need to give the backend finish_dynamic_symbol
10231 function a chance to make it dynamic. */
10233 && h
->dynindx
== -1
10234 && type
!= STT_GNU_IFUNC
10235 && !h
->forced_local
)
10239 sym
.st_size
= h
->size
;
10240 sym
.st_other
= h
->other
;
10241 switch (h
->root
.type
)
10244 case bfd_link_hash_new
:
10245 case bfd_link_hash_warning
:
10249 case bfd_link_hash_undefined
:
10250 case bfd_link_hash_undefweak
:
10251 input_sec
= bfd_und_section_ptr
;
10252 sym
.st_shndx
= SHN_UNDEF
;
10255 case bfd_link_hash_defined
:
10256 case bfd_link_hash_defweak
:
10258 input_sec
= h
->root
.u
.def
.section
;
10259 if (input_sec
->output_section
!= NULL
)
10262 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
10263 input_sec
->output_section
);
10264 if (sym
.st_shndx
== SHN_BAD
)
10267 /* xgettext:c-format */
10268 (_("%pB: could not find output section %pA for input section %pA"),
10269 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
10270 bfd_set_error (bfd_error_nonrepresentable_section
);
10271 eoinfo
->failed
= TRUE
;
10275 /* ELF symbols in relocatable files are section relative,
10276 but in nonrelocatable files they are virtual
10278 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
10279 if (!bfd_link_relocatable (flinfo
->info
))
10281 sym
.st_value
+= input_sec
->output_section
->vma
;
10282 if (h
->type
== STT_TLS
)
10284 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
10285 if (tls_sec
!= NULL
)
10286 sym
.st_value
-= tls_sec
->vma
;
10292 BFD_ASSERT (input_sec
->owner
== NULL
10293 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
10294 sym
.st_shndx
= SHN_UNDEF
;
10295 input_sec
= bfd_und_section_ptr
;
10300 case bfd_link_hash_common
:
10301 input_sec
= h
->root
.u
.c
.p
->section
;
10302 sym
.st_shndx
= bed
->common_section_index (input_sec
);
10303 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
10306 case bfd_link_hash_indirect
:
10307 /* These symbols are created by symbol versioning. They point
10308 to the decorated version of the name. For example, if the
10309 symbol foo@@GNU_1.2 is the default, which should be used when
10310 foo is used with no version, then we add an indirect symbol
10311 foo which points to foo@@GNU_1.2. We ignore these symbols,
10312 since the indirected symbol is already in the hash table. */
10316 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
10317 switch (h
->root
.type
)
10319 case bfd_link_hash_common
:
10320 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10322 case bfd_link_hash_defined
:
10323 case bfd_link_hash_defweak
:
10324 if (bed
->common_definition (&sym
))
10325 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10329 case bfd_link_hash_undefined
:
10330 case bfd_link_hash_undefweak
:
10336 if (h
->forced_local
)
10338 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
10339 /* Turn off visibility on local symbol. */
10340 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
10342 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10343 else if (h
->unique_global
&& h
->def_regular
)
10344 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
10345 else if (h
->root
.type
== bfd_link_hash_undefweak
10346 || h
->root
.type
== bfd_link_hash_defweak
)
10347 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
10349 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
10350 sym
.st_target_internal
= h
->target_internal
;
10352 /* Give the processor backend a chance to tweak the symbol value,
10353 and also to finish up anything that needs to be done for this
10354 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10355 forced local syms when non-shared is due to a historical quirk.
10356 STT_GNU_IFUNC symbol must go through PLT. */
10357 if ((h
->type
== STT_GNU_IFUNC
10359 && !bfd_link_relocatable (flinfo
->info
))
10360 || ((h
->dynindx
!= -1
10361 || h
->forced_local
)
10362 && ((bfd_link_pic (flinfo
->info
)
10363 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
10364 || h
->root
.type
!= bfd_link_hash_undefweak
))
10365 || !h
->forced_local
)
10366 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
10368 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
10369 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
10371 eoinfo
->failed
= TRUE
;
10376 /* If we are marking the symbol as undefined, and there are no
10377 non-weak references to this symbol from a regular object, then
10378 mark the symbol as weak undefined; if there are non-weak
10379 references, mark the symbol as strong. We can't do this earlier,
10380 because it might not be marked as undefined until the
10381 finish_dynamic_symbol routine gets through with it. */
10382 if (sym
.st_shndx
== SHN_UNDEF
10384 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
10385 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
10388 type
= ELF_ST_TYPE (sym
.st_info
);
10390 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10391 if (type
== STT_GNU_IFUNC
)
10394 if (h
->ref_regular_nonweak
)
10395 bindtype
= STB_GLOBAL
;
10397 bindtype
= STB_WEAK
;
10398 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
10401 /* If this is a symbol defined in a dynamic library, don't use the
10402 symbol size from the dynamic library. Relinking an executable
10403 against a new library may introduce gratuitous changes in the
10404 executable's symbols if we keep the size. */
10405 if (sym
.st_shndx
== SHN_UNDEF
10410 /* If a non-weak symbol with non-default visibility is not defined
10411 locally, it is a fatal error. */
10412 if (!bfd_link_relocatable (flinfo
->info
)
10413 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
10414 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
10415 && h
->root
.type
== bfd_link_hash_undefined
10416 && !h
->def_regular
)
10420 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
10421 /* xgettext:c-format */
10422 msg
= _("%pB: protected symbol `%s' isn't defined");
10423 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
10424 /* xgettext:c-format */
10425 msg
= _("%pB: internal symbol `%s' isn't defined");
10427 /* xgettext:c-format */
10428 msg
= _("%pB: hidden symbol `%s' isn't defined");
10429 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
10430 bfd_set_error (bfd_error_bad_value
);
10431 eoinfo
->failed
= TRUE
;
10435 /* If this symbol should be put in the .dynsym section, then put it
10436 there now. We already know the symbol index. We also fill in
10437 the entry in the .hash section. */
10438 if (h
->dynindx
!= -1
10439 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
10440 && elf_hash_table (flinfo
->info
)->dynsym
!= NULL
10441 && !discarded_section (elf_hash_table (flinfo
->info
)->dynsym
))
10445 /* Since there is no version information in the dynamic string,
10446 if there is no version info in symbol version section, we will
10447 have a run-time problem if not linking executable, referenced
10448 by shared library, or not bound locally. */
10449 if (h
->verinfo
.verdef
== NULL
10450 && (!bfd_link_executable (flinfo
->info
)
10452 || !h
->def_regular
))
10454 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
10456 if (p
&& p
[1] != '\0')
10459 /* xgettext:c-format */
10460 (_("%pB: no symbol version section for versioned symbol `%s'"),
10461 flinfo
->output_bfd
, h
->root
.root
.string
);
10462 eoinfo
->failed
= TRUE
;
10467 sym
.st_name
= h
->dynstr_index
;
10468 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
10469 + h
->dynindx
* bed
->s
->sizeof_sym
);
10470 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
10472 eoinfo
->failed
= TRUE
;
10475 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
10477 if (flinfo
->hash_sec
!= NULL
)
10479 size_t hash_entry_size
;
10480 bfd_byte
*bucketpos
;
10482 size_t bucketcount
;
10485 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
10486 bucket
= h
->u
.elf_hash_value
% bucketcount
;
10489 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
10490 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
10491 + (bucket
+ 2) * hash_entry_size
);
10492 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
10493 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
10495 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
10496 ((bfd_byte
*) flinfo
->hash_sec
->contents
10497 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
10500 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
10502 Elf_Internal_Versym iversym
;
10503 Elf_External_Versym
*eversym
;
10505 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
10507 if (h
->verinfo
.verdef
== NULL
10508 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
10509 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
10510 iversym
.vs_vers
= 0;
10512 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
10516 if (h
->verinfo
.vertree
== NULL
)
10517 iversym
.vs_vers
= 1;
10519 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
10520 if (flinfo
->info
->create_default_symver
)
10524 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10525 defined locally. */
10526 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
10527 iversym
.vs_vers
|= VERSYM_HIDDEN
;
10529 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
10530 eversym
+= h
->dynindx
;
10531 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
10535 /* If the symbol is undefined, and we didn't output it to .dynsym,
10536 strip it from .symtab too. Obviously we can't do this for
10537 relocatable output or when needed for --emit-relocs. */
10538 else if (input_sec
== bfd_und_section_ptr
10540 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10541 && (h
->mark
!= 1 || ELF_ST_BIND (sym
.st_info
) != STB_GLOBAL
)
10542 && !bfd_link_relocatable (flinfo
->info
))
10545 /* Also strip others that we couldn't earlier due to dynamic symbol
10549 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
10552 /* Output a FILE symbol so that following locals are not associated
10553 with the wrong input file. We need one for forced local symbols
10554 if we've seen more than one FILE symbol or when we have exactly
10555 one FILE symbol but global symbols are present in a file other
10556 than the one with the FILE symbol. We also need one if linker
10557 defined symbols are present. In practice these conditions are
10558 always met, so just emit the FILE symbol unconditionally. */
10559 if (eoinfo
->localsyms
10560 && !eoinfo
->file_sym_done
10561 && eoinfo
->flinfo
->filesym_count
!= 0)
10563 Elf_Internal_Sym fsym
;
10565 memset (&fsym
, 0, sizeof (fsym
));
10566 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10567 fsym
.st_shndx
= SHN_ABS
;
10568 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
10569 bfd_und_section_ptr
, NULL
))
10572 eoinfo
->file_sym_done
= TRUE
;
10575 indx
= bfd_get_symcount (flinfo
->output_bfd
);
10576 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
10580 eoinfo
->failed
= TRUE
;
10585 else if (h
->indx
== -2)
10591 /* Return TRUE if special handling is done for relocs in SEC against
10592 symbols defined in discarded sections. */
10595 elf_section_ignore_discarded_relocs (asection
*sec
)
10597 const struct elf_backend_data
*bed
;
10599 switch (sec
->sec_info_type
)
10601 case SEC_INFO_TYPE_STABS
:
10602 case SEC_INFO_TYPE_EH_FRAME
:
10603 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10609 bed
= get_elf_backend_data (sec
->owner
);
10610 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
10611 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
10617 /* Return a mask saying how ld should treat relocations in SEC against
10618 symbols defined in discarded sections. If this function returns
10619 COMPLAIN set, ld will issue a warning message. If this function
10620 returns PRETEND set, and the discarded section was link-once and the
10621 same size as the kept link-once section, ld will pretend that the
10622 symbol was actually defined in the kept section. Otherwise ld will
10623 zero the reloc (at least that is the intent, but some cooperation by
10624 the target dependent code is needed, particularly for REL targets). */
10627 _bfd_elf_default_action_discarded (asection
*sec
)
10629 if (sec
->flags
& SEC_DEBUGGING
)
10632 if (strcmp (".eh_frame", sec
->name
) == 0)
10635 if (strcmp (".gcc_except_table", sec
->name
) == 0)
10638 return COMPLAIN
| PRETEND
;
10641 /* Find a match between a section and a member of a section group. */
10644 match_group_member (asection
*sec
, asection
*group
,
10645 struct bfd_link_info
*info
)
10647 asection
*first
= elf_next_in_group (group
);
10648 asection
*s
= first
;
10652 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
10655 s
= elf_next_in_group (s
);
10663 /* Check if the kept section of a discarded section SEC can be used
10664 to replace it. Return the replacement if it is OK. Otherwise return
10668 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
10672 kept
= sec
->kept_section
;
10675 if ((kept
->flags
& SEC_GROUP
) != 0)
10676 kept
= match_group_member (sec
, kept
, info
);
10678 && ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
10679 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
)))
10681 sec
->kept_section
= kept
;
10686 /* Link an input file into the linker output file. This function
10687 handles all the sections and relocations of the input file at once.
10688 This is so that we only have to read the local symbols once, and
10689 don't have to keep them in memory. */
10692 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
10694 int (*relocate_section
)
10695 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
10696 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
10698 Elf_Internal_Shdr
*symtab_hdr
;
10699 size_t locsymcount
;
10701 Elf_Internal_Sym
*isymbuf
;
10702 Elf_Internal_Sym
*isym
;
10703 Elf_Internal_Sym
*isymend
;
10705 asection
**ppsection
;
10707 const struct elf_backend_data
*bed
;
10708 struct elf_link_hash_entry
**sym_hashes
;
10709 bfd_size_type address_size
;
10710 bfd_vma r_type_mask
;
10712 bfd_boolean have_file_sym
= FALSE
;
10714 output_bfd
= flinfo
->output_bfd
;
10715 bed
= get_elf_backend_data (output_bfd
);
10716 relocate_section
= bed
->elf_backend_relocate_section
;
10718 /* If this is a dynamic object, we don't want to do anything here:
10719 we don't want the local symbols, and we don't want the section
10721 if ((input_bfd
->flags
& DYNAMIC
) != 0)
10724 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
10725 if (elf_bad_symtab (input_bfd
))
10727 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10732 locsymcount
= symtab_hdr
->sh_info
;
10733 extsymoff
= symtab_hdr
->sh_info
;
10736 /* Read the local symbols. */
10737 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10738 if (isymbuf
== NULL
&& locsymcount
!= 0)
10740 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
10741 flinfo
->internal_syms
,
10742 flinfo
->external_syms
,
10743 flinfo
->locsym_shndx
);
10744 if (isymbuf
== NULL
)
10748 /* Find local symbol sections and adjust values of symbols in
10749 SEC_MERGE sections. Write out those local symbols we know are
10750 going into the output file. */
10751 isymend
= isymbuf
+ locsymcount
;
10752 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
10754 isym
++, pindex
++, ppsection
++)
10758 Elf_Internal_Sym osym
;
10764 if (elf_bad_symtab (input_bfd
))
10766 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
10773 if (isym
->st_shndx
== SHN_UNDEF
)
10774 isec
= bfd_und_section_ptr
;
10775 else if (isym
->st_shndx
== SHN_ABS
)
10776 isec
= bfd_abs_section_ptr
;
10777 else if (isym
->st_shndx
== SHN_COMMON
)
10778 isec
= bfd_com_section_ptr
;
10781 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
10784 /* Don't attempt to output symbols with st_shnx in the
10785 reserved range other than SHN_ABS and SHN_COMMON. */
10786 isec
= bfd_und_section_ptr
;
10788 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
10789 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
10791 _bfd_merged_section_offset (output_bfd
, &isec
,
10792 elf_section_data (isec
)->sec_info
,
10798 /* Don't output the first, undefined, symbol. In fact, don't
10799 output any undefined local symbol. */
10800 if (isec
== bfd_und_section_ptr
)
10803 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
10805 /* We never output section symbols. Instead, we use the
10806 section symbol of the corresponding section in the output
10811 /* If we are stripping all symbols, we don't want to output this
10813 if (flinfo
->info
->strip
== strip_all
)
10816 /* If we are discarding all local symbols, we don't want to
10817 output this one. If we are generating a relocatable output
10818 file, then some of the local symbols may be required by
10819 relocs; we output them below as we discover that they are
10821 if (flinfo
->info
->discard
== discard_all
)
10824 /* If this symbol is defined in a section which we are
10825 discarding, we don't need to keep it. */
10826 if (isym
->st_shndx
!= SHN_UNDEF
10827 && isym
->st_shndx
< SHN_LORESERVE
10828 && isec
->output_section
== NULL
10829 && flinfo
->info
->non_contiguous_regions
10830 && flinfo
->info
->non_contiguous_regions_warnings
)
10832 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10833 "discards section `%s' from '%s'\n"),
10834 isec
->name
, bfd_get_filename (isec
->owner
));
10838 if (isym
->st_shndx
!= SHN_UNDEF
10839 && isym
->st_shndx
< SHN_LORESERVE
10840 && bfd_section_removed_from_list (output_bfd
,
10841 isec
->output_section
))
10844 /* Get the name of the symbol. */
10845 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
10850 /* See if we are discarding symbols with this name. */
10851 if ((flinfo
->info
->strip
== strip_some
10852 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
10854 || (((flinfo
->info
->discard
== discard_sec_merge
10855 && (isec
->flags
& SEC_MERGE
)
10856 && !bfd_link_relocatable (flinfo
->info
))
10857 || flinfo
->info
->discard
== discard_l
)
10858 && bfd_is_local_label_name (input_bfd
, name
)))
10861 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
10863 if (input_bfd
->lto_output
)
10864 /* -flto puts a temp file name here. This means builds
10865 are not reproducible. Discard the symbol. */
10867 have_file_sym
= TRUE
;
10868 flinfo
->filesym_count
+= 1;
10870 if (!have_file_sym
)
10872 /* In the absence of debug info, bfd_find_nearest_line uses
10873 FILE symbols to determine the source file for local
10874 function symbols. Provide a FILE symbol here if input
10875 files lack such, so that their symbols won't be
10876 associated with a previous input file. It's not the
10877 source file, but the best we can do. */
10878 have_file_sym
= TRUE
;
10879 flinfo
->filesym_count
+= 1;
10880 memset (&osym
, 0, sizeof (osym
));
10881 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10882 osym
.st_shndx
= SHN_ABS
;
10883 if (!elf_link_output_symstrtab (flinfo
,
10884 (input_bfd
->lto_output
? NULL
10885 : bfd_get_filename (input_bfd
)),
10886 &osym
, bfd_abs_section_ptr
,
10893 /* Adjust the section index for the output file. */
10894 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10895 isec
->output_section
);
10896 if (osym
.st_shndx
== SHN_BAD
)
10899 /* ELF symbols in relocatable files are section relative, but
10900 in executable files they are virtual addresses. Note that
10901 this code assumes that all ELF sections have an associated
10902 BFD section with a reasonable value for output_offset; below
10903 we assume that they also have a reasonable value for
10904 output_section. Any special sections must be set up to meet
10905 these requirements. */
10906 osym
.st_value
+= isec
->output_offset
;
10907 if (!bfd_link_relocatable (flinfo
->info
))
10909 osym
.st_value
+= isec
->output_section
->vma
;
10910 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
10912 /* STT_TLS symbols are relative to PT_TLS segment base. */
10913 if (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
)
10914 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
10916 osym
.st_info
= ELF_ST_INFO (ELF_ST_BIND (osym
.st_info
),
10921 indx
= bfd_get_symcount (output_bfd
);
10922 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
10929 if (bed
->s
->arch_size
== 32)
10931 r_type_mask
= 0xff;
10937 r_type_mask
= 0xffffffff;
10942 /* Relocate the contents of each section. */
10943 sym_hashes
= elf_sym_hashes (input_bfd
);
10944 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
10946 bfd_byte
*contents
;
10948 if (! o
->linker_mark
)
10950 /* This section was omitted from the link. */
10954 if (!flinfo
->info
->resolve_section_groups
10955 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
10957 /* Deal with the group signature symbol. */
10958 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
10959 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
10960 asection
*osec
= o
->output_section
;
10962 BFD_ASSERT (bfd_link_relocatable (flinfo
->info
));
10963 if (symndx
>= locsymcount
10964 || (elf_bad_symtab (input_bfd
)
10965 && flinfo
->sections
[symndx
] == NULL
))
10967 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
10968 while (h
->root
.type
== bfd_link_hash_indirect
10969 || h
->root
.type
== bfd_link_hash_warning
)
10970 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10971 /* Arrange for symbol to be output. */
10973 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
10975 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
10977 /* We'll use the output section target_index. */
10978 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10979 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
10983 if (flinfo
->indices
[symndx
] == -1)
10985 /* Otherwise output the local symbol now. */
10986 Elf_Internal_Sym sym
= isymbuf
[symndx
];
10987 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10992 name
= bfd_elf_string_from_elf_section (input_bfd
,
10993 symtab_hdr
->sh_link
,
10998 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11000 if (sym
.st_shndx
== SHN_BAD
)
11003 sym
.st_value
+= o
->output_offset
;
11005 indx
= bfd_get_symcount (output_bfd
);
11006 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
11011 flinfo
->indices
[symndx
] = indx
;
11015 elf_section_data (osec
)->this_hdr
.sh_info
11016 = flinfo
->indices
[symndx
];
11020 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
11021 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
11024 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
11026 /* Section was created by _bfd_elf_link_create_dynamic_sections
11031 /* Get the contents of the section. They have been cached by a
11032 relaxation routine. Note that o is a section in an input
11033 file, so the contents field will not have been set by any of
11034 the routines which work on output files. */
11035 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
11037 contents
= elf_section_data (o
)->this_hdr
.contents
;
11038 if (bed
->caches_rawsize
11040 && o
->rawsize
< o
->size
)
11042 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
11043 contents
= flinfo
->contents
;
11048 contents
= flinfo
->contents
;
11049 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
11053 if ((o
->flags
& SEC_RELOC
) != 0)
11055 Elf_Internal_Rela
*internal_relocs
;
11056 Elf_Internal_Rela
*rel
, *relend
;
11057 int action_discarded
;
11060 /* Get the swapped relocs. */
11062 = _bfd_elf_link_read_relocs (input_bfd
, o
, flinfo
->external_relocs
,
11063 flinfo
->internal_relocs
, FALSE
);
11064 if (internal_relocs
== NULL
11065 && o
->reloc_count
> 0)
11068 /* We need to reverse-copy input .ctors/.dtors sections if
11069 they are placed in .init_array/.finit_array for output. */
11070 if (o
->size
> address_size
11071 && ((strncmp (o
->name
, ".ctors", 6) == 0
11072 && strcmp (o
->output_section
->name
,
11073 ".init_array") == 0)
11074 || (strncmp (o
->name
, ".dtors", 6) == 0
11075 && strcmp (o
->output_section
->name
,
11076 ".fini_array") == 0))
11077 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
11079 if (o
->size
* bed
->s
->int_rels_per_ext_rel
11080 != o
->reloc_count
* address_size
)
11083 /* xgettext:c-format */
11084 (_("error: %pB: size of section %pA is not "
11085 "multiple of address size"),
11087 bfd_set_error (bfd_error_bad_value
);
11090 o
->flags
|= SEC_ELF_REVERSE_COPY
;
11093 action_discarded
= -1;
11094 if (!elf_section_ignore_discarded_relocs (o
))
11095 action_discarded
= (*bed
->action_discarded
) (o
);
11097 /* Run through the relocs evaluating complex reloc symbols and
11098 looking for relocs against symbols from discarded sections
11099 or section symbols from removed link-once sections.
11100 Complain about relocs against discarded sections. Zero
11101 relocs against removed link-once sections. */
11103 rel
= internal_relocs
;
11104 relend
= rel
+ o
->reloc_count
;
11105 for ( ; rel
< relend
; rel
++)
11107 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
11108 unsigned int s_type
;
11109 asection
**ps
, *sec
;
11110 struct elf_link_hash_entry
*h
= NULL
;
11111 const char *sym_name
;
11113 if (r_symndx
== STN_UNDEF
)
11116 if (r_symndx
>= locsymcount
11117 || (elf_bad_symtab (input_bfd
)
11118 && flinfo
->sections
[r_symndx
] == NULL
))
11120 h
= sym_hashes
[r_symndx
- extsymoff
];
11122 /* Badly formatted input files can contain relocs that
11123 reference non-existant symbols. Check here so that
11124 we do not seg fault. */
11128 /* xgettext:c-format */
11129 (_("error: %pB contains a reloc (%#" PRIx64
") for section %pA "
11130 "that references a non-existent global symbol"),
11131 input_bfd
, (uint64_t) rel
->r_info
, o
);
11132 bfd_set_error (bfd_error_bad_value
);
11136 while (h
->root
.type
== bfd_link_hash_indirect
11137 || h
->root
.type
== bfd_link_hash_warning
)
11138 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11142 /* If a plugin symbol is referenced from a non-IR file,
11143 mark the symbol as undefined. Note that the
11144 linker may attach linker created dynamic sections
11145 to the plugin bfd. Symbols defined in linker
11146 created sections are not plugin symbols. */
11147 if ((h
->root
.non_ir_ref_regular
11148 || h
->root
.non_ir_ref_dynamic
)
11149 && (h
->root
.type
== bfd_link_hash_defined
11150 || h
->root
.type
== bfd_link_hash_defweak
)
11151 && (h
->root
.u
.def
.section
->flags
11152 & SEC_LINKER_CREATED
) == 0
11153 && h
->root
.u
.def
.section
->owner
!= NULL
11154 && (h
->root
.u
.def
.section
->owner
->flags
11155 & BFD_PLUGIN
) != 0)
11157 h
->root
.type
= bfd_link_hash_undefined
;
11158 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
11162 if (h
->root
.type
== bfd_link_hash_defined
11163 || h
->root
.type
== bfd_link_hash_defweak
)
11164 ps
= &h
->root
.u
.def
.section
;
11166 sym_name
= h
->root
.root
.string
;
11170 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
11172 s_type
= ELF_ST_TYPE (sym
->st_info
);
11173 ps
= &flinfo
->sections
[r_symndx
];
11174 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
11178 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
11179 && !bfd_link_relocatable (flinfo
->info
))
11182 bfd_vma dot
= (rel
->r_offset
11183 + o
->output_offset
+ o
->output_section
->vma
);
11185 printf ("Encountered a complex symbol!");
11186 printf (" (input_bfd %s, section %s, reloc %ld\n",
11187 bfd_get_filename (input_bfd
), o
->name
,
11188 (long) (rel
- internal_relocs
));
11189 printf (" symbol: idx %8.8lx, name %s\n",
11190 r_symndx
, sym_name
);
11191 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11192 (unsigned long) rel
->r_info
,
11193 (unsigned long) rel
->r_offset
);
11195 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
11196 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
11199 /* Symbol evaluated OK. Update to absolute value. */
11200 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
11205 if (action_discarded
!= -1 && ps
!= NULL
)
11207 /* Complain if the definition comes from a
11208 discarded section. */
11209 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
11211 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
11212 if (action_discarded
& COMPLAIN
)
11213 (*flinfo
->info
->callbacks
->einfo
)
11214 /* xgettext:c-format */
11215 (_("%X`%s' referenced in section `%pA' of %pB: "
11216 "defined in discarded section `%pA' of %pB\n"),
11217 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
11219 /* Try to do the best we can to support buggy old
11220 versions of gcc. Pretend that the symbol is
11221 really defined in the kept linkonce section.
11222 FIXME: This is quite broken. Modifying the
11223 symbol here means we will be changing all later
11224 uses of the symbol, not just in this section. */
11225 if (action_discarded
& PRETEND
)
11229 kept
= _bfd_elf_check_kept_section (sec
,
11241 /* Relocate the section by invoking a back end routine.
11243 The back end routine is responsible for adjusting the
11244 section contents as necessary, and (if using Rela relocs
11245 and generating a relocatable output file) adjusting the
11246 reloc addend as necessary.
11248 The back end routine does not have to worry about setting
11249 the reloc address or the reloc symbol index.
11251 The back end routine is given a pointer to the swapped in
11252 internal symbols, and can access the hash table entries
11253 for the external symbols via elf_sym_hashes (input_bfd).
11255 When generating relocatable output, the back end routine
11256 must handle STB_LOCAL/STT_SECTION symbols specially. The
11257 output symbol is going to be a section symbol
11258 corresponding to the output section, which will require
11259 the addend to be adjusted. */
11261 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
11262 input_bfd
, o
, contents
,
11270 || bfd_link_relocatable (flinfo
->info
)
11271 || flinfo
->info
->emitrelocations
)
11273 Elf_Internal_Rela
*irela
;
11274 Elf_Internal_Rela
*irelaend
, *irelamid
;
11275 bfd_vma last_offset
;
11276 struct elf_link_hash_entry
**rel_hash
;
11277 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
11278 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
11279 unsigned int next_erel
;
11280 bfd_boolean rela_normal
;
11281 struct bfd_elf_section_data
*esdi
, *esdo
;
11283 esdi
= elf_section_data (o
);
11284 esdo
= elf_section_data (o
->output_section
);
11285 rela_normal
= FALSE
;
11287 /* Adjust the reloc addresses and symbol indices. */
11289 irela
= internal_relocs
;
11290 irelaend
= irela
+ o
->reloc_count
;
11291 rel_hash
= esdo
->rel
.hashes
+ esdo
->rel
.count
;
11292 /* We start processing the REL relocs, if any. When we reach
11293 IRELAMID in the loop, we switch to the RELA relocs. */
11295 if (esdi
->rel
.hdr
!= NULL
)
11296 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
11297 * bed
->s
->int_rels_per_ext_rel
);
11298 rel_hash_list
= rel_hash
;
11299 rela_hash_list
= NULL
;
11300 last_offset
= o
->output_offset
;
11301 if (!bfd_link_relocatable (flinfo
->info
))
11302 last_offset
+= o
->output_section
->vma
;
11303 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
11305 unsigned long r_symndx
;
11307 Elf_Internal_Sym sym
;
11309 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
11315 if (irela
== irelamid
)
11317 rel_hash
= esdo
->rela
.hashes
+ esdo
->rela
.count
;
11318 rela_hash_list
= rel_hash
;
11319 rela_normal
= bed
->rela_normal
;
11322 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
11325 if (irela
->r_offset
>= (bfd_vma
) -2)
11327 /* This is a reloc for a deleted entry or somesuch.
11328 Turn it into an R_*_NONE reloc, at the same
11329 offset as the last reloc. elf_eh_frame.c and
11330 bfd_elf_discard_info rely on reloc offsets
11332 irela
->r_offset
= last_offset
;
11334 irela
->r_addend
= 0;
11338 irela
->r_offset
+= o
->output_offset
;
11340 /* Relocs in an executable have to be virtual addresses. */
11341 if (!bfd_link_relocatable (flinfo
->info
))
11342 irela
->r_offset
+= o
->output_section
->vma
;
11344 last_offset
= irela
->r_offset
;
11346 r_symndx
= irela
->r_info
>> r_sym_shift
;
11347 if (r_symndx
== STN_UNDEF
)
11350 if (r_symndx
>= locsymcount
11351 || (elf_bad_symtab (input_bfd
)
11352 && flinfo
->sections
[r_symndx
] == NULL
))
11354 struct elf_link_hash_entry
*rh
;
11355 unsigned long indx
;
11357 /* This is a reloc against a global symbol. We
11358 have not yet output all the local symbols, so
11359 we do not know the symbol index of any global
11360 symbol. We set the rel_hash entry for this
11361 reloc to point to the global hash table entry
11362 for this symbol. The symbol index is then
11363 set at the end of bfd_elf_final_link. */
11364 indx
= r_symndx
- extsymoff
;
11365 rh
= elf_sym_hashes (input_bfd
)[indx
];
11366 while (rh
->root
.type
== bfd_link_hash_indirect
11367 || rh
->root
.type
== bfd_link_hash_warning
)
11368 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
11370 /* Setting the index to -2 tells
11371 elf_link_output_extsym that this symbol is
11372 used by a reloc. */
11373 BFD_ASSERT (rh
->indx
< 0);
11380 /* This is a reloc against a local symbol. */
11383 sym
= isymbuf
[r_symndx
];
11384 sec
= flinfo
->sections
[r_symndx
];
11385 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
11387 /* I suppose the backend ought to fill in the
11388 section of any STT_SECTION symbol against a
11389 processor specific section. */
11390 r_symndx
= STN_UNDEF
;
11391 if (bfd_is_abs_section (sec
))
11393 else if (sec
== NULL
|| sec
->owner
== NULL
)
11395 bfd_set_error (bfd_error_bad_value
);
11400 asection
*osec
= sec
->output_section
;
11402 /* If we have discarded a section, the output
11403 section will be the absolute section. In
11404 case of discarded SEC_MERGE sections, use
11405 the kept section. relocate_section should
11406 have already handled discarded linkonce
11408 if (bfd_is_abs_section (osec
)
11409 && sec
->kept_section
!= NULL
11410 && sec
->kept_section
->output_section
!= NULL
)
11412 osec
= sec
->kept_section
->output_section
;
11413 irela
->r_addend
-= osec
->vma
;
11416 if (!bfd_is_abs_section (osec
))
11418 r_symndx
= osec
->target_index
;
11419 if (r_symndx
== STN_UNDEF
)
11421 irela
->r_addend
+= osec
->vma
;
11422 osec
= _bfd_nearby_section (output_bfd
, osec
,
11424 irela
->r_addend
-= osec
->vma
;
11425 r_symndx
= osec
->target_index
;
11430 /* Adjust the addend according to where the
11431 section winds up in the output section. */
11433 irela
->r_addend
+= sec
->output_offset
;
11437 if (flinfo
->indices
[r_symndx
] == -1)
11439 unsigned long shlink
;
11444 if (flinfo
->info
->strip
== strip_all
)
11446 /* You can't do ld -r -s. */
11447 bfd_set_error (bfd_error_invalid_operation
);
11451 /* This symbol was skipped earlier, but
11452 since it is needed by a reloc, we
11453 must output it now. */
11454 shlink
= symtab_hdr
->sh_link
;
11455 name
= (bfd_elf_string_from_elf_section
11456 (input_bfd
, shlink
, sym
.st_name
));
11460 osec
= sec
->output_section
;
11462 _bfd_elf_section_from_bfd_section (output_bfd
,
11464 if (sym
.st_shndx
== SHN_BAD
)
11467 sym
.st_value
+= sec
->output_offset
;
11468 if (!bfd_link_relocatable (flinfo
->info
))
11470 sym
.st_value
+= osec
->vma
;
11471 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
11473 struct elf_link_hash_table
*htab
11474 = elf_hash_table (flinfo
->info
);
11476 /* STT_TLS symbols are relative to PT_TLS
11478 if (htab
->tls_sec
!= NULL
)
11479 sym
.st_value
-= htab
->tls_sec
->vma
;
11482 = ELF_ST_INFO (ELF_ST_BIND (sym
.st_info
),
11487 indx
= bfd_get_symcount (output_bfd
);
11488 ret
= elf_link_output_symstrtab (flinfo
, name
,
11494 flinfo
->indices
[r_symndx
] = indx
;
11499 r_symndx
= flinfo
->indices
[r_symndx
];
11502 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
11503 | (irela
->r_info
& r_type_mask
));
11506 /* Swap out the relocs. */
11507 input_rel_hdr
= esdi
->rel
.hdr
;
11508 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
11510 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11515 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
11516 * bed
->s
->int_rels_per_ext_rel
);
11517 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
11520 input_rela_hdr
= esdi
->rela
.hdr
;
11521 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
11523 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11532 /* Write out the modified section contents. */
11533 if (bed
->elf_backend_write_section
11534 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
11537 /* Section written out. */
11539 else switch (o
->sec_info_type
)
11541 case SEC_INFO_TYPE_STABS
:
11542 if (! (_bfd_write_section_stabs
11544 &elf_hash_table (flinfo
->info
)->stab_info
,
11545 o
, &elf_section_data (o
)->sec_info
, contents
)))
11548 case SEC_INFO_TYPE_MERGE
:
11549 if (! _bfd_write_merged_section (output_bfd
, o
,
11550 elf_section_data (o
)->sec_info
))
11553 case SEC_INFO_TYPE_EH_FRAME
:
11555 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
11560 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
11562 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
11570 if (! (o
->flags
& SEC_EXCLUDE
))
11572 file_ptr offset
= (file_ptr
) o
->output_offset
;
11573 bfd_size_type todo
= o
->size
;
11575 offset
*= bfd_octets_per_byte (output_bfd
, o
);
11577 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
11579 /* Reverse-copy input section to output. */
11582 todo
-= address_size
;
11583 if (! bfd_set_section_contents (output_bfd
,
11591 offset
+= address_size
;
11595 else if (! bfd_set_section_contents (output_bfd
,
11609 /* Generate a reloc when linking an ELF file. This is a reloc
11610 requested by the linker, and does not come from any input file. This
11611 is used to build constructor and destructor tables when linking
11615 elf_reloc_link_order (bfd
*output_bfd
,
11616 struct bfd_link_info
*info
,
11617 asection
*output_section
,
11618 struct bfd_link_order
*link_order
)
11620 reloc_howto_type
*howto
;
11624 struct bfd_elf_section_reloc_data
*reldata
;
11625 struct elf_link_hash_entry
**rel_hash_ptr
;
11626 Elf_Internal_Shdr
*rel_hdr
;
11627 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
11628 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
11631 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
11633 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
11636 bfd_set_error (bfd_error_bad_value
);
11640 addend
= link_order
->u
.reloc
.p
->addend
;
11643 reldata
= &esdo
->rel
;
11644 else if (esdo
->rela
.hdr
)
11645 reldata
= &esdo
->rela
;
11652 /* Figure out the symbol index. */
11653 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
11654 if (link_order
->type
== bfd_section_reloc_link_order
)
11656 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
11657 BFD_ASSERT (indx
!= 0);
11658 *rel_hash_ptr
= NULL
;
11662 struct elf_link_hash_entry
*h
;
11664 /* Treat a reloc against a defined symbol as though it were
11665 actually against the section. */
11666 h
= ((struct elf_link_hash_entry
*)
11667 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
11668 link_order
->u
.reloc
.p
->u
.name
,
11669 FALSE
, FALSE
, TRUE
));
11671 && (h
->root
.type
== bfd_link_hash_defined
11672 || h
->root
.type
== bfd_link_hash_defweak
))
11676 section
= h
->root
.u
.def
.section
;
11677 indx
= section
->output_section
->target_index
;
11678 *rel_hash_ptr
= NULL
;
11679 /* It seems that we ought to add the symbol value to the
11680 addend here, but in practice it has already been added
11681 because it was passed to constructor_callback. */
11682 addend
+= section
->output_section
->vma
+ section
->output_offset
;
11684 else if (h
!= NULL
)
11686 /* Setting the index to -2 tells elf_link_output_extsym that
11687 this symbol is used by a reloc. */
11694 (*info
->callbacks
->unattached_reloc
)
11695 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
11700 /* If this is an inplace reloc, we must write the addend into the
11702 if (howto
->partial_inplace
&& addend
!= 0)
11704 bfd_size_type size
;
11705 bfd_reloc_status_type rstat
;
11708 const char *sym_name
;
11709 bfd_size_type octets
;
11711 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
11712 buf
= (bfd_byte
*) bfd_zmalloc (size
);
11713 if (buf
== NULL
&& size
!= 0)
11715 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
11722 case bfd_reloc_outofrange
:
11725 case bfd_reloc_overflow
:
11726 if (link_order
->type
== bfd_section_reloc_link_order
)
11727 sym_name
= bfd_section_name (link_order
->u
.reloc
.p
->u
.section
);
11729 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
11730 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
11731 howto
->name
, addend
, NULL
, NULL
,
11736 octets
= link_order
->offset
* bfd_octets_per_byte (output_bfd
,
11738 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
11745 /* The address of a reloc is relative to the section in a
11746 relocatable file, and is a virtual address in an executable
11748 offset
= link_order
->offset
;
11749 if (! bfd_link_relocatable (info
))
11750 offset
+= output_section
->vma
;
11752 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
11754 irel
[i
].r_offset
= offset
;
11755 irel
[i
].r_info
= 0;
11756 irel
[i
].r_addend
= 0;
11758 if (bed
->s
->arch_size
== 32)
11759 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
11761 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
11763 rel_hdr
= reldata
->hdr
;
11764 erel
= rel_hdr
->contents
;
11765 if (rel_hdr
->sh_type
== SHT_REL
)
11767 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
11768 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
11772 irel
[0].r_addend
= addend
;
11773 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
11774 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
11783 /* Compare two sections based on the locations of the sections they are
11784 linked to. Used by elf_fixup_link_order. */
11787 compare_link_order (const void *a
, const void *b
)
11789 const struct bfd_link_order
*alo
= *(const struct bfd_link_order
**) a
;
11790 const struct bfd_link_order
*blo
= *(const struct bfd_link_order
**) b
;
11791 asection
*asec
= elf_linked_to_section (alo
->u
.indirect
.section
);
11792 asection
*bsec
= elf_linked_to_section (blo
->u
.indirect
.section
);
11793 bfd_vma apos
= asec
->output_section
->lma
+ asec
->output_offset
;
11794 bfd_vma bpos
= bsec
->output_section
->lma
+ bsec
->output_offset
;
11801 /* The only way we should get matching LMAs is when the first of two
11802 sections has zero size. */
11803 if (asec
->size
< bsec
->size
)
11805 if (asec
->size
> bsec
->size
)
11808 /* If they are both zero size then they almost certainly have the same
11809 VMA and thus are not ordered with respect to each other. Test VMA
11810 anyway, and fall back to id to make the result reproducible across
11811 qsort implementations. */
11812 apos
= asec
->output_section
->vma
+ asec
->output_offset
;
11813 bpos
= bsec
->output_section
->vma
+ bsec
->output_offset
;
11819 return asec
->id
- bsec
->id
;
11823 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11824 order as their linked sections. Returns false if this could not be done
11825 because an output section includes both ordered and unordered
11826 sections. Ideally we'd do this in the linker proper. */
11829 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
11831 size_t seen_linkorder
;
11834 struct bfd_link_order
*p
;
11836 struct bfd_link_order
**sections
;
11837 asection
*other_sec
, *linkorder_sec
;
11838 bfd_vma offset
; /* Octets. */
11841 linkorder_sec
= NULL
;
11843 seen_linkorder
= 0;
11844 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11846 if (p
->type
== bfd_indirect_link_order
)
11848 asection
*s
= p
->u
.indirect
.section
;
11850 if ((s
->flags
& SEC_LINKER_CREATED
) == 0
11851 && bfd_get_flavour (sub
) == bfd_target_elf_flavour
11852 && elf_section_data (s
) != NULL
11853 && elf_linked_to_section (s
) != NULL
)
11867 if (seen_other
&& seen_linkorder
)
11869 if (other_sec
&& linkorder_sec
)
11871 /* xgettext:c-format */
11872 (_("%pA has both ordered [`%pA' in %pB] "
11873 "and unordered [`%pA' in %pB] sections"),
11874 o
, linkorder_sec
, linkorder_sec
->owner
,
11875 other_sec
, other_sec
->owner
);
11878 (_("%pA has both ordered and unordered sections"), o
);
11879 bfd_set_error (bfd_error_bad_value
);
11884 if (!seen_linkorder
)
11887 sections
= bfd_malloc (seen_linkorder
* sizeof (*sections
));
11888 if (sections
== NULL
)
11891 seen_linkorder
= 0;
11892 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11893 sections
[seen_linkorder
++] = p
;
11895 /* Sort the input sections in the order of their linked section. */
11896 qsort (sections
, seen_linkorder
, sizeof (*sections
), compare_link_order
);
11898 /* Change the offsets of the sections. */
11900 for (n
= 0; n
< seen_linkorder
; n
++)
11903 asection
*s
= sections
[n
]->u
.indirect
.section
;
11904 unsigned int opb
= bfd_octets_per_byte (abfd
, s
);
11906 mask
= ~(bfd_vma
) 0 << s
->alignment_power
* opb
;
11907 offset
= (offset
+ ~mask
) & mask
;
11908 sections
[n
]->offset
= s
->output_offset
= offset
/ opb
;
11909 offset
+= sections
[n
]->size
;
11916 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11917 Returns TRUE upon success, FALSE otherwise. */
11920 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
11922 bfd_boolean ret
= FALSE
;
11924 const struct elf_backend_data
*bed
;
11926 enum bfd_architecture arch
;
11928 asymbol
**sympp
= NULL
;
11932 elf_symbol_type
*osymbuf
;
11935 implib_bfd
= info
->out_implib_bfd
;
11936 bed
= get_elf_backend_data (abfd
);
11938 if (!bfd_set_format (implib_bfd
, bfd_object
))
11941 /* Use flag from executable but make it a relocatable object. */
11942 flags
= bfd_get_file_flags (abfd
);
11943 flags
&= ~HAS_RELOC
;
11944 if (!bfd_set_start_address (implib_bfd
, 0)
11945 || !bfd_set_file_flags (implib_bfd
, flags
& ~EXEC_P
))
11948 /* Copy architecture of output file to import library file. */
11949 arch
= bfd_get_arch (abfd
);
11950 mach
= bfd_get_mach (abfd
);
11951 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
11952 && (abfd
->target_defaulted
11953 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
11956 /* Get symbol table size. */
11957 symsize
= bfd_get_symtab_upper_bound (abfd
);
11961 /* Read in the symbol table. */
11962 sympp
= (asymbol
**) bfd_malloc (symsize
);
11966 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
11970 /* Allow the BFD backend to copy any private header data it
11971 understands from the output BFD to the import library BFD. */
11972 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
11975 /* Filter symbols to appear in the import library. */
11976 if (bed
->elf_backend_filter_implib_symbols
)
11977 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
11980 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
11983 bfd_set_error (bfd_error_no_symbols
);
11984 _bfd_error_handler (_("%pB: no symbol found for import library"),
11990 /* Make symbols absolute. */
11991 amt
= symcount
* sizeof (*osymbuf
);
11992 osymbuf
= (elf_symbol_type
*) bfd_alloc (implib_bfd
, amt
);
11993 if (osymbuf
== NULL
)
11996 for (src_count
= 0; src_count
< symcount
; src_count
++)
11998 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
11999 sizeof (*osymbuf
));
12000 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
12001 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
12002 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
12003 osymbuf
[src_count
].internal_elf_sym
.st_value
=
12004 osymbuf
[src_count
].symbol
.value
;
12005 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
12008 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
12010 /* Allow the BFD backend to copy any private data it understands
12011 from the output BFD to the import library BFD. This is done last
12012 to permit the routine to look at the filtered symbol table. */
12013 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
12016 if (!bfd_close (implib_bfd
))
12027 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
12031 if (flinfo
->symstrtab
!= NULL
)
12032 _bfd_elf_strtab_free (flinfo
->symstrtab
);
12033 free (flinfo
->contents
);
12034 free (flinfo
->external_relocs
);
12035 free (flinfo
->internal_relocs
);
12036 free (flinfo
->external_syms
);
12037 free (flinfo
->locsym_shndx
);
12038 free (flinfo
->internal_syms
);
12039 free (flinfo
->indices
);
12040 free (flinfo
->sections
);
12041 if (flinfo
->symshndxbuf
!= (Elf_External_Sym_Shndx
*) -1)
12042 free (flinfo
->symshndxbuf
);
12043 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
12045 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12046 free (esdo
->rel
.hashes
);
12047 free (esdo
->rela
.hashes
);
12051 /* Do the final step of an ELF link. */
12054 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
12056 bfd_boolean dynamic
;
12057 bfd_boolean emit_relocs
;
12059 struct elf_final_link_info flinfo
;
12061 struct bfd_link_order
*p
;
12063 bfd_size_type max_contents_size
;
12064 bfd_size_type max_external_reloc_size
;
12065 bfd_size_type max_internal_reloc_count
;
12066 bfd_size_type max_sym_count
;
12067 bfd_size_type max_sym_shndx_count
;
12068 Elf_Internal_Sym elfsym
;
12070 Elf_Internal_Shdr
*symtab_hdr
;
12071 Elf_Internal_Shdr
*symtab_shndx_hdr
;
12072 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12073 struct elf_outext_info eoinfo
;
12074 bfd_boolean merged
;
12075 size_t relativecount
= 0;
12076 asection
*reldyn
= 0;
12078 asection
*attr_section
= NULL
;
12079 bfd_vma attr_size
= 0;
12080 const char *std_attrs_section
;
12081 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
12082 bfd_boolean sections_removed
;
12085 if (!is_elf_hash_table (htab
))
12088 if (bfd_link_pic (info
))
12089 abfd
->flags
|= DYNAMIC
;
12091 dynamic
= htab
->dynamic_sections_created
;
12092 dynobj
= htab
->dynobj
;
12094 emit_relocs
= (bfd_link_relocatable (info
)
12095 || info
->emitrelocations
);
12097 memset (&flinfo
, 0, sizeof (flinfo
));
12098 flinfo
.info
= info
;
12099 flinfo
.output_bfd
= abfd
;
12100 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
12101 if (flinfo
.symstrtab
== NULL
)
12106 flinfo
.hash_sec
= NULL
;
12107 flinfo
.symver_sec
= NULL
;
12111 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
12112 /* Note that dynsym_sec can be NULL (on VMS). */
12113 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
12114 /* Note that it is OK if symver_sec is NULL. */
12117 if (info
->unique_symbol
12118 && !bfd_hash_table_init (&flinfo
.local_hash_table
,
12119 local_hash_newfunc
,
12120 sizeof (struct local_hash_entry
)))
12123 /* The object attributes have been merged. Remove the input
12124 sections from the link, and set the contents of the output
12126 sections_removed
= FALSE
;
12127 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
12128 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12130 bfd_boolean remove_section
= FALSE
;
12132 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
12133 || strcmp (o
->name
, ".gnu.attributes") == 0)
12135 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12137 asection
*input_section
;
12139 if (p
->type
!= bfd_indirect_link_order
)
12141 input_section
= p
->u
.indirect
.section
;
12142 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12143 elf_link_input_bfd ignores this section. */
12144 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
12147 attr_size
= bfd_elf_obj_attr_size (abfd
);
12148 bfd_set_section_size (o
, attr_size
);
12149 /* Skip this section later on. */
12150 o
->map_head
.link_order
= NULL
;
12154 remove_section
= TRUE
;
12156 else if ((o
->flags
& SEC_GROUP
) != 0 && o
->size
== 0)
12158 /* Remove empty group section from linker output. */
12159 remove_section
= TRUE
;
12161 if (remove_section
)
12163 o
->flags
|= SEC_EXCLUDE
;
12164 bfd_section_list_remove (abfd
, o
);
12165 abfd
->section_count
--;
12166 sections_removed
= TRUE
;
12169 if (sections_removed
)
12170 _bfd_fix_excluded_sec_syms (abfd
, info
);
12172 /* Count up the number of relocations we will output for each output
12173 section, so that we know the sizes of the reloc sections. We
12174 also figure out some maximum sizes. */
12175 max_contents_size
= 0;
12176 max_external_reloc_size
= 0;
12177 max_internal_reloc_count
= 0;
12179 max_sym_shndx_count
= 0;
12181 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12183 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12184 o
->reloc_count
= 0;
12186 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12188 unsigned int reloc_count
= 0;
12189 unsigned int additional_reloc_count
= 0;
12190 struct bfd_elf_section_data
*esdi
= NULL
;
12192 if (p
->type
== bfd_section_reloc_link_order
12193 || p
->type
== bfd_symbol_reloc_link_order
)
12195 else if (p
->type
== bfd_indirect_link_order
)
12199 sec
= p
->u
.indirect
.section
;
12201 /* Mark all sections which are to be included in the
12202 link. This will normally be every section. We need
12203 to do this so that we can identify any sections which
12204 the linker has decided to not include. */
12205 sec
->linker_mark
= TRUE
;
12207 if (sec
->flags
& SEC_MERGE
)
12210 if (sec
->rawsize
> max_contents_size
)
12211 max_contents_size
= sec
->rawsize
;
12212 if (sec
->size
> max_contents_size
)
12213 max_contents_size
= sec
->size
;
12215 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
12216 && (sec
->owner
->flags
& DYNAMIC
) == 0)
12220 /* We are interested in just local symbols, not all
12222 if (elf_bad_symtab (sec
->owner
))
12223 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
12224 / bed
->s
->sizeof_sym
);
12226 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
12228 if (sym_count
> max_sym_count
)
12229 max_sym_count
= sym_count
;
12231 if (sym_count
> max_sym_shndx_count
12232 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
12233 max_sym_shndx_count
= sym_count
;
12235 if (esdo
->this_hdr
.sh_type
== SHT_REL
12236 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
12237 /* Some backends use reloc_count in relocation sections
12238 to count particular types of relocs. Of course,
12239 reloc sections themselves can't have relocations. */
12241 else if (emit_relocs
)
12243 reloc_count
= sec
->reloc_count
;
12244 if (bed
->elf_backend_count_additional_relocs
)
12247 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
12248 additional_reloc_count
+= c
;
12251 else if (bed
->elf_backend_count_relocs
)
12252 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
12254 esdi
= elf_section_data (sec
);
12256 if ((sec
->flags
& SEC_RELOC
) != 0)
12258 size_t ext_size
= 0;
12260 if (esdi
->rel
.hdr
!= NULL
)
12261 ext_size
= esdi
->rel
.hdr
->sh_size
;
12262 if (esdi
->rela
.hdr
!= NULL
)
12263 ext_size
+= esdi
->rela
.hdr
->sh_size
;
12265 if (ext_size
> max_external_reloc_size
)
12266 max_external_reloc_size
= ext_size
;
12267 if (sec
->reloc_count
> max_internal_reloc_count
)
12268 max_internal_reloc_count
= sec
->reloc_count
;
12273 if (reloc_count
== 0)
12276 reloc_count
+= additional_reloc_count
;
12277 o
->reloc_count
+= reloc_count
;
12279 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
12283 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
12284 esdo
->rel
.count
+= additional_reloc_count
;
12286 if (esdi
->rela
.hdr
)
12288 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
12289 esdo
->rela
.count
+= additional_reloc_count
;
12295 esdo
->rela
.count
+= reloc_count
;
12297 esdo
->rel
.count
+= reloc_count
;
12301 if (o
->reloc_count
> 0)
12302 o
->flags
|= SEC_RELOC
;
12305 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12306 set it (this is probably a bug) and if it is set
12307 assign_section_numbers will create a reloc section. */
12308 o
->flags
&=~ SEC_RELOC
;
12311 /* If the SEC_ALLOC flag is not set, force the section VMA to
12312 zero. This is done in elf_fake_sections as well, but forcing
12313 the VMA to 0 here will ensure that relocs against these
12314 sections are handled correctly. */
12315 if ((o
->flags
& SEC_ALLOC
) == 0
12316 && ! o
->user_set_vma
)
12320 if (! bfd_link_relocatable (info
) && merged
)
12321 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
12323 /* Figure out the file positions for everything but the symbol table
12324 and the relocs. We set symcount to force assign_section_numbers
12325 to create a symbol table. */
12326 abfd
->symcount
= info
->strip
!= strip_all
|| emit_relocs
;
12327 BFD_ASSERT (! abfd
->output_has_begun
);
12328 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
12331 /* Set sizes, and assign file positions for reloc sections. */
12332 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12334 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12335 if ((o
->flags
& SEC_RELOC
) != 0)
12338 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
12342 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
12346 /* _bfd_elf_compute_section_file_positions makes temporary use
12347 of target_index. Reset it. */
12348 o
->target_index
= 0;
12350 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12351 to count upwards while actually outputting the relocations. */
12352 esdo
->rel
.count
= 0;
12353 esdo
->rela
.count
= 0;
12355 if ((esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
12356 && !bfd_section_is_ctf (o
))
12358 /* Cache the section contents so that they can be compressed
12359 later. Use bfd_malloc since it will be freed by
12360 bfd_compress_section_contents. */
12361 unsigned char *contents
= esdo
->this_hdr
.contents
;
12362 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
12365 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
12366 if (contents
== NULL
)
12368 esdo
->this_hdr
.contents
= contents
;
12372 /* We have now assigned file positions for all the sections except .symtab,
12373 .strtab, and non-loaded reloc and compressed debugging sections. We start
12374 the .symtab section at the current file position, and write directly to it.
12375 We build the .strtab section in memory. */
12376 abfd
->symcount
= 0;
12377 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12378 /* sh_name is set in prep_headers. */
12379 symtab_hdr
->sh_type
= SHT_SYMTAB
;
12380 /* sh_flags, sh_addr and sh_size all start off zero. */
12381 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
12382 /* sh_link is set in assign_section_numbers. */
12383 /* sh_info is set below. */
12384 /* sh_offset is set just below. */
12385 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
12387 if (max_sym_count
< 20)
12388 max_sym_count
= 20;
12389 htab
->strtabsize
= max_sym_count
;
12390 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
12391 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
12392 if (htab
->strtab
== NULL
)
12394 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12396 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
12397 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
12399 if (info
->strip
!= strip_all
|| emit_relocs
)
12401 bfd_boolean name_local_sections
;
12404 file_ptr off
= elf_next_file_pos (abfd
);
12406 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
12408 /* Note that at this point elf_next_file_pos (abfd) is
12409 incorrect. We do not yet know the size of the .symtab section.
12410 We correct next_file_pos below, after we do know the size. */
12412 /* Start writing out the symbol table. The first symbol is always a
12414 elfsym
.st_value
= 0;
12415 elfsym
.st_size
= 0;
12416 elfsym
.st_info
= 0;
12417 elfsym
.st_other
= 0;
12418 elfsym
.st_shndx
= SHN_UNDEF
;
12419 elfsym
.st_target_internal
= 0;
12420 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
12421 bfd_und_section_ptr
, NULL
) != 1)
12424 /* Output a symbol for each section. We output these even if we are
12425 discarding local symbols, since they are used for relocs. These
12426 symbols usually have no names. We store the index of each one in
12427 the index field of the section, so that we can find it again when
12428 outputting relocs. */
12430 name_local_sections
12431 = (bed
->elf_backend_name_local_section_symbols
12432 && bed
->elf_backend_name_local_section_symbols (abfd
));
12435 elfsym
.st_size
= 0;
12436 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12437 elfsym
.st_other
= 0;
12438 elfsym
.st_value
= 0;
12439 elfsym
.st_target_internal
= 0;
12440 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12442 o
= bfd_section_from_elf_index (abfd
, i
);
12445 o
->target_index
= bfd_get_symcount (abfd
);
12446 elfsym
.st_shndx
= i
;
12447 if (!bfd_link_relocatable (info
))
12448 elfsym
.st_value
= o
->vma
;
12449 if (name_local_sections
)
12451 if (elf_link_output_symstrtab (&flinfo
, name
, &elfsym
, o
,
12458 /* On some targets like Irix 5 the symbol split between local and global
12459 ones recorded in the sh_info field needs to be done between section
12460 and all other symbols. */
12461 if (bed
->elf_backend_elfsym_local_is_section
12462 && bed
->elf_backend_elfsym_local_is_section (abfd
))
12463 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12465 /* Allocate some memory to hold information read in from the input
12467 if (max_contents_size
!= 0)
12469 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
12470 if (flinfo
.contents
== NULL
)
12474 if (max_external_reloc_size
!= 0)
12476 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
12477 if (flinfo
.external_relocs
== NULL
)
12481 if (max_internal_reloc_count
!= 0)
12483 amt
= max_internal_reloc_count
* sizeof (Elf_Internal_Rela
);
12484 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
12485 if (flinfo
.internal_relocs
== NULL
)
12489 if (max_sym_count
!= 0)
12491 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
12492 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
12493 if (flinfo
.external_syms
== NULL
)
12496 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
12497 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
12498 if (flinfo
.internal_syms
== NULL
)
12501 amt
= max_sym_count
* sizeof (long);
12502 flinfo
.indices
= (long int *) bfd_malloc (amt
);
12503 if (flinfo
.indices
== NULL
)
12506 amt
= max_sym_count
* sizeof (asection
*);
12507 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
12508 if (flinfo
.sections
== NULL
)
12512 if (max_sym_shndx_count
!= 0)
12514 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
12515 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
12516 if (flinfo
.locsym_shndx
== NULL
)
12522 bfd_vma base
, end
= 0; /* Both bytes. */
12525 for (sec
= htab
->tls_sec
;
12526 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
12529 bfd_size_type size
= sec
->size
;
12530 unsigned int opb
= bfd_octets_per_byte (abfd
, sec
);
12533 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
12535 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
12538 size
= ord
->offset
* opb
+ ord
->size
;
12540 end
= sec
->vma
+ size
/ opb
;
12542 base
= htab
->tls_sec
->vma
;
12543 /* Only align end of TLS section if static TLS doesn't have special
12544 alignment requirements. */
12545 if (bed
->static_tls_alignment
== 1)
12546 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
12547 htab
->tls_size
= end
- base
;
12550 /* Reorder SHF_LINK_ORDER sections. */
12551 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12553 if (!elf_fixup_link_order (abfd
, o
))
12557 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
12560 /* Since ELF permits relocations to be against local symbols, we
12561 must have the local symbols available when we do the relocations.
12562 Since we would rather only read the local symbols once, and we
12563 would rather not keep them in memory, we handle all the
12564 relocations for a single input file at the same time.
12566 Unfortunately, there is no way to know the total number of local
12567 symbols until we have seen all of them, and the local symbol
12568 indices precede the global symbol indices. This means that when
12569 we are generating relocatable output, and we see a reloc against
12570 a global symbol, we can not know the symbol index until we have
12571 finished examining all the local symbols to see which ones we are
12572 going to output. To deal with this, we keep the relocations in
12573 memory, and don't output them until the end of the link. This is
12574 an unfortunate waste of memory, but I don't see a good way around
12575 it. Fortunately, it only happens when performing a relocatable
12576 link, which is not the common case. FIXME: If keep_memory is set
12577 we could write the relocs out and then read them again; I don't
12578 know how bad the memory loss will be. */
12580 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12581 sub
->output_has_begun
= FALSE
;
12582 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12584 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12586 if (p
->type
== bfd_indirect_link_order
12587 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
12588 == bfd_target_elf_flavour
)
12589 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
12591 if (! sub
->output_has_begun
)
12593 if (! elf_link_input_bfd (&flinfo
, sub
))
12595 sub
->output_has_begun
= TRUE
;
12598 else if (p
->type
== bfd_section_reloc_link_order
12599 || p
->type
== bfd_symbol_reloc_link_order
)
12601 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
12606 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
12608 if (p
->type
== bfd_indirect_link_order
12609 && (bfd_get_flavour (sub
)
12610 == bfd_target_elf_flavour
)
12611 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
12612 != bed
->s
->elfclass
))
12614 const char *iclass
, *oclass
;
12616 switch (bed
->s
->elfclass
)
12618 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
12619 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
12620 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
12624 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
12626 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
12627 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
12628 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
12632 bfd_set_error (bfd_error_wrong_format
);
12634 /* xgettext:c-format */
12635 (_("%pB: file class %s incompatible with %s"),
12636 sub
, iclass
, oclass
);
12645 /* Free symbol buffer if needed. */
12646 if (!info
->reduce_memory_overheads
)
12648 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12649 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
12651 free (elf_tdata (sub
)->symbuf
);
12652 elf_tdata (sub
)->symbuf
= NULL
;
12658 /* Output any global symbols that got converted to local in a
12659 version script or due to symbol visibility. We do this in a
12660 separate step since ELF requires all local symbols to appear
12661 prior to any global symbols. FIXME: We should only do this if
12662 some global symbols were, in fact, converted to become local.
12663 FIXME: Will this work correctly with the Irix 5 linker? */
12664 eoinfo
.failed
= FALSE
;
12665 eoinfo
.flinfo
= &flinfo
;
12666 eoinfo
.localsyms
= TRUE
;
12667 eoinfo
.file_sym_done
= FALSE
;
12668 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12672 goto return_local_hash_table
;
12675 /* If backend needs to output some local symbols not present in the hash
12676 table, do it now. */
12677 if (bed
->elf_backend_output_arch_local_syms
12678 && (info
->strip
!= strip_all
|| emit_relocs
))
12680 typedef int (*out_sym_func
)
12681 (void *, const char *, Elf_Internal_Sym
*, asection
*,
12682 struct elf_link_hash_entry
*);
12684 if (! ((*bed
->elf_backend_output_arch_local_syms
)
12685 (abfd
, info
, &flinfo
,
12686 (out_sym_func
) elf_link_output_symstrtab
)))
12689 goto return_local_hash_table
;
12693 /* That wrote out all the local symbols. Finish up the symbol table
12694 with the global symbols. Even if we want to strip everything we
12695 can, we still need to deal with those global symbols that got
12696 converted to local in a version script. */
12698 /* The sh_info field records the index of the first non local symbol. */
12699 if (!symtab_hdr
->sh_info
)
12700 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12703 && htab
->dynsym
!= NULL
12704 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
12706 Elf_Internal_Sym sym
;
12707 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
12709 o
= htab
->dynsym
->output_section
;
12710 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
12712 /* Write out the section symbols for the output sections. */
12713 if (bfd_link_pic (info
)
12714 || htab
->is_relocatable_executable
)
12720 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12722 sym
.st_target_internal
= 0;
12724 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
12730 dynindx
= elf_section_data (s
)->dynindx
;
12733 indx
= elf_section_data (s
)->this_idx
;
12734 BFD_ASSERT (indx
> 0);
12735 sym
.st_shndx
= indx
;
12736 if (! check_dynsym (abfd
, &sym
))
12739 goto return_local_hash_table
;
12741 sym
.st_value
= s
->vma
;
12742 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
12743 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12747 /* Write out the local dynsyms. */
12748 if (htab
->dynlocal
)
12750 struct elf_link_local_dynamic_entry
*e
;
12751 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
12756 /* Copy the internal symbol and turn off visibility.
12757 Note that we saved a word of storage and overwrote
12758 the original st_name with the dynstr_index. */
12760 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
12761 sym
.st_shndx
= SHN_UNDEF
;
12763 s
= bfd_section_from_elf_index (e
->input_bfd
,
12766 && s
->output_section
!= NULL
12767 && elf_section_data (s
->output_section
) != NULL
)
12770 elf_section_data (s
->output_section
)->this_idx
;
12771 if (! check_dynsym (abfd
, &sym
))
12774 goto return_local_hash_table
;
12776 sym
.st_value
= (s
->output_section
->vma
12778 + e
->isym
.st_value
);
12781 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
12782 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12787 /* We get the global symbols from the hash table. */
12788 eoinfo
.failed
= FALSE
;
12789 eoinfo
.localsyms
= FALSE
;
12790 eoinfo
.flinfo
= &flinfo
;
12791 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12795 goto return_local_hash_table
;
12798 /* If backend needs to output some symbols not present in the hash
12799 table, do it now. */
12800 if (bed
->elf_backend_output_arch_syms
12801 && (info
->strip
!= strip_all
|| emit_relocs
))
12803 typedef int (*out_sym_func
)
12804 (void *, const char *, Elf_Internal_Sym
*, asection
*,
12805 struct elf_link_hash_entry
*);
12807 if (! ((*bed
->elf_backend_output_arch_syms
)
12808 (abfd
, info
, &flinfo
,
12809 (out_sym_func
) elf_link_output_symstrtab
)))
12812 goto return_local_hash_table
;
12816 /* Finalize the .strtab section. */
12817 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
12819 /* Swap out the .strtab section. */
12820 if (!elf_link_swap_symbols_out (&flinfo
))
12823 goto return_local_hash_table
;
12826 /* Now we know the size of the symtab section. */
12827 if (bfd_get_symcount (abfd
) > 0)
12829 /* Finish up and write out the symbol string table (.strtab)
12831 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
12832 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
12834 if (elf_symtab_shndx_list (abfd
))
12836 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
12838 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
12840 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
12841 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
12842 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
12843 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
12844 symtab_shndx_hdr
->sh_size
= amt
;
12846 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
12849 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
12850 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
12853 goto return_local_hash_table
;
12858 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
12859 /* sh_name was set in prep_headers. */
12860 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
12861 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
12862 symstrtab_hdr
->sh_addr
= 0;
12863 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
12864 symstrtab_hdr
->sh_entsize
= 0;
12865 symstrtab_hdr
->sh_link
= 0;
12866 symstrtab_hdr
->sh_info
= 0;
12867 /* sh_offset is set just below. */
12868 symstrtab_hdr
->sh_addralign
= 1;
12870 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
12872 elf_next_file_pos (abfd
) = off
;
12874 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
12875 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
12878 goto return_local_hash_table
;
12882 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
12884 _bfd_error_handler (_("%pB: failed to generate import library"),
12885 info
->out_implib_bfd
);
12887 goto return_local_hash_table
;
12890 /* Adjust the relocs to have the correct symbol indices. */
12891 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12893 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12896 if ((o
->flags
& SEC_RELOC
) == 0)
12899 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
12900 if (esdo
->rel
.hdr
!= NULL
12901 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
, info
))
12904 goto return_local_hash_table
;
12906 if (esdo
->rela
.hdr
!= NULL
12907 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
, info
))
12910 goto return_local_hash_table
;
12913 /* Set the reloc_count field to 0 to prevent write_relocs from
12914 trying to swap the relocs out itself. */
12915 o
->reloc_count
= 0;
12918 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
12919 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
12921 /* If we are linking against a dynamic object, or generating a
12922 shared library, finish up the dynamic linking information. */
12925 bfd_byte
*dyncon
, *dynconend
;
12927 /* Fix up .dynamic entries. */
12928 o
= bfd_get_linker_section (dynobj
, ".dynamic");
12929 BFD_ASSERT (o
!= NULL
);
12931 dyncon
= o
->contents
;
12932 dynconend
= o
->contents
+ o
->size
;
12933 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12935 Elf_Internal_Dyn dyn
;
12938 bfd_size_type sh_size
;
12941 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12948 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
12950 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
12952 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
12953 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
12956 dyn
.d_un
.d_val
= relativecount
;
12963 name
= info
->init_function
;
12966 name
= info
->fini_function
;
12969 struct elf_link_hash_entry
*h
;
12971 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
12973 && (h
->root
.type
== bfd_link_hash_defined
12974 || h
->root
.type
== bfd_link_hash_defweak
))
12976 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
12977 o
= h
->root
.u
.def
.section
;
12978 if (o
->output_section
!= NULL
)
12979 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
12980 + o
->output_offset
);
12983 /* The symbol is imported from another shared
12984 library and does not apply to this one. */
12985 dyn
.d_un
.d_ptr
= 0;
12992 case DT_PREINIT_ARRAYSZ
:
12993 name
= ".preinit_array";
12995 case DT_INIT_ARRAYSZ
:
12996 name
= ".init_array";
12998 case DT_FINI_ARRAYSZ
:
12999 name
= ".fini_array";
13001 o
= bfd_get_section_by_name (abfd
, name
);
13005 (_("could not find section %s"), name
);
13010 (_("warning: %s section has zero size"), name
);
13011 dyn
.d_un
.d_val
= o
->size
;
13014 case DT_PREINIT_ARRAY
:
13015 name
= ".preinit_array";
13017 case DT_INIT_ARRAY
:
13018 name
= ".init_array";
13020 case DT_FINI_ARRAY
:
13021 name
= ".fini_array";
13023 o
= bfd_get_section_by_name (abfd
, name
);
13030 name
= ".gnu.hash";
13039 name
= ".gnu.version_d";
13042 name
= ".gnu.version_r";
13045 name
= ".gnu.version";
13047 o
= bfd_get_linker_section (dynobj
, name
);
13049 if (o
== NULL
|| bfd_is_abs_section (o
->output_section
))
13052 (_("could not find section %s"), name
);
13055 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
13058 (_("warning: section '%s' is being made into a note"), name
);
13059 bfd_set_error (bfd_error_nonrepresentable_section
);
13062 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
13069 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
13075 for (i
= 1; i
< elf_numsections (abfd
); i
++)
13077 Elf_Internal_Shdr
*hdr
;
13079 hdr
= elf_elfsections (abfd
)[i
];
13080 if (hdr
->sh_type
== type
13081 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
13083 sh_size
+= hdr
->sh_size
;
13085 || sh_addr
> hdr
->sh_addr
)
13086 sh_addr
= hdr
->sh_addr
;
13090 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
13092 unsigned int opb
= bfd_octets_per_byte (abfd
, o
);
13094 /* Don't count procedure linkage table relocs in the
13095 overall reloc count. */
13096 sh_size
-= htab
->srelplt
->size
;
13098 /* If the size is zero, make the address zero too.
13099 This is to avoid a glibc bug. If the backend
13100 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13101 zero, then we'll put DT_RELA at the end of
13102 DT_JMPREL. glibc will interpret the end of
13103 DT_RELA matching the end of DT_JMPREL as the
13104 case where DT_RELA includes DT_JMPREL, and for
13105 LD_BIND_NOW will decide that processing DT_RELA
13106 will process the PLT relocs too. Net result:
13107 No PLT relocs applied. */
13110 /* If .rela.plt is the first .rela section, exclude
13111 it from DT_RELA. */
13112 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
13113 + htab
->srelplt
->output_offset
) * opb
)
13114 sh_addr
+= htab
->srelplt
->size
;
13117 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
13118 dyn
.d_un
.d_val
= sh_size
;
13120 dyn
.d_un
.d_ptr
= sh_addr
;
13123 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13127 /* If we have created any dynamic sections, then output them. */
13128 if (dynobj
!= NULL
)
13130 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
13133 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13134 if (bfd_link_textrel_check (info
)
13135 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
)
13137 bfd_byte
*dyncon
, *dynconend
;
13139 dyncon
= o
->contents
;
13140 dynconend
= o
->contents
+ o
->size
;
13141 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13143 Elf_Internal_Dyn dyn
;
13145 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13147 if (dyn
.d_tag
== DT_TEXTREL
)
13149 if (info
->textrel_check
== textrel_check_error
)
13150 info
->callbacks
->einfo
13151 (_("%P%X: read-only segment has dynamic relocations\n"));
13152 else if (bfd_link_dll (info
))
13153 info
->callbacks
->einfo
13154 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13156 info
->callbacks
->einfo
13157 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13163 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
13165 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
13167 || o
->output_section
== bfd_abs_section_ptr
)
13169 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
13171 /* At this point, we are only interested in sections
13172 created by _bfd_elf_link_create_dynamic_sections. */
13175 if (htab
->stab_info
.stabstr
== o
)
13177 if (htab
->eh_info
.hdr_sec
== o
)
13179 if (strcmp (o
->name
, ".dynstr") != 0)
13181 bfd_size_type octets
= ((file_ptr
) o
->output_offset
13182 * bfd_octets_per_byte (abfd
, o
));
13183 if (!bfd_set_section_contents (abfd
, o
->output_section
,
13184 o
->contents
, octets
, o
->size
))
13189 /* The contents of the .dynstr section are actually in a
13193 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
13194 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
13195 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
13201 if (!info
->resolve_section_groups
)
13203 bfd_boolean failed
= FALSE
;
13205 BFD_ASSERT (bfd_link_relocatable (info
));
13206 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
13211 /* If we have optimized stabs strings, output them. */
13212 if (htab
->stab_info
.stabstr
!= NULL
)
13214 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
13218 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
13221 if (info
->callbacks
->emit_ctf
)
13222 info
->callbacks
->emit_ctf ();
13224 elf_final_link_free (abfd
, &flinfo
);
13228 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
13229 if (contents
== NULL
)
13231 /* Bail out and fail. */
13233 goto return_local_hash_table
;
13235 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
13236 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
13240 return_local_hash_table
:
13241 if (info
->unique_symbol
)
13242 bfd_hash_table_free (&flinfo
.local_hash_table
);
13246 elf_final_link_free (abfd
, &flinfo
);
13248 goto return_local_hash_table
;
13251 /* Initialize COOKIE for input bfd ABFD. */
13254 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
13255 struct bfd_link_info
*info
, bfd
*abfd
)
13257 Elf_Internal_Shdr
*symtab_hdr
;
13258 const struct elf_backend_data
*bed
;
13260 bed
= get_elf_backend_data (abfd
);
13261 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13263 cookie
->abfd
= abfd
;
13264 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
13265 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
13266 if (cookie
->bad_symtab
)
13268 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13269 cookie
->extsymoff
= 0;
13273 cookie
->locsymcount
= symtab_hdr
->sh_info
;
13274 cookie
->extsymoff
= symtab_hdr
->sh_info
;
13277 if (bed
->s
->arch_size
== 32)
13278 cookie
->r_sym_shift
= 8;
13280 cookie
->r_sym_shift
= 32;
13282 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
13283 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
13285 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
13286 cookie
->locsymcount
, 0,
13288 if (cookie
->locsyms
== NULL
)
13290 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
13293 if (info
->keep_memory
)
13294 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
13299 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13302 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
13304 Elf_Internal_Shdr
*symtab_hdr
;
13306 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13307 if (symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
13308 free (cookie
->locsyms
);
13311 /* Initialize the relocation information in COOKIE for input section SEC
13312 of input bfd ABFD. */
13315 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13316 struct bfd_link_info
*info
, bfd
*abfd
,
13319 if (sec
->reloc_count
== 0)
13321 cookie
->rels
= NULL
;
13322 cookie
->relend
= NULL
;
13326 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
13327 info
->keep_memory
);
13328 if (cookie
->rels
== NULL
)
13330 cookie
->rel
= cookie
->rels
;
13331 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
13333 cookie
->rel
= cookie
->rels
;
13337 /* Free the memory allocated by init_reloc_cookie_rels,
13341 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13344 if (elf_section_data (sec
)->relocs
!= cookie
->rels
)
13345 free (cookie
->rels
);
13348 /* Initialize the whole of COOKIE for input section SEC. */
13351 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13352 struct bfd_link_info
*info
,
13355 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
13357 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
13362 fini_reloc_cookie (cookie
, sec
->owner
);
13367 /* Free the memory allocated by init_reloc_cookie_for_section,
13371 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13374 fini_reloc_cookie_rels (cookie
, sec
);
13375 fini_reloc_cookie (cookie
, sec
->owner
);
13378 /* Garbage collect unused sections. */
13380 /* Default gc_mark_hook. */
13383 _bfd_elf_gc_mark_hook (asection
*sec
,
13384 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13385 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13386 struct elf_link_hash_entry
*h
,
13387 Elf_Internal_Sym
*sym
)
13391 switch (h
->root
.type
)
13393 case bfd_link_hash_defined
:
13394 case bfd_link_hash_defweak
:
13395 return h
->root
.u
.def
.section
;
13397 case bfd_link_hash_common
:
13398 return h
->root
.u
.c
.p
->section
;
13405 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
13410 /* Return the debug definition section. */
13413 elf_gc_mark_debug_section (asection
*sec ATTRIBUTE_UNUSED
,
13414 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13415 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13416 struct elf_link_hash_entry
*h
,
13417 Elf_Internal_Sym
*sym
)
13421 /* Return the global debug definition section. */
13422 if ((h
->root
.type
== bfd_link_hash_defined
13423 || h
->root
.type
== bfd_link_hash_defweak
)
13424 && (h
->root
.u
.def
.section
->flags
& SEC_DEBUGGING
) != 0)
13425 return h
->root
.u
.def
.section
;
13429 /* Return the local debug definition section. */
13430 asection
*isec
= bfd_section_from_elf_index (sec
->owner
,
13432 if ((isec
->flags
& SEC_DEBUGGING
) != 0)
13439 /* COOKIE->rel describes a relocation against section SEC, which is
13440 a section we've decided to keep. Return the section that contains
13441 the relocation symbol, or NULL if no section contains it. */
13444 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
13445 elf_gc_mark_hook_fn gc_mark_hook
,
13446 struct elf_reloc_cookie
*cookie
,
13447 bfd_boolean
*start_stop
)
13449 unsigned long r_symndx
;
13450 struct elf_link_hash_entry
*h
, *hw
;
13452 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
13453 if (r_symndx
== STN_UNDEF
)
13456 if (r_symndx
>= cookie
->locsymcount
13457 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13459 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
13462 info
->callbacks
->einfo (_("%F%P: corrupt input: %pB\n"),
13466 while (h
->root
.type
== bfd_link_hash_indirect
13467 || h
->root
.type
== bfd_link_hash_warning
)
13468 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13470 /* Keep all aliases of the symbol too. If an object symbol
13471 needs to be copied into .dynbss then all of its aliases
13472 should be present as dynamic symbols, not just the one used
13473 on the copy relocation. */
13475 while (hw
->is_weakalias
)
13481 if (start_stop
!= NULL
)
13483 /* To work around a glibc bug, mark XXX input sections
13484 when there is a reference to __start_XXX or __stop_XXX
13488 asection
*s
= h
->u2
.start_stop_section
;
13489 *start_stop
= !s
->gc_mark
;
13494 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
13497 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
13498 &cookie
->locsyms
[r_symndx
]);
13501 /* COOKIE->rel describes a relocation against section SEC, which is
13502 a section we've decided to keep. Mark the section that contains
13503 the relocation symbol. */
13506 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
13508 elf_gc_mark_hook_fn gc_mark_hook
,
13509 struct elf_reloc_cookie
*cookie
)
13512 bfd_boolean start_stop
= FALSE
;
13514 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
13515 while (rsec
!= NULL
)
13517 if (!rsec
->gc_mark
)
13519 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
13520 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
13522 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
13527 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
13532 /* The mark phase of garbage collection. For a given section, mark
13533 it and any sections in this section's group, and all the sections
13534 which define symbols to which it refers. */
13537 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
13539 elf_gc_mark_hook_fn gc_mark_hook
)
13542 asection
*group_sec
, *eh_frame
;
13546 /* Mark all the sections in the group. */
13547 group_sec
= elf_section_data (sec
)->next_in_group
;
13548 if (group_sec
&& !group_sec
->gc_mark
)
13549 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
13552 /* Look through the section relocs. */
13554 eh_frame
= elf_eh_frame_section (sec
->owner
);
13555 if ((sec
->flags
& SEC_RELOC
) != 0
13556 && sec
->reloc_count
> 0
13557 && sec
!= eh_frame
)
13559 struct elf_reloc_cookie cookie
;
13561 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
13565 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
13566 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
13571 fini_reloc_cookie_for_section (&cookie
, sec
);
13575 if (ret
&& eh_frame
&& elf_fde_list (sec
))
13577 struct elf_reloc_cookie cookie
;
13579 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
13583 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
13584 gc_mark_hook
, &cookie
))
13586 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
13590 eh_frame
= elf_section_eh_frame_entry (sec
);
13591 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
13592 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
13598 /* Scan and mark sections in a special or debug section group. */
13601 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
13603 /* Point to first section of section group. */
13605 /* Used to iterate the section group. */
13608 bfd_boolean is_special_grp
= TRUE
;
13609 bfd_boolean is_debug_grp
= TRUE
;
13611 /* First scan to see if group contains any section other than debug
13612 and special section. */
13613 ssec
= msec
= elf_next_in_group (grp
);
13616 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
13617 is_debug_grp
= FALSE
;
13619 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
13620 is_special_grp
= FALSE
;
13622 msec
= elf_next_in_group (msec
);
13624 while (msec
!= ssec
);
13626 /* If this is a pure debug section group or pure special section group,
13627 keep all sections in this group. */
13628 if (is_debug_grp
|| is_special_grp
)
13633 msec
= elf_next_in_group (msec
);
13635 while (msec
!= ssec
);
13639 /* Keep debug and special sections. */
13642 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
13643 elf_gc_mark_hook_fn mark_hook
)
13647 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13650 bfd_boolean some_kept
;
13651 bfd_boolean debug_frag_seen
;
13652 bfd_boolean has_kept_debug_info
;
13654 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13656 isec
= ibfd
->sections
;
13657 if (isec
== NULL
|| isec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13660 /* Ensure all linker created sections are kept,
13661 see if any other section is already marked,
13662 and note if we have any fragmented debug sections. */
13663 debug_frag_seen
= some_kept
= has_kept_debug_info
= FALSE
;
13664 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13666 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
13668 else if (isec
->gc_mark
13669 && (isec
->flags
& SEC_ALLOC
) != 0
13670 && elf_section_type (isec
) != SHT_NOTE
)
13674 /* Since all sections, except for backend specific ones,
13675 have been garbage collected, call mark_hook on this
13676 section if any of its linked-to sections is marked. */
13677 asection
*linked_to_sec
= elf_linked_to_section (isec
);
13678 for (; linked_to_sec
!= NULL
;
13679 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
13680 if (linked_to_sec
->gc_mark
)
13682 if (!_bfd_elf_gc_mark (info
, isec
, mark_hook
))
13688 if (!debug_frag_seen
13689 && (isec
->flags
& SEC_DEBUGGING
)
13690 && CONST_STRNEQ (isec
->name
, ".debug_line."))
13691 debug_frag_seen
= TRUE
;
13692 else if (strcmp (bfd_section_name (isec
),
13693 "__patchable_function_entries") == 0
13694 && elf_linked_to_section (isec
) == NULL
)
13695 info
->callbacks
->einfo (_("%F%P: %pB(%pA): error: "
13696 "need linked-to section "
13697 "for --gc-sections\n"),
13698 isec
->owner
, isec
);
13701 /* If no non-note alloc section in this file will be kept, then
13702 we can toss out the debug and special sections. */
13706 /* Keep debug and special sections like .comment when they are
13707 not part of a group. Also keep section groups that contain
13708 just debug sections or special sections. NB: Sections with
13709 linked-to section has been handled above. */
13710 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13712 if ((isec
->flags
& SEC_GROUP
) != 0)
13713 _bfd_elf_gc_mark_debug_special_section_group (isec
);
13714 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
13715 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
13716 && elf_next_in_group (isec
) == NULL
13717 && elf_linked_to_section (isec
) == NULL
)
13719 if (isec
->gc_mark
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
13720 has_kept_debug_info
= TRUE
;
13723 /* Look for CODE sections which are going to be discarded,
13724 and find and discard any fragmented debug sections which
13725 are associated with that code section. */
13726 if (debug_frag_seen
)
13727 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13728 if ((isec
->flags
& SEC_CODE
) != 0
13729 && isec
->gc_mark
== 0)
13734 ilen
= strlen (isec
->name
);
13736 /* Association is determined by the name of the debug
13737 section containing the name of the code section as
13738 a suffix. For example .debug_line.text.foo is a
13739 debug section associated with .text.foo. */
13740 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
13744 if (dsec
->gc_mark
== 0
13745 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
13748 dlen
= strlen (dsec
->name
);
13751 && strncmp (dsec
->name
+ (dlen
- ilen
),
13752 isec
->name
, ilen
) == 0)
13757 /* Mark debug sections referenced by kept debug sections. */
13758 if (has_kept_debug_info
)
13759 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13761 && (isec
->flags
& SEC_DEBUGGING
) != 0)
13762 if (!_bfd_elf_gc_mark (info
, isec
,
13763 elf_gc_mark_debug_section
))
13770 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
13773 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13775 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13779 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13780 || elf_object_id (sub
) != elf_hash_table_id (elf_hash_table (info
))
13781 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13784 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13787 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13789 /* When any section in a section group is kept, we keep all
13790 sections in the section group. If the first member of
13791 the section group is excluded, we will also exclude the
13793 if (o
->flags
& SEC_GROUP
)
13795 asection
*first
= elf_next_in_group (o
);
13796 o
->gc_mark
= first
->gc_mark
;
13802 /* Skip sweeping sections already excluded. */
13803 if (o
->flags
& SEC_EXCLUDE
)
13806 /* Since this is early in the link process, it is simple
13807 to remove a section from the output. */
13808 o
->flags
|= SEC_EXCLUDE
;
13810 if (info
->print_gc_sections
&& o
->size
!= 0)
13811 /* xgettext:c-format */
13812 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13820 /* Propagate collected vtable information. This is called through
13821 elf_link_hash_traverse. */
13824 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
13826 /* Those that are not vtables. */
13828 || h
->u2
.vtable
== NULL
13829 || h
->u2
.vtable
->parent
== NULL
)
13832 /* Those vtables that do not have parents, we cannot merge. */
13833 if (h
->u2
.vtable
->parent
== (struct elf_link_hash_entry
*) -1)
13836 /* If we've already been done, exit. */
13837 if (h
->u2
.vtable
->used
&& h
->u2
.vtable
->used
[-1])
13840 /* Make sure the parent's table is up to date. */
13841 elf_gc_propagate_vtable_entries_used (h
->u2
.vtable
->parent
, okp
);
13843 if (h
->u2
.vtable
->used
== NULL
)
13845 /* None of this table's entries were referenced. Re-use the
13847 h
->u2
.vtable
->used
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13848 h
->u2
.vtable
->size
= h
->u2
.vtable
->parent
->u2
.vtable
->size
;
13853 bfd_boolean
*cu
, *pu
;
13855 /* Or the parent's entries into ours. */
13856 cu
= h
->u2
.vtable
->used
;
13858 pu
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13861 const struct elf_backend_data
*bed
;
13862 unsigned int log_file_align
;
13864 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
13865 log_file_align
= bed
->s
->log_file_align
;
13866 n
= h
->u2
.vtable
->parent
->u2
.vtable
->size
>> log_file_align
;
13881 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
13884 bfd_vma hstart
, hend
;
13885 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
13886 const struct elf_backend_data
*bed
;
13887 unsigned int log_file_align
;
13889 /* Take care of both those symbols that do not describe vtables as
13890 well as those that are not loaded. */
13892 || h
->u2
.vtable
== NULL
13893 || h
->u2
.vtable
->parent
== NULL
)
13896 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
13897 || h
->root
.type
== bfd_link_hash_defweak
);
13899 sec
= h
->root
.u
.def
.section
;
13900 hstart
= h
->root
.u
.def
.value
;
13901 hend
= hstart
+ h
->size
;
13903 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
13905 return *(bfd_boolean
*) okp
= FALSE
;
13906 bed
= get_elf_backend_data (sec
->owner
);
13907 log_file_align
= bed
->s
->log_file_align
;
13909 relend
= relstart
+ sec
->reloc_count
;
13911 for (rel
= relstart
; rel
< relend
; ++rel
)
13912 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
13914 /* If the entry is in use, do nothing. */
13915 if (h
->u2
.vtable
->used
13916 && (rel
->r_offset
- hstart
) < h
->u2
.vtable
->size
)
13918 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
13919 if (h
->u2
.vtable
->used
[entry
])
13922 /* Otherwise, kill it. */
13923 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
13929 /* Mark sections containing dynamically referenced symbols. When
13930 building shared libraries, we must assume that any visible symbol is
13934 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
13936 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
13937 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
13939 if ((h
->root
.type
== bfd_link_hash_defined
13940 || h
->root
.type
== bfd_link_hash_defweak
)
13941 && ((h
->ref_dynamic
&& !h
->forced_local
)
13942 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
13943 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
13944 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
13945 && (!bfd_link_executable (info
)
13946 || info
->gc_keep_exported
13947 || info
->export_dynamic
13950 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
13951 && (h
->versioned
>= versioned
13952 || !bfd_hide_sym_by_version (info
->version_info
,
13953 h
->root
.root
.string
)))))
13954 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13959 /* Keep all sections containing symbols undefined on the command-line,
13960 and the section containing the entry symbol. */
13963 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
13965 struct bfd_sym_chain
*sym
;
13967 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
13969 struct elf_link_hash_entry
*h
;
13971 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
13972 FALSE
, FALSE
, FALSE
);
13975 && (h
->root
.type
== bfd_link_hash_defined
13976 || h
->root
.type
== bfd_link_hash_defweak
)
13977 && !bfd_is_const_section (h
->root
.u
.def
.section
))
13978 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13983 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
13984 struct bfd_link_info
*info
)
13986 bfd
*ibfd
= info
->input_bfds
;
13988 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13991 struct elf_reloc_cookie cookie
;
13993 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13995 sec
= ibfd
->sections
;
13996 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13999 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
14002 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
14004 if (CONST_STRNEQ (bfd_section_name (sec
), ".eh_frame_entry")
14005 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
14007 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
14008 fini_reloc_cookie_rels (&cookie
, sec
);
14015 /* Do mark and sweep of unused sections. */
14018 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
14020 bfd_boolean ok
= TRUE
;
14022 elf_gc_mark_hook_fn gc_mark_hook
;
14023 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14024 struct elf_link_hash_table
*htab
;
14026 if (!bed
->can_gc_sections
14027 || !is_elf_hash_table (info
->hash
))
14029 _bfd_error_handler(_("warning: gc-sections option ignored"));
14033 bed
->gc_keep (info
);
14034 htab
= elf_hash_table (info
);
14036 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14037 at the .eh_frame section if we can mark the FDEs individually. */
14038 for (sub
= info
->input_bfds
;
14039 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
14040 sub
= sub
->link
.next
)
14043 struct elf_reloc_cookie cookie
;
14045 sec
= sub
->sections
;
14046 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14048 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
14049 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
14051 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
14052 if (elf_section_data (sec
)->sec_info
14053 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
14054 elf_eh_frame_section (sub
) = sec
;
14055 fini_reloc_cookie_for_section (&cookie
, sec
);
14056 sec
= bfd_get_next_section_by_name (NULL
, sec
);
14060 /* Apply transitive closure to the vtable entry usage info. */
14061 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
14065 /* Kill the vtable relocations that were not used. */
14066 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &ok
);
14070 /* Mark dynamically referenced symbols. */
14071 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
14072 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
14074 /* Grovel through relocs to find out who stays ... */
14075 gc_mark_hook
= bed
->gc_mark_hook
;
14076 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
14080 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
14081 || elf_object_id (sub
) != elf_hash_table_id (htab
)
14082 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
14086 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14089 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14090 Also treat note sections as a root, if the section is not part
14091 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14092 well as FINI_ARRAY sections for ld -r. */
14093 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
14095 && (o
->flags
& SEC_EXCLUDE
) == 0
14096 && ((o
->flags
& SEC_KEEP
) != 0
14097 || (bfd_link_relocatable (info
)
14098 && ((elf_section_data (o
)->this_hdr
.sh_type
14099 == SHT_PREINIT_ARRAY
)
14100 || (elf_section_data (o
)->this_hdr
.sh_type
14102 || (elf_section_data (o
)->this_hdr
.sh_type
14103 == SHT_FINI_ARRAY
)))
14104 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
14105 && elf_next_in_group (o
) == NULL
)))
14107 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
14112 /* Allow the backend to mark additional target specific sections. */
14113 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
14115 /* ... and mark SEC_EXCLUDE for those that go. */
14116 return elf_gc_sweep (abfd
, info
);
14119 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14122 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
14124 struct elf_link_hash_entry
*h
,
14127 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
14128 struct elf_link_hash_entry
**search
, *child
;
14129 size_t extsymcount
;
14130 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14132 /* The sh_info field of the symtab header tells us where the
14133 external symbols start. We don't care about the local symbols at
14135 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
14136 if (!elf_bad_symtab (abfd
))
14137 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
14139 sym_hashes
= elf_sym_hashes (abfd
);
14140 sym_hashes_end
= sym_hashes
+ extsymcount
;
14142 /* Hunt down the child symbol, which is in this section at the same
14143 offset as the relocation. */
14144 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
14146 if ((child
= *search
) != NULL
14147 && (child
->root
.type
== bfd_link_hash_defined
14148 || child
->root
.type
== bfd_link_hash_defweak
)
14149 && child
->root
.u
.def
.section
== sec
14150 && child
->root
.u
.def
.value
== offset
)
14154 /* xgettext:c-format */
14155 _bfd_error_handler (_("%pB: %pA+%#" PRIx64
": no symbol found for INHERIT"),
14156 abfd
, sec
, (uint64_t) offset
);
14157 bfd_set_error (bfd_error_invalid_operation
);
14161 if (!child
->u2
.vtable
)
14163 child
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14164 bfd_zalloc (abfd
, sizeof (*child
->u2
.vtable
)));
14165 if (!child
->u2
.vtable
)
14170 /* This *should* only be the absolute section. It could potentially
14171 be that someone has defined a non-global vtable though, which
14172 would be bad. It isn't worth paging in the local symbols to be
14173 sure though; that case should simply be handled by the assembler. */
14175 child
->u2
.vtable
->parent
= (struct elf_link_hash_entry
*) -1;
14178 child
->u2
.vtable
->parent
= h
;
14183 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14186 bfd_elf_gc_record_vtentry (bfd
*abfd
, asection
*sec
,
14187 struct elf_link_hash_entry
*h
,
14190 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14191 unsigned int log_file_align
= bed
->s
->log_file_align
;
14195 /* xgettext:c-format */
14196 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14198 bfd_set_error (bfd_error_bad_value
);
14204 h
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14205 bfd_zalloc (abfd
, sizeof (*h
->u2
.vtable
)));
14210 if (addend
>= h
->u2
.vtable
->size
)
14212 size_t size
, bytes
, file_align
;
14213 bfd_boolean
*ptr
= h
->u2
.vtable
->used
;
14215 /* While the symbol is undefined, we have to be prepared to handle
14217 file_align
= 1 << log_file_align
;
14218 if (h
->root
.type
== bfd_link_hash_undefined
)
14219 size
= addend
+ file_align
;
14223 if (addend
>= size
)
14225 /* Oops! We've got a reference past the defined end of
14226 the table. This is probably a bug -- shall we warn? */
14227 size
= addend
+ file_align
;
14230 size
= (size
+ file_align
- 1) & -file_align
;
14232 /* Allocate one extra entry for use as a "done" flag for the
14233 consolidation pass. */
14234 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
14238 ptr
= (bfd_boolean
*) bfd_realloc (ptr
- 1, bytes
);
14244 oldbytes
= (((h
->u2
.vtable
->size
>> log_file_align
) + 1)
14245 * sizeof (bfd_boolean
));
14246 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
14250 ptr
= (bfd_boolean
*) bfd_zmalloc (bytes
);
14255 /* And arrange for that done flag to be at index -1. */
14256 h
->u2
.vtable
->used
= ptr
+ 1;
14257 h
->u2
.vtable
->size
= size
;
14260 h
->u2
.vtable
->used
[addend
>> log_file_align
] = TRUE
;
14265 /* Map an ELF section header flag to its corresponding string. */
14269 flagword flag_value
;
14270 } elf_flags_to_name_table
;
14272 static elf_flags_to_name_table elf_flags_to_names
[] =
14274 { "SHF_WRITE", SHF_WRITE
},
14275 { "SHF_ALLOC", SHF_ALLOC
},
14276 { "SHF_EXECINSTR", SHF_EXECINSTR
},
14277 { "SHF_MERGE", SHF_MERGE
},
14278 { "SHF_STRINGS", SHF_STRINGS
},
14279 { "SHF_INFO_LINK", SHF_INFO_LINK
},
14280 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
14281 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
14282 { "SHF_GROUP", SHF_GROUP
},
14283 { "SHF_TLS", SHF_TLS
},
14284 { "SHF_MASKOS", SHF_MASKOS
},
14285 { "SHF_EXCLUDE", SHF_EXCLUDE
},
14288 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14290 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
14291 struct flag_info
*flaginfo
,
14294 const bfd_vma sh_flags
= elf_section_flags (section
);
14296 if (!flaginfo
->flags_initialized
)
14298 bfd
*obfd
= info
->output_bfd
;
14299 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14300 struct flag_info_list
*tf
= flaginfo
->flag_list
;
14302 int without_hex
= 0;
14304 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
14307 flagword (*lookup
) (char *);
14309 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
14310 if (lookup
!= NULL
)
14312 flagword hexval
= (*lookup
) ((char *) tf
->name
);
14316 if (tf
->with
== with_flags
)
14317 with_hex
|= hexval
;
14318 else if (tf
->with
== without_flags
)
14319 without_hex
|= hexval
;
14324 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
14326 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
14328 if (tf
->with
== with_flags
)
14329 with_hex
|= elf_flags_to_names
[i
].flag_value
;
14330 else if (tf
->with
== without_flags
)
14331 without_hex
|= elf_flags_to_names
[i
].flag_value
;
14338 info
->callbacks
->einfo
14339 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
14343 flaginfo
->flags_initialized
= TRUE
;
14344 flaginfo
->only_with_flags
|= with_hex
;
14345 flaginfo
->not_with_flags
|= without_hex
;
14348 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
14351 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
14357 struct alloc_got_off_arg
{
14359 struct bfd_link_info
*info
;
14362 /* We need a special top-level link routine to convert got reference counts
14363 to real got offsets. */
14366 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
14368 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
14369 bfd
*obfd
= gofarg
->info
->output_bfd
;
14370 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14372 if (h
->got
.refcount
> 0)
14374 h
->got
.offset
= gofarg
->gotoff
;
14375 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
14378 h
->got
.offset
= (bfd_vma
) -1;
14383 /* And an accompanying bit to work out final got entry offsets once
14384 we're done. Should be called from final_link. */
14387 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
14388 struct bfd_link_info
*info
)
14391 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14393 struct alloc_got_off_arg gofarg
;
14395 BFD_ASSERT (abfd
== info
->output_bfd
);
14397 if (! is_elf_hash_table (info
->hash
))
14400 /* The GOT offset is relative to the .got section, but the GOT header is
14401 put into the .got.plt section, if the backend uses it. */
14402 if (bed
->want_got_plt
)
14405 gotoff
= bed
->got_header_size
;
14407 /* Do the local .got entries first. */
14408 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
14410 bfd_signed_vma
*local_got
;
14411 size_t j
, locsymcount
;
14412 Elf_Internal_Shdr
*symtab_hdr
;
14414 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
14417 local_got
= elf_local_got_refcounts (i
);
14421 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
14422 if (elf_bad_symtab (i
))
14423 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
14425 locsymcount
= symtab_hdr
->sh_info
;
14427 for (j
= 0; j
< locsymcount
; ++j
)
14429 if (local_got
[j
] > 0)
14431 local_got
[j
] = gotoff
;
14432 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
14435 local_got
[j
] = (bfd_vma
) -1;
14439 /* Then the global .got entries. .plt refcounts are handled by
14440 adjust_dynamic_symbol */
14441 gofarg
.gotoff
= gotoff
;
14442 gofarg
.info
= info
;
14443 elf_link_hash_traverse (elf_hash_table (info
),
14444 elf_gc_allocate_got_offsets
,
14449 /* Many folk need no more in the way of final link than this, once
14450 got entry reference counting is enabled. */
14453 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
14455 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
14458 /* Invoke the regular ELF backend linker to do all the work. */
14459 return bfd_elf_final_link (abfd
, info
);
14463 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
14465 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
14467 if (rcookie
->bad_symtab
)
14468 rcookie
->rel
= rcookie
->rels
;
14470 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
14472 unsigned long r_symndx
;
14474 if (! rcookie
->bad_symtab
)
14475 if (rcookie
->rel
->r_offset
> offset
)
14477 if (rcookie
->rel
->r_offset
!= offset
)
14480 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
14481 if (r_symndx
== STN_UNDEF
)
14484 if (r_symndx
>= rcookie
->locsymcount
14485 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
14487 struct elf_link_hash_entry
*h
;
14489 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
14491 while (h
->root
.type
== bfd_link_hash_indirect
14492 || h
->root
.type
== bfd_link_hash_warning
)
14493 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
14495 if ((h
->root
.type
== bfd_link_hash_defined
14496 || h
->root
.type
== bfd_link_hash_defweak
)
14497 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
14498 || h
->root
.u
.def
.section
->kept_section
!= NULL
14499 || discarded_section (h
->root
.u
.def
.section
)))
14504 /* It's not a relocation against a global symbol,
14505 but it could be a relocation against a local
14506 symbol for a discarded section. */
14508 Elf_Internal_Sym
*isym
;
14510 /* Need to: get the symbol; get the section. */
14511 isym
= &rcookie
->locsyms
[r_symndx
];
14512 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
14514 && (isec
->kept_section
!= NULL
14515 || discarded_section (isec
)))
14523 /* Discard unneeded references to discarded sections.
14524 Returns -1 on error, 1 if any section's size was changed, 0 if
14525 nothing changed. This function assumes that the relocations are in
14526 sorted order, which is true for all known assemblers. */
14529 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
14531 struct elf_reloc_cookie cookie
;
14536 if (info
->traditional_format
14537 || !is_elf_hash_table (info
->hash
))
14540 o
= bfd_get_section_by_name (output_bfd
, ".stab");
14545 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14548 || i
->reloc_count
== 0
14549 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
14553 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14556 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14559 if (_bfd_discard_section_stabs (abfd
, i
,
14560 elf_section_data (i
)->sec_info
,
14561 bfd_elf_reloc_symbol_deleted_p
,
14565 fini_reloc_cookie_for_section (&cookie
, i
);
14570 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
14571 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
14575 int eh_changed
= 0;
14576 unsigned int eh_alignment
; /* Octets. */
14578 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14584 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14587 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14590 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
14591 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
14592 bfd_elf_reloc_symbol_deleted_p
,
14596 if (i
->size
!= i
->rawsize
)
14600 fini_reloc_cookie_for_section (&cookie
, i
);
14603 eh_alignment
= ((1 << o
->alignment_power
)
14604 * bfd_octets_per_byte (output_bfd
, o
));
14605 /* Skip over zero terminator, and prevent empty sections from
14606 adding alignment padding at the end. */
14607 for (i
= o
->map_tail
.s
; i
!= NULL
; i
= i
->map_tail
.s
)
14609 i
->flags
|= SEC_EXCLUDE
;
14610 else if (i
->size
> 4)
14612 /* The last non-empty eh_frame section doesn't need padding. */
14615 /* Any prior sections must pad the last FDE out to the output
14616 section alignment. Otherwise we might have zero padding
14617 between sections, which would be seen as a terminator. */
14618 for (; i
!= NULL
; i
= i
->map_tail
.s
)
14620 /* All but the last zero terminator should have been removed. */
14625 = (i
->size
+ eh_alignment
- 1) & -eh_alignment
;
14626 if (i
->size
!= size
)
14634 elf_link_hash_traverse (elf_hash_table (info
),
14635 _bfd_elf_adjust_eh_frame_global_symbol
, NULL
);
14638 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
14640 const struct elf_backend_data
*bed
;
14643 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14645 s
= abfd
->sections
;
14646 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14649 bed
= get_elf_backend_data (abfd
);
14651 if (bed
->elf_backend_discard_info
!= NULL
)
14653 if (!init_reloc_cookie (&cookie
, info
, abfd
))
14656 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
14659 fini_reloc_cookie (&cookie
, abfd
);
14663 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
14664 _bfd_elf_end_eh_frame_parsing (info
);
14666 if (info
->eh_frame_hdr_type
14667 && !bfd_link_relocatable (info
)
14668 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
14675 _bfd_elf_section_already_linked (bfd
*abfd
,
14677 struct bfd_link_info
*info
)
14680 const char *name
, *key
;
14681 struct bfd_section_already_linked
*l
;
14682 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
14684 if (sec
->output_section
== bfd_abs_section_ptr
)
14687 flags
= sec
->flags
;
14689 /* Return if it isn't a linkonce section. A comdat group section
14690 also has SEC_LINK_ONCE set. */
14691 if ((flags
& SEC_LINK_ONCE
) == 0)
14694 /* Don't put group member sections on our list of already linked
14695 sections. They are handled as a group via their group section. */
14696 if (elf_sec_group (sec
) != NULL
)
14699 /* For a SHT_GROUP section, use the group signature as the key. */
14701 if ((flags
& SEC_GROUP
) != 0
14702 && elf_next_in_group (sec
) != NULL
14703 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
14704 key
= elf_group_name (elf_next_in_group (sec
));
14707 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14708 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
14709 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
14712 /* Must be a user linkonce section that doesn't follow gcc's
14713 naming convention. In this case we won't be matching
14714 single member groups. */
14718 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
14720 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14722 /* We may have 2 different types of sections on the list: group
14723 sections with a signature of <key> (<key> is some string),
14724 and linkonce sections named .gnu.linkonce.<type>.<key>.
14725 Match like sections. LTO plugin sections are an exception.
14726 They are always named .gnu.linkonce.t.<key> and match either
14727 type of section. */
14728 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
14729 && ((flags
& SEC_GROUP
) != 0
14730 || strcmp (name
, l
->sec
->name
) == 0))
14731 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0
14732 || (sec
->owner
->flags
& BFD_PLUGIN
) != 0)
14734 /* The section has already been linked. See if we should
14735 issue a warning. */
14736 if (!_bfd_handle_already_linked (sec
, l
, info
))
14739 if (flags
& SEC_GROUP
)
14741 asection
*first
= elf_next_in_group (sec
);
14742 asection
*s
= first
;
14746 s
->output_section
= bfd_abs_section_ptr
;
14747 /* Record which group discards it. */
14748 s
->kept_section
= l
->sec
;
14749 s
= elf_next_in_group (s
);
14750 /* These lists are circular. */
14760 /* A single member comdat group section may be discarded by a
14761 linkonce section and vice versa. */
14762 if ((flags
& SEC_GROUP
) != 0)
14764 asection
*first
= elf_next_in_group (sec
);
14766 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
14767 /* Check this single member group against linkonce sections. */
14768 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14769 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14770 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
14772 first
->output_section
= bfd_abs_section_ptr
;
14773 first
->kept_section
= l
->sec
;
14774 sec
->output_section
= bfd_abs_section_ptr
;
14779 /* Check this linkonce section against single member groups. */
14780 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14781 if (l
->sec
->flags
& SEC_GROUP
)
14783 asection
*first
= elf_next_in_group (l
->sec
);
14786 && elf_next_in_group (first
) == first
14787 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
14789 sec
->output_section
= bfd_abs_section_ptr
;
14790 sec
->kept_section
= first
;
14795 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14796 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14797 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14798 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14799 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14800 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14801 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14802 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14803 The reverse order cannot happen as there is never a bfd with only the
14804 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14805 matter as here were are looking only for cross-bfd sections. */
14807 if ((flags
& SEC_GROUP
) == 0 && CONST_STRNEQ (name
, ".gnu.linkonce.r."))
14808 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14809 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14810 && CONST_STRNEQ (l
->sec
->name
, ".gnu.linkonce.t."))
14812 if (abfd
!= l
->sec
->owner
)
14813 sec
->output_section
= bfd_abs_section_ptr
;
14817 /* This is the first section with this name. Record it. */
14818 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
14819 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
14820 return sec
->output_section
== bfd_abs_section_ptr
;
14824 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
14826 return sym
->st_shndx
== SHN_COMMON
;
14830 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
14836 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
14838 return bfd_com_section_ptr
;
14842 _bfd_elf_default_got_elt_size (bfd
*abfd
,
14843 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
14844 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
14845 bfd
*ibfd ATTRIBUTE_UNUSED
,
14846 unsigned long symndx ATTRIBUTE_UNUSED
)
14848 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14849 return bed
->s
->arch_size
/ 8;
14852 /* Routines to support the creation of dynamic relocs. */
14854 /* Returns the name of the dynamic reloc section associated with SEC. */
14856 static const char *
14857 get_dynamic_reloc_section_name (bfd
* abfd
,
14859 bfd_boolean is_rela
)
14862 const char *old_name
= bfd_section_name (sec
);
14863 const char *prefix
= is_rela
? ".rela" : ".rel";
14865 if (old_name
== NULL
)
14868 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
14869 sprintf (name
, "%s%s", prefix
, old_name
);
14874 /* Returns the dynamic reloc section associated with SEC.
14875 If necessary compute the name of the dynamic reloc section based
14876 on SEC's name (looked up in ABFD's string table) and the setting
14880 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
14882 bfd_boolean is_rela
)
14884 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14886 if (reloc_sec
== NULL
)
14888 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14892 reloc_sec
= bfd_get_linker_section (abfd
, name
);
14894 if (reloc_sec
!= NULL
)
14895 elf_section_data (sec
)->sreloc
= reloc_sec
;
14902 /* Returns the dynamic reloc section associated with SEC. If the
14903 section does not exist it is created and attached to the DYNOBJ
14904 bfd and stored in the SRELOC field of SEC's elf_section_data
14907 ALIGNMENT is the alignment for the newly created section and
14908 IS_RELA defines whether the name should be .rela.<SEC's name>
14909 or .rel.<SEC's name>. The section name is looked up in the
14910 string table associated with ABFD. */
14913 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
14915 unsigned int alignment
,
14917 bfd_boolean is_rela
)
14919 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14921 if (reloc_sec
== NULL
)
14923 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14928 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
14930 if (reloc_sec
== NULL
)
14932 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
14933 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
14934 if ((sec
->flags
& SEC_ALLOC
) != 0)
14935 flags
|= SEC_ALLOC
| SEC_LOAD
;
14937 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
14938 if (reloc_sec
!= NULL
)
14940 /* _bfd_elf_get_sec_type_attr chooses a section type by
14941 name. Override as it may be wrong, eg. for a user
14942 section named "auto" we'll get ".relauto" which is
14943 seen to be a .rela section. */
14944 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
14945 if (!bfd_set_section_alignment (reloc_sec
, alignment
))
14950 elf_section_data (sec
)->sreloc
= reloc_sec
;
14956 /* Copy the ELF symbol type and other attributes for a linker script
14957 assignment from HSRC to HDEST. Generally this should be treated as
14958 if we found a strong non-dynamic definition for HDEST (except that
14959 ld ignores multiple definition errors). */
14961 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
14962 struct bfd_link_hash_entry
*hdest
,
14963 struct bfd_link_hash_entry
*hsrc
)
14965 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
14966 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
14967 Elf_Internal_Sym isym
;
14969 ehdest
->type
= ehsrc
->type
;
14970 ehdest
->target_internal
= ehsrc
->target_internal
;
14972 isym
.st_other
= ehsrc
->other
;
14973 elf_merge_st_other (abfd
, ehdest
, &isym
, NULL
, TRUE
, FALSE
);
14976 /* Append a RELA relocation REL to section S in BFD. */
14979 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14981 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14982 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
14983 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
14984 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
14987 /* Append a REL relocation REL to section S in BFD. */
14990 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14992 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14993 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
14994 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
14995 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);
14998 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15000 struct bfd_link_hash_entry
*
15001 bfd_elf_define_start_stop (struct bfd_link_info
*info
,
15002 const char *symbol
, asection
*sec
)
15004 struct elf_link_hash_entry
*h
;
15006 h
= elf_link_hash_lookup (elf_hash_table (info
), symbol
,
15007 FALSE
, FALSE
, TRUE
);
15008 /* NB: Common symbols will be turned into definition later. */
15010 && (h
->root
.type
== bfd_link_hash_undefined
15011 || h
->root
.type
== bfd_link_hash_undefweak
15012 || ((h
->ref_regular
|| h
->def_dynamic
)
15014 && h
->root
.type
!= bfd_link_hash_common
)))
15016 bfd_boolean was_dynamic
= h
->ref_dynamic
|| h
->def_dynamic
;
15017 h
->verinfo
.verdef
= NULL
;
15018 h
->root
.type
= bfd_link_hash_defined
;
15019 h
->root
.u
.def
.section
= sec
;
15020 h
->root
.u
.def
.value
= 0;
15021 h
->def_regular
= 1;
15022 h
->def_dynamic
= 0;
15024 h
->u2
.start_stop_section
= sec
;
15025 if (symbol
[0] == '.')
15027 /* .startof. and .sizeof. symbols are local. */
15028 const struct elf_backend_data
*bed
;
15029 bed
= get_elf_backend_data (info
->output_bfd
);
15030 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
15034 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
15035 h
->other
= ((h
->other
& ~ELF_ST_VISIBILITY (-1))
15036 | info
->start_stop_visibility
);
15038 bfd_elf_link_record_dynamic_symbol (info
, h
);
15045 /* Find dynamic relocs for H that apply to read-only sections. */
15048 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry
*h
)
15050 struct elf_dyn_relocs
*p
;
15052 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
15054 asection
*s
= p
->sec
->output_section
;
15056 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
15062 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15063 read-only sections. */
15066 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry
*h
, void *inf
)
15070 if (h
->root
.type
== bfd_link_hash_indirect
)
15073 sec
= _bfd_elf_readonly_dynrelocs (h
);
15076 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
15078 info
->flags
|= DF_TEXTREL
;
15079 /* xgettext:c-format */
15080 info
->callbacks
->minfo (_("%pB: dynamic relocation against `%pT' "
15081 "in read-only section `%pA'\n"),
15082 sec
->owner
, h
->root
.root
.string
, sec
);
15084 if (bfd_link_textrel_check (info
))
15085 /* xgettext:c-format */
15086 info
->callbacks
->einfo (_("%P: %pB: warning: relocation against `%s' "
15087 "in read-only section `%pA'\n"),
15088 sec
->owner
, h
->root
.root
.string
, sec
);
15090 /* Not an error, just cut short the traversal. */
15096 /* Add dynamic tags. */
15099 _bfd_elf_add_dynamic_tags (bfd
*output_bfd
, struct bfd_link_info
*info
,
15100 bfd_boolean need_dynamic_reloc
)
15102 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
15104 if (htab
->dynamic_sections_created
)
15106 /* Add some entries to the .dynamic section. We fill in the
15107 values later, in finish_dynamic_sections, but we must add
15108 the entries now so that we get the correct size for the
15109 .dynamic section. The DT_DEBUG entry is filled in by the
15110 dynamic linker and used by the debugger. */
15111 #define add_dynamic_entry(TAG, VAL) \
15112 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15114 const struct elf_backend_data
*bed
15115 = get_elf_backend_data (output_bfd
);
15117 if (bfd_link_executable (info
))
15119 if (!add_dynamic_entry (DT_DEBUG
, 0))
15123 if (htab
->dt_pltgot_required
|| htab
->splt
->size
!= 0)
15125 /* DT_PLTGOT is used by prelink even if there is no PLT
15127 if (!add_dynamic_entry (DT_PLTGOT
, 0))
15131 if (htab
->dt_jmprel_required
|| htab
->srelplt
->size
!= 0)
15133 if (!add_dynamic_entry (DT_PLTRELSZ
, 0)
15134 || !add_dynamic_entry (DT_PLTREL
,
15135 (bed
->rela_plts_and_copies_p
15136 ? DT_RELA
: DT_REL
))
15137 || !add_dynamic_entry (DT_JMPREL
, 0))
15141 if (htab
->tlsdesc_plt
15142 && (!add_dynamic_entry (DT_TLSDESC_PLT
, 0)
15143 || !add_dynamic_entry (DT_TLSDESC_GOT
, 0)))
15146 if (need_dynamic_reloc
)
15148 if (bed
->rela_plts_and_copies_p
)
15150 if (!add_dynamic_entry (DT_RELA
, 0)
15151 || !add_dynamic_entry (DT_RELASZ
, 0)
15152 || !add_dynamic_entry (DT_RELAENT
,
15153 bed
->s
->sizeof_rela
))
15158 if (!add_dynamic_entry (DT_REL
, 0)
15159 || !add_dynamic_entry (DT_RELSZ
, 0)
15160 || !add_dynamic_entry (DT_RELENT
,
15161 bed
->s
->sizeof_rel
))
15165 /* If any dynamic relocs apply to a read-only section,
15166 then we need a DT_TEXTREL entry. */
15167 if ((info
->flags
& DF_TEXTREL
) == 0)
15168 elf_link_hash_traverse (htab
, _bfd_elf_maybe_set_textrel
,
15171 if ((info
->flags
& DF_TEXTREL
) != 0)
15173 if (htab
->ifunc_resolvers
)
15174 info
->callbacks
->einfo
15175 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15176 "may result in a segfault at runtime; recompile with %s\n"),
15177 bfd_link_dll (info
) ? "-fPIC" : "-fPIE");
15179 if (!add_dynamic_entry (DT_TEXTREL
, 0))
15184 #undef add_dynamic_entry