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"
35 /* This struct is used to pass information to routines called via
36 elf_link_hash_traverse which must return failure. */
38 struct elf_info_failed
40 struct bfd_link_info
*info
;
44 /* This structure is used to pass information to
45 _bfd_elf_link_find_version_dependencies. */
47 struct elf_find_verdep_info
49 /* General link information. */
50 struct bfd_link_info
*info
;
51 /* The number of dependencies. */
53 /* Whether we had a failure. */
57 static bfd_boolean _bfd_elf_fix_symbol_flags
58 (struct elf_link_hash_entry
*, struct elf_info_failed
*);
61 _bfd_elf_section_for_symbol (struct elf_reloc_cookie
*cookie
,
62 unsigned long r_symndx
,
65 if (r_symndx
>= cookie
->locsymcount
66 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
68 struct elf_link_hash_entry
*h
;
70 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
72 while (h
->root
.type
== bfd_link_hash_indirect
73 || h
->root
.type
== bfd_link_hash_warning
)
74 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
76 if ((h
->root
.type
== bfd_link_hash_defined
77 || h
->root
.type
== bfd_link_hash_defweak
)
78 && discarded_section (h
->root
.u
.def
.section
))
79 return h
->root
.u
.def
.section
;
85 /* It's not a relocation against a global symbol,
86 but it could be a relocation against a local
87 symbol for a discarded section. */
89 Elf_Internal_Sym
*isym
;
91 /* Need to: get the symbol; get the section. */
92 isym
= &cookie
->locsyms
[r_symndx
];
93 isec
= bfd_section_from_elf_index (cookie
->abfd
, isym
->st_shndx
);
95 && discard
? discarded_section (isec
) : 1)
101 /* Define a symbol in a dynamic linkage section. */
103 struct elf_link_hash_entry
*
104 _bfd_elf_define_linkage_sym (bfd
*abfd
,
105 struct bfd_link_info
*info
,
109 struct elf_link_hash_entry
*h
;
110 struct bfd_link_hash_entry
*bh
;
111 const struct elf_backend_data
*bed
;
113 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
116 /* Zap symbol defined in an as-needed lib that wasn't linked.
117 This is a symptom of a larger problem: Absolute symbols
118 defined in shared libraries can't be overridden, because we
119 lose the link to the bfd which is via the symbol section. */
120 h
->root
.type
= bfd_link_hash_new
;
126 bed
= get_elf_backend_data (abfd
);
127 if (!_bfd_generic_link_add_one_symbol (info
, abfd
, name
, BSF_GLOBAL
,
128 sec
, 0, NULL
, FALSE
, bed
->collect
,
131 h
= (struct elf_link_hash_entry
*) bh
;
132 BFD_ASSERT (h
!= NULL
);
135 h
->root
.linker_def
= 1;
136 h
->type
= STT_OBJECT
;
137 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
138 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
140 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
145 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
149 struct elf_link_hash_entry
*h
;
150 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
151 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
153 /* This function may be called more than once. */
154 if (htab
->sgot
!= NULL
)
157 flags
= bed
->dynamic_sec_flags
;
159 s
= bfd_make_section_anyway_with_flags (abfd
,
160 (bed
->rela_plts_and_copies_p
161 ? ".rela.got" : ".rel.got"),
162 (bed
->dynamic_sec_flags
165 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
169 s
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
171 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
175 if (bed
->want_got_plt
)
177 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
179 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
184 /* The first bit of the global offset table is the header. */
185 s
->size
+= bed
->got_header_size
;
187 if (bed
->want_got_sym
)
189 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
190 (or .got.plt) section. We don't do this in the linker script
191 because we don't want to define the symbol if we are not creating
192 a global offset table. */
193 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
194 "_GLOBAL_OFFSET_TABLE_");
195 elf_hash_table (info
)->hgot
= h
;
203 /* Create a strtab to hold the dynamic symbol names. */
205 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
207 struct elf_link_hash_table
*hash_table
;
209 hash_table
= elf_hash_table (info
);
210 if (hash_table
->dynobj
== NULL
)
212 /* We may not set dynobj, an input file holding linker created
213 dynamic sections to abfd, which may be a dynamic object with
214 its own dynamic sections. We need to find a normal input file
215 to hold linker created sections if possible. */
216 if ((abfd
->flags
& (DYNAMIC
| BFD_PLUGIN
)) != 0)
220 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link
.next
)
222 & (DYNAMIC
| BFD_LINKER_CREATED
| BFD_PLUGIN
)) == 0
223 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
224 && elf_object_id (ibfd
) == elf_hash_table_id (hash_table
)
225 && !((s
= ibfd
->sections
) != NULL
226 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
))
232 hash_table
->dynobj
= abfd
;
235 if (hash_table
->dynstr
== NULL
)
237 hash_table
->dynstr
= _bfd_elf_strtab_init ();
238 if (hash_table
->dynstr
== NULL
)
244 /* Create some sections which will be filled in with dynamic linking
245 information. ABFD is an input file which requires dynamic sections
246 to be created. The dynamic sections take up virtual memory space
247 when the final executable is run, so we need to create them before
248 addresses are assigned to the output sections. We work out the
249 actual contents and size of these sections later. */
252 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
256 const struct elf_backend_data
*bed
;
257 struct elf_link_hash_entry
*h
;
259 if (! is_elf_hash_table (info
->hash
))
262 if (elf_hash_table (info
)->dynamic_sections_created
)
265 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
268 abfd
= elf_hash_table (info
)->dynobj
;
269 bed
= get_elf_backend_data (abfd
);
271 flags
= bed
->dynamic_sec_flags
;
273 /* A dynamically linked executable has a .interp section, but a
274 shared library does not. */
275 if (bfd_link_executable (info
) && !info
->nointerp
)
277 s
= bfd_make_section_anyway_with_flags (abfd
, ".interp",
278 flags
| SEC_READONLY
);
283 /* Create sections to hold version informations. These are removed
284 if they are not needed. */
285 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_d",
286 flags
| SEC_READONLY
);
288 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
291 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version",
292 flags
| SEC_READONLY
);
294 || !bfd_set_section_alignment (s
, 1))
297 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_r",
298 flags
| SEC_READONLY
);
300 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
303 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynsym",
304 flags
| SEC_READONLY
);
306 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
308 elf_hash_table (info
)->dynsym
= s
;
310 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynstr",
311 flags
| SEC_READONLY
);
315 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynamic", flags
);
317 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
320 /* The special symbol _DYNAMIC is always set to the start of the
321 .dynamic section. We could set _DYNAMIC in a linker script, but we
322 only want to define it if we are, in fact, creating a .dynamic
323 section. We don't want to define it if there is no .dynamic
324 section, since on some ELF platforms the start up code examines it
325 to decide how to initialize the process. */
326 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC");
327 elf_hash_table (info
)->hdynamic
= h
;
333 s
= bfd_make_section_anyway_with_flags (abfd
, ".hash",
334 flags
| SEC_READONLY
);
336 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
338 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
341 if (info
->emit_gnu_hash
&& bed
->record_xhash_symbol
== NULL
)
343 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.hash",
344 flags
| SEC_READONLY
);
346 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
348 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
349 4 32-bit words followed by variable count of 64-bit words, then
350 variable count of 32-bit words. */
351 if (bed
->s
->arch_size
== 64)
352 elf_section_data (s
)->this_hdr
.sh_entsize
= 0;
354 elf_section_data (s
)->this_hdr
.sh_entsize
= 4;
357 /* Let the backend create the rest of the sections. This lets the
358 backend set the right flags. The backend will normally create
359 the .got and .plt sections. */
360 if (bed
->elf_backend_create_dynamic_sections
== NULL
361 || ! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
364 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
369 /* Create dynamic sections when linking against a dynamic object. */
372 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
374 flagword flags
, pltflags
;
375 struct elf_link_hash_entry
*h
;
377 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
378 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
380 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
381 .rel[a].bss sections. */
382 flags
= bed
->dynamic_sec_flags
;
385 if (bed
->plt_not_loaded
)
386 /* We do not clear SEC_ALLOC here because we still want the OS to
387 allocate space for the section; it's just that there's nothing
388 to read in from the object file. */
389 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
391 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
392 if (bed
->plt_readonly
)
393 pltflags
|= SEC_READONLY
;
395 s
= bfd_make_section_anyway_with_flags (abfd
, ".plt", pltflags
);
397 || !bfd_set_section_alignment (s
, bed
->plt_alignment
))
401 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
403 if (bed
->want_plt_sym
)
405 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
406 "_PROCEDURE_LINKAGE_TABLE_");
407 elf_hash_table (info
)->hplt
= h
;
412 s
= bfd_make_section_anyway_with_flags (abfd
,
413 (bed
->rela_plts_and_copies_p
414 ? ".rela.plt" : ".rel.plt"),
415 flags
| SEC_READONLY
);
417 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
421 if (! _bfd_elf_create_got_section (abfd
, info
))
424 if (bed
->want_dynbss
)
426 /* The .dynbss section is a place to put symbols which are defined
427 by dynamic objects, are referenced by regular objects, and are
428 not functions. We must allocate space for them in the process
429 image and use a R_*_COPY reloc to tell the dynamic linker to
430 initialize them at run time. The linker script puts the .dynbss
431 section into the .bss section of the final image. */
432 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynbss",
433 SEC_ALLOC
| SEC_LINKER_CREATED
);
438 if (bed
->want_dynrelro
)
440 /* Similarly, but for symbols that were originally in read-only
441 sections. This section doesn't really need to have contents,
442 but make it like other .data.rel.ro sections. */
443 s
= bfd_make_section_anyway_with_flags (abfd
, ".data.rel.ro",
450 /* The .rel[a].bss section holds copy relocs. This section is not
451 normally needed. We need to create it here, though, so that the
452 linker will map it to an output section. We can't just create it
453 only if we need it, because we will not know whether we need it
454 until we have seen all the input files, and the first time the
455 main linker code calls BFD after examining all the input files
456 (size_dynamic_sections) the input sections have already been
457 mapped to the output sections. If the section turns out not to
458 be needed, we can discard it later. We will never need this
459 section when generating a shared object, since they do not use
461 if (bfd_link_executable (info
))
463 s
= bfd_make_section_anyway_with_flags (abfd
,
464 (bed
->rela_plts_and_copies_p
465 ? ".rela.bss" : ".rel.bss"),
466 flags
| SEC_READONLY
);
468 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
472 if (bed
->want_dynrelro
)
474 s
= (bfd_make_section_anyway_with_flags
475 (abfd
, (bed
->rela_plts_and_copies_p
476 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
477 flags
| SEC_READONLY
));
479 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
481 htab
->sreldynrelro
= s
;
489 /* Record a new dynamic symbol. We record the dynamic symbols as we
490 read the input files, since we need to have a list of all of them
491 before we can determine the final sizes of the output sections.
492 Note that we may actually call this function even though we are not
493 going to output any dynamic symbols; in some cases we know that a
494 symbol should be in the dynamic symbol table, but only if there is
498 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
499 struct elf_link_hash_entry
*h
)
501 if (h
->dynindx
== -1)
503 struct elf_strtab_hash
*dynstr
;
508 if (h
->root
.type
== bfd_link_hash_defined
509 || h
->root
.type
== bfd_link_hash_defweak
)
511 /* An IR symbol should not be made dynamic. */
512 if (h
->root
.u
.def
.section
!= NULL
513 && h
->root
.u
.def
.section
->owner
!= NULL
514 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
518 /* XXX: The ABI draft says the linker must turn hidden and
519 internal symbols into STB_LOCAL symbols when producing the
520 DSO. However, if ld.so honors st_other in the dynamic table,
521 this would not be necessary. */
522 switch (ELF_ST_VISIBILITY (h
->other
))
526 if (h
->root
.type
!= bfd_link_hash_undefined
527 && h
->root
.type
!= bfd_link_hash_undefweak
)
530 if (!elf_hash_table (info
)->is_relocatable_executable
)
538 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
539 ++elf_hash_table (info
)->dynsymcount
;
541 dynstr
= elf_hash_table (info
)->dynstr
;
544 /* Create a strtab to hold the dynamic symbol names. */
545 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
550 /* We don't put any version information in the dynamic string
552 name
= h
->root
.root
.string
;
553 p
= strchr (name
, ELF_VER_CHR
);
555 /* We know that the p points into writable memory. In fact,
556 there are only a few symbols that have read-only names, being
557 those like _GLOBAL_OFFSET_TABLE_ that are created specially
558 by the backends. Most symbols will have names pointing into
559 an ELF string table read from a file, or to objalloc memory. */
562 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
567 if (indx
== (size_t) -1)
569 h
->dynstr_index
= indx
;
575 /* Mark a symbol dynamic. */
578 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
579 struct elf_link_hash_entry
*h
,
580 Elf_Internal_Sym
*sym
)
582 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
584 /* It may be called more than once on the same H. */
585 if(h
->dynamic
|| bfd_link_relocatable (info
))
588 if ((info
->dynamic_data
589 && (h
->type
== STT_OBJECT
590 || h
->type
== STT_COMMON
592 && (ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
593 || ELF_ST_TYPE (sym
->st_info
) == STT_COMMON
))))
596 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
599 /* NB: If a symbol is made dynamic by --dynamic-list, it has
601 h
->root
.non_ir_ref_dynamic
= 1;
605 /* Record an assignment to a symbol made by a linker script. We need
606 this in case some dynamic object refers to this symbol. */
609 bfd_elf_record_link_assignment (bfd
*output_bfd
,
610 struct bfd_link_info
*info
,
615 struct elf_link_hash_entry
*h
, *hv
;
616 struct elf_link_hash_table
*htab
;
617 const struct elf_backend_data
*bed
;
619 if (!is_elf_hash_table (info
->hash
))
622 htab
= elf_hash_table (info
);
623 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
627 if (h
->root
.type
== bfd_link_hash_warning
)
628 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
630 if (h
->versioned
== unknown
)
632 /* Set versioned if symbol version is unknown. */
633 char *version
= strrchr (name
, ELF_VER_CHR
);
636 if (version
> name
&& version
[-1] != ELF_VER_CHR
)
637 h
->versioned
= versioned_hidden
;
639 h
->versioned
= versioned
;
643 /* Symbols defined in a linker script but not referenced anywhere
644 else will have non_elf set. */
647 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
651 switch (h
->root
.type
)
653 case bfd_link_hash_defined
:
654 case bfd_link_hash_defweak
:
655 case bfd_link_hash_common
:
657 case bfd_link_hash_undefweak
:
658 case bfd_link_hash_undefined
:
659 /* Since we're defining the symbol, don't let it seem to have not
660 been defined. record_dynamic_symbol and size_dynamic_sections
661 may depend on this. */
662 h
->root
.type
= bfd_link_hash_new
;
663 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
664 bfd_link_repair_undef_list (&htab
->root
);
666 case bfd_link_hash_new
:
668 case bfd_link_hash_indirect
:
669 /* We had a versioned symbol in a dynamic library. We make the
670 the versioned symbol point to this one. */
671 bed
= get_elf_backend_data (output_bfd
);
673 while (hv
->root
.type
== bfd_link_hash_indirect
674 || hv
->root
.type
== bfd_link_hash_warning
)
675 hv
= (struct elf_link_hash_entry
*) hv
->root
.u
.i
.link
;
676 /* We don't need to update h->root.u since linker will set them
678 h
->root
.type
= bfd_link_hash_undefined
;
679 hv
->root
.type
= bfd_link_hash_indirect
;
680 hv
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
681 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hv
);
688 /* If this symbol is being provided by the linker script, and it is
689 currently defined by a dynamic object, but not by a regular
690 object, then mark it as undefined so that the generic linker will
691 force the correct value. */
695 h
->root
.type
= bfd_link_hash_undefined
;
697 /* If this symbol is currently defined by a dynamic object, but not
698 by a regular object, then clear out any version information because
699 the symbol will not be associated with the dynamic object any
701 if (h
->def_dynamic
&& !h
->def_regular
)
702 h
->verinfo
.verdef
= NULL
;
704 /* Make sure this symbol is not garbage collected. */
711 bed
= get_elf_backend_data (output_bfd
);
712 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
713 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
714 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
717 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
719 if (!bfd_link_relocatable (info
)
721 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
722 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
727 || bfd_link_dll (info
)
728 || elf_hash_table (info
)->is_relocatable_executable
)
732 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
735 /* If this is a weak defined symbol, and we know a corresponding
736 real symbol from the same dynamic object, make sure the real
737 symbol is also made into a dynamic symbol. */
740 struct elf_link_hash_entry
*def
= weakdef (h
);
742 if (def
->dynindx
== -1
743 && !bfd_elf_link_record_dynamic_symbol (info
, def
))
751 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
752 success, and 2 on a failure caused by attempting to record a symbol
753 in a discarded section, eg. a discarded link-once section symbol. */
756 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
761 struct elf_link_local_dynamic_entry
*entry
;
762 struct elf_link_hash_table
*eht
;
763 struct elf_strtab_hash
*dynstr
;
766 Elf_External_Sym_Shndx eshndx
;
767 char esym
[sizeof (Elf64_External_Sym
)];
769 if (! is_elf_hash_table (info
->hash
))
772 /* See if the entry exists already. */
773 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
774 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
777 amt
= sizeof (*entry
);
778 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
782 /* Go find the symbol, so that we can find it's name. */
783 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
784 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
786 bfd_release (input_bfd
, entry
);
790 if (entry
->isym
.st_shndx
!= SHN_UNDEF
791 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
795 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
796 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
798 /* We can still bfd_release here as nothing has done another
799 bfd_alloc. We can't do this later in this function. */
800 bfd_release (input_bfd
, entry
);
805 name
= (bfd_elf_string_from_elf_section
806 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
807 entry
->isym
.st_name
));
809 dynstr
= elf_hash_table (info
)->dynstr
;
812 /* Create a strtab to hold the dynamic symbol names. */
813 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
818 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
819 if (dynstr_index
== (size_t) -1)
821 entry
->isym
.st_name
= dynstr_index
;
823 eht
= elf_hash_table (info
);
825 entry
->next
= eht
->dynlocal
;
826 eht
->dynlocal
= entry
;
827 entry
->input_bfd
= input_bfd
;
828 entry
->input_indx
= input_indx
;
831 /* Whatever binding the symbol had before, it's now local. */
833 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
835 /* The dynindx will be set at the end of size_dynamic_sections. */
840 /* Return the dynindex of a local dynamic symbol. */
843 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
847 struct elf_link_local_dynamic_entry
*e
;
849 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
850 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
855 /* This function is used to renumber the dynamic symbols, if some of
856 them are removed because they are marked as local. This is called
857 via elf_link_hash_traverse. */
860 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
863 size_t *count
= (size_t *) data
;
868 if (h
->dynindx
!= -1)
869 h
->dynindx
= ++(*count
);
875 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
876 STB_LOCAL binding. */
879 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
882 size_t *count
= (size_t *) data
;
884 if (!h
->forced_local
)
887 if (h
->dynindx
!= -1)
888 h
->dynindx
= ++(*count
);
893 /* Return true if the dynamic symbol for a given section should be
894 omitted when creating a shared library. */
896 _bfd_elf_omit_section_dynsym_default (bfd
*output_bfd ATTRIBUTE_UNUSED
,
897 struct bfd_link_info
*info
,
900 struct elf_link_hash_table
*htab
;
903 switch (elf_section_data (p
)->this_hdr
.sh_type
)
907 /* If sh_type is yet undecided, assume it could be
908 SHT_PROGBITS/SHT_NOBITS. */
910 htab
= elf_hash_table (info
);
911 if (htab
->text_index_section
!= NULL
)
912 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
914 return (htab
->dynobj
!= NULL
915 && (ip
= bfd_get_linker_section (htab
->dynobj
, p
->name
)) != NULL
916 && ip
->output_section
== p
);
918 /* There shouldn't be section relative relocations
919 against any other section. */
926 _bfd_elf_omit_section_dynsym_all
927 (bfd
*output_bfd ATTRIBUTE_UNUSED
,
928 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
929 asection
*p ATTRIBUTE_UNUSED
)
934 /* Assign dynsym indices. In a shared library we generate a section
935 symbol for each output section, which come first. Next come symbols
936 which have been forced to local binding. Then all of the back-end
937 allocated local dynamic syms, followed by the rest of the global
938 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
939 (This prevents the early call before elf_backend_init_index_section
940 and strip_excluded_output_sections setting dynindx for sections
941 that are stripped.) */
944 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
945 struct bfd_link_info
*info
,
946 unsigned long *section_sym_count
)
948 unsigned long dynsymcount
= 0;
949 bfd_boolean do_sec
= section_sym_count
!= NULL
;
951 if (bfd_link_pic (info
)
952 || elf_hash_table (info
)->is_relocatable_executable
)
954 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
956 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
957 if ((p
->flags
& SEC_EXCLUDE
) == 0
958 && (p
->flags
& SEC_ALLOC
) != 0
959 && elf_hash_table (info
)->dynamic_relocs
960 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
964 elf_section_data (p
)->dynindx
= dynsymcount
;
967 elf_section_data (p
)->dynindx
= 0;
970 *section_sym_count
= dynsymcount
;
972 elf_link_hash_traverse (elf_hash_table (info
),
973 elf_link_renumber_local_hash_table_dynsyms
,
976 if (elf_hash_table (info
)->dynlocal
)
978 struct elf_link_local_dynamic_entry
*p
;
979 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
980 p
->dynindx
= ++dynsymcount
;
982 elf_hash_table (info
)->local_dynsymcount
= dynsymcount
;
984 elf_link_hash_traverse (elf_hash_table (info
),
985 elf_link_renumber_hash_table_dynsyms
,
988 /* There is an unused NULL entry at the head of the table which we
989 must account for in our count even if the table is empty since it
990 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
994 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
998 /* Merge st_other field. */
1001 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
1002 const Elf_Internal_Sym
*isym
, asection
*sec
,
1003 bfd_boolean definition
, bfd_boolean dynamic
)
1005 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1007 /* If st_other has a processor-specific meaning, specific
1008 code might be needed here. */
1009 if (bed
->elf_backend_merge_symbol_attribute
)
1010 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
1015 unsigned symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
1016 unsigned hvis
= ELF_ST_VISIBILITY (h
->other
);
1018 /* Keep the most constraining visibility. Leave the remainder
1019 of the st_other field to elf_backend_merge_symbol_attribute. */
1020 if (symvis
- 1 < hvis
- 1)
1021 h
->other
= symvis
| (h
->other
& ~ELF_ST_VISIBILITY (-1));
1024 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_DEFAULT
1025 && (sec
->flags
& SEC_READONLY
) == 0)
1026 h
->protected_def
= 1;
1029 /* This function is called when we want to merge a new symbol with an
1030 existing symbol. It handles the various cases which arise when we
1031 find a definition in a dynamic object, or when there is already a
1032 definition in a dynamic object. The new symbol is described by
1033 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1034 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1035 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1036 of an old common symbol. We set OVERRIDE if the old symbol is
1037 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1038 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1039 to change. By OK to change, we mean that we shouldn't warn if the
1040 type or size does change. */
1043 _bfd_elf_merge_symbol (bfd
*abfd
,
1044 struct bfd_link_info
*info
,
1046 Elf_Internal_Sym
*sym
,
1049 struct elf_link_hash_entry
**sym_hash
,
1051 bfd_boolean
*pold_weak
,
1052 unsigned int *pold_alignment
,
1054 bfd_boolean
*override
,
1055 bfd_boolean
*type_change_ok
,
1056 bfd_boolean
*size_change_ok
,
1057 bfd_boolean
*matched
)
1059 asection
*sec
, *oldsec
;
1060 struct elf_link_hash_entry
*h
;
1061 struct elf_link_hash_entry
*hi
;
1062 struct elf_link_hash_entry
*flip
;
1065 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
1066 bfd_boolean newweak
, oldweak
, newfunc
, oldfunc
;
1067 const struct elf_backend_data
*bed
;
1069 bfd_boolean default_sym
= *matched
;
1075 bind
= ELF_ST_BIND (sym
->st_info
);
1077 if (! bfd_is_und_section (sec
))
1078 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
1080 h
= ((struct elf_link_hash_entry
*)
1081 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
1086 bed
= get_elf_backend_data (abfd
);
1088 /* NEW_VERSION is the symbol version of the new symbol. */
1089 if (h
->versioned
!= unversioned
)
1091 /* Symbol version is unknown or versioned. */
1092 new_version
= strrchr (name
, ELF_VER_CHR
);
1095 if (h
->versioned
== unknown
)
1097 if (new_version
> name
&& new_version
[-1] != ELF_VER_CHR
)
1098 h
->versioned
= versioned_hidden
;
1100 h
->versioned
= versioned
;
1103 if (new_version
[0] == '\0')
1107 h
->versioned
= unversioned
;
1112 /* For merging, we only care about real symbols. But we need to make
1113 sure that indirect symbol dynamic flags are updated. */
1115 while (h
->root
.type
== bfd_link_hash_indirect
1116 || h
->root
.type
== bfd_link_hash_warning
)
1117 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1121 if (hi
== h
|| h
->root
.type
== bfd_link_hash_new
)
1125 /* OLD_HIDDEN is true if the existing symbol is only visible
1126 to the symbol with the same symbol version. NEW_HIDDEN is
1127 true if the new symbol is only visible to the symbol with
1128 the same symbol version. */
1129 bfd_boolean old_hidden
= h
->versioned
== versioned_hidden
;
1130 bfd_boolean new_hidden
= hi
->versioned
== versioned_hidden
;
1131 if (!old_hidden
&& !new_hidden
)
1132 /* The new symbol matches the existing symbol if both
1137 /* OLD_VERSION is the symbol version of the existing
1141 if (h
->versioned
>= versioned
)
1142 old_version
= strrchr (h
->root
.root
.string
,
1147 /* The new symbol matches the existing symbol if they
1148 have the same symbol version. */
1149 *matched
= (old_version
== new_version
1150 || (old_version
!= NULL
1151 && new_version
!= NULL
1152 && strcmp (old_version
, new_version
) == 0));
1157 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1162 switch (h
->root
.type
)
1167 case bfd_link_hash_undefined
:
1168 case bfd_link_hash_undefweak
:
1169 oldbfd
= h
->root
.u
.undef
.abfd
;
1172 case bfd_link_hash_defined
:
1173 case bfd_link_hash_defweak
:
1174 oldbfd
= h
->root
.u
.def
.section
->owner
;
1175 oldsec
= h
->root
.u
.def
.section
;
1178 case bfd_link_hash_common
:
1179 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1180 oldsec
= h
->root
.u
.c
.p
->section
;
1182 *pold_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1185 if (poldbfd
&& *poldbfd
== NULL
)
1188 /* Differentiate strong and weak symbols. */
1189 newweak
= bind
== STB_WEAK
;
1190 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1191 || h
->root
.type
== bfd_link_hash_undefweak
);
1193 *pold_weak
= oldweak
;
1195 /* We have to check it for every instance since the first few may be
1196 references and not all compilers emit symbol type for undefined
1198 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1200 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1201 respectively, is from a dynamic object. */
1203 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1205 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1206 syms and defined syms in dynamic libraries respectively.
1207 ref_dynamic on the other hand can be set for a symbol defined in
1208 a dynamic library, and def_dynamic may not be set; When the
1209 definition in a dynamic lib is overridden by a definition in the
1210 executable use of the symbol in the dynamic lib becomes a
1211 reference to the executable symbol. */
1214 if (bfd_is_und_section (sec
))
1216 if (bind
!= STB_WEAK
)
1218 h
->ref_dynamic_nonweak
= 1;
1219 hi
->ref_dynamic_nonweak
= 1;
1224 /* Update the existing symbol only if they match. */
1227 hi
->dynamic_def
= 1;
1231 /* If we just created the symbol, mark it as being an ELF symbol.
1232 Other than that, there is nothing to do--there is no merge issue
1233 with a newly defined symbol--so we just return. */
1235 if (h
->root
.type
== bfd_link_hash_new
)
1241 /* In cases involving weak versioned symbols, we may wind up trying
1242 to merge a symbol with itself. Catch that here, to avoid the
1243 confusion that results if we try to override a symbol with
1244 itself. The additional tests catch cases like
1245 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1246 dynamic object, which we do want to handle here. */
1248 && (newweak
|| oldweak
)
1249 && ((abfd
->flags
& DYNAMIC
) == 0
1250 || !h
->def_regular
))
1255 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1256 else if (oldsec
!= NULL
)
1258 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1259 indices used by MIPS ELF. */
1260 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1263 /* Handle a case where plugin_notice won't be called and thus won't
1264 set the non_ir_ref flags on the first pass over symbols. */
1266 && (oldbfd
->flags
& BFD_PLUGIN
) != (abfd
->flags
& BFD_PLUGIN
)
1267 && newdyn
!= olddyn
)
1269 h
->root
.non_ir_ref_dynamic
= TRUE
;
1270 hi
->root
.non_ir_ref_dynamic
= TRUE
;
1273 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1274 respectively, appear to be a definition rather than reference. */
1276 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1278 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1279 && h
->root
.type
!= bfd_link_hash_undefweak
1280 && h
->root
.type
!= bfd_link_hash_common
);
1282 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1283 respectively, appear to be a function. */
1285 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1286 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1288 oldfunc
= (h
->type
!= STT_NOTYPE
1289 && bed
->is_function_type (h
->type
));
1291 if (!(newfunc
&& oldfunc
)
1292 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1293 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1294 && h
->type
!= STT_NOTYPE
1295 && (newdef
|| bfd_is_com_section (sec
))
1296 && (olddef
|| h
->root
.type
== bfd_link_hash_common
))
1298 /* If creating a default indirect symbol ("foo" or "foo@") from
1299 a dynamic versioned definition ("foo@@") skip doing so if
1300 there is an existing regular definition with a different
1301 type. We don't want, for example, a "time" variable in the
1302 executable overriding a "time" function in a shared library. */
1310 /* When adding a symbol from a regular object file after we have
1311 created indirect symbols, undo the indirection and any
1318 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1319 h
->forced_local
= 0;
1323 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1325 h
->root
.type
= bfd_link_hash_undefined
;
1326 h
->root
.u
.undef
.abfd
= abfd
;
1330 h
->root
.type
= bfd_link_hash_new
;
1331 h
->root
.u
.undef
.abfd
= NULL
;
1337 /* Check TLS symbols. We don't check undefined symbols introduced
1338 by "ld -u" which have no type (and oldbfd NULL), and we don't
1339 check symbols from plugins because they also have no type. */
1341 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1342 && (abfd
->flags
& BFD_PLUGIN
) == 0
1343 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1344 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1347 bfd_boolean ntdef
, tdef
;
1348 asection
*ntsec
, *tsec
;
1350 if (h
->type
== STT_TLS
)
1371 /* xgettext:c-format */
1372 (_("%s: TLS definition in %pB section %pA "
1373 "mismatches non-TLS definition in %pB section %pA"),
1374 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
, ntsec
);
1375 else if (!tdef
&& !ntdef
)
1377 /* xgettext:c-format */
1378 (_("%s: TLS reference in %pB "
1379 "mismatches non-TLS reference in %pB"),
1380 h
->root
.root
.string
, tbfd
, ntbfd
);
1383 /* xgettext:c-format */
1384 (_("%s: TLS definition in %pB section %pA "
1385 "mismatches non-TLS reference in %pB"),
1386 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
);
1389 /* xgettext:c-format */
1390 (_("%s: TLS reference in %pB "
1391 "mismatches non-TLS definition in %pB section %pA"),
1392 h
->root
.root
.string
, tbfd
, ntbfd
, ntsec
);
1394 bfd_set_error (bfd_error_bad_value
);
1398 /* If the old symbol has non-default visibility, we ignore the new
1399 definition from a dynamic object. */
1401 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1402 && !bfd_is_und_section (sec
))
1405 /* Make sure this symbol is dynamic. */
1407 hi
->ref_dynamic
= 1;
1408 /* A protected symbol has external availability. Make sure it is
1409 recorded as dynamic.
1411 FIXME: Should we check type and size for protected symbol? */
1412 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1413 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1418 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1421 /* If the new symbol with non-default visibility comes from a
1422 relocatable file and the old definition comes from a dynamic
1423 object, we remove the old definition. */
1424 if (hi
->root
.type
== bfd_link_hash_indirect
)
1426 /* Handle the case where the old dynamic definition is
1427 default versioned. We need to copy the symbol info from
1428 the symbol with default version to the normal one if it
1429 was referenced before. */
1432 hi
->root
.type
= h
->root
.type
;
1433 h
->root
.type
= bfd_link_hash_indirect
;
1434 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1436 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1437 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1439 /* If the new symbol is hidden or internal, completely undo
1440 any dynamic link state. */
1441 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1442 h
->forced_local
= 0;
1449 /* FIXME: Should we check type and size for protected symbol? */
1459 /* If the old symbol was undefined before, then it will still be
1460 on the undefs list. If the new symbol is undefined or
1461 common, we can't make it bfd_link_hash_new here, because new
1462 undefined or common symbols will be added to the undefs list
1463 by _bfd_generic_link_add_one_symbol. Symbols may not be
1464 added twice to the undefs list. Also, if the new symbol is
1465 undefweak then we don't want to lose the strong undef. */
1466 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1468 h
->root
.type
= bfd_link_hash_undefined
;
1469 h
->root
.u
.undef
.abfd
= abfd
;
1473 h
->root
.type
= bfd_link_hash_new
;
1474 h
->root
.u
.undef
.abfd
= NULL
;
1477 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1479 /* If the new symbol is hidden or internal, completely undo
1480 any dynamic link state. */
1481 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1482 h
->forced_local
= 0;
1488 /* FIXME: Should we check type and size for protected symbol? */
1494 /* If a new weak symbol definition comes from a regular file and the
1495 old symbol comes from a dynamic library, we treat the new one as
1496 strong. Similarly, an old weak symbol definition from a regular
1497 file is treated as strong when the new symbol comes from a dynamic
1498 library. Further, an old weak symbol from a dynamic library is
1499 treated as strong if the new symbol is from a dynamic library.
1500 This reflects the way glibc's ld.so works.
1502 Also allow a weak symbol to override a linker script symbol
1503 defined by an early pass over the script. This is done so the
1504 linker knows the symbol is defined in an object file, for the
1505 DEFINED script function.
1507 Do this before setting *type_change_ok or *size_change_ok so that
1508 we warn properly when dynamic library symbols are overridden. */
1510 if (newdef
&& !newdyn
&& (olddyn
|| h
->root
.ldscript_def
))
1512 if (olddef
&& newdyn
)
1515 /* Allow changes between different types of function symbol. */
1516 if (newfunc
&& oldfunc
)
1517 *type_change_ok
= TRUE
;
1519 /* It's OK to change the type if either the existing symbol or the
1520 new symbol is weak. A type change is also OK if the old symbol
1521 is undefined and the new symbol is defined. */
1526 && h
->root
.type
== bfd_link_hash_undefined
))
1527 *type_change_ok
= TRUE
;
1529 /* It's OK to change the size if either the existing symbol or the
1530 new symbol is weak, or if the old symbol is undefined. */
1533 || h
->root
.type
== bfd_link_hash_undefined
)
1534 *size_change_ok
= TRUE
;
1536 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1537 symbol, respectively, appears to be a common symbol in a dynamic
1538 object. If a symbol appears in an uninitialized section, and is
1539 not weak, and is not a function, then it may be a common symbol
1540 which was resolved when the dynamic object was created. We want
1541 to treat such symbols specially, because they raise special
1542 considerations when setting the symbol size: if the symbol
1543 appears as a common symbol in a regular object, and the size in
1544 the regular object is larger, we must make sure that we use the
1545 larger size. This problematic case can always be avoided in C,
1546 but it must be handled correctly when using Fortran shared
1549 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1550 likewise for OLDDYNCOMMON and OLDDEF.
1552 Note that this test is just a heuristic, and that it is quite
1553 possible to have an uninitialized symbol in a shared object which
1554 is really a definition, rather than a common symbol. This could
1555 lead to some minor confusion when the symbol really is a common
1556 symbol in some regular object. However, I think it will be
1562 && (sec
->flags
& SEC_ALLOC
) != 0
1563 && (sec
->flags
& SEC_LOAD
) == 0
1566 newdyncommon
= TRUE
;
1568 newdyncommon
= FALSE
;
1572 && h
->root
.type
== bfd_link_hash_defined
1574 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1575 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1578 olddyncommon
= TRUE
;
1580 olddyncommon
= FALSE
;
1582 /* We now know everything about the old and new symbols. We ask the
1583 backend to check if we can merge them. */
1584 if (bed
->merge_symbol
!= NULL
)
1586 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1591 /* There are multiple definitions of a normal symbol. Skip the
1592 default symbol as well as definition from an IR object. */
1593 if (olddef
&& !olddyn
&& !oldweak
&& newdef
&& !newdyn
&& !newweak
1594 && !default_sym
&& h
->def_regular
1596 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1597 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1599 /* Handle a multiple definition. */
1600 (*info
->callbacks
->multiple_definition
) (info
, &h
->root
,
1601 abfd
, sec
, *pvalue
);
1606 /* If both the old and the new symbols look like common symbols in a
1607 dynamic object, set the size of the symbol to the larger of the
1612 && sym
->st_size
!= h
->size
)
1614 /* Since we think we have two common symbols, issue a multiple
1615 common warning if desired. Note that we only warn if the
1616 size is different. If the size is the same, we simply let
1617 the old symbol override the new one as normally happens with
1618 symbols defined in dynamic objects. */
1620 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1621 bfd_link_hash_common
, sym
->st_size
);
1622 if (sym
->st_size
> h
->size
)
1623 h
->size
= sym
->st_size
;
1625 *size_change_ok
= TRUE
;
1628 /* If we are looking at a dynamic object, and we have found a
1629 definition, we need to see if the symbol was already defined by
1630 some other object. If so, we want to use the existing
1631 definition, and we do not want to report a multiple symbol
1632 definition error; we do this by clobbering *PSEC to be
1633 bfd_und_section_ptr.
1635 We treat a common symbol as a definition if the symbol in the
1636 shared library is a function, since common symbols always
1637 represent variables; this can cause confusion in principle, but
1638 any such confusion would seem to indicate an erroneous program or
1639 shared library. We also permit a common symbol in a regular
1640 object to override a weak symbol in a shared object. */
1645 || (h
->root
.type
== bfd_link_hash_common
1646 && (newweak
|| newfunc
))))
1650 newdyncommon
= FALSE
;
1652 *psec
= sec
= bfd_und_section_ptr
;
1653 *size_change_ok
= TRUE
;
1655 /* If we get here when the old symbol is a common symbol, then
1656 we are explicitly letting it override a weak symbol or
1657 function in a dynamic object, and we don't want to warn about
1658 a type change. If the old symbol is a defined symbol, a type
1659 change warning may still be appropriate. */
1661 if (h
->root
.type
== bfd_link_hash_common
)
1662 *type_change_ok
= TRUE
;
1665 /* Handle the special case of an old common symbol merging with a
1666 new symbol which looks like a common symbol in a shared object.
1667 We change *PSEC and *PVALUE to make the new symbol look like a
1668 common symbol, and let _bfd_generic_link_add_one_symbol do the
1672 && h
->root
.type
== bfd_link_hash_common
)
1676 newdyncommon
= FALSE
;
1677 *pvalue
= sym
->st_size
;
1678 *psec
= sec
= bed
->common_section (oldsec
);
1679 *size_change_ok
= TRUE
;
1682 /* Skip weak definitions of symbols that are already defined. */
1683 if (newdef
&& olddef
&& newweak
)
1685 /* Don't skip new non-IR weak syms. */
1686 if (!(oldbfd
!= NULL
1687 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1688 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1694 /* Merge st_other. If the symbol already has a dynamic index,
1695 but visibility says it should not be visible, turn it into a
1697 elf_merge_st_other (abfd
, h
, sym
, sec
, newdef
, newdyn
);
1698 if (h
->dynindx
!= -1)
1699 switch (ELF_ST_VISIBILITY (h
->other
))
1703 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1708 /* If the old symbol is from a dynamic object, and the new symbol is
1709 a definition which is not from a dynamic object, then the new
1710 symbol overrides the old symbol. Symbols from regular files
1711 always take precedence over symbols from dynamic objects, even if
1712 they are defined after the dynamic object in the link.
1714 As above, we again permit a common symbol in a regular object to
1715 override a definition in a shared object if the shared object
1716 symbol is a function or is weak. */
1721 || (bfd_is_com_section (sec
)
1722 && (oldweak
|| oldfunc
)))
1727 /* Change the hash table entry to undefined, and let
1728 _bfd_generic_link_add_one_symbol do the right thing with the
1731 h
->root
.type
= bfd_link_hash_undefined
;
1732 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1733 *size_change_ok
= TRUE
;
1736 olddyncommon
= FALSE
;
1738 /* We again permit a type change when a common symbol may be
1739 overriding a function. */
1741 if (bfd_is_com_section (sec
))
1745 /* If a common symbol overrides a function, make sure
1746 that it isn't defined dynamically nor has type
1749 h
->type
= STT_NOTYPE
;
1751 *type_change_ok
= TRUE
;
1754 if (hi
->root
.type
== bfd_link_hash_indirect
)
1757 /* This union may have been set to be non-NULL when this symbol
1758 was seen in a dynamic object. We must force the union to be
1759 NULL, so that it is correct for a regular symbol. */
1760 h
->verinfo
.vertree
= NULL
;
1763 /* Handle the special case of a new common symbol merging with an
1764 old symbol that looks like it might be a common symbol defined in
1765 a shared object. Note that we have already handled the case in
1766 which a new common symbol should simply override the definition
1767 in the shared library. */
1770 && bfd_is_com_section (sec
)
1773 /* It would be best if we could set the hash table entry to a
1774 common symbol, but we don't know what to use for the section
1775 or the alignment. */
1776 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1777 bfd_link_hash_common
, sym
->st_size
);
1779 /* If the presumed common symbol in the dynamic object is
1780 larger, pretend that the new symbol has its size. */
1782 if (h
->size
> *pvalue
)
1785 /* We need to remember the alignment required by the symbol
1786 in the dynamic object. */
1787 BFD_ASSERT (pold_alignment
);
1788 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1791 olddyncommon
= FALSE
;
1793 h
->root
.type
= bfd_link_hash_undefined
;
1794 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1796 *size_change_ok
= TRUE
;
1797 *type_change_ok
= TRUE
;
1799 if (hi
->root
.type
== bfd_link_hash_indirect
)
1802 h
->verinfo
.vertree
= NULL
;
1807 /* Handle the case where we had a versioned symbol in a dynamic
1808 library and now find a definition in a normal object. In this
1809 case, we make the versioned symbol point to the normal one. */
1810 flip
->root
.type
= h
->root
.type
;
1811 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1812 h
->root
.type
= bfd_link_hash_indirect
;
1813 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1814 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1818 flip
->ref_dynamic
= 1;
1825 /* This function is called to create an indirect symbol from the
1826 default for the symbol with the default version if needed. The
1827 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1828 set DYNSYM if the new indirect symbol is dynamic. */
1831 _bfd_elf_add_default_symbol (bfd
*abfd
,
1832 struct bfd_link_info
*info
,
1833 struct elf_link_hash_entry
*h
,
1835 Elf_Internal_Sym
*sym
,
1839 bfd_boolean
*dynsym
)
1841 bfd_boolean type_change_ok
;
1842 bfd_boolean size_change_ok
;
1845 struct elf_link_hash_entry
*hi
;
1846 struct bfd_link_hash_entry
*bh
;
1847 const struct elf_backend_data
*bed
;
1848 bfd_boolean collect
;
1849 bfd_boolean dynamic
;
1850 bfd_boolean override
;
1852 size_t len
, shortlen
;
1854 bfd_boolean matched
;
1856 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1859 /* If this symbol has a version, and it is the default version, we
1860 create an indirect symbol from the default name to the fully
1861 decorated name. This will cause external references which do not
1862 specify a version to be bound to this version of the symbol. */
1863 p
= strchr (name
, ELF_VER_CHR
);
1864 if (h
->versioned
== unknown
)
1868 h
->versioned
= unversioned
;
1873 if (p
[1] != ELF_VER_CHR
)
1875 h
->versioned
= versioned_hidden
;
1879 h
->versioned
= versioned
;
1884 /* PR ld/19073: We may see an unversioned definition after the
1890 bed
= get_elf_backend_data (abfd
);
1891 collect
= bed
->collect
;
1892 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1894 shortlen
= p
- name
;
1895 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1896 if (shortname
== NULL
)
1898 memcpy (shortname
, name
, shortlen
);
1899 shortname
[shortlen
] = '\0';
1901 /* We are going to create a new symbol. Merge it with any existing
1902 symbol with this name. For the purposes of the merge, act as
1903 though we were defining the symbol we just defined, although we
1904 actually going to define an indirect symbol. */
1905 type_change_ok
= FALSE
;
1906 size_change_ok
= FALSE
;
1909 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1910 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1911 &type_change_ok
, &size_change_ok
, &matched
))
1917 if (hi
->def_regular
|| ELF_COMMON_DEF_P (hi
))
1919 /* If the undecorated symbol will have a version added by a
1920 script different to H, then don't indirect to/from the
1921 undecorated symbol. This isn't ideal because we may not yet
1922 have seen symbol versions, if given by a script on the
1923 command line rather than via --version-script. */
1924 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
1929 = bfd_find_version_for_sym (info
->version_info
,
1930 hi
->root
.root
.string
, &hide
);
1931 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
1933 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
1937 if (hi
->verinfo
.vertree
!= NULL
1938 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
1944 /* Add the default symbol if not performing a relocatable link. */
1945 if (! bfd_link_relocatable (info
))
1948 if (bh
->type
== bfd_link_hash_defined
1949 && bh
->u
.def
.section
->owner
!= NULL
1950 && (bh
->u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
1952 /* Mark the previous definition from IR object as
1953 undefined so that the generic linker will override
1955 bh
->type
= bfd_link_hash_undefined
;
1956 bh
->u
.undef
.abfd
= bh
->u
.def
.section
->owner
;
1958 if (! (_bfd_generic_link_add_one_symbol
1959 (info
, abfd
, shortname
, BSF_INDIRECT
,
1960 bfd_ind_section_ptr
,
1961 0, name
, FALSE
, collect
, &bh
)))
1963 hi
= (struct elf_link_hash_entry
*) bh
;
1968 /* In this case the symbol named SHORTNAME is overriding the
1969 indirect symbol we want to add. We were planning on making
1970 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1971 is the name without a version. NAME is the fully versioned
1972 name, and it is the default version.
1974 Overriding means that we already saw a definition for the
1975 symbol SHORTNAME in a regular object, and it is overriding
1976 the symbol defined in the dynamic object.
1978 When this happens, we actually want to change NAME, the
1979 symbol we just added, to refer to SHORTNAME. This will cause
1980 references to NAME in the shared object to become references
1981 to SHORTNAME in the regular object. This is what we expect
1982 when we override a function in a shared object: that the
1983 references in the shared object will be mapped to the
1984 definition in the regular object. */
1986 while (hi
->root
.type
== bfd_link_hash_indirect
1987 || hi
->root
.type
== bfd_link_hash_warning
)
1988 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1990 h
->root
.type
= bfd_link_hash_indirect
;
1991 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1995 hi
->ref_dynamic
= 1;
1999 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
2004 /* Now set HI to H, so that the following code will set the
2005 other fields correctly. */
2009 /* Check if HI is a warning symbol. */
2010 if (hi
->root
.type
== bfd_link_hash_warning
)
2011 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2013 /* If there is a duplicate definition somewhere, then HI may not
2014 point to an indirect symbol. We will have reported an error to
2015 the user in that case. */
2017 if (hi
->root
.type
== bfd_link_hash_indirect
)
2019 struct elf_link_hash_entry
*ht
;
2021 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2022 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
2024 /* A reference to the SHORTNAME symbol from a dynamic library
2025 will be satisfied by the versioned symbol at runtime. In
2026 effect, we have a reference to the versioned symbol. */
2027 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2028 hi
->dynamic_def
|= ht
->dynamic_def
;
2030 /* See if the new flags lead us to realize that the symbol must
2036 if (! bfd_link_executable (info
)
2043 if (hi
->ref_regular
)
2049 /* We also need to define an indirection from the nondefault version
2053 len
= strlen (name
);
2054 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
2055 if (shortname
== NULL
)
2057 memcpy (shortname
, name
, shortlen
);
2058 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
2060 /* Once again, merge with any existing symbol. */
2061 type_change_ok
= FALSE
;
2062 size_change_ok
= FALSE
;
2064 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
2065 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
2066 &type_change_ok
, &size_change_ok
, &matched
))
2074 /* Here SHORTNAME is a versioned name, so we don't expect to see
2075 the type of override we do in the case above unless it is
2076 overridden by a versioned definition. */
2077 if (hi
->root
.type
!= bfd_link_hash_defined
2078 && hi
->root
.type
!= bfd_link_hash_defweak
)
2080 /* xgettext:c-format */
2081 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2087 if (! (_bfd_generic_link_add_one_symbol
2088 (info
, abfd
, shortname
, BSF_INDIRECT
,
2089 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
2091 hi
= (struct elf_link_hash_entry
*) bh
;
2093 /* If there is a duplicate definition somewhere, then HI may not
2094 point to an indirect symbol. We will have reported an error
2095 to the user in that case. */
2097 if (hi
->root
.type
== bfd_link_hash_indirect
)
2099 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
2100 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2101 hi
->dynamic_def
|= h
->dynamic_def
;
2103 /* See if the new flags lead us to realize that the symbol
2109 if (! bfd_link_executable (info
)
2115 if (hi
->ref_regular
)
2125 /* This routine is used to export all defined symbols into the dynamic
2126 symbol table. It is called via elf_link_hash_traverse. */
2129 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
2131 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2133 /* Ignore indirect symbols. These are added by the versioning code. */
2134 if (h
->root
.type
== bfd_link_hash_indirect
)
2137 /* Ignore this if we won't export it. */
2138 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2141 if (h
->dynindx
== -1
2142 && (h
->def_regular
|| h
->ref_regular
)
2143 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2144 h
->root
.root
.string
))
2146 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2156 /* Look through the symbols which are defined in other shared
2157 libraries and referenced here. Update the list of version
2158 dependencies. This will be put into the .gnu.version_r section.
2159 This function is called via elf_link_hash_traverse. */
2162 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2165 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2166 Elf_Internal_Verneed
*t
;
2167 Elf_Internal_Vernaux
*a
;
2170 /* We only care about symbols defined in shared objects with version
2175 || h
->verinfo
.verdef
== NULL
2176 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2177 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2180 /* See if we already know about this version. */
2181 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2185 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2188 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2189 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2195 /* This is a new version. Add it to tree we are building. */
2200 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2203 rinfo
->failed
= TRUE
;
2207 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2208 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2209 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2213 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2216 rinfo
->failed
= TRUE
;
2220 /* Note that we are copying a string pointer here, and testing it
2221 above. If bfd_elf_string_from_elf_section is ever changed to
2222 discard the string data when low in memory, this will have to be
2224 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2226 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2227 a
->vna_nextptr
= t
->vn_auxptr
;
2229 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2232 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2239 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2240 hidden. Set *T_P to NULL if there is no match. */
2243 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info
*info
,
2244 struct elf_link_hash_entry
*h
,
2245 const char *version_p
,
2246 struct bfd_elf_version_tree
**t_p
,
2249 struct bfd_elf_version_tree
*t
;
2251 /* Look for the version. If we find it, it is no longer weak. */
2252 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
2254 if (strcmp (t
->name
, version_p
) == 0)
2258 struct bfd_elf_version_expr
*d
;
2260 len
= version_p
- h
->root
.root
.string
;
2261 alc
= (char *) bfd_malloc (len
);
2264 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2265 alc
[len
- 1] = '\0';
2266 if (alc
[len
- 2] == ELF_VER_CHR
)
2267 alc
[len
- 2] = '\0';
2269 h
->verinfo
.vertree
= t
;
2273 if (t
->globals
.list
!= NULL
)
2274 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2276 /* See if there is anything to force this symbol to
2278 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2280 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2283 && ! info
->export_dynamic
)
2297 /* Return TRUE if the symbol H is hidden by version script. */
2300 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info
*info
,
2301 struct elf_link_hash_entry
*h
)
2304 bfd_boolean hide
= FALSE
;
2305 const struct elf_backend_data
*bed
2306 = get_elf_backend_data (info
->output_bfd
);
2308 /* Version script only hides symbols defined in regular objects. */
2309 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2312 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2313 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2315 struct bfd_elf_version_tree
*t
;
2318 if (*p
== ELF_VER_CHR
)
2322 && _bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
)
2326 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2331 /* If we don't have a version for this symbol, see if we can find
2333 if (h
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
2336 = bfd_find_version_for_sym (info
->version_info
,
2337 h
->root
.root
.string
, &hide
);
2338 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2340 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2348 /* Figure out appropriate versions for all the symbols. We may not
2349 have the version number script until we have read all of the input
2350 files, so until that point we don't know which symbols should be
2351 local. This function is called via elf_link_hash_traverse. */
2354 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2356 struct elf_info_failed
*sinfo
;
2357 struct bfd_link_info
*info
;
2358 const struct elf_backend_data
*bed
;
2359 struct elf_info_failed eif
;
2363 sinfo
= (struct elf_info_failed
*) data
;
2366 /* Fix the symbol flags. */
2369 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2372 sinfo
->failed
= TRUE
;
2376 bed
= get_elf_backend_data (info
->output_bfd
);
2378 /* We only need version numbers for symbols defined in regular
2380 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2382 /* Hide symbols defined in discarded input sections. */
2383 if ((h
->root
.type
== bfd_link_hash_defined
2384 || h
->root
.type
== bfd_link_hash_defweak
)
2385 && discarded_section (h
->root
.u
.def
.section
))
2386 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2391 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2392 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2394 struct bfd_elf_version_tree
*t
;
2397 if (*p
== ELF_VER_CHR
)
2400 /* If there is no version string, we can just return out. */
2404 if (!_bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
))
2406 sinfo
->failed
= TRUE
;
2411 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2413 /* If we are building an application, we need to create a
2414 version node for this version. */
2415 if (t
== NULL
&& bfd_link_executable (info
))
2417 struct bfd_elf_version_tree
**pp
;
2420 /* If we aren't going to export this symbol, we don't need
2421 to worry about it. */
2422 if (h
->dynindx
== -1)
2425 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2429 sinfo
->failed
= TRUE
;
2434 t
->name_indx
= (unsigned int) -1;
2438 /* Don't count anonymous version tag. */
2439 if (sinfo
->info
->version_info
!= NULL
2440 && sinfo
->info
->version_info
->vernum
== 0)
2442 for (pp
= &sinfo
->info
->version_info
;
2446 t
->vernum
= version_index
;
2450 h
->verinfo
.vertree
= t
;
2454 /* We could not find the version for a symbol when
2455 generating a shared archive. Return an error. */
2457 /* xgettext:c-format */
2458 (_("%pB: version node not found for symbol %s"),
2459 info
->output_bfd
, h
->root
.root
.string
);
2460 bfd_set_error (bfd_error_bad_value
);
2461 sinfo
->failed
= TRUE
;
2466 /* If we don't have a version for this symbol, see if we can find
2469 && h
->verinfo
.vertree
== NULL
2470 && sinfo
->info
->version_info
!= NULL
)
2473 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2474 h
->root
.root
.string
, &hide
);
2475 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2476 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2482 /* Read and swap the relocs from the section indicated by SHDR. This
2483 may be either a REL or a RELA section. The relocations are
2484 translated into RELA relocations and stored in INTERNAL_RELOCS,
2485 which should have already been allocated to contain enough space.
2486 The EXTERNAL_RELOCS are a buffer where the external form of the
2487 relocations should be stored.
2489 Returns FALSE if something goes wrong. */
2492 elf_link_read_relocs_from_section (bfd
*abfd
,
2494 Elf_Internal_Shdr
*shdr
,
2495 void *external_relocs
,
2496 Elf_Internal_Rela
*internal_relocs
)
2498 const struct elf_backend_data
*bed
;
2499 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2500 const bfd_byte
*erela
;
2501 const bfd_byte
*erelaend
;
2502 Elf_Internal_Rela
*irela
;
2503 Elf_Internal_Shdr
*symtab_hdr
;
2506 /* Position ourselves at the start of the section. */
2507 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2510 /* Read the relocations. */
2511 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2514 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2515 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2517 bed
= get_elf_backend_data (abfd
);
2519 /* Convert the external relocations to the internal format. */
2520 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2521 swap_in
= bed
->s
->swap_reloc_in
;
2522 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2523 swap_in
= bed
->s
->swap_reloca_in
;
2526 bfd_set_error (bfd_error_wrong_format
);
2530 erela
= (const bfd_byte
*) external_relocs
;
2531 /* Setting erelaend like this and comparing with <= handles case of
2532 a fuzzed object with sh_size not a multiple of sh_entsize. */
2533 erelaend
= erela
+ shdr
->sh_size
- shdr
->sh_entsize
;
2534 irela
= internal_relocs
;
2535 while (erela
<= erelaend
)
2539 (*swap_in
) (abfd
, erela
, irela
);
2540 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2541 if (bed
->s
->arch_size
== 64)
2545 if ((size_t) r_symndx
>= nsyms
)
2548 /* xgettext:c-format */
2549 (_("%pB: bad reloc symbol index (%#" PRIx64
" >= %#lx)"
2550 " for offset %#" PRIx64
" in section `%pA'"),
2551 abfd
, (uint64_t) r_symndx
, (unsigned long) nsyms
,
2552 (uint64_t) irela
->r_offset
, sec
);
2553 bfd_set_error (bfd_error_bad_value
);
2557 else if (r_symndx
!= STN_UNDEF
)
2560 /* xgettext:c-format */
2561 (_("%pB: non-zero symbol index (%#" PRIx64
")"
2562 " for offset %#" PRIx64
" in section `%pA'"
2563 " when the object file has no symbol table"),
2564 abfd
, (uint64_t) r_symndx
,
2565 (uint64_t) irela
->r_offset
, sec
);
2566 bfd_set_error (bfd_error_bad_value
);
2569 irela
+= bed
->s
->int_rels_per_ext_rel
;
2570 erela
+= shdr
->sh_entsize
;
2576 /* Read and swap the relocs for a section O. They may have been
2577 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2578 not NULL, they are used as buffers to read into. They are known to
2579 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2580 the return value is allocated using either malloc or bfd_alloc,
2581 according to the KEEP_MEMORY argument. If O has two relocation
2582 sections (both REL and RELA relocations), then the REL_HDR
2583 relocations will appear first in INTERNAL_RELOCS, followed by the
2584 RELA_HDR relocations. */
2587 _bfd_elf_link_read_relocs (bfd
*abfd
,
2589 void *external_relocs
,
2590 Elf_Internal_Rela
*internal_relocs
,
2591 bfd_boolean keep_memory
)
2593 void *alloc1
= NULL
;
2594 Elf_Internal_Rela
*alloc2
= NULL
;
2595 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2596 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2597 Elf_Internal_Rela
*internal_rela_relocs
;
2599 if (esdo
->relocs
!= NULL
)
2600 return esdo
->relocs
;
2602 if (o
->reloc_count
== 0)
2605 if (internal_relocs
== NULL
)
2609 size
= (bfd_size_type
) o
->reloc_count
* sizeof (Elf_Internal_Rela
);
2611 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2613 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2614 if (internal_relocs
== NULL
)
2618 if (external_relocs
== NULL
)
2620 bfd_size_type size
= 0;
2623 size
+= esdo
->rel
.hdr
->sh_size
;
2625 size
+= esdo
->rela
.hdr
->sh_size
;
2627 alloc1
= bfd_malloc (size
);
2630 external_relocs
= alloc1
;
2633 internal_rela_relocs
= internal_relocs
;
2636 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2640 external_relocs
= (((bfd_byte
*) external_relocs
)
2641 + esdo
->rel
.hdr
->sh_size
);
2642 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2643 * bed
->s
->int_rels_per_ext_rel
);
2647 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2649 internal_rela_relocs
)))
2652 /* Cache the results for next time, if we can. */
2654 esdo
->relocs
= internal_relocs
;
2658 /* Don't free alloc2, since if it was allocated we are passing it
2659 back (under the name of internal_relocs). */
2661 return internal_relocs
;
2668 bfd_release (abfd
, alloc2
);
2675 /* Compute the size of, and allocate space for, REL_HDR which is the
2676 section header for a section containing relocations for O. */
2679 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2680 struct bfd_elf_section_reloc_data
*reldata
)
2682 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2684 /* That allows us to calculate the size of the section. */
2685 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2687 /* The contents field must last into write_object_contents, so we
2688 allocate it with bfd_alloc rather than malloc. Also since we
2689 cannot be sure that the contents will actually be filled in,
2690 we zero the allocated space. */
2691 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2692 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2695 if (reldata
->hashes
== NULL
&& reldata
->count
)
2697 struct elf_link_hash_entry
**p
;
2699 p
= ((struct elf_link_hash_entry
**)
2700 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2704 reldata
->hashes
= p
;
2710 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2711 originated from the section given by INPUT_REL_HDR) to the
2715 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2716 asection
*input_section
,
2717 Elf_Internal_Shdr
*input_rel_hdr
,
2718 Elf_Internal_Rela
*internal_relocs
,
2719 struct elf_link_hash_entry
**rel_hash
2722 Elf_Internal_Rela
*irela
;
2723 Elf_Internal_Rela
*irelaend
;
2725 struct bfd_elf_section_reloc_data
*output_reldata
;
2726 asection
*output_section
;
2727 const struct elf_backend_data
*bed
;
2728 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2729 struct bfd_elf_section_data
*esdo
;
2731 output_section
= input_section
->output_section
;
2733 bed
= get_elf_backend_data (output_bfd
);
2734 esdo
= elf_section_data (output_section
);
2735 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2737 output_reldata
= &esdo
->rel
;
2738 swap_out
= bed
->s
->swap_reloc_out
;
2740 else if (esdo
->rela
.hdr
2741 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2743 output_reldata
= &esdo
->rela
;
2744 swap_out
= bed
->s
->swap_reloca_out
;
2749 /* xgettext:c-format */
2750 (_("%pB: relocation size mismatch in %pB section %pA"),
2751 output_bfd
, input_section
->owner
, input_section
);
2752 bfd_set_error (bfd_error_wrong_format
);
2756 erel
= output_reldata
->hdr
->contents
;
2757 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2758 irela
= internal_relocs
;
2759 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2760 * bed
->s
->int_rels_per_ext_rel
);
2761 while (irela
< irelaend
)
2763 (*swap_out
) (output_bfd
, irela
, erel
);
2764 irela
+= bed
->s
->int_rels_per_ext_rel
;
2765 erel
+= input_rel_hdr
->sh_entsize
;
2768 /* Bump the counter, so that we know where to add the next set of
2770 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2775 /* Make weak undefined symbols in PIE dynamic. */
2778 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2779 struct elf_link_hash_entry
*h
)
2781 if (bfd_link_pie (info
)
2783 && h
->root
.type
== bfd_link_hash_undefweak
)
2784 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2789 /* Fix up the flags for a symbol. This handles various cases which
2790 can only be fixed after all the input files are seen. This is
2791 currently called by both adjust_dynamic_symbol and
2792 assign_sym_version, which is unnecessary but perhaps more robust in
2793 the face of future changes. */
2796 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2797 struct elf_info_failed
*eif
)
2799 const struct elf_backend_data
*bed
;
2801 /* If this symbol was mentioned in a non-ELF file, try to set
2802 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2803 permit a non-ELF file to correctly refer to a symbol defined in
2804 an ELF dynamic object. */
2807 while (h
->root
.type
== bfd_link_hash_indirect
)
2808 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2810 if (h
->root
.type
!= bfd_link_hash_defined
2811 && h
->root
.type
!= bfd_link_hash_defweak
)
2814 h
->ref_regular_nonweak
= 1;
2818 if (h
->root
.u
.def
.section
->owner
!= NULL
2819 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2820 == bfd_target_elf_flavour
))
2823 h
->ref_regular_nonweak
= 1;
2829 if (h
->dynindx
== -1
2833 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2842 /* Unfortunately, NON_ELF is only correct if the symbol
2843 was first seen in a non-ELF file. Fortunately, if the symbol
2844 was first seen in an ELF file, we're probably OK unless the
2845 symbol was defined in a non-ELF file. Catch that case here.
2846 FIXME: We're still in trouble if the symbol was first seen in
2847 a dynamic object, and then later in a non-ELF regular object. */
2848 if ((h
->root
.type
== bfd_link_hash_defined
2849 || h
->root
.type
== bfd_link_hash_defweak
)
2851 && (h
->root
.u
.def
.section
->owner
!= NULL
2852 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2853 != bfd_target_elf_flavour
)
2854 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2855 && !h
->def_dynamic
)))
2859 /* Backend specific symbol fixup. */
2860 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2861 if (bed
->elf_backend_fixup_symbol
2862 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2865 /* If this is a final link, and the symbol was defined as a common
2866 symbol in a regular object file, and there was no definition in
2867 any dynamic object, then the linker will have allocated space for
2868 the symbol in a common section but the DEF_REGULAR
2869 flag will not have been set. */
2870 if (h
->root
.type
== bfd_link_hash_defined
2874 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
2877 /* Symbols defined in discarded sections shouldn't be dynamic. */
2878 if (h
->root
.type
== bfd_link_hash_undefined
&& h
->indx
== -3)
2879 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2881 /* If a weak undefined symbol has non-default visibility, we also
2882 hide it from the dynamic linker. */
2883 else if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2884 && h
->root
.type
== bfd_link_hash_undefweak
)
2885 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2887 /* A hidden versioned symbol in executable should be forced local if
2888 it is is locally defined, not referenced by shared library and not
2890 else if (bfd_link_executable (eif
->info
)
2891 && h
->versioned
== versioned_hidden
2892 && !eif
->info
->export_dynamic
2896 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2898 /* If -Bsymbolic was used (which means to bind references to global
2899 symbols to the definition within the shared object), and this
2900 symbol was defined in a regular object, then it actually doesn't
2901 need a PLT entry. Likewise, if the symbol has non-default
2902 visibility. If the symbol has hidden or internal visibility, we
2903 will force it local. */
2904 else if (h
->needs_plt
2905 && bfd_link_pic (eif
->info
)
2906 && is_elf_hash_table (eif
->info
->hash
)
2907 && (SYMBOLIC_BIND (eif
->info
, h
)
2908 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2911 bfd_boolean force_local
;
2913 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2914 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2915 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2918 /* If this is a weak defined symbol in a dynamic object, and we know
2919 the real definition in the dynamic object, copy interesting flags
2920 over to the real definition. */
2921 if (h
->is_weakalias
)
2923 struct elf_link_hash_entry
*def
= weakdef (h
);
2925 /* If the real definition is defined by a regular object file,
2926 don't do anything special. See the longer description in
2927 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2928 bfd_link_hash_defined as it was when put on the alias list
2929 then it must have originally been a versioned symbol (for
2930 which a non-versioned indirect symbol is created) and later
2931 a definition for the non-versioned symbol is found. In that
2932 case the indirection is flipped with the versioned symbol
2933 becoming an indirect pointing at the non-versioned symbol.
2934 Thus, not an alias any more. */
2935 if (def
->def_regular
2936 || def
->root
.type
!= bfd_link_hash_defined
)
2939 while ((h
= h
->u
.alias
) != def
)
2940 h
->is_weakalias
= 0;
2944 while (h
->root
.type
== bfd_link_hash_indirect
)
2945 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2946 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2947 || h
->root
.type
== bfd_link_hash_defweak
);
2948 BFD_ASSERT (def
->def_dynamic
);
2949 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, def
, h
);
2956 /* Make the backend pick a good value for a dynamic symbol. This is
2957 called via elf_link_hash_traverse, and also calls itself
2961 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2963 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2964 struct elf_link_hash_table
*htab
;
2965 const struct elf_backend_data
*bed
;
2967 if (! is_elf_hash_table (eif
->info
->hash
))
2970 /* Ignore indirect symbols. These are added by the versioning code. */
2971 if (h
->root
.type
== bfd_link_hash_indirect
)
2974 /* Fix the symbol flags. */
2975 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2978 htab
= elf_hash_table (eif
->info
);
2979 bed
= get_elf_backend_data (htab
->dynobj
);
2981 if (h
->root
.type
== bfd_link_hash_undefweak
)
2983 if (eif
->info
->dynamic_undefined_weak
== 0)
2984 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2985 else if (eif
->info
->dynamic_undefined_weak
> 0
2987 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2988 && !bfd_hide_sym_by_version (eif
->info
->version_info
,
2989 h
->root
.root
.string
))
2991 if (!bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2999 /* If this symbol does not require a PLT entry, and it is not
3000 defined by a dynamic object, or is not referenced by a regular
3001 object, ignore it. We do have to handle a weak defined symbol,
3002 even if no regular object refers to it, if we decided to add it
3003 to the dynamic symbol table. FIXME: Do we normally need to worry
3004 about symbols which are defined by one dynamic object and
3005 referenced by another one? */
3007 && h
->type
!= STT_GNU_IFUNC
3011 && (!h
->is_weakalias
|| weakdef (h
)->dynindx
== -1))))
3013 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
3017 /* If we've already adjusted this symbol, don't do it again. This
3018 can happen via a recursive call. */
3019 if (h
->dynamic_adjusted
)
3022 /* Don't look at this symbol again. Note that we must set this
3023 after checking the above conditions, because we may look at a
3024 symbol once, decide not to do anything, and then get called
3025 recursively later after REF_REGULAR is set below. */
3026 h
->dynamic_adjusted
= 1;
3028 /* If this is a weak definition, and we know a real definition, and
3029 the real symbol is not itself defined by a regular object file,
3030 then get a good value for the real definition. We handle the
3031 real symbol first, for the convenience of the backend routine.
3033 Note that there is a confusing case here. If the real definition
3034 is defined by a regular object file, we don't get the real symbol
3035 from the dynamic object, but we do get the weak symbol. If the
3036 processor backend uses a COPY reloc, then if some routine in the
3037 dynamic object changes the real symbol, we will not see that
3038 change in the corresponding weak symbol. This is the way other
3039 ELF linkers work as well, and seems to be a result of the shared
3042 I will clarify this issue. Most SVR4 shared libraries define the
3043 variable _timezone and define timezone as a weak synonym. The
3044 tzset call changes _timezone. If you write
3045 extern int timezone;
3047 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3048 you might expect that, since timezone is a synonym for _timezone,
3049 the same number will print both times. However, if the processor
3050 backend uses a COPY reloc, then actually timezone will be copied
3051 into your process image, and, since you define _timezone
3052 yourself, _timezone will not. Thus timezone and _timezone will
3053 wind up at different memory locations. The tzset call will set
3054 _timezone, leaving timezone unchanged. */
3056 if (h
->is_weakalias
)
3058 struct elf_link_hash_entry
*def
= weakdef (h
);
3060 /* If we get to this point, there is an implicit reference to
3061 the alias by a regular object file via the weak symbol H. */
3062 def
->ref_regular
= 1;
3064 /* Ensure that the backend adjust_dynamic_symbol function sees
3065 the strong alias before H by recursively calling ourselves. */
3066 if (!_bfd_elf_adjust_dynamic_symbol (def
, eif
))
3070 /* If a symbol has no type and no size and does not require a PLT
3071 entry, then we are probably about to do the wrong thing here: we
3072 are probably going to create a COPY reloc for an empty object.
3073 This case can arise when a shared object is built with assembly
3074 code, and the assembly code fails to set the symbol type. */
3076 && h
->type
== STT_NOTYPE
3079 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3080 h
->root
.root
.string
);
3082 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3091 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3095 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
3096 struct elf_link_hash_entry
*h
,
3099 unsigned int power_of_two
;
3101 asection
*sec
= h
->root
.u
.def
.section
;
3103 /* The section alignment of the definition is the maximum alignment
3104 requirement of symbols defined in the section. Since we don't
3105 know the symbol alignment requirement, we start with the
3106 maximum alignment and check low bits of the symbol address
3107 for the minimum alignment. */
3108 power_of_two
= bfd_section_alignment (sec
);
3109 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
3110 while ((h
->root
.u
.def
.value
& mask
) != 0)
3116 if (power_of_two
> bfd_section_alignment (dynbss
))
3118 /* Adjust the section alignment if needed. */
3119 if (!bfd_set_section_alignment (dynbss
, power_of_two
))
3123 /* We make sure that the symbol will be aligned properly. */
3124 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
3126 /* Define the symbol as being at this point in DYNBSS. */
3127 h
->root
.u
.def
.section
= dynbss
;
3128 h
->root
.u
.def
.value
= dynbss
->size
;
3130 /* Increment the size of DYNBSS to make room for the symbol. */
3131 dynbss
->size
+= h
->size
;
3133 /* No error if extern_protected_data is true. */
3134 if (h
->protected_def
3135 && (!info
->extern_protected_data
3136 || (info
->extern_protected_data
< 0
3137 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
3138 info
->callbacks
->einfo
3139 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3140 h
->root
.root
.string
);
3145 /* Adjust all external symbols pointing into SEC_MERGE sections
3146 to reflect the object merging within the sections. */
3149 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
3153 if ((h
->root
.type
== bfd_link_hash_defined
3154 || h
->root
.type
== bfd_link_hash_defweak
)
3155 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
3156 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
3158 bfd
*output_bfd
= (bfd
*) data
;
3160 h
->root
.u
.def
.value
=
3161 _bfd_merged_section_offset (output_bfd
,
3162 &h
->root
.u
.def
.section
,
3163 elf_section_data (sec
)->sec_info
,
3164 h
->root
.u
.def
.value
);
3170 /* Returns false if the symbol referred to by H should be considered
3171 to resolve local to the current module, and true if it should be
3172 considered to bind dynamically. */
3175 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
3176 struct bfd_link_info
*info
,
3177 bfd_boolean not_local_protected
)
3179 bfd_boolean binding_stays_local_p
;
3180 const struct elf_backend_data
*bed
;
3181 struct elf_link_hash_table
*hash_table
;
3186 while (h
->root
.type
== bfd_link_hash_indirect
3187 || h
->root
.type
== bfd_link_hash_warning
)
3188 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3190 /* If it was forced local, then clearly it's not dynamic. */
3191 if (h
->dynindx
== -1)
3193 if (h
->forced_local
)
3196 /* Identify the cases where name binding rules say that a
3197 visible symbol resolves locally. */
3198 binding_stays_local_p
= (bfd_link_executable (info
)
3199 || SYMBOLIC_BIND (info
, h
));
3201 switch (ELF_ST_VISIBILITY (h
->other
))
3208 hash_table
= elf_hash_table (info
);
3209 if (!is_elf_hash_table (hash_table
))
3212 bed
= get_elf_backend_data (hash_table
->dynobj
);
3214 /* Proper resolution for function pointer equality may require
3215 that these symbols perhaps be resolved dynamically, even though
3216 we should be resolving them to the current module. */
3217 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
3218 binding_stays_local_p
= TRUE
;
3225 /* If it isn't defined locally, then clearly it's dynamic. */
3226 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
3229 /* Otherwise, the symbol is dynamic if binding rules don't tell
3230 us that it remains local. */
3231 return !binding_stays_local_p
;
3234 /* Return true if the symbol referred to by H should be considered
3235 to resolve local to the current module, and false otherwise. Differs
3236 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3237 undefined symbols. The two functions are virtually identical except
3238 for the place where dynindx == -1 is tested. If that test is true,
3239 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3240 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3242 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3243 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3244 treatment of undefined weak symbols. For those that do not make
3245 undefined weak symbols dynamic, both functions may return false. */
3248 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3249 struct bfd_link_info
*info
,
3250 bfd_boolean local_protected
)
3252 const struct elf_backend_data
*bed
;
3253 struct elf_link_hash_table
*hash_table
;
3255 /* If it's a local sym, of course we resolve locally. */
3259 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3260 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3261 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3264 /* Forced local symbols resolve locally. */
3265 if (h
->forced_local
)
3268 /* Common symbols that become definitions don't get the DEF_REGULAR
3269 flag set, so test it first, and don't bail out. */
3270 if (ELF_COMMON_DEF_P (h
))
3272 /* If we don't have a definition in a regular file, then we can't
3273 resolve locally. The sym is either undefined or dynamic. */
3274 else if (!h
->def_regular
)
3277 /* Non-dynamic symbols resolve locally. */
3278 if (h
->dynindx
== -1)
3281 /* At this point, we know the symbol is defined and dynamic. In an
3282 executable it must resolve locally, likewise when building symbolic
3283 shared libraries. */
3284 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3287 /* Now deal with defined dynamic symbols in shared libraries. Ones
3288 with default visibility might not resolve locally. */
3289 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3292 hash_table
= elf_hash_table (info
);
3293 if (!is_elf_hash_table (hash_table
))
3296 bed
= get_elf_backend_data (hash_table
->dynobj
);
3298 /* If extern_protected_data is false, STV_PROTECTED non-function
3299 symbols are local. */
3300 if ((!info
->extern_protected_data
3301 || (info
->extern_protected_data
< 0
3302 && !bed
->extern_protected_data
))
3303 && !bed
->is_function_type (h
->type
))
3306 /* Function pointer equality tests may require that STV_PROTECTED
3307 symbols be treated as dynamic symbols. If the address of a
3308 function not defined in an executable is set to that function's
3309 plt entry in the executable, then the address of the function in
3310 a shared library must also be the plt entry in the executable. */
3311 return local_protected
;
3314 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3315 aligned. Returns the first TLS output section. */
3317 struct bfd_section
*
3318 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3320 struct bfd_section
*sec
, *tls
;
3321 unsigned int align
= 0;
3323 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3324 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3328 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3329 if (sec
->alignment_power
> align
)
3330 align
= sec
->alignment_power
;
3332 elf_hash_table (info
)->tls_sec
= tls
;
3334 /* Ensure the alignment of the first section (usually .tdata) is the largest
3335 alignment, so that the tls segment starts aligned. */
3337 tls
->alignment_power
= align
;
3342 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3344 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3345 Elf_Internal_Sym
*sym
)
3347 const struct elf_backend_data
*bed
;
3349 /* Local symbols do not count, but target specific ones might. */
3350 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3351 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3354 bed
= get_elf_backend_data (abfd
);
3355 /* Function symbols do not count. */
3356 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3359 /* If the section is undefined, then so is the symbol. */
3360 if (sym
->st_shndx
== SHN_UNDEF
)
3363 /* If the symbol is defined in the common section, then
3364 it is a common definition and so does not count. */
3365 if (bed
->common_definition (sym
))
3368 /* If the symbol is in a target specific section then we
3369 must rely upon the backend to tell us what it is. */
3370 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3371 /* FIXME - this function is not coded yet:
3373 return _bfd_is_global_symbol_definition (abfd, sym);
3375 Instead for now assume that the definition is not global,
3376 Even if this is wrong, at least the linker will behave
3377 in the same way that it used to do. */
3383 /* Search the symbol table of the archive element of the archive ABFD
3384 whose archive map contains a mention of SYMDEF, and determine if
3385 the symbol is defined in this element. */
3387 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3389 Elf_Internal_Shdr
* hdr
;
3393 Elf_Internal_Sym
*isymbuf
;
3394 Elf_Internal_Sym
*isym
;
3395 Elf_Internal_Sym
*isymend
;
3398 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
3402 if (! bfd_check_format (abfd
, bfd_object
))
3405 /* Select the appropriate symbol table. If we don't know if the
3406 object file is an IR object, give linker LTO plugin a chance to
3407 get the correct symbol table. */
3408 if (abfd
->plugin_format
== bfd_plugin_yes
3409 #if BFD_SUPPORTS_PLUGINS
3410 || (abfd
->plugin_format
== bfd_plugin_unknown
3411 && bfd_link_plugin_object_p (abfd
))
3415 /* Use the IR symbol table if the object has been claimed by
3417 abfd
= abfd
->plugin_dummy_bfd
;
3418 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3420 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3421 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3423 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3425 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3427 /* The sh_info field of the symtab header tells us where the
3428 external symbols start. We don't care about the local symbols. */
3429 if (elf_bad_symtab (abfd
))
3431 extsymcount
= symcount
;
3436 extsymcount
= symcount
- hdr
->sh_info
;
3437 extsymoff
= hdr
->sh_info
;
3440 if (extsymcount
== 0)
3443 /* Read in the symbol table. */
3444 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3446 if (isymbuf
== NULL
)
3449 /* Scan the symbol table looking for SYMDEF. */
3451 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3455 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3460 if (strcmp (name
, symdef
->name
) == 0)
3462 result
= is_global_data_symbol_definition (abfd
, isym
);
3472 /* Add an entry to the .dynamic table. */
3475 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3479 struct elf_link_hash_table
*hash_table
;
3480 const struct elf_backend_data
*bed
;
3482 bfd_size_type newsize
;
3483 bfd_byte
*newcontents
;
3484 Elf_Internal_Dyn dyn
;
3486 hash_table
= elf_hash_table (info
);
3487 if (! is_elf_hash_table (hash_table
))
3490 if (tag
== DT_RELA
|| tag
== DT_REL
)
3491 hash_table
->dynamic_relocs
= TRUE
;
3493 bed
= get_elf_backend_data (hash_table
->dynobj
);
3494 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3495 BFD_ASSERT (s
!= NULL
);
3497 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3498 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3499 if (newcontents
== NULL
)
3503 dyn
.d_un
.d_val
= val
;
3504 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3507 s
->contents
= newcontents
;
3512 /* Strip zero-sized dynamic sections. */
3515 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info
*info
)
3517 struct elf_link_hash_table
*hash_table
;
3518 const struct elf_backend_data
*bed
;
3519 asection
*s
, *sdynamic
, **pp
;
3520 asection
*rela_dyn
, *rel_dyn
;
3521 Elf_Internal_Dyn dyn
;
3522 bfd_byte
*extdyn
, *next
;
3523 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
3524 bfd_boolean strip_zero_sized
;
3525 bfd_boolean strip_zero_sized_plt
;
3527 if (bfd_link_relocatable (info
))
3530 hash_table
= elf_hash_table (info
);
3531 if (!is_elf_hash_table (hash_table
))
3534 if (!hash_table
->dynobj
)
3537 sdynamic
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3541 bed
= get_elf_backend_data (hash_table
->dynobj
);
3542 swap_dyn_in
= bed
->s
->swap_dyn_in
;
3544 strip_zero_sized
= FALSE
;
3545 strip_zero_sized_plt
= FALSE
;
3547 /* Strip zero-sized dynamic sections. */
3548 rela_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rela.dyn");
3549 rel_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rel.dyn");
3550 for (pp
= &info
->output_bfd
->sections
; (s
= *pp
) != NULL
;)
3554 || s
== hash_table
->srelplt
->output_section
3555 || s
== hash_table
->splt
->output_section
))
3558 info
->output_bfd
->section_count
--;
3559 strip_zero_sized
= TRUE
;
3564 else if (s
== hash_table
->splt
->output_section
)
3566 s
= hash_table
->splt
;
3567 strip_zero_sized_plt
= TRUE
;
3570 s
= hash_table
->srelplt
;
3571 s
->flags
|= SEC_EXCLUDE
;
3572 s
->output_section
= bfd_abs_section_ptr
;
3577 if (strip_zero_sized_plt
)
3578 for (extdyn
= sdynamic
->contents
;
3579 extdyn
< sdynamic
->contents
+ sdynamic
->size
;
3582 next
= extdyn
+ bed
->s
->sizeof_dyn
;
3583 swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3591 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3592 the procedure linkage table (the .plt section) has been
3594 memmove (extdyn
, next
,
3595 sdynamic
->size
- (next
- sdynamic
->contents
));
3600 if (strip_zero_sized
)
3602 /* Regenerate program headers. */
3603 elf_seg_map (info
->output_bfd
) = NULL
;
3604 return _bfd_elf_map_sections_to_segments (info
->output_bfd
, info
);
3610 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3611 1 if a DT_NEEDED tag already exists, and 0 on success. */
3614 bfd_elf_add_dt_needed_tag (bfd
*abfd
, struct bfd_link_info
*info
)
3616 struct elf_link_hash_table
*hash_table
;
3620 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3623 hash_table
= elf_hash_table (info
);
3624 soname
= elf_dt_name (abfd
);
3625 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3626 if (strindex
== (size_t) -1)
3629 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3632 const struct elf_backend_data
*bed
;
3635 bed
= get_elf_backend_data (hash_table
->dynobj
);
3636 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3638 for (extdyn
= sdyn
->contents
;
3639 extdyn
< sdyn
->contents
+ sdyn
->size
;
3640 extdyn
+= bed
->s
->sizeof_dyn
)
3642 Elf_Internal_Dyn dyn
;
3644 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3645 if (dyn
.d_tag
== DT_NEEDED
3646 && dyn
.d_un
.d_val
== strindex
)
3648 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3654 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3657 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3663 /* Return true if SONAME is on the needed list between NEEDED and STOP
3664 (or the end of list if STOP is NULL), and needed by a library that
3668 on_needed_list (const char *soname
,
3669 struct bfd_link_needed_list
*needed
,
3670 struct bfd_link_needed_list
*stop
)
3672 struct bfd_link_needed_list
*look
;
3673 for (look
= needed
; look
!= stop
; look
= look
->next
)
3674 if (strcmp (soname
, look
->name
) == 0
3675 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3676 /* If needed by a library that itself is not directly
3677 needed, recursively check whether that library is
3678 indirectly needed. Since we add DT_NEEDED entries to
3679 the end of the list, library dependencies appear after
3680 the library. Therefore search prior to the current
3681 LOOK, preventing possible infinite recursion. */
3682 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3688 /* Sort symbol by value, section, size, and type. */
3690 elf_sort_symbol (const void *arg1
, const void *arg2
)
3692 const struct elf_link_hash_entry
*h1
;
3693 const struct elf_link_hash_entry
*h2
;
3694 bfd_signed_vma vdiff
;
3699 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3700 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3701 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3703 return vdiff
> 0 ? 1 : -1;
3705 sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3709 /* Sort so that sized symbols are selected over zero size symbols. */
3710 vdiff
= h1
->size
- h2
->size
;
3712 return vdiff
> 0 ? 1 : -1;
3714 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3715 if (h1
->type
!= h2
->type
)
3716 return h1
->type
- h2
->type
;
3718 /* If symbols are properly sized and typed, and multiple strong
3719 aliases are not defined in a shared library by the user we
3720 shouldn't get here. Unfortunately linker script symbols like
3721 __bss_start sometimes match a user symbol defined at the start of
3722 .bss without proper size and type. We'd like to preference the
3723 user symbol over reserved system symbols. Sort on leading
3725 n1
= h1
->root
.root
.string
;
3726 n2
= h2
->root
.root
.string
;
3739 /* Final sort on name selects user symbols like '_u' over reserved
3740 system symbols like '_Z' and also will avoid qsort instability. */
3744 /* This function is used to adjust offsets into .dynstr for
3745 dynamic symbols. This is called via elf_link_hash_traverse. */
3748 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3750 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3752 if (h
->dynindx
!= -1)
3753 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3757 /* Assign string offsets in .dynstr, update all structures referencing
3761 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3763 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3764 struct elf_link_local_dynamic_entry
*entry
;
3765 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3766 bfd
*dynobj
= hash_table
->dynobj
;
3769 const struct elf_backend_data
*bed
;
3772 _bfd_elf_strtab_finalize (dynstr
);
3773 size
= _bfd_elf_strtab_size (dynstr
);
3775 bed
= get_elf_backend_data (dynobj
);
3776 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3777 BFD_ASSERT (sdyn
!= NULL
);
3779 /* Update all .dynamic entries referencing .dynstr strings. */
3780 for (extdyn
= sdyn
->contents
;
3781 extdyn
< sdyn
->contents
+ sdyn
->size
;
3782 extdyn
+= bed
->s
->sizeof_dyn
)
3784 Elf_Internal_Dyn dyn
;
3786 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3790 dyn
.d_un
.d_val
= size
;
3800 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3805 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3808 /* Now update local dynamic symbols. */
3809 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3810 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3811 entry
->isym
.st_name
);
3813 /* And the rest of dynamic symbols. */
3814 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3816 /* Adjust version definitions. */
3817 if (elf_tdata (output_bfd
)->cverdefs
)
3822 Elf_Internal_Verdef def
;
3823 Elf_Internal_Verdaux defaux
;
3825 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3829 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3831 p
+= sizeof (Elf_External_Verdef
);
3832 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3834 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3836 _bfd_elf_swap_verdaux_in (output_bfd
,
3837 (Elf_External_Verdaux
*) p
, &defaux
);
3838 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3840 _bfd_elf_swap_verdaux_out (output_bfd
,
3841 &defaux
, (Elf_External_Verdaux
*) p
);
3842 p
+= sizeof (Elf_External_Verdaux
);
3845 while (def
.vd_next
);
3848 /* Adjust version references. */
3849 if (elf_tdata (output_bfd
)->verref
)
3854 Elf_Internal_Verneed need
;
3855 Elf_Internal_Vernaux needaux
;
3857 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
3861 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3863 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3864 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3865 (Elf_External_Verneed
*) p
);
3866 p
+= sizeof (Elf_External_Verneed
);
3867 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3869 _bfd_elf_swap_vernaux_in (output_bfd
,
3870 (Elf_External_Vernaux
*) p
, &needaux
);
3871 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3873 _bfd_elf_swap_vernaux_out (output_bfd
,
3875 (Elf_External_Vernaux
*) p
);
3876 p
+= sizeof (Elf_External_Vernaux
);
3879 while (need
.vn_next
);
3885 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3886 The default is to only match when the INPUT and OUTPUT are exactly
3890 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3891 const bfd_target
*output
)
3893 return input
== output
;
3896 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3897 This version is used when different targets for the same architecture
3898 are virtually identical. */
3901 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3902 const bfd_target
*output
)
3904 const struct elf_backend_data
*obed
, *ibed
;
3906 if (input
== output
)
3909 ibed
= xvec_get_elf_backend_data (input
);
3910 obed
= xvec_get_elf_backend_data (output
);
3912 if (ibed
->arch
!= obed
->arch
)
3915 /* If both backends are using this function, deem them compatible. */
3916 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3919 /* Make a special call to the linker "notice" function to tell it that
3920 we are about to handle an as-needed lib, or have finished
3921 processing the lib. */
3924 _bfd_elf_notice_as_needed (bfd
*ibfd
,
3925 struct bfd_link_info
*info
,
3926 enum notice_asneeded_action act
)
3928 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
3931 /* Check relocations an ELF object file. */
3934 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
3936 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3937 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
3939 /* If this object is the same format as the output object, and it is
3940 not a shared library, then let the backend look through the
3943 This is required to build global offset table entries and to
3944 arrange for dynamic relocs. It is not required for the
3945 particular common case of linking non PIC code, even when linking
3946 against shared libraries, but unfortunately there is no way of
3947 knowing whether an object file has been compiled PIC or not.
3948 Looking through the relocs is not particularly time consuming.
3949 The problem is that we must either (1) keep the relocs in memory,
3950 which causes the linker to require additional runtime memory or
3951 (2) read the relocs twice from the input file, which wastes time.
3952 This would be a good case for using mmap.
3954 I have no idea how to handle linking PIC code into a file of a
3955 different format. It probably can't be done. */
3956 if ((abfd
->flags
& DYNAMIC
) == 0
3957 && is_elf_hash_table (htab
)
3958 && bed
->check_relocs
!= NULL
3959 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
3960 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
3964 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3966 Elf_Internal_Rela
*internal_relocs
;
3969 /* Don't check relocations in excluded sections. Don't do
3970 anything special with non-loaded, non-alloced sections.
3971 In particular, any relocs in such sections should not
3972 affect GOT and PLT reference counting (ie. we don't
3973 allow them to create GOT or PLT entries), there's no
3974 possibility or desire to optimize TLS relocs, and
3975 there's not much point in propagating relocs to shared
3976 libs that the dynamic linker won't relocate. */
3977 if ((o
->flags
& SEC_ALLOC
) == 0
3978 || (o
->flags
& SEC_RELOC
) == 0
3979 || (o
->flags
& SEC_EXCLUDE
) != 0
3980 || o
->reloc_count
== 0
3981 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
3982 && (o
->flags
& SEC_DEBUGGING
) != 0)
3983 || bfd_is_abs_section (o
->output_section
))
3986 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
3988 if (internal_relocs
== NULL
)
3991 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
3993 if (elf_section_data (o
)->relocs
!= internal_relocs
)
3994 free (internal_relocs
);
4004 /* Add symbols from an ELF object file to the linker hash table. */
4007 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4009 Elf_Internal_Ehdr
*ehdr
;
4010 Elf_Internal_Shdr
*hdr
;
4014 struct elf_link_hash_entry
**sym_hash
;
4015 bfd_boolean dynamic
;
4016 Elf_External_Versym
*extversym
= NULL
;
4017 Elf_External_Versym
*extversym_end
= NULL
;
4018 Elf_External_Versym
*ever
;
4019 struct elf_link_hash_entry
*weaks
;
4020 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
4021 size_t nondeflt_vers_cnt
= 0;
4022 Elf_Internal_Sym
*isymbuf
= NULL
;
4023 Elf_Internal_Sym
*isym
;
4024 Elf_Internal_Sym
*isymend
;
4025 const struct elf_backend_data
*bed
;
4026 bfd_boolean add_needed
;
4027 struct elf_link_hash_table
*htab
;
4028 void *alloc_mark
= NULL
;
4029 struct bfd_hash_entry
**old_table
= NULL
;
4030 unsigned int old_size
= 0;
4031 unsigned int old_count
= 0;
4032 void *old_tab
= NULL
;
4034 struct bfd_link_hash_entry
*old_undefs
= NULL
;
4035 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
4036 void *old_strtab
= NULL
;
4039 bfd_boolean just_syms
;
4041 htab
= elf_hash_table (info
);
4042 bed
= get_elf_backend_data (abfd
);
4044 if ((abfd
->flags
& DYNAMIC
) == 0)
4050 /* You can't use -r against a dynamic object. Also, there's no
4051 hope of using a dynamic object which does not exactly match
4052 the format of the output file. */
4053 if (bfd_link_relocatable (info
)
4054 || !is_elf_hash_table (htab
)
4055 || info
->output_bfd
->xvec
!= abfd
->xvec
)
4057 if (bfd_link_relocatable (info
))
4058 bfd_set_error (bfd_error_invalid_operation
);
4060 bfd_set_error (bfd_error_wrong_format
);
4065 ehdr
= elf_elfheader (abfd
);
4066 if (info
->warn_alternate_em
4067 && bed
->elf_machine_code
!= ehdr
->e_machine
4068 && ((bed
->elf_machine_alt1
!= 0
4069 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
4070 || (bed
->elf_machine_alt2
!= 0
4071 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
4073 /* xgettext:c-format */
4074 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4075 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
4077 /* As a GNU extension, any input sections which are named
4078 .gnu.warning.SYMBOL are treated as warning symbols for the given
4079 symbol. This differs from .gnu.warning sections, which generate
4080 warnings when they are included in an output file. */
4081 /* PR 12761: Also generate this warning when building shared libraries. */
4082 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4086 name
= bfd_section_name (s
);
4087 if (CONST_STRNEQ (name
, ".gnu.warning."))
4092 name
+= sizeof ".gnu.warning." - 1;
4094 /* If this is a shared object, then look up the symbol
4095 in the hash table. If it is there, and it is already
4096 been defined, then we will not be using the entry
4097 from this shared object, so we don't need to warn.
4098 FIXME: If we see the definition in a regular object
4099 later on, we will warn, but we shouldn't. The only
4100 fix is to keep track of what warnings we are supposed
4101 to emit, and then handle them all at the end of the
4105 struct elf_link_hash_entry
*h
;
4107 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
4109 /* FIXME: What about bfd_link_hash_common? */
4111 && (h
->root
.type
== bfd_link_hash_defined
4112 || h
->root
.type
== bfd_link_hash_defweak
))
4117 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
4121 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
4126 if (! (_bfd_generic_link_add_one_symbol
4127 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
4128 FALSE
, bed
->collect
, NULL
)))
4131 if (bfd_link_executable (info
))
4133 /* Clobber the section size so that the warning does
4134 not get copied into the output file. */
4137 /* Also set SEC_EXCLUDE, so that symbols defined in
4138 the warning section don't get copied to the output. */
4139 s
->flags
|= SEC_EXCLUDE
;
4144 just_syms
= ((s
= abfd
->sections
) != NULL
4145 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
4150 /* If we are creating a shared library, create all the dynamic
4151 sections immediately. We need to attach them to something,
4152 so we attach them to this BFD, provided it is the right
4153 format and is not from ld --just-symbols. Always create the
4154 dynamic sections for -E/--dynamic-list. FIXME: If there
4155 are no input BFD's of the same format as the output, we can't
4156 make a shared library. */
4158 && (bfd_link_pic (info
)
4159 || (!bfd_link_relocatable (info
)
4161 && (info
->export_dynamic
|| info
->dynamic
)))
4162 && is_elf_hash_table (htab
)
4163 && info
->output_bfd
->xvec
== abfd
->xvec
4164 && !htab
->dynamic_sections_created
)
4166 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
4170 else if (!is_elf_hash_table (htab
))
4174 const char *soname
= NULL
;
4176 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
4177 const Elf_Internal_Phdr
*phdr
;
4178 struct elf_link_loaded_list
*loaded_lib
;
4180 /* ld --just-symbols and dynamic objects don't mix very well.
4181 ld shouldn't allow it. */
4185 /* If this dynamic lib was specified on the command line with
4186 --as-needed in effect, then we don't want to add a DT_NEEDED
4187 tag unless the lib is actually used. Similary for libs brought
4188 in by another lib's DT_NEEDED. When --no-add-needed is used
4189 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4190 any dynamic library in DT_NEEDED tags in the dynamic lib at
4192 add_needed
= (elf_dyn_lib_class (abfd
)
4193 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
4194 | DYN_NO_NEEDED
)) == 0;
4196 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4201 unsigned int elfsec
;
4202 unsigned long shlink
;
4204 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
4211 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
4212 if (elfsec
== SHN_BAD
)
4213 goto error_free_dyn
;
4214 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
4216 for (extdyn
= dynbuf
;
4217 extdyn
<= dynbuf
+ s
->size
- bed
->s
->sizeof_dyn
;
4218 extdyn
+= bed
->s
->sizeof_dyn
)
4220 Elf_Internal_Dyn dyn
;
4222 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
4223 if (dyn
.d_tag
== DT_SONAME
)
4225 unsigned int tagv
= dyn
.d_un
.d_val
;
4226 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4228 goto error_free_dyn
;
4230 if (dyn
.d_tag
== DT_NEEDED
)
4232 struct bfd_link_needed_list
*n
, **pn
;
4234 unsigned int tagv
= dyn
.d_un
.d_val
;
4235 size_t amt
= sizeof (struct bfd_link_needed_list
);
4237 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4238 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4239 if (n
== NULL
|| fnm
== NULL
)
4240 goto error_free_dyn
;
4241 amt
= strlen (fnm
) + 1;
4242 anm
= (char *) bfd_alloc (abfd
, amt
);
4244 goto error_free_dyn
;
4245 memcpy (anm
, fnm
, amt
);
4249 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4253 if (dyn
.d_tag
== DT_RUNPATH
)
4255 struct bfd_link_needed_list
*n
, **pn
;
4257 unsigned int tagv
= dyn
.d_un
.d_val
;
4258 size_t amt
= sizeof (struct bfd_link_needed_list
);
4260 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4261 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4262 if (n
== NULL
|| fnm
== NULL
)
4263 goto error_free_dyn
;
4264 amt
= strlen (fnm
) + 1;
4265 anm
= (char *) bfd_alloc (abfd
, amt
);
4267 goto error_free_dyn
;
4268 memcpy (anm
, fnm
, amt
);
4272 for (pn
= & runpath
;
4278 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4279 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
4281 struct bfd_link_needed_list
*n
, **pn
;
4283 unsigned int tagv
= dyn
.d_un
.d_val
;
4284 size_t amt
= sizeof (struct bfd_link_needed_list
);
4286 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4287 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4288 if (n
== NULL
|| fnm
== NULL
)
4289 goto error_free_dyn
;
4290 amt
= strlen (fnm
) + 1;
4291 anm
= (char *) bfd_alloc (abfd
, amt
);
4293 goto error_free_dyn
;
4294 memcpy (anm
, fnm
, amt
);
4304 if (dyn
.d_tag
== DT_AUDIT
)
4306 unsigned int tagv
= dyn
.d_un
.d_val
;
4307 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4314 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4315 frees all more recently bfd_alloc'd blocks as well. */
4321 struct bfd_link_needed_list
**pn
;
4322 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4327 /* If we have a PT_GNU_RELRO program header, mark as read-only
4328 all sections contained fully therein. This makes relro
4329 shared library sections appear as they will at run-time. */
4330 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4331 while (phdr
-- > elf_tdata (abfd
)->phdr
)
4332 if (phdr
->p_type
== PT_GNU_RELRO
)
4334 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4336 unsigned int opb
= bfd_octets_per_byte (abfd
, s
);
4338 if ((s
->flags
& SEC_ALLOC
) != 0
4339 && s
->vma
* opb
>= phdr
->p_vaddr
4340 && s
->vma
* opb
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4341 s
->flags
|= SEC_READONLY
;
4346 /* We do not want to include any of the sections in a dynamic
4347 object in the output file. We hack by simply clobbering the
4348 list of sections in the BFD. This could be handled more
4349 cleanly by, say, a new section flag; the existing
4350 SEC_NEVER_LOAD flag is not the one we want, because that one
4351 still implies that the section takes up space in the output
4353 bfd_section_list_clear (abfd
);
4355 /* Find the name to use in a DT_NEEDED entry that refers to this
4356 object. If the object has a DT_SONAME entry, we use it.
4357 Otherwise, if the generic linker stuck something in
4358 elf_dt_name, we use that. Otherwise, we just use the file
4360 if (soname
== NULL
|| *soname
== '\0')
4362 soname
= elf_dt_name (abfd
);
4363 if (soname
== NULL
|| *soname
== '\0')
4364 soname
= bfd_get_filename (abfd
);
4367 /* Save the SONAME because sometimes the linker emulation code
4368 will need to know it. */
4369 elf_dt_name (abfd
) = soname
;
4371 /* If we have already included this dynamic object in the
4372 link, just ignore it. There is no reason to include a
4373 particular dynamic object more than once. */
4374 for (loaded_lib
= htab
->dyn_loaded
;
4376 loaded_lib
= loaded_lib
->next
)
4378 if (strcmp (elf_dt_name (loaded_lib
->abfd
), soname
) == 0)
4382 /* Create dynamic sections for backends that require that be done
4383 before setup_gnu_properties. */
4385 && !_bfd_elf_link_create_dynamic_sections (abfd
, info
))
4388 /* Save the DT_AUDIT entry for the linker emulation code. */
4389 elf_dt_audit (abfd
) = audit
;
4392 /* If this is a dynamic object, we always link against the .dynsym
4393 symbol table, not the .symtab symbol table. The dynamic linker
4394 will only see the .dynsym symbol table, so there is no reason to
4395 look at .symtab for a dynamic object. */
4397 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4398 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4400 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4402 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4404 /* The sh_info field of the symtab header tells us where the
4405 external symbols start. We don't care about the local symbols at
4407 if (elf_bad_symtab (abfd
))
4409 extsymcount
= symcount
;
4414 extsymcount
= symcount
- hdr
->sh_info
;
4415 extsymoff
= hdr
->sh_info
;
4418 sym_hash
= elf_sym_hashes (abfd
);
4419 if (extsymcount
!= 0)
4421 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4423 if (isymbuf
== NULL
)
4426 if (sym_hash
== NULL
)
4428 /* We store a pointer to the hash table entry for each
4430 size_t amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4431 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4432 if (sym_hash
== NULL
)
4433 goto error_free_sym
;
4434 elf_sym_hashes (abfd
) = sym_hash
;
4440 /* Read in any version definitions. */
4441 if (!_bfd_elf_slurp_version_tables (abfd
,
4442 info
->default_imported_symver
))
4443 goto error_free_sym
;
4445 /* Read in the symbol versions, but don't bother to convert them
4446 to internal format. */
4447 if (elf_dynversym (abfd
) != 0)
4449 Elf_Internal_Shdr
*versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4450 bfd_size_type amt
= versymhdr
->sh_size
;
4452 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0)
4453 goto error_free_sym
;
4454 extversym
= (Elf_External_Versym
*)
4455 _bfd_malloc_and_read (abfd
, amt
, amt
);
4456 if (extversym
== NULL
)
4457 goto error_free_sym
;
4458 extversym_end
= extversym
+ amt
/ sizeof (*extversym
);
4462 /* If we are loading an as-needed shared lib, save the symbol table
4463 state before we start adding symbols. If the lib turns out
4464 to be unneeded, restore the state. */
4465 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4470 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4472 struct bfd_hash_entry
*p
;
4473 struct elf_link_hash_entry
*h
;
4475 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4477 h
= (struct elf_link_hash_entry
*) p
;
4478 entsize
+= htab
->root
.table
.entsize
;
4479 if (h
->root
.type
== bfd_link_hash_warning
)
4480 entsize
+= htab
->root
.table
.entsize
;
4484 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4485 old_tab
= bfd_malloc (tabsize
+ entsize
);
4486 if (old_tab
== NULL
)
4487 goto error_free_vers
;
4489 /* Remember the current objalloc pointer, so that all mem for
4490 symbols added can later be reclaimed. */
4491 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4492 if (alloc_mark
== NULL
)
4493 goto error_free_vers
;
4495 /* Make a special call to the linker "notice" function to
4496 tell it that we are about to handle an as-needed lib. */
4497 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4498 goto error_free_vers
;
4500 /* Clone the symbol table. Remember some pointers into the
4501 symbol table, and dynamic symbol count. */
4502 old_ent
= (char *) old_tab
+ tabsize
;
4503 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4504 old_undefs
= htab
->root
.undefs
;
4505 old_undefs_tail
= htab
->root
.undefs_tail
;
4506 old_table
= htab
->root
.table
.table
;
4507 old_size
= htab
->root
.table
.size
;
4508 old_count
= htab
->root
.table
.count
;
4510 if (htab
->dynstr
!= NULL
)
4512 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4513 if (old_strtab
== NULL
)
4514 goto error_free_vers
;
4517 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4519 struct bfd_hash_entry
*p
;
4520 struct elf_link_hash_entry
*h
;
4522 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4524 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
4525 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4526 h
= (struct elf_link_hash_entry
*) p
;
4527 if (h
->root
.type
== bfd_link_hash_warning
)
4529 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
4530 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4537 if (extversym
== NULL
)
4539 else if (extversym
+ extsymoff
< extversym_end
)
4540 ever
= extversym
+ extsymoff
;
4543 /* xgettext:c-format */
4544 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4545 abfd
, (long) extsymoff
,
4546 (long) (extversym_end
- extversym
) / sizeof (* extversym
));
4547 bfd_set_error (bfd_error_bad_value
);
4548 goto error_free_vers
;
4551 if (!bfd_link_relocatable (info
)
4552 && abfd
->lto_slim_object
)
4555 (_("%pB: plugin needed to handle lto object"), abfd
);
4558 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
4560 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4564 asection
*sec
, *new_sec
;
4567 struct elf_link_hash_entry
*h
;
4568 struct elf_link_hash_entry
*hi
;
4569 bfd_boolean definition
;
4570 bfd_boolean size_change_ok
;
4571 bfd_boolean type_change_ok
;
4572 bfd_boolean new_weak
;
4573 bfd_boolean old_weak
;
4574 bfd_boolean override
;
4576 bfd_boolean discarded
;
4577 unsigned int old_alignment
;
4578 unsigned int shindex
;
4580 bfd_boolean matched
;
4584 flags
= BSF_NO_FLAGS
;
4586 value
= isym
->st_value
;
4587 common
= bed
->common_definition (isym
);
4588 if (common
&& info
->inhibit_common_definition
)
4590 /* Treat common symbol as undefined for --no-define-common. */
4591 isym
->st_shndx
= SHN_UNDEF
;
4596 bind
= ELF_ST_BIND (isym
->st_info
);
4600 /* This should be impossible, since ELF requires that all
4601 global symbols follow all local symbols, and that sh_info
4602 point to the first global symbol. Unfortunately, Irix 5
4604 if (elf_bad_symtab (abfd
))
4607 /* If we aren't prepared to handle locals within the globals
4608 then we'll likely segfault on a NULL symbol hash if the
4609 symbol is ever referenced in relocations. */
4610 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
4611 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, hdr
->sh_name
);
4612 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4613 " (>= sh_info of %lu)"),
4614 abfd
, name
, (long) (isym
- isymbuf
+ extsymoff
),
4617 /* Dynamic object relocations are not processed by ld, so
4618 ld won't run into the problem mentioned above. */
4621 bfd_set_error (bfd_error_bad_value
);
4622 goto error_free_vers
;
4625 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4633 case STB_GNU_UNIQUE
:
4634 flags
= BSF_GNU_UNIQUE
;
4638 /* Leave it up to the processor backend. */
4642 if (isym
->st_shndx
== SHN_UNDEF
)
4643 sec
= bfd_und_section_ptr
;
4644 else if (isym
->st_shndx
== SHN_ABS
)
4645 sec
= bfd_abs_section_ptr
;
4646 else if (isym
->st_shndx
== SHN_COMMON
)
4648 sec
= bfd_com_section_ptr
;
4649 /* What ELF calls the size we call the value. What ELF
4650 calls the value we call the alignment. */
4651 value
= isym
->st_size
;
4655 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4657 sec
= bfd_abs_section_ptr
;
4658 else if (discarded_section (sec
))
4660 /* Symbols from discarded section are undefined. We keep
4662 sec
= bfd_und_section_ptr
;
4664 isym
->st_shndx
= SHN_UNDEF
;
4666 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4670 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4673 goto error_free_vers
;
4675 if (isym
->st_shndx
== SHN_COMMON
4676 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4678 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4682 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4684 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4686 goto error_free_vers
;
4690 else if (isym
->st_shndx
== SHN_COMMON
4691 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4692 && !bfd_link_relocatable (info
))
4694 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4698 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4699 | SEC_LINKER_CREATED
);
4700 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4702 goto error_free_vers
;
4706 else if (bed
->elf_add_symbol_hook
)
4708 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4710 goto error_free_vers
;
4712 /* The hook function sets the name to NULL if this symbol
4713 should be skipped for some reason. */
4718 /* Sanity check that all possibilities were handled. */
4722 /* Silently discard TLS symbols from --just-syms. There's
4723 no way to combine a static TLS block with a new TLS block
4724 for this executable. */
4725 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4726 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4729 if (bfd_is_und_section (sec
)
4730 || bfd_is_com_section (sec
))
4735 size_change_ok
= FALSE
;
4736 type_change_ok
= bed
->type_change_ok
;
4743 if (is_elf_hash_table (htab
))
4745 Elf_Internal_Versym iver
;
4746 unsigned int vernum
= 0;
4751 if (info
->default_imported_symver
)
4752 /* Use the default symbol version created earlier. */
4753 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4757 else if (ever
>= extversym_end
)
4759 /* xgettext:c-format */
4760 _bfd_error_handler (_("%pB: not enough version information"),
4762 bfd_set_error (bfd_error_bad_value
);
4763 goto error_free_vers
;
4766 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4768 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4770 /* If this is a hidden symbol, or if it is not version
4771 1, we append the version name to the symbol name.
4772 However, we do not modify a non-hidden absolute symbol
4773 if it is not a function, because it might be the version
4774 symbol itself. FIXME: What if it isn't? */
4775 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4777 && (!bfd_is_abs_section (sec
)
4778 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4781 size_t namelen
, verlen
, newlen
;
4784 if (isym
->st_shndx
!= SHN_UNDEF
)
4786 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4788 else if (vernum
> 1)
4790 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4797 /* xgettext:c-format */
4798 (_("%pB: %s: invalid version %u (max %d)"),
4800 elf_tdata (abfd
)->cverdefs
);
4801 bfd_set_error (bfd_error_bad_value
);
4802 goto error_free_vers
;
4807 /* We cannot simply test for the number of
4808 entries in the VERNEED section since the
4809 numbers for the needed versions do not start
4811 Elf_Internal_Verneed
*t
;
4814 for (t
= elf_tdata (abfd
)->verref
;
4818 Elf_Internal_Vernaux
*a
;
4820 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4822 if (a
->vna_other
== vernum
)
4824 verstr
= a
->vna_nodename
;
4834 /* xgettext:c-format */
4835 (_("%pB: %s: invalid needed version %d"),
4836 abfd
, name
, vernum
);
4837 bfd_set_error (bfd_error_bad_value
);
4838 goto error_free_vers
;
4842 namelen
= strlen (name
);
4843 verlen
= strlen (verstr
);
4844 newlen
= namelen
+ verlen
+ 2;
4845 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4846 && isym
->st_shndx
!= SHN_UNDEF
)
4849 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4850 if (newname
== NULL
)
4851 goto error_free_vers
;
4852 memcpy (newname
, name
, namelen
);
4853 p
= newname
+ namelen
;
4855 /* If this is a defined non-hidden version symbol,
4856 we add another @ to the name. This indicates the
4857 default version of the symbol. */
4858 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4859 && isym
->st_shndx
!= SHN_UNDEF
)
4861 memcpy (p
, verstr
, verlen
+ 1);
4866 /* If this symbol has default visibility and the user has
4867 requested we not re-export it, then mark it as hidden. */
4868 if (!bfd_is_und_section (sec
)
4871 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4872 isym
->st_other
= (STV_HIDDEN
4873 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4875 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4876 sym_hash
, &old_bfd
, &old_weak
,
4877 &old_alignment
, &skip
, &override
,
4878 &type_change_ok
, &size_change_ok
,
4880 goto error_free_vers
;
4885 /* Override a definition only if the new symbol matches the
4887 if (override
&& matched
)
4891 while (h
->root
.type
== bfd_link_hash_indirect
4892 || h
->root
.type
== bfd_link_hash_warning
)
4893 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4895 if (elf_tdata (abfd
)->verdef
!= NULL
4898 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4901 if (! (_bfd_generic_link_add_one_symbol
4902 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4903 (struct bfd_link_hash_entry
**) sym_hash
)))
4904 goto error_free_vers
;
4907 /* We need to make sure that indirect symbol dynamic flags are
4910 while (h
->root
.type
== bfd_link_hash_indirect
4911 || h
->root
.type
== bfd_link_hash_warning
)
4912 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4914 /* Setting the index to -3 tells elf_link_output_extsym that
4915 this symbol is defined in a discarded section. */
4921 new_weak
= (flags
& BSF_WEAK
) != 0;
4925 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4926 && is_elf_hash_table (htab
)
4927 && h
->u
.alias
== NULL
)
4929 /* Keep a list of all weak defined non function symbols from
4930 a dynamic object, using the alias field. Later in this
4931 function we will set the alias field to the correct
4932 value. We only put non-function symbols from dynamic
4933 objects on this list, because that happens to be the only
4934 time we need to know the normal symbol corresponding to a
4935 weak symbol, and the information is time consuming to
4936 figure out. If the alias field is not already NULL,
4937 then this symbol was already defined by some previous
4938 dynamic object, and we will be using that previous
4939 definition anyhow. */
4945 /* Set the alignment of a common symbol. */
4946 if ((common
|| bfd_is_com_section (sec
))
4947 && h
->root
.type
== bfd_link_hash_common
)
4952 align
= bfd_log2 (isym
->st_value
);
4955 /* The new symbol is a common symbol in a shared object.
4956 We need to get the alignment from the section. */
4957 align
= new_sec
->alignment_power
;
4959 if (align
> old_alignment
)
4960 h
->root
.u
.c
.p
->alignment_power
= align
;
4962 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4965 if (is_elf_hash_table (htab
))
4967 /* Set a flag in the hash table entry indicating the type of
4968 reference or definition we just found. A dynamic symbol
4969 is one which is referenced or defined by both a regular
4970 object and a shared object. */
4971 bfd_boolean dynsym
= FALSE
;
4973 /* Plugin symbols aren't normal. Don't set def_regular or
4974 ref_regular for them, or make them dynamic. */
4975 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4982 if (bind
!= STB_WEAK
)
4983 h
->ref_regular_nonweak
= 1;
4995 /* If the indirect symbol has been forced local, don't
4996 make the real symbol dynamic. */
4997 if ((h
== hi
|| !hi
->forced_local
)
4998 && (bfd_link_dll (info
)
5008 hi
->ref_dynamic
= 1;
5013 hi
->def_dynamic
= 1;
5016 /* If the indirect symbol has been forced local, don't
5017 make the real symbol dynamic. */
5018 if ((h
== hi
|| !hi
->forced_local
)
5022 && weakdef (h
)->dynindx
!= -1)))
5026 /* Check to see if we need to add an indirect symbol for
5027 the default name. */
5029 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
5030 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
5031 sec
, value
, &old_bfd
, &dynsym
))
5032 goto error_free_vers
;
5034 /* Check the alignment when a common symbol is involved. This
5035 can change when a common symbol is overridden by a normal
5036 definition or a common symbol is ignored due to the old
5037 normal definition. We need to make sure the maximum
5038 alignment is maintained. */
5039 if ((old_alignment
|| common
)
5040 && h
->root
.type
!= bfd_link_hash_common
)
5042 unsigned int common_align
;
5043 unsigned int normal_align
;
5044 unsigned int symbol_align
;
5048 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
5049 || h
->root
.type
== bfd_link_hash_defweak
);
5051 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
5052 if (h
->root
.u
.def
.section
->owner
!= NULL
5053 && (h
->root
.u
.def
.section
->owner
->flags
5054 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
5056 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
5057 if (normal_align
> symbol_align
)
5058 normal_align
= symbol_align
;
5061 normal_align
= symbol_align
;
5065 common_align
= old_alignment
;
5066 common_bfd
= old_bfd
;
5071 common_align
= bfd_log2 (isym
->st_value
);
5073 normal_bfd
= old_bfd
;
5076 if (normal_align
< common_align
)
5078 /* PR binutils/2735 */
5079 if (normal_bfd
== NULL
)
5081 /* xgettext:c-format */
5082 (_("warning: alignment %u of common symbol `%s' in %pB is"
5083 " greater than the alignment (%u) of its section %pA"),
5084 1 << common_align
, name
, common_bfd
,
5085 1 << normal_align
, h
->root
.u
.def
.section
);
5088 /* xgettext:c-format */
5089 (_("warning: alignment %u of symbol `%s' in %pB"
5090 " is smaller than %u in %pB"),
5091 1 << normal_align
, name
, normal_bfd
,
5092 1 << common_align
, common_bfd
);
5096 /* Remember the symbol size if it isn't undefined. */
5097 if (isym
->st_size
!= 0
5098 && isym
->st_shndx
!= SHN_UNDEF
5099 && (definition
|| h
->size
== 0))
5102 && h
->size
!= isym
->st_size
5103 && ! size_change_ok
)
5105 /* xgettext:c-format */
5106 (_("warning: size of symbol `%s' changed"
5107 " from %" PRIu64
" in %pB to %" PRIu64
" in %pB"),
5108 name
, (uint64_t) h
->size
, old_bfd
,
5109 (uint64_t) isym
->st_size
, abfd
);
5111 h
->size
= isym
->st_size
;
5114 /* If this is a common symbol, then we always want H->SIZE
5115 to be the size of the common symbol. The code just above
5116 won't fix the size if a common symbol becomes larger. We
5117 don't warn about a size change here, because that is
5118 covered by --warn-common. Allow changes between different
5120 if (h
->root
.type
== bfd_link_hash_common
)
5121 h
->size
= h
->root
.u
.c
.size
;
5123 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
5124 && ((definition
&& !new_weak
)
5125 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
5126 || h
->type
== STT_NOTYPE
))
5128 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
5130 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5132 if (type
== STT_GNU_IFUNC
5133 && (abfd
->flags
& DYNAMIC
) != 0)
5136 if (h
->type
!= type
)
5138 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
5139 /* xgettext:c-format */
5141 (_("warning: type of symbol `%s' changed"
5142 " from %d to %d in %pB"),
5143 name
, h
->type
, type
, abfd
);
5149 /* Merge st_other field. */
5150 elf_merge_st_other (abfd
, h
, isym
, sec
, definition
, dynamic
);
5152 /* We don't want to make debug symbol dynamic. */
5154 && (sec
->flags
& SEC_DEBUGGING
)
5155 && !bfd_link_relocatable (info
))
5158 /* Nor should we make plugin symbols dynamic. */
5159 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5164 h
->target_internal
= isym
->st_target_internal
;
5165 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
5168 if (definition
&& !dynamic
)
5170 char *p
= strchr (name
, ELF_VER_CHR
);
5171 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
5173 /* Queue non-default versions so that .symver x, x@FOO
5174 aliases can be checked. */
5177 size_t amt
= ((isymend
- isym
+ 1)
5178 * sizeof (struct elf_link_hash_entry
*));
5180 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5182 goto error_free_vers
;
5184 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
5188 if (dynsym
&& h
->dynindx
== -1)
5190 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5191 goto error_free_vers
;
5193 && weakdef (h
)->dynindx
== -1)
5195 if (!bfd_elf_link_record_dynamic_symbol (info
, weakdef (h
)))
5196 goto error_free_vers
;
5199 else if (h
->dynindx
!= -1)
5200 /* If the symbol already has a dynamic index, but
5201 visibility says it should not be visible, turn it into
5203 switch (ELF_ST_VISIBILITY (h
->other
))
5207 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
5216 && h
->ref_regular_nonweak
)
5217 || (h
->ref_dynamic_nonweak
5218 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5219 && !on_needed_list (elf_dt_name (abfd
),
5220 htab
->needed
, NULL
))))
5222 const char *soname
= elf_dt_name (abfd
);
5224 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
5225 h
->root
.root
.string
);
5227 /* A symbol from a library loaded via DT_NEEDED of some
5228 other library is referenced by a regular object.
5229 Add a DT_NEEDED entry for it. Issue an error if
5230 --no-add-needed is used and the reference was not
5233 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
5236 /* xgettext:c-format */
5237 (_("%pB: undefined reference to symbol '%s'"),
5239 bfd_set_error (bfd_error_missing_dso
);
5240 goto error_free_vers
;
5243 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
5244 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
5246 /* Create dynamic sections for backends that require
5247 that be done before setup_gnu_properties. */
5248 if (!_bfd_elf_link_create_dynamic_sections (abfd
, info
))
5255 if (info
->lto_plugin_active
5256 && !bfd_link_relocatable (info
)
5257 && (abfd
->flags
& BFD_PLUGIN
) == 0
5263 if (bed
->s
->arch_size
== 32)
5268 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5269 referenced in regular objects so that linker plugin will get
5270 the correct symbol resolution. */
5272 sym_hash
= elf_sym_hashes (abfd
);
5273 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5275 Elf_Internal_Rela
*internal_relocs
;
5276 Elf_Internal_Rela
*rel
, *relend
;
5278 /* Don't check relocations in excluded sections. */
5279 if ((s
->flags
& SEC_RELOC
) == 0
5280 || s
->reloc_count
== 0
5281 || (s
->flags
& SEC_EXCLUDE
) != 0
5282 || ((info
->strip
== strip_all
5283 || info
->strip
== strip_debugger
)
5284 && (s
->flags
& SEC_DEBUGGING
) != 0))
5287 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, s
, NULL
,
5290 if (internal_relocs
== NULL
)
5291 goto error_free_vers
;
5293 rel
= internal_relocs
;
5294 relend
= rel
+ s
->reloc_count
;
5295 for ( ; rel
< relend
; rel
++)
5297 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
5298 struct elf_link_hash_entry
*h
;
5300 /* Skip local symbols. */
5301 if (r_symndx
< extsymoff
)
5304 h
= sym_hash
[r_symndx
- extsymoff
];
5306 h
->root
.non_ir_ref_regular
= 1;
5309 if (elf_section_data (s
)->relocs
!= internal_relocs
)
5310 free (internal_relocs
);
5319 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
5323 /* Restore the symbol table. */
5324 old_ent
= (char *) old_tab
+ tabsize
;
5325 memset (elf_sym_hashes (abfd
), 0,
5326 extsymcount
* sizeof (struct elf_link_hash_entry
*));
5327 htab
->root
.table
.table
= old_table
;
5328 htab
->root
.table
.size
= old_size
;
5329 htab
->root
.table
.count
= old_count
;
5330 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
5331 htab
->root
.undefs
= old_undefs
;
5332 htab
->root
.undefs_tail
= old_undefs_tail
;
5333 if (htab
->dynstr
!= NULL
)
5334 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
5337 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
5339 struct bfd_hash_entry
*p
;
5340 struct elf_link_hash_entry
*h
;
5342 unsigned int alignment_power
;
5343 unsigned int non_ir_ref_dynamic
;
5345 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
5347 h
= (struct elf_link_hash_entry
*) p
;
5348 if (h
->root
.type
== bfd_link_hash_warning
)
5349 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5351 /* Preserve the maximum alignment and size for common
5352 symbols even if this dynamic lib isn't on DT_NEEDED
5353 since it can still be loaded at run time by another
5355 if (h
->root
.type
== bfd_link_hash_common
)
5357 size
= h
->root
.u
.c
.size
;
5358 alignment_power
= h
->root
.u
.c
.p
->alignment_power
;
5363 alignment_power
= 0;
5365 /* Preserve non_ir_ref_dynamic so that this symbol
5366 will be exported when the dynamic lib becomes needed
5367 in the second pass. */
5368 non_ir_ref_dynamic
= h
->root
.non_ir_ref_dynamic
;
5369 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
5370 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5371 h
= (struct elf_link_hash_entry
*) p
;
5372 if (h
->root
.type
== bfd_link_hash_warning
)
5374 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
5375 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5376 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5378 if (h
->root
.type
== bfd_link_hash_common
)
5380 if (size
> h
->root
.u
.c
.size
)
5381 h
->root
.u
.c
.size
= size
;
5382 if (alignment_power
> h
->root
.u
.c
.p
->alignment_power
)
5383 h
->root
.u
.c
.p
->alignment_power
= alignment_power
;
5385 h
->root
.non_ir_ref_dynamic
= non_ir_ref_dynamic
;
5389 /* Make a special call to the linker "notice" function to
5390 tell it that symbols added for crefs may need to be removed. */
5391 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
5392 goto error_free_vers
;
5395 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
5397 free (nondeflt_vers
);
5401 if (old_tab
!= NULL
)
5403 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
5404 goto error_free_vers
;
5409 /* Now that all the symbols from this input file are created, if
5410 not performing a relocatable link, handle .symver foo, foo@BAR
5411 such that any relocs against foo become foo@BAR. */
5412 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5416 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5418 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5419 char *shortname
, *p
;
5422 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5424 || (h
->root
.type
!= bfd_link_hash_defined
5425 && h
->root
.type
!= bfd_link_hash_defweak
))
5428 amt
= p
- h
->root
.root
.string
;
5429 shortname
= (char *) bfd_malloc (amt
+ 1);
5431 goto error_free_vers
;
5432 memcpy (shortname
, h
->root
.root
.string
, amt
);
5433 shortname
[amt
] = '\0';
5435 hi
= (struct elf_link_hash_entry
*)
5436 bfd_link_hash_lookup (&htab
->root
, shortname
,
5437 FALSE
, FALSE
, FALSE
);
5439 && hi
->root
.type
== h
->root
.type
5440 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5441 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5443 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
5444 hi
->root
.type
= bfd_link_hash_indirect
;
5445 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5446 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5447 sym_hash
= elf_sym_hashes (abfd
);
5449 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5450 if (sym_hash
[symidx
] == hi
)
5452 sym_hash
[symidx
] = h
;
5458 free (nondeflt_vers
);
5459 nondeflt_vers
= NULL
;
5462 /* Now set the alias field correctly for all the weak defined
5463 symbols we found. The only way to do this is to search all the
5464 symbols. Since we only need the information for non functions in
5465 dynamic objects, that's the only time we actually put anything on
5466 the list WEAKS. We need this information so that if a regular
5467 object refers to a symbol defined weakly in a dynamic object, the
5468 real symbol in the dynamic object is also put in the dynamic
5469 symbols; we also must arrange for both symbols to point to the
5470 same memory location. We could handle the general case of symbol
5471 aliasing, but a general symbol alias can only be generated in
5472 assembler code, handling it correctly would be very time
5473 consuming, and other ELF linkers don't handle general aliasing
5477 struct elf_link_hash_entry
**hpp
;
5478 struct elf_link_hash_entry
**hppend
;
5479 struct elf_link_hash_entry
**sorted_sym_hash
;
5480 struct elf_link_hash_entry
*h
;
5481 size_t sym_count
, amt
;
5483 /* Since we have to search the whole symbol list for each weak
5484 defined symbol, search time for N weak defined symbols will be
5485 O(N^2). Binary search will cut it down to O(NlogN). */
5486 amt
= extsymcount
* sizeof (*sorted_sym_hash
);
5487 sorted_sym_hash
= bfd_malloc (amt
);
5488 if (sorted_sym_hash
== NULL
)
5490 sym_hash
= sorted_sym_hash
;
5491 hpp
= elf_sym_hashes (abfd
);
5492 hppend
= hpp
+ extsymcount
;
5494 for (; hpp
< hppend
; hpp
++)
5498 && h
->root
.type
== bfd_link_hash_defined
5499 && !bed
->is_function_type (h
->type
))
5507 qsort (sorted_sym_hash
, sym_count
, sizeof (*sorted_sym_hash
),
5510 while (weaks
!= NULL
)
5512 struct elf_link_hash_entry
*hlook
;
5515 size_t i
, j
, idx
= 0;
5518 weaks
= hlook
->u
.alias
;
5519 hlook
->u
.alias
= NULL
;
5521 if (hlook
->root
.type
!= bfd_link_hash_defined
5522 && hlook
->root
.type
!= bfd_link_hash_defweak
)
5525 slook
= hlook
->root
.u
.def
.section
;
5526 vlook
= hlook
->root
.u
.def
.value
;
5532 bfd_signed_vma vdiff
;
5534 h
= sorted_sym_hash
[idx
];
5535 vdiff
= vlook
- h
->root
.u
.def
.value
;
5542 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5552 /* We didn't find a value/section match. */
5556 /* With multiple aliases, or when the weak symbol is already
5557 strongly defined, we have multiple matching symbols and
5558 the binary search above may land on any of them. Step
5559 one past the matching symbol(s). */
5562 h
= sorted_sym_hash
[idx
];
5563 if (h
->root
.u
.def
.section
!= slook
5564 || h
->root
.u
.def
.value
!= vlook
)
5568 /* Now look back over the aliases. Since we sorted by size
5569 as well as value and section, we'll choose the one with
5570 the largest size. */
5573 h
= sorted_sym_hash
[idx
];
5575 /* Stop if value or section doesn't match. */
5576 if (h
->root
.u
.def
.section
!= slook
5577 || h
->root
.u
.def
.value
!= vlook
)
5579 else if (h
!= hlook
)
5581 struct elf_link_hash_entry
*t
;
5584 hlook
->is_weakalias
= 1;
5586 if (t
->u
.alias
!= NULL
)
5587 while (t
->u
.alias
!= h
)
5591 /* If the weak definition is in the list of dynamic
5592 symbols, make sure the real definition is put
5594 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5596 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5599 free (sorted_sym_hash
);
5604 /* If the real definition is in the list of dynamic
5605 symbols, make sure the weak definition is put
5606 there as well. If we don't do this, then the
5607 dynamic loader might not merge the entries for the
5608 real definition and the weak definition. */
5609 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5611 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5612 goto err_free_sym_hash
;
5619 free (sorted_sym_hash
);
5622 if (bed
->check_directives
5623 && !(*bed
->check_directives
) (abfd
, info
))
5626 /* If this is a non-traditional link, try to optimize the handling
5627 of the .stab/.stabstr sections. */
5629 && ! info
->traditional_format
5630 && is_elf_hash_table (htab
)
5631 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5635 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5636 if (stabstr
!= NULL
)
5638 bfd_size_type string_offset
= 0;
5641 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5642 if (CONST_STRNEQ (stab
->name
, ".stab")
5643 && (!stab
->name
[5] ||
5644 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5645 && (stab
->flags
& SEC_MERGE
) == 0
5646 && !bfd_is_abs_section (stab
->output_section
))
5648 struct bfd_elf_section_data
*secdata
;
5650 secdata
= elf_section_data (stab
);
5651 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5652 stabstr
, &secdata
->sec_info
,
5655 if (secdata
->sec_info
)
5656 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5661 if (dynamic
&& add_needed
)
5663 /* Add this bfd to the loaded list. */
5664 struct elf_link_loaded_list
*n
;
5666 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5670 n
->next
= htab
->dyn_loaded
;
5671 htab
->dyn_loaded
= n
;
5673 if (dynamic
&& !add_needed
5674 && (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) != 0)
5675 elf_dyn_lib_class (abfd
) |= DYN_NO_NEEDED
;
5682 free (nondeflt_vers
);
5690 /* Return the linker hash table entry of a symbol that might be
5691 satisfied by an archive symbol. Return -1 on error. */
5693 struct elf_link_hash_entry
*
5694 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5695 struct bfd_link_info
*info
,
5698 struct elf_link_hash_entry
*h
;
5702 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, TRUE
);
5706 /* If this is a default version (the name contains @@), look up the
5707 symbol again with only one `@' as well as without the version.
5708 The effect is that references to the symbol with and without the
5709 version will be matched by the default symbol in the archive. */
5711 p
= strchr (name
, ELF_VER_CHR
);
5712 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5715 /* First check with only one `@'. */
5716 len
= strlen (name
);
5717 copy
= (char *) bfd_alloc (abfd
, len
);
5719 return (struct elf_link_hash_entry
*) -1;
5721 first
= p
- name
+ 1;
5722 memcpy (copy
, name
, first
);
5723 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5725 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, TRUE
);
5728 /* We also need to check references to the symbol without the
5730 copy
[first
- 1] = '\0';
5731 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
5732 FALSE
, FALSE
, TRUE
);
5735 bfd_release (abfd
, copy
);
5739 /* Add symbols from an ELF archive file to the linker hash table. We
5740 don't use _bfd_generic_link_add_archive_symbols because we need to
5741 handle versioned symbols.
5743 Fortunately, ELF archive handling is simpler than that done by
5744 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5745 oddities. In ELF, if we find a symbol in the archive map, and the
5746 symbol is currently undefined, we know that we must pull in that
5749 Unfortunately, we do have to make multiple passes over the symbol
5750 table until nothing further is resolved. */
5753 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5756 unsigned char *included
= NULL
;
5760 const struct elf_backend_data
*bed
;
5761 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
5762 (bfd
*, struct bfd_link_info
*, const char *);
5764 if (! bfd_has_map (abfd
))
5766 /* An empty archive is a special case. */
5767 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5769 bfd_set_error (bfd_error_no_armap
);
5773 /* Keep track of all symbols we know to be already defined, and all
5774 files we know to be already included. This is to speed up the
5775 second and subsequent passes. */
5776 c
= bfd_ardata (abfd
)->symdef_count
;
5779 amt
= c
* sizeof (*included
);
5780 included
= (unsigned char *) bfd_zmalloc (amt
);
5781 if (included
== NULL
)
5784 symdefs
= bfd_ardata (abfd
)->symdefs
;
5785 bed
= get_elf_backend_data (abfd
);
5786 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5799 symdefend
= symdef
+ c
;
5800 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5802 struct elf_link_hash_entry
*h
;
5804 struct bfd_link_hash_entry
*undefs_tail
;
5809 if (symdef
->file_offset
== last
)
5815 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5816 if (h
== (struct elf_link_hash_entry
*) -1)
5822 if (h
->root
.type
== bfd_link_hash_undefined
)
5824 /* If the archive element has already been loaded then one
5825 of the symbols defined by that element might have been
5826 made undefined due to being in a discarded section. */
5830 else if (h
->root
.type
== bfd_link_hash_common
)
5832 /* We currently have a common symbol. The archive map contains
5833 a reference to this symbol, so we may want to include it. We
5834 only want to include it however, if this archive element
5835 contains a definition of the symbol, not just another common
5838 Unfortunately some archivers (including GNU ar) will put
5839 declarations of common symbols into their archive maps, as
5840 well as real definitions, so we cannot just go by the archive
5841 map alone. Instead we must read in the element's symbol
5842 table and check that to see what kind of symbol definition
5844 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5849 if (h
->root
.type
!= bfd_link_hash_undefweak
)
5850 /* Symbol must be defined. Don't check it again. */
5855 /* We need to include this archive member. */
5856 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5857 if (element
== NULL
)
5860 if (! bfd_check_format (element
, bfd_object
))
5863 undefs_tail
= info
->hash
->undefs_tail
;
5865 if (!(*info
->callbacks
5866 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5868 if (!bfd_link_add_symbols (element
, info
))
5871 /* If there are any new undefined symbols, we need to make
5872 another pass through the archive in order to see whether
5873 they can be defined. FIXME: This isn't perfect, because
5874 common symbols wind up on undefs_tail and because an
5875 undefined symbol which is defined later on in this pass
5876 does not require another pass. This isn't a bug, but it
5877 does make the code less efficient than it could be. */
5878 if (undefs_tail
!= info
->hash
->undefs_tail
)
5881 /* Look backward to mark all symbols from this object file
5882 which we have already seen in this pass. */
5886 included
[mark
] = TRUE
;
5891 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5893 /* We mark subsequent symbols from this object file as we go
5894 on through the loop. */
5895 last
= symdef
->file_offset
;
5908 /* Given an ELF BFD, add symbols to the global hash table as
5912 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5914 switch (bfd_get_format (abfd
))
5917 return elf_link_add_object_symbols (abfd
, info
);
5919 return elf_link_add_archive_symbols (abfd
, info
);
5921 bfd_set_error (bfd_error_wrong_format
);
5926 struct hash_codes_info
5928 unsigned long *hashcodes
;
5932 /* This function will be called though elf_link_hash_traverse to store
5933 all hash value of the exported symbols in an array. */
5936 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5938 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
5943 /* Ignore indirect symbols. These are added by the versioning code. */
5944 if (h
->dynindx
== -1)
5947 name
= h
->root
.root
.string
;
5948 if (h
->versioned
>= versioned
)
5950 char *p
= strchr (name
, ELF_VER_CHR
);
5953 alc
= (char *) bfd_malloc (p
- name
+ 1);
5959 memcpy (alc
, name
, p
- name
);
5960 alc
[p
- name
] = '\0';
5965 /* Compute the hash value. */
5966 ha
= bfd_elf_hash (name
);
5968 /* Store the found hash value in the array given as the argument. */
5969 *(inf
->hashcodes
)++ = ha
;
5971 /* And store it in the struct so that we can put it in the hash table
5973 h
->u
.elf_hash_value
= ha
;
5979 struct collect_gnu_hash_codes
5982 const struct elf_backend_data
*bed
;
5983 unsigned long int nsyms
;
5984 unsigned long int maskbits
;
5985 unsigned long int *hashcodes
;
5986 unsigned long int *hashval
;
5987 unsigned long int *indx
;
5988 unsigned long int *counts
;
5992 long int min_dynindx
;
5993 unsigned long int bucketcount
;
5994 unsigned long int symindx
;
5995 long int local_indx
;
5996 long int shift1
, shift2
;
5997 unsigned long int mask
;
6001 /* This function will be called though elf_link_hash_traverse to store
6002 all hash value of the exported symbols in an array. */
6005 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6007 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6012 /* Ignore indirect symbols. These are added by the versioning code. */
6013 if (h
->dynindx
== -1)
6016 /* Ignore also local symbols and undefined symbols. */
6017 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6020 name
= h
->root
.root
.string
;
6021 if (h
->versioned
>= versioned
)
6023 char *p
= strchr (name
, ELF_VER_CHR
);
6026 alc
= (char *) bfd_malloc (p
- name
+ 1);
6032 memcpy (alc
, name
, p
- name
);
6033 alc
[p
- name
] = '\0';
6038 /* Compute the hash value. */
6039 ha
= bfd_elf_gnu_hash (name
);
6041 /* Store the found hash value in the array for compute_bucket_count,
6042 and also for .dynsym reordering purposes. */
6043 s
->hashcodes
[s
->nsyms
] = ha
;
6044 s
->hashval
[h
->dynindx
] = ha
;
6046 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
6047 s
->min_dynindx
= h
->dynindx
;
6053 /* This function will be called though elf_link_hash_traverse to do
6054 final dynamic symbol renumbering in case of .gnu.hash.
6055 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6056 to the translation table. */
6059 elf_gnu_hash_process_symidx (struct elf_link_hash_entry
*h
, void *data
)
6061 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6062 unsigned long int bucket
;
6063 unsigned long int val
;
6065 /* Ignore indirect symbols. */
6066 if (h
->dynindx
== -1)
6069 /* Ignore also local symbols and undefined symbols. */
6070 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6072 if (h
->dynindx
>= s
->min_dynindx
)
6074 if (s
->bed
->record_xhash_symbol
!= NULL
)
6076 (*s
->bed
->record_xhash_symbol
) (h
, 0);
6080 h
->dynindx
= s
->local_indx
++;
6085 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
6086 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
6087 & ((s
->maskbits
>> s
->shift1
) - 1);
6088 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
6090 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
6091 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
6092 if (s
->counts
[bucket
] == 1)
6093 /* Last element terminates the chain. */
6095 bfd_put_32 (s
->output_bfd
, val
,
6096 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
6097 --s
->counts
[bucket
];
6098 if (s
->bed
->record_xhash_symbol
!= NULL
)
6100 bfd_vma xlat_loc
= s
->xlat
+ (s
->indx
[bucket
]++ - s
->symindx
) * 4;
6102 (*s
->bed
->record_xhash_symbol
) (h
, xlat_loc
);
6105 h
->dynindx
= s
->indx
[bucket
]++;
6109 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6112 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
6114 return !(h
->forced_local
6115 || h
->root
.type
== bfd_link_hash_undefined
6116 || h
->root
.type
== bfd_link_hash_undefweak
6117 || ((h
->root
.type
== bfd_link_hash_defined
6118 || h
->root
.type
== bfd_link_hash_defweak
)
6119 && h
->root
.u
.def
.section
->output_section
== NULL
));
6122 /* Array used to determine the number of hash table buckets to use
6123 based on the number of symbols there are. If there are fewer than
6124 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6125 fewer than 37 we use 17 buckets, and so forth. We never use more
6126 than 32771 buckets. */
6128 static const size_t elf_buckets
[] =
6130 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6134 /* Compute bucket count for hashing table. We do not use a static set
6135 of possible tables sizes anymore. Instead we determine for all
6136 possible reasonable sizes of the table the outcome (i.e., the
6137 number of collisions etc) and choose the best solution. The
6138 weighting functions are not too simple to allow the table to grow
6139 without bounds. Instead one of the weighting factors is the size.
6140 Therefore the result is always a good payoff between few collisions
6141 (= short chain lengths) and table size. */
6143 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6144 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
6145 unsigned long int nsyms
,
6148 size_t best_size
= 0;
6149 unsigned long int i
;
6151 /* We have a problem here. The following code to optimize the table
6152 size requires an integer type with more the 32 bits. If
6153 BFD_HOST_U_64_BIT is set we know about such a type. */
6154 #ifdef BFD_HOST_U_64_BIT
6159 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
6160 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
6161 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
6162 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
6163 unsigned long int *counts
;
6165 unsigned int no_improvement_count
= 0;
6167 /* Possible optimization parameters: if we have NSYMS symbols we say
6168 that the hashing table must at least have NSYMS/4 and at most
6170 minsize
= nsyms
/ 4;
6173 best_size
= maxsize
= nsyms
* 2;
6178 if ((best_size
& 31) == 0)
6182 /* Create array where we count the collisions in. We must use bfd_malloc
6183 since the size could be large. */
6185 amt
*= sizeof (unsigned long int);
6186 counts
= (unsigned long int *) bfd_malloc (amt
);
6190 /* Compute the "optimal" size for the hash table. The criteria is a
6191 minimal chain length. The minor criteria is (of course) the size
6193 for (i
= minsize
; i
< maxsize
; ++i
)
6195 /* Walk through the array of hashcodes and count the collisions. */
6196 BFD_HOST_U_64_BIT max
;
6197 unsigned long int j
;
6198 unsigned long int fact
;
6200 if (gnu_hash
&& (i
& 31) == 0)
6203 memset (counts
, '\0', i
* sizeof (unsigned long int));
6205 /* Determine how often each hash bucket is used. */
6206 for (j
= 0; j
< nsyms
; ++j
)
6207 ++counts
[hashcodes
[j
] % i
];
6209 /* For the weight function we need some information about the
6210 pagesize on the target. This is information need not be 100%
6211 accurate. Since this information is not available (so far) we
6212 define it here to a reasonable default value. If it is crucial
6213 to have a better value some day simply define this value. */
6214 # ifndef BFD_TARGET_PAGESIZE
6215 # define BFD_TARGET_PAGESIZE (4096)
6218 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6220 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
6223 /* Variant 1: optimize for short chains. We add the squares
6224 of all the chain lengths (which favors many small chain
6225 over a few long chains). */
6226 for (j
= 0; j
< i
; ++j
)
6227 max
+= counts
[j
] * counts
[j
];
6229 /* This adds penalties for the overall size of the table. */
6230 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6233 /* Variant 2: Optimize a lot more for small table. Here we
6234 also add squares of the size but we also add penalties for
6235 empty slots (the +1 term). */
6236 for (j
= 0; j
< i
; ++j
)
6237 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
6239 /* The overall size of the table is considered, but not as
6240 strong as in variant 1, where it is squared. */
6241 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6245 /* Compare with current best results. */
6246 if (max
< best_chlen
)
6250 no_improvement_count
= 0;
6252 /* PR 11843: Avoid futile long searches for the best bucket size
6253 when there are a large number of symbols. */
6254 else if (++no_improvement_count
== 100)
6261 #endif /* defined (BFD_HOST_U_64_BIT) */
6263 /* This is the fallback solution if no 64bit type is available or if we
6264 are not supposed to spend much time on optimizations. We select the
6265 bucket count using a fixed set of numbers. */
6266 for (i
= 0; elf_buckets
[i
] != 0; i
++)
6268 best_size
= elf_buckets
[i
];
6269 if (nsyms
< elf_buckets
[i
+ 1])
6272 if (gnu_hash
&& best_size
< 2)
6279 /* Size any SHT_GROUP section for ld -r. */
6282 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
6287 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6288 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6289 && (s
= ibfd
->sections
) != NULL
6290 && s
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
6291 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
6296 /* Set a default stack segment size. The value in INFO wins. If it
6297 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6298 undefined it is initialized. */
6301 bfd_elf_stack_segment_size (bfd
*output_bfd
,
6302 struct bfd_link_info
*info
,
6303 const char *legacy_symbol
,
6304 bfd_vma default_size
)
6306 struct elf_link_hash_entry
*h
= NULL
;
6308 /* Look for legacy symbol. */
6310 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
6311 FALSE
, FALSE
, FALSE
);
6312 if (h
&& (h
->root
.type
== bfd_link_hash_defined
6313 || h
->root
.type
== bfd_link_hash_defweak
)
6315 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
6317 /* The symbol has no type if specified on the command line. */
6318 h
->type
= STT_OBJECT
;
6319 if (info
->stacksize
)
6320 /* xgettext:c-format */
6321 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6322 output_bfd
, legacy_symbol
);
6323 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
6324 /* xgettext:c-format */
6325 _bfd_error_handler (_("%pB: %s not absolute"),
6326 output_bfd
, legacy_symbol
);
6328 info
->stacksize
= h
->root
.u
.def
.value
;
6331 if (!info
->stacksize
)
6332 /* If the user didn't set a size, or explicitly inhibit the
6333 size, set it now. */
6334 info
->stacksize
= default_size
;
6336 /* Provide the legacy symbol, if it is referenced. */
6337 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
6338 || h
->root
.type
== bfd_link_hash_undefweak
))
6340 struct bfd_link_hash_entry
*bh
= NULL
;
6342 if (!(_bfd_generic_link_add_one_symbol
6343 (info
, output_bfd
, legacy_symbol
,
6344 BSF_GLOBAL
, bfd_abs_section_ptr
,
6345 info
->stacksize
>= 0 ? info
->stacksize
: 0,
6346 NULL
, FALSE
, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
6349 h
= (struct elf_link_hash_entry
*) bh
;
6351 h
->type
= STT_OBJECT
;
6357 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6359 struct elf_gc_sweep_symbol_info
6361 struct bfd_link_info
*info
;
6362 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
6367 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
6370 && (((h
->root
.type
== bfd_link_hash_defined
6371 || h
->root
.type
== bfd_link_hash_defweak
)
6372 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
6373 && h
->root
.u
.def
.section
->gc_mark
))
6374 || h
->root
.type
== bfd_link_hash_undefined
6375 || h
->root
.type
== bfd_link_hash_undefweak
))
6377 struct elf_gc_sweep_symbol_info
*inf
;
6379 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
6380 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
6383 h
->ref_regular_nonweak
= 0;
6389 /* Set up the sizes and contents of the ELF dynamic sections. This is
6390 called by the ELF linker emulation before_allocation routine. We
6391 must set the sizes of the sections before the linker sets the
6392 addresses of the various sections. */
6395 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
6398 const char *filter_shlib
,
6400 const char *depaudit
,
6401 const char * const *auxiliary_filters
,
6402 struct bfd_link_info
*info
,
6403 asection
**sinterpptr
)
6406 const struct elf_backend_data
*bed
;
6410 if (!is_elf_hash_table (info
->hash
))
6413 dynobj
= elf_hash_table (info
)->dynobj
;
6415 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6417 struct bfd_elf_version_tree
*verdefs
;
6418 struct elf_info_failed asvinfo
;
6419 struct bfd_elf_version_tree
*t
;
6420 struct bfd_elf_version_expr
*d
;
6424 /* If we are supposed to export all symbols into the dynamic symbol
6425 table (this is not the normal case), then do so. */
6426 if (info
->export_dynamic
6427 || (bfd_link_executable (info
) && info
->dynamic
))
6429 struct elf_info_failed eif
;
6433 elf_link_hash_traverse (elf_hash_table (info
),
6434 _bfd_elf_export_symbol
,
6442 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6444 if (soname_indx
== (size_t) -1
6445 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6449 soname_indx
= (size_t) -1;
6451 /* Make all global versions with definition. */
6452 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6453 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6454 if (!d
->symver
&& d
->literal
)
6456 const char *verstr
, *name
;
6457 size_t namelen
, verlen
, newlen
;
6458 char *newname
, *p
, leading_char
;
6459 struct elf_link_hash_entry
*newh
;
6461 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6463 namelen
= strlen (name
) + (leading_char
!= '\0');
6465 verlen
= strlen (verstr
);
6466 newlen
= namelen
+ verlen
+ 3;
6468 newname
= (char *) bfd_malloc (newlen
);
6469 if (newname
== NULL
)
6471 newname
[0] = leading_char
;
6472 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6474 /* Check the hidden versioned definition. */
6475 p
= newname
+ namelen
;
6477 memcpy (p
, verstr
, verlen
+ 1);
6478 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6479 newname
, FALSE
, FALSE
,
6482 || (newh
->root
.type
!= bfd_link_hash_defined
6483 && newh
->root
.type
!= bfd_link_hash_defweak
))
6485 /* Check the default versioned definition. */
6487 memcpy (p
, verstr
, verlen
+ 1);
6488 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6489 newname
, FALSE
, FALSE
,
6494 /* Mark this version if there is a definition and it is
6495 not defined in a shared object. */
6497 && !newh
->def_dynamic
6498 && (newh
->root
.type
== bfd_link_hash_defined
6499 || newh
->root
.type
== bfd_link_hash_defweak
))
6503 /* Attach all the symbols to their version information. */
6504 asvinfo
.info
= info
;
6505 asvinfo
.failed
= FALSE
;
6507 elf_link_hash_traverse (elf_hash_table (info
),
6508 _bfd_elf_link_assign_sym_version
,
6513 if (!info
->allow_undefined_version
)
6515 /* Check if all global versions have a definition. */
6516 bfd_boolean all_defined
= TRUE
;
6517 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6518 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6519 if (d
->literal
&& !d
->symver
&& !d
->script
)
6522 (_("%s: undefined version: %s"),
6523 d
->pattern
, t
->name
);
6524 all_defined
= FALSE
;
6529 bfd_set_error (bfd_error_bad_value
);
6534 /* Set up the version definition section. */
6535 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6536 BFD_ASSERT (s
!= NULL
);
6538 /* We may have created additional version definitions if we are
6539 just linking a regular application. */
6540 verdefs
= info
->version_info
;
6542 /* Skip anonymous version tag. */
6543 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6544 verdefs
= verdefs
->next
;
6546 if (verdefs
== NULL
&& !info
->create_default_symver
)
6547 s
->flags
|= SEC_EXCLUDE
;
6553 Elf_Internal_Verdef def
;
6554 Elf_Internal_Verdaux defaux
;
6555 struct bfd_link_hash_entry
*bh
;
6556 struct elf_link_hash_entry
*h
;
6562 /* Make space for the base version. */
6563 size
+= sizeof (Elf_External_Verdef
);
6564 size
+= sizeof (Elf_External_Verdaux
);
6567 /* Make space for the default version. */
6568 if (info
->create_default_symver
)
6570 size
+= sizeof (Elf_External_Verdef
);
6574 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6576 struct bfd_elf_version_deps
*n
;
6578 /* Don't emit base version twice. */
6582 size
+= sizeof (Elf_External_Verdef
);
6583 size
+= sizeof (Elf_External_Verdaux
);
6586 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6587 size
+= sizeof (Elf_External_Verdaux
);
6591 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6592 if (s
->contents
== NULL
&& s
->size
!= 0)
6595 /* Fill in the version definition section. */
6599 def
.vd_version
= VER_DEF_CURRENT
;
6600 def
.vd_flags
= VER_FLG_BASE
;
6603 if (info
->create_default_symver
)
6605 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6606 def
.vd_next
= sizeof (Elf_External_Verdef
);
6610 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6611 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6612 + sizeof (Elf_External_Verdaux
));
6615 if (soname_indx
!= (size_t) -1)
6617 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6619 def
.vd_hash
= bfd_elf_hash (soname
);
6620 defaux
.vda_name
= soname_indx
;
6627 name
= lbasename (bfd_get_filename (output_bfd
));
6628 def
.vd_hash
= bfd_elf_hash (name
);
6629 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6631 if (indx
== (size_t) -1)
6633 defaux
.vda_name
= indx
;
6635 defaux
.vda_next
= 0;
6637 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6638 (Elf_External_Verdef
*) p
);
6639 p
+= sizeof (Elf_External_Verdef
);
6640 if (info
->create_default_symver
)
6642 /* Add a symbol representing this version. */
6644 if (! (_bfd_generic_link_add_one_symbol
6645 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6647 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6649 h
= (struct elf_link_hash_entry
*) bh
;
6652 h
->type
= STT_OBJECT
;
6653 h
->verinfo
.vertree
= NULL
;
6655 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6658 /* Create a duplicate of the base version with the same
6659 aux block, but different flags. */
6662 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6664 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6665 + sizeof (Elf_External_Verdaux
));
6668 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6669 (Elf_External_Verdef
*) p
);
6670 p
+= sizeof (Elf_External_Verdef
);
6672 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6673 (Elf_External_Verdaux
*) p
);
6674 p
+= sizeof (Elf_External_Verdaux
);
6676 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6679 struct bfd_elf_version_deps
*n
;
6681 /* Don't emit the base version twice. */
6686 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6689 /* Add a symbol representing this version. */
6691 if (! (_bfd_generic_link_add_one_symbol
6692 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6694 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6696 h
= (struct elf_link_hash_entry
*) bh
;
6699 h
->type
= STT_OBJECT
;
6700 h
->verinfo
.vertree
= t
;
6702 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6705 def
.vd_version
= VER_DEF_CURRENT
;
6707 if (t
->globals
.list
== NULL
6708 && t
->locals
.list
== NULL
6710 def
.vd_flags
|= VER_FLG_WEAK
;
6711 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6712 def
.vd_cnt
= cdeps
+ 1;
6713 def
.vd_hash
= bfd_elf_hash (t
->name
);
6714 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6717 /* If a basever node is next, it *must* be the last node in
6718 the chain, otherwise Verdef construction breaks. */
6719 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6720 BFD_ASSERT (t
->next
->next
== NULL
);
6722 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6723 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6724 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6726 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6727 (Elf_External_Verdef
*) p
);
6728 p
+= sizeof (Elf_External_Verdef
);
6730 defaux
.vda_name
= h
->dynstr_index
;
6731 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6733 defaux
.vda_next
= 0;
6734 if (t
->deps
!= NULL
)
6735 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6736 t
->name_indx
= defaux
.vda_name
;
6738 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6739 (Elf_External_Verdaux
*) p
);
6740 p
+= sizeof (Elf_External_Verdaux
);
6742 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6744 if (n
->version_needed
== NULL
)
6746 /* This can happen if there was an error in the
6748 defaux
.vda_name
= 0;
6752 defaux
.vda_name
= n
->version_needed
->name_indx
;
6753 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6756 if (n
->next
== NULL
)
6757 defaux
.vda_next
= 0;
6759 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6761 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6762 (Elf_External_Verdaux
*) p
);
6763 p
+= sizeof (Elf_External_Verdaux
);
6767 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6771 bed
= get_elf_backend_data (output_bfd
);
6773 if (info
->gc_sections
&& bed
->can_gc_sections
)
6775 struct elf_gc_sweep_symbol_info sweep_info
;
6777 /* Remove the symbols that were in the swept sections from the
6778 dynamic symbol table. */
6779 sweep_info
.info
= info
;
6780 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
6781 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
6785 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6788 struct elf_find_verdep_info sinfo
;
6790 /* Work out the size of the version reference section. */
6792 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6793 BFD_ASSERT (s
!= NULL
);
6796 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6797 if (sinfo
.vers
== 0)
6799 sinfo
.failed
= FALSE
;
6801 elf_link_hash_traverse (elf_hash_table (info
),
6802 _bfd_elf_link_find_version_dependencies
,
6807 if (elf_tdata (output_bfd
)->verref
== NULL
)
6808 s
->flags
|= SEC_EXCLUDE
;
6811 Elf_Internal_Verneed
*vn
;
6816 /* Build the version dependency section. */
6819 for (vn
= elf_tdata (output_bfd
)->verref
;
6821 vn
= vn
->vn_nextref
)
6823 Elf_Internal_Vernaux
*a
;
6825 size
+= sizeof (Elf_External_Verneed
);
6827 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6828 size
+= sizeof (Elf_External_Vernaux
);
6832 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6833 if (s
->contents
== NULL
)
6837 for (vn
= elf_tdata (output_bfd
)->verref
;
6839 vn
= vn
->vn_nextref
)
6842 Elf_Internal_Vernaux
*a
;
6846 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6849 vn
->vn_version
= VER_NEED_CURRENT
;
6851 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6852 elf_dt_name (vn
->vn_bfd
) != NULL
6853 ? elf_dt_name (vn
->vn_bfd
)
6854 : lbasename (bfd_get_filename
6857 if (indx
== (size_t) -1)
6860 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
6861 if (vn
->vn_nextref
== NULL
)
6864 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
6865 + caux
* sizeof (Elf_External_Vernaux
));
6867 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
6868 (Elf_External_Verneed
*) p
);
6869 p
+= sizeof (Elf_External_Verneed
);
6871 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6873 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6874 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6875 a
->vna_nodename
, FALSE
);
6876 if (indx
== (size_t) -1)
6879 if (a
->vna_nextptr
== NULL
)
6882 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6884 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6885 (Elf_External_Vernaux
*) p
);
6886 p
+= sizeof (Elf_External_Vernaux
);
6890 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6894 /* Any syms created from now on start with -1 in
6895 got.refcount/offset and plt.refcount/offset. */
6896 elf_hash_table (info
)->init_got_refcount
6897 = elf_hash_table (info
)->init_got_offset
;
6898 elf_hash_table (info
)->init_plt_refcount
6899 = elf_hash_table (info
)->init_plt_offset
;
6901 if (bfd_link_relocatable (info
)
6902 && !_bfd_elf_size_group_sections (info
))
6905 /* The backend may have to create some sections regardless of whether
6906 we're dynamic or not. */
6907 if (bed
->elf_backend_always_size_sections
6908 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
6911 /* Determine any GNU_STACK segment requirements, after the backend
6912 has had a chance to set a default segment size. */
6913 if (info
->execstack
)
6914 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
6915 else if (info
->noexecstack
)
6916 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
6920 asection
*notesec
= NULL
;
6923 for (inputobj
= info
->input_bfds
;
6925 inputobj
= inputobj
->link
.next
)
6930 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
6932 s
= inputobj
->sections
;
6933 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
6936 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
6939 if (s
->flags
& SEC_CODE
)
6943 else if (bed
->default_execstack
)
6946 if (notesec
|| info
->stacksize
> 0)
6947 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
6948 if (notesec
&& exec
&& bfd_link_relocatable (info
)
6949 && notesec
->output_section
!= bfd_abs_section_ptr
)
6950 notesec
->output_section
->flags
|= SEC_CODE
;
6953 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6955 struct elf_info_failed eif
;
6956 struct elf_link_hash_entry
*h
;
6960 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
6961 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
6965 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
6967 info
->flags
|= DF_SYMBOLIC
;
6975 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
6977 if (indx
== (size_t) -1)
6980 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
6981 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
6985 if (filter_shlib
!= NULL
)
6989 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6990 filter_shlib
, TRUE
);
6991 if (indx
== (size_t) -1
6992 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
6996 if (auxiliary_filters
!= NULL
)
6998 const char * const *p
;
7000 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
7004 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7006 if (indx
== (size_t) -1
7007 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
7016 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
7018 if (indx
== (size_t) -1
7019 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
7023 if (depaudit
!= NULL
)
7027 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
7029 if (indx
== (size_t) -1
7030 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
7037 /* Find all symbols which were defined in a dynamic object and make
7038 the backend pick a reasonable value for them. */
7039 elf_link_hash_traverse (elf_hash_table (info
),
7040 _bfd_elf_adjust_dynamic_symbol
,
7045 /* Add some entries to the .dynamic section. We fill in some of the
7046 values later, in bfd_elf_final_link, but we must add the entries
7047 now so that we know the final size of the .dynamic section. */
7049 /* If there are initialization and/or finalization functions to
7050 call then add the corresponding DT_INIT/DT_FINI entries. */
7051 h
= (info
->init_function
7052 ? elf_link_hash_lookup (elf_hash_table (info
),
7053 info
->init_function
, FALSE
,
7060 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
7063 h
= (info
->fini_function
7064 ? elf_link_hash_lookup (elf_hash_table (info
),
7065 info
->fini_function
, FALSE
,
7072 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
7076 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
7077 if (s
!= NULL
&& s
->linker_has_input
)
7079 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7080 if (! bfd_link_executable (info
))
7085 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
7086 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
7087 && (o
= sub
->sections
) != NULL
7088 && o
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
)
7089 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7090 if (elf_section_data (o
)->this_hdr
.sh_type
7091 == SHT_PREINIT_ARRAY
)
7094 (_("%pB: .preinit_array section is not allowed in DSO"),
7099 bfd_set_error (bfd_error_nonrepresentable_section
);
7103 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
7104 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
7107 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
7108 if (s
!= NULL
&& s
->linker_has_input
)
7110 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
7111 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
7114 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
7115 if (s
!= NULL
&& s
->linker_has_input
)
7117 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
7118 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
7122 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
7123 /* If .dynstr is excluded from the link, we don't want any of
7124 these tags. Strictly, we should be checking each section
7125 individually; This quick check covers for the case where
7126 someone does a /DISCARD/ : { *(*) }. */
7127 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
7129 bfd_size_type strsize
;
7131 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7132 if ((info
->emit_hash
7133 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
7134 || (info
->emit_gnu_hash
7135 && (bed
->record_xhash_symbol
== NULL
7136 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0)))
7137 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
7138 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
7139 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
7140 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
7141 bed
->s
->sizeof_sym
))
7146 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
7149 /* The backend must work out the sizes of all the other dynamic
7152 && bed
->elf_backend_size_dynamic_sections
!= NULL
7153 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
7156 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7158 if (elf_tdata (output_bfd
)->cverdefs
)
7160 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
7162 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
7163 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
7167 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
7169 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
7172 else if (info
->flags
& DF_BIND_NOW
)
7174 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
7180 if (bfd_link_executable (info
))
7181 info
->flags_1
&= ~ (DF_1_INITFIRST
7184 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
7188 if (elf_tdata (output_bfd
)->cverrefs
)
7190 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
7192 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
7193 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
7197 if ((elf_tdata (output_bfd
)->cverrefs
== 0
7198 && elf_tdata (output_bfd
)->cverdefs
== 0)
7199 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, NULL
) <= 1)
7203 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7204 s
->flags
|= SEC_EXCLUDE
;
7210 /* Find the first non-excluded output section. We'll use its
7211 section symbol for some emitted relocs. */
7213 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
7216 asection
*found
= NULL
;
7218 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7219 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7220 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7223 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7226 elf_hash_table (info
)->text_index_section
= found
;
7229 /* Find two non-excluded output sections, one for code, one for data.
7230 We'll use their section symbols for some emitted relocs. */
7232 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
7235 asection
*found
= NULL
;
7237 /* Data first, since setting text_index_section changes
7238 _bfd_elf_omit_section_dynsym_default. */
7239 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7240 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7241 && !(s
->flags
& SEC_READONLY
)
7242 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7245 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7248 elf_hash_table (info
)->data_index_section
= found
;
7250 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7251 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7252 && (s
->flags
& SEC_READONLY
)
7253 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7258 elf_hash_table (info
)->text_index_section
= found
;
7261 #define GNU_HASH_SECTION_NAME(bed) \
7262 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7265 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
7267 const struct elf_backend_data
*bed
;
7268 unsigned long section_sym_count
;
7269 bfd_size_type dynsymcount
= 0;
7271 if (!is_elf_hash_table (info
->hash
))
7274 bed
= get_elf_backend_data (output_bfd
);
7275 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
7277 /* Assign dynsym indices. In a shared library we generate a section
7278 symbol for each output section, which come first. Next come all
7279 of the back-end allocated local dynamic syms, followed by the rest
7280 of the global symbols.
7282 This is usually not needed for static binaries, however backends
7283 can request to always do it, e.g. the MIPS backend uses dynamic
7284 symbol counts to lay out GOT, which will be produced in the
7285 presence of GOT relocations even in static binaries (holding fixed
7286 data in that case, to satisfy those relocations). */
7288 if (elf_hash_table (info
)->dynamic_sections_created
7289 || bed
->always_renumber_dynsyms
)
7290 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
7291 §ion_sym_count
);
7293 if (elf_hash_table (info
)->dynamic_sections_created
)
7297 unsigned int dtagcount
;
7299 dynobj
= elf_hash_table (info
)->dynobj
;
7301 /* Work out the size of the symbol version section. */
7302 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7303 BFD_ASSERT (s
!= NULL
);
7304 if ((s
->flags
& SEC_EXCLUDE
) == 0)
7306 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
7307 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7308 if (s
->contents
== NULL
)
7311 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
7315 /* Set the size of the .dynsym and .hash sections. We counted
7316 the number of dynamic symbols in elf_link_add_object_symbols.
7317 We will build the contents of .dynsym and .hash when we build
7318 the final symbol table, because until then we do not know the
7319 correct value to give the symbols. We built the .dynstr
7320 section as we went along in elf_link_add_object_symbols. */
7321 s
= elf_hash_table (info
)->dynsym
;
7322 BFD_ASSERT (s
!= NULL
);
7323 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
7325 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7326 if (s
->contents
== NULL
)
7329 /* The first entry in .dynsym is a dummy symbol. Clear all the
7330 section syms, in case we don't output them all. */
7331 ++section_sym_count
;
7332 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
7334 elf_hash_table (info
)->bucketcount
= 0;
7336 /* Compute the size of the hashing table. As a side effect this
7337 computes the hash values for all the names we export. */
7338 if (info
->emit_hash
)
7340 unsigned long int *hashcodes
;
7341 struct hash_codes_info hashinf
;
7343 unsigned long int nsyms
;
7345 size_t hash_entry_size
;
7347 /* Compute the hash values for all exported symbols. At the same
7348 time store the values in an array so that we could use them for
7350 amt
= dynsymcount
* sizeof (unsigned long int);
7351 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
7352 if (hashcodes
== NULL
)
7354 hashinf
.hashcodes
= hashcodes
;
7355 hashinf
.error
= FALSE
;
7357 /* Put all hash values in HASHCODES. */
7358 elf_link_hash_traverse (elf_hash_table (info
),
7359 elf_collect_hash_codes
, &hashinf
);
7366 nsyms
= hashinf
.hashcodes
- hashcodes
;
7368 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
7371 if (bucketcount
== 0 && nsyms
> 0)
7374 elf_hash_table (info
)->bucketcount
= bucketcount
;
7376 s
= bfd_get_linker_section (dynobj
, ".hash");
7377 BFD_ASSERT (s
!= NULL
);
7378 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
7379 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
7380 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7381 if (s
->contents
== NULL
)
7384 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
7385 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
7386 s
->contents
+ hash_entry_size
);
7389 if (info
->emit_gnu_hash
)
7392 unsigned char *contents
;
7393 struct collect_gnu_hash_codes cinfo
;
7397 memset (&cinfo
, 0, sizeof (cinfo
));
7399 /* Compute the hash values for all exported symbols. At the same
7400 time store the values in an array so that we could use them for
7402 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
7403 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
7404 if (cinfo
.hashcodes
== NULL
)
7407 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
7408 cinfo
.min_dynindx
= -1;
7409 cinfo
.output_bfd
= output_bfd
;
7412 /* Put all hash values in HASHCODES. */
7413 elf_link_hash_traverse (elf_hash_table (info
),
7414 elf_collect_gnu_hash_codes
, &cinfo
);
7417 free (cinfo
.hashcodes
);
7422 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
7424 if (bucketcount
== 0)
7426 free (cinfo
.hashcodes
);
7430 s
= bfd_get_linker_section (dynobj
, GNU_HASH_SECTION_NAME (bed
));
7431 BFD_ASSERT (s
!= NULL
);
7433 if (cinfo
.nsyms
== 0)
7435 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7436 BFD_ASSERT (cinfo
.min_dynindx
== -1);
7437 free (cinfo
.hashcodes
);
7438 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
7439 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7440 if (contents
== NULL
)
7442 s
->contents
= contents
;
7443 /* 1 empty bucket. */
7444 bfd_put_32 (output_bfd
, 1, contents
);
7445 /* SYMIDX above the special symbol 0. */
7446 bfd_put_32 (output_bfd
, 1, contents
+ 4);
7447 /* Just one word for bitmask. */
7448 bfd_put_32 (output_bfd
, 1, contents
+ 8);
7449 /* Only hash fn bloom filter. */
7450 bfd_put_32 (output_bfd
, 0, contents
+ 12);
7451 /* No hashes are valid - empty bitmask. */
7452 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
7453 /* No hashes in the only bucket. */
7454 bfd_put_32 (output_bfd
, 0,
7455 contents
+ 16 + bed
->s
->arch_size
/ 8);
7459 unsigned long int maskwords
, maskbitslog2
, x
;
7460 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
7464 while ((x
>>= 1) != 0)
7466 if (maskbitslog2
< 3)
7468 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
7469 maskbitslog2
= maskbitslog2
+ 3;
7471 maskbitslog2
= maskbitslog2
+ 2;
7472 if (bed
->s
->arch_size
== 64)
7474 if (maskbitslog2
== 5)
7480 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
7481 cinfo
.shift2
= maskbitslog2
;
7482 cinfo
.maskbits
= 1 << maskbitslog2
;
7483 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
7484 amt
= bucketcount
* sizeof (unsigned long int) * 2;
7485 amt
+= maskwords
* sizeof (bfd_vma
);
7486 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
7487 if (cinfo
.bitmask
== NULL
)
7489 free (cinfo
.hashcodes
);
7493 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
7494 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
7495 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
7496 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
7498 /* Determine how often each hash bucket is used. */
7499 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
7500 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
7501 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
7503 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
7504 if (cinfo
.counts
[i
] != 0)
7506 cinfo
.indx
[i
] = cnt
;
7507 cnt
+= cinfo
.counts
[i
];
7509 BFD_ASSERT (cnt
== dynsymcount
);
7510 cinfo
.bucketcount
= bucketcount
;
7511 cinfo
.local_indx
= cinfo
.min_dynindx
;
7513 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
7514 s
->size
+= cinfo
.maskbits
/ 8;
7515 if (bed
->record_xhash_symbol
!= NULL
)
7516 s
->size
+= cinfo
.nsyms
* 4;
7517 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7518 if (contents
== NULL
)
7520 free (cinfo
.bitmask
);
7521 free (cinfo
.hashcodes
);
7525 s
->contents
= contents
;
7526 bfd_put_32 (output_bfd
, bucketcount
, contents
);
7527 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
7528 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
7529 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
7530 contents
+= 16 + cinfo
.maskbits
/ 8;
7532 for (i
= 0; i
< bucketcount
; ++i
)
7534 if (cinfo
.counts
[i
] == 0)
7535 bfd_put_32 (output_bfd
, 0, contents
);
7537 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
7541 cinfo
.contents
= contents
;
7543 cinfo
.xlat
= contents
+ cinfo
.nsyms
* 4 - s
->contents
;
7544 /* Renumber dynamic symbols, if populating .gnu.hash section.
7545 If using .MIPS.xhash, populate the translation table. */
7546 elf_link_hash_traverse (elf_hash_table (info
),
7547 elf_gnu_hash_process_symidx
, &cinfo
);
7549 contents
= s
->contents
+ 16;
7550 for (i
= 0; i
< maskwords
; ++i
)
7552 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
7554 contents
+= bed
->s
->arch_size
/ 8;
7557 free (cinfo
.bitmask
);
7558 free (cinfo
.hashcodes
);
7562 s
= bfd_get_linker_section (dynobj
, ".dynstr");
7563 BFD_ASSERT (s
!= NULL
);
7565 elf_finalize_dynstr (output_bfd
, info
);
7567 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7569 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
7570 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
7577 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7580 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
7583 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
7584 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
7587 /* Finish SHF_MERGE section merging. */
7590 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
7595 if (!is_elf_hash_table (info
->hash
))
7598 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
7599 if ((ibfd
->flags
& DYNAMIC
) == 0
7600 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
7601 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
7602 == get_elf_backend_data (obfd
)->s
->elfclass
))
7603 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7604 if ((sec
->flags
& SEC_MERGE
) != 0
7605 && !bfd_is_abs_section (sec
->output_section
))
7607 struct bfd_elf_section_data
*secdata
;
7609 secdata
= elf_section_data (sec
);
7610 if (! _bfd_add_merge_section (obfd
,
7611 &elf_hash_table (info
)->merge_info
,
7612 sec
, &secdata
->sec_info
))
7614 else if (secdata
->sec_info
)
7615 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7618 if (elf_hash_table (info
)->merge_info
!= NULL
)
7619 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7620 merge_sections_remove_hook
);
7624 /* Create an entry in an ELF linker hash table. */
7626 struct bfd_hash_entry
*
7627 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7628 struct bfd_hash_table
*table
,
7631 /* Allocate the structure if it has not already been allocated by a
7635 entry
= (struct bfd_hash_entry
*)
7636 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7641 /* Call the allocation method of the superclass. */
7642 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7645 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7646 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7648 /* Set local fields. */
7651 ret
->got
= htab
->init_got_refcount
;
7652 ret
->plt
= htab
->init_plt_refcount
;
7653 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7654 - offsetof (struct elf_link_hash_entry
, size
)));
7655 /* Assume that we have been called by a non-ELF symbol reader.
7656 This flag is then reset by the code which reads an ELF input
7657 file. This ensures that a symbol created by a non-ELF symbol
7658 reader will have the flag set correctly. */
7665 /* Copy data from an indirect symbol to its direct symbol, hiding the
7666 old indirect symbol. Also used for copying flags to a weakdef. */
7669 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7670 struct elf_link_hash_entry
*dir
,
7671 struct elf_link_hash_entry
*ind
)
7673 struct elf_link_hash_table
*htab
;
7675 if (ind
->dyn_relocs
!= NULL
)
7677 if (dir
->dyn_relocs
!= NULL
)
7679 struct elf_dyn_relocs
**pp
;
7680 struct elf_dyn_relocs
*p
;
7682 /* Add reloc counts against the indirect sym to the direct sym
7683 list. Merge any entries against the same section. */
7684 for (pp
= &ind
->dyn_relocs
; (p
= *pp
) != NULL
; )
7686 struct elf_dyn_relocs
*q
;
7688 for (q
= dir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
7689 if (q
->sec
== p
->sec
)
7691 q
->pc_count
+= p
->pc_count
;
7692 q
->count
+= p
->count
;
7699 *pp
= dir
->dyn_relocs
;
7702 dir
->dyn_relocs
= ind
->dyn_relocs
;
7703 ind
->dyn_relocs
= NULL
;
7706 /* Copy down any references that we may have already seen to the
7707 symbol which just became indirect. */
7709 if (dir
->versioned
!= versioned_hidden
)
7710 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7711 dir
->ref_regular
|= ind
->ref_regular
;
7712 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7713 dir
->non_got_ref
|= ind
->non_got_ref
;
7714 dir
->needs_plt
|= ind
->needs_plt
;
7715 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7717 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7720 /* Copy over the global and procedure linkage table refcount entries.
7721 These may have been already set up by a check_relocs routine. */
7722 htab
= elf_hash_table (info
);
7723 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7725 if (dir
->got
.refcount
< 0)
7726 dir
->got
.refcount
= 0;
7727 dir
->got
.refcount
+= ind
->got
.refcount
;
7728 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7731 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7733 if (dir
->plt
.refcount
< 0)
7734 dir
->plt
.refcount
= 0;
7735 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7736 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7739 if (ind
->dynindx
!= -1)
7741 if (dir
->dynindx
!= -1)
7742 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7743 dir
->dynindx
= ind
->dynindx
;
7744 dir
->dynstr_index
= ind
->dynstr_index
;
7746 ind
->dynstr_index
= 0;
7751 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7752 struct elf_link_hash_entry
*h
,
7753 bfd_boolean force_local
)
7755 /* STT_GNU_IFUNC symbol must go through PLT. */
7756 if (h
->type
!= STT_GNU_IFUNC
)
7758 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7763 h
->forced_local
= 1;
7764 if (h
->dynindx
!= -1)
7766 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7769 h
->dynstr_index
= 0;
7774 /* Hide a symbol. */
7777 _bfd_elf_link_hide_symbol (bfd
*output_bfd
,
7778 struct bfd_link_info
*info
,
7779 struct bfd_link_hash_entry
*h
)
7781 if (is_elf_hash_table (info
->hash
))
7783 const struct elf_backend_data
*bed
7784 = get_elf_backend_data (output_bfd
);
7785 struct elf_link_hash_entry
*eh
7786 = (struct elf_link_hash_entry
*) h
;
7787 bed
->elf_backend_hide_symbol (info
, eh
, TRUE
);
7788 eh
->def_dynamic
= 0;
7789 eh
->ref_dynamic
= 0;
7790 eh
->dynamic_def
= 0;
7794 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7798 _bfd_elf_link_hash_table_init
7799 (struct elf_link_hash_table
*table
,
7801 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
7802 struct bfd_hash_table
*,
7804 unsigned int entsize
,
7805 enum elf_target_id target_id
)
7808 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
7810 table
->init_got_refcount
.refcount
= can_refcount
- 1;
7811 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
7812 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
7813 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
7814 /* The first dynamic symbol is a dummy. */
7815 table
->dynsymcount
= 1;
7817 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
7819 table
->root
.type
= bfd_link_elf_hash_table
;
7820 table
->hash_table_id
= target_id
;
7821 table
->target_os
= get_elf_backend_data (abfd
)->target_os
;
7826 /* Create an ELF linker hash table. */
7828 struct bfd_link_hash_table
*
7829 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7831 struct elf_link_hash_table
*ret
;
7832 size_t amt
= sizeof (struct elf_link_hash_table
);
7834 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7838 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7839 sizeof (struct elf_link_hash_entry
),
7845 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7850 /* Destroy an ELF linker hash table. */
7853 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7855 struct elf_link_hash_table
*htab
;
7857 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7858 if (htab
->dynstr
!= NULL
)
7859 _bfd_elf_strtab_free (htab
->dynstr
);
7860 _bfd_merge_sections_free (htab
->merge_info
);
7861 _bfd_generic_link_hash_table_free (obfd
);
7864 /* This is a hook for the ELF emulation code in the generic linker to
7865 tell the backend linker what file name to use for the DT_NEEDED
7866 entry for a dynamic object. */
7869 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7871 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7872 && bfd_get_format (abfd
) == bfd_object
)
7873 elf_dt_name (abfd
) = name
;
7877 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
7880 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7881 && bfd_get_format (abfd
) == bfd_object
)
7882 lib_class
= elf_dyn_lib_class (abfd
);
7889 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
7891 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7892 && bfd_get_format (abfd
) == bfd_object
)
7893 elf_dyn_lib_class (abfd
) = lib_class
;
7896 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7897 the linker ELF emulation code. */
7899 struct bfd_link_needed_list
*
7900 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7901 struct bfd_link_info
*info
)
7903 if (! is_elf_hash_table (info
->hash
))
7905 return elf_hash_table (info
)->needed
;
7908 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7909 hook for the linker ELF emulation code. */
7911 struct bfd_link_needed_list
*
7912 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7913 struct bfd_link_info
*info
)
7915 if (! is_elf_hash_table (info
->hash
))
7917 return elf_hash_table (info
)->runpath
;
7920 /* Get the name actually used for a dynamic object for a link. This
7921 is the SONAME entry if there is one. Otherwise, it is the string
7922 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7925 bfd_elf_get_dt_soname (bfd
*abfd
)
7927 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7928 && bfd_get_format (abfd
) == bfd_object
)
7929 return elf_dt_name (abfd
);
7933 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7934 the ELF linker emulation code. */
7937 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
7938 struct bfd_link_needed_list
**pneeded
)
7941 bfd_byte
*dynbuf
= NULL
;
7942 unsigned int elfsec
;
7943 unsigned long shlink
;
7944 bfd_byte
*extdyn
, *extdynend
;
7946 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
7950 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
7951 || bfd_get_format (abfd
) != bfd_object
)
7954 s
= bfd_get_section_by_name (abfd
, ".dynamic");
7955 if (s
== NULL
|| s
->size
== 0)
7958 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
7961 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
7962 if (elfsec
== SHN_BAD
)
7965 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
7967 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
7968 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
7971 extdynend
= extdyn
+ s
->size
;
7972 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
7974 Elf_Internal_Dyn dyn
;
7976 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
7978 if (dyn
.d_tag
== DT_NULL
)
7981 if (dyn
.d_tag
== DT_NEEDED
)
7984 struct bfd_link_needed_list
*l
;
7985 unsigned int tagv
= dyn
.d_un
.d_val
;
7988 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
7993 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
8013 struct elf_symbuf_symbol
8015 unsigned long st_name
; /* Symbol name, index in string tbl */
8016 unsigned char st_info
; /* Type and binding attributes */
8017 unsigned char st_other
; /* Visibilty, and target specific */
8020 struct elf_symbuf_head
8022 struct elf_symbuf_symbol
*ssym
;
8024 unsigned int st_shndx
;
8031 Elf_Internal_Sym
*isym
;
8032 struct elf_symbuf_symbol
*ssym
;
8038 /* Sort references to symbols by ascending section number. */
8041 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
8043 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
8044 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
8046 if (s1
->st_shndx
!= s2
->st_shndx
)
8047 return s1
->st_shndx
> s2
->st_shndx
? 1 : -1;
8048 /* Final sort by the address of the sym in the symbuf ensures
8051 return s1
> s2
? 1 : -1;
8056 elf_sym_name_compare (const void *arg1
, const void *arg2
)
8058 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
8059 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
8060 int ret
= strcmp (s1
->name
, s2
->name
);
8063 if (s1
->u
.p
!= s2
->u
.p
)
8064 return s1
->u
.p
> s2
->u
.p
? 1 : -1;
8068 static struct elf_symbuf_head
*
8069 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
8071 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
8072 struct elf_symbuf_symbol
*ssym
;
8073 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
8074 size_t i
, shndx_count
, total_size
, amt
;
8076 amt
= symcount
* sizeof (*indbuf
);
8077 indbuf
= (Elf_Internal_Sym
**) bfd_malloc (amt
);
8081 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
8082 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
8083 *ind
++ = &isymbuf
[i
];
8086 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
8087 elf_sort_elf_symbol
);
8090 if (indbufend
> indbuf
)
8091 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
8092 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
8095 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
8096 + (indbufend
- indbuf
) * sizeof (*ssym
));
8097 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
8098 if (ssymbuf
== NULL
)
8104 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
8105 ssymbuf
->ssym
= NULL
;
8106 ssymbuf
->count
= shndx_count
;
8107 ssymbuf
->st_shndx
= 0;
8108 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
8110 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
8113 ssymhead
->ssym
= ssym
;
8114 ssymhead
->count
= 0;
8115 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
8117 ssym
->st_name
= (*ind
)->st_name
;
8118 ssym
->st_info
= (*ind
)->st_info
;
8119 ssym
->st_other
= (*ind
)->st_other
;
8122 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
8123 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
8130 /* Check if 2 sections define the same set of local and global
8134 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
8135 struct bfd_link_info
*info
)
8138 const struct elf_backend_data
*bed1
, *bed2
;
8139 Elf_Internal_Shdr
*hdr1
, *hdr2
;
8140 size_t symcount1
, symcount2
;
8141 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
8142 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
8143 Elf_Internal_Sym
*isym
, *isymend
;
8144 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
8145 size_t count1
, count2
, i
;
8146 unsigned int shndx1
, shndx2
;
8152 /* Both sections have to be in ELF. */
8153 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
8154 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
8157 if (elf_section_type (sec1
) != elf_section_type (sec2
))
8160 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
8161 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
8162 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
8165 bed1
= get_elf_backend_data (bfd1
);
8166 bed2
= get_elf_backend_data (bfd2
);
8167 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
8168 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
8169 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
8170 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
8172 if (symcount1
== 0 || symcount2
== 0)
8178 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
8179 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
8181 if (ssymbuf1
== NULL
)
8183 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
8185 if (isymbuf1
== NULL
)
8188 if (info
!= NULL
&& !info
->reduce_memory_overheads
)
8190 ssymbuf1
= elf_create_symbuf (symcount1
, isymbuf1
);
8191 elf_tdata (bfd1
)->symbuf
= ssymbuf1
;
8195 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
8197 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
8199 if (isymbuf2
== NULL
)
8202 if (ssymbuf1
!= NULL
&& info
!= NULL
&& !info
->reduce_memory_overheads
)
8204 ssymbuf2
= elf_create_symbuf (symcount2
, isymbuf2
);
8205 elf_tdata (bfd2
)->symbuf
= ssymbuf2
;
8209 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
8211 /* Optimized faster version. */
8213 struct elf_symbol
*symp
;
8214 struct elf_symbuf_symbol
*ssym
, *ssymend
;
8217 hi
= ssymbuf1
->count
;
8222 mid
= (lo
+ hi
) / 2;
8223 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
8225 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
8229 count1
= ssymbuf1
[mid
].count
;
8236 hi
= ssymbuf2
->count
;
8241 mid
= (lo
+ hi
) / 2;
8242 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
8244 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
8248 count2
= ssymbuf2
[mid
].count
;
8254 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8258 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
8260 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
8261 if (symtable1
== NULL
|| symtable2
== NULL
)
8265 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
8266 ssym
< ssymend
; ssym
++, symp
++)
8268 symp
->u
.ssym
= ssym
;
8269 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
8275 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
8276 ssym
< ssymend
; ssym
++, symp
++)
8278 symp
->u
.ssym
= ssym
;
8279 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
8284 /* Sort symbol by name. */
8285 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8286 elf_sym_name_compare
);
8287 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8288 elf_sym_name_compare
);
8290 for (i
= 0; i
< count1
; i
++)
8291 /* Two symbols must have the same binding, type and name. */
8292 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
8293 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
8294 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8301 symtable1
= (struct elf_symbol
*)
8302 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
8303 symtable2
= (struct elf_symbol
*)
8304 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
8305 if (symtable1
== NULL
|| symtable2
== NULL
)
8308 /* Count definitions in the section. */
8310 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
8311 if (isym
->st_shndx
== shndx1
)
8312 symtable1
[count1
++].u
.isym
= isym
;
8315 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
8316 if (isym
->st_shndx
== shndx2
)
8317 symtable2
[count2
++].u
.isym
= isym
;
8319 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8322 for (i
= 0; i
< count1
; i
++)
8324 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
8325 symtable1
[i
].u
.isym
->st_name
);
8327 for (i
= 0; i
< count2
; i
++)
8329 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
8330 symtable2
[i
].u
.isym
->st_name
);
8332 /* Sort symbol by name. */
8333 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8334 elf_sym_name_compare
);
8335 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8336 elf_sym_name_compare
);
8338 for (i
= 0; i
< count1
; i
++)
8339 /* Two symbols must have the same binding, type and name. */
8340 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
8341 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
8342 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8356 /* Return TRUE if 2 section types are compatible. */
8359 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
8360 bfd
*bbfd
, const asection
*bsec
)
8364 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
8365 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8368 return elf_section_type (asec
) == elf_section_type (bsec
);
8371 /* Final phase of ELF linker. */
8373 /* A structure we use to avoid passing large numbers of arguments. */
8375 struct elf_final_link_info
8377 /* General link information. */
8378 struct bfd_link_info
*info
;
8381 /* Symbol string table. */
8382 struct elf_strtab_hash
*symstrtab
;
8383 /* .hash section. */
8385 /* symbol version section (.gnu.version). */
8386 asection
*symver_sec
;
8387 /* Buffer large enough to hold contents of any section. */
8389 /* Buffer large enough to hold external relocs of any section. */
8390 void *external_relocs
;
8391 /* Buffer large enough to hold internal relocs of any section. */
8392 Elf_Internal_Rela
*internal_relocs
;
8393 /* Buffer large enough to hold external local symbols of any input
8395 bfd_byte
*external_syms
;
8396 /* And a buffer for symbol section indices. */
8397 Elf_External_Sym_Shndx
*locsym_shndx
;
8398 /* Buffer large enough to hold internal local symbols of any input
8400 Elf_Internal_Sym
*internal_syms
;
8401 /* Array large enough to hold a symbol index for each local symbol
8402 of any input BFD. */
8404 /* Array large enough to hold a section pointer for each local
8405 symbol of any input BFD. */
8406 asection
**sections
;
8407 /* Buffer for SHT_SYMTAB_SHNDX section. */
8408 Elf_External_Sym_Shndx
*symshndxbuf
;
8409 /* Number of STT_FILE syms seen. */
8410 size_t filesym_count
;
8413 /* This struct is used to pass information to elf_link_output_extsym. */
8415 struct elf_outext_info
8418 bfd_boolean localsyms
;
8419 bfd_boolean file_sym_done
;
8420 struct elf_final_link_info
*flinfo
;
8424 /* Support for evaluating a complex relocation.
8426 Complex relocations are generalized, self-describing relocations. The
8427 implementation of them consists of two parts: complex symbols, and the
8428 relocations themselves.
8430 The relocations are use a reserved elf-wide relocation type code (R_RELC
8431 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8432 information (start bit, end bit, word width, etc) into the addend. This
8433 information is extracted from CGEN-generated operand tables within gas.
8435 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8436 internal) representing prefix-notation expressions, including but not
8437 limited to those sorts of expressions normally encoded as addends in the
8438 addend field. The symbol mangling format is:
8441 | <unary-operator> ':' <node>
8442 | <binary-operator> ':' <node> ':' <node>
8445 <literal> := 's' <digits=N> ':' <N character symbol name>
8446 | 'S' <digits=N> ':' <N character section name>
8450 <binary-operator> := as in C
8451 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8454 set_symbol_value (bfd
*bfd_with_globals
,
8455 Elf_Internal_Sym
*isymbuf
,
8460 struct elf_link_hash_entry
**sym_hashes
;
8461 struct elf_link_hash_entry
*h
;
8462 size_t extsymoff
= locsymcount
;
8464 if (symidx
< locsymcount
)
8466 Elf_Internal_Sym
*sym
;
8468 sym
= isymbuf
+ symidx
;
8469 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
8471 /* It is a local symbol: move it to the
8472 "absolute" section and give it a value. */
8473 sym
->st_shndx
= SHN_ABS
;
8474 sym
->st_value
= val
;
8477 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
8481 /* It is a global symbol: set its link type
8482 to "defined" and give it a value. */
8484 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
8485 h
= sym_hashes
[symidx
- extsymoff
];
8486 while (h
->root
.type
== bfd_link_hash_indirect
8487 || h
->root
.type
== bfd_link_hash_warning
)
8488 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8489 h
->root
.type
= bfd_link_hash_defined
;
8490 h
->root
.u
.def
.value
= val
;
8491 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
8495 resolve_symbol (const char *name
,
8497 struct elf_final_link_info
*flinfo
,
8499 Elf_Internal_Sym
*isymbuf
,
8502 Elf_Internal_Sym
*sym
;
8503 struct bfd_link_hash_entry
*global_entry
;
8504 const char *candidate
= NULL
;
8505 Elf_Internal_Shdr
*symtab_hdr
;
8508 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
8510 for (i
= 0; i
< locsymcount
; ++ i
)
8514 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
8517 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
8518 symtab_hdr
->sh_link
,
8521 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8522 name
, candidate
, (unsigned long) sym
->st_value
);
8524 if (candidate
&& strcmp (candidate
, name
) == 0)
8526 asection
*sec
= flinfo
->sections
[i
];
8528 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
8529 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
8531 printf ("Found symbol with value %8.8lx\n",
8532 (unsigned long) *result
);
8538 /* Hmm, haven't found it yet. perhaps it is a global. */
8539 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
8540 FALSE
, FALSE
, TRUE
);
8544 if (global_entry
->type
== bfd_link_hash_defined
8545 || global_entry
->type
== bfd_link_hash_defweak
)
8547 *result
= (global_entry
->u
.def
.value
8548 + global_entry
->u
.def
.section
->output_section
->vma
8549 + global_entry
->u
.def
.section
->output_offset
);
8551 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8552 global_entry
->root
.string
, (unsigned long) *result
);
8560 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8561 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8562 names like "foo.end" which is the end address of section "foo". */
8565 resolve_section (const char *name
,
8573 for (curr
= sections
; curr
; curr
= curr
->next
)
8574 if (strcmp (curr
->name
, name
) == 0)
8576 *result
= curr
->vma
;
8580 /* Hmm. still haven't found it. try pseudo-section names. */
8581 /* FIXME: This could be coded more efficiently... */
8582 for (curr
= sections
; curr
; curr
= curr
->next
)
8584 len
= strlen (curr
->name
);
8585 if (len
> strlen (name
))
8588 if (strncmp (curr
->name
, name
, len
) == 0)
8590 if (strncmp (".end", name
+ len
, 4) == 0)
8592 *result
= (curr
->vma
8593 + curr
->size
/ bfd_octets_per_byte (abfd
, curr
));
8597 /* Insert more pseudo-section names here, if you like. */
8605 undefined_reference (const char *reftype
, const char *name
)
8607 /* xgettext:c-format */
8608 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8613 eval_symbol (bfd_vma
*result
,
8616 struct elf_final_link_info
*flinfo
,
8618 Elf_Internal_Sym
*isymbuf
,
8627 const char *sym
= *symp
;
8629 bfd_boolean symbol_is_section
= FALSE
;
8634 if (len
< 1 || len
> sizeof (symbuf
))
8636 bfd_set_error (bfd_error_invalid_operation
);
8649 *result
= strtoul (sym
, (char **) symp
, 16);
8653 symbol_is_section
= TRUE
;
8657 symlen
= strtol (sym
, (char **) symp
, 10);
8658 sym
= *symp
+ 1; /* Skip the trailing ':'. */
8660 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
8662 bfd_set_error (bfd_error_invalid_operation
);
8666 memcpy (symbuf
, sym
, symlen
);
8667 symbuf
[symlen
] = '\0';
8668 *symp
= sym
+ symlen
;
8670 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8671 the symbol as a section, or vice-versa. so we're pretty liberal in our
8672 interpretation here; section means "try section first", not "must be a
8673 section", and likewise with symbol. */
8675 if (symbol_is_section
)
8677 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
8678 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8679 isymbuf
, locsymcount
))
8681 undefined_reference ("section", symbuf
);
8687 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8688 isymbuf
, locsymcount
)
8689 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8692 undefined_reference ("symbol", symbuf
);
8699 /* All that remains are operators. */
8701 #define UNARY_OP(op) \
8702 if (strncmp (sym, #op, strlen (#op)) == 0) \
8704 sym += strlen (#op); \
8708 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8709 isymbuf, locsymcount, signed_p)) \
8712 *result = op ((bfd_signed_vma) a); \
8718 #define BINARY_OP(op) \
8719 if (strncmp (sym, #op, strlen (#op)) == 0) \
8721 sym += strlen (#op); \
8725 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8726 isymbuf, locsymcount, signed_p)) \
8729 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8730 isymbuf, locsymcount, signed_p)) \
8733 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8763 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
8764 bfd_set_error (bfd_error_invalid_operation
);
8770 put_value (bfd_vma size
,
8771 unsigned long chunksz
,
8776 location
+= (size
- chunksz
);
8778 for (; size
; size
-= chunksz
, location
-= chunksz
)
8783 bfd_put_8 (input_bfd
, x
, location
);
8787 bfd_put_16 (input_bfd
, x
, location
);
8791 bfd_put_32 (input_bfd
, x
, location
);
8792 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8798 bfd_put_64 (input_bfd
, x
, location
);
8799 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8812 get_value (bfd_vma size
,
8813 unsigned long chunksz
,
8820 /* Sanity checks. */
8821 BFD_ASSERT (chunksz
<= sizeof (x
)
8824 && (size
% chunksz
) == 0
8825 && input_bfd
!= NULL
8826 && location
!= NULL
);
8828 if (chunksz
== sizeof (x
))
8830 BFD_ASSERT (size
== chunksz
);
8832 /* Make sure that we do not perform an undefined shift operation.
8833 We know that size == chunksz so there will only be one iteration
8834 of the loop below. */
8838 shift
= 8 * chunksz
;
8840 for (; size
; size
-= chunksz
, location
+= chunksz
)
8845 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
8848 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
8851 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
8855 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
8866 decode_complex_addend (unsigned long *start
, /* in bits */
8867 unsigned long *oplen
, /* in bits */
8868 unsigned long *len
, /* in bits */
8869 unsigned long *wordsz
, /* in bytes */
8870 unsigned long *chunksz
, /* in bytes */
8871 unsigned long *lsb0_p
,
8872 unsigned long *signed_p
,
8873 unsigned long *trunc_p
,
8874 unsigned long encoded
)
8876 * start
= encoded
& 0x3F;
8877 * len
= (encoded
>> 6) & 0x3F;
8878 * oplen
= (encoded
>> 12) & 0x3F;
8879 * wordsz
= (encoded
>> 18) & 0xF;
8880 * chunksz
= (encoded
>> 22) & 0xF;
8881 * lsb0_p
= (encoded
>> 27) & 1;
8882 * signed_p
= (encoded
>> 28) & 1;
8883 * trunc_p
= (encoded
>> 29) & 1;
8886 bfd_reloc_status_type
8887 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
8888 asection
*input_section
,
8890 Elf_Internal_Rela
*rel
,
8893 bfd_vma shift
, x
, mask
;
8894 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
8895 bfd_reloc_status_type r
;
8896 bfd_size_type octets
;
8898 /* Perform this reloc, since it is complex.
8899 (this is not to say that it necessarily refers to a complex
8900 symbol; merely that it is a self-describing CGEN based reloc.
8901 i.e. the addend has the complete reloc information (bit start, end,
8902 word size, etc) encoded within it.). */
8904 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
8905 &chunksz
, &lsb0_p
, &signed_p
,
8906 &trunc_p
, rel
->r_addend
);
8908 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
8911 shift
= (start
+ 1) - len
;
8913 shift
= (8 * wordsz
) - (start
+ len
);
8915 octets
= rel
->r_offset
* bfd_octets_per_byte (input_bfd
, input_section
);
8916 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ octets
);
8919 printf ("Doing complex reloc: "
8920 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8921 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8922 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8923 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
8924 oplen
, (unsigned long) x
, (unsigned long) mask
,
8925 (unsigned long) relocation
);
8930 /* Now do an overflow check. */
8931 r
= bfd_check_overflow ((signed_p
8932 ? complain_overflow_signed
8933 : complain_overflow_unsigned
),
8934 len
, 0, (8 * wordsz
),
8938 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
8941 printf (" relocation: %8.8lx\n"
8942 " shifted mask: %8.8lx\n"
8943 " shifted/masked reloc: %8.8lx\n"
8944 " result: %8.8lx\n",
8945 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
8946 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
8948 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ octets
);
8952 /* Functions to read r_offset from external (target order) reloc
8953 entry. Faster than bfd_getl32 et al, because we let the compiler
8954 know the value is aligned. */
8957 ext32l_r_offset (const void *p
)
8964 const union aligned32
*a
8965 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8967 uint32_t aval
= ( (uint32_t) a
->c
[0]
8968 | (uint32_t) a
->c
[1] << 8
8969 | (uint32_t) a
->c
[2] << 16
8970 | (uint32_t) a
->c
[3] << 24);
8975 ext32b_r_offset (const void *p
)
8982 const union aligned32
*a
8983 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8985 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
8986 | (uint32_t) a
->c
[1] << 16
8987 | (uint32_t) a
->c
[2] << 8
8988 | (uint32_t) a
->c
[3]);
8992 #ifdef BFD_HOST_64_BIT
8994 ext64l_r_offset (const void *p
)
9001 const union aligned64
*a
9002 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9004 uint64_t aval
= ( (uint64_t) a
->c
[0]
9005 | (uint64_t) a
->c
[1] << 8
9006 | (uint64_t) a
->c
[2] << 16
9007 | (uint64_t) a
->c
[3] << 24
9008 | (uint64_t) a
->c
[4] << 32
9009 | (uint64_t) a
->c
[5] << 40
9010 | (uint64_t) a
->c
[6] << 48
9011 | (uint64_t) a
->c
[7] << 56);
9016 ext64b_r_offset (const void *p
)
9023 const union aligned64
*a
9024 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9026 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
9027 | (uint64_t) a
->c
[1] << 48
9028 | (uint64_t) a
->c
[2] << 40
9029 | (uint64_t) a
->c
[3] << 32
9030 | (uint64_t) a
->c
[4] << 24
9031 | (uint64_t) a
->c
[5] << 16
9032 | (uint64_t) a
->c
[6] << 8
9033 | (uint64_t) a
->c
[7]);
9038 /* When performing a relocatable link, the input relocations are
9039 preserved. But, if they reference global symbols, the indices
9040 referenced must be updated. Update all the relocations found in
9044 elf_link_adjust_relocs (bfd
*abfd
,
9046 struct bfd_elf_section_reloc_data
*reldata
,
9048 struct bfd_link_info
*info
)
9051 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9053 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9054 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9055 bfd_vma r_type_mask
;
9057 unsigned int count
= reldata
->count
;
9058 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
9060 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
9062 swap_in
= bed
->s
->swap_reloc_in
;
9063 swap_out
= bed
->s
->swap_reloc_out
;
9065 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
9067 swap_in
= bed
->s
->swap_reloca_in
;
9068 swap_out
= bed
->s
->swap_reloca_out
;
9073 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
9076 if (bed
->s
->arch_size
== 32)
9083 r_type_mask
= 0xffffffff;
9087 erela
= reldata
->hdr
->contents
;
9088 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
9090 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
9093 if (*rel_hash
== NULL
)
9096 if ((*rel_hash
)->indx
== -2
9097 && info
->gc_sections
9098 && ! info
->gc_keep_exported
)
9100 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9101 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9103 (*rel_hash
)->root
.root
.string
);
9104 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9106 bfd_set_error (bfd_error_invalid_operation
);
9109 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
9111 (*swap_in
) (abfd
, erela
, irela
);
9112 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
9113 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
9114 | (irela
[j
].r_info
& r_type_mask
));
9115 (*swap_out
) (abfd
, irela
, erela
);
9118 if (bed
->elf_backend_update_relocs
)
9119 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
9121 if (sort
&& count
!= 0)
9123 bfd_vma (*ext_r_off
) (const void *);
9126 bfd_byte
*base
, *end
, *p
, *loc
;
9127 bfd_byte
*buf
= NULL
;
9129 if (bed
->s
->arch_size
== 32)
9131 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9132 ext_r_off
= ext32l_r_offset
;
9133 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9134 ext_r_off
= ext32b_r_offset
;
9140 #ifdef BFD_HOST_64_BIT
9141 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9142 ext_r_off
= ext64l_r_offset
;
9143 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9144 ext_r_off
= ext64b_r_offset
;
9150 /* Must use a stable sort here. A modified insertion sort,
9151 since the relocs are mostly sorted already. */
9152 elt_size
= reldata
->hdr
->sh_entsize
;
9153 base
= reldata
->hdr
->contents
;
9154 end
= base
+ count
* elt_size
;
9155 if (elt_size
> sizeof (Elf64_External_Rela
))
9158 /* Ensure the first element is lowest. This acts as a sentinel,
9159 speeding the main loop below. */
9160 r_off
= (*ext_r_off
) (base
);
9161 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
9163 bfd_vma r_off2
= (*ext_r_off
) (p
);
9172 /* Don't just swap *base and *loc as that changes the order
9173 of the original base[0] and base[1] if they happen to
9174 have the same r_offset. */
9175 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
9176 memcpy (onebuf
, loc
, elt_size
);
9177 memmove (base
+ elt_size
, base
, loc
- base
);
9178 memcpy (base
, onebuf
, elt_size
);
9181 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
9183 /* base to p is sorted, *p is next to insert. */
9184 r_off
= (*ext_r_off
) (p
);
9185 /* Search the sorted region for location to insert. */
9187 while (r_off
< (*ext_r_off
) (loc
))
9192 /* Chances are there is a run of relocs to insert here,
9193 from one of more input files. Files are not always
9194 linked in order due to the way elf_link_input_bfd is
9195 called. See pr17666. */
9196 size_t sortlen
= p
- loc
;
9197 bfd_vma r_off2
= (*ext_r_off
) (loc
);
9198 size_t runlen
= elt_size
;
9199 size_t buf_size
= 96 * 1024;
9200 while (p
+ runlen
< end
9201 && (sortlen
<= buf_size
9202 || runlen
+ elt_size
<= buf_size
)
9203 && r_off2
> (*ext_r_off
) (p
+ runlen
))
9207 buf
= bfd_malloc (buf_size
);
9211 if (runlen
< sortlen
)
9213 memcpy (buf
, p
, runlen
);
9214 memmove (loc
+ runlen
, loc
, sortlen
);
9215 memcpy (loc
, buf
, runlen
);
9219 memcpy (buf
, loc
, sortlen
);
9220 memmove (loc
, p
, runlen
);
9221 memcpy (loc
+ runlen
, buf
, sortlen
);
9223 p
+= runlen
- elt_size
;
9226 /* Hashes are no longer valid. */
9227 free (reldata
->hashes
);
9228 reldata
->hashes
= NULL
;
9234 struct elf_link_sort_rela
9240 enum elf_reloc_type_class type
;
9241 /* We use this as an array of size int_rels_per_ext_rel. */
9242 Elf_Internal_Rela rela
[1];
9245 /* qsort stability here and for cmp2 is only an issue if multiple
9246 dynamic relocations are emitted at the same address. But targets
9247 that apply a series of dynamic relocations each operating on the
9248 result of the prior relocation can't use -z combreloc as
9249 implemented anyway. Such schemes tend to be broken by sorting on
9250 symbol index. That leaves dynamic NONE relocs as the only other
9251 case where ld might emit multiple relocs at the same address, and
9252 those are only emitted due to target bugs. */
9255 elf_link_sort_cmp1 (const void *A
, const void *B
)
9257 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9258 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9259 int relativea
, relativeb
;
9261 relativea
= a
->type
== reloc_class_relative
;
9262 relativeb
= b
->type
== reloc_class_relative
;
9264 if (relativea
< relativeb
)
9266 if (relativea
> relativeb
)
9268 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
9270 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
9272 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9274 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9280 elf_link_sort_cmp2 (const void *A
, const void *B
)
9282 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9283 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9285 if (a
->type
< b
->type
)
9287 if (a
->type
> b
->type
)
9289 if (a
->u
.offset
< b
->u
.offset
)
9291 if (a
->u
.offset
> b
->u
.offset
)
9293 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9295 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9301 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
9303 asection
*dynamic_relocs
;
9306 bfd_size_type count
, size
;
9307 size_t i
, ret
, sort_elt
, ext_size
;
9308 bfd_byte
*sort
, *s_non_relative
, *p
;
9309 struct elf_link_sort_rela
*sq
;
9310 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9311 int i2e
= bed
->s
->int_rels_per_ext_rel
;
9312 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
9313 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9314 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9315 struct bfd_link_order
*lo
;
9317 bfd_boolean use_rela
;
9319 /* Find a dynamic reloc section. */
9320 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
9321 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
9322 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
9323 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9325 bfd_boolean use_rela_initialised
= FALSE
;
9327 /* This is just here to stop gcc from complaining.
9328 Its initialization checking code is not perfect. */
9331 /* Both sections are present. Examine the sizes
9332 of the indirect sections to help us choose. */
9333 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9334 if (lo
->type
== bfd_indirect_link_order
)
9336 asection
*o
= lo
->u
.indirect
.section
;
9338 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9340 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9341 /* Section size is divisible by both rel and rela sizes.
9342 It is of no help to us. */
9346 /* Section size is only divisible by rela. */
9347 if (use_rela_initialised
&& !use_rela
)
9349 _bfd_error_handler (_("%pB: unable to sort relocs - "
9350 "they are in more than one size"),
9352 bfd_set_error (bfd_error_invalid_operation
);
9358 use_rela_initialised
= TRUE
;
9362 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9364 /* Section size is only divisible by rel. */
9365 if (use_rela_initialised
&& use_rela
)
9367 _bfd_error_handler (_("%pB: unable to sort relocs - "
9368 "they are in more than one size"),
9370 bfd_set_error (bfd_error_invalid_operation
);
9376 use_rela_initialised
= TRUE
;
9381 /* The section size is not divisible by either -
9382 something is wrong. */
9383 _bfd_error_handler (_("%pB: unable to sort relocs - "
9384 "they are of an unknown size"), abfd
);
9385 bfd_set_error (bfd_error_invalid_operation
);
9390 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9391 if (lo
->type
== bfd_indirect_link_order
)
9393 asection
*o
= lo
->u
.indirect
.section
;
9395 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9397 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9398 /* Section size is divisible by both rel and rela sizes.
9399 It is of no help to us. */
9403 /* Section size is only divisible by rela. */
9404 if (use_rela_initialised
&& !use_rela
)
9406 _bfd_error_handler (_("%pB: unable to sort relocs - "
9407 "they are in more than one size"),
9409 bfd_set_error (bfd_error_invalid_operation
);
9415 use_rela_initialised
= TRUE
;
9419 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9421 /* Section size is only divisible by rel. */
9422 if (use_rela_initialised
&& use_rela
)
9424 _bfd_error_handler (_("%pB: unable to sort relocs - "
9425 "they are in more than one size"),
9427 bfd_set_error (bfd_error_invalid_operation
);
9433 use_rela_initialised
= TRUE
;
9438 /* The section size is not divisible by either -
9439 something is wrong. */
9440 _bfd_error_handler (_("%pB: unable to sort relocs - "
9441 "they are of an unknown size"), abfd
);
9442 bfd_set_error (bfd_error_invalid_operation
);
9447 if (! use_rela_initialised
)
9451 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
9453 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9460 dynamic_relocs
= rela_dyn
;
9461 ext_size
= bed
->s
->sizeof_rela
;
9462 swap_in
= bed
->s
->swap_reloca_in
;
9463 swap_out
= bed
->s
->swap_reloca_out
;
9467 dynamic_relocs
= rel_dyn
;
9468 ext_size
= bed
->s
->sizeof_rel
;
9469 swap_in
= bed
->s
->swap_reloc_in
;
9470 swap_out
= bed
->s
->swap_reloc_out
;
9474 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9475 if (lo
->type
== bfd_indirect_link_order
)
9476 size
+= lo
->u
.indirect
.section
->size
;
9478 if (size
!= dynamic_relocs
->size
)
9481 sort_elt
= (sizeof (struct elf_link_sort_rela
)
9482 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
9484 count
= dynamic_relocs
->size
/ ext_size
;
9487 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
9491 (*info
->callbacks
->warning
)
9492 (info
, _("not enough memory to sort relocations"), 0, abfd
, 0, 0);
9496 if (bed
->s
->arch_size
== 32)
9497 r_sym_mask
= ~(bfd_vma
) 0xff;
9499 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
9501 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9502 if (lo
->type
== bfd_indirect_link_order
)
9504 bfd_byte
*erel
, *erelend
;
9505 asection
*o
= lo
->u
.indirect
.section
;
9507 if (o
->contents
== NULL
&& o
->size
!= 0)
9509 /* This is a reloc section that is being handled as a normal
9510 section. See bfd_section_from_shdr. We can't combine
9511 relocs in this case. */
9516 erelend
= o
->contents
+ o
->size
;
9517 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
9519 while (erel
< erelend
)
9521 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9523 (*swap_in
) (abfd
, erel
, s
->rela
);
9524 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
9525 s
->u
.sym_mask
= r_sym_mask
;
9531 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
9533 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
9535 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9536 if (s
->type
!= reloc_class_relative
)
9542 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
9543 for (; i
< count
; i
++, p
+= sort_elt
)
9545 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
9546 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
9548 sp
->u
.offset
= sq
->rela
->r_offset
;
9551 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
9553 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
9554 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
9556 /* We have plt relocs in .rela.dyn. */
9557 sq
= (struct elf_link_sort_rela
*) sort
;
9558 for (i
= 0; i
< count
; i
++)
9559 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
9561 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
9563 struct bfd_link_order
**plo
;
9564 /* Put srelplt link_order last. This is so the output_offset
9565 set in the next loop is correct for DT_JMPREL. */
9566 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
9567 if ((*plo
)->type
== bfd_indirect_link_order
9568 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
9574 plo
= &(*plo
)->next
;
9577 dynamic_relocs
->map_tail
.link_order
= lo
;
9582 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9583 if (lo
->type
== bfd_indirect_link_order
)
9585 bfd_byte
*erel
, *erelend
;
9586 asection
*o
= lo
->u
.indirect
.section
;
9589 erelend
= o
->contents
+ o
->size
;
9590 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
9591 while (erel
< erelend
)
9593 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9594 (*swap_out
) (abfd
, s
->rela
, erel
);
9601 *psec
= dynamic_relocs
;
9605 /* Add a symbol to the output symbol string table. */
9608 elf_link_output_symstrtab (struct elf_final_link_info
*flinfo
,
9610 Elf_Internal_Sym
*elfsym
,
9611 asection
*input_sec
,
9612 struct elf_link_hash_entry
*h
)
9614 int (*output_symbol_hook
)
9615 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
9616 struct elf_link_hash_entry
*);
9617 struct elf_link_hash_table
*hash_table
;
9618 const struct elf_backend_data
*bed
;
9619 bfd_size_type strtabsize
;
9621 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9623 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9624 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
9625 if (output_symbol_hook
!= NULL
)
9627 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
9632 if (ELF_ST_TYPE (elfsym
->st_info
) == STT_GNU_IFUNC
)
9633 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_ifunc
;
9634 if (ELF_ST_BIND (elfsym
->st_info
) == STB_GNU_UNIQUE
)
9635 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_unique
;
9639 || (input_sec
->flags
& SEC_EXCLUDE
))
9640 elfsym
->st_name
= (unsigned long) -1;
9643 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9644 to get the final offset for st_name. */
9646 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
9648 if (elfsym
->st_name
== (unsigned long) -1)
9652 hash_table
= elf_hash_table (flinfo
->info
);
9653 strtabsize
= hash_table
->strtabsize
;
9654 if (strtabsize
<= hash_table
->strtabcount
)
9656 strtabsize
+= strtabsize
;
9657 hash_table
->strtabsize
= strtabsize
;
9658 strtabsize
*= sizeof (*hash_table
->strtab
);
9660 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
9662 if (hash_table
->strtab
== NULL
)
9665 hash_table
->strtab
[hash_table
->strtabcount
].sym
= *elfsym
;
9666 hash_table
->strtab
[hash_table
->strtabcount
].dest_index
9667 = hash_table
->strtabcount
;
9668 hash_table
->strtab
[hash_table
->strtabcount
].destshndx_index
9669 = flinfo
->symshndxbuf
? bfd_get_symcount (flinfo
->output_bfd
) : 0;
9671 flinfo
->output_bfd
->symcount
+= 1;
9672 hash_table
->strtabcount
+= 1;
9677 /* Swap symbols out to the symbol table and flush the output symbols to
9681 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
9683 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
9686 const struct elf_backend_data
*bed
;
9688 Elf_Internal_Shdr
*hdr
;
9692 if (!hash_table
->strtabcount
)
9695 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9697 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9699 amt
= bed
->s
->sizeof_sym
* hash_table
->strtabcount
;
9700 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
9704 if (flinfo
->symshndxbuf
)
9706 amt
= sizeof (Elf_External_Sym_Shndx
);
9707 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
9708 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
9709 if (flinfo
->symshndxbuf
== NULL
)
9716 for (i
= 0; i
< hash_table
->strtabcount
; i
++)
9718 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
9719 if (elfsym
->sym
.st_name
== (unsigned long) -1)
9720 elfsym
->sym
.st_name
= 0;
9723 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
9724 elfsym
->sym
.st_name
);
9725 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
9726 ((bfd_byte
*) symbuf
9727 + (elfsym
->dest_index
9728 * bed
->s
->sizeof_sym
)),
9729 (flinfo
->symshndxbuf
9730 + elfsym
->destshndx_index
));
9733 /* Allow the linker to examine the strtab and symtab now they are
9736 if (flinfo
->info
->callbacks
->examine_strtab
)
9737 flinfo
->info
->callbacks
->examine_strtab (hash_table
->strtab
,
9738 hash_table
->strtabcount
,
9741 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
9742 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
9743 amt
= hash_table
->strtabcount
* bed
->s
->sizeof_sym
;
9744 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
9745 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
9747 hdr
->sh_size
+= amt
;
9755 free (hash_table
->strtab
);
9756 hash_table
->strtab
= NULL
;
9761 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9764 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
9766 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
9767 && sym
->st_shndx
< SHN_LORESERVE
)
9769 /* The gABI doesn't support dynamic symbols in output sections
9772 /* xgettext:c-format */
9773 (_("%pB: too many sections: %d (>= %d)"),
9774 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
9775 bfd_set_error (bfd_error_nonrepresentable_section
);
9781 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9782 allowing an unsatisfied unversioned symbol in the DSO to match a
9783 versioned symbol that would normally require an explicit version.
9784 We also handle the case that a DSO references a hidden symbol
9785 which may be satisfied by a versioned symbol in another DSO. */
9788 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
9789 const struct elf_backend_data
*bed
,
9790 struct elf_link_hash_entry
*h
)
9793 struct elf_link_loaded_list
*loaded
;
9795 if (!is_elf_hash_table (info
->hash
))
9798 /* Check indirect symbol. */
9799 while (h
->root
.type
== bfd_link_hash_indirect
)
9800 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9802 switch (h
->root
.type
)
9808 case bfd_link_hash_undefined
:
9809 case bfd_link_hash_undefweak
:
9810 abfd
= h
->root
.u
.undef
.abfd
;
9812 || (abfd
->flags
& DYNAMIC
) == 0
9813 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
9817 case bfd_link_hash_defined
:
9818 case bfd_link_hash_defweak
:
9819 abfd
= h
->root
.u
.def
.section
->owner
;
9822 case bfd_link_hash_common
:
9823 abfd
= h
->root
.u
.c
.p
->section
->owner
;
9826 BFD_ASSERT (abfd
!= NULL
);
9828 for (loaded
= elf_hash_table (info
)->dyn_loaded
;
9830 loaded
= loaded
->next
)
9833 Elf_Internal_Shdr
*hdr
;
9837 Elf_Internal_Shdr
*versymhdr
;
9838 Elf_Internal_Sym
*isym
;
9839 Elf_Internal_Sym
*isymend
;
9840 Elf_Internal_Sym
*isymbuf
;
9841 Elf_External_Versym
*ever
;
9842 Elf_External_Versym
*extversym
;
9844 input
= loaded
->abfd
;
9846 /* We check each DSO for a possible hidden versioned definition. */
9848 || elf_dynversym (input
) == 0)
9851 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
9853 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9854 if (elf_bad_symtab (input
))
9856 extsymcount
= symcount
;
9861 extsymcount
= symcount
- hdr
->sh_info
;
9862 extsymoff
= hdr
->sh_info
;
9865 if (extsymcount
== 0)
9868 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
9870 if (isymbuf
== NULL
)
9873 /* Read in any version definitions. */
9874 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
9875 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
9876 || (extversym
= (Elf_External_Versym
*)
9877 _bfd_malloc_and_read (input
, versymhdr
->sh_size
,
9878 versymhdr
->sh_size
)) == NULL
)
9884 ever
= extversym
+ extsymoff
;
9885 isymend
= isymbuf
+ extsymcount
;
9886 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
9889 Elf_Internal_Versym iver
;
9890 unsigned short version_index
;
9892 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
9893 || isym
->st_shndx
== SHN_UNDEF
)
9896 name
= bfd_elf_string_from_elf_section (input
,
9899 if (strcmp (name
, h
->root
.root
.string
) != 0)
9902 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
9904 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
9906 && h
->forced_local
))
9908 /* If we have a non-hidden versioned sym, then it should
9909 have provided a definition for the undefined sym unless
9910 it is defined in a non-shared object and forced local.
9915 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
9916 if (version_index
== 1 || version_index
== 2)
9918 /* This is the base or first version. We can use it. */
9932 /* Convert ELF common symbol TYPE. */
9935 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
9937 /* Commom symbol can only appear in relocatable link. */
9938 if (!bfd_link_relocatable (info
))
9940 switch (info
->elf_stt_common
)
9944 case elf_stt_common
:
9947 case no_elf_stt_common
:
9954 /* Add an external symbol to the symbol table. This is called from
9955 the hash table traversal routine. When generating a shared object,
9956 we go through the symbol table twice. The first time we output
9957 anything that might have been forced to local scope in a version
9958 script. The second time we output the symbols that are still
9962 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
9964 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
9965 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
9966 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
9968 Elf_Internal_Sym sym
;
9969 asection
*input_sec
;
9970 const struct elf_backend_data
*bed
;
9975 if (h
->root
.type
== bfd_link_hash_warning
)
9977 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9978 if (h
->root
.type
== bfd_link_hash_new
)
9982 /* Decide whether to output this symbol in this pass. */
9983 if (eoinfo
->localsyms
)
9985 if (!h
->forced_local
)
9990 if (h
->forced_local
)
9994 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9996 if (h
->root
.type
== bfd_link_hash_undefined
)
9998 /* If we have an undefined symbol reference here then it must have
9999 come from a shared library that is being linked in. (Undefined
10000 references in regular files have already been handled unless
10001 they are in unreferenced sections which are removed by garbage
10003 bfd_boolean ignore_undef
= FALSE
;
10005 /* Some symbols may be special in that the fact that they're
10006 undefined can be safely ignored - let backend determine that. */
10007 if (bed
->elf_backend_ignore_undef_symbol
)
10008 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
10010 /* If we are reporting errors for this situation then do so now. */
10012 && h
->ref_dynamic_nonweak
10013 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
10014 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
10015 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
10017 flinfo
->info
->callbacks
->undefined_symbol
10018 (flinfo
->info
, h
->root
.root
.string
,
10019 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
, NULL
, 0,
10020 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_DIAGNOSE
10021 && !flinfo
->info
->warn_unresolved_syms
);
10024 /* Strip a global symbol defined in a discarded section. */
10029 /* We should also warn if a forced local symbol is referenced from
10030 shared libraries. */
10031 if (bfd_link_executable (flinfo
->info
)
10036 && h
->ref_dynamic_nonweak
10037 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
10041 struct elf_link_hash_entry
*hi
= h
;
10043 /* Check indirect symbol. */
10044 while (hi
->root
.type
== bfd_link_hash_indirect
)
10045 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
10047 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
10048 /* xgettext:c-format */
10049 msg
= _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10050 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
10051 /* xgettext:c-format */
10052 msg
= _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10054 /* xgettext:c-format */
10055 msg
= _("%pB: local symbol `%s' in %pB is referenced by DSO");
10056 def_bfd
= flinfo
->output_bfd
;
10057 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
10058 def_bfd
= hi
->root
.u
.def
.section
->owner
;
10059 _bfd_error_handler (msg
, flinfo
->output_bfd
,
10060 h
->root
.root
.string
, def_bfd
);
10061 bfd_set_error (bfd_error_bad_value
);
10062 eoinfo
->failed
= TRUE
;
10066 /* We don't want to output symbols that have never been mentioned by
10067 a regular file, or that we have been told to strip. However, if
10068 h->indx is set to -2, the symbol is used by a reloc and we must
10073 else if ((h
->def_dynamic
10075 || h
->root
.type
== bfd_link_hash_new
)
10077 && !h
->ref_regular
)
10079 else if (flinfo
->info
->strip
== strip_all
)
10081 else if (flinfo
->info
->strip
== strip_some
10082 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
10083 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
10085 else if ((h
->root
.type
== bfd_link_hash_defined
10086 || h
->root
.type
== bfd_link_hash_defweak
)
10087 && ((flinfo
->info
->strip_discarded
10088 && discarded_section (h
->root
.u
.def
.section
))
10089 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
10090 && h
->root
.u
.def
.section
->owner
!= NULL
10091 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
10093 else if ((h
->root
.type
== bfd_link_hash_undefined
10094 || h
->root
.type
== bfd_link_hash_undefweak
)
10095 && h
->root
.u
.undef
.abfd
!= NULL
10096 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
10101 /* If we're stripping it, and it's not a dynamic symbol, there's
10102 nothing else to do. However, if it is a forced local symbol or
10103 an ifunc symbol we need to give the backend finish_dynamic_symbol
10104 function a chance to make it dynamic. */
10106 && h
->dynindx
== -1
10107 && type
!= STT_GNU_IFUNC
10108 && !h
->forced_local
)
10112 sym
.st_size
= h
->size
;
10113 sym
.st_other
= h
->other
;
10114 switch (h
->root
.type
)
10117 case bfd_link_hash_new
:
10118 case bfd_link_hash_warning
:
10122 case bfd_link_hash_undefined
:
10123 case bfd_link_hash_undefweak
:
10124 input_sec
= bfd_und_section_ptr
;
10125 sym
.st_shndx
= SHN_UNDEF
;
10128 case bfd_link_hash_defined
:
10129 case bfd_link_hash_defweak
:
10131 input_sec
= h
->root
.u
.def
.section
;
10132 if (input_sec
->output_section
!= NULL
)
10135 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
10136 input_sec
->output_section
);
10137 if (sym
.st_shndx
== SHN_BAD
)
10140 /* xgettext:c-format */
10141 (_("%pB: could not find output section %pA for input section %pA"),
10142 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
10143 bfd_set_error (bfd_error_nonrepresentable_section
);
10144 eoinfo
->failed
= TRUE
;
10148 /* ELF symbols in relocatable files are section relative,
10149 but in nonrelocatable files they are virtual
10151 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
10152 if (!bfd_link_relocatable (flinfo
->info
))
10154 sym
.st_value
+= input_sec
->output_section
->vma
;
10155 if (h
->type
== STT_TLS
)
10157 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
10158 if (tls_sec
!= NULL
)
10159 sym
.st_value
-= tls_sec
->vma
;
10165 BFD_ASSERT (input_sec
->owner
== NULL
10166 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
10167 sym
.st_shndx
= SHN_UNDEF
;
10168 input_sec
= bfd_und_section_ptr
;
10173 case bfd_link_hash_common
:
10174 input_sec
= h
->root
.u
.c
.p
->section
;
10175 sym
.st_shndx
= bed
->common_section_index (input_sec
);
10176 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
10179 case bfd_link_hash_indirect
:
10180 /* These symbols are created by symbol versioning. They point
10181 to the decorated version of the name. For example, if the
10182 symbol foo@@GNU_1.2 is the default, which should be used when
10183 foo is used with no version, then we add an indirect symbol
10184 foo which points to foo@@GNU_1.2. We ignore these symbols,
10185 since the indirected symbol is already in the hash table. */
10189 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
10190 switch (h
->root
.type
)
10192 case bfd_link_hash_common
:
10193 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10195 case bfd_link_hash_defined
:
10196 case bfd_link_hash_defweak
:
10197 if (bed
->common_definition (&sym
))
10198 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10202 case bfd_link_hash_undefined
:
10203 case bfd_link_hash_undefweak
:
10209 if (h
->forced_local
)
10211 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
10212 /* Turn off visibility on local symbol. */
10213 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
10215 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10216 else if (h
->unique_global
&& h
->def_regular
)
10217 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
10218 else if (h
->root
.type
== bfd_link_hash_undefweak
10219 || h
->root
.type
== bfd_link_hash_defweak
)
10220 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
10222 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
10223 sym
.st_target_internal
= h
->target_internal
;
10225 /* Give the processor backend a chance to tweak the symbol value,
10226 and also to finish up anything that needs to be done for this
10227 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10228 forced local syms when non-shared is due to a historical quirk.
10229 STT_GNU_IFUNC symbol must go through PLT. */
10230 if ((h
->type
== STT_GNU_IFUNC
10232 && !bfd_link_relocatable (flinfo
->info
))
10233 || ((h
->dynindx
!= -1
10234 || h
->forced_local
)
10235 && ((bfd_link_pic (flinfo
->info
)
10236 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
10237 || h
->root
.type
!= bfd_link_hash_undefweak
))
10238 || !h
->forced_local
)
10239 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
10241 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
10242 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
10244 eoinfo
->failed
= TRUE
;
10249 /* If we are marking the symbol as undefined, and there are no
10250 non-weak references to this symbol from a regular object, then
10251 mark the symbol as weak undefined; if there are non-weak
10252 references, mark the symbol as strong. We can't do this earlier,
10253 because it might not be marked as undefined until the
10254 finish_dynamic_symbol routine gets through with it. */
10255 if (sym
.st_shndx
== SHN_UNDEF
10257 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
10258 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
10261 type
= ELF_ST_TYPE (sym
.st_info
);
10263 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10264 if (type
== STT_GNU_IFUNC
)
10267 if (h
->ref_regular_nonweak
)
10268 bindtype
= STB_GLOBAL
;
10270 bindtype
= STB_WEAK
;
10271 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
10274 /* If this is a symbol defined in a dynamic library, don't use the
10275 symbol size from the dynamic library. Relinking an executable
10276 against a new library may introduce gratuitous changes in the
10277 executable's symbols if we keep the size. */
10278 if (sym
.st_shndx
== SHN_UNDEF
10283 /* If a non-weak symbol with non-default visibility is not defined
10284 locally, it is a fatal error. */
10285 if (!bfd_link_relocatable (flinfo
->info
)
10286 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
10287 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
10288 && h
->root
.type
== bfd_link_hash_undefined
10289 && !h
->def_regular
)
10293 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
10294 /* xgettext:c-format */
10295 msg
= _("%pB: protected symbol `%s' isn't defined");
10296 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
10297 /* xgettext:c-format */
10298 msg
= _("%pB: internal symbol `%s' isn't defined");
10300 /* xgettext:c-format */
10301 msg
= _("%pB: hidden symbol `%s' isn't defined");
10302 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
10303 bfd_set_error (bfd_error_bad_value
);
10304 eoinfo
->failed
= TRUE
;
10308 /* If this symbol should be put in the .dynsym section, then put it
10309 there now. We already know the symbol index. We also fill in
10310 the entry in the .hash section. */
10311 if (h
->dynindx
!= -1
10312 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
10313 && elf_hash_table (flinfo
->info
)->dynsym
!= NULL
10314 && !discarded_section (elf_hash_table (flinfo
->info
)->dynsym
))
10318 /* Since there is no version information in the dynamic string,
10319 if there is no version info in symbol version section, we will
10320 have a run-time problem if not linking executable, referenced
10321 by shared library, or not bound locally. */
10322 if (h
->verinfo
.verdef
== NULL
10323 && (!bfd_link_executable (flinfo
->info
)
10325 || !h
->def_regular
))
10327 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
10329 if (p
&& p
[1] != '\0')
10332 /* xgettext:c-format */
10333 (_("%pB: no symbol version section for versioned symbol `%s'"),
10334 flinfo
->output_bfd
, h
->root
.root
.string
);
10335 eoinfo
->failed
= TRUE
;
10340 sym
.st_name
= h
->dynstr_index
;
10341 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
10342 + h
->dynindx
* bed
->s
->sizeof_sym
);
10343 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
10345 eoinfo
->failed
= TRUE
;
10348 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
10350 if (flinfo
->hash_sec
!= NULL
)
10352 size_t hash_entry_size
;
10353 bfd_byte
*bucketpos
;
10355 size_t bucketcount
;
10358 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
10359 bucket
= h
->u
.elf_hash_value
% bucketcount
;
10362 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
10363 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
10364 + (bucket
+ 2) * hash_entry_size
);
10365 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
10366 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
10368 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
10369 ((bfd_byte
*) flinfo
->hash_sec
->contents
10370 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
10373 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
10375 Elf_Internal_Versym iversym
;
10376 Elf_External_Versym
*eversym
;
10378 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
10380 if (h
->verinfo
.verdef
== NULL
10381 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
10382 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
10383 iversym
.vs_vers
= 0;
10385 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
10389 if (h
->verinfo
.vertree
== NULL
)
10390 iversym
.vs_vers
= 1;
10392 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
10393 if (flinfo
->info
->create_default_symver
)
10397 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10398 defined locally. */
10399 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
10400 iversym
.vs_vers
|= VERSYM_HIDDEN
;
10402 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
10403 eversym
+= h
->dynindx
;
10404 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
10408 /* If the symbol is undefined, and we didn't output it to .dynsym,
10409 strip it from .symtab too. Obviously we can't do this for
10410 relocatable output or when needed for --emit-relocs. */
10411 else if (input_sec
== bfd_und_section_ptr
10413 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10414 && (h
->mark
!= 1 || ELF_ST_BIND (sym
.st_info
) != STB_GLOBAL
)
10415 && !bfd_link_relocatable (flinfo
->info
))
10418 /* Also strip others that we couldn't earlier due to dynamic symbol
10422 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
10425 /* Output a FILE symbol so that following locals are not associated
10426 with the wrong input file. We need one for forced local symbols
10427 if we've seen more than one FILE symbol or when we have exactly
10428 one FILE symbol but global symbols are present in a file other
10429 than the one with the FILE symbol. We also need one if linker
10430 defined symbols are present. In practice these conditions are
10431 always met, so just emit the FILE symbol unconditionally. */
10432 if (eoinfo
->localsyms
10433 && !eoinfo
->file_sym_done
10434 && eoinfo
->flinfo
->filesym_count
!= 0)
10436 Elf_Internal_Sym fsym
;
10438 memset (&fsym
, 0, sizeof (fsym
));
10439 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10440 fsym
.st_shndx
= SHN_ABS
;
10441 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
10442 bfd_und_section_ptr
, NULL
))
10445 eoinfo
->file_sym_done
= TRUE
;
10448 indx
= bfd_get_symcount (flinfo
->output_bfd
);
10449 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
10453 eoinfo
->failed
= TRUE
;
10458 else if (h
->indx
== -2)
10464 /* Return TRUE if special handling is done for relocs in SEC against
10465 symbols defined in discarded sections. */
10468 elf_section_ignore_discarded_relocs (asection
*sec
)
10470 const struct elf_backend_data
*bed
;
10472 switch (sec
->sec_info_type
)
10474 case SEC_INFO_TYPE_STABS
:
10475 case SEC_INFO_TYPE_EH_FRAME
:
10476 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10482 bed
= get_elf_backend_data (sec
->owner
);
10483 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
10484 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
10490 /* Return a mask saying how ld should treat relocations in SEC against
10491 symbols defined in discarded sections. If this function returns
10492 COMPLAIN set, ld will issue a warning message. If this function
10493 returns PRETEND set, and the discarded section was link-once and the
10494 same size as the kept link-once section, ld will pretend that the
10495 symbol was actually defined in the kept section. Otherwise ld will
10496 zero the reloc (at least that is the intent, but some cooperation by
10497 the target dependent code is needed, particularly for REL targets). */
10500 _bfd_elf_default_action_discarded (asection
*sec
)
10502 if (sec
->flags
& SEC_DEBUGGING
)
10505 if (strcmp (".eh_frame", sec
->name
) == 0)
10508 if (strcmp (".gcc_except_table", sec
->name
) == 0)
10511 return COMPLAIN
| PRETEND
;
10514 /* Find a match between a section and a member of a section group. */
10517 match_group_member (asection
*sec
, asection
*group
,
10518 struct bfd_link_info
*info
)
10520 asection
*first
= elf_next_in_group (group
);
10521 asection
*s
= first
;
10525 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
10528 s
= elf_next_in_group (s
);
10536 /* Check if the kept section of a discarded section SEC can be used
10537 to replace it. Return the replacement if it is OK. Otherwise return
10541 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
10545 kept
= sec
->kept_section
;
10548 if ((kept
->flags
& SEC_GROUP
) != 0)
10549 kept
= match_group_member (sec
, kept
, info
);
10551 && ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
10552 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
)))
10554 sec
->kept_section
= kept
;
10559 /* Link an input file into the linker output file. This function
10560 handles all the sections and relocations of the input file at once.
10561 This is so that we only have to read the local symbols once, and
10562 don't have to keep them in memory. */
10565 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
10567 int (*relocate_section
)
10568 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
10569 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
10571 Elf_Internal_Shdr
*symtab_hdr
;
10572 size_t locsymcount
;
10574 Elf_Internal_Sym
*isymbuf
;
10575 Elf_Internal_Sym
*isym
;
10576 Elf_Internal_Sym
*isymend
;
10578 asection
**ppsection
;
10580 const struct elf_backend_data
*bed
;
10581 struct elf_link_hash_entry
**sym_hashes
;
10582 bfd_size_type address_size
;
10583 bfd_vma r_type_mask
;
10585 bfd_boolean have_file_sym
= FALSE
;
10587 output_bfd
= flinfo
->output_bfd
;
10588 bed
= get_elf_backend_data (output_bfd
);
10589 relocate_section
= bed
->elf_backend_relocate_section
;
10591 /* If this is a dynamic object, we don't want to do anything here:
10592 we don't want the local symbols, and we don't want the section
10594 if ((input_bfd
->flags
& DYNAMIC
) != 0)
10597 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
10598 if (elf_bad_symtab (input_bfd
))
10600 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10605 locsymcount
= symtab_hdr
->sh_info
;
10606 extsymoff
= symtab_hdr
->sh_info
;
10609 /* Read the local symbols. */
10610 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10611 if (isymbuf
== NULL
&& locsymcount
!= 0)
10613 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
10614 flinfo
->internal_syms
,
10615 flinfo
->external_syms
,
10616 flinfo
->locsym_shndx
);
10617 if (isymbuf
== NULL
)
10621 /* Find local symbol sections and adjust values of symbols in
10622 SEC_MERGE sections. Write out those local symbols we know are
10623 going into the output file. */
10624 isymend
= isymbuf
+ locsymcount
;
10625 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
10627 isym
++, pindex
++, ppsection
++)
10631 Elf_Internal_Sym osym
;
10637 if (elf_bad_symtab (input_bfd
))
10639 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
10646 if (isym
->st_shndx
== SHN_UNDEF
)
10647 isec
= bfd_und_section_ptr
;
10648 else if (isym
->st_shndx
== SHN_ABS
)
10649 isec
= bfd_abs_section_ptr
;
10650 else if (isym
->st_shndx
== SHN_COMMON
)
10651 isec
= bfd_com_section_ptr
;
10654 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
10657 /* Don't attempt to output symbols with st_shnx in the
10658 reserved range other than SHN_ABS and SHN_COMMON. */
10659 isec
= bfd_und_section_ptr
;
10661 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
10662 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
10664 _bfd_merged_section_offset (output_bfd
, &isec
,
10665 elf_section_data (isec
)->sec_info
,
10671 /* Don't output the first, undefined, symbol. In fact, don't
10672 output any undefined local symbol. */
10673 if (isec
== bfd_und_section_ptr
)
10676 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
10678 /* We never output section symbols. Instead, we use the
10679 section symbol of the corresponding section in the output
10684 /* If we are stripping all symbols, we don't want to output this
10686 if (flinfo
->info
->strip
== strip_all
)
10689 /* If we are discarding all local symbols, we don't want to
10690 output this one. If we are generating a relocatable output
10691 file, then some of the local symbols may be required by
10692 relocs; we output them below as we discover that they are
10694 if (flinfo
->info
->discard
== discard_all
)
10697 /* If this symbol is defined in a section which we are
10698 discarding, we don't need to keep it. */
10699 if (isym
->st_shndx
!= SHN_UNDEF
10700 && isym
->st_shndx
< SHN_LORESERVE
10701 && isec
->output_section
== NULL
10702 && flinfo
->info
->non_contiguous_regions
10703 && flinfo
->info
->non_contiguous_regions_warnings
)
10705 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10706 "discards section `%s' from '%s'\n"),
10707 isec
->name
, bfd_get_filename (isec
->owner
));
10711 if (isym
->st_shndx
!= SHN_UNDEF
10712 && isym
->st_shndx
< SHN_LORESERVE
10713 && bfd_section_removed_from_list (output_bfd
,
10714 isec
->output_section
))
10717 /* Get the name of the symbol. */
10718 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
10723 /* See if we are discarding symbols with this name. */
10724 if ((flinfo
->info
->strip
== strip_some
10725 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
10727 || (((flinfo
->info
->discard
== discard_sec_merge
10728 && (isec
->flags
& SEC_MERGE
)
10729 && !bfd_link_relocatable (flinfo
->info
))
10730 || flinfo
->info
->discard
== discard_l
)
10731 && bfd_is_local_label_name (input_bfd
, name
)))
10734 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
10736 if (input_bfd
->lto_output
)
10737 /* -flto puts a temp file name here. This means builds
10738 are not reproducible. Discard the symbol. */
10740 have_file_sym
= TRUE
;
10741 flinfo
->filesym_count
+= 1;
10743 if (!have_file_sym
)
10745 /* In the absence of debug info, bfd_find_nearest_line uses
10746 FILE symbols to determine the source file for local
10747 function symbols. Provide a FILE symbol here if input
10748 files lack such, so that their symbols won't be
10749 associated with a previous input file. It's not the
10750 source file, but the best we can do. */
10751 have_file_sym
= TRUE
;
10752 flinfo
->filesym_count
+= 1;
10753 memset (&osym
, 0, sizeof (osym
));
10754 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10755 osym
.st_shndx
= SHN_ABS
;
10756 if (!elf_link_output_symstrtab (flinfo
,
10757 (input_bfd
->lto_output
? NULL
10758 : bfd_get_filename (input_bfd
)),
10759 &osym
, bfd_abs_section_ptr
,
10766 /* Adjust the section index for the output file. */
10767 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10768 isec
->output_section
);
10769 if (osym
.st_shndx
== SHN_BAD
)
10772 /* ELF symbols in relocatable files are section relative, but
10773 in executable files they are virtual addresses. Note that
10774 this code assumes that all ELF sections have an associated
10775 BFD section with a reasonable value for output_offset; below
10776 we assume that they also have a reasonable value for
10777 output_section. Any special sections must be set up to meet
10778 these requirements. */
10779 osym
.st_value
+= isec
->output_offset
;
10780 if (!bfd_link_relocatable (flinfo
->info
))
10782 osym
.st_value
+= isec
->output_section
->vma
;
10783 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
10785 /* STT_TLS symbols are relative to PT_TLS segment base. */
10786 if (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
)
10787 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
10789 osym
.st_info
= ELF_ST_INFO (ELF_ST_BIND (osym
.st_info
),
10794 indx
= bfd_get_symcount (output_bfd
);
10795 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
10802 if (bed
->s
->arch_size
== 32)
10804 r_type_mask
= 0xff;
10810 r_type_mask
= 0xffffffff;
10815 /* Relocate the contents of each section. */
10816 sym_hashes
= elf_sym_hashes (input_bfd
);
10817 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
10819 bfd_byte
*contents
;
10821 if (! o
->linker_mark
)
10823 /* This section was omitted from the link. */
10827 if (!flinfo
->info
->resolve_section_groups
10828 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
10830 /* Deal with the group signature symbol. */
10831 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
10832 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
10833 asection
*osec
= o
->output_section
;
10835 BFD_ASSERT (bfd_link_relocatable (flinfo
->info
));
10836 if (symndx
>= locsymcount
10837 || (elf_bad_symtab (input_bfd
)
10838 && flinfo
->sections
[symndx
] == NULL
))
10840 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
10841 while (h
->root
.type
== bfd_link_hash_indirect
10842 || h
->root
.type
== bfd_link_hash_warning
)
10843 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10844 /* Arrange for symbol to be output. */
10846 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
10848 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
10850 /* We'll use the output section target_index. */
10851 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10852 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
10856 if (flinfo
->indices
[symndx
] == -1)
10858 /* Otherwise output the local symbol now. */
10859 Elf_Internal_Sym sym
= isymbuf
[symndx
];
10860 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10865 name
= bfd_elf_string_from_elf_section (input_bfd
,
10866 symtab_hdr
->sh_link
,
10871 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10873 if (sym
.st_shndx
== SHN_BAD
)
10876 sym
.st_value
+= o
->output_offset
;
10878 indx
= bfd_get_symcount (output_bfd
);
10879 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
10884 flinfo
->indices
[symndx
] = indx
;
10888 elf_section_data (osec
)->this_hdr
.sh_info
10889 = flinfo
->indices
[symndx
];
10893 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
10894 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
10897 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
10899 /* Section was created by _bfd_elf_link_create_dynamic_sections
10904 /* Get the contents of the section. They have been cached by a
10905 relaxation routine. Note that o is a section in an input
10906 file, so the contents field will not have been set by any of
10907 the routines which work on output files. */
10908 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
10910 contents
= elf_section_data (o
)->this_hdr
.contents
;
10911 if (bed
->caches_rawsize
10913 && o
->rawsize
< o
->size
)
10915 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
10916 contents
= flinfo
->contents
;
10921 contents
= flinfo
->contents
;
10922 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
10926 if ((o
->flags
& SEC_RELOC
) != 0)
10928 Elf_Internal_Rela
*internal_relocs
;
10929 Elf_Internal_Rela
*rel
, *relend
;
10930 int action_discarded
;
10933 /* Get the swapped relocs. */
10935 = _bfd_elf_link_read_relocs (input_bfd
, o
, flinfo
->external_relocs
,
10936 flinfo
->internal_relocs
, FALSE
);
10937 if (internal_relocs
== NULL
10938 && o
->reloc_count
> 0)
10941 /* We need to reverse-copy input .ctors/.dtors sections if
10942 they are placed in .init_array/.finit_array for output. */
10943 if (o
->size
> address_size
10944 && ((strncmp (o
->name
, ".ctors", 6) == 0
10945 && strcmp (o
->output_section
->name
,
10946 ".init_array") == 0)
10947 || (strncmp (o
->name
, ".dtors", 6) == 0
10948 && strcmp (o
->output_section
->name
,
10949 ".fini_array") == 0))
10950 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
10952 if (o
->size
* bed
->s
->int_rels_per_ext_rel
10953 != o
->reloc_count
* address_size
)
10956 /* xgettext:c-format */
10957 (_("error: %pB: size of section %pA is not "
10958 "multiple of address size"),
10960 bfd_set_error (bfd_error_bad_value
);
10963 o
->flags
|= SEC_ELF_REVERSE_COPY
;
10966 action_discarded
= -1;
10967 if (!elf_section_ignore_discarded_relocs (o
))
10968 action_discarded
= (*bed
->action_discarded
) (o
);
10970 /* Run through the relocs evaluating complex reloc symbols and
10971 looking for relocs against symbols from discarded sections
10972 or section symbols from removed link-once sections.
10973 Complain about relocs against discarded sections. Zero
10974 relocs against removed link-once sections. */
10976 rel
= internal_relocs
;
10977 relend
= rel
+ o
->reloc_count
;
10978 for ( ; rel
< relend
; rel
++)
10980 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
10981 unsigned int s_type
;
10982 asection
**ps
, *sec
;
10983 struct elf_link_hash_entry
*h
= NULL
;
10984 const char *sym_name
;
10986 if (r_symndx
== STN_UNDEF
)
10989 if (r_symndx
>= locsymcount
10990 || (elf_bad_symtab (input_bfd
)
10991 && flinfo
->sections
[r_symndx
] == NULL
))
10993 h
= sym_hashes
[r_symndx
- extsymoff
];
10995 /* Badly formatted input files can contain relocs that
10996 reference non-existant symbols. Check here so that
10997 we do not seg fault. */
11001 /* xgettext:c-format */
11002 (_("error: %pB contains a reloc (%#" PRIx64
") for section %pA "
11003 "that references a non-existent global symbol"),
11004 input_bfd
, (uint64_t) rel
->r_info
, o
);
11005 bfd_set_error (bfd_error_bad_value
);
11009 while (h
->root
.type
== bfd_link_hash_indirect
11010 || h
->root
.type
== bfd_link_hash_warning
)
11011 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11015 /* If a plugin symbol is referenced from a non-IR file,
11016 mark the symbol as undefined. Note that the
11017 linker may attach linker created dynamic sections
11018 to the plugin bfd. Symbols defined in linker
11019 created sections are not plugin symbols. */
11020 if ((h
->root
.non_ir_ref_regular
11021 || h
->root
.non_ir_ref_dynamic
)
11022 && (h
->root
.type
== bfd_link_hash_defined
11023 || h
->root
.type
== bfd_link_hash_defweak
)
11024 && (h
->root
.u
.def
.section
->flags
11025 & SEC_LINKER_CREATED
) == 0
11026 && h
->root
.u
.def
.section
->owner
!= NULL
11027 && (h
->root
.u
.def
.section
->owner
->flags
11028 & BFD_PLUGIN
) != 0)
11030 h
->root
.type
= bfd_link_hash_undefined
;
11031 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
11035 if (h
->root
.type
== bfd_link_hash_defined
11036 || h
->root
.type
== bfd_link_hash_defweak
)
11037 ps
= &h
->root
.u
.def
.section
;
11039 sym_name
= h
->root
.root
.string
;
11043 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
11045 s_type
= ELF_ST_TYPE (sym
->st_info
);
11046 ps
= &flinfo
->sections
[r_symndx
];
11047 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
11051 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
11052 && !bfd_link_relocatable (flinfo
->info
))
11055 bfd_vma dot
= (rel
->r_offset
11056 + o
->output_offset
+ o
->output_section
->vma
);
11058 printf ("Encountered a complex symbol!");
11059 printf (" (input_bfd %s, section %s, reloc %ld\n",
11060 bfd_get_filename (input_bfd
), o
->name
,
11061 (long) (rel
- internal_relocs
));
11062 printf (" symbol: idx %8.8lx, name %s\n",
11063 r_symndx
, sym_name
);
11064 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11065 (unsigned long) rel
->r_info
,
11066 (unsigned long) rel
->r_offset
);
11068 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
11069 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
11072 /* Symbol evaluated OK. Update to absolute value. */
11073 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
11078 if (action_discarded
!= -1 && ps
!= NULL
)
11080 /* Complain if the definition comes from a
11081 discarded section. */
11082 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
11084 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
11085 if (action_discarded
& COMPLAIN
)
11086 (*flinfo
->info
->callbacks
->einfo
)
11087 /* xgettext:c-format */
11088 (_("%X`%s' referenced in section `%pA' of %pB: "
11089 "defined in discarded section `%pA' of %pB\n"),
11090 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
11092 /* Try to do the best we can to support buggy old
11093 versions of gcc. Pretend that the symbol is
11094 really defined in the kept linkonce section.
11095 FIXME: This is quite broken. Modifying the
11096 symbol here means we will be changing all later
11097 uses of the symbol, not just in this section. */
11098 if (action_discarded
& PRETEND
)
11102 kept
= _bfd_elf_check_kept_section (sec
,
11114 /* Relocate the section by invoking a back end routine.
11116 The back end routine is responsible for adjusting the
11117 section contents as necessary, and (if using Rela relocs
11118 and generating a relocatable output file) adjusting the
11119 reloc addend as necessary.
11121 The back end routine does not have to worry about setting
11122 the reloc address or the reloc symbol index.
11124 The back end routine is given a pointer to the swapped in
11125 internal symbols, and can access the hash table entries
11126 for the external symbols via elf_sym_hashes (input_bfd).
11128 When generating relocatable output, the back end routine
11129 must handle STB_LOCAL/STT_SECTION symbols specially. The
11130 output symbol is going to be a section symbol
11131 corresponding to the output section, which will require
11132 the addend to be adjusted. */
11134 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
11135 input_bfd
, o
, contents
,
11143 || bfd_link_relocatable (flinfo
->info
)
11144 || flinfo
->info
->emitrelocations
)
11146 Elf_Internal_Rela
*irela
;
11147 Elf_Internal_Rela
*irelaend
, *irelamid
;
11148 bfd_vma last_offset
;
11149 struct elf_link_hash_entry
**rel_hash
;
11150 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
11151 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
11152 unsigned int next_erel
;
11153 bfd_boolean rela_normal
;
11154 struct bfd_elf_section_data
*esdi
, *esdo
;
11156 esdi
= elf_section_data (o
);
11157 esdo
= elf_section_data (o
->output_section
);
11158 rela_normal
= FALSE
;
11160 /* Adjust the reloc addresses and symbol indices. */
11162 irela
= internal_relocs
;
11163 irelaend
= irela
+ o
->reloc_count
;
11164 rel_hash
= esdo
->rel
.hashes
+ esdo
->rel
.count
;
11165 /* We start processing the REL relocs, if any. When we reach
11166 IRELAMID in the loop, we switch to the RELA relocs. */
11168 if (esdi
->rel
.hdr
!= NULL
)
11169 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
11170 * bed
->s
->int_rels_per_ext_rel
);
11171 rel_hash_list
= rel_hash
;
11172 rela_hash_list
= NULL
;
11173 last_offset
= o
->output_offset
;
11174 if (!bfd_link_relocatable (flinfo
->info
))
11175 last_offset
+= o
->output_section
->vma
;
11176 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
11178 unsigned long r_symndx
;
11180 Elf_Internal_Sym sym
;
11182 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
11188 if (irela
== irelamid
)
11190 rel_hash
= esdo
->rela
.hashes
+ esdo
->rela
.count
;
11191 rela_hash_list
= rel_hash
;
11192 rela_normal
= bed
->rela_normal
;
11195 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
11198 if (irela
->r_offset
>= (bfd_vma
) -2)
11200 /* This is a reloc for a deleted entry or somesuch.
11201 Turn it into an R_*_NONE reloc, at the same
11202 offset as the last reloc. elf_eh_frame.c and
11203 bfd_elf_discard_info rely on reloc offsets
11205 irela
->r_offset
= last_offset
;
11207 irela
->r_addend
= 0;
11211 irela
->r_offset
+= o
->output_offset
;
11213 /* Relocs in an executable have to be virtual addresses. */
11214 if (!bfd_link_relocatable (flinfo
->info
))
11215 irela
->r_offset
+= o
->output_section
->vma
;
11217 last_offset
= irela
->r_offset
;
11219 r_symndx
= irela
->r_info
>> r_sym_shift
;
11220 if (r_symndx
== STN_UNDEF
)
11223 if (r_symndx
>= locsymcount
11224 || (elf_bad_symtab (input_bfd
)
11225 && flinfo
->sections
[r_symndx
] == NULL
))
11227 struct elf_link_hash_entry
*rh
;
11228 unsigned long indx
;
11230 /* This is a reloc against a global symbol. We
11231 have not yet output all the local symbols, so
11232 we do not know the symbol index of any global
11233 symbol. We set the rel_hash entry for this
11234 reloc to point to the global hash table entry
11235 for this symbol. The symbol index is then
11236 set at the end of bfd_elf_final_link. */
11237 indx
= r_symndx
- extsymoff
;
11238 rh
= elf_sym_hashes (input_bfd
)[indx
];
11239 while (rh
->root
.type
== bfd_link_hash_indirect
11240 || rh
->root
.type
== bfd_link_hash_warning
)
11241 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
11243 /* Setting the index to -2 tells
11244 elf_link_output_extsym that this symbol is
11245 used by a reloc. */
11246 BFD_ASSERT (rh
->indx
< 0);
11253 /* This is a reloc against a local symbol. */
11256 sym
= isymbuf
[r_symndx
];
11257 sec
= flinfo
->sections
[r_symndx
];
11258 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
11260 /* I suppose the backend ought to fill in the
11261 section of any STT_SECTION symbol against a
11262 processor specific section. */
11263 r_symndx
= STN_UNDEF
;
11264 if (bfd_is_abs_section (sec
))
11266 else if (sec
== NULL
|| sec
->owner
== NULL
)
11268 bfd_set_error (bfd_error_bad_value
);
11273 asection
*osec
= sec
->output_section
;
11275 /* If we have discarded a section, the output
11276 section will be the absolute section. In
11277 case of discarded SEC_MERGE sections, use
11278 the kept section. relocate_section should
11279 have already handled discarded linkonce
11281 if (bfd_is_abs_section (osec
)
11282 && sec
->kept_section
!= NULL
11283 && sec
->kept_section
->output_section
!= NULL
)
11285 osec
= sec
->kept_section
->output_section
;
11286 irela
->r_addend
-= osec
->vma
;
11289 if (!bfd_is_abs_section (osec
))
11291 r_symndx
= osec
->target_index
;
11292 if (r_symndx
== STN_UNDEF
)
11294 irela
->r_addend
+= osec
->vma
;
11295 osec
= _bfd_nearby_section (output_bfd
, osec
,
11297 irela
->r_addend
-= osec
->vma
;
11298 r_symndx
= osec
->target_index
;
11303 /* Adjust the addend according to where the
11304 section winds up in the output section. */
11306 irela
->r_addend
+= sec
->output_offset
;
11310 if (flinfo
->indices
[r_symndx
] == -1)
11312 unsigned long shlink
;
11317 if (flinfo
->info
->strip
== strip_all
)
11319 /* You can't do ld -r -s. */
11320 bfd_set_error (bfd_error_invalid_operation
);
11324 /* This symbol was skipped earlier, but
11325 since it is needed by a reloc, we
11326 must output it now. */
11327 shlink
= symtab_hdr
->sh_link
;
11328 name
= (bfd_elf_string_from_elf_section
11329 (input_bfd
, shlink
, sym
.st_name
));
11333 osec
= sec
->output_section
;
11335 _bfd_elf_section_from_bfd_section (output_bfd
,
11337 if (sym
.st_shndx
== SHN_BAD
)
11340 sym
.st_value
+= sec
->output_offset
;
11341 if (!bfd_link_relocatable (flinfo
->info
))
11343 sym
.st_value
+= osec
->vma
;
11344 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
11346 struct elf_link_hash_table
*htab
11347 = elf_hash_table (flinfo
->info
);
11349 /* STT_TLS symbols are relative to PT_TLS
11351 if (htab
->tls_sec
!= NULL
)
11352 sym
.st_value
-= htab
->tls_sec
->vma
;
11355 = ELF_ST_INFO (ELF_ST_BIND (sym
.st_info
),
11360 indx
= bfd_get_symcount (output_bfd
);
11361 ret
= elf_link_output_symstrtab (flinfo
, name
,
11367 flinfo
->indices
[r_symndx
] = indx
;
11372 r_symndx
= flinfo
->indices
[r_symndx
];
11375 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
11376 | (irela
->r_info
& r_type_mask
));
11379 /* Swap out the relocs. */
11380 input_rel_hdr
= esdi
->rel
.hdr
;
11381 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
11383 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11388 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
11389 * bed
->s
->int_rels_per_ext_rel
);
11390 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
11393 input_rela_hdr
= esdi
->rela
.hdr
;
11394 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
11396 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11405 /* Write out the modified section contents. */
11406 if (bed
->elf_backend_write_section
11407 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
11410 /* Section written out. */
11412 else switch (o
->sec_info_type
)
11414 case SEC_INFO_TYPE_STABS
:
11415 if (! (_bfd_write_section_stabs
11417 &elf_hash_table (flinfo
->info
)->stab_info
,
11418 o
, &elf_section_data (o
)->sec_info
, contents
)))
11421 case SEC_INFO_TYPE_MERGE
:
11422 if (! _bfd_write_merged_section (output_bfd
, o
,
11423 elf_section_data (o
)->sec_info
))
11426 case SEC_INFO_TYPE_EH_FRAME
:
11428 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
11433 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
11435 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
11443 if (! (o
->flags
& SEC_EXCLUDE
))
11445 file_ptr offset
= (file_ptr
) o
->output_offset
;
11446 bfd_size_type todo
= o
->size
;
11448 offset
*= bfd_octets_per_byte (output_bfd
, o
);
11450 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
11452 /* Reverse-copy input section to output. */
11455 todo
-= address_size
;
11456 if (! bfd_set_section_contents (output_bfd
,
11464 offset
+= address_size
;
11468 else if (! bfd_set_section_contents (output_bfd
,
11482 /* Generate a reloc when linking an ELF file. This is a reloc
11483 requested by the linker, and does not come from any input file. This
11484 is used to build constructor and destructor tables when linking
11488 elf_reloc_link_order (bfd
*output_bfd
,
11489 struct bfd_link_info
*info
,
11490 asection
*output_section
,
11491 struct bfd_link_order
*link_order
)
11493 reloc_howto_type
*howto
;
11497 struct bfd_elf_section_reloc_data
*reldata
;
11498 struct elf_link_hash_entry
**rel_hash_ptr
;
11499 Elf_Internal_Shdr
*rel_hdr
;
11500 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
11501 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
11504 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
11506 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
11509 bfd_set_error (bfd_error_bad_value
);
11513 addend
= link_order
->u
.reloc
.p
->addend
;
11516 reldata
= &esdo
->rel
;
11517 else if (esdo
->rela
.hdr
)
11518 reldata
= &esdo
->rela
;
11525 /* Figure out the symbol index. */
11526 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
11527 if (link_order
->type
== bfd_section_reloc_link_order
)
11529 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
11530 BFD_ASSERT (indx
!= 0);
11531 *rel_hash_ptr
= NULL
;
11535 struct elf_link_hash_entry
*h
;
11537 /* Treat a reloc against a defined symbol as though it were
11538 actually against the section. */
11539 h
= ((struct elf_link_hash_entry
*)
11540 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
11541 link_order
->u
.reloc
.p
->u
.name
,
11542 FALSE
, FALSE
, TRUE
));
11544 && (h
->root
.type
== bfd_link_hash_defined
11545 || h
->root
.type
== bfd_link_hash_defweak
))
11549 section
= h
->root
.u
.def
.section
;
11550 indx
= section
->output_section
->target_index
;
11551 *rel_hash_ptr
= NULL
;
11552 /* It seems that we ought to add the symbol value to the
11553 addend here, but in practice it has already been added
11554 because it was passed to constructor_callback. */
11555 addend
+= section
->output_section
->vma
+ section
->output_offset
;
11557 else if (h
!= NULL
)
11559 /* Setting the index to -2 tells elf_link_output_extsym that
11560 this symbol is used by a reloc. */
11567 (*info
->callbacks
->unattached_reloc
)
11568 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
11573 /* If this is an inplace reloc, we must write the addend into the
11575 if (howto
->partial_inplace
&& addend
!= 0)
11577 bfd_size_type size
;
11578 bfd_reloc_status_type rstat
;
11581 const char *sym_name
;
11582 bfd_size_type octets
;
11584 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
11585 buf
= (bfd_byte
*) bfd_zmalloc (size
);
11586 if (buf
== NULL
&& size
!= 0)
11588 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
11595 case bfd_reloc_outofrange
:
11598 case bfd_reloc_overflow
:
11599 if (link_order
->type
== bfd_section_reloc_link_order
)
11600 sym_name
= bfd_section_name (link_order
->u
.reloc
.p
->u
.section
);
11602 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
11603 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
11604 howto
->name
, addend
, NULL
, NULL
,
11609 octets
= link_order
->offset
* bfd_octets_per_byte (output_bfd
,
11611 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
11618 /* The address of a reloc is relative to the section in a
11619 relocatable file, and is a virtual address in an executable
11621 offset
= link_order
->offset
;
11622 if (! bfd_link_relocatable (info
))
11623 offset
+= output_section
->vma
;
11625 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
11627 irel
[i
].r_offset
= offset
;
11628 irel
[i
].r_info
= 0;
11629 irel
[i
].r_addend
= 0;
11631 if (bed
->s
->arch_size
== 32)
11632 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
11634 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
11636 rel_hdr
= reldata
->hdr
;
11637 erel
= rel_hdr
->contents
;
11638 if (rel_hdr
->sh_type
== SHT_REL
)
11640 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
11641 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
11645 irel
[0].r_addend
= addend
;
11646 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
11647 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
11656 /* Compare two sections based on the locations of the sections they are
11657 linked to. Used by elf_fixup_link_order. */
11660 compare_link_order (const void *a
, const void *b
)
11662 const struct bfd_link_order
*alo
= *(const struct bfd_link_order
**) a
;
11663 const struct bfd_link_order
*blo
= *(const struct bfd_link_order
**) b
;
11664 asection
*asec
= elf_linked_to_section (alo
->u
.indirect
.section
);
11665 asection
*bsec
= elf_linked_to_section (blo
->u
.indirect
.section
);
11666 bfd_vma apos
= asec
->output_section
->lma
+ asec
->output_offset
;
11667 bfd_vma bpos
= bsec
->output_section
->lma
+ bsec
->output_offset
;
11674 /* The only way we should get matching LMAs is when the first of two
11675 sections has zero size. */
11676 if (asec
->size
< bsec
->size
)
11678 if (asec
->size
> bsec
->size
)
11681 /* If they are both zero size then they almost certainly have the same
11682 VMA and thus are not ordered with respect to each other. Test VMA
11683 anyway, and fall back to id to make the result reproducible across
11684 qsort implementations. */
11685 apos
= asec
->output_section
->vma
+ asec
->output_offset
;
11686 bpos
= bsec
->output_section
->vma
+ bsec
->output_offset
;
11692 return asec
->id
- bsec
->id
;
11696 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11697 order as their linked sections. Returns false if this could not be done
11698 because an output section includes both ordered and unordered
11699 sections. Ideally we'd do this in the linker proper. */
11702 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
11704 size_t seen_linkorder
;
11707 struct bfd_link_order
*p
;
11709 struct bfd_link_order
**sections
;
11710 asection
*other_sec
, *linkorder_sec
;
11711 bfd_vma offset
; /* Octets. */
11714 linkorder_sec
= NULL
;
11716 seen_linkorder
= 0;
11717 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11719 if (p
->type
== bfd_indirect_link_order
)
11721 asection
*s
= p
->u
.indirect
.section
;
11723 if ((s
->flags
& SEC_LINKER_CREATED
) == 0
11724 && bfd_get_flavour (sub
) == bfd_target_elf_flavour
11725 && elf_section_data (s
) != NULL
11726 && elf_linked_to_section (s
) != NULL
)
11740 if (seen_other
&& seen_linkorder
)
11742 if (other_sec
&& linkorder_sec
)
11744 /* xgettext:c-format */
11745 (_("%pA has both ordered [`%pA' in %pB] "
11746 "and unordered [`%pA' in %pB] sections"),
11747 o
, linkorder_sec
, linkorder_sec
->owner
,
11748 other_sec
, other_sec
->owner
);
11751 (_("%pA has both ordered and unordered sections"), o
);
11752 bfd_set_error (bfd_error_bad_value
);
11757 if (!seen_linkorder
)
11760 sections
= bfd_malloc (seen_linkorder
* sizeof (*sections
));
11761 if (sections
== NULL
)
11764 seen_linkorder
= 0;
11765 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11766 sections
[seen_linkorder
++] = p
;
11768 /* Sort the input sections in the order of their linked section. */
11769 qsort (sections
, seen_linkorder
, sizeof (*sections
), compare_link_order
);
11771 /* Change the offsets of the sections. */
11773 for (n
= 0; n
< seen_linkorder
; n
++)
11776 asection
*s
= sections
[n
]->u
.indirect
.section
;
11777 unsigned int opb
= bfd_octets_per_byte (abfd
, s
);
11779 mask
= ~(bfd_vma
) 0 << s
->alignment_power
* opb
;
11780 offset
= (offset
+ ~mask
) & mask
;
11781 sections
[n
]->offset
= s
->output_offset
= offset
/ opb
;
11782 offset
+= sections
[n
]->size
;
11789 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11790 Returns TRUE upon success, FALSE otherwise. */
11793 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
11795 bfd_boolean ret
= FALSE
;
11797 const struct elf_backend_data
*bed
;
11799 enum bfd_architecture arch
;
11801 asymbol
**sympp
= NULL
;
11805 elf_symbol_type
*osymbuf
;
11808 implib_bfd
= info
->out_implib_bfd
;
11809 bed
= get_elf_backend_data (abfd
);
11811 if (!bfd_set_format (implib_bfd
, bfd_object
))
11814 /* Use flag from executable but make it a relocatable object. */
11815 flags
= bfd_get_file_flags (abfd
);
11816 flags
&= ~HAS_RELOC
;
11817 if (!bfd_set_start_address (implib_bfd
, 0)
11818 || !bfd_set_file_flags (implib_bfd
, flags
& ~EXEC_P
))
11821 /* Copy architecture of output file to import library file. */
11822 arch
= bfd_get_arch (abfd
);
11823 mach
= bfd_get_mach (abfd
);
11824 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
11825 && (abfd
->target_defaulted
11826 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
11829 /* Get symbol table size. */
11830 symsize
= bfd_get_symtab_upper_bound (abfd
);
11834 /* Read in the symbol table. */
11835 sympp
= (asymbol
**) bfd_malloc (symsize
);
11839 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
11843 /* Allow the BFD backend to copy any private header data it
11844 understands from the output BFD to the import library BFD. */
11845 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
11848 /* Filter symbols to appear in the import library. */
11849 if (bed
->elf_backend_filter_implib_symbols
)
11850 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
11853 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
11856 bfd_set_error (bfd_error_no_symbols
);
11857 _bfd_error_handler (_("%pB: no symbol found for import library"),
11863 /* Make symbols absolute. */
11864 amt
= symcount
* sizeof (*osymbuf
);
11865 osymbuf
= (elf_symbol_type
*) bfd_alloc (implib_bfd
, amt
);
11866 if (osymbuf
== NULL
)
11869 for (src_count
= 0; src_count
< symcount
; src_count
++)
11871 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
11872 sizeof (*osymbuf
));
11873 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
11874 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
11875 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
11876 osymbuf
[src_count
].internal_elf_sym
.st_value
=
11877 osymbuf
[src_count
].symbol
.value
;
11878 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
11881 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
11883 /* Allow the BFD backend to copy any private data it understands
11884 from the output BFD to the import library BFD. This is done last
11885 to permit the routine to look at the filtered symbol table. */
11886 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
11889 if (!bfd_close (implib_bfd
))
11900 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
11904 if (flinfo
->symstrtab
!= NULL
)
11905 _bfd_elf_strtab_free (flinfo
->symstrtab
);
11906 free (flinfo
->contents
);
11907 free (flinfo
->external_relocs
);
11908 free (flinfo
->internal_relocs
);
11909 free (flinfo
->external_syms
);
11910 free (flinfo
->locsym_shndx
);
11911 free (flinfo
->internal_syms
);
11912 free (flinfo
->indices
);
11913 free (flinfo
->sections
);
11914 if (flinfo
->symshndxbuf
!= (Elf_External_Sym_Shndx
*) -1)
11915 free (flinfo
->symshndxbuf
);
11916 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
11918 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11919 free (esdo
->rel
.hashes
);
11920 free (esdo
->rela
.hashes
);
11924 /* Do the final step of an ELF link. */
11927 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
11929 bfd_boolean dynamic
;
11930 bfd_boolean emit_relocs
;
11932 struct elf_final_link_info flinfo
;
11934 struct bfd_link_order
*p
;
11936 bfd_size_type max_contents_size
;
11937 bfd_size_type max_external_reloc_size
;
11938 bfd_size_type max_internal_reloc_count
;
11939 bfd_size_type max_sym_count
;
11940 bfd_size_type max_sym_shndx_count
;
11941 Elf_Internal_Sym elfsym
;
11943 Elf_Internal_Shdr
*symtab_hdr
;
11944 Elf_Internal_Shdr
*symtab_shndx_hdr
;
11945 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11946 struct elf_outext_info eoinfo
;
11947 bfd_boolean merged
;
11948 size_t relativecount
= 0;
11949 asection
*reldyn
= 0;
11951 asection
*attr_section
= NULL
;
11952 bfd_vma attr_size
= 0;
11953 const char *std_attrs_section
;
11954 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
11955 bfd_boolean sections_removed
;
11957 if (!is_elf_hash_table (htab
))
11960 if (bfd_link_pic (info
))
11961 abfd
->flags
|= DYNAMIC
;
11963 dynamic
= htab
->dynamic_sections_created
;
11964 dynobj
= htab
->dynobj
;
11966 emit_relocs
= (bfd_link_relocatable (info
)
11967 || info
->emitrelocations
);
11969 flinfo
.info
= info
;
11970 flinfo
.output_bfd
= abfd
;
11971 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
11972 if (flinfo
.symstrtab
== NULL
)
11977 flinfo
.hash_sec
= NULL
;
11978 flinfo
.symver_sec
= NULL
;
11982 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
11983 /* Note that dynsym_sec can be NULL (on VMS). */
11984 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
11985 /* Note that it is OK if symver_sec is NULL. */
11988 flinfo
.contents
= NULL
;
11989 flinfo
.external_relocs
= NULL
;
11990 flinfo
.internal_relocs
= NULL
;
11991 flinfo
.external_syms
= NULL
;
11992 flinfo
.locsym_shndx
= NULL
;
11993 flinfo
.internal_syms
= NULL
;
11994 flinfo
.indices
= NULL
;
11995 flinfo
.sections
= NULL
;
11996 flinfo
.symshndxbuf
= NULL
;
11997 flinfo
.filesym_count
= 0;
11999 /* The object attributes have been merged. Remove the input
12000 sections from the link, and set the contents of the output
12002 sections_removed
= FALSE
;
12003 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
12004 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12006 bfd_boolean remove_section
= FALSE
;
12008 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
12009 || strcmp (o
->name
, ".gnu.attributes") == 0)
12011 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12013 asection
*input_section
;
12015 if (p
->type
!= bfd_indirect_link_order
)
12017 input_section
= p
->u
.indirect
.section
;
12018 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12019 elf_link_input_bfd ignores this section. */
12020 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
12023 attr_size
= bfd_elf_obj_attr_size (abfd
);
12024 bfd_set_section_size (o
, attr_size
);
12025 /* Skip this section later on. */
12026 o
->map_head
.link_order
= NULL
;
12030 remove_section
= TRUE
;
12032 else if ((o
->flags
& SEC_GROUP
) != 0 && o
->size
== 0)
12034 /* Remove empty group section from linker output. */
12035 remove_section
= TRUE
;
12037 if (remove_section
)
12039 o
->flags
|= SEC_EXCLUDE
;
12040 bfd_section_list_remove (abfd
, o
);
12041 abfd
->section_count
--;
12042 sections_removed
= TRUE
;
12045 if (sections_removed
)
12046 _bfd_fix_excluded_sec_syms (abfd
, info
);
12048 /* Count up the number of relocations we will output for each output
12049 section, so that we know the sizes of the reloc sections. We
12050 also figure out some maximum sizes. */
12051 max_contents_size
= 0;
12052 max_external_reloc_size
= 0;
12053 max_internal_reloc_count
= 0;
12055 max_sym_shndx_count
= 0;
12057 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12059 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12060 o
->reloc_count
= 0;
12062 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12064 unsigned int reloc_count
= 0;
12065 unsigned int additional_reloc_count
= 0;
12066 struct bfd_elf_section_data
*esdi
= NULL
;
12068 if (p
->type
== bfd_section_reloc_link_order
12069 || p
->type
== bfd_symbol_reloc_link_order
)
12071 else if (p
->type
== bfd_indirect_link_order
)
12075 sec
= p
->u
.indirect
.section
;
12077 /* Mark all sections which are to be included in the
12078 link. This will normally be every section. We need
12079 to do this so that we can identify any sections which
12080 the linker has decided to not include. */
12081 sec
->linker_mark
= TRUE
;
12083 if (sec
->flags
& SEC_MERGE
)
12086 if (sec
->rawsize
> max_contents_size
)
12087 max_contents_size
= sec
->rawsize
;
12088 if (sec
->size
> max_contents_size
)
12089 max_contents_size
= sec
->size
;
12091 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
12092 && (sec
->owner
->flags
& DYNAMIC
) == 0)
12096 /* We are interested in just local symbols, not all
12098 if (elf_bad_symtab (sec
->owner
))
12099 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
12100 / bed
->s
->sizeof_sym
);
12102 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
12104 if (sym_count
> max_sym_count
)
12105 max_sym_count
= sym_count
;
12107 if (sym_count
> max_sym_shndx_count
12108 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
12109 max_sym_shndx_count
= sym_count
;
12111 if (esdo
->this_hdr
.sh_type
== SHT_REL
12112 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
12113 /* Some backends use reloc_count in relocation sections
12114 to count particular types of relocs. Of course,
12115 reloc sections themselves can't have relocations. */
12117 else if (emit_relocs
)
12119 reloc_count
= sec
->reloc_count
;
12120 if (bed
->elf_backend_count_additional_relocs
)
12123 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
12124 additional_reloc_count
+= c
;
12127 else if (bed
->elf_backend_count_relocs
)
12128 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
12130 esdi
= elf_section_data (sec
);
12132 if ((sec
->flags
& SEC_RELOC
) != 0)
12134 size_t ext_size
= 0;
12136 if (esdi
->rel
.hdr
!= NULL
)
12137 ext_size
= esdi
->rel
.hdr
->sh_size
;
12138 if (esdi
->rela
.hdr
!= NULL
)
12139 ext_size
+= esdi
->rela
.hdr
->sh_size
;
12141 if (ext_size
> max_external_reloc_size
)
12142 max_external_reloc_size
= ext_size
;
12143 if (sec
->reloc_count
> max_internal_reloc_count
)
12144 max_internal_reloc_count
= sec
->reloc_count
;
12149 if (reloc_count
== 0)
12152 reloc_count
+= additional_reloc_count
;
12153 o
->reloc_count
+= reloc_count
;
12155 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
12159 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
12160 esdo
->rel
.count
+= additional_reloc_count
;
12162 if (esdi
->rela
.hdr
)
12164 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
12165 esdo
->rela
.count
+= additional_reloc_count
;
12171 esdo
->rela
.count
+= reloc_count
;
12173 esdo
->rel
.count
+= reloc_count
;
12177 if (o
->reloc_count
> 0)
12178 o
->flags
|= SEC_RELOC
;
12181 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12182 set it (this is probably a bug) and if it is set
12183 assign_section_numbers will create a reloc section. */
12184 o
->flags
&=~ SEC_RELOC
;
12187 /* If the SEC_ALLOC flag is not set, force the section VMA to
12188 zero. This is done in elf_fake_sections as well, but forcing
12189 the VMA to 0 here will ensure that relocs against these
12190 sections are handled correctly. */
12191 if ((o
->flags
& SEC_ALLOC
) == 0
12192 && ! o
->user_set_vma
)
12196 if (! bfd_link_relocatable (info
) && merged
)
12197 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
12199 /* Figure out the file positions for everything but the symbol table
12200 and the relocs. We set symcount to force assign_section_numbers
12201 to create a symbol table. */
12202 abfd
->symcount
= info
->strip
!= strip_all
|| emit_relocs
;
12203 BFD_ASSERT (! abfd
->output_has_begun
);
12204 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
12207 /* Set sizes, and assign file positions for reloc sections. */
12208 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12210 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12211 if ((o
->flags
& SEC_RELOC
) != 0)
12214 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
12218 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
12222 /* _bfd_elf_compute_section_file_positions makes temporary use
12223 of target_index. Reset it. */
12224 o
->target_index
= 0;
12226 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12227 to count upwards while actually outputting the relocations. */
12228 esdo
->rel
.count
= 0;
12229 esdo
->rela
.count
= 0;
12231 if ((esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
12232 && !bfd_section_is_ctf (o
))
12234 /* Cache the section contents so that they can be compressed
12235 later. Use bfd_malloc since it will be freed by
12236 bfd_compress_section_contents. */
12237 unsigned char *contents
= esdo
->this_hdr
.contents
;
12238 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
12241 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
12242 if (contents
== NULL
)
12244 esdo
->this_hdr
.contents
= contents
;
12248 /* We have now assigned file positions for all the sections except .symtab,
12249 .strtab, and non-loaded reloc and compressed debugging sections. We start
12250 the .symtab section at the current file position, and write directly to it.
12251 We build the .strtab section in memory. */
12252 abfd
->symcount
= 0;
12253 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12254 /* sh_name is set in prep_headers. */
12255 symtab_hdr
->sh_type
= SHT_SYMTAB
;
12256 /* sh_flags, sh_addr and sh_size all start off zero. */
12257 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
12258 /* sh_link is set in assign_section_numbers. */
12259 /* sh_info is set below. */
12260 /* sh_offset is set just below. */
12261 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
12263 if (max_sym_count
< 20)
12264 max_sym_count
= 20;
12265 htab
->strtabsize
= max_sym_count
;
12266 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
12267 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
12268 if (htab
->strtab
== NULL
)
12270 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12272 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
12273 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
12275 if (info
->strip
!= strip_all
|| emit_relocs
)
12277 bfd_boolean name_local_sections
;
12280 file_ptr off
= elf_next_file_pos (abfd
);
12282 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
12284 /* Note that at this point elf_next_file_pos (abfd) is
12285 incorrect. We do not yet know the size of the .symtab section.
12286 We correct next_file_pos below, after we do know the size. */
12288 /* Start writing out the symbol table. The first symbol is always a
12290 elfsym
.st_value
= 0;
12291 elfsym
.st_size
= 0;
12292 elfsym
.st_info
= 0;
12293 elfsym
.st_other
= 0;
12294 elfsym
.st_shndx
= SHN_UNDEF
;
12295 elfsym
.st_target_internal
= 0;
12296 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
12297 bfd_und_section_ptr
, NULL
) != 1)
12300 /* Output a symbol for each section. We output these even if we are
12301 discarding local symbols, since they are used for relocs. These
12302 symbols usually have no names. We store the index of each one in
12303 the index field of the section, so that we can find it again when
12304 outputting relocs. */
12306 name_local_sections
12307 = (bed
->elf_backend_name_local_section_symbols
12308 && bed
->elf_backend_name_local_section_symbols (abfd
));
12311 elfsym
.st_size
= 0;
12312 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12313 elfsym
.st_other
= 0;
12314 elfsym
.st_value
= 0;
12315 elfsym
.st_target_internal
= 0;
12316 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12318 o
= bfd_section_from_elf_index (abfd
, i
);
12321 o
->target_index
= bfd_get_symcount (abfd
);
12322 elfsym
.st_shndx
= i
;
12323 if (!bfd_link_relocatable (info
))
12324 elfsym
.st_value
= o
->vma
;
12325 if (name_local_sections
)
12327 if (elf_link_output_symstrtab (&flinfo
, name
, &elfsym
, o
,
12334 /* On some targets like Irix 5 the symbol split between local and global
12335 ones recorded in the sh_info field needs to be done between section
12336 and all other symbols. */
12337 if (bed
->elf_backend_elfsym_local_is_section
12338 && bed
->elf_backend_elfsym_local_is_section (abfd
))
12339 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12341 /* Allocate some memory to hold information read in from the input
12343 if (max_contents_size
!= 0)
12345 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
12346 if (flinfo
.contents
== NULL
)
12350 if (max_external_reloc_size
!= 0)
12352 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
12353 if (flinfo
.external_relocs
== NULL
)
12357 if (max_internal_reloc_count
!= 0)
12359 amt
= max_internal_reloc_count
* sizeof (Elf_Internal_Rela
);
12360 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
12361 if (flinfo
.internal_relocs
== NULL
)
12365 if (max_sym_count
!= 0)
12367 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
12368 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
12369 if (flinfo
.external_syms
== NULL
)
12372 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
12373 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
12374 if (flinfo
.internal_syms
== NULL
)
12377 amt
= max_sym_count
* sizeof (long);
12378 flinfo
.indices
= (long int *) bfd_malloc (amt
);
12379 if (flinfo
.indices
== NULL
)
12382 amt
= max_sym_count
* sizeof (asection
*);
12383 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
12384 if (flinfo
.sections
== NULL
)
12388 if (max_sym_shndx_count
!= 0)
12390 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
12391 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
12392 if (flinfo
.locsym_shndx
== NULL
)
12398 bfd_vma base
, end
= 0; /* Both bytes. */
12401 for (sec
= htab
->tls_sec
;
12402 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
12405 bfd_size_type size
= sec
->size
;
12406 unsigned int opb
= bfd_octets_per_byte (abfd
, sec
);
12409 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
12411 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
12414 size
= ord
->offset
* opb
+ ord
->size
;
12416 end
= sec
->vma
+ size
/ opb
;
12418 base
= htab
->tls_sec
->vma
;
12419 /* Only align end of TLS section if static TLS doesn't have special
12420 alignment requirements. */
12421 if (bed
->static_tls_alignment
== 1)
12422 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
12423 htab
->tls_size
= end
- base
;
12426 /* Reorder SHF_LINK_ORDER sections. */
12427 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12429 if (!elf_fixup_link_order (abfd
, o
))
12433 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
12436 /* Since ELF permits relocations to be against local symbols, we
12437 must have the local symbols available when we do the relocations.
12438 Since we would rather only read the local symbols once, and we
12439 would rather not keep them in memory, we handle all the
12440 relocations for a single input file at the same time.
12442 Unfortunately, there is no way to know the total number of local
12443 symbols until we have seen all of them, and the local symbol
12444 indices precede the global symbol indices. This means that when
12445 we are generating relocatable output, and we see a reloc against
12446 a global symbol, we can not know the symbol index until we have
12447 finished examining all the local symbols to see which ones we are
12448 going to output. To deal with this, we keep the relocations in
12449 memory, and don't output them until the end of the link. This is
12450 an unfortunate waste of memory, but I don't see a good way around
12451 it. Fortunately, it only happens when performing a relocatable
12452 link, which is not the common case. FIXME: If keep_memory is set
12453 we could write the relocs out and then read them again; I don't
12454 know how bad the memory loss will be. */
12456 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12457 sub
->output_has_begun
= FALSE
;
12458 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12460 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12462 if (p
->type
== bfd_indirect_link_order
12463 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
12464 == bfd_target_elf_flavour
)
12465 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
12467 if (! sub
->output_has_begun
)
12469 if (! elf_link_input_bfd (&flinfo
, sub
))
12471 sub
->output_has_begun
= TRUE
;
12474 else if (p
->type
== bfd_section_reloc_link_order
12475 || p
->type
== bfd_symbol_reloc_link_order
)
12477 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
12482 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
12484 if (p
->type
== bfd_indirect_link_order
12485 && (bfd_get_flavour (sub
)
12486 == bfd_target_elf_flavour
)
12487 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
12488 != bed
->s
->elfclass
))
12490 const char *iclass
, *oclass
;
12492 switch (bed
->s
->elfclass
)
12494 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
12495 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
12496 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
12500 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
12502 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
12503 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
12504 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
12508 bfd_set_error (bfd_error_wrong_format
);
12510 /* xgettext:c-format */
12511 (_("%pB: file class %s incompatible with %s"),
12512 sub
, iclass
, oclass
);
12521 /* Free symbol buffer if needed. */
12522 if (!info
->reduce_memory_overheads
)
12524 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12525 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
12527 free (elf_tdata (sub
)->symbuf
);
12528 elf_tdata (sub
)->symbuf
= NULL
;
12532 /* Output any global symbols that got converted to local in a
12533 version script or due to symbol visibility. We do this in a
12534 separate step since ELF requires all local symbols to appear
12535 prior to any global symbols. FIXME: We should only do this if
12536 some global symbols were, in fact, converted to become local.
12537 FIXME: Will this work correctly with the Irix 5 linker? */
12538 eoinfo
.failed
= FALSE
;
12539 eoinfo
.flinfo
= &flinfo
;
12540 eoinfo
.localsyms
= TRUE
;
12541 eoinfo
.file_sym_done
= FALSE
;
12542 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12546 /* If backend needs to output some local symbols not present in the hash
12547 table, do it now. */
12548 if (bed
->elf_backend_output_arch_local_syms
12549 && (info
->strip
!= strip_all
|| emit_relocs
))
12551 typedef int (*out_sym_func
)
12552 (void *, const char *, Elf_Internal_Sym
*, asection
*,
12553 struct elf_link_hash_entry
*);
12555 if (! ((*bed
->elf_backend_output_arch_local_syms
)
12556 (abfd
, info
, &flinfo
,
12557 (out_sym_func
) elf_link_output_symstrtab
)))
12561 /* That wrote out all the local symbols. Finish up the symbol table
12562 with the global symbols. Even if we want to strip everything we
12563 can, we still need to deal with those global symbols that got
12564 converted to local in a version script. */
12566 /* The sh_info field records the index of the first non local symbol. */
12567 if (!symtab_hdr
->sh_info
)
12568 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12571 && htab
->dynsym
!= NULL
12572 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
12574 Elf_Internal_Sym sym
;
12575 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
12577 o
= htab
->dynsym
->output_section
;
12578 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
12580 /* Write out the section symbols for the output sections. */
12581 if (bfd_link_pic (info
)
12582 || htab
->is_relocatable_executable
)
12588 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12590 sym
.st_target_internal
= 0;
12592 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
12598 dynindx
= elf_section_data (s
)->dynindx
;
12601 indx
= elf_section_data (s
)->this_idx
;
12602 BFD_ASSERT (indx
> 0);
12603 sym
.st_shndx
= indx
;
12604 if (! check_dynsym (abfd
, &sym
))
12606 sym
.st_value
= s
->vma
;
12607 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
12608 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12612 /* Write out the local dynsyms. */
12613 if (htab
->dynlocal
)
12615 struct elf_link_local_dynamic_entry
*e
;
12616 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
12621 /* Copy the internal symbol and turn off visibility.
12622 Note that we saved a word of storage and overwrote
12623 the original st_name with the dynstr_index. */
12625 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
12626 sym
.st_shndx
= SHN_UNDEF
;
12628 s
= bfd_section_from_elf_index (e
->input_bfd
,
12631 && s
->output_section
!= NULL
12632 && elf_section_data (s
->output_section
) != NULL
)
12635 elf_section_data (s
->output_section
)->this_idx
;
12636 if (! check_dynsym (abfd
, &sym
))
12638 sym
.st_value
= (s
->output_section
->vma
12640 + e
->isym
.st_value
);
12643 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
12644 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12649 /* We get the global symbols from the hash table. */
12650 eoinfo
.failed
= FALSE
;
12651 eoinfo
.localsyms
= FALSE
;
12652 eoinfo
.flinfo
= &flinfo
;
12653 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12657 /* If backend needs to output some symbols not present in the hash
12658 table, do it now. */
12659 if (bed
->elf_backend_output_arch_syms
12660 && (info
->strip
!= strip_all
|| emit_relocs
))
12662 typedef int (*out_sym_func
)
12663 (void *, const char *, Elf_Internal_Sym
*, asection
*,
12664 struct elf_link_hash_entry
*);
12666 if (! ((*bed
->elf_backend_output_arch_syms
)
12667 (abfd
, info
, &flinfo
,
12668 (out_sym_func
) elf_link_output_symstrtab
)))
12672 /* Finalize the .strtab section. */
12673 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
12675 /* Swap out the .strtab section. */
12676 if (!elf_link_swap_symbols_out (&flinfo
))
12679 /* Now we know the size of the symtab section. */
12680 if (bfd_get_symcount (abfd
) > 0)
12682 /* Finish up and write out the symbol string table (.strtab)
12684 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
12685 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
12687 if (elf_symtab_shndx_list (abfd
))
12689 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
12691 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
12693 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
12694 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
12695 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
12696 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
12697 symtab_shndx_hdr
->sh_size
= amt
;
12699 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
12702 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
12703 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
12708 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
12709 /* sh_name was set in prep_headers. */
12710 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
12711 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
12712 symstrtab_hdr
->sh_addr
= 0;
12713 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
12714 symstrtab_hdr
->sh_entsize
= 0;
12715 symstrtab_hdr
->sh_link
= 0;
12716 symstrtab_hdr
->sh_info
= 0;
12717 /* sh_offset is set just below. */
12718 symstrtab_hdr
->sh_addralign
= 1;
12720 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
12722 elf_next_file_pos (abfd
) = off
;
12724 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
12725 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
12729 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
12731 _bfd_error_handler (_("%pB: failed to generate import library"),
12732 info
->out_implib_bfd
);
12736 /* Adjust the relocs to have the correct symbol indices. */
12737 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12739 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12742 if ((o
->flags
& SEC_RELOC
) == 0)
12745 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
12746 if (esdo
->rel
.hdr
!= NULL
12747 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
, info
))
12749 if (esdo
->rela
.hdr
!= NULL
12750 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
, info
))
12753 /* Set the reloc_count field to 0 to prevent write_relocs from
12754 trying to swap the relocs out itself. */
12755 o
->reloc_count
= 0;
12758 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
12759 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
12761 /* If we are linking against a dynamic object, or generating a
12762 shared library, finish up the dynamic linking information. */
12765 bfd_byte
*dyncon
, *dynconend
;
12767 /* Fix up .dynamic entries. */
12768 o
= bfd_get_linker_section (dynobj
, ".dynamic");
12769 BFD_ASSERT (o
!= NULL
);
12771 dyncon
= o
->contents
;
12772 dynconend
= o
->contents
+ o
->size
;
12773 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12775 Elf_Internal_Dyn dyn
;
12778 bfd_size_type sh_size
;
12781 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12788 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
12790 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
12792 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
12793 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
12796 dyn
.d_un
.d_val
= relativecount
;
12803 name
= info
->init_function
;
12806 name
= info
->fini_function
;
12809 struct elf_link_hash_entry
*h
;
12811 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
12813 && (h
->root
.type
== bfd_link_hash_defined
12814 || h
->root
.type
== bfd_link_hash_defweak
))
12816 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
12817 o
= h
->root
.u
.def
.section
;
12818 if (o
->output_section
!= NULL
)
12819 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
12820 + o
->output_offset
);
12823 /* The symbol is imported from another shared
12824 library and does not apply to this one. */
12825 dyn
.d_un
.d_ptr
= 0;
12832 case DT_PREINIT_ARRAYSZ
:
12833 name
= ".preinit_array";
12835 case DT_INIT_ARRAYSZ
:
12836 name
= ".init_array";
12838 case DT_FINI_ARRAYSZ
:
12839 name
= ".fini_array";
12841 o
= bfd_get_section_by_name (abfd
, name
);
12845 (_("could not find section %s"), name
);
12850 (_("warning: %s section has zero size"), name
);
12851 dyn
.d_un
.d_val
= o
->size
;
12854 case DT_PREINIT_ARRAY
:
12855 name
= ".preinit_array";
12857 case DT_INIT_ARRAY
:
12858 name
= ".init_array";
12860 case DT_FINI_ARRAY
:
12861 name
= ".fini_array";
12863 o
= bfd_get_section_by_name (abfd
, name
);
12870 name
= ".gnu.hash";
12879 name
= ".gnu.version_d";
12882 name
= ".gnu.version_r";
12885 name
= ".gnu.version";
12887 o
= bfd_get_linker_section (dynobj
, name
);
12889 if (o
== NULL
|| bfd_is_abs_section (o
->output_section
))
12892 (_("could not find section %s"), name
);
12895 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
12898 (_("warning: section '%s' is being made into a note"), name
);
12899 bfd_set_error (bfd_error_nonrepresentable_section
);
12902 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
12909 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
12915 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12917 Elf_Internal_Shdr
*hdr
;
12919 hdr
= elf_elfsections (abfd
)[i
];
12920 if (hdr
->sh_type
== type
12921 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
12923 sh_size
+= hdr
->sh_size
;
12925 || sh_addr
> hdr
->sh_addr
)
12926 sh_addr
= hdr
->sh_addr
;
12930 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
12932 unsigned int opb
= bfd_octets_per_byte (abfd
, o
);
12934 /* Don't count procedure linkage table relocs in the
12935 overall reloc count. */
12936 sh_size
-= htab
->srelplt
->size
;
12938 /* If the size is zero, make the address zero too.
12939 This is to avoid a glibc bug. If the backend
12940 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12941 zero, then we'll put DT_RELA at the end of
12942 DT_JMPREL. glibc will interpret the end of
12943 DT_RELA matching the end of DT_JMPREL as the
12944 case where DT_RELA includes DT_JMPREL, and for
12945 LD_BIND_NOW will decide that processing DT_RELA
12946 will process the PLT relocs too. Net result:
12947 No PLT relocs applied. */
12950 /* If .rela.plt is the first .rela section, exclude
12951 it from DT_RELA. */
12952 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
12953 + htab
->srelplt
->output_offset
) * opb
)
12954 sh_addr
+= htab
->srelplt
->size
;
12957 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
12958 dyn
.d_un
.d_val
= sh_size
;
12960 dyn
.d_un
.d_ptr
= sh_addr
;
12963 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
12967 /* If we have created any dynamic sections, then output them. */
12968 if (dynobj
!= NULL
)
12970 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
12973 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12974 if (bfd_link_textrel_check (info
)
12975 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
)
12977 bfd_byte
*dyncon
, *dynconend
;
12979 dyncon
= o
->contents
;
12980 dynconend
= o
->contents
+ o
->size
;
12981 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12983 Elf_Internal_Dyn dyn
;
12985 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12987 if (dyn
.d_tag
== DT_TEXTREL
)
12989 if (info
->textrel_check
== textrel_check_error
)
12990 info
->callbacks
->einfo
12991 (_("%P%X: read-only segment has dynamic relocations\n"));
12992 else if (bfd_link_dll (info
))
12993 info
->callbacks
->einfo
12994 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
12996 info
->callbacks
->einfo
12997 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13003 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
13005 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
13007 || o
->output_section
== bfd_abs_section_ptr
)
13009 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
13011 /* At this point, we are only interested in sections
13012 created by _bfd_elf_link_create_dynamic_sections. */
13015 if (htab
->stab_info
.stabstr
== o
)
13017 if (htab
->eh_info
.hdr_sec
== o
)
13019 if (strcmp (o
->name
, ".dynstr") != 0)
13021 bfd_size_type octets
= ((file_ptr
) o
->output_offset
13022 * bfd_octets_per_byte (abfd
, o
));
13023 if (!bfd_set_section_contents (abfd
, o
->output_section
,
13024 o
->contents
, octets
, o
->size
))
13029 /* The contents of the .dynstr section are actually in a
13033 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
13034 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
13035 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
13041 if (!info
->resolve_section_groups
)
13043 bfd_boolean failed
= FALSE
;
13045 BFD_ASSERT (bfd_link_relocatable (info
));
13046 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
13051 /* If we have optimized stabs strings, output them. */
13052 if (htab
->stab_info
.stabstr
!= NULL
)
13054 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
13058 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
13061 if (info
->callbacks
->emit_ctf
)
13062 info
->callbacks
->emit_ctf ();
13064 elf_final_link_free (abfd
, &flinfo
);
13068 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
13069 if (contents
== NULL
)
13070 return FALSE
; /* Bail out and fail. */
13071 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
13072 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
13079 elf_final_link_free (abfd
, &flinfo
);
13083 /* Initialize COOKIE for input bfd ABFD. */
13086 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
13087 struct bfd_link_info
*info
, bfd
*abfd
)
13089 Elf_Internal_Shdr
*symtab_hdr
;
13090 const struct elf_backend_data
*bed
;
13092 bed
= get_elf_backend_data (abfd
);
13093 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13095 cookie
->abfd
= abfd
;
13096 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
13097 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
13098 if (cookie
->bad_symtab
)
13100 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13101 cookie
->extsymoff
= 0;
13105 cookie
->locsymcount
= symtab_hdr
->sh_info
;
13106 cookie
->extsymoff
= symtab_hdr
->sh_info
;
13109 if (bed
->s
->arch_size
== 32)
13110 cookie
->r_sym_shift
= 8;
13112 cookie
->r_sym_shift
= 32;
13114 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
13115 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
13117 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
13118 cookie
->locsymcount
, 0,
13120 if (cookie
->locsyms
== NULL
)
13122 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
13125 if (info
->keep_memory
)
13126 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
13131 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13134 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
13136 Elf_Internal_Shdr
*symtab_hdr
;
13138 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13139 if (symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
13140 free (cookie
->locsyms
);
13143 /* Initialize the relocation information in COOKIE for input section SEC
13144 of input bfd ABFD. */
13147 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13148 struct bfd_link_info
*info
, bfd
*abfd
,
13151 if (sec
->reloc_count
== 0)
13153 cookie
->rels
= NULL
;
13154 cookie
->relend
= NULL
;
13158 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
13159 info
->keep_memory
);
13160 if (cookie
->rels
== NULL
)
13162 cookie
->rel
= cookie
->rels
;
13163 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
13165 cookie
->rel
= cookie
->rels
;
13169 /* Free the memory allocated by init_reloc_cookie_rels,
13173 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13176 if (elf_section_data (sec
)->relocs
!= cookie
->rels
)
13177 free (cookie
->rels
);
13180 /* Initialize the whole of COOKIE for input section SEC. */
13183 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13184 struct bfd_link_info
*info
,
13187 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
13189 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
13194 fini_reloc_cookie (cookie
, sec
->owner
);
13199 /* Free the memory allocated by init_reloc_cookie_for_section,
13203 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13206 fini_reloc_cookie_rels (cookie
, sec
);
13207 fini_reloc_cookie (cookie
, sec
->owner
);
13210 /* Garbage collect unused sections. */
13212 /* Default gc_mark_hook. */
13215 _bfd_elf_gc_mark_hook (asection
*sec
,
13216 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13217 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13218 struct elf_link_hash_entry
*h
,
13219 Elf_Internal_Sym
*sym
)
13223 switch (h
->root
.type
)
13225 case bfd_link_hash_defined
:
13226 case bfd_link_hash_defweak
:
13227 return h
->root
.u
.def
.section
;
13229 case bfd_link_hash_common
:
13230 return h
->root
.u
.c
.p
->section
;
13237 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
13242 /* Return the debug definition section. */
13245 elf_gc_mark_debug_section (asection
*sec ATTRIBUTE_UNUSED
,
13246 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13247 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13248 struct elf_link_hash_entry
*h
,
13249 Elf_Internal_Sym
*sym
)
13253 /* Return the global debug definition section. */
13254 if ((h
->root
.type
== bfd_link_hash_defined
13255 || h
->root
.type
== bfd_link_hash_defweak
)
13256 && (h
->root
.u
.def
.section
->flags
& SEC_DEBUGGING
) != 0)
13257 return h
->root
.u
.def
.section
;
13261 /* Return the local debug definition section. */
13262 asection
*isec
= bfd_section_from_elf_index (sec
->owner
,
13264 if ((isec
->flags
& SEC_DEBUGGING
) != 0)
13271 /* COOKIE->rel describes a relocation against section SEC, which is
13272 a section we've decided to keep. Return the section that contains
13273 the relocation symbol, or NULL if no section contains it. */
13276 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
13277 elf_gc_mark_hook_fn gc_mark_hook
,
13278 struct elf_reloc_cookie
*cookie
,
13279 bfd_boolean
*start_stop
)
13281 unsigned long r_symndx
;
13282 struct elf_link_hash_entry
*h
, *hw
;
13284 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
13285 if (r_symndx
== STN_UNDEF
)
13288 if (r_symndx
>= cookie
->locsymcount
13289 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13291 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
13294 info
->callbacks
->einfo (_("%F%P: corrupt input: %pB\n"),
13298 while (h
->root
.type
== bfd_link_hash_indirect
13299 || h
->root
.type
== bfd_link_hash_warning
)
13300 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13302 /* Keep all aliases of the symbol too. If an object symbol
13303 needs to be copied into .dynbss then all of its aliases
13304 should be present as dynamic symbols, not just the one used
13305 on the copy relocation. */
13307 while (hw
->is_weakalias
)
13313 if (start_stop
!= NULL
)
13315 /* To work around a glibc bug, mark XXX input sections
13316 when there is a reference to __start_XXX or __stop_XXX
13320 asection
*s
= h
->u2
.start_stop_section
;
13321 *start_stop
= !s
->gc_mark
;
13326 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
13329 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
13330 &cookie
->locsyms
[r_symndx
]);
13333 /* COOKIE->rel describes a relocation against section SEC, which is
13334 a section we've decided to keep. Mark the section that contains
13335 the relocation symbol. */
13338 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
13340 elf_gc_mark_hook_fn gc_mark_hook
,
13341 struct elf_reloc_cookie
*cookie
)
13344 bfd_boolean start_stop
= FALSE
;
13346 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
13347 while (rsec
!= NULL
)
13349 if (!rsec
->gc_mark
)
13351 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
13352 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
13354 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
13359 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
13364 /* The mark phase of garbage collection. For a given section, mark
13365 it and any sections in this section's group, and all the sections
13366 which define symbols to which it refers. */
13369 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
13371 elf_gc_mark_hook_fn gc_mark_hook
)
13374 asection
*group_sec
, *eh_frame
;
13378 /* Mark all the sections in the group. */
13379 group_sec
= elf_section_data (sec
)->next_in_group
;
13380 if (group_sec
&& !group_sec
->gc_mark
)
13381 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
13384 /* Look through the section relocs. */
13386 eh_frame
= elf_eh_frame_section (sec
->owner
);
13387 if ((sec
->flags
& SEC_RELOC
) != 0
13388 && sec
->reloc_count
> 0
13389 && sec
!= eh_frame
)
13391 struct elf_reloc_cookie cookie
;
13393 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
13397 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
13398 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
13403 fini_reloc_cookie_for_section (&cookie
, sec
);
13407 if (ret
&& eh_frame
&& elf_fde_list (sec
))
13409 struct elf_reloc_cookie cookie
;
13411 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
13415 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
13416 gc_mark_hook
, &cookie
))
13418 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
13422 eh_frame
= elf_section_eh_frame_entry (sec
);
13423 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
13424 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
13430 /* Scan and mark sections in a special or debug section group. */
13433 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
13435 /* Point to first section of section group. */
13437 /* Used to iterate the section group. */
13440 bfd_boolean is_special_grp
= TRUE
;
13441 bfd_boolean is_debug_grp
= TRUE
;
13443 /* First scan to see if group contains any section other than debug
13444 and special section. */
13445 ssec
= msec
= elf_next_in_group (grp
);
13448 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
13449 is_debug_grp
= FALSE
;
13451 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
13452 is_special_grp
= FALSE
;
13454 msec
= elf_next_in_group (msec
);
13456 while (msec
!= ssec
);
13458 /* If this is a pure debug section group or pure special section group,
13459 keep all sections in this group. */
13460 if (is_debug_grp
|| is_special_grp
)
13465 msec
= elf_next_in_group (msec
);
13467 while (msec
!= ssec
);
13471 /* Keep debug and special sections. */
13474 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
13475 elf_gc_mark_hook_fn mark_hook
)
13479 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13482 bfd_boolean some_kept
;
13483 bfd_boolean debug_frag_seen
;
13484 bfd_boolean has_kept_debug_info
;
13486 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13488 isec
= ibfd
->sections
;
13489 if (isec
== NULL
|| isec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13492 /* Ensure all linker created sections are kept,
13493 see if any other section is already marked,
13494 and note if we have any fragmented debug sections. */
13495 debug_frag_seen
= some_kept
= has_kept_debug_info
= FALSE
;
13496 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13498 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
13500 else if (isec
->gc_mark
13501 && (isec
->flags
& SEC_ALLOC
) != 0
13502 && elf_section_type (isec
) != SHT_NOTE
)
13506 /* Since all sections, except for backend specific ones,
13507 have been garbage collected, call mark_hook on this
13508 section if any of its linked-to sections is marked. */
13509 asection
*linked_to_sec
= elf_linked_to_section (isec
);
13510 for (; linked_to_sec
!= NULL
;
13511 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
13512 if (linked_to_sec
->gc_mark
)
13514 if (!_bfd_elf_gc_mark (info
, isec
, mark_hook
))
13520 if (!debug_frag_seen
13521 && (isec
->flags
& SEC_DEBUGGING
)
13522 && CONST_STRNEQ (isec
->name
, ".debug_line."))
13523 debug_frag_seen
= TRUE
;
13524 else if (strcmp (bfd_section_name (isec
),
13525 "__patchable_function_entries") == 0
13526 && elf_linked_to_section (isec
) == NULL
)
13527 info
->callbacks
->einfo (_("%F%P: %pB(%pA): error: "
13528 "need linked-to section "
13529 "for --gc-sections\n"),
13530 isec
->owner
, isec
);
13533 /* If no non-note alloc section in this file will be kept, then
13534 we can toss out the debug and special sections. */
13538 /* Keep debug and special sections like .comment when they are
13539 not part of a group. Also keep section groups that contain
13540 just debug sections or special sections. NB: Sections with
13541 linked-to section has been handled above. */
13542 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13544 if ((isec
->flags
& SEC_GROUP
) != 0)
13545 _bfd_elf_gc_mark_debug_special_section_group (isec
);
13546 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
13547 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
13548 && elf_next_in_group (isec
) == NULL
13549 && elf_linked_to_section (isec
) == NULL
)
13551 if (isec
->gc_mark
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
13552 has_kept_debug_info
= TRUE
;
13555 /* Look for CODE sections which are going to be discarded,
13556 and find and discard any fragmented debug sections which
13557 are associated with that code section. */
13558 if (debug_frag_seen
)
13559 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13560 if ((isec
->flags
& SEC_CODE
) != 0
13561 && isec
->gc_mark
== 0)
13566 ilen
= strlen (isec
->name
);
13568 /* Association is determined by the name of the debug
13569 section containing the name of the code section as
13570 a suffix. For example .debug_line.text.foo is a
13571 debug section associated with .text.foo. */
13572 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
13576 if (dsec
->gc_mark
== 0
13577 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
13580 dlen
= strlen (dsec
->name
);
13583 && strncmp (dsec
->name
+ (dlen
- ilen
),
13584 isec
->name
, ilen
) == 0)
13589 /* Mark debug sections referenced by kept debug sections. */
13590 if (has_kept_debug_info
)
13591 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13593 && (isec
->flags
& SEC_DEBUGGING
) != 0)
13594 if (!_bfd_elf_gc_mark (info
, isec
,
13595 elf_gc_mark_debug_section
))
13602 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
13605 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13607 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13611 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13612 || elf_object_id (sub
) != elf_hash_table_id (elf_hash_table (info
))
13613 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13616 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13619 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13621 /* When any section in a section group is kept, we keep all
13622 sections in the section group. If the first member of
13623 the section group is excluded, we will also exclude the
13625 if (o
->flags
& SEC_GROUP
)
13627 asection
*first
= elf_next_in_group (o
);
13628 o
->gc_mark
= first
->gc_mark
;
13634 /* Skip sweeping sections already excluded. */
13635 if (o
->flags
& SEC_EXCLUDE
)
13638 /* Since this is early in the link process, it is simple
13639 to remove a section from the output. */
13640 o
->flags
|= SEC_EXCLUDE
;
13642 if (info
->print_gc_sections
&& o
->size
!= 0)
13643 /* xgettext:c-format */
13644 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13652 /* Propagate collected vtable information. This is called through
13653 elf_link_hash_traverse. */
13656 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
13658 /* Those that are not vtables. */
13660 || h
->u2
.vtable
== NULL
13661 || h
->u2
.vtable
->parent
== NULL
)
13664 /* Those vtables that do not have parents, we cannot merge. */
13665 if (h
->u2
.vtable
->parent
== (struct elf_link_hash_entry
*) -1)
13668 /* If we've already been done, exit. */
13669 if (h
->u2
.vtable
->used
&& h
->u2
.vtable
->used
[-1])
13672 /* Make sure the parent's table is up to date. */
13673 elf_gc_propagate_vtable_entries_used (h
->u2
.vtable
->parent
, okp
);
13675 if (h
->u2
.vtable
->used
== NULL
)
13677 /* None of this table's entries were referenced. Re-use the
13679 h
->u2
.vtable
->used
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13680 h
->u2
.vtable
->size
= h
->u2
.vtable
->parent
->u2
.vtable
->size
;
13685 bfd_boolean
*cu
, *pu
;
13687 /* Or the parent's entries into ours. */
13688 cu
= h
->u2
.vtable
->used
;
13690 pu
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13693 const struct elf_backend_data
*bed
;
13694 unsigned int log_file_align
;
13696 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
13697 log_file_align
= bed
->s
->log_file_align
;
13698 n
= h
->u2
.vtable
->parent
->u2
.vtable
->size
>> log_file_align
;
13713 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
13716 bfd_vma hstart
, hend
;
13717 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
13718 const struct elf_backend_data
*bed
;
13719 unsigned int log_file_align
;
13721 /* Take care of both those symbols that do not describe vtables as
13722 well as those that are not loaded. */
13724 || h
->u2
.vtable
== NULL
13725 || h
->u2
.vtable
->parent
== NULL
)
13728 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
13729 || h
->root
.type
== bfd_link_hash_defweak
);
13731 sec
= h
->root
.u
.def
.section
;
13732 hstart
= h
->root
.u
.def
.value
;
13733 hend
= hstart
+ h
->size
;
13735 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
13737 return *(bfd_boolean
*) okp
= FALSE
;
13738 bed
= get_elf_backend_data (sec
->owner
);
13739 log_file_align
= bed
->s
->log_file_align
;
13741 relend
= relstart
+ sec
->reloc_count
;
13743 for (rel
= relstart
; rel
< relend
; ++rel
)
13744 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
13746 /* If the entry is in use, do nothing. */
13747 if (h
->u2
.vtable
->used
13748 && (rel
->r_offset
- hstart
) < h
->u2
.vtable
->size
)
13750 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
13751 if (h
->u2
.vtable
->used
[entry
])
13754 /* Otherwise, kill it. */
13755 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
13761 /* Mark sections containing dynamically referenced symbols. When
13762 building shared libraries, we must assume that any visible symbol is
13766 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
13768 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
13769 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
13771 if ((h
->root
.type
== bfd_link_hash_defined
13772 || h
->root
.type
== bfd_link_hash_defweak
)
13773 && ((h
->ref_dynamic
&& !h
->forced_local
)
13774 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
13775 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
13776 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
13777 && (!bfd_link_executable (info
)
13778 || info
->gc_keep_exported
13779 || info
->export_dynamic
13782 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
13783 && (h
->versioned
>= versioned
13784 || !bfd_hide_sym_by_version (info
->version_info
,
13785 h
->root
.root
.string
)))))
13786 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13791 /* Keep all sections containing symbols undefined on the command-line,
13792 and the section containing the entry symbol. */
13795 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
13797 struct bfd_sym_chain
*sym
;
13799 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
13801 struct elf_link_hash_entry
*h
;
13803 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
13804 FALSE
, FALSE
, FALSE
);
13807 && (h
->root
.type
== bfd_link_hash_defined
13808 || h
->root
.type
== bfd_link_hash_defweak
)
13809 && !bfd_is_const_section (h
->root
.u
.def
.section
))
13810 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13815 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
13816 struct bfd_link_info
*info
)
13818 bfd
*ibfd
= info
->input_bfds
;
13820 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13823 struct elf_reloc_cookie cookie
;
13825 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13827 sec
= ibfd
->sections
;
13828 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13831 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
13834 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
13836 if (CONST_STRNEQ (bfd_section_name (sec
), ".eh_frame_entry")
13837 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
13839 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
13840 fini_reloc_cookie_rels (&cookie
, sec
);
13847 /* Do mark and sweep of unused sections. */
13850 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
13852 bfd_boolean ok
= TRUE
;
13854 elf_gc_mark_hook_fn gc_mark_hook
;
13855 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13856 struct elf_link_hash_table
*htab
;
13858 if (!bed
->can_gc_sections
13859 || !is_elf_hash_table (info
->hash
))
13861 _bfd_error_handler(_("warning: gc-sections option ignored"));
13865 bed
->gc_keep (info
);
13866 htab
= elf_hash_table (info
);
13868 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13869 at the .eh_frame section if we can mark the FDEs individually. */
13870 for (sub
= info
->input_bfds
;
13871 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
13872 sub
= sub
->link
.next
)
13875 struct elf_reloc_cookie cookie
;
13877 sec
= sub
->sections
;
13878 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13880 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
13881 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
13883 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
13884 if (elf_section_data (sec
)->sec_info
13885 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
13886 elf_eh_frame_section (sub
) = sec
;
13887 fini_reloc_cookie_for_section (&cookie
, sec
);
13888 sec
= bfd_get_next_section_by_name (NULL
, sec
);
13892 /* Apply transitive closure to the vtable entry usage info. */
13893 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
13897 /* Kill the vtable relocations that were not used. */
13898 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &ok
);
13902 /* Mark dynamically referenced symbols. */
13903 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
13904 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
13906 /* Grovel through relocs to find out who stays ... */
13907 gc_mark_hook
= bed
->gc_mark_hook
;
13908 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13912 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13913 || elf_object_id (sub
) != elf_hash_table_id (htab
)
13914 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13918 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13921 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13922 Also treat note sections as a root, if the section is not part
13923 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13924 well as FINI_ARRAY sections for ld -r. */
13925 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13927 && (o
->flags
& SEC_EXCLUDE
) == 0
13928 && ((o
->flags
& SEC_KEEP
) != 0
13929 || (bfd_link_relocatable (info
)
13930 && ((elf_section_data (o
)->this_hdr
.sh_type
13931 == SHT_PREINIT_ARRAY
)
13932 || (elf_section_data (o
)->this_hdr
.sh_type
13934 || (elf_section_data (o
)->this_hdr
.sh_type
13935 == SHT_FINI_ARRAY
)))
13936 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
13937 && elf_next_in_group (o
) == NULL
)))
13939 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
13944 /* Allow the backend to mark additional target specific sections. */
13945 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
13947 /* ... and mark SEC_EXCLUDE for those that go. */
13948 return elf_gc_sweep (abfd
, info
);
13951 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13954 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
13956 struct elf_link_hash_entry
*h
,
13959 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
13960 struct elf_link_hash_entry
**search
, *child
;
13961 size_t extsymcount
;
13962 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13964 /* The sh_info field of the symtab header tells us where the
13965 external symbols start. We don't care about the local symbols at
13967 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
13968 if (!elf_bad_symtab (abfd
))
13969 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
13971 sym_hashes
= elf_sym_hashes (abfd
);
13972 sym_hashes_end
= sym_hashes
+ extsymcount
;
13974 /* Hunt down the child symbol, which is in this section at the same
13975 offset as the relocation. */
13976 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
13978 if ((child
= *search
) != NULL
13979 && (child
->root
.type
== bfd_link_hash_defined
13980 || child
->root
.type
== bfd_link_hash_defweak
)
13981 && child
->root
.u
.def
.section
== sec
13982 && child
->root
.u
.def
.value
== offset
)
13986 /* xgettext:c-format */
13987 _bfd_error_handler (_("%pB: %pA+%#" PRIx64
": no symbol found for INHERIT"),
13988 abfd
, sec
, (uint64_t) offset
);
13989 bfd_set_error (bfd_error_invalid_operation
);
13993 if (!child
->u2
.vtable
)
13995 child
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
13996 bfd_zalloc (abfd
, sizeof (*child
->u2
.vtable
)));
13997 if (!child
->u2
.vtable
)
14002 /* This *should* only be the absolute section. It could potentially
14003 be that someone has defined a non-global vtable though, which
14004 would be bad. It isn't worth paging in the local symbols to be
14005 sure though; that case should simply be handled by the assembler. */
14007 child
->u2
.vtable
->parent
= (struct elf_link_hash_entry
*) -1;
14010 child
->u2
.vtable
->parent
= h
;
14015 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14018 bfd_elf_gc_record_vtentry (bfd
*abfd
, asection
*sec
,
14019 struct elf_link_hash_entry
*h
,
14022 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14023 unsigned int log_file_align
= bed
->s
->log_file_align
;
14027 /* xgettext:c-format */
14028 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14030 bfd_set_error (bfd_error_bad_value
);
14036 h
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14037 bfd_zalloc (abfd
, sizeof (*h
->u2
.vtable
)));
14042 if (addend
>= h
->u2
.vtable
->size
)
14044 size_t size
, bytes
, file_align
;
14045 bfd_boolean
*ptr
= h
->u2
.vtable
->used
;
14047 /* While the symbol is undefined, we have to be prepared to handle
14049 file_align
= 1 << log_file_align
;
14050 if (h
->root
.type
== bfd_link_hash_undefined
)
14051 size
= addend
+ file_align
;
14055 if (addend
>= size
)
14057 /* Oops! We've got a reference past the defined end of
14058 the table. This is probably a bug -- shall we warn? */
14059 size
= addend
+ file_align
;
14062 size
= (size
+ file_align
- 1) & -file_align
;
14064 /* Allocate one extra entry for use as a "done" flag for the
14065 consolidation pass. */
14066 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
14070 ptr
= (bfd_boolean
*) bfd_realloc (ptr
- 1, bytes
);
14076 oldbytes
= (((h
->u2
.vtable
->size
>> log_file_align
) + 1)
14077 * sizeof (bfd_boolean
));
14078 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
14082 ptr
= (bfd_boolean
*) bfd_zmalloc (bytes
);
14087 /* And arrange for that done flag to be at index -1. */
14088 h
->u2
.vtable
->used
= ptr
+ 1;
14089 h
->u2
.vtable
->size
= size
;
14092 h
->u2
.vtable
->used
[addend
>> log_file_align
] = TRUE
;
14097 /* Map an ELF section header flag to its corresponding string. */
14101 flagword flag_value
;
14102 } elf_flags_to_name_table
;
14104 static elf_flags_to_name_table elf_flags_to_names
[] =
14106 { "SHF_WRITE", SHF_WRITE
},
14107 { "SHF_ALLOC", SHF_ALLOC
},
14108 { "SHF_EXECINSTR", SHF_EXECINSTR
},
14109 { "SHF_MERGE", SHF_MERGE
},
14110 { "SHF_STRINGS", SHF_STRINGS
},
14111 { "SHF_INFO_LINK", SHF_INFO_LINK
},
14112 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
14113 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
14114 { "SHF_GROUP", SHF_GROUP
},
14115 { "SHF_TLS", SHF_TLS
},
14116 { "SHF_MASKOS", SHF_MASKOS
},
14117 { "SHF_EXCLUDE", SHF_EXCLUDE
},
14120 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14122 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
14123 struct flag_info
*flaginfo
,
14126 const bfd_vma sh_flags
= elf_section_flags (section
);
14128 if (!flaginfo
->flags_initialized
)
14130 bfd
*obfd
= info
->output_bfd
;
14131 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14132 struct flag_info_list
*tf
= flaginfo
->flag_list
;
14134 int without_hex
= 0;
14136 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
14139 flagword (*lookup
) (char *);
14141 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
14142 if (lookup
!= NULL
)
14144 flagword hexval
= (*lookup
) ((char *) tf
->name
);
14148 if (tf
->with
== with_flags
)
14149 with_hex
|= hexval
;
14150 else if (tf
->with
== without_flags
)
14151 without_hex
|= hexval
;
14156 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
14158 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
14160 if (tf
->with
== with_flags
)
14161 with_hex
|= elf_flags_to_names
[i
].flag_value
;
14162 else if (tf
->with
== without_flags
)
14163 without_hex
|= elf_flags_to_names
[i
].flag_value
;
14170 info
->callbacks
->einfo
14171 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
14175 flaginfo
->flags_initialized
= TRUE
;
14176 flaginfo
->only_with_flags
|= with_hex
;
14177 flaginfo
->not_with_flags
|= without_hex
;
14180 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
14183 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
14189 struct alloc_got_off_arg
{
14191 struct bfd_link_info
*info
;
14194 /* We need a special top-level link routine to convert got reference counts
14195 to real got offsets. */
14198 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
14200 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
14201 bfd
*obfd
= gofarg
->info
->output_bfd
;
14202 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14204 if (h
->got
.refcount
> 0)
14206 h
->got
.offset
= gofarg
->gotoff
;
14207 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
14210 h
->got
.offset
= (bfd_vma
) -1;
14215 /* And an accompanying bit to work out final got entry offsets once
14216 we're done. Should be called from final_link. */
14219 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
14220 struct bfd_link_info
*info
)
14223 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14225 struct alloc_got_off_arg gofarg
;
14227 BFD_ASSERT (abfd
== info
->output_bfd
);
14229 if (! is_elf_hash_table (info
->hash
))
14232 /* The GOT offset is relative to the .got section, but the GOT header is
14233 put into the .got.plt section, if the backend uses it. */
14234 if (bed
->want_got_plt
)
14237 gotoff
= bed
->got_header_size
;
14239 /* Do the local .got entries first. */
14240 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
14242 bfd_signed_vma
*local_got
;
14243 size_t j
, locsymcount
;
14244 Elf_Internal_Shdr
*symtab_hdr
;
14246 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
14249 local_got
= elf_local_got_refcounts (i
);
14253 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
14254 if (elf_bad_symtab (i
))
14255 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
14257 locsymcount
= symtab_hdr
->sh_info
;
14259 for (j
= 0; j
< locsymcount
; ++j
)
14261 if (local_got
[j
] > 0)
14263 local_got
[j
] = gotoff
;
14264 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
14267 local_got
[j
] = (bfd_vma
) -1;
14271 /* Then the global .got entries. .plt refcounts are handled by
14272 adjust_dynamic_symbol */
14273 gofarg
.gotoff
= gotoff
;
14274 gofarg
.info
= info
;
14275 elf_link_hash_traverse (elf_hash_table (info
),
14276 elf_gc_allocate_got_offsets
,
14281 /* Many folk need no more in the way of final link than this, once
14282 got entry reference counting is enabled. */
14285 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
14287 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
14290 /* Invoke the regular ELF backend linker to do all the work. */
14291 return bfd_elf_final_link (abfd
, info
);
14295 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
14297 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
14299 if (rcookie
->bad_symtab
)
14300 rcookie
->rel
= rcookie
->rels
;
14302 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
14304 unsigned long r_symndx
;
14306 if (! rcookie
->bad_symtab
)
14307 if (rcookie
->rel
->r_offset
> offset
)
14309 if (rcookie
->rel
->r_offset
!= offset
)
14312 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
14313 if (r_symndx
== STN_UNDEF
)
14316 if (r_symndx
>= rcookie
->locsymcount
14317 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
14319 struct elf_link_hash_entry
*h
;
14321 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
14323 while (h
->root
.type
== bfd_link_hash_indirect
14324 || h
->root
.type
== bfd_link_hash_warning
)
14325 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
14327 if ((h
->root
.type
== bfd_link_hash_defined
14328 || h
->root
.type
== bfd_link_hash_defweak
)
14329 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
14330 || h
->root
.u
.def
.section
->kept_section
!= NULL
14331 || discarded_section (h
->root
.u
.def
.section
)))
14336 /* It's not a relocation against a global symbol,
14337 but it could be a relocation against a local
14338 symbol for a discarded section. */
14340 Elf_Internal_Sym
*isym
;
14342 /* Need to: get the symbol; get the section. */
14343 isym
= &rcookie
->locsyms
[r_symndx
];
14344 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
14346 && (isec
->kept_section
!= NULL
14347 || discarded_section (isec
)))
14355 /* Discard unneeded references to discarded sections.
14356 Returns -1 on error, 1 if any section's size was changed, 0 if
14357 nothing changed. This function assumes that the relocations are in
14358 sorted order, which is true for all known assemblers. */
14361 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
14363 struct elf_reloc_cookie cookie
;
14368 if (info
->traditional_format
14369 || !is_elf_hash_table (info
->hash
))
14372 o
= bfd_get_section_by_name (output_bfd
, ".stab");
14377 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14380 || i
->reloc_count
== 0
14381 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
14385 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14388 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14391 if (_bfd_discard_section_stabs (abfd
, i
,
14392 elf_section_data (i
)->sec_info
,
14393 bfd_elf_reloc_symbol_deleted_p
,
14397 fini_reloc_cookie_for_section (&cookie
, i
);
14402 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
14403 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
14407 int eh_changed
= 0;
14408 unsigned int eh_alignment
; /* Octets. */
14410 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14416 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14419 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14422 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
14423 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
14424 bfd_elf_reloc_symbol_deleted_p
,
14428 if (i
->size
!= i
->rawsize
)
14432 fini_reloc_cookie_for_section (&cookie
, i
);
14435 eh_alignment
= ((1 << o
->alignment_power
)
14436 * bfd_octets_per_byte (output_bfd
, o
));
14437 /* Skip over zero terminator, and prevent empty sections from
14438 adding alignment padding at the end. */
14439 for (i
= o
->map_tail
.s
; i
!= NULL
; i
= i
->map_tail
.s
)
14441 i
->flags
|= SEC_EXCLUDE
;
14442 else if (i
->size
> 4)
14444 /* The last non-empty eh_frame section doesn't need padding. */
14447 /* Any prior sections must pad the last FDE out to the output
14448 section alignment. Otherwise we might have zero padding
14449 between sections, which would be seen as a terminator. */
14450 for (; i
!= NULL
; i
= i
->map_tail
.s
)
14452 /* All but the last zero terminator should have been removed. */
14457 = (i
->size
+ eh_alignment
- 1) & -eh_alignment
;
14458 if (i
->size
!= size
)
14466 elf_link_hash_traverse (elf_hash_table (info
),
14467 _bfd_elf_adjust_eh_frame_global_symbol
, NULL
);
14470 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
14472 const struct elf_backend_data
*bed
;
14475 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14477 s
= abfd
->sections
;
14478 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14481 bed
= get_elf_backend_data (abfd
);
14483 if (bed
->elf_backend_discard_info
!= NULL
)
14485 if (!init_reloc_cookie (&cookie
, info
, abfd
))
14488 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
14491 fini_reloc_cookie (&cookie
, abfd
);
14495 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
14496 _bfd_elf_end_eh_frame_parsing (info
);
14498 if (info
->eh_frame_hdr_type
14499 && !bfd_link_relocatable (info
)
14500 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
14507 _bfd_elf_section_already_linked (bfd
*abfd
,
14509 struct bfd_link_info
*info
)
14512 const char *name
, *key
;
14513 struct bfd_section_already_linked
*l
;
14514 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
14516 if (sec
->output_section
== bfd_abs_section_ptr
)
14519 flags
= sec
->flags
;
14521 /* Return if it isn't a linkonce section. A comdat group section
14522 also has SEC_LINK_ONCE set. */
14523 if ((flags
& SEC_LINK_ONCE
) == 0)
14526 /* Don't put group member sections on our list of already linked
14527 sections. They are handled as a group via their group section. */
14528 if (elf_sec_group (sec
) != NULL
)
14531 /* For a SHT_GROUP section, use the group signature as the key. */
14533 if ((flags
& SEC_GROUP
) != 0
14534 && elf_next_in_group (sec
) != NULL
14535 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
14536 key
= elf_group_name (elf_next_in_group (sec
));
14539 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14540 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
14541 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
14544 /* Must be a user linkonce section that doesn't follow gcc's
14545 naming convention. In this case we won't be matching
14546 single member groups. */
14550 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
14552 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14554 /* We may have 2 different types of sections on the list: group
14555 sections with a signature of <key> (<key> is some string),
14556 and linkonce sections named .gnu.linkonce.<type>.<key>.
14557 Match like sections. LTO plugin sections are an exception.
14558 They are always named .gnu.linkonce.t.<key> and match either
14559 type of section. */
14560 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
14561 && ((flags
& SEC_GROUP
) != 0
14562 || strcmp (name
, l
->sec
->name
) == 0))
14563 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0
14564 || (sec
->owner
->flags
& BFD_PLUGIN
) != 0)
14566 /* The section has already been linked. See if we should
14567 issue a warning. */
14568 if (!_bfd_handle_already_linked (sec
, l
, info
))
14571 if (flags
& SEC_GROUP
)
14573 asection
*first
= elf_next_in_group (sec
);
14574 asection
*s
= first
;
14578 s
->output_section
= bfd_abs_section_ptr
;
14579 /* Record which group discards it. */
14580 s
->kept_section
= l
->sec
;
14581 s
= elf_next_in_group (s
);
14582 /* These lists are circular. */
14592 /* A single member comdat group section may be discarded by a
14593 linkonce section and vice versa. */
14594 if ((flags
& SEC_GROUP
) != 0)
14596 asection
*first
= elf_next_in_group (sec
);
14598 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
14599 /* Check this single member group against linkonce sections. */
14600 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14601 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14602 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
14604 first
->output_section
= bfd_abs_section_ptr
;
14605 first
->kept_section
= l
->sec
;
14606 sec
->output_section
= bfd_abs_section_ptr
;
14611 /* Check this linkonce section against single member groups. */
14612 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14613 if (l
->sec
->flags
& SEC_GROUP
)
14615 asection
*first
= elf_next_in_group (l
->sec
);
14618 && elf_next_in_group (first
) == first
14619 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
14621 sec
->output_section
= bfd_abs_section_ptr
;
14622 sec
->kept_section
= first
;
14627 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14628 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14629 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14630 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14631 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14632 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14633 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14634 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14635 The reverse order cannot happen as there is never a bfd with only the
14636 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14637 matter as here were are looking only for cross-bfd sections. */
14639 if ((flags
& SEC_GROUP
) == 0 && CONST_STRNEQ (name
, ".gnu.linkonce.r."))
14640 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14641 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14642 && CONST_STRNEQ (l
->sec
->name
, ".gnu.linkonce.t."))
14644 if (abfd
!= l
->sec
->owner
)
14645 sec
->output_section
= bfd_abs_section_ptr
;
14649 /* This is the first section with this name. Record it. */
14650 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
14651 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
14652 return sec
->output_section
== bfd_abs_section_ptr
;
14656 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
14658 return sym
->st_shndx
== SHN_COMMON
;
14662 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
14668 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
14670 return bfd_com_section_ptr
;
14674 _bfd_elf_default_got_elt_size (bfd
*abfd
,
14675 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
14676 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
14677 bfd
*ibfd ATTRIBUTE_UNUSED
,
14678 unsigned long symndx ATTRIBUTE_UNUSED
)
14680 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14681 return bed
->s
->arch_size
/ 8;
14684 /* Routines to support the creation of dynamic relocs. */
14686 /* Returns the name of the dynamic reloc section associated with SEC. */
14688 static const char *
14689 get_dynamic_reloc_section_name (bfd
* abfd
,
14691 bfd_boolean is_rela
)
14694 const char *old_name
= bfd_section_name (sec
);
14695 const char *prefix
= is_rela
? ".rela" : ".rel";
14697 if (old_name
== NULL
)
14700 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
14701 sprintf (name
, "%s%s", prefix
, old_name
);
14706 /* Returns the dynamic reloc section associated with SEC.
14707 If necessary compute the name of the dynamic reloc section based
14708 on SEC's name (looked up in ABFD's string table) and the setting
14712 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
14714 bfd_boolean is_rela
)
14716 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14718 if (reloc_sec
== NULL
)
14720 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14724 reloc_sec
= bfd_get_linker_section (abfd
, name
);
14726 if (reloc_sec
!= NULL
)
14727 elf_section_data (sec
)->sreloc
= reloc_sec
;
14734 /* Returns the dynamic reloc section associated with SEC. If the
14735 section does not exist it is created and attached to the DYNOBJ
14736 bfd and stored in the SRELOC field of SEC's elf_section_data
14739 ALIGNMENT is the alignment for the newly created section and
14740 IS_RELA defines whether the name should be .rela.<SEC's name>
14741 or .rel.<SEC's name>. The section name is looked up in the
14742 string table associated with ABFD. */
14745 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
14747 unsigned int alignment
,
14749 bfd_boolean is_rela
)
14751 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14753 if (reloc_sec
== NULL
)
14755 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14760 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
14762 if (reloc_sec
== NULL
)
14764 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
14765 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
14766 if ((sec
->flags
& SEC_ALLOC
) != 0)
14767 flags
|= SEC_ALLOC
| SEC_LOAD
;
14769 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
14770 if (reloc_sec
!= NULL
)
14772 /* _bfd_elf_get_sec_type_attr chooses a section type by
14773 name. Override as it may be wrong, eg. for a user
14774 section named "auto" we'll get ".relauto" which is
14775 seen to be a .rela section. */
14776 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
14777 if (!bfd_set_section_alignment (reloc_sec
, alignment
))
14782 elf_section_data (sec
)->sreloc
= reloc_sec
;
14788 /* Copy the ELF symbol type and other attributes for a linker script
14789 assignment from HSRC to HDEST. Generally this should be treated as
14790 if we found a strong non-dynamic definition for HDEST (except that
14791 ld ignores multiple definition errors). */
14793 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
14794 struct bfd_link_hash_entry
*hdest
,
14795 struct bfd_link_hash_entry
*hsrc
)
14797 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
14798 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
14799 Elf_Internal_Sym isym
;
14801 ehdest
->type
= ehsrc
->type
;
14802 ehdest
->target_internal
= ehsrc
->target_internal
;
14804 isym
.st_other
= ehsrc
->other
;
14805 elf_merge_st_other (abfd
, ehdest
, &isym
, NULL
, TRUE
, FALSE
);
14808 /* Append a RELA relocation REL to section S in BFD. */
14811 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14813 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14814 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
14815 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
14816 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
14819 /* Append a REL relocation REL to section S in BFD. */
14822 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14824 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14825 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
14826 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
14827 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);
14830 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14832 struct bfd_link_hash_entry
*
14833 bfd_elf_define_start_stop (struct bfd_link_info
*info
,
14834 const char *symbol
, asection
*sec
)
14836 struct elf_link_hash_entry
*h
;
14838 h
= elf_link_hash_lookup (elf_hash_table (info
), symbol
,
14839 FALSE
, FALSE
, TRUE
);
14840 /* NB: Common symbols will be turned into definition later. */
14842 && (h
->root
.type
== bfd_link_hash_undefined
14843 || h
->root
.type
== bfd_link_hash_undefweak
14844 || ((h
->ref_regular
|| h
->def_dynamic
)
14846 && h
->root
.type
!= bfd_link_hash_common
)))
14848 bfd_boolean was_dynamic
= h
->ref_dynamic
|| h
->def_dynamic
;
14849 h
->verinfo
.verdef
= NULL
;
14850 h
->root
.type
= bfd_link_hash_defined
;
14851 h
->root
.u
.def
.section
= sec
;
14852 h
->root
.u
.def
.value
= 0;
14853 h
->def_regular
= 1;
14854 h
->def_dynamic
= 0;
14856 h
->u2
.start_stop_section
= sec
;
14857 if (symbol
[0] == '.')
14859 /* .startof. and .sizeof. symbols are local. */
14860 const struct elf_backend_data
*bed
;
14861 bed
= get_elf_backend_data (info
->output_bfd
);
14862 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
14866 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
14867 h
->other
= ((h
->other
& ~ELF_ST_VISIBILITY (-1))
14868 | info
->start_stop_visibility
);
14870 bfd_elf_link_record_dynamic_symbol (info
, h
);
14877 /* Find dynamic relocs for H that apply to read-only sections. */
14880 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry
*h
)
14882 struct elf_dyn_relocs
*p
;
14884 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
14886 asection
*s
= p
->sec
->output_section
;
14888 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
14894 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
14895 read-only sections. */
14898 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry
*h
, void *inf
)
14902 if (h
->root
.type
== bfd_link_hash_indirect
)
14905 sec
= _bfd_elf_readonly_dynrelocs (h
);
14908 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
14910 info
->flags
|= DF_TEXTREL
;
14911 /* xgettext:c-format */
14912 info
->callbacks
->minfo (_("%pB: dynamic relocation against `%pT' "
14913 "in read-only section `%pA'\n"),
14914 sec
->owner
, h
->root
.root
.string
, sec
);
14916 if (bfd_link_textrel_check (info
))
14917 /* xgettext:c-format */
14918 info
->callbacks
->einfo (_("%P: %pB: warning: relocation against `%s' "
14919 "in read-only section `%pA'\n"),
14920 sec
->owner
, h
->root
.root
.string
, sec
);
14922 /* Not an error, just cut short the traversal. */
14928 /* Add dynamic tags. */
14931 _bfd_elf_add_dynamic_tags (bfd
*output_bfd
, struct bfd_link_info
*info
,
14932 bfd_boolean need_dynamic_reloc
)
14934 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
14936 if (htab
->dynamic_sections_created
)
14938 /* Add some entries to the .dynamic section. We fill in the
14939 values later, in finish_dynamic_sections, but we must add
14940 the entries now so that we get the correct size for the
14941 .dynamic section. The DT_DEBUG entry is filled in by the
14942 dynamic linker and used by the debugger. */
14943 #define add_dynamic_entry(TAG, VAL) \
14944 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
14946 const struct elf_backend_data
*bed
14947 = get_elf_backend_data (output_bfd
);
14949 if (bfd_link_executable (info
))
14951 if (!add_dynamic_entry (DT_DEBUG
, 0))
14955 if (htab
->dt_pltgot_required
|| htab
->splt
->size
!= 0)
14957 /* DT_PLTGOT is used by prelink even if there is no PLT
14959 if (!add_dynamic_entry (DT_PLTGOT
, 0))
14963 if (htab
->dt_jmprel_required
|| htab
->srelplt
->size
!= 0)
14965 if (!add_dynamic_entry (DT_PLTRELSZ
, 0)
14966 || !add_dynamic_entry (DT_PLTREL
,
14967 (bed
->rela_plts_and_copies_p
14968 ? DT_RELA
: DT_REL
))
14969 || !add_dynamic_entry (DT_JMPREL
, 0))
14973 if (htab
->tlsdesc_plt
14974 && (!add_dynamic_entry (DT_TLSDESC_PLT
, 0)
14975 || !add_dynamic_entry (DT_TLSDESC_GOT
, 0)))
14978 if (need_dynamic_reloc
)
14980 if (bed
->rela_plts_and_copies_p
)
14982 if (!add_dynamic_entry (DT_RELA
, 0)
14983 || !add_dynamic_entry (DT_RELASZ
, 0)
14984 || !add_dynamic_entry (DT_RELAENT
,
14985 bed
->s
->sizeof_rela
))
14990 if (!add_dynamic_entry (DT_REL
, 0)
14991 || !add_dynamic_entry (DT_RELSZ
, 0)
14992 || !add_dynamic_entry (DT_RELENT
,
14993 bed
->s
->sizeof_rel
))
14997 /* If any dynamic relocs apply to a read-only section,
14998 then we need a DT_TEXTREL entry. */
14999 if ((info
->flags
& DF_TEXTREL
) == 0)
15000 elf_link_hash_traverse (htab
, _bfd_elf_maybe_set_textrel
,
15003 if ((info
->flags
& DF_TEXTREL
) != 0)
15005 if (htab
->ifunc_resolvers
)
15006 info
->callbacks
->einfo
15007 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15008 "may result in a segfault at runtime; recompile with %s\n"),
15009 bfd_link_dll (info
) ? "-fPIC" : "-fPIE");
15011 if (!add_dynamic_entry (DT_TEXTREL
, 0))
15016 #undef add_dynamic_entry