1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2017 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
23 #include "bfd_stdint.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
31 #if BFD_SUPPORTS_PLUGINS
32 #include "plugin-api.h"
36 /* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
39 struct elf_info_failed
41 struct bfd_link_info
*info
;
45 /* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
48 struct elf_find_verdep_info
50 /* General link information. */
51 struct bfd_link_info
*info
;
52 /* The number of dependencies. */
54 /* Whether we had a failure. */
58 static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry
*, struct elf_info_failed
*);
62 _bfd_elf_section_for_symbol (struct elf_reloc_cookie
*cookie
,
63 unsigned long r_symndx
,
66 if (r_symndx
>= cookie
->locsymcount
67 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
69 struct elf_link_hash_entry
*h
;
71 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
73 while (h
->root
.type
== bfd_link_hash_indirect
74 || h
->root
.type
== bfd_link_hash_warning
)
75 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
77 if ((h
->root
.type
== bfd_link_hash_defined
78 || h
->root
.type
== bfd_link_hash_defweak
)
79 && discarded_section (h
->root
.u
.def
.section
))
80 return h
->root
.u
.def
.section
;
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
90 Elf_Internal_Sym
*isym
;
92 /* Need to: get the symbol; get the section. */
93 isym
= &cookie
->locsyms
[r_symndx
];
94 isec
= bfd_section_from_elf_index (cookie
->abfd
, isym
->st_shndx
);
96 && discard
? discarded_section (isec
) : 1)
102 /* Define a symbol in a dynamic linkage section. */
104 struct elf_link_hash_entry
*
105 _bfd_elf_define_linkage_sym (bfd
*abfd
,
106 struct bfd_link_info
*info
,
110 struct elf_link_hash_entry
*h
;
111 struct bfd_link_hash_entry
*bh
;
112 const struct elf_backend_data
*bed
;
114 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h
->root
.type
= bfd_link_hash_new
;
125 bed
= get_elf_backend_data (abfd
);
126 if (!_bfd_generic_link_add_one_symbol (info
, abfd
, name
, BSF_GLOBAL
,
127 sec
, 0, NULL
, FALSE
, bed
->collect
,
130 h
= (struct elf_link_hash_entry
*) bh
;
133 h
->root
.linker_def
= 1;
134 h
->type
= STT_OBJECT
;
135 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
136 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
138 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
143 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
147 struct elf_link_hash_entry
*h
;
148 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
149 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
151 /* This function may be called more than once. */
152 if (htab
->sgot
!= NULL
)
155 flags
= bed
->dynamic_sec_flags
;
157 s
= bfd_make_section_anyway_with_flags (abfd
,
158 (bed
->rela_plts_and_copies_p
159 ? ".rela.got" : ".rel.got"),
160 (bed
->dynamic_sec_flags
163 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
167 s
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
169 || !bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
173 if (bed
->want_got_plt
)
175 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
177 || !bfd_set_section_alignment (abfd
, s
,
178 bed
->s
->log_file_align
))
183 /* The first bit of the global offset table is the header. */
184 s
->size
+= bed
->got_header_size
;
186 if (bed
->want_got_sym
)
188 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
189 (or .got.plt) section. We don't do this in the linker script
190 because we don't want to define the symbol if we are not creating
191 a global offset table. */
192 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
193 "_GLOBAL_OFFSET_TABLE_");
194 elf_hash_table (info
)->hgot
= h
;
202 /* Create a strtab to hold the dynamic symbol names. */
204 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
206 struct elf_link_hash_table
*hash_table
;
208 hash_table
= elf_hash_table (info
);
209 if (hash_table
->dynobj
== NULL
)
211 /* We may not set dynobj, an input file holding linker created
212 dynamic sections to abfd, which may be a dynamic object with
213 its own dynamic sections. We need to find a normal input file
214 to hold linker created sections if possible. */
215 if ((abfd
->flags
& (DYNAMIC
| BFD_PLUGIN
)) != 0)
218 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link
.next
)
220 & (DYNAMIC
| BFD_LINKER_CREATED
| BFD_PLUGIN
)) == 0)
226 hash_table
->dynobj
= abfd
;
229 if (hash_table
->dynstr
== NULL
)
231 hash_table
->dynstr
= _bfd_elf_strtab_init ();
232 if (hash_table
->dynstr
== NULL
)
238 /* Create some sections which will be filled in with dynamic linking
239 information. ABFD is an input file which requires dynamic sections
240 to be created. The dynamic sections take up virtual memory space
241 when the final executable is run, so we need to create them before
242 addresses are assigned to the output sections. We work out the
243 actual contents and size of these sections later. */
246 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
250 const struct elf_backend_data
*bed
;
251 struct elf_link_hash_entry
*h
;
253 if (! is_elf_hash_table (info
->hash
))
256 if (elf_hash_table (info
)->dynamic_sections_created
)
259 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
262 abfd
= elf_hash_table (info
)->dynobj
;
263 bed
= get_elf_backend_data (abfd
);
265 flags
= bed
->dynamic_sec_flags
;
267 /* A dynamically linked executable has a .interp section, but a
268 shared library does not. */
269 if (bfd_link_executable (info
) && !info
->nointerp
)
271 s
= bfd_make_section_anyway_with_flags (abfd
, ".interp",
272 flags
| SEC_READONLY
);
277 /* Create sections to hold version informations. These are removed
278 if they are not needed. */
279 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_d",
280 flags
| SEC_READONLY
);
282 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
285 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version",
286 flags
| SEC_READONLY
);
288 || ! bfd_set_section_alignment (abfd
, s
, 1))
291 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_r",
292 flags
| SEC_READONLY
);
294 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
297 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynsym",
298 flags
| SEC_READONLY
);
300 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
302 elf_hash_table (info
)->dynsym
= s
;
304 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynstr",
305 flags
| SEC_READONLY
);
309 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynamic", flags
);
311 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
314 /* The special symbol _DYNAMIC is always set to the start of the
315 .dynamic section. We could set _DYNAMIC in a linker script, but we
316 only want to define it if we are, in fact, creating a .dynamic
317 section. We don't want to define it if there is no .dynamic
318 section, since on some ELF platforms the start up code examines it
319 to decide how to initialize the process. */
320 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC");
321 elf_hash_table (info
)->hdynamic
= h
;
327 s
= bfd_make_section_anyway_with_flags (abfd
, ".hash",
328 flags
| SEC_READONLY
);
330 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
332 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
335 if (info
->emit_gnu_hash
)
337 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.hash",
338 flags
| SEC_READONLY
);
340 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
342 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
343 4 32-bit words followed by variable count of 64-bit words, then
344 variable count of 32-bit words. */
345 if (bed
->s
->arch_size
== 64)
346 elf_section_data (s
)->this_hdr
.sh_entsize
= 0;
348 elf_section_data (s
)->this_hdr
.sh_entsize
= 4;
351 /* Let the backend create the rest of the sections. This lets the
352 backend set the right flags. The backend will normally create
353 the .got and .plt sections. */
354 if (bed
->elf_backend_create_dynamic_sections
== NULL
355 || ! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
358 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
363 /* Create dynamic sections when linking against a dynamic object. */
366 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
368 flagword flags
, pltflags
;
369 struct elf_link_hash_entry
*h
;
371 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
372 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
374 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
375 .rel[a].bss sections. */
376 flags
= bed
->dynamic_sec_flags
;
379 if (bed
->plt_not_loaded
)
380 /* We do not clear SEC_ALLOC here because we still want the OS to
381 allocate space for the section; it's just that there's nothing
382 to read in from the object file. */
383 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
385 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
386 if (bed
->plt_readonly
)
387 pltflags
|= SEC_READONLY
;
389 s
= bfd_make_section_anyway_with_flags (abfd
, ".plt", pltflags
);
391 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
395 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
397 if (bed
->want_plt_sym
)
399 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
400 "_PROCEDURE_LINKAGE_TABLE_");
401 elf_hash_table (info
)->hplt
= h
;
406 s
= bfd_make_section_anyway_with_flags (abfd
,
407 (bed
->rela_plts_and_copies_p
408 ? ".rela.plt" : ".rel.plt"),
409 flags
| SEC_READONLY
);
411 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
415 if (! _bfd_elf_create_got_section (abfd
, info
))
418 if (bed
->want_dynbss
)
420 /* The .dynbss section is a place to put symbols which are defined
421 by dynamic objects, are referenced by regular objects, and are
422 not functions. We must allocate space for them in the process
423 image and use a R_*_COPY reloc to tell the dynamic linker to
424 initialize them at run time. The linker script puts the .dynbss
425 section into the .bss section of the final image. */
426 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynbss",
427 (SEC_ALLOC
| SEC_LINKER_CREATED
));
432 if (bed
->want_dynrelro
)
434 /* Similarly, but for symbols that were originally in read-only
436 s
= bfd_make_section_anyway_with_flags (abfd
, ".data.rel.ro",
437 (SEC_ALLOC
| SEC_READONLY
439 | SEC_LINKER_CREATED
));
445 /* The .rel[a].bss section holds copy relocs. This section is not
446 normally needed. We need to create it here, though, so that the
447 linker will map it to an output section. We can't just create it
448 only if we need it, because we will not know whether we need it
449 until we have seen all the input files, and the first time the
450 main linker code calls BFD after examining all the input files
451 (size_dynamic_sections) the input sections have already been
452 mapped to the output sections. If the section turns out not to
453 be needed, we can discard it later. We will never need this
454 section when generating a shared object, since they do not use
456 if (bfd_link_executable (info
))
458 s
= bfd_make_section_anyway_with_flags (abfd
,
459 (bed
->rela_plts_and_copies_p
460 ? ".rela.bss" : ".rel.bss"),
461 flags
| SEC_READONLY
);
463 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
467 if (bed
->want_dynrelro
)
469 s
= (bfd_make_section_anyway_with_flags
470 (abfd
, (bed
->rela_plts_and_copies_p
471 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
472 flags
| SEC_READONLY
));
474 || ! bfd_set_section_alignment (abfd
, s
,
475 bed
->s
->log_file_align
))
477 htab
->sreldynrelro
= s
;
485 /* Record a new dynamic symbol. We record the dynamic symbols as we
486 read the input files, since we need to have a list of all of them
487 before we can determine the final sizes of the output sections.
488 Note that we may actually call this function even though we are not
489 going to output any dynamic symbols; in some cases we know that a
490 symbol should be in the dynamic symbol table, but only if there is
494 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
495 struct elf_link_hash_entry
*h
)
497 if (h
->dynindx
== -1)
499 struct elf_strtab_hash
*dynstr
;
504 /* XXX: The ABI draft says the linker must turn hidden and
505 internal symbols into STB_LOCAL symbols when producing the
506 DSO. However, if ld.so honors st_other in the dynamic table,
507 this would not be necessary. */
508 switch (ELF_ST_VISIBILITY (h
->other
))
512 if (h
->root
.type
!= bfd_link_hash_undefined
513 && h
->root
.type
!= bfd_link_hash_undefweak
)
516 if (!elf_hash_table (info
)->is_relocatable_executable
)
524 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
525 ++elf_hash_table (info
)->dynsymcount
;
527 dynstr
= elf_hash_table (info
)->dynstr
;
530 /* Create a strtab to hold the dynamic symbol names. */
531 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
536 /* We don't put any version information in the dynamic string
538 name
= h
->root
.root
.string
;
539 p
= strchr (name
, ELF_VER_CHR
);
541 /* We know that the p points into writable memory. In fact,
542 there are only a few symbols that have read-only names, being
543 those like _GLOBAL_OFFSET_TABLE_ that are created specially
544 by the backends. Most symbols will have names pointing into
545 an ELF string table read from a file, or to objalloc memory. */
548 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
553 if (indx
== (size_t) -1)
555 h
->dynstr_index
= indx
;
561 /* Mark a symbol dynamic. */
564 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
565 struct elf_link_hash_entry
*h
,
566 Elf_Internal_Sym
*sym
)
568 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
570 /* It may be called more than once on the same H. */
571 if(h
->dynamic
|| bfd_link_relocatable (info
))
574 if ((info
->dynamic_data
575 && (h
->type
== STT_OBJECT
576 || h
->type
== STT_COMMON
578 && (ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
579 || ELF_ST_TYPE (sym
->st_info
) == STT_COMMON
))))
581 && h
->root
.type
== bfd_link_hash_new
582 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
586 /* Record an assignment to a symbol made by a linker script. We need
587 this in case some dynamic object refers to this symbol. */
590 bfd_elf_record_link_assignment (bfd
*output_bfd
,
591 struct bfd_link_info
*info
,
596 struct elf_link_hash_entry
*h
, *hv
;
597 struct elf_link_hash_table
*htab
;
598 const struct elf_backend_data
*bed
;
600 if (!is_elf_hash_table (info
->hash
))
603 htab
= elf_hash_table (info
);
604 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
608 if (h
->root
.type
== bfd_link_hash_warning
)
609 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
611 if (h
->versioned
== unknown
)
613 /* Set versioned if symbol version is unknown. */
614 char *version
= strrchr (name
, ELF_VER_CHR
);
617 if (version
> name
&& version
[-1] != ELF_VER_CHR
)
618 h
->versioned
= versioned_hidden
;
620 h
->versioned
= versioned
;
624 switch (h
->root
.type
)
626 case bfd_link_hash_defined
:
627 case bfd_link_hash_defweak
:
628 case bfd_link_hash_common
:
630 case bfd_link_hash_undefweak
:
631 case bfd_link_hash_undefined
:
632 /* Since we're defining the symbol, don't let it seem to have not
633 been defined. record_dynamic_symbol and size_dynamic_sections
634 may depend on this. */
635 h
->root
.type
= bfd_link_hash_new
;
636 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
637 bfd_link_repair_undef_list (&htab
->root
);
639 case bfd_link_hash_new
:
640 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
643 case bfd_link_hash_indirect
:
644 /* We had a versioned symbol in a dynamic library. We make the
645 the versioned symbol point to this one. */
646 bed
= get_elf_backend_data (output_bfd
);
648 while (hv
->root
.type
== bfd_link_hash_indirect
649 || hv
->root
.type
== bfd_link_hash_warning
)
650 hv
= (struct elf_link_hash_entry
*) hv
->root
.u
.i
.link
;
651 /* We don't need to update h->root.u since linker will set them
653 h
->root
.type
= bfd_link_hash_undefined
;
654 hv
->root
.type
= bfd_link_hash_indirect
;
655 hv
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
656 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hv
);
663 /* If this symbol is being provided by the linker script, and it is
664 currently defined by a dynamic object, but not by a regular
665 object, then mark it as undefined so that the generic linker will
666 force the correct value. */
670 h
->root
.type
= bfd_link_hash_undefined
;
672 /* If this symbol is not being provided by the linker script, and it is
673 currently defined by a dynamic object, but not by a regular object,
674 then clear out any version information because the symbol will not be
675 associated with the dynamic object any more. */
679 h
->verinfo
.verdef
= NULL
;
681 /* Make sure this symbol is not garbage collected. */
688 bed
= get_elf_backend_data (output_bfd
);
689 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
690 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
691 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
694 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
696 if (!bfd_link_relocatable (info
)
698 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
699 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
704 || bfd_link_dll (info
)
705 || elf_hash_table (info
)->is_relocatable_executable
)
708 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
711 /* If this is a weak defined symbol, and we know a corresponding
712 real symbol from the same dynamic object, make sure the real
713 symbol is also made into a dynamic symbol. */
714 if (h
->u
.weakdef
!= NULL
715 && h
->u
.weakdef
->dynindx
== -1)
717 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
725 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
726 success, and 2 on a failure caused by attempting to record a symbol
727 in a discarded section, eg. a discarded link-once section symbol. */
730 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
735 struct elf_link_local_dynamic_entry
*entry
;
736 struct elf_link_hash_table
*eht
;
737 struct elf_strtab_hash
*dynstr
;
740 Elf_External_Sym_Shndx eshndx
;
741 char esym
[sizeof (Elf64_External_Sym
)];
743 if (! is_elf_hash_table (info
->hash
))
746 /* See if the entry exists already. */
747 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
748 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
751 amt
= sizeof (*entry
);
752 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
756 /* Go find the symbol, so that we can find it's name. */
757 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
758 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
760 bfd_release (input_bfd
, entry
);
764 if (entry
->isym
.st_shndx
!= SHN_UNDEF
765 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
769 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
770 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
772 /* We can still bfd_release here as nothing has done another
773 bfd_alloc. We can't do this later in this function. */
774 bfd_release (input_bfd
, entry
);
779 name
= (bfd_elf_string_from_elf_section
780 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
781 entry
->isym
.st_name
));
783 dynstr
= elf_hash_table (info
)->dynstr
;
786 /* Create a strtab to hold the dynamic symbol names. */
787 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
792 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
793 if (dynstr_index
== (size_t) -1)
795 entry
->isym
.st_name
= dynstr_index
;
797 eht
= elf_hash_table (info
);
799 entry
->next
= eht
->dynlocal
;
800 eht
->dynlocal
= entry
;
801 entry
->input_bfd
= input_bfd
;
802 entry
->input_indx
= input_indx
;
805 /* Whatever binding the symbol had before, it's now local. */
807 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
809 /* The dynindx will be set at the end of size_dynamic_sections. */
814 /* Return the dynindex of a local dynamic symbol. */
817 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
821 struct elf_link_local_dynamic_entry
*e
;
823 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
824 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
829 /* This function is used to renumber the dynamic symbols, if some of
830 them are removed because they are marked as local. This is called
831 via elf_link_hash_traverse. */
834 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
837 size_t *count
= (size_t *) data
;
842 if (h
->dynindx
!= -1)
843 h
->dynindx
= ++(*count
);
849 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
850 STB_LOCAL binding. */
853 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
856 size_t *count
= (size_t *) data
;
858 if (!h
->forced_local
)
861 if (h
->dynindx
!= -1)
862 h
->dynindx
= ++(*count
);
867 /* Return true if the dynamic symbol for a given section should be
868 omitted when creating a shared library. */
870 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
871 struct bfd_link_info
*info
,
874 struct elf_link_hash_table
*htab
;
877 switch (elf_section_data (p
)->this_hdr
.sh_type
)
881 /* If sh_type is yet undecided, assume it could be
882 SHT_PROGBITS/SHT_NOBITS. */
884 htab
= elf_hash_table (info
);
885 if (p
== htab
->tls_sec
)
888 if (htab
->text_index_section
!= NULL
)
889 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
891 return (htab
->dynobj
!= NULL
892 && (ip
= bfd_get_linker_section (htab
->dynobj
, p
->name
)) != NULL
893 && ip
->output_section
== p
);
895 /* There shouldn't be section relative relocations
896 against any other section. */
902 /* Assign dynsym indices. In a shared library we generate a section
903 symbol for each output section, which come first. Next come symbols
904 which have been forced to local binding. Then all of the back-end
905 allocated local dynamic syms, followed by the rest of the global
909 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
910 struct bfd_link_info
*info
,
911 unsigned long *section_sym_count
)
913 unsigned long dynsymcount
= 0;
915 if (bfd_link_pic (info
)
916 || elf_hash_table (info
)->is_relocatable_executable
)
918 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
920 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
921 if ((p
->flags
& SEC_EXCLUDE
) == 0
922 && (p
->flags
& SEC_ALLOC
) != 0
923 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
924 elf_section_data (p
)->dynindx
= ++dynsymcount
;
926 elf_section_data (p
)->dynindx
= 0;
928 *section_sym_count
= dynsymcount
;
930 elf_link_hash_traverse (elf_hash_table (info
),
931 elf_link_renumber_local_hash_table_dynsyms
,
934 if (elf_hash_table (info
)->dynlocal
)
936 struct elf_link_local_dynamic_entry
*p
;
937 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
938 p
->dynindx
= ++dynsymcount
;
940 elf_hash_table (info
)->local_dynsymcount
= dynsymcount
;
942 elf_link_hash_traverse (elf_hash_table (info
),
943 elf_link_renumber_hash_table_dynsyms
,
946 /* There is an unused NULL entry at the head of the table which we
947 must account for in our count even if the table is empty since it
948 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
952 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
956 /* Merge st_other field. */
959 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
960 const Elf_Internal_Sym
*isym
, asection
*sec
,
961 bfd_boolean definition
, bfd_boolean dynamic
)
963 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
965 /* If st_other has a processor-specific meaning, specific
966 code might be needed here. */
967 if (bed
->elf_backend_merge_symbol_attribute
)
968 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
973 unsigned symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
974 unsigned hvis
= ELF_ST_VISIBILITY (h
->other
);
976 /* Keep the most constraining visibility. Leave the remainder
977 of the st_other field to elf_backend_merge_symbol_attribute. */
978 if (symvis
- 1 < hvis
- 1)
979 h
->other
= symvis
| (h
->other
& ~ELF_ST_VISIBILITY (-1));
982 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_DEFAULT
983 && (sec
->flags
& SEC_READONLY
) == 0)
984 h
->protected_def
= 1;
987 /* This function is called when we want to merge a new symbol with an
988 existing symbol. It handles the various cases which arise when we
989 find a definition in a dynamic object, or when there is already a
990 definition in a dynamic object. The new symbol is described by
991 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
992 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
993 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
994 of an old common symbol. We set OVERRIDE if the old symbol is
995 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
996 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
997 to change. By OK to change, we mean that we shouldn't warn if the
998 type or size does change. */
1001 _bfd_elf_merge_symbol (bfd
*abfd
,
1002 struct bfd_link_info
*info
,
1004 Elf_Internal_Sym
*sym
,
1007 struct elf_link_hash_entry
**sym_hash
,
1009 bfd_boolean
*pold_weak
,
1010 unsigned int *pold_alignment
,
1012 bfd_boolean
*override
,
1013 bfd_boolean
*type_change_ok
,
1014 bfd_boolean
*size_change_ok
,
1015 bfd_boolean
*matched
)
1017 asection
*sec
, *oldsec
;
1018 struct elf_link_hash_entry
*h
;
1019 struct elf_link_hash_entry
*hi
;
1020 struct elf_link_hash_entry
*flip
;
1023 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
1024 bfd_boolean newweak
, oldweak
, newfunc
, oldfunc
;
1025 const struct elf_backend_data
*bed
;
1032 bind
= ELF_ST_BIND (sym
->st_info
);
1034 if (! bfd_is_und_section (sec
))
1035 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
1037 h
= ((struct elf_link_hash_entry
*)
1038 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
1043 bed
= get_elf_backend_data (abfd
);
1045 /* NEW_VERSION is the symbol version of the new symbol. */
1046 if (h
->versioned
!= unversioned
)
1048 /* Symbol version is unknown or versioned. */
1049 new_version
= strrchr (name
, ELF_VER_CHR
);
1052 if (h
->versioned
== unknown
)
1054 if (new_version
> name
&& new_version
[-1] != ELF_VER_CHR
)
1055 h
->versioned
= versioned_hidden
;
1057 h
->versioned
= versioned
;
1060 if (new_version
[0] == '\0')
1064 h
->versioned
= unversioned
;
1069 /* For merging, we only care about real symbols. But we need to make
1070 sure that indirect symbol dynamic flags are updated. */
1072 while (h
->root
.type
== bfd_link_hash_indirect
1073 || h
->root
.type
== bfd_link_hash_warning
)
1074 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1078 if (hi
== h
|| h
->root
.type
== bfd_link_hash_new
)
1082 /* OLD_HIDDEN is true if the existing symbol is only visible
1083 to the symbol with the same symbol version. NEW_HIDDEN is
1084 true if the new symbol is only visible to the symbol with
1085 the same symbol version. */
1086 bfd_boolean old_hidden
= h
->versioned
== versioned_hidden
;
1087 bfd_boolean new_hidden
= hi
->versioned
== versioned_hidden
;
1088 if (!old_hidden
&& !new_hidden
)
1089 /* The new symbol matches the existing symbol if both
1094 /* OLD_VERSION is the symbol version of the existing
1098 if (h
->versioned
>= versioned
)
1099 old_version
= strrchr (h
->root
.root
.string
,
1104 /* The new symbol matches the existing symbol if they
1105 have the same symbol version. */
1106 *matched
= (old_version
== new_version
1107 || (old_version
!= NULL
1108 && new_version
!= NULL
1109 && strcmp (old_version
, new_version
) == 0));
1114 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1119 switch (h
->root
.type
)
1124 case bfd_link_hash_undefined
:
1125 case bfd_link_hash_undefweak
:
1126 oldbfd
= h
->root
.u
.undef
.abfd
;
1129 case bfd_link_hash_defined
:
1130 case bfd_link_hash_defweak
:
1131 oldbfd
= h
->root
.u
.def
.section
->owner
;
1132 oldsec
= h
->root
.u
.def
.section
;
1135 case bfd_link_hash_common
:
1136 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1137 oldsec
= h
->root
.u
.c
.p
->section
;
1139 *pold_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1142 if (poldbfd
&& *poldbfd
== NULL
)
1145 /* Differentiate strong and weak symbols. */
1146 newweak
= bind
== STB_WEAK
;
1147 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1148 || h
->root
.type
== bfd_link_hash_undefweak
);
1150 *pold_weak
= oldweak
;
1152 /* This code is for coping with dynamic objects, and is only useful
1153 if we are doing an ELF link. */
1154 if (!(*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
1157 /* We have to check it for every instance since the first few may be
1158 references and not all compilers emit symbol type for undefined
1160 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1162 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1163 respectively, is from a dynamic object. */
1165 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1167 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1168 syms and defined syms in dynamic libraries respectively.
1169 ref_dynamic on the other hand can be set for a symbol defined in
1170 a dynamic library, and def_dynamic may not be set; When the
1171 definition in a dynamic lib is overridden by a definition in the
1172 executable use of the symbol in the dynamic lib becomes a
1173 reference to the executable symbol. */
1176 if (bfd_is_und_section (sec
))
1178 if (bind
!= STB_WEAK
)
1180 h
->ref_dynamic_nonweak
= 1;
1181 hi
->ref_dynamic_nonweak
= 1;
1186 /* Update the existing symbol only if they match. */
1189 hi
->dynamic_def
= 1;
1193 /* If we just created the symbol, mark it as being an ELF symbol.
1194 Other than that, there is nothing to do--there is no merge issue
1195 with a newly defined symbol--so we just return. */
1197 if (h
->root
.type
== bfd_link_hash_new
)
1203 /* In cases involving weak versioned symbols, we may wind up trying
1204 to merge a symbol with itself. Catch that here, to avoid the
1205 confusion that results if we try to override a symbol with
1206 itself. The additional tests catch cases like
1207 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1208 dynamic object, which we do want to handle here. */
1210 && (newweak
|| oldweak
)
1211 && ((abfd
->flags
& DYNAMIC
) == 0
1212 || !h
->def_regular
))
1217 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1218 else if (oldsec
!= NULL
)
1220 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1221 indices used by MIPS ELF. */
1222 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1225 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1226 respectively, appear to be a definition rather than reference. */
1228 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1230 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1231 && h
->root
.type
!= bfd_link_hash_undefweak
1232 && h
->root
.type
!= bfd_link_hash_common
);
1234 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1235 respectively, appear to be a function. */
1237 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1238 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1240 oldfunc
= (h
->type
!= STT_NOTYPE
1241 && bed
->is_function_type (h
->type
));
1243 /* If creating a default indirect symbol ("foo" or "foo@") from a
1244 dynamic versioned definition ("foo@@") skip doing so if there is
1245 an existing regular definition with a different type. We don't
1246 want, for example, a "time" variable in the executable overriding
1247 a "time" function in a shared library. */
1248 if (pold_alignment
== NULL
1252 && (olddef
|| h
->root
.type
== bfd_link_hash_common
)
1253 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1254 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1255 && h
->type
!= STT_NOTYPE
1256 && !(newfunc
&& oldfunc
))
1262 /* Check TLS symbols. We don't check undefined symbols introduced
1263 by "ld -u" which have no type (and oldbfd NULL), and we don't
1264 check symbols from plugins because they also have no type. */
1266 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1267 && (abfd
->flags
& BFD_PLUGIN
) == 0
1268 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1269 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1272 bfd_boolean ntdef
, tdef
;
1273 asection
*ntsec
, *tsec
;
1275 if (h
->type
== STT_TLS
)
1296 /* xgettext:c-format */
1297 (_("%s: TLS definition in %B section %A "
1298 "mismatches non-TLS definition in %B section %A"),
1299 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
1300 else if (!tdef
&& !ntdef
)
1302 /* xgettext:c-format */
1303 (_("%s: TLS reference in %B "
1304 "mismatches non-TLS reference in %B"),
1305 tbfd
, ntbfd
, h
->root
.root
.string
);
1308 /* xgettext:c-format */
1309 (_("%s: TLS definition in %B section %A "
1310 "mismatches non-TLS reference in %B"),
1311 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
1314 /* xgettext:c-format */
1315 (_("%s: TLS reference in %B "
1316 "mismatches non-TLS definition in %B section %A"),
1317 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
1319 bfd_set_error (bfd_error_bad_value
);
1323 /* If the old symbol has non-default visibility, we ignore the new
1324 definition from a dynamic object. */
1326 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1327 && !bfd_is_und_section (sec
))
1330 /* Make sure this symbol is dynamic. */
1332 hi
->ref_dynamic
= 1;
1333 /* A protected symbol has external availability. Make sure it is
1334 recorded as dynamic.
1336 FIXME: Should we check type and size for protected symbol? */
1337 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1338 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1343 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1346 /* If the new symbol with non-default visibility comes from a
1347 relocatable file and the old definition comes from a dynamic
1348 object, we remove the old definition. */
1349 if (hi
->root
.type
== bfd_link_hash_indirect
)
1351 /* Handle the case where the old dynamic definition is
1352 default versioned. We need to copy the symbol info from
1353 the symbol with default version to the normal one if it
1354 was referenced before. */
1357 hi
->root
.type
= h
->root
.type
;
1358 h
->root
.type
= bfd_link_hash_indirect
;
1359 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1361 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1362 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1364 /* If the new symbol is hidden or internal, completely undo
1365 any dynamic link state. */
1366 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1367 h
->forced_local
= 0;
1374 /* FIXME: Should we check type and size for protected symbol? */
1384 /* If the old symbol was undefined before, then it will still be
1385 on the undefs list. If the new symbol is undefined or
1386 common, we can't make it bfd_link_hash_new here, because new
1387 undefined or common symbols will be added to the undefs list
1388 by _bfd_generic_link_add_one_symbol. Symbols may not be
1389 added twice to the undefs list. Also, if the new symbol is
1390 undefweak then we don't want to lose the strong undef. */
1391 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1393 h
->root
.type
= bfd_link_hash_undefined
;
1394 h
->root
.u
.undef
.abfd
= abfd
;
1398 h
->root
.type
= bfd_link_hash_new
;
1399 h
->root
.u
.undef
.abfd
= NULL
;
1402 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1404 /* If the new symbol is hidden or internal, completely undo
1405 any dynamic link state. */
1406 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1407 h
->forced_local
= 0;
1413 /* FIXME: Should we check type and size for protected symbol? */
1419 /* If a new weak symbol definition comes from a regular file and the
1420 old symbol comes from a dynamic library, we treat the new one as
1421 strong. Similarly, an old weak symbol definition from a regular
1422 file is treated as strong when the new symbol comes from a dynamic
1423 library. Further, an old weak symbol from a dynamic library is
1424 treated as strong if the new symbol is from a dynamic library.
1425 This reflects the way glibc's ld.so works.
1427 Do this before setting *type_change_ok or *size_change_ok so that
1428 we warn properly when dynamic library symbols are overridden. */
1430 if (newdef
&& !newdyn
&& olddyn
)
1432 if (olddef
&& newdyn
)
1435 /* Allow changes between different types of function symbol. */
1436 if (newfunc
&& oldfunc
)
1437 *type_change_ok
= TRUE
;
1439 /* It's OK to change the type if either the existing symbol or the
1440 new symbol is weak. A type change is also OK if the old symbol
1441 is undefined and the new symbol is defined. */
1446 && h
->root
.type
== bfd_link_hash_undefined
))
1447 *type_change_ok
= TRUE
;
1449 /* It's OK to change the size if either the existing symbol or the
1450 new symbol is weak, or if the old symbol is undefined. */
1453 || h
->root
.type
== bfd_link_hash_undefined
)
1454 *size_change_ok
= TRUE
;
1456 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1457 symbol, respectively, appears to be a common symbol in a dynamic
1458 object. If a symbol appears in an uninitialized section, and is
1459 not weak, and is not a function, then it may be a common symbol
1460 which was resolved when the dynamic object was created. We want
1461 to treat such symbols specially, because they raise special
1462 considerations when setting the symbol size: if the symbol
1463 appears as a common symbol in a regular object, and the size in
1464 the regular object is larger, we must make sure that we use the
1465 larger size. This problematic case can always be avoided in C,
1466 but it must be handled correctly when using Fortran shared
1469 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1470 likewise for OLDDYNCOMMON and OLDDEF.
1472 Note that this test is just a heuristic, and that it is quite
1473 possible to have an uninitialized symbol in a shared object which
1474 is really a definition, rather than a common symbol. This could
1475 lead to some minor confusion when the symbol really is a common
1476 symbol in some regular object. However, I think it will be
1482 && (sec
->flags
& SEC_ALLOC
) != 0
1483 && (sec
->flags
& SEC_LOAD
) == 0
1486 newdyncommon
= TRUE
;
1488 newdyncommon
= FALSE
;
1492 && h
->root
.type
== bfd_link_hash_defined
1494 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1495 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1498 olddyncommon
= TRUE
;
1500 olddyncommon
= FALSE
;
1502 /* We now know everything about the old and new symbols. We ask the
1503 backend to check if we can merge them. */
1504 if (bed
->merge_symbol
!= NULL
)
1506 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1511 /* If both the old and the new symbols look like common symbols in a
1512 dynamic object, set the size of the symbol to the larger of the
1517 && sym
->st_size
!= h
->size
)
1519 /* Since we think we have two common symbols, issue a multiple
1520 common warning if desired. Note that we only warn if the
1521 size is different. If the size is the same, we simply let
1522 the old symbol override the new one as normally happens with
1523 symbols defined in dynamic objects. */
1525 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1526 bfd_link_hash_common
, sym
->st_size
);
1527 if (sym
->st_size
> h
->size
)
1528 h
->size
= sym
->st_size
;
1530 *size_change_ok
= TRUE
;
1533 /* If we are looking at a dynamic object, and we have found a
1534 definition, we need to see if the symbol was already defined by
1535 some other object. If so, we want to use the existing
1536 definition, and we do not want to report a multiple symbol
1537 definition error; we do this by clobbering *PSEC to be
1538 bfd_und_section_ptr.
1540 We treat a common symbol as a definition if the symbol in the
1541 shared library is a function, since common symbols always
1542 represent variables; this can cause confusion in principle, but
1543 any such confusion would seem to indicate an erroneous program or
1544 shared library. We also permit a common symbol in a regular
1545 object to override a weak symbol in a shared object. A common
1546 symbol in executable also overrides a symbol in a shared object. */
1551 || (h
->root
.type
== bfd_link_hash_common
1554 || (!olddyn
&& bfd_link_executable (info
))))))
1558 newdyncommon
= FALSE
;
1560 *psec
= sec
= bfd_und_section_ptr
;
1561 *size_change_ok
= TRUE
;
1563 /* If we get here when the old symbol is a common symbol, then
1564 we are explicitly letting it override a weak symbol or
1565 function in a dynamic object, and we don't want to warn about
1566 a type change. If the old symbol is a defined symbol, a type
1567 change warning may still be appropriate. */
1569 if (h
->root
.type
== bfd_link_hash_common
)
1570 *type_change_ok
= TRUE
;
1573 /* Handle the special case of an old common symbol merging with a
1574 new symbol which looks like a common symbol in a shared object.
1575 We change *PSEC and *PVALUE to make the new symbol look like a
1576 common symbol, and let _bfd_generic_link_add_one_symbol do the
1580 && h
->root
.type
== bfd_link_hash_common
)
1584 newdyncommon
= FALSE
;
1585 *pvalue
= sym
->st_size
;
1586 *psec
= sec
= bed
->common_section (oldsec
);
1587 *size_change_ok
= TRUE
;
1590 /* Skip weak definitions of symbols that are already defined. */
1591 if (newdef
&& olddef
&& newweak
)
1593 /* Don't skip new non-IR weak syms. */
1594 if (!(oldbfd
!= NULL
1595 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1596 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1602 /* Merge st_other. If the symbol already has a dynamic index,
1603 but visibility says it should not be visible, turn it into a
1605 elf_merge_st_other (abfd
, h
, sym
, sec
, newdef
, newdyn
);
1606 if (h
->dynindx
!= -1)
1607 switch (ELF_ST_VISIBILITY (h
->other
))
1611 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1616 /* If the old symbol is from a dynamic object, and the new symbol is
1617 a definition which is not from a dynamic object, then the new
1618 symbol overrides the old symbol. Symbols from regular files
1619 always take precedence over symbols from dynamic objects, even if
1620 they are defined after the dynamic object in the link.
1622 As above, we again permit a common symbol in a regular object to
1623 override a definition in a shared object if the shared object
1624 symbol is a function or is weak. */
1629 || (bfd_is_com_section (sec
)
1630 && (oldweak
|| oldfunc
)))
1635 /* Change the hash table entry to undefined, and let
1636 _bfd_generic_link_add_one_symbol do the right thing with the
1639 h
->root
.type
= bfd_link_hash_undefined
;
1640 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1641 *size_change_ok
= TRUE
;
1644 olddyncommon
= FALSE
;
1646 /* We again permit a type change when a common symbol may be
1647 overriding a function. */
1649 if (bfd_is_com_section (sec
))
1653 /* If a common symbol overrides a function, make sure
1654 that it isn't defined dynamically nor has type
1657 h
->type
= STT_NOTYPE
;
1659 *type_change_ok
= TRUE
;
1662 if (hi
->root
.type
== bfd_link_hash_indirect
)
1665 /* This union may have been set to be non-NULL when this symbol
1666 was seen in a dynamic object. We must force the union to be
1667 NULL, so that it is correct for a regular symbol. */
1668 h
->verinfo
.vertree
= NULL
;
1671 /* Handle the special case of a new common symbol merging with an
1672 old symbol that looks like it might be a common symbol defined in
1673 a shared object. Note that we have already handled the case in
1674 which a new common symbol should simply override the definition
1675 in the shared library. */
1678 && bfd_is_com_section (sec
)
1681 /* It would be best if we could set the hash table entry to a
1682 common symbol, but we don't know what to use for the section
1683 or the alignment. */
1684 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1685 bfd_link_hash_common
, sym
->st_size
);
1687 /* If the presumed common symbol in the dynamic object is
1688 larger, pretend that the new symbol has its size. */
1690 if (h
->size
> *pvalue
)
1693 /* We need to remember the alignment required by the symbol
1694 in the dynamic object. */
1695 BFD_ASSERT (pold_alignment
);
1696 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1699 olddyncommon
= FALSE
;
1701 h
->root
.type
= bfd_link_hash_undefined
;
1702 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1704 *size_change_ok
= TRUE
;
1705 *type_change_ok
= TRUE
;
1707 if (hi
->root
.type
== bfd_link_hash_indirect
)
1710 h
->verinfo
.vertree
= NULL
;
1715 /* Handle the case where we had a versioned symbol in a dynamic
1716 library and now find a definition in a normal object. In this
1717 case, we make the versioned symbol point to the normal one. */
1718 flip
->root
.type
= h
->root
.type
;
1719 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1720 h
->root
.type
= bfd_link_hash_indirect
;
1721 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1722 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1726 flip
->ref_dynamic
= 1;
1733 /* This function is called to create an indirect symbol from the
1734 default for the symbol with the default version if needed. The
1735 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1736 set DYNSYM if the new indirect symbol is dynamic. */
1739 _bfd_elf_add_default_symbol (bfd
*abfd
,
1740 struct bfd_link_info
*info
,
1741 struct elf_link_hash_entry
*h
,
1743 Elf_Internal_Sym
*sym
,
1747 bfd_boolean
*dynsym
)
1749 bfd_boolean type_change_ok
;
1750 bfd_boolean size_change_ok
;
1753 struct elf_link_hash_entry
*hi
;
1754 struct bfd_link_hash_entry
*bh
;
1755 const struct elf_backend_data
*bed
;
1756 bfd_boolean collect
;
1757 bfd_boolean dynamic
;
1758 bfd_boolean override
;
1760 size_t len
, shortlen
;
1762 bfd_boolean matched
;
1764 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1767 /* If this symbol has a version, and it is the default version, we
1768 create an indirect symbol from the default name to the fully
1769 decorated name. This will cause external references which do not
1770 specify a version to be bound to this version of the symbol. */
1771 p
= strchr (name
, ELF_VER_CHR
);
1772 if (h
->versioned
== unknown
)
1776 h
->versioned
= unversioned
;
1781 if (p
[1] != ELF_VER_CHR
)
1783 h
->versioned
= versioned_hidden
;
1787 h
->versioned
= versioned
;
1792 /* PR ld/19073: We may see an unversioned definition after the
1798 bed
= get_elf_backend_data (abfd
);
1799 collect
= bed
->collect
;
1800 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1802 shortlen
= p
- name
;
1803 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1804 if (shortname
== NULL
)
1806 memcpy (shortname
, name
, shortlen
);
1807 shortname
[shortlen
] = '\0';
1809 /* We are going to create a new symbol. Merge it with any existing
1810 symbol with this name. For the purposes of the merge, act as
1811 though we were defining the symbol we just defined, although we
1812 actually going to define an indirect symbol. */
1813 type_change_ok
= FALSE
;
1814 size_change_ok
= FALSE
;
1817 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1818 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1819 &type_change_ok
, &size_change_ok
, &matched
))
1825 if (hi
->def_regular
)
1827 /* If the undecorated symbol will have a version added by a
1828 script different to H, then don't indirect to/from the
1829 undecorated symbol. This isn't ideal because we may not yet
1830 have seen symbol versions, if given by a script on the
1831 command line rather than via --version-script. */
1832 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
1837 = bfd_find_version_for_sym (info
->version_info
,
1838 hi
->root
.root
.string
, &hide
);
1839 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
1841 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
1845 if (hi
->verinfo
.vertree
!= NULL
1846 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
1852 /* Add the default symbol if not performing a relocatable link. */
1853 if (! bfd_link_relocatable (info
))
1856 if (! (_bfd_generic_link_add_one_symbol
1857 (info
, abfd
, shortname
, BSF_INDIRECT
,
1858 bfd_ind_section_ptr
,
1859 0, name
, FALSE
, collect
, &bh
)))
1861 hi
= (struct elf_link_hash_entry
*) bh
;
1866 /* In this case the symbol named SHORTNAME is overriding the
1867 indirect symbol we want to add. We were planning on making
1868 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1869 is the name without a version. NAME is the fully versioned
1870 name, and it is the default version.
1872 Overriding means that we already saw a definition for the
1873 symbol SHORTNAME in a regular object, and it is overriding
1874 the symbol defined in the dynamic object.
1876 When this happens, we actually want to change NAME, the
1877 symbol we just added, to refer to SHORTNAME. This will cause
1878 references to NAME in the shared object to become references
1879 to SHORTNAME in the regular object. This is what we expect
1880 when we override a function in a shared object: that the
1881 references in the shared object will be mapped to the
1882 definition in the regular object. */
1884 while (hi
->root
.type
== bfd_link_hash_indirect
1885 || hi
->root
.type
== bfd_link_hash_warning
)
1886 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1888 h
->root
.type
= bfd_link_hash_indirect
;
1889 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1893 hi
->ref_dynamic
= 1;
1897 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1902 /* Now set HI to H, so that the following code will set the
1903 other fields correctly. */
1907 /* Check if HI is a warning symbol. */
1908 if (hi
->root
.type
== bfd_link_hash_warning
)
1909 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1911 /* If there is a duplicate definition somewhere, then HI may not
1912 point to an indirect symbol. We will have reported an error to
1913 the user in that case. */
1915 if (hi
->root
.type
== bfd_link_hash_indirect
)
1917 struct elf_link_hash_entry
*ht
;
1919 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1920 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
1922 /* A reference to the SHORTNAME symbol from a dynamic library
1923 will be satisfied by the versioned symbol at runtime. In
1924 effect, we have a reference to the versioned symbol. */
1925 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
1926 hi
->dynamic_def
|= ht
->dynamic_def
;
1928 /* See if the new flags lead us to realize that the symbol must
1934 if (! bfd_link_executable (info
)
1941 if (hi
->ref_regular
)
1947 /* We also need to define an indirection from the nondefault version
1951 len
= strlen (name
);
1952 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
1953 if (shortname
== NULL
)
1955 memcpy (shortname
, name
, shortlen
);
1956 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1958 /* Once again, merge with any existing symbol. */
1959 type_change_ok
= FALSE
;
1960 size_change_ok
= FALSE
;
1962 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1963 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1964 &type_change_ok
, &size_change_ok
, &matched
))
1972 /* Here SHORTNAME is a versioned name, so we don't expect to see
1973 the type of override we do in the case above unless it is
1974 overridden by a versioned definition. */
1975 if (hi
->root
.type
!= bfd_link_hash_defined
1976 && hi
->root
.type
!= bfd_link_hash_defweak
)
1978 /* xgettext:c-format */
1979 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1985 if (! (_bfd_generic_link_add_one_symbol
1986 (info
, abfd
, shortname
, BSF_INDIRECT
,
1987 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1989 hi
= (struct elf_link_hash_entry
*) bh
;
1991 /* If there is a duplicate definition somewhere, then HI may not
1992 point to an indirect symbol. We will have reported an error
1993 to the user in that case. */
1995 if (hi
->root
.type
== bfd_link_hash_indirect
)
1997 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
1998 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
1999 hi
->dynamic_def
|= h
->dynamic_def
;
2001 /* See if the new flags lead us to realize that the symbol
2007 if (! bfd_link_executable (info
)
2013 if (hi
->ref_regular
)
2023 /* This routine is used to export all defined symbols into the dynamic
2024 symbol table. It is called via elf_link_hash_traverse. */
2027 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
2029 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2031 /* Ignore indirect symbols. These are added by the versioning code. */
2032 if (h
->root
.type
== bfd_link_hash_indirect
)
2035 /* Ignore this if we won't export it. */
2036 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2039 if (h
->dynindx
== -1
2040 && (h
->def_regular
|| h
->ref_regular
)
2041 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2042 h
->root
.root
.string
))
2044 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2054 /* Look through the symbols which are defined in other shared
2055 libraries and referenced here. Update the list of version
2056 dependencies. This will be put into the .gnu.version_r section.
2057 This function is called via elf_link_hash_traverse. */
2060 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2063 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2064 Elf_Internal_Verneed
*t
;
2065 Elf_Internal_Vernaux
*a
;
2068 /* We only care about symbols defined in shared objects with version
2073 || h
->verinfo
.verdef
== NULL
2074 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2075 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2078 /* See if we already know about this version. */
2079 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2083 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2086 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2087 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2093 /* This is a new version. Add it to tree we are building. */
2098 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2101 rinfo
->failed
= TRUE
;
2105 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2106 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2107 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2111 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2114 rinfo
->failed
= TRUE
;
2118 /* Note that we are copying a string pointer here, and testing it
2119 above. If bfd_elf_string_from_elf_section is ever changed to
2120 discard the string data when low in memory, this will have to be
2122 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2124 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2125 a
->vna_nextptr
= t
->vn_auxptr
;
2127 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2130 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2137 /* Figure out appropriate versions for all the symbols. We may not
2138 have the version number script until we have read all of the input
2139 files, so until that point we don't know which symbols should be
2140 local. This function is called via elf_link_hash_traverse. */
2143 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2145 struct elf_info_failed
*sinfo
;
2146 struct bfd_link_info
*info
;
2147 const struct elf_backend_data
*bed
;
2148 struct elf_info_failed eif
;
2151 sinfo
= (struct elf_info_failed
*) data
;
2154 /* Fix the symbol flags. */
2157 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2160 sinfo
->failed
= TRUE
;
2164 /* We only need version numbers for symbols defined in regular
2166 if (!h
->def_regular
)
2169 bed
= get_elf_backend_data (info
->output_bfd
);
2170 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2171 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2173 struct bfd_elf_version_tree
*t
;
2176 if (*p
== ELF_VER_CHR
)
2179 /* If there is no version string, we can just return out. */
2183 /* Look for the version. If we find it, it is no longer weak. */
2184 for (t
= sinfo
->info
->version_info
; t
!= NULL
; t
= t
->next
)
2186 if (strcmp (t
->name
, p
) == 0)
2190 struct bfd_elf_version_expr
*d
;
2192 len
= p
- h
->root
.root
.string
;
2193 alc
= (char *) bfd_malloc (len
);
2196 sinfo
->failed
= TRUE
;
2199 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2200 alc
[len
- 1] = '\0';
2201 if (alc
[len
- 2] == ELF_VER_CHR
)
2202 alc
[len
- 2] = '\0';
2204 h
->verinfo
.vertree
= t
;
2208 if (t
->globals
.list
!= NULL
)
2209 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2211 /* See if there is anything to force this symbol to
2213 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2215 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2218 && ! info
->export_dynamic
)
2219 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2227 /* If we are building an application, we need to create a
2228 version node for this version. */
2229 if (t
== NULL
&& bfd_link_executable (info
))
2231 struct bfd_elf_version_tree
**pp
;
2234 /* If we aren't going to export this symbol, we don't need
2235 to worry about it. */
2236 if (h
->dynindx
== -1)
2239 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2243 sinfo
->failed
= TRUE
;
2248 t
->name_indx
= (unsigned int) -1;
2252 /* Don't count anonymous version tag. */
2253 if (sinfo
->info
->version_info
!= NULL
2254 && sinfo
->info
->version_info
->vernum
== 0)
2256 for (pp
= &sinfo
->info
->version_info
;
2260 t
->vernum
= version_index
;
2264 h
->verinfo
.vertree
= t
;
2268 /* We could not find the version for a symbol when
2269 generating a shared archive. Return an error. */
2271 /* xgettext:c-format */
2272 (_("%B: version node not found for symbol %s"),
2273 info
->output_bfd
, h
->root
.root
.string
);
2274 bfd_set_error (bfd_error_bad_value
);
2275 sinfo
->failed
= TRUE
;
2280 /* If we don't have a version for this symbol, see if we can find
2282 if (h
->verinfo
.vertree
== NULL
&& sinfo
->info
->version_info
!= NULL
)
2287 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2288 h
->root
.root
.string
, &hide
);
2289 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2290 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2296 /* Read and swap the relocs from the section indicated by SHDR. This
2297 may be either a REL or a RELA section. The relocations are
2298 translated into RELA relocations and stored in INTERNAL_RELOCS,
2299 which should have already been allocated to contain enough space.
2300 The EXTERNAL_RELOCS are a buffer where the external form of the
2301 relocations should be stored.
2303 Returns FALSE if something goes wrong. */
2306 elf_link_read_relocs_from_section (bfd
*abfd
,
2308 Elf_Internal_Shdr
*shdr
,
2309 void *external_relocs
,
2310 Elf_Internal_Rela
*internal_relocs
)
2312 const struct elf_backend_data
*bed
;
2313 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2314 const bfd_byte
*erela
;
2315 const bfd_byte
*erelaend
;
2316 Elf_Internal_Rela
*irela
;
2317 Elf_Internal_Shdr
*symtab_hdr
;
2320 /* Position ourselves at the start of the section. */
2321 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2324 /* Read the relocations. */
2325 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2328 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2329 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2331 bed
= get_elf_backend_data (abfd
);
2333 /* Convert the external relocations to the internal format. */
2334 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2335 swap_in
= bed
->s
->swap_reloc_in
;
2336 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2337 swap_in
= bed
->s
->swap_reloca_in
;
2340 bfd_set_error (bfd_error_wrong_format
);
2344 erela
= (const bfd_byte
*) external_relocs
;
2345 erelaend
= erela
+ shdr
->sh_size
;
2346 irela
= internal_relocs
;
2347 while (erela
< erelaend
)
2351 (*swap_in
) (abfd
, erela
, irela
);
2352 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2353 if (bed
->s
->arch_size
== 64)
2357 if ((size_t) r_symndx
>= nsyms
)
2360 /* xgettext:c-format */
2361 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2362 " for offset 0x%lx in section `%A'"),
2364 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2365 bfd_set_error (bfd_error_bad_value
);
2369 else if (r_symndx
!= STN_UNDEF
)
2372 /* xgettext:c-format */
2373 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2374 " when the object file has no symbol table"),
2376 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2377 bfd_set_error (bfd_error_bad_value
);
2380 irela
+= bed
->s
->int_rels_per_ext_rel
;
2381 erela
+= shdr
->sh_entsize
;
2387 /* Read and swap the relocs for a section O. They may have been
2388 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2389 not NULL, they are used as buffers to read into. They are known to
2390 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2391 the return value is allocated using either malloc or bfd_alloc,
2392 according to the KEEP_MEMORY argument. If O has two relocation
2393 sections (both REL and RELA relocations), then the REL_HDR
2394 relocations will appear first in INTERNAL_RELOCS, followed by the
2395 RELA_HDR relocations. */
2398 _bfd_elf_link_read_relocs (bfd
*abfd
,
2400 void *external_relocs
,
2401 Elf_Internal_Rela
*internal_relocs
,
2402 bfd_boolean keep_memory
)
2404 void *alloc1
= NULL
;
2405 Elf_Internal_Rela
*alloc2
= NULL
;
2406 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2407 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2408 Elf_Internal_Rela
*internal_rela_relocs
;
2410 if (esdo
->relocs
!= NULL
)
2411 return esdo
->relocs
;
2413 if (o
->reloc_count
== 0)
2416 if (internal_relocs
== NULL
)
2420 size
= o
->reloc_count
;
2421 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2423 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2425 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2426 if (internal_relocs
== NULL
)
2430 if (external_relocs
== NULL
)
2432 bfd_size_type size
= 0;
2435 size
+= esdo
->rel
.hdr
->sh_size
;
2437 size
+= esdo
->rela
.hdr
->sh_size
;
2439 alloc1
= bfd_malloc (size
);
2442 external_relocs
= alloc1
;
2445 internal_rela_relocs
= internal_relocs
;
2448 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2452 external_relocs
= (((bfd_byte
*) external_relocs
)
2453 + esdo
->rel
.hdr
->sh_size
);
2454 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2455 * bed
->s
->int_rels_per_ext_rel
);
2459 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2461 internal_rela_relocs
)))
2464 /* Cache the results for next time, if we can. */
2466 esdo
->relocs
= internal_relocs
;
2471 /* Don't free alloc2, since if it was allocated we are passing it
2472 back (under the name of internal_relocs). */
2474 return internal_relocs
;
2482 bfd_release (abfd
, alloc2
);
2489 /* Compute the size of, and allocate space for, REL_HDR which is the
2490 section header for a section containing relocations for O. */
2493 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2494 struct bfd_elf_section_reloc_data
*reldata
)
2496 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2498 /* That allows us to calculate the size of the section. */
2499 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2501 /* The contents field must last into write_object_contents, so we
2502 allocate it with bfd_alloc rather than malloc. Also since we
2503 cannot be sure that the contents will actually be filled in,
2504 we zero the allocated space. */
2505 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2506 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2509 if (reldata
->hashes
== NULL
&& reldata
->count
)
2511 struct elf_link_hash_entry
**p
;
2513 p
= ((struct elf_link_hash_entry
**)
2514 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2518 reldata
->hashes
= p
;
2524 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2525 originated from the section given by INPUT_REL_HDR) to the
2529 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2530 asection
*input_section
,
2531 Elf_Internal_Shdr
*input_rel_hdr
,
2532 Elf_Internal_Rela
*internal_relocs
,
2533 struct elf_link_hash_entry
**rel_hash
2536 Elf_Internal_Rela
*irela
;
2537 Elf_Internal_Rela
*irelaend
;
2539 struct bfd_elf_section_reloc_data
*output_reldata
;
2540 asection
*output_section
;
2541 const struct elf_backend_data
*bed
;
2542 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2543 struct bfd_elf_section_data
*esdo
;
2545 output_section
= input_section
->output_section
;
2547 bed
= get_elf_backend_data (output_bfd
);
2548 esdo
= elf_section_data (output_section
);
2549 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2551 output_reldata
= &esdo
->rel
;
2552 swap_out
= bed
->s
->swap_reloc_out
;
2554 else if (esdo
->rela
.hdr
2555 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2557 output_reldata
= &esdo
->rela
;
2558 swap_out
= bed
->s
->swap_reloca_out
;
2563 /* xgettext:c-format */
2564 (_("%B: relocation size mismatch in %B section %A"),
2565 output_bfd
, input_section
->owner
, input_section
);
2566 bfd_set_error (bfd_error_wrong_format
);
2570 erel
= output_reldata
->hdr
->contents
;
2571 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2572 irela
= internal_relocs
;
2573 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2574 * bed
->s
->int_rels_per_ext_rel
);
2575 while (irela
< irelaend
)
2577 (*swap_out
) (output_bfd
, irela
, erel
);
2578 irela
+= bed
->s
->int_rels_per_ext_rel
;
2579 erel
+= input_rel_hdr
->sh_entsize
;
2582 /* Bump the counter, so that we know where to add the next set of
2584 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2589 /* Make weak undefined symbols in PIE dynamic. */
2592 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2593 struct elf_link_hash_entry
*h
)
2595 if (bfd_link_pie (info
)
2597 && h
->root
.type
== bfd_link_hash_undefweak
)
2598 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2603 /* Fix up the flags for a symbol. This handles various cases which
2604 can only be fixed after all the input files are seen. This is
2605 currently called by both adjust_dynamic_symbol and
2606 assign_sym_version, which is unnecessary but perhaps more robust in
2607 the face of future changes. */
2610 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2611 struct elf_info_failed
*eif
)
2613 const struct elf_backend_data
*bed
;
2615 /* If this symbol was mentioned in a non-ELF file, try to set
2616 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2617 permit a non-ELF file to correctly refer to a symbol defined in
2618 an ELF dynamic object. */
2621 while (h
->root
.type
== bfd_link_hash_indirect
)
2622 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2624 if (h
->root
.type
!= bfd_link_hash_defined
2625 && h
->root
.type
!= bfd_link_hash_defweak
)
2628 h
->ref_regular_nonweak
= 1;
2632 if (h
->root
.u
.def
.section
->owner
!= NULL
2633 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2634 == bfd_target_elf_flavour
))
2637 h
->ref_regular_nonweak
= 1;
2643 if (h
->dynindx
== -1
2647 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2656 /* Unfortunately, NON_ELF is only correct if the symbol
2657 was first seen in a non-ELF file. Fortunately, if the symbol
2658 was first seen in an ELF file, we're probably OK unless the
2659 symbol was defined in a non-ELF file. Catch that case here.
2660 FIXME: We're still in trouble if the symbol was first seen in
2661 a dynamic object, and then later in a non-ELF regular object. */
2662 if ((h
->root
.type
== bfd_link_hash_defined
2663 || h
->root
.type
== bfd_link_hash_defweak
)
2665 && (h
->root
.u
.def
.section
->owner
!= NULL
2666 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2667 != bfd_target_elf_flavour
)
2668 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2669 && !h
->def_dynamic
)))
2673 /* Backend specific symbol fixup. */
2674 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2675 if (bed
->elf_backend_fixup_symbol
2676 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2679 /* If this is a final link, and the symbol was defined as a common
2680 symbol in a regular object file, and there was no definition in
2681 any dynamic object, then the linker will have allocated space for
2682 the symbol in a common section but the DEF_REGULAR
2683 flag will not have been set. */
2684 if (h
->root
.type
== bfd_link_hash_defined
2688 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
2691 /* If a weak undefined symbol has non-default visibility, we also
2692 hide it from the dynamic linker. */
2693 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2694 && h
->root
.type
== bfd_link_hash_undefweak
)
2695 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2697 /* A hidden versioned symbol in executable should be forced local if
2698 it is is locally defined, not referenced by shared library and not
2700 else if (bfd_link_executable (eif
->info
)
2701 && h
->versioned
== versioned_hidden
2702 && !eif
->info
->export_dynamic
2706 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2708 /* If -Bsymbolic was used (which means to bind references to global
2709 symbols to the definition within the shared object), and this
2710 symbol was defined in a regular object, then it actually doesn't
2711 need a PLT entry. Likewise, if the symbol has non-default
2712 visibility. If the symbol has hidden or internal visibility, we
2713 will force it local. */
2714 else if (h
->needs_plt
2715 && bfd_link_pic (eif
->info
)
2716 && is_elf_hash_table (eif
->info
->hash
)
2717 && (SYMBOLIC_BIND (eif
->info
, h
)
2718 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2721 bfd_boolean force_local
;
2723 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2724 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2725 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2728 /* If this is a weak defined symbol in a dynamic object, and we know
2729 the real definition in the dynamic object, copy interesting flags
2730 over to the real definition. */
2731 if (h
->u
.weakdef
!= NULL
)
2733 /* If the real definition is defined by a regular object file,
2734 don't do anything special. See the longer description in
2735 _bfd_elf_adjust_dynamic_symbol, below. */
2736 if (h
->u
.weakdef
->def_regular
)
2737 h
->u
.weakdef
= NULL
;
2740 struct elf_link_hash_entry
*weakdef
= h
->u
.weakdef
;
2742 while (h
->root
.type
== bfd_link_hash_indirect
)
2743 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2745 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2746 || h
->root
.type
== bfd_link_hash_defweak
);
2747 BFD_ASSERT (weakdef
->def_dynamic
);
2748 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2749 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2750 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
, h
);
2757 /* Make the backend pick a good value for a dynamic symbol. This is
2758 called via elf_link_hash_traverse, and also calls itself
2762 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2764 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2766 const struct elf_backend_data
*bed
;
2768 if (! is_elf_hash_table (eif
->info
->hash
))
2771 /* Ignore indirect symbols. These are added by the versioning code. */
2772 if (h
->root
.type
== bfd_link_hash_indirect
)
2775 /* Fix the symbol flags. */
2776 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2779 /* If this symbol does not require a PLT entry, and it is not
2780 defined by a dynamic object, or is not referenced by a regular
2781 object, ignore it. We do have to handle a weak defined symbol,
2782 even if no regular object refers to it, if we decided to add it
2783 to the dynamic symbol table. FIXME: Do we normally need to worry
2784 about symbols which are defined by one dynamic object and
2785 referenced by another one? */
2787 && h
->type
!= STT_GNU_IFUNC
2791 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2793 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2797 /* If we've already adjusted this symbol, don't do it again. This
2798 can happen via a recursive call. */
2799 if (h
->dynamic_adjusted
)
2802 /* Don't look at this symbol again. Note that we must set this
2803 after checking the above conditions, because we may look at a
2804 symbol once, decide not to do anything, and then get called
2805 recursively later after REF_REGULAR is set below. */
2806 h
->dynamic_adjusted
= 1;
2808 /* If this is a weak definition, and we know a real definition, and
2809 the real symbol is not itself defined by a regular object file,
2810 then get a good value for the real definition. We handle the
2811 real symbol first, for the convenience of the backend routine.
2813 Note that there is a confusing case here. If the real definition
2814 is defined by a regular object file, we don't get the real symbol
2815 from the dynamic object, but we do get the weak symbol. If the
2816 processor backend uses a COPY reloc, then if some routine in the
2817 dynamic object changes the real symbol, we will not see that
2818 change in the corresponding weak symbol. This is the way other
2819 ELF linkers work as well, and seems to be a result of the shared
2822 I will clarify this issue. Most SVR4 shared libraries define the
2823 variable _timezone and define timezone as a weak synonym. The
2824 tzset call changes _timezone. If you write
2825 extern int timezone;
2827 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2828 you might expect that, since timezone is a synonym for _timezone,
2829 the same number will print both times. However, if the processor
2830 backend uses a COPY reloc, then actually timezone will be copied
2831 into your process image, and, since you define _timezone
2832 yourself, _timezone will not. Thus timezone and _timezone will
2833 wind up at different memory locations. The tzset call will set
2834 _timezone, leaving timezone unchanged. */
2836 if (h
->u
.weakdef
!= NULL
)
2838 /* If we get to this point, there is an implicit reference to
2839 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2840 h
->u
.weakdef
->ref_regular
= 1;
2842 /* Ensure that the backend adjust_dynamic_symbol function sees
2843 H->U.WEAKDEF before H by recursively calling ourselves. */
2844 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2848 /* If a symbol has no type and no size and does not require a PLT
2849 entry, then we are probably about to do the wrong thing here: we
2850 are probably going to create a COPY reloc for an empty object.
2851 This case can arise when a shared object is built with assembly
2852 code, and the assembly code fails to set the symbol type. */
2854 && h
->type
== STT_NOTYPE
2857 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2858 h
->root
.root
.string
);
2860 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2861 bed
= get_elf_backend_data (dynobj
);
2863 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2872 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2876 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
2877 struct elf_link_hash_entry
*h
,
2880 unsigned int power_of_two
;
2882 asection
*sec
= h
->root
.u
.def
.section
;
2884 /* The section aligment of definition is the maximum alignment
2885 requirement of symbols defined in the section. Since we don't
2886 know the symbol alignment requirement, we start with the
2887 maximum alignment and check low bits of the symbol address
2888 for the minimum alignment. */
2889 power_of_two
= bfd_get_section_alignment (sec
->owner
, sec
);
2890 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
2891 while ((h
->root
.u
.def
.value
& mask
) != 0)
2897 if (power_of_two
> bfd_get_section_alignment (dynbss
->owner
,
2900 /* Adjust the section alignment if needed. */
2901 if (! bfd_set_section_alignment (dynbss
->owner
, dynbss
,
2906 /* We make sure that the symbol will be aligned properly. */
2907 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
2909 /* Define the symbol as being at this point in DYNBSS. */
2910 h
->root
.u
.def
.section
= dynbss
;
2911 h
->root
.u
.def
.value
= dynbss
->size
;
2913 /* Increment the size of DYNBSS to make room for the symbol. */
2914 dynbss
->size
+= h
->size
;
2916 /* No error if extern_protected_data is true. */
2917 if (h
->protected_def
2918 && (!info
->extern_protected_data
2919 || (info
->extern_protected_data
< 0
2920 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
2921 info
->callbacks
->einfo
2922 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2923 h
->root
.root
.string
);
2928 /* Adjust all external symbols pointing into SEC_MERGE sections
2929 to reflect the object merging within the sections. */
2932 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2936 if ((h
->root
.type
== bfd_link_hash_defined
2937 || h
->root
.type
== bfd_link_hash_defweak
)
2938 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2939 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
2941 bfd
*output_bfd
= (bfd
*) data
;
2943 h
->root
.u
.def
.value
=
2944 _bfd_merged_section_offset (output_bfd
,
2945 &h
->root
.u
.def
.section
,
2946 elf_section_data (sec
)->sec_info
,
2947 h
->root
.u
.def
.value
);
2953 /* Returns false if the symbol referred to by H should be considered
2954 to resolve local to the current module, and true if it should be
2955 considered to bind dynamically. */
2958 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2959 struct bfd_link_info
*info
,
2960 bfd_boolean not_local_protected
)
2962 bfd_boolean binding_stays_local_p
;
2963 const struct elf_backend_data
*bed
;
2964 struct elf_link_hash_table
*hash_table
;
2969 while (h
->root
.type
== bfd_link_hash_indirect
2970 || h
->root
.type
== bfd_link_hash_warning
)
2971 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2973 /* If it was forced local, then clearly it's not dynamic. */
2974 if (h
->dynindx
== -1)
2976 if (h
->forced_local
)
2979 /* Identify the cases where name binding rules say that a
2980 visible symbol resolves locally. */
2981 binding_stays_local_p
= (bfd_link_executable (info
)
2982 || SYMBOLIC_BIND (info
, h
));
2984 switch (ELF_ST_VISIBILITY (h
->other
))
2991 hash_table
= elf_hash_table (info
);
2992 if (!is_elf_hash_table (hash_table
))
2995 bed
= get_elf_backend_data (hash_table
->dynobj
);
2997 /* Proper resolution for function pointer equality may require
2998 that these symbols perhaps be resolved dynamically, even though
2999 we should be resolving them to the current module. */
3000 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
3001 binding_stays_local_p
= TRUE
;
3008 /* If it isn't defined locally, then clearly it's dynamic. */
3009 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
3012 /* Otherwise, the symbol is dynamic if binding rules don't tell
3013 us that it remains local. */
3014 return !binding_stays_local_p
;
3017 /* Return true if the symbol referred to by H should be considered
3018 to resolve local to the current module, and false otherwise. Differs
3019 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3020 undefined symbols. The two functions are virtually identical except
3021 for the place where forced_local and dynindx == -1 are tested. If
3022 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
3023 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
3024 the symbol is local only for defined symbols.
3025 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3026 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3027 treatment of undefined weak symbols. For those that do not make
3028 undefined weak symbols dynamic, both functions may return false. */
3031 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3032 struct bfd_link_info
*info
,
3033 bfd_boolean local_protected
)
3035 const struct elf_backend_data
*bed
;
3036 struct elf_link_hash_table
*hash_table
;
3038 /* If it's a local sym, of course we resolve locally. */
3042 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3043 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3044 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3047 /* Common symbols that become definitions don't get the DEF_REGULAR
3048 flag set, so test it first, and don't bail out. */
3049 if (ELF_COMMON_DEF_P (h
))
3051 /* If we don't have a definition in a regular file, then we can't
3052 resolve locally. The sym is either undefined or dynamic. */
3053 else if (!h
->def_regular
)
3056 /* Forced local symbols resolve locally. */
3057 if (h
->forced_local
)
3060 /* As do non-dynamic symbols. */
3061 if (h
->dynindx
== -1)
3064 /* At this point, we know the symbol is defined and dynamic. In an
3065 executable it must resolve locally, likewise when building symbolic
3066 shared libraries. */
3067 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3070 /* Now deal with defined dynamic symbols in shared libraries. Ones
3071 with default visibility might not resolve locally. */
3072 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3075 hash_table
= elf_hash_table (info
);
3076 if (!is_elf_hash_table (hash_table
))
3079 bed
= get_elf_backend_data (hash_table
->dynobj
);
3081 /* If extern_protected_data is false, STV_PROTECTED non-function
3082 symbols are local. */
3083 if ((!info
->extern_protected_data
3084 || (info
->extern_protected_data
< 0
3085 && !bed
->extern_protected_data
))
3086 && !bed
->is_function_type (h
->type
))
3089 /* Function pointer equality tests may require that STV_PROTECTED
3090 symbols be treated as dynamic symbols. If the address of a
3091 function not defined in an executable is set to that function's
3092 plt entry in the executable, then the address of the function in
3093 a shared library must also be the plt entry in the executable. */
3094 return local_protected
;
3097 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3098 aligned. Returns the first TLS output section. */
3100 struct bfd_section
*
3101 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3103 struct bfd_section
*sec
, *tls
;
3104 unsigned int align
= 0;
3106 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3107 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3111 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3112 if (sec
->alignment_power
> align
)
3113 align
= sec
->alignment_power
;
3115 elf_hash_table (info
)->tls_sec
= tls
;
3117 /* Ensure the alignment of the first section is the largest alignment,
3118 so that the tls segment starts aligned. */
3120 tls
->alignment_power
= align
;
3125 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3127 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3128 Elf_Internal_Sym
*sym
)
3130 const struct elf_backend_data
*bed
;
3132 /* Local symbols do not count, but target specific ones might. */
3133 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3134 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3137 bed
= get_elf_backend_data (abfd
);
3138 /* Function symbols do not count. */
3139 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3142 /* If the section is undefined, then so is the symbol. */
3143 if (sym
->st_shndx
== SHN_UNDEF
)
3146 /* If the symbol is defined in the common section, then
3147 it is a common definition and so does not count. */
3148 if (bed
->common_definition (sym
))
3151 /* If the symbol is in a target specific section then we
3152 must rely upon the backend to tell us what it is. */
3153 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3154 /* FIXME - this function is not coded yet:
3156 return _bfd_is_global_symbol_definition (abfd, sym);
3158 Instead for now assume that the definition is not global,
3159 Even if this is wrong, at least the linker will behave
3160 in the same way that it used to do. */
3166 /* Search the symbol table of the archive element of the archive ABFD
3167 whose archive map contains a mention of SYMDEF, and determine if
3168 the symbol is defined in this element. */
3170 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3172 Elf_Internal_Shdr
* hdr
;
3176 Elf_Internal_Sym
*isymbuf
;
3177 Elf_Internal_Sym
*isym
;
3178 Elf_Internal_Sym
*isymend
;
3181 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
3185 if (! bfd_check_format (abfd
, bfd_object
))
3188 /* Select the appropriate symbol table. If we don't know if the
3189 object file is an IR object, give linker LTO plugin a chance to
3190 get the correct symbol table. */
3191 if (abfd
->plugin_format
== bfd_plugin_yes
3192 #if BFD_SUPPORTS_PLUGINS
3193 || (abfd
->plugin_format
== bfd_plugin_unknown
3194 && bfd_link_plugin_object_p (abfd
))
3198 /* Use the IR symbol table if the object has been claimed by
3200 abfd
= abfd
->plugin_dummy_bfd
;
3201 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3203 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3204 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3206 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3208 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3210 /* The sh_info field of the symtab header tells us where the
3211 external symbols start. We don't care about the local symbols. */
3212 if (elf_bad_symtab (abfd
))
3214 extsymcount
= symcount
;
3219 extsymcount
= symcount
- hdr
->sh_info
;
3220 extsymoff
= hdr
->sh_info
;
3223 if (extsymcount
== 0)
3226 /* Read in the symbol table. */
3227 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3229 if (isymbuf
== NULL
)
3232 /* Scan the symbol table looking for SYMDEF. */
3234 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3238 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3243 if (strcmp (name
, symdef
->name
) == 0)
3245 result
= is_global_data_symbol_definition (abfd
, isym
);
3255 /* Add an entry to the .dynamic table. */
3258 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3262 struct elf_link_hash_table
*hash_table
;
3263 const struct elf_backend_data
*bed
;
3265 bfd_size_type newsize
;
3266 bfd_byte
*newcontents
;
3267 Elf_Internal_Dyn dyn
;
3269 hash_table
= elf_hash_table (info
);
3270 if (! is_elf_hash_table (hash_table
))
3273 bed
= get_elf_backend_data (hash_table
->dynobj
);
3274 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3275 BFD_ASSERT (s
!= NULL
);
3277 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3278 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3279 if (newcontents
== NULL
)
3283 dyn
.d_un
.d_val
= val
;
3284 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3287 s
->contents
= newcontents
;
3292 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3293 otherwise just check whether one already exists. Returns -1 on error,
3294 1 if a DT_NEEDED tag already exists, and 0 on success. */
3297 elf_add_dt_needed_tag (bfd
*abfd
,
3298 struct bfd_link_info
*info
,
3302 struct elf_link_hash_table
*hash_table
;
3305 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3308 hash_table
= elf_hash_table (info
);
3309 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3310 if (strindex
== (size_t) -1)
3313 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3316 const struct elf_backend_data
*bed
;
3319 bed
= get_elf_backend_data (hash_table
->dynobj
);
3320 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3322 for (extdyn
= sdyn
->contents
;
3323 extdyn
< sdyn
->contents
+ sdyn
->size
;
3324 extdyn
+= bed
->s
->sizeof_dyn
)
3326 Elf_Internal_Dyn dyn
;
3328 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3329 if (dyn
.d_tag
== DT_NEEDED
3330 && dyn
.d_un
.d_val
== strindex
)
3332 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3340 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3343 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3347 /* We were just checking for existence of the tag. */
3348 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3353 /* Return true if SONAME is on the needed list between NEEDED and STOP
3354 (or the end of list if STOP is NULL), and needed by a library that
3358 on_needed_list (const char *soname
,
3359 struct bfd_link_needed_list
*needed
,
3360 struct bfd_link_needed_list
*stop
)
3362 struct bfd_link_needed_list
*look
;
3363 for (look
= needed
; look
!= stop
; look
= look
->next
)
3364 if (strcmp (soname
, look
->name
) == 0
3365 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3366 /* If needed by a library that itself is not directly
3367 needed, recursively check whether that library is
3368 indirectly needed. Since we add DT_NEEDED entries to
3369 the end of the list, library dependencies appear after
3370 the library. Therefore search prior to the current
3371 LOOK, preventing possible infinite recursion. */
3372 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3378 /* Sort symbol by value, section, and size. */
3380 elf_sort_symbol (const void *arg1
, const void *arg2
)
3382 const struct elf_link_hash_entry
*h1
;
3383 const struct elf_link_hash_entry
*h2
;
3384 bfd_signed_vma vdiff
;
3386 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3387 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3388 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3390 return vdiff
> 0 ? 1 : -1;
3393 int sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3395 return sdiff
> 0 ? 1 : -1;
3397 vdiff
= h1
->size
- h2
->size
;
3398 return vdiff
== 0 ? 0 : vdiff
> 0 ? 1 : -1;
3401 /* This function is used to adjust offsets into .dynstr for
3402 dynamic symbols. This is called via elf_link_hash_traverse. */
3405 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3407 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3409 if (h
->dynindx
!= -1)
3410 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3414 /* Assign string offsets in .dynstr, update all structures referencing
3418 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3420 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3421 struct elf_link_local_dynamic_entry
*entry
;
3422 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3423 bfd
*dynobj
= hash_table
->dynobj
;
3426 const struct elf_backend_data
*bed
;
3429 _bfd_elf_strtab_finalize (dynstr
);
3430 size
= _bfd_elf_strtab_size (dynstr
);
3432 bed
= get_elf_backend_data (dynobj
);
3433 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3434 BFD_ASSERT (sdyn
!= NULL
);
3436 /* Update all .dynamic entries referencing .dynstr strings. */
3437 for (extdyn
= sdyn
->contents
;
3438 extdyn
< sdyn
->contents
+ sdyn
->size
;
3439 extdyn
+= bed
->s
->sizeof_dyn
)
3441 Elf_Internal_Dyn dyn
;
3443 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3447 dyn
.d_un
.d_val
= size
;
3457 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3462 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3465 /* Now update local dynamic symbols. */
3466 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3467 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3468 entry
->isym
.st_name
);
3470 /* And the rest of dynamic symbols. */
3471 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3473 /* Adjust version definitions. */
3474 if (elf_tdata (output_bfd
)->cverdefs
)
3479 Elf_Internal_Verdef def
;
3480 Elf_Internal_Verdaux defaux
;
3482 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3486 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3488 p
+= sizeof (Elf_External_Verdef
);
3489 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3491 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3493 _bfd_elf_swap_verdaux_in (output_bfd
,
3494 (Elf_External_Verdaux
*) p
, &defaux
);
3495 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3497 _bfd_elf_swap_verdaux_out (output_bfd
,
3498 &defaux
, (Elf_External_Verdaux
*) p
);
3499 p
+= sizeof (Elf_External_Verdaux
);
3502 while (def
.vd_next
);
3505 /* Adjust version references. */
3506 if (elf_tdata (output_bfd
)->verref
)
3511 Elf_Internal_Verneed need
;
3512 Elf_Internal_Vernaux needaux
;
3514 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
3518 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3520 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3521 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3522 (Elf_External_Verneed
*) p
);
3523 p
+= sizeof (Elf_External_Verneed
);
3524 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3526 _bfd_elf_swap_vernaux_in (output_bfd
,
3527 (Elf_External_Vernaux
*) p
, &needaux
);
3528 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3530 _bfd_elf_swap_vernaux_out (output_bfd
,
3532 (Elf_External_Vernaux
*) p
);
3533 p
+= sizeof (Elf_External_Vernaux
);
3536 while (need
.vn_next
);
3542 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3543 The default is to only match when the INPUT and OUTPUT are exactly
3547 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3548 const bfd_target
*output
)
3550 return input
== output
;
3553 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3554 This version is used when different targets for the same architecture
3555 are virtually identical. */
3558 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3559 const bfd_target
*output
)
3561 const struct elf_backend_data
*obed
, *ibed
;
3563 if (input
== output
)
3566 ibed
= xvec_get_elf_backend_data (input
);
3567 obed
= xvec_get_elf_backend_data (output
);
3569 if (ibed
->arch
!= obed
->arch
)
3572 /* If both backends are using this function, deem them compatible. */
3573 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3576 /* Make a special call to the linker "notice" function to tell it that
3577 we are about to handle an as-needed lib, or have finished
3578 processing the lib. */
3581 _bfd_elf_notice_as_needed (bfd
*ibfd
,
3582 struct bfd_link_info
*info
,
3583 enum notice_asneeded_action act
)
3585 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
3588 /* Check relocations an ELF object file. */
3591 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
3593 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3594 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
3596 /* If this object is the same format as the output object, and it is
3597 not a shared library, then let the backend look through the
3600 This is required to build global offset table entries and to
3601 arrange for dynamic relocs. It is not required for the
3602 particular common case of linking non PIC code, even when linking
3603 against shared libraries, but unfortunately there is no way of
3604 knowing whether an object file has been compiled PIC or not.
3605 Looking through the relocs is not particularly time consuming.
3606 The problem is that we must either (1) keep the relocs in memory,
3607 which causes the linker to require additional runtime memory or
3608 (2) read the relocs twice from the input file, which wastes time.
3609 This would be a good case for using mmap.
3611 I have no idea how to handle linking PIC code into a file of a
3612 different format. It probably can't be done. */
3613 if ((abfd
->flags
& DYNAMIC
) == 0
3614 && is_elf_hash_table (htab
)
3615 && bed
->check_relocs
!= NULL
3616 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
3617 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
3621 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3623 Elf_Internal_Rela
*internal_relocs
;
3626 /* Don't check relocations in excluded sections. */
3627 if ((o
->flags
& SEC_RELOC
) == 0
3628 || (o
->flags
& SEC_EXCLUDE
) != 0
3629 || o
->reloc_count
== 0
3630 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
3631 && (o
->flags
& SEC_DEBUGGING
) != 0)
3632 || bfd_is_abs_section (o
->output_section
))
3635 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
3637 if (internal_relocs
== NULL
)
3640 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
3642 if (elf_section_data (o
)->relocs
!= internal_relocs
)
3643 free (internal_relocs
);
3653 /* Add symbols from an ELF object file to the linker hash table. */
3656 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3658 Elf_Internal_Ehdr
*ehdr
;
3659 Elf_Internal_Shdr
*hdr
;
3663 struct elf_link_hash_entry
**sym_hash
;
3664 bfd_boolean dynamic
;
3665 Elf_External_Versym
*extversym
= NULL
;
3666 Elf_External_Versym
*ever
;
3667 struct elf_link_hash_entry
*weaks
;
3668 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3669 size_t nondeflt_vers_cnt
= 0;
3670 Elf_Internal_Sym
*isymbuf
= NULL
;
3671 Elf_Internal_Sym
*isym
;
3672 Elf_Internal_Sym
*isymend
;
3673 const struct elf_backend_data
*bed
;
3674 bfd_boolean add_needed
;
3675 struct elf_link_hash_table
*htab
;
3677 void *alloc_mark
= NULL
;
3678 struct bfd_hash_entry
**old_table
= NULL
;
3679 unsigned int old_size
= 0;
3680 unsigned int old_count
= 0;
3681 void *old_tab
= NULL
;
3683 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3684 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3685 void *old_strtab
= NULL
;
3688 bfd_boolean just_syms
;
3690 htab
= elf_hash_table (info
);
3691 bed
= get_elf_backend_data (abfd
);
3693 if ((abfd
->flags
& DYNAMIC
) == 0)
3699 /* You can't use -r against a dynamic object. Also, there's no
3700 hope of using a dynamic object which does not exactly match
3701 the format of the output file. */
3702 if (bfd_link_relocatable (info
)
3703 || !is_elf_hash_table (htab
)
3704 || info
->output_bfd
->xvec
!= abfd
->xvec
)
3706 if (bfd_link_relocatable (info
))
3707 bfd_set_error (bfd_error_invalid_operation
);
3709 bfd_set_error (bfd_error_wrong_format
);
3714 ehdr
= elf_elfheader (abfd
);
3715 if (info
->warn_alternate_em
3716 && bed
->elf_machine_code
!= ehdr
->e_machine
3717 && ((bed
->elf_machine_alt1
!= 0
3718 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
3719 || (bed
->elf_machine_alt2
!= 0
3720 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
3721 info
->callbacks
->einfo
3722 /* xgettext:c-format */
3723 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3724 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
3726 /* As a GNU extension, any input sections which are named
3727 .gnu.warning.SYMBOL are treated as warning symbols for the given
3728 symbol. This differs from .gnu.warning sections, which generate
3729 warnings when they are included in an output file. */
3730 /* PR 12761: Also generate this warning when building shared libraries. */
3731 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3735 name
= bfd_get_section_name (abfd
, s
);
3736 if (CONST_STRNEQ (name
, ".gnu.warning."))
3741 name
+= sizeof ".gnu.warning." - 1;
3743 /* If this is a shared object, then look up the symbol
3744 in the hash table. If it is there, and it is already
3745 been defined, then we will not be using the entry
3746 from this shared object, so we don't need to warn.
3747 FIXME: If we see the definition in a regular object
3748 later on, we will warn, but we shouldn't. The only
3749 fix is to keep track of what warnings we are supposed
3750 to emit, and then handle them all at the end of the
3754 struct elf_link_hash_entry
*h
;
3756 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3758 /* FIXME: What about bfd_link_hash_common? */
3760 && (h
->root
.type
== bfd_link_hash_defined
3761 || h
->root
.type
== bfd_link_hash_defweak
))
3766 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
3770 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3775 if (! (_bfd_generic_link_add_one_symbol
3776 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3777 FALSE
, bed
->collect
, NULL
)))
3780 if (bfd_link_executable (info
))
3782 /* Clobber the section size so that the warning does
3783 not get copied into the output file. */
3786 /* Also set SEC_EXCLUDE, so that symbols defined in
3787 the warning section don't get copied to the output. */
3788 s
->flags
|= SEC_EXCLUDE
;
3793 just_syms
= ((s
= abfd
->sections
) != NULL
3794 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
3799 /* If we are creating a shared library, create all the dynamic
3800 sections immediately. We need to attach them to something,
3801 so we attach them to this BFD, provided it is the right
3802 format and is not from ld --just-symbols. Always create the
3803 dynamic sections for -E/--dynamic-list. FIXME: If there
3804 are no input BFD's of the same format as the output, we can't
3805 make a shared library. */
3807 && (bfd_link_pic (info
)
3808 || (!bfd_link_relocatable (info
)
3809 && (info
->export_dynamic
|| info
->dynamic
)))
3810 && is_elf_hash_table (htab
)
3811 && info
->output_bfd
->xvec
== abfd
->xvec
3812 && !htab
->dynamic_sections_created
)
3814 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3818 else if (!is_elf_hash_table (htab
))
3822 const char *soname
= NULL
;
3824 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3825 const Elf_Internal_Phdr
*phdr
;
3828 /* ld --just-symbols and dynamic objects don't mix very well.
3829 ld shouldn't allow it. */
3833 /* If this dynamic lib was specified on the command line with
3834 --as-needed in effect, then we don't want to add a DT_NEEDED
3835 tag unless the lib is actually used. Similary for libs brought
3836 in by another lib's DT_NEEDED. When --no-add-needed is used
3837 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3838 any dynamic library in DT_NEEDED tags in the dynamic lib at
3840 add_needed
= (elf_dyn_lib_class (abfd
)
3841 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3842 | DYN_NO_NEEDED
)) == 0;
3844 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3849 unsigned int elfsec
;
3850 unsigned long shlink
;
3852 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3859 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3860 if (elfsec
== SHN_BAD
)
3861 goto error_free_dyn
;
3862 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3864 for (extdyn
= dynbuf
;
3865 extdyn
< dynbuf
+ s
->size
;
3866 extdyn
+= bed
->s
->sizeof_dyn
)
3868 Elf_Internal_Dyn dyn
;
3870 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3871 if (dyn
.d_tag
== DT_SONAME
)
3873 unsigned int tagv
= dyn
.d_un
.d_val
;
3874 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3876 goto error_free_dyn
;
3878 if (dyn
.d_tag
== DT_NEEDED
)
3880 struct bfd_link_needed_list
*n
, **pn
;
3882 unsigned int tagv
= dyn
.d_un
.d_val
;
3884 amt
= sizeof (struct bfd_link_needed_list
);
3885 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3886 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3887 if (n
== NULL
|| fnm
== NULL
)
3888 goto error_free_dyn
;
3889 amt
= strlen (fnm
) + 1;
3890 anm
= (char *) bfd_alloc (abfd
, amt
);
3892 goto error_free_dyn
;
3893 memcpy (anm
, fnm
, amt
);
3897 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3901 if (dyn
.d_tag
== DT_RUNPATH
)
3903 struct bfd_link_needed_list
*n
, **pn
;
3905 unsigned int tagv
= dyn
.d_un
.d_val
;
3907 amt
= sizeof (struct bfd_link_needed_list
);
3908 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3909 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3910 if (n
== NULL
|| fnm
== NULL
)
3911 goto error_free_dyn
;
3912 amt
= strlen (fnm
) + 1;
3913 anm
= (char *) bfd_alloc (abfd
, amt
);
3915 goto error_free_dyn
;
3916 memcpy (anm
, fnm
, amt
);
3920 for (pn
= & runpath
;
3926 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3927 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3929 struct bfd_link_needed_list
*n
, **pn
;
3931 unsigned int tagv
= dyn
.d_un
.d_val
;
3933 amt
= sizeof (struct bfd_link_needed_list
);
3934 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3935 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3936 if (n
== NULL
|| fnm
== NULL
)
3937 goto error_free_dyn
;
3938 amt
= strlen (fnm
) + 1;
3939 anm
= (char *) bfd_alloc (abfd
, amt
);
3941 goto error_free_dyn
;
3942 memcpy (anm
, fnm
, amt
);
3952 if (dyn
.d_tag
== DT_AUDIT
)
3954 unsigned int tagv
= dyn
.d_un
.d_val
;
3955 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3962 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3963 frees all more recently bfd_alloc'd blocks as well. */
3969 struct bfd_link_needed_list
**pn
;
3970 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3975 /* If we have a PT_GNU_RELRO program header, mark as read-only
3976 all sections contained fully therein. This makes relro
3977 shared library sections appear as they will at run-time. */
3978 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
3979 while (--phdr
>= elf_tdata (abfd
)->phdr
)
3980 if (phdr
->p_type
== PT_GNU_RELRO
)
3982 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3983 if ((s
->flags
& SEC_ALLOC
) != 0
3984 && s
->vma
>= phdr
->p_vaddr
3985 && s
->vma
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
3986 s
->flags
|= SEC_READONLY
;
3990 /* We do not want to include any of the sections in a dynamic
3991 object in the output file. We hack by simply clobbering the
3992 list of sections in the BFD. This could be handled more
3993 cleanly by, say, a new section flag; the existing
3994 SEC_NEVER_LOAD flag is not the one we want, because that one
3995 still implies that the section takes up space in the output
3997 bfd_section_list_clear (abfd
);
3999 /* Find the name to use in a DT_NEEDED entry that refers to this
4000 object. If the object has a DT_SONAME entry, we use it.
4001 Otherwise, if the generic linker stuck something in
4002 elf_dt_name, we use that. Otherwise, we just use the file
4004 if (soname
== NULL
|| *soname
== '\0')
4006 soname
= elf_dt_name (abfd
);
4007 if (soname
== NULL
|| *soname
== '\0')
4008 soname
= bfd_get_filename (abfd
);
4011 /* Save the SONAME because sometimes the linker emulation code
4012 will need to know it. */
4013 elf_dt_name (abfd
) = soname
;
4015 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4019 /* If we have already included this dynamic object in the
4020 link, just ignore it. There is no reason to include a
4021 particular dynamic object more than once. */
4025 /* Save the DT_AUDIT entry for the linker emulation code. */
4026 elf_dt_audit (abfd
) = audit
;
4029 /* If this is a dynamic object, we always link against the .dynsym
4030 symbol table, not the .symtab symbol table. The dynamic linker
4031 will only see the .dynsym symbol table, so there is no reason to
4032 look at .symtab for a dynamic object. */
4034 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4035 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4037 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4039 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4041 /* The sh_info field of the symtab header tells us where the
4042 external symbols start. We don't care about the local symbols at
4044 if (elf_bad_symtab (abfd
))
4046 extsymcount
= symcount
;
4051 extsymcount
= symcount
- hdr
->sh_info
;
4052 extsymoff
= hdr
->sh_info
;
4055 sym_hash
= elf_sym_hashes (abfd
);
4056 if (extsymcount
!= 0)
4058 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4060 if (isymbuf
== NULL
)
4063 if (sym_hash
== NULL
)
4065 /* We store a pointer to the hash table entry for each
4068 amt
*= sizeof (struct elf_link_hash_entry
*);
4069 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4070 if (sym_hash
== NULL
)
4071 goto error_free_sym
;
4072 elf_sym_hashes (abfd
) = sym_hash
;
4078 /* Read in any version definitions. */
4079 if (!_bfd_elf_slurp_version_tables (abfd
,
4080 info
->default_imported_symver
))
4081 goto error_free_sym
;
4083 /* Read in the symbol versions, but don't bother to convert them
4084 to internal format. */
4085 if (elf_dynversym (abfd
) != 0)
4087 Elf_Internal_Shdr
*versymhdr
;
4089 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4090 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
4091 if (extversym
== NULL
)
4092 goto error_free_sym
;
4093 amt
= versymhdr
->sh_size
;
4094 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
4095 || bfd_bread (extversym
, amt
, abfd
) != amt
)
4096 goto error_free_vers
;
4100 /* If we are loading an as-needed shared lib, save the symbol table
4101 state before we start adding symbols. If the lib turns out
4102 to be unneeded, restore the state. */
4103 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4108 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4110 struct bfd_hash_entry
*p
;
4111 struct elf_link_hash_entry
*h
;
4113 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4115 h
= (struct elf_link_hash_entry
*) p
;
4116 entsize
+= htab
->root
.table
.entsize
;
4117 if (h
->root
.type
== bfd_link_hash_warning
)
4118 entsize
+= htab
->root
.table
.entsize
;
4122 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4123 old_tab
= bfd_malloc (tabsize
+ entsize
);
4124 if (old_tab
== NULL
)
4125 goto error_free_vers
;
4127 /* Remember the current objalloc pointer, so that all mem for
4128 symbols added can later be reclaimed. */
4129 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4130 if (alloc_mark
== NULL
)
4131 goto error_free_vers
;
4133 /* Make a special call to the linker "notice" function to
4134 tell it that we are about to handle an as-needed lib. */
4135 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4136 goto error_free_vers
;
4138 /* Clone the symbol table. Remember some pointers into the
4139 symbol table, and dynamic symbol count. */
4140 old_ent
= (char *) old_tab
+ tabsize
;
4141 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4142 old_undefs
= htab
->root
.undefs
;
4143 old_undefs_tail
= htab
->root
.undefs_tail
;
4144 old_table
= htab
->root
.table
.table
;
4145 old_size
= htab
->root
.table
.size
;
4146 old_count
= htab
->root
.table
.count
;
4147 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4148 if (old_strtab
== NULL
)
4149 goto error_free_vers
;
4151 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4153 struct bfd_hash_entry
*p
;
4154 struct elf_link_hash_entry
*h
;
4156 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4158 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
4159 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4160 h
= (struct elf_link_hash_entry
*) p
;
4161 if (h
->root
.type
== bfd_link_hash_warning
)
4163 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
4164 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4171 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
4172 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
4174 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4178 asection
*sec
, *new_sec
;
4181 struct elf_link_hash_entry
*h
;
4182 struct elf_link_hash_entry
*hi
;
4183 bfd_boolean definition
;
4184 bfd_boolean size_change_ok
;
4185 bfd_boolean type_change_ok
;
4186 bfd_boolean new_weakdef
;
4187 bfd_boolean new_weak
;
4188 bfd_boolean old_weak
;
4189 bfd_boolean override
;
4191 bfd_boolean discarded
;
4192 unsigned int old_alignment
;
4194 bfd_boolean matched
;
4198 flags
= BSF_NO_FLAGS
;
4200 value
= isym
->st_value
;
4201 common
= bed
->common_definition (isym
);
4204 bind
= ELF_ST_BIND (isym
->st_info
);
4208 /* This should be impossible, since ELF requires that all
4209 global symbols follow all local symbols, and that sh_info
4210 point to the first global symbol. Unfortunately, Irix 5
4215 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4223 case STB_GNU_UNIQUE
:
4224 flags
= BSF_GNU_UNIQUE
;
4228 /* Leave it up to the processor backend. */
4232 if (isym
->st_shndx
== SHN_UNDEF
)
4233 sec
= bfd_und_section_ptr
;
4234 else if (isym
->st_shndx
== SHN_ABS
)
4235 sec
= bfd_abs_section_ptr
;
4236 else if (isym
->st_shndx
== SHN_COMMON
)
4238 sec
= bfd_com_section_ptr
;
4239 /* What ELF calls the size we call the value. What ELF
4240 calls the value we call the alignment. */
4241 value
= isym
->st_size
;
4245 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4247 sec
= bfd_abs_section_ptr
;
4248 else if (discarded_section (sec
))
4250 /* Symbols from discarded section are undefined. We keep
4252 sec
= bfd_und_section_ptr
;
4254 isym
->st_shndx
= SHN_UNDEF
;
4256 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4260 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4263 goto error_free_vers
;
4265 if (isym
->st_shndx
== SHN_COMMON
4266 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4268 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4272 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4274 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4276 goto error_free_vers
;
4280 else if (isym
->st_shndx
== SHN_COMMON
4281 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4282 && !bfd_link_relocatable (info
))
4284 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4288 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4289 | SEC_LINKER_CREATED
);
4290 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4292 goto error_free_vers
;
4296 else if (bed
->elf_add_symbol_hook
)
4298 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4300 goto error_free_vers
;
4302 /* The hook function sets the name to NULL if this symbol
4303 should be skipped for some reason. */
4308 /* Sanity check that all possibilities were handled. */
4311 bfd_set_error (bfd_error_bad_value
);
4312 goto error_free_vers
;
4315 /* Silently discard TLS symbols from --just-syms. There's
4316 no way to combine a static TLS block with a new TLS block
4317 for this executable. */
4318 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4319 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4322 if (bfd_is_und_section (sec
)
4323 || bfd_is_com_section (sec
))
4328 size_change_ok
= FALSE
;
4329 type_change_ok
= bed
->type_change_ok
;
4336 if (is_elf_hash_table (htab
))
4338 Elf_Internal_Versym iver
;
4339 unsigned int vernum
= 0;
4344 if (info
->default_imported_symver
)
4345 /* Use the default symbol version created earlier. */
4346 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4351 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4353 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4355 /* If this is a hidden symbol, or if it is not version
4356 1, we append the version name to the symbol name.
4357 However, we do not modify a non-hidden absolute symbol
4358 if it is not a function, because it might be the version
4359 symbol itself. FIXME: What if it isn't? */
4360 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4362 && (!bfd_is_abs_section (sec
)
4363 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4366 size_t namelen
, verlen
, newlen
;
4369 if (isym
->st_shndx
!= SHN_UNDEF
)
4371 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4373 else if (vernum
> 1)
4375 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4382 /* xgettext:c-format */
4383 (_("%B: %s: invalid version %u (max %d)"),
4385 elf_tdata (abfd
)->cverdefs
);
4386 bfd_set_error (bfd_error_bad_value
);
4387 goto error_free_vers
;
4392 /* We cannot simply test for the number of
4393 entries in the VERNEED section since the
4394 numbers for the needed versions do not start
4396 Elf_Internal_Verneed
*t
;
4399 for (t
= elf_tdata (abfd
)->verref
;
4403 Elf_Internal_Vernaux
*a
;
4405 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4407 if (a
->vna_other
== vernum
)
4409 verstr
= a
->vna_nodename
;
4419 /* xgettext:c-format */
4420 (_("%B: %s: invalid needed version %d"),
4421 abfd
, name
, vernum
);
4422 bfd_set_error (bfd_error_bad_value
);
4423 goto error_free_vers
;
4427 namelen
= strlen (name
);
4428 verlen
= strlen (verstr
);
4429 newlen
= namelen
+ verlen
+ 2;
4430 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4431 && isym
->st_shndx
!= SHN_UNDEF
)
4434 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4435 if (newname
== NULL
)
4436 goto error_free_vers
;
4437 memcpy (newname
, name
, namelen
);
4438 p
= newname
+ namelen
;
4440 /* If this is a defined non-hidden version symbol,
4441 we add another @ to the name. This indicates the
4442 default version of the symbol. */
4443 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4444 && isym
->st_shndx
!= SHN_UNDEF
)
4446 memcpy (p
, verstr
, verlen
+ 1);
4451 /* If this symbol has default visibility and the user has
4452 requested we not re-export it, then mark it as hidden. */
4453 if (!bfd_is_und_section (sec
)
4456 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4457 isym
->st_other
= (STV_HIDDEN
4458 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4460 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4461 sym_hash
, &old_bfd
, &old_weak
,
4462 &old_alignment
, &skip
, &override
,
4463 &type_change_ok
, &size_change_ok
,
4465 goto error_free_vers
;
4470 /* Override a definition only if the new symbol matches the
4472 if (override
&& matched
)
4476 while (h
->root
.type
== bfd_link_hash_indirect
4477 || h
->root
.type
== bfd_link_hash_warning
)
4478 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4480 if (elf_tdata (abfd
)->verdef
!= NULL
4483 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4486 if (! (_bfd_generic_link_add_one_symbol
4487 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4488 (struct bfd_link_hash_entry
**) sym_hash
)))
4489 goto error_free_vers
;
4491 if ((flags
& BSF_GNU_UNIQUE
)
4492 && (abfd
->flags
& DYNAMIC
) == 0
4493 && bfd_get_flavour (info
->output_bfd
) == bfd_target_elf_flavour
)
4494 elf_tdata (info
->output_bfd
)->has_gnu_symbols
|= elf_gnu_symbol_unique
;
4497 /* We need to make sure that indirect symbol dynamic flags are
4500 while (h
->root
.type
== bfd_link_hash_indirect
4501 || h
->root
.type
== bfd_link_hash_warning
)
4502 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4504 /* Setting the index to -3 tells elf_link_output_extsym that
4505 this symbol is defined in a discarded section. */
4511 new_weak
= (flags
& BSF_WEAK
) != 0;
4512 new_weakdef
= FALSE
;
4516 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4517 && is_elf_hash_table (htab
)
4518 && h
->u
.weakdef
== NULL
)
4520 /* Keep a list of all weak defined non function symbols from
4521 a dynamic object, using the weakdef field. Later in this
4522 function we will set the weakdef field to the correct
4523 value. We only put non-function symbols from dynamic
4524 objects on this list, because that happens to be the only
4525 time we need to know the normal symbol corresponding to a
4526 weak symbol, and the information is time consuming to
4527 figure out. If the weakdef field is not already NULL,
4528 then this symbol was already defined by some previous
4529 dynamic object, and we will be using that previous
4530 definition anyhow. */
4532 h
->u
.weakdef
= weaks
;
4537 /* Set the alignment of a common symbol. */
4538 if ((common
|| bfd_is_com_section (sec
))
4539 && h
->root
.type
== bfd_link_hash_common
)
4544 align
= bfd_log2 (isym
->st_value
);
4547 /* The new symbol is a common symbol in a shared object.
4548 We need to get the alignment from the section. */
4549 align
= new_sec
->alignment_power
;
4551 if (align
> old_alignment
)
4552 h
->root
.u
.c
.p
->alignment_power
= align
;
4554 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4557 if (is_elf_hash_table (htab
))
4559 /* Set a flag in the hash table entry indicating the type of
4560 reference or definition we just found. A dynamic symbol
4561 is one which is referenced or defined by both a regular
4562 object and a shared object. */
4563 bfd_boolean dynsym
= FALSE
;
4565 /* Plugin symbols aren't normal. Don't set def_regular or
4566 ref_regular for them, or make them dynamic. */
4567 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4574 if (bind
!= STB_WEAK
)
4575 h
->ref_regular_nonweak
= 1;
4587 /* If the indirect symbol has been forced local, don't
4588 make the real symbol dynamic. */
4589 if ((h
== hi
|| !hi
->forced_local
)
4590 && (bfd_link_dll (info
)
4600 hi
->ref_dynamic
= 1;
4605 hi
->def_dynamic
= 1;
4608 /* If the indirect symbol has been forced local, don't
4609 make the real symbol dynamic. */
4610 if ((h
== hi
|| !hi
->forced_local
)
4613 || (h
->u
.weakdef
!= NULL
4615 && h
->u
.weakdef
->dynindx
!= -1)))
4619 /* Check to see if we need to add an indirect symbol for
4620 the default name. */
4622 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
4623 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4624 sec
, value
, &old_bfd
, &dynsym
))
4625 goto error_free_vers
;
4627 /* Check the alignment when a common symbol is involved. This
4628 can change when a common symbol is overridden by a normal
4629 definition or a common symbol is ignored due to the old
4630 normal definition. We need to make sure the maximum
4631 alignment is maintained. */
4632 if ((old_alignment
|| common
)
4633 && h
->root
.type
!= bfd_link_hash_common
)
4635 unsigned int common_align
;
4636 unsigned int normal_align
;
4637 unsigned int symbol_align
;
4641 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
4642 || h
->root
.type
== bfd_link_hash_defweak
);
4644 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4645 if (h
->root
.u
.def
.section
->owner
!= NULL
4646 && (h
->root
.u
.def
.section
->owner
->flags
4647 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
4649 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4650 if (normal_align
> symbol_align
)
4651 normal_align
= symbol_align
;
4654 normal_align
= symbol_align
;
4658 common_align
= old_alignment
;
4659 common_bfd
= old_bfd
;
4664 common_align
= bfd_log2 (isym
->st_value
);
4666 normal_bfd
= old_bfd
;
4669 if (normal_align
< common_align
)
4671 /* PR binutils/2735 */
4672 if (normal_bfd
== NULL
)
4674 /* xgettext:c-format */
4675 (_("Warning: alignment %u of common symbol `%s' in %B is"
4676 " greater than the alignment (%u) of its section %A"),
4677 common_bfd
, h
->root
.u
.def
.section
,
4678 1 << common_align
, name
, 1 << normal_align
);
4681 /* xgettext:c-format */
4682 (_("Warning: alignment %u of symbol `%s' in %B"
4683 " is smaller than %u in %B"),
4684 normal_bfd
, common_bfd
,
4685 1 << normal_align
, name
, 1 << common_align
);
4689 /* Remember the symbol size if it isn't undefined. */
4690 if (isym
->st_size
!= 0
4691 && isym
->st_shndx
!= SHN_UNDEF
4692 && (definition
|| h
->size
== 0))
4695 && h
->size
!= isym
->st_size
4696 && ! size_change_ok
)
4698 /* xgettext:c-format */
4699 (_("Warning: size of symbol `%s' changed"
4700 " from %lu in %B to %lu in %B"),
4702 name
, (unsigned long) h
->size
,
4703 (unsigned long) isym
->st_size
);
4705 h
->size
= isym
->st_size
;
4708 /* If this is a common symbol, then we always want H->SIZE
4709 to be the size of the common symbol. The code just above
4710 won't fix the size if a common symbol becomes larger. We
4711 don't warn about a size change here, because that is
4712 covered by --warn-common. Allow changes between different
4714 if (h
->root
.type
== bfd_link_hash_common
)
4715 h
->size
= h
->root
.u
.c
.size
;
4717 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4718 && ((definition
&& !new_weak
)
4719 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
4720 || h
->type
== STT_NOTYPE
))
4722 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
4724 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4726 if (type
== STT_GNU_IFUNC
4727 && (abfd
->flags
& DYNAMIC
) != 0)
4730 if (h
->type
!= type
)
4732 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
4733 /* xgettext:c-format */
4735 (_("Warning: type of symbol `%s' changed"
4736 " from %d to %d in %B"),
4737 abfd
, name
, h
->type
, type
);
4743 /* Merge st_other field. */
4744 elf_merge_st_other (abfd
, h
, isym
, sec
, definition
, dynamic
);
4746 /* We don't want to make debug symbol dynamic. */
4748 && (sec
->flags
& SEC_DEBUGGING
)
4749 && !bfd_link_relocatable (info
))
4752 /* Nor should we make plugin symbols dynamic. */
4753 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4758 h
->target_internal
= isym
->st_target_internal
;
4759 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
4762 if (definition
&& !dynamic
)
4764 char *p
= strchr (name
, ELF_VER_CHR
);
4765 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4767 /* Queue non-default versions so that .symver x, x@FOO
4768 aliases can be checked. */
4771 amt
= ((isymend
- isym
+ 1)
4772 * sizeof (struct elf_link_hash_entry
*));
4774 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
4776 goto error_free_vers
;
4778 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4782 if (dynsym
&& h
->dynindx
== -1)
4784 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4785 goto error_free_vers
;
4786 if (h
->u
.weakdef
!= NULL
4788 && h
->u
.weakdef
->dynindx
== -1)
4790 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4791 goto error_free_vers
;
4794 else if (h
->dynindx
!= -1)
4795 /* If the symbol already has a dynamic index, but
4796 visibility says it should not be visible, turn it into
4798 switch (ELF_ST_VISIBILITY (h
->other
))
4802 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4807 /* Don't add DT_NEEDED for references from the dummy bfd nor
4808 for unmatched symbol. */
4813 && h
->ref_regular_nonweak
4815 || (old_bfd
->flags
& BFD_PLUGIN
) == 0))
4816 || (h
->ref_dynamic_nonweak
4817 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
4818 && !on_needed_list (elf_dt_name (abfd
),
4819 htab
->needed
, NULL
))))
4822 const char *soname
= elf_dt_name (abfd
);
4824 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
4825 h
->root
.root
.string
);
4827 /* A symbol from a library loaded via DT_NEEDED of some
4828 other library is referenced by a regular object.
4829 Add a DT_NEEDED entry for it. Issue an error if
4830 --no-add-needed is used and the reference was not
4833 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4836 /* xgettext:c-format */
4837 (_("%B: undefined reference to symbol '%s'"),
4839 bfd_set_error (bfd_error_missing_dso
);
4840 goto error_free_vers
;
4843 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
4844 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
4847 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4849 goto error_free_vers
;
4851 BFD_ASSERT (ret
== 0);
4856 if (extversym
!= NULL
)
4862 if (isymbuf
!= NULL
)
4868 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4872 /* Restore the symbol table. */
4873 old_ent
= (char *) old_tab
+ tabsize
;
4874 memset (elf_sym_hashes (abfd
), 0,
4875 extsymcount
* sizeof (struct elf_link_hash_entry
*));
4876 htab
->root
.table
.table
= old_table
;
4877 htab
->root
.table
.size
= old_size
;
4878 htab
->root
.table
.count
= old_count
;
4879 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4880 htab
->root
.undefs
= old_undefs
;
4881 htab
->root
.undefs_tail
= old_undefs_tail
;
4882 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
4885 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4887 struct bfd_hash_entry
*p
;
4888 struct elf_link_hash_entry
*h
;
4890 unsigned int alignment_power
;
4892 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4894 h
= (struct elf_link_hash_entry
*) p
;
4895 if (h
->root
.type
== bfd_link_hash_warning
)
4896 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4898 /* Preserve the maximum alignment and size for common
4899 symbols even if this dynamic lib isn't on DT_NEEDED
4900 since it can still be loaded at run time by another
4902 if (h
->root
.type
== bfd_link_hash_common
)
4904 size
= h
->root
.u
.c
.size
;
4905 alignment_power
= h
->root
.u
.c
.p
->alignment_power
;
4910 alignment_power
= 0;
4912 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4913 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4914 h
= (struct elf_link_hash_entry
*) p
;
4915 if (h
->root
.type
== bfd_link_hash_warning
)
4917 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4918 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4919 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4921 if (h
->root
.type
== bfd_link_hash_common
)
4923 if (size
> h
->root
.u
.c
.size
)
4924 h
->root
.u
.c
.size
= size
;
4925 if (alignment_power
> h
->root
.u
.c
.p
->alignment_power
)
4926 h
->root
.u
.c
.p
->alignment_power
= alignment_power
;
4931 /* Make a special call to the linker "notice" function to
4932 tell it that symbols added for crefs may need to be removed. */
4933 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
4934 goto error_free_vers
;
4937 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4939 if (nondeflt_vers
!= NULL
)
4940 free (nondeflt_vers
);
4944 if (old_tab
!= NULL
)
4946 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
4947 goto error_free_vers
;
4952 /* Now that all the symbols from this input file are created, if
4953 not performing a relocatable link, handle .symver foo, foo@BAR
4954 such that any relocs against foo become foo@BAR. */
4955 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
4959 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4961 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4962 char *shortname
, *p
;
4964 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4966 || (h
->root
.type
!= bfd_link_hash_defined
4967 && h
->root
.type
!= bfd_link_hash_defweak
))
4970 amt
= p
- h
->root
.root
.string
;
4971 shortname
= (char *) bfd_malloc (amt
+ 1);
4973 goto error_free_vers
;
4974 memcpy (shortname
, h
->root
.root
.string
, amt
);
4975 shortname
[amt
] = '\0';
4977 hi
= (struct elf_link_hash_entry
*)
4978 bfd_link_hash_lookup (&htab
->root
, shortname
,
4979 FALSE
, FALSE
, FALSE
);
4981 && hi
->root
.type
== h
->root
.type
4982 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4983 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4985 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4986 hi
->root
.type
= bfd_link_hash_indirect
;
4987 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4988 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4989 sym_hash
= elf_sym_hashes (abfd
);
4991 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4992 if (sym_hash
[symidx
] == hi
)
4994 sym_hash
[symidx
] = h
;
5000 free (nondeflt_vers
);
5001 nondeflt_vers
= NULL
;
5004 /* Now set the weakdefs field correctly for all the weak defined
5005 symbols we found. The only way to do this is to search all the
5006 symbols. Since we only need the information for non functions in
5007 dynamic objects, that's the only time we actually put anything on
5008 the list WEAKS. We need this information so that if a regular
5009 object refers to a symbol defined weakly in a dynamic object, the
5010 real symbol in the dynamic object is also put in the dynamic
5011 symbols; we also must arrange for both symbols to point to the
5012 same memory location. We could handle the general case of symbol
5013 aliasing, but a general symbol alias can only be generated in
5014 assembler code, handling it correctly would be very time
5015 consuming, and other ELF linkers don't handle general aliasing
5019 struct elf_link_hash_entry
**hpp
;
5020 struct elf_link_hash_entry
**hppend
;
5021 struct elf_link_hash_entry
**sorted_sym_hash
;
5022 struct elf_link_hash_entry
*h
;
5025 /* Since we have to search the whole symbol list for each weak
5026 defined symbol, search time for N weak defined symbols will be
5027 O(N^2). Binary search will cut it down to O(NlogN). */
5029 amt
*= sizeof (struct elf_link_hash_entry
*);
5030 sorted_sym_hash
= (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5031 if (sorted_sym_hash
== NULL
)
5033 sym_hash
= sorted_sym_hash
;
5034 hpp
= elf_sym_hashes (abfd
);
5035 hppend
= hpp
+ extsymcount
;
5037 for (; hpp
< hppend
; hpp
++)
5041 && h
->root
.type
== bfd_link_hash_defined
5042 && !bed
->is_function_type (h
->type
))
5050 qsort (sorted_sym_hash
, sym_count
,
5051 sizeof (struct elf_link_hash_entry
*),
5054 while (weaks
!= NULL
)
5056 struct elf_link_hash_entry
*hlook
;
5059 size_t i
, j
, idx
= 0;
5062 weaks
= hlook
->u
.weakdef
;
5063 hlook
->u
.weakdef
= NULL
;
5065 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
5066 || hlook
->root
.type
== bfd_link_hash_defweak
5067 || hlook
->root
.type
== bfd_link_hash_common
5068 || hlook
->root
.type
== bfd_link_hash_indirect
);
5069 slook
= hlook
->root
.u
.def
.section
;
5070 vlook
= hlook
->root
.u
.def
.value
;
5076 bfd_signed_vma vdiff
;
5078 h
= sorted_sym_hash
[idx
];
5079 vdiff
= vlook
- h
->root
.u
.def
.value
;
5086 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5096 /* We didn't find a value/section match. */
5100 /* With multiple aliases, or when the weak symbol is already
5101 strongly defined, we have multiple matching symbols and
5102 the binary search above may land on any of them. Step
5103 one past the matching symbol(s). */
5106 h
= sorted_sym_hash
[idx
];
5107 if (h
->root
.u
.def
.section
!= slook
5108 || h
->root
.u
.def
.value
!= vlook
)
5112 /* Now look back over the aliases. Since we sorted by size
5113 as well as value and section, we'll choose the one with
5114 the largest size. */
5117 h
= sorted_sym_hash
[idx
];
5119 /* Stop if value or section doesn't match. */
5120 if (h
->root
.u
.def
.section
!= slook
5121 || h
->root
.u
.def
.value
!= vlook
)
5123 else if (h
!= hlook
)
5125 hlook
->u
.weakdef
= h
;
5127 /* If the weak definition is in the list of dynamic
5128 symbols, make sure the real definition is put
5130 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5132 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5135 free (sorted_sym_hash
);
5140 /* If the real definition is in the list of dynamic
5141 symbols, make sure the weak definition is put
5142 there as well. If we don't do this, then the
5143 dynamic loader might not merge the entries for the
5144 real definition and the weak definition. */
5145 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5147 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5148 goto err_free_sym_hash
;
5155 free (sorted_sym_hash
);
5158 if (bed
->check_directives
5159 && !(*bed
->check_directives
) (abfd
, info
))
5162 if (!info
->check_relocs_after_open_input
5163 && !_bfd_elf_link_check_relocs (abfd
, info
))
5166 /* If this is a non-traditional link, try to optimize the handling
5167 of the .stab/.stabstr sections. */
5169 && ! info
->traditional_format
5170 && is_elf_hash_table (htab
)
5171 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5175 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5176 if (stabstr
!= NULL
)
5178 bfd_size_type string_offset
= 0;
5181 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5182 if (CONST_STRNEQ (stab
->name
, ".stab")
5183 && (!stab
->name
[5] ||
5184 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5185 && (stab
->flags
& SEC_MERGE
) == 0
5186 && !bfd_is_abs_section (stab
->output_section
))
5188 struct bfd_elf_section_data
*secdata
;
5190 secdata
= elf_section_data (stab
);
5191 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5192 stabstr
, &secdata
->sec_info
,
5195 if (secdata
->sec_info
)
5196 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5201 if (is_elf_hash_table (htab
) && add_needed
)
5203 /* Add this bfd to the loaded list. */
5204 struct elf_link_loaded_list
*n
;
5206 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5210 n
->next
= htab
->loaded
;
5217 if (old_tab
!= NULL
)
5219 if (old_strtab
!= NULL
)
5221 if (nondeflt_vers
!= NULL
)
5222 free (nondeflt_vers
);
5223 if (extversym
!= NULL
)
5226 if (isymbuf
!= NULL
)
5232 /* Return the linker hash table entry of a symbol that might be
5233 satisfied by an archive symbol. Return -1 on error. */
5235 struct elf_link_hash_entry
*
5236 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5237 struct bfd_link_info
*info
,
5240 struct elf_link_hash_entry
*h
;
5244 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, TRUE
);
5248 /* If this is a default version (the name contains @@), look up the
5249 symbol again with only one `@' as well as without the version.
5250 The effect is that references to the symbol with and without the
5251 version will be matched by the default symbol in the archive. */
5253 p
= strchr (name
, ELF_VER_CHR
);
5254 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5257 /* First check with only one `@'. */
5258 len
= strlen (name
);
5259 copy
= (char *) bfd_alloc (abfd
, len
);
5261 return (struct elf_link_hash_entry
*) 0 - 1;
5263 first
= p
- name
+ 1;
5264 memcpy (copy
, name
, first
);
5265 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5267 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, TRUE
);
5270 /* We also need to check references to the symbol without the
5272 copy
[first
- 1] = '\0';
5273 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
5274 FALSE
, FALSE
, TRUE
);
5277 bfd_release (abfd
, copy
);
5281 /* Add symbols from an ELF archive file to the linker hash table. We
5282 don't use _bfd_generic_link_add_archive_symbols because we need to
5283 handle versioned symbols.
5285 Fortunately, ELF archive handling is simpler than that done by
5286 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5287 oddities. In ELF, if we find a symbol in the archive map, and the
5288 symbol is currently undefined, we know that we must pull in that
5291 Unfortunately, we do have to make multiple passes over the symbol
5292 table until nothing further is resolved. */
5295 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5298 unsigned char *included
= NULL
;
5302 const struct elf_backend_data
*bed
;
5303 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
5304 (bfd
*, struct bfd_link_info
*, const char *);
5306 if (! bfd_has_map (abfd
))
5308 /* An empty archive is a special case. */
5309 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5311 bfd_set_error (bfd_error_no_armap
);
5315 /* Keep track of all symbols we know to be already defined, and all
5316 files we know to be already included. This is to speed up the
5317 second and subsequent passes. */
5318 c
= bfd_ardata (abfd
)->symdef_count
;
5322 amt
*= sizeof (*included
);
5323 included
= (unsigned char *) bfd_zmalloc (amt
);
5324 if (included
== NULL
)
5327 symdefs
= bfd_ardata (abfd
)->symdefs
;
5328 bed
= get_elf_backend_data (abfd
);
5329 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5342 symdefend
= symdef
+ c
;
5343 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5345 struct elf_link_hash_entry
*h
;
5347 struct bfd_link_hash_entry
*undefs_tail
;
5352 if (symdef
->file_offset
== last
)
5358 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5359 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
5365 if (h
->root
.type
== bfd_link_hash_common
)
5367 /* We currently have a common symbol. The archive map contains
5368 a reference to this symbol, so we may want to include it. We
5369 only want to include it however, if this archive element
5370 contains a definition of the symbol, not just another common
5373 Unfortunately some archivers (including GNU ar) will put
5374 declarations of common symbols into their archive maps, as
5375 well as real definitions, so we cannot just go by the archive
5376 map alone. Instead we must read in the element's symbol
5377 table and check that to see what kind of symbol definition
5379 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5382 else if (h
->root
.type
!= bfd_link_hash_undefined
)
5384 if (h
->root
.type
!= bfd_link_hash_undefweak
)
5385 /* Symbol must be defined. Don't check it again. */
5390 /* We need to include this archive member. */
5391 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5392 if (element
== NULL
)
5395 if (! bfd_check_format (element
, bfd_object
))
5398 undefs_tail
= info
->hash
->undefs_tail
;
5400 if (!(*info
->callbacks
5401 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5403 if (!bfd_link_add_symbols (element
, info
))
5406 /* If there are any new undefined symbols, we need to make
5407 another pass through the archive in order to see whether
5408 they can be defined. FIXME: This isn't perfect, because
5409 common symbols wind up on undefs_tail and because an
5410 undefined symbol which is defined later on in this pass
5411 does not require another pass. This isn't a bug, but it
5412 does make the code less efficient than it could be. */
5413 if (undefs_tail
!= info
->hash
->undefs_tail
)
5416 /* Look backward to mark all symbols from this object file
5417 which we have already seen in this pass. */
5421 included
[mark
] = TRUE
;
5426 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5428 /* We mark subsequent symbols from this object file as we go
5429 on through the loop. */
5430 last
= symdef
->file_offset
;
5440 if (included
!= NULL
)
5445 /* Given an ELF BFD, add symbols to the global hash table as
5449 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5451 switch (bfd_get_format (abfd
))
5454 return elf_link_add_object_symbols (abfd
, info
);
5456 return elf_link_add_archive_symbols (abfd
, info
);
5458 bfd_set_error (bfd_error_wrong_format
);
5463 struct hash_codes_info
5465 unsigned long *hashcodes
;
5469 /* This function will be called though elf_link_hash_traverse to store
5470 all hash value of the exported symbols in an array. */
5473 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5475 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
5480 /* Ignore indirect symbols. These are added by the versioning code. */
5481 if (h
->dynindx
== -1)
5484 name
= h
->root
.root
.string
;
5485 if (h
->versioned
>= versioned
)
5487 char *p
= strchr (name
, ELF_VER_CHR
);
5490 alc
= (char *) bfd_malloc (p
- name
+ 1);
5496 memcpy (alc
, name
, p
- name
);
5497 alc
[p
- name
] = '\0';
5502 /* Compute the hash value. */
5503 ha
= bfd_elf_hash (name
);
5505 /* Store the found hash value in the array given as the argument. */
5506 *(inf
->hashcodes
)++ = ha
;
5508 /* And store it in the struct so that we can put it in the hash table
5510 h
->u
.elf_hash_value
= ha
;
5518 struct collect_gnu_hash_codes
5521 const struct elf_backend_data
*bed
;
5522 unsigned long int nsyms
;
5523 unsigned long int maskbits
;
5524 unsigned long int *hashcodes
;
5525 unsigned long int *hashval
;
5526 unsigned long int *indx
;
5527 unsigned long int *counts
;
5530 long int min_dynindx
;
5531 unsigned long int bucketcount
;
5532 unsigned long int symindx
;
5533 long int local_indx
;
5534 long int shift1
, shift2
;
5535 unsigned long int mask
;
5539 /* This function will be called though elf_link_hash_traverse to store
5540 all hash value of the exported symbols in an array. */
5543 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5545 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5550 /* Ignore indirect symbols. These are added by the versioning code. */
5551 if (h
->dynindx
== -1)
5554 /* Ignore also local symbols and undefined symbols. */
5555 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5558 name
= h
->root
.root
.string
;
5559 if (h
->versioned
>= versioned
)
5561 char *p
= strchr (name
, ELF_VER_CHR
);
5564 alc
= (char *) bfd_malloc (p
- name
+ 1);
5570 memcpy (alc
, name
, p
- name
);
5571 alc
[p
- name
] = '\0';
5576 /* Compute the hash value. */
5577 ha
= bfd_elf_gnu_hash (name
);
5579 /* Store the found hash value in the array for compute_bucket_count,
5580 and also for .dynsym reordering purposes. */
5581 s
->hashcodes
[s
->nsyms
] = ha
;
5582 s
->hashval
[h
->dynindx
] = ha
;
5584 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5585 s
->min_dynindx
= h
->dynindx
;
5593 /* This function will be called though elf_link_hash_traverse to do
5594 final dynaminc symbol renumbering. */
5597 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5599 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5600 unsigned long int bucket
;
5601 unsigned long int val
;
5603 /* Ignore indirect symbols. */
5604 if (h
->dynindx
== -1)
5607 /* Ignore also local symbols and undefined symbols. */
5608 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5610 if (h
->dynindx
>= s
->min_dynindx
)
5611 h
->dynindx
= s
->local_indx
++;
5615 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5616 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5617 & ((s
->maskbits
>> s
->shift1
) - 1);
5618 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5620 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5621 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5622 if (s
->counts
[bucket
] == 1)
5623 /* Last element terminates the chain. */
5625 bfd_put_32 (s
->output_bfd
, val
,
5626 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5627 --s
->counts
[bucket
];
5628 h
->dynindx
= s
->indx
[bucket
]++;
5632 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5635 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5637 return !(h
->forced_local
5638 || h
->root
.type
== bfd_link_hash_undefined
5639 || h
->root
.type
== bfd_link_hash_undefweak
5640 || ((h
->root
.type
== bfd_link_hash_defined
5641 || h
->root
.type
== bfd_link_hash_defweak
)
5642 && h
->root
.u
.def
.section
->output_section
== NULL
));
5645 /* Array used to determine the number of hash table buckets to use
5646 based on the number of symbols there are. If there are fewer than
5647 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5648 fewer than 37 we use 17 buckets, and so forth. We never use more
5649 than 32771 buckets. */
5651 static const size_t elf_buckets
[] =
5653 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5657 /* Compute bucket count for hashing table. We do not use a static set
5658 of possible tables sizes anymore. Instead we determine for all
5659 possible reasonable sizes of the table the outcome (i.e., the
5660 number of collisions etc) and choose the best solution. The
5661 weighting functions are not too simple to allow the table to grow
5662 without bounds. Instead one of the weighting factors is the size.
5663 Therefore the result is always a good payoff between few collisions
5664 (= short chain lengths) and table size. */
5666 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
5667 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
5668 unsigned long int nsyms
,
5671 size_t best_size
= 0;
5672 unsigned long int i
;
5674 /* We have a problem here. The following code to optimize the table
5675 size requires an integer type with more the 32 bits. If
5676 BFD_HOST_U_64_BIT is set we know about such a type. */
5677 #ifdef BFD_HOST_U_64_BIT
5682 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5683 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5684 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5685 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5686 unsigned long int *counts
;
5688 unsigned int no_improvement_count
= 0;
5690 /* Possible optimization parameters: if we have NSYMS symbols we say
5691 that the hashing table must at least have NSYMS/4 and at most
5693 minsize
= nsyms
/ 4;
5696 best_size
= maxsize
= nsyms
* 2;
5701 if ((best_size
& 31) == 0)
5705 /* Create array where we count the collisions in. We must use bfd_malloc
5706 since the size could be large. */
5708 amt
*= sizeof (unsigned long int);
5709 counts
= (unsigned long int *) bfd_malloc (amt
);
5713 /* Compute the "optimal" size for the hash table. The criteria is a
5714 minimal chain length. The minor criteria is (of course) the size
5716 for (i
= minsize
; i
< maxsize
; ++i
)
5718 /* Walk through the array of hashcodes and count the collisions. */
5719 BFD_HOST_U_64_BIT max
;
5720 unsigned long int j
;
5721 unsigned long int fact
;
5723 if (gnu_hash
&& (i
& 31) == 0)
5726 memset (counts
, '\0', i
* sizeof (unsigned long int));
5728 /* Determine how often each hash bucket is used. */
5729 for (j
= 0; j
< nsyms
; ++j
)
5730 ++counts
[hashcodes
[j
] % i
];
5732 /* For the weight function we need some information about the
5733 pagesize on the target. This is information need not be 100%
5734 accurate. Since this information is not available (so far) we
5735 define it here to a reasonable default value. If it is crucial
5736 to have a better value some day simply define this value. */
5737 # ifndef BFD_TARGET_PAGESIZE
5738 # define BFD_TARGET_PAGESIZE (4096)
5741 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5743 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5746 /* Variant 1: optimize for short chains. We add the squares
5747 of all the chain lengths (which favors many small chain
5748 over a few long chains). */
5749 for (j
= 0; j
< i
; ++j
)
5750 max
+= counts
[j
] * counts
[j
];
5752 /* This adds penalties for the overall size of the table. */
5753 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5756 /* Variant 2: Optimize a lot more for small table. Here we
5757 also add squares of the size but we also add penalties for
5758 empty slots (the +1 term). */
5759 for (j
= 0; j
< i
; ++j
)
5760 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5762 /* The overall size of the table is considered, but not as
5763 strong as in variant 1, where it is squared. */
5764 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5768 /* Compare with current best results. */
5769 if (max
< best_chlen
)
5773 no_improvement_count
= 0;
5775 /* PR 11843: Avoid futile long searches for the best bucket size
5776 when there are a large number of symbols. */
5777 else if (++no_improvement_count
== 100)
5784 #endif /* defined (BFD_HOST_U_64_BIT) */
5786 /* This is the fallback solution if no 64bit type is available or if we
5787 are not supposed to spend much time on optimizations. We select the
5788 bucket count using a fixed set of numbers. */
5789 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5791 best_size
= elf_buckets
[i
];
5792 if (nsyms
< elf_buckets
[i
+ 1])
5795 if (gnu_hash
&& best_size
< 2)
5802 /* Size any SHT_GROUP section for ld -r. */
5805 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
5809 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
5810 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
5811 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
5816 /* Set a default stack segment size. The value in INFO wins. If it
5817 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5818 undefined it is initialized. */
5821 bfd_elf_stack_segment_size (bfd
*output_bfd
,
5822 struct bfd_link_info
*info
,
5823 const char *legacy_symbol
,
5824 bfd_vma default_size
)
5826 struct elf_link_hash_entry
*h
= NULL
;
5828 /* Look for legacy symbol. */
5830 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
5831 FALSE
, FALSE
, FALSE
);
5832 if (h
&& (h
->root
.type
== bfd_link_hash_defined
5833 || h
->root
.type
== bfd_link_hash_defweak
)
5835 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
5837 /* The symbol has no type if specified on the command line. */
5838 h
->type
= STT_OBJECT
;
5839 if (info
->stacksize
)
5840 /* xgettext:c-format */
5841 _bfd_error_handler (_("%B: stack size specified and %s set"),
5842 output_bfd
, legacy_symbol
);
5843 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
5844 /* xgettext:c-format */
5845 _bfd_error_handler (_("%B: %s not absolute"),
5846 output_bfd
, legacy_symbol
);
5848 info
->stacksize
= h
->root
.u
.def
.value
;
5851 if (!info
->stacksize
)
5852 /* If the user didn't set a size, or explicitly inhibit the
5853 size, set it now. */
5854 info
->stacksize
= default_size
;
5856 /* Provide the legacy symbol, if it is referenced. */
5857 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
5858 || h
->root
.type
== bfd_link_hash_undefweak
))
5860 struct bfd_link_hash_entry
*bh
= NULL
;
5862 if (!(_bfd_generic_link_add_one_symbol
5863 (info
, output_bfd
, legacy_symbol
,
5864 BSF_GLOBAL
, bfd_abs_section_ptr
,
5865 info
->stacksize
>= 0 ? info
->stacksize
: 0,
5866 NULL
, FALSE
, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
5869 h
= (struct elf_link_hash_entry
*) bh
;
5871 h
->type
= STT_OBJECT
;
5877 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5879 struct elf_gc_sweep_symbol_info
5881 struct bfd_link_info
*info
;
5882 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
5887 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
5890 && (((h
->root
.type
== bfd_link_hash_defined
5891 || h
->root
.type
== bfd_link_hash_defweak
)
5892 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
5893 && h
->root
.u
.def
.section
->gc_mark
))
5894 || h
->root
.type
== bfd_link_hash_undefined
5895 || h
->root
.type
== bfd_link_hash_undefweak
))
5897 struct elf_gc_sweep_symbol_info
*inf
;
5899 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
5900 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
5903 h
->ref_regular_nonweak
= 0;
5909 /* Set up the sizes and contents of the ELF dynamic sections. This is
5910 called by the ELF linker emulation before_allocation routine. We
5911 must set the sizes of the sections before the linker sets the
5912 addresses of the various sections. */
5915 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5918 const char *filter_shlib
,
5920 const char *depaudit
,
5921 const char * const *auxiliary_filters
,
5922 struct bfd_link_info
*info
,
5923 asection
**sinterpptr
)
5927 const struct elf_backend_data
*bed
;
5931 soname_indx
= (size_t) -1;
5933 if (!is_elf_hash_table (info
->hash
))
5936 dynobj
= elf_hash_table (info
)->dynobj
;
5938 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
5940 struct bfd_elf_version_tree
*verdefs
;
5941 struct elf_info_failed asvinfo
;
5942 struct bfd_elf_version_tree
*t
;
5943 struct bfd_elf_version_expr
*d
;
5944 struct elf_info_failed eif
;
5945 bfd_boolean all_defined
;
5951 /* If we are supposed to export all symbols into the dynamic symbol
5952 table (this is not the normal case), then do so. */
5953 if (info
->export_dynamic
5954 || (bfd_link_executable (info
) && info
->dynamic
))
5956 elf_link_hash_traverse (elf_hash_table (info
),
5957 _bfd_elf_export_symbol
,
5963 /* Make all global versions with definition. */
5964 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
5965 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5966 if (!d
->symver
&& d
->literal
)
5968 const char *verstr
, *name
;
5969 size_t namelen
, verlen
, newlen
;
5970 char *newname
, *p
, leading_char
;
5971 struct elf_link_hash_entry
*newh
;
5973 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
5975 namelen
= strlen (name
) + (leading_char
!= '\0');
5977 verlen
= strlen (verstr
);
5978 newlen
= namelen
+ verlen
+ 3;
5980 newname
= (char *) bfd_malloc (newlen
);
5981 if (newname
== NULL
)
5983 newname
[0] = leading_char
;
5984 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
5986 /* Check the hidden versioned definition. */
5987 p
= newname
+ namelen
;
5989 memcpy (p
, verstr
, verlen
+ 1);
5990 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5991 newname
, FALSE
, FALSE
,
5994 || (newh
->root
.type
!= bfd_link_hash_defined
5995 && newh
->root
.type
!= bfd_link_hash_defweak
))
5997 /* Check the default versioned definition. */
5999 memcpy (p
, verstr
, verlen
+ 1);
6000 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6001 newname
, FALSE
, FALSE
,
6006 /* Mark this version if there is a definition and it is
6007 not defined in a shared object. */
6009 && !newh
->def_dynamic
6010 && (newh
->root
.type
== bfd_link_hash_defined
6011 || newh
->root
.type
== bfd_link_hash_defweak
))
6015 /* Attach all the symbols to their version information. */
6016 asvinfo
.info
= info
;
6017 asvinfo
.failed
= FALSE
;
6019 elf_link_hash_traverse (elf_hash_table (info
),
6020 _bfd_elf_link_assign_sym_version
,
6025 if (!info
->allow_undefined_version
)
6027 /* Check if all global versions have a definition. */
6029 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6030 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6031 if (d
->literal
&& !d
->symver
&& !d
->script
)
6034 (_("%s: undefined version: %s"),
6035 d
->pattern
, t
->name
);
6036 all_defined
= FALSE
;
6041 bfd_set_error (bfd_error_bad_value
);
6046 /* Set up the version definition section. */
6047 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6048 BFD_ASSERT (s
!= NULL
);
6050 /* We may have created additional version definitions if we are
6051 just linking a regular application. */
6052 verdefs
= info
->version_info
;
6054 /* Skip anonymous version tag. */
6055 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6056 verdefs
= verdefs
->next
;
6058 if (verdefs
== NULL
&& !info
->create_default_symver
)
6059 s
->flags
|= SEC_EXCLUDE
;
6065 Elf_Internal_Verdef def
;
6066 Elf_Internal_Verdaux defaux
;
6067 struct bfd_link_hash_entry
*bh
;
6068 struct elf_link_hash_entry
*h
;
6074 /* Make space for the base version. */
6075 size
+= sizeof (Elf_External_Verdef
);
6076 size
+= sizeof (Elf_External_Verdaux
);
6079 /* Make space for the default version. */
6080 if (info
->create_default_symver
)
6082 size
+= sizeof (Elf_External_Verdef
);
6086 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6088 struct bfd_elf_version_deps
*n
;
6090 /* Don't emit base version twice. */
6094 size
+= sizeof (Elf_External_Verdef
);
6095 size
+= sizeof (Elf_External_Verdaux
);
6098 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6099 size
+= sizeof (Elf_External_Verdaux
);
6103 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6104 if (s
->contents
== NULL
&& s
->size
!= 0)
6107 /* Fill in the version definition section. */
6111 def
.vd_version
= VER_DEF_CURRENT
;
6112 def
.vd_flags
= VER_FLG_BASE
;
6115 if (info
->create_default_symver
)
6117 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6118 def
.vd_next
= sizeof (Elf_External_Verdef
);
6122 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6123 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6124 + sizeof (Elf_External_Verdaux
));
6127 if (soname_indx
!= (size_t) -1)
6129 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6131 def
.vd_hash
= bfd_elf_hash (soname
);
6132 defaux
.vda_name
= soname_indx
;
6139 name
= lbasename (output_bfd
->filename
);
6140 def
.vd_hash
= bfd_elf_hash (name
);
6141 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6143 if (indx
== (size_t) -1)
6145 defaux
.vda_name
= indx
;
6147 defaux
.vda_next
= 0;
6149 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6150 (Elf_External_Verdef
*) p
);
6151 p
+= sizeof (Elf_External_Verdef
);
6152 if (info
->create_default_symver
)
6154 /* Add a symbol representing this version. */
6156 if (! (_bfd_generic_link_add_one_symbol
6157 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6159 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6161 h
= (struct elf_link_hash_entry
*) bh
;
6164 h
->type
= STT_OBJECT
;
6165 h
->verinfo
.vertree
= NULL
;
6167 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6170 /* Create a duplicate of the base version with the same
6171 aux block, but different flags. */
6174 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6176 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6177 + sizeof (Elf_External_Verdaux
));
6180 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6181 (Elf_External_Verdef
*) p
);
6182 p
+= sizeof (Elf_External_Verdef
);
6184 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6185 (Elf_External_Verdaux
*) p
);
6186 p
+= sizeof (Elf_External_Verdaux
);
6188 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6191 struct bfd_elf_version_deps
*n
;
6193 /* Don't emit the base version twice. */
6198 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6201 /* Add a symbol representing this version. */
6203 if (! (_bfd_generic_link_add_one_symbol
6204 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6206 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6208 h
= (struct elf_link_hash_entry
*) bh
;
6211 h
->type
= STT_OBJECT
;
6212 h
->verinfo
.vertree
= t
;
6214 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6217 def
.vd_version
= VER_DEF_CURRENT
;
6219 if (t
->globals
.list
== NULL
6220 && t
->locals
.list
== NULL
6222 def
.vd_flags
|= VER_FLG_WEAK
;
6223 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6224 def
.vd_cnt
= cdeps
+ 1;
6225 def
.vd_hash
= bfd_elf_hash (t
->name
);
6226 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6229 /* If a basever node is next, it *must* be the last node in
6230 the chain, otherwise Verdef construction breaks. */
6231 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6232 BFD_ASSERT (t
->next
->next
== NULL
);
6234 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6235 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6236 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6238 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6239 (Elf_External_Verdef
*) p
);
6240 p
+= sizeof (Elf_External_Verdef
);
6242 defaux
.vda_name
= h
->dynstr_index
;
6243 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6245 defaux
.vda_next
= 0;
6246 if (t
->deps
!= NULL
)
6247 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6248 t
->name_indx
= defaux
.vda_name
;
6250 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6251 (Elf_External_Verdaux
*) p
);
6252 p
+= sizeof (Elf_External_Verdaux
);
6254 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6256 if (n
->version_needed
== NULL
)
6258 /* This can happen if there was an error in the
6260 defaux
.vda_name
= 0;
6264 defaux
.vda_name
= n
->version_needed
->name_indx
;
6265 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6268 if (n
->next
== NULL
)
6269 defaux
.vda_next
= 0;
6271 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6273 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6274 (Elf_External_Verdaux
*) p
);
6275 p
+= sizeof (Elf_External_Verdaux
);
6279 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6282 /* Work out the size of the version reference section. */
6284 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6285 BFD_ASSERT (s
!= NULL
);
6287 struct elf_find_verdep_info sinfo
;
6290 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6291 if (sinfo
.vers
== 0)
6293 sinfo
.failed
= FALSE
;
6295 elf_link_hash_traverse (elf_hash_table (info
),
6296 _bfd_elf_link_find_version_dependencies
,
6301 if (elf_tdata (output_bfd
)->verref
== NULL
)
6302 s
->flags
|= SEC_EXCLUDE
;
6305 Elf_Internal_Verneed
*vn
;
6310 /* Build the version dependency section. */
6313 for (vn
= elf_tdata (output_bfd
)->verref
;
6315 vn
= vn
->vn_nextref
)
6317 Elf_Internal_Vernaux
*a
;
6319 size
+= sizeof (Elf_External_Verneed
);
6321 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6322 size
+= sizeof (Elf_External_Vernaux
);
6326 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6327 if (s
->contents
== NULL
)
6331 for (vn
= elf_tdata (output_bfd
)->verref
;
6333 vn
= vn
->vn_nextref
)
6336 Elf_Internal_Vernaux
*a
;
6340 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6343 vn
->vn_version
= VER_NEED_CURRENT
;
6345 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6346 elf_dt_name (vn
->vn_bfd
) != NULL
6347 ? elf_dt_name (vn
->vn_bfd
)
6348 : lbasename (vn
->vn_bfd
->filename
),
6350 if (indx
== (size_t) -1)
6353 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
6354 if (vn
->vn_nextref
== NULL
)
6357 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
6358 + caux
* sizeof (Elf_External_Vernaux
));
6360 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
6361 (Elf_External_Verneed
*) p
);
6362 p
+= sizeof (Elf_External_Verneed
);
6364 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6366 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6367 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6368 a
->vna_nodename
, FALSE
);
6369 if (indx
== (size_t) -1)
6372 if (a
->vna_nextptr
== NULL
)
6375 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6377 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6378 (Elf_External_Vernaux
*) p
);
6379 p
+= sizeof (Elf_External_Vernaux
);
6383 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6388 bed
= get_elf_backend_data (output_bfd
);
6390 if (info
->gc_sections
&& bed
->can_gc_sections
)
6392 struct elf_gc_sweep_symbol_info sweep_info
;
6393 unsigned long section_sym_count
;
6395 /* Remove the symbols that were in the swept sections from the
6396 dynamic symbol table. GCFIXME: Anyone know how to get them
6397 out of the static symbol table as well? */
6398 sweep_info
.info
= info
;
6399 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
6400 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
6403 _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, §ion_sym_count
);
6406 /* Any syms created from now on start with -1 in
6407 got.refcount/offset and plt.refcount/offset. */
6408 elf_hash_table (info
)->init_got_refcount
6409 = elf_hash_table (info
)->init_got_offset
;
6410 elf_hash_table (info
)->init_plt_refcount
6411 = elf_hash_table (info
)->init_plt_offset
;
6413 if (bfd_link_relocatable (info
)
6414 && !_bfd_elf_size_group_sections (info
))
6417 /* The backend may have to create some sections regardless of whether
6418 we're dynamic or not. */
6419 if (bed
->elf_backend_always_size_sections
6420 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
6423 /* Determine any GNU_STACK segment requirements, after the backend
6424 has had a chance to set a default segment size. */
6425 if (info
->execstack
)
6426 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
6427 else if (info
->noexecstack
)
6428 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
6432 asection
*notesec
= NULL
;
6435 for (inputobj
= info
->input_bfds
;
6437 inputobj
= inputobj
->link
.next
)
6442 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
6444 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
6447 if (s
->flags
& SEC_CODE
)
6451 else if (bed
->default_execstack
)
6454 if (notesec
|| info
->stacksize
> 0)
6455 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
6456 if (notesec
&& exec
&& bfd_link_relocatable (info
)
6457 && notesec
->output_section
!= bfd_abs_section_ptr
)
6458 notesec
->output_section
->flags
|= SEC_CODE
;
6461 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6463 struct elf_info_failed eif
;
6464 struct elf_link_hash_entry
*h
;
6468 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
6469 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
6473 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6475 if (soname_indx
== (size_t) -1
6476 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6482 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
6484 info
->flags
|= DF_SYMBOLIC
;
6492 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
6494 if (indx
== (size_t) -1)
6497 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
6498 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
6502 if (filter_shlib
!= NULL
)
6506 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6507 filter_shlib
, TRUE
);
6508 if (indx
== (size_t) -1
6509 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
6513 if (auxiliary_filters
!= NULL
)
6515 const char * const *p
;
6517 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
6521 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6523 if (indx
== (size_t) -1
6524 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
6533 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
6535 if (indx
== (size_t) -1
6536 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
6540 if (depaudit
!= NULL
)
6544 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
6546 if (indx
== (size_t) -1
6547 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
6554 /* Find all symbols which were defined in a dynamic object and make
6555 the backend pick a reasonable value for them. */
6556 elf_link_hash_traverse (elf_hash_table (info
),
6557 _bfd_elf_adjust_dynamic_symbol
,
6562 /* Add some entries to the .dynamic section. We fill in some of the
6563 values later, in bfd_elf_final_link, but we must add the entries
6564 now so that we know the final size of the .dynamic section. */
6566 /* If there are initialization and/or finalization functions to
6567 call then add the corresponding DT_INIT/DT_FINI entries. */
6568 h
= (info
->init_function
6569 ? elf_link_hash_lookup (elf_hash_table (info
),
6570 info
->init_function
, FALSE
,
6577 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
6580 h
= (info
->fini_function
6581 ? elf_link_hash_lookup (elf_hash_table (info
),
6582 info
->fini_function
, FALSE
,
6589 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
6593 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
6594 if (s
!= NULL
&& s
->linker_has_input
)
6596 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6597 if (! bfd_link_executable (info
))
6602 for (sub
= info
->input_bfds
; sub
!= NULL
;
6603 sub
= sub
->link
.next
)
6604 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
6605 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
6606 if (elf_section_data (o
)->this_hdr
.sh_type
6607 == SHT_PREINIT_ARRAY
)
6610 (_("%B: .preinit_array section is not allowed in DSO"),
6615 bfd_set_error (bfd_error_nonrepresentable_section
);
6619 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
6620 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
6623 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
6624 if (s
!= NULL
&& s
->linker_has_input
)
6626 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
6627 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
6630 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
6631 if (s
!= NULL
&& s
->linker_has_input
)
6633 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
6634 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
6638 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
6639 /* If .dynstr is excluded from the link, we don't want any of
6640 these tags. Strictly, we should be checking each section
6641 individually; This quick check covers for the case where
6642 someone does a /DISCARD/ : { *(*) }. */
6643 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
6645 bfd_size_type strsize
;
6647 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6648 if ((info
->emit_hash
6649 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
6650 || (info
->emit_gnu_hash
6651 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
6652 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
6653 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
6654 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
6655 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
6656 bed
->s
->sizeof_sym
))
6661 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
6664 /* The backend must work out the sizes of all the other dynamic
6667 && bed
->elf_backend_size_dynamic_sections
!= NULL
6668 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
6671 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6673 unsigned long section_sym_count
;
6675 if (elf_tdata (output_bfd
)->cverdefs
)
6677 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
6679 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
6680 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
6684 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
6686 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
6689 else if (info
->flags
& DF_BIND_NOW
)
6691 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
6697 if (bfd_link_executable (info
))
6698 info
->flags_1
&= ~ (DF_1_INITFIRST
6701 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
6705 if (elf_tdata (output_bfd
)->cverrefs
)
6707 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
6709 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
6710 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
6714 if ((elf_tdata (output_bfd
)->cverrefs
== 0
6715 && elf_tdata (output_bfd
)->cverdefs
== 0)
6716 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6717 §ion_sym_count
) == 0)
6721 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6722 s
->flags
|= SEC_EXCLUDE
;
6728 /* Find the first non-excluded output section. We'll use its
6729 section symbol for some emitted relocs. */
6731 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
6735 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6736 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
6737 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6739 elf_hash_table (info
)->text_index_section
= s
;
6744 /* Find two non-excluded output sections, one for code, one for data.
6745 We'll use their section symbols for some emitted relocs. */
6747 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6751 /* Data first, since setting text_index_section changes
6752 _bfd_elf_link_omit_section_dynsym. */
6753 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6754 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
6755 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6757 elf_hash_table (info
)->data_index_section
= s
;
6761 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6762 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
6763 == (SEC_ALLOC
| SEC_READONLY
))
6764 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6766 elf_hash_table (info
)->text_index_section
= s
;
6770 if (elf_hash_table (info
)->text_index_section
== NULL
)
6771 elf_hash_table (info
)->text_index_section
6772 = elf_hash_table (info
)->data_index_section
;
6776 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6778 const struct elf_backend_data
*bed
;
6780 if (!is_elf_hash_table (info
->hash
))
6783 bed
= get_elf_backend_data (output_bfd
);
6784 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6786 if (elf_hash_table (info
)->dynamic_sections_created
)
6790 bfd_size_type dynsymcount
;
6791 unsigned long section_sym_count
;
6792 unsigned int dtagcount
;
6794 dynobj
= elf_hash_table (info
)->dynobj
;
6796 /* Assign dynsym indicies. In a shared library we generate a
6797 section symbol for each output section, which come first.
6798 Next come all of the back-end allocated local dynamic syms,
6799 followed by the rest of the global symbols. */
6801 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6802 §ion_sym_count
);
6804 /* Work out the size of the symbol version section. */
6805 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6806 BFD_ASSERT (s
!= NULL
);
6807 if ((s
->flags
& SEC_EXCLUDE
) == 0)
6809 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6810 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6811 if (s
->contents
== NULL
)
6814 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
6818 /* Set the size of the .dynsym and .hash sections. We counted
6819 the number of dynamic symbols in elf_link_add_object_symbols.
6820 We will build the contents of .dynsym and .hash when we build
6821 the final symbol table, because until then we do not know the
6822 correct value to give the symbols. We built the .dynstr
6823 section as we went along in elf_link_add_object_symbols. */
6824 s
= elf_hash_table (info
)->dynsym
;
6825 BFD_ASSERT (s
!= NULL
);
6826 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
6828 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6829 if (s
->contents
== NULL
)
6832 /* The first entry in .dynsym is a dummy symbol. Clear all the
6833 section syms, in case we don't output them all. */
6834 ++section_sym_count
;
6835 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
6837 elf_hash_table (info
)->bucketcount
= 0;
6839 /* Compute the size of the hashing table. As a side effect this
6840 computes the hash values for all the names we export. */
6841 if (info
->emit_hash
)
6843 unsigned long int *hashcodes
;
6844 struct hash_codes_info hashinf
;
6846 unsigned long int nsyms
;
6848 size_t hash_entry_size
;
6850 /* Compute the hash values for all exported symbols. At the same
6851 time store the values in an array so that we could use them for
6853 amt
= dynsymcount
* sizeof (unsigned long int);
6854 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
6855 if (hashcodes
== NULL
)
6857 hashinf
.hashcodes
= hashcodes
;
6858 hashinf
.error
= FALSE
;
6860 /* Put all hash values in HASHCODES. */
6861 elf_link_hash_traverse (elf_hash_table (info
),
6862 elf_collect_hash_codes
, &hashinf
);
6869 nsyms
= hashinf
.hashcodes
- hashcodes
;
6871 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6874 if (bucketcount
== 0)
6877 elf_hash_table (info
)->bucketcount
= bucketcount
;
6879 s
= bfd_get_linker_section (dynobj
, ".hash");
6880 BFD_ASSERT (s
!= NULL
);
6881 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6882 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6883 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6884 if (s
->contents
== NULL
)
6887 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6888 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6889 s
->contents
+ hash_entry_size
);
6892 if (info
->emit_gnu_hash
)
6895 unsigned char *contents
;
6896 struct collect_gnu_hash_codes cinfo
;
6900 memset (&cinfo
, 0, sizeof (cinfo
));
6902 /* Compute the hash values for all exported symbols. At the same
6903 time store the values in an array so that we could use them for
6905 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6906 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
6907 if (cinfo
.hashcodes
== NULL
)
6910 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6911 cinfo
.min_dynindx
= -1;
6912 cinfo
.output_bfd
= output_bfd
;
6915 /* Put all hash values in HASHCODES. */
6916 elf_link_hash_traverse (elf_hash_table (info
),
6917 elf_collect_gnu_hash_codes
, &cinfo
);
6920 free (cinfo
.hashcodes
);
6925 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6927 if (bucketcount
== 0)
6929 free (cinfo
.hashcodes
);
6933 s
= bfd_get_linker_section (dynobj
, ".gnu.hash");
6934 BFD_ASSERT (s
!= NULL
);
6936 if (cinfo
.nsyms
== 0)
6938 /* Empty .gnu.hash section is special. */
6939 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6940 free (cinfo
.hashcodes
);
6941 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6942 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6943 if (contents
== NULL
)
6945 s
->contents
= contents
;
6946 /* 1 empty bucket. */
6947 bfd_put_32 (output_bfd
, 1, contents
);
6948 /* SYMIDX above the special symbol 0. */
6949 bfd_put_32 (output_bfd
, 1, contents
+ 4);
6950 /* Just one word for bitmask. */
6951 bfd_put_32 (output_bfd
, 1, contents
+ 8);
6952 /* Only hash fn bloom filter. */
6953 bfd_put_32 (output_bfd
, 0, contents
+ 12);
6954 /* No hashes are valid - empty bitmask. */
6955 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
6956 /* No hashes in the only bucket. */
6957 bfd_put_32 (output_bfd
, 0,
6958 contents
+ 16 + bed
->s
->arch_size
/ 8);
6962 unsigned long int maskwords
, maskbitslog2
, x
;
6963 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
6967 while ((x
>>= 1) != 0)
6969 if (maskbitslog2
< 3)
6971 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
6972 maskbitslog2
= maskbitslog2
+ 3;
6974 maskbitslog2
= maskbitslog2
+ 2;
6975 if (bed
->s
->arch_size
== 64)
6977 if (maskbitslog2
== 5)
6983 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
6984 cinfo
.shift2
= maskbitslog2
;
6985 cinfo
.maskbits
= 1 << maskbitslog2
;
6986 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
6987 amt
= bucketcount
* sizeof (unsigned long int) * 2;
6988 amt
+= maskwords
* sizeof (bfd_vma
);
6989 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
6990 if (cinfo
.bitmask
== NULL
)
6992 free (cinfo
.hashcodes
);
6996 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
6997 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
6998 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
6999 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
7001 /* Determine how often each hash bucket is used. */
7002 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
7003 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
7004 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
7006 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
7007 if (cinfo
.counts
[i
] != 0)
7009 cinfo
.indx
[i
] = cnt
;
7010 cnt
+= cinfo
.counts
[i
];
7012 BFD_ASSERT (cnt
== dynsymcount
);
7013 cinfo
.bucketcount
= bucketcount
;
7014 cinfo
.local_indx
= cinfo
.min_dynindx
;
7016 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
7017 s
->size
+= cinfo
.maskbits
/ 8;
7018 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7019 if (contents
== NULL
)
7021 free (cinfo
.bitmask
);
7022 free (cinfo
.hashcodes
);
7026 s
->contents
= contents
;
7027 bfd_put_32 (output_bfd
, bucketcount
, contents
);
7028 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
7029 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
7030 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
7031 contents
+= 16 + cinfo
.maskbits
/ 8;
7033 for (i
= 0; i
< bucketcount
; ++i
)
7035 if (cinfo
.counts
[i
] == 0)
7036 bfd_put_32 (output_bfd
, 0, contents
);
7038 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
7042 cinfo
.contents
= contents
;
7044 /* Renumber dynamic symbols, populate .gnu.hash section. */
7045 elf_link_hash_traverse (elf_hash_table (info
),
7046 elf_renumber_gnu_hash_syms
, &cinfo
);
7048 contents
= s
->contents
+ 16;
7049 for (i
= 0; i
< maskwords
; ++i
)
7051 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
7053 contents
+= bed
->s
->arch_size
/ 8;
7056 free (cinfo
.bitmask
);
7057 free (cinfo
.hashcodes
);
7061 s
= bfd_get_linker_section (dynobj
, ".dynstr");
7062 BFD_ASSERT (s
!= NULL
);
7064 elf_finalize_dynstr (output_bfd
, info
);
7066 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7068 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
7069 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
7076 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7079 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
7082 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
7083 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
7086 /* Finish SHF_MERGE section merging. */
7089 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
7094 if (!is_elf_hash_table (info
->hash
))
7097 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
7098 if ((ibfd
->flags
& DYNAMIC
) == 0
7099 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
7100 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
7101 == get_elf_backend_data (obfd
)->s
->elfclass
))
7102 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7103 if ((sec
->flags
& SEC_MERGE
) != 0
7104 && !bfd_is_abs_section (sec
->output_section
))
7106 struct bfd_elf_section_data
*secdata
;
7108 secdata
= elf_section_data (sec
);
7109 if (! _bfd_add_merge_section (obfd
,
7110 &elf_hash_table (info
)->merge_info
,
7111 sec
, &secdata
->sec_info
))
7113 else if (secdata
->sec_info
)
7114 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7117 if (elf_hash_table (info
)->merge_info
!= NULL
)
7118 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7119 merge_sections_remove_hook
);
7123 /* Create an entry in an ELF linker hash table. */
7125 struct bfd_hash_entry
*
7126 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7127 struct bfd_hash_table
*table
,
7130 /* Allocate the structure if it has not already been allocated by a
7134 entry
= (struct bfd_hash_entry
*)
7135 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7140 /* Call the allocation method of the superclass. */
7141 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7144 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7145 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7147 /* Set local fields. */
7150 ret
->got
= htab
->init_got_refcount
;
7151 ret
->plt
= htab
->init_plt_refcount
;
7152 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7153 - offsetof (struct elf_link_hash_entry
, size
)));
7154 /* Assume that we have been called by a non-ELF symbol reader.
7155 This flag is then reset by the code which reads an ELF input
7156 file. This ensures that a symbol created by a non-ELF symbol
7157 reader will have the flag set correctly. */
7164 /* Copy data from an indirect symbol to its direct symbol, hiding the
7165 old indirect symbol. Also used for copying flags to a weakdef. */
7168 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7169 struct elf_link_hash_entry
*dir
,
7170 struct elf_link_hash_entry
*ind
)
7172 struct elf_link_hash_table
*htab
;
7174 /* Copy down any references that we may have already seen to the
7175 symbol which just became indirect. */
7177 if (dir
->versioned
!= versioned_hidden
)
7178 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7179 dir
->ref_regular
|= ind
->ref_regular
;
7180 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7181 dir
->non_got_ref
|= ind
->non_got_ref
;
7182 dir
->needs_plt
|= ind
->needs_plt
;
7183 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7185 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7188 /* Copy over the global and procedure linkage table refcount entries.
7189 These may have been already set up by a check_relocs routine. */
7190 htab
= elf_hash_table (info
);
7191 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7193 if (dir
->got
.refcount
< 0)
7194 dir
->got
.refcount
= 0;
7195 dir
->got
.refcount
+= ind
->got
.refcount
;
7196 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7199 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7201 if (dir
->plt
.refcount
< 0)
7202 dir
->plt
.refcount
= 0;
7203 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7204 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7207 if (ind
->dynindx
!= -1)
7209 if (dir
->dynindx
!= -1)
7210 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7211 dir
->dynindx
= ind
->dynindx
;
7212 dir
->dynstr_index
= ind
->dynstr_index
;
7214 ind
->dynstr_index
= 0;
7219 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7220 struct elf_link_hash_entry
*h
,
7221 bfd_boolean force_local
)
7223 /* STT_GNU_IFUNC symbol must go through PLT. */
7224 if (h
->type
!= STT_GNU_IFUNC
)
7226 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7231 h
->forced_local
= 1;
7232 if (h
->dynindx
!= -1)
7235 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7241 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7245 _bfd_elf_link_hash_table_init
7246 (struct elf_link_hash_table
*table
,
7248 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
7249 struct bfd_hash_table
*,
7251 unsigned int entsize
,
7252 enum elf_target_id target_id
)
7255 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
7257 table
->init_got_refcount
.refcount
= can_refcount
- 1;
7258 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
7259 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
7260 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
7261 /* The first dynamic symbol is a dummy. */
7262 table
->dynsymcount
= 1;
7264 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
7266 table
->root
.type
= bfd_link_elf_hash_table
;
7267 table
->hash_table_id
= target_id
;
7272 /* Create an ELF linker hash table. */
7274 struct bfd_link_hash_table
*
7275 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7277 struct elf_link_hash_table
*ret
;
7278 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
7280 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7284 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7285 sizeof (struct elf_link_hash_entry
),
7291 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7296 /* Destroy an ELF linker hash table. */
7299 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7301 struct elf_link_hash_table
*htab
;
7303 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7304 if (htab
->dynstr
!= NULL
)
7305 _bfd_elf_strtab_free (htab
->dynstr
);
7306 _bfd_merge_sections_free (htab
->merge_info
);
7307 _bfd_generic_link_hash_table_free (obfd
);
7310 /* This is a hook for the ELF emulation code in the generic linker to
7311 tell the backend linker what file name to use for the DT_NEEDED
7312 entry for a dynamic object. */
7315 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7317 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7318 && bfd_get_format (abfd
) == bfd_object
)
7319 elf_dt_name (abfd
) = name
;
7323 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
7326 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7327 && bfd_get_format (abfd
) == bfd_object
)
7328 lib_class
= elf_dyn_lib_class (abfd
);
7335 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
7337 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7338 && bfd_get_format (abfd
) == bfd_object
)
7339 elf_dyn_lib_class (abfd
) = lib_class
;
7342 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7343 the linker ELF emulation code. */
7345 struct bfd_link_needed_list
*
7346 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7347 struct bfd_link_info
*info
)
7349 if (! is_elf_hash_table (info
->hash
))
7351 return elf_hash_table (info
)->needed
;
7354 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7355 hook for the linker ELF emulation code. */
7357 struct bfd_link_needed_list
*
7358 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7359 struct bfd_link_info
*info
)
7361 if (! is_elf_hash_table (info
->hash
))
7363 return elf_hash_table (info
)->runpath
;
7366 /* Get the name actually used for a dynamic object for a link. This
7367 is the SONAME entry if there is one. Otherwise, it is the string
7368 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7371 bfd_elf_get_dt_soname (bfd
*abfd
)
7373 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7374 && bfd_get_format (abfd
) == bfd_object
)
7375 return elf_dt_name (abfd
);
7379 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7380 the ELF linker emulation code. */
7383 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
7384 struct bfd_link_needed_list
**pneeded
)
7387 bfd_byte
*dynbuf
= NULL
;
7388 unsigned int elfsec
;
7389 unsigned long shlink
;
7390 bfd_byte
*extdyn
, *extdynend
;
7392 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
7396 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
7397 || bfd_get_format (abfd
) != bfd_object
)
7400 s
= bfd_get_section_by_name (abfd
, ".dynamic");
7401 if (s
== NULL
|| s
->size
== 0)
7404 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
7407 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
7408 if (elfsec
== SHN_BAD
)
7411 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
7413 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
7414 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
7417 extdynend
= extdyn
+ s
->size
;
7418 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
7420 Elf_Internal_Dyn dyn
;
7422 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
7424 if (dyn
.d_tag
== DT_NULL
)
7427 if (dyn
.d_tag
== DT_NEEDED
)
7430 struct bfd_link_needed_list
*l
;
7431 unsigned int tagv
= dyn
.d_un
.d_val
;
7434 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
7439 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
7460 struct elf_symbuf_symbol
7462 unsigned long st_name
; /* Symbol name, index in string tbl */
7463 unsigned char st_info
; /* Type and binding attributes */
7464 unsigned char st_other
; /* Visibilty, and target specific */
7467 struct elf_symbuf_head
7469 struct elf_symbuf_symbol
*ssym
;
7471 unsigned int st_shndx
;
7478 Elf_Internal_Sym
*isym
;
7479 struct elf_symbuf_symbol
*ssym
;
7484 /* Sort references to symbols by ascending section number. */
7487 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
7489 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
7490 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
7492 return s1
->st_shndx
- s2
->st_shndx
;
7496 elf_sym_name_compare (const void *arg1
, const void *arg2
)
7498 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
7499 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
7500 return strcmp (s1
->name
, s2
->name
);
7503 static struct elf_symbuf_head
*
7504 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
7506 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
7507 struct elf_symbuf_symbol
*ssym
;
7508 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
7509 size_t i
, shndx_count
, total_size
;
7511 indbuf
= (Elf_Internal_Sym
**) bfd_malloc2 (symcount
, sizeof (*indbuf
));
7515 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
7516 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
7517 *ind
++ = &isymbuf
[i
];
7520 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
7521 elf_sort_elf_symbol
);
7524 if (indbufend
> indbuf
)
7525 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
7526 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
7529 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
7530 + (indbufend
- indbuf
) * sizeof (*ssym
));
7531 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
7532 if (ssymbuf
== NULL
)
7538 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
7539 ssymbuf
->ssym
= NULL
;
7540 ssymbuf
->count
= shndx_count
;
7541 ssymbuf
->st_shndx
= 0;
7542 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
7544 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
7547 ssymhead
->ssym
= ssym
;
7548 ssymhead
->count
= 0;
7549 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
7551 ssym
->st_name
= (*ind
)->st_name
;
7552 ssym
->st_info
= (*ind
)->st_info
;
7553 ssym
->st_other
= (*ind
)->st_other
;
7556 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
7557 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
7564 /* Check if 2 sections define the same set of local and global
7568 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
7569 struct bfd_link_info
*info
)
7572 const struct elf_backend_data
*bed1
, *bed2
;
7573 Elf_Internal_Shdr
*hdr1
, *hdr2
;
7574 size_t symcount1
, symcount2
;
7575 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
7576 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
7577 Elf_Internal_Sym
*isym
, *isymend
;
7578 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
7579 size_t count1
, count2
, i
;
7580 unsigned int shndx1
, shndx2
;
7586 /* Both sections have to be in ELF. */
7587 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
7588 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
7591 if (elf_section_type (sec1
) != elf_section_type (sec2
))
7594 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
7595 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
7596 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
7599 bed1
= get_elf_backend_data (bfd1
);
7600 bed2
= get_elf_backend_data (bfd2
);
7601 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
7602 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
7603 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
7604 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
7606 if (symcount1
== 0 || symcount2
== 0)
7612 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
7613 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
7615 if (ssymbuf1
== NULL
)
7617 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
7619 if (isymbuf1
== NULL
)
7622 if (!info
->reduce_memory_overheads
)
7623 elf_tdata (bfd1
)->symbuf
= ssymbuf1
7624 = elf_create_symbuf (symcount1
, isymbuf1
);
7627 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
7629 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
7631 if (isymbuf2
== NULL
)
7634 if (ssymbuf1
!= NULL
&& !info
->reduce_memory_overheads
)
7635 elf_tdata (bfd2
)->symbuf
= ssymbuf2
7636 = elf_create_symbuf (symcount2
, isymbuf2
);
7639 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
7641 /* Optimized faster version. */
7643 struct elf_symbol
*symp
;
7644 struct elf_symbuf_symbol
*ssym
, *ssymend
;
7647 hi
= ssymbuf1
->count
;
7652 mid
= (lo
+ hi
) / 2;
7653 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
7655 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
7659 count1
= ssymbuf1
[mid
].count
;
7666 hi
= ssymbuf2
->count
;
7671 mid
= (lo
+ hi
) / 2;
7672 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
7674 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
7678 count2
= ssymbuf2
[mid
].count
;
7684 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7688 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
7690 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
7691 if (symtable1
== NULL
|| symtable2
== NULL
)
7695 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
7696 ssym
< ssymend
; ssym
++, symp
++)
7698 symp
->u
.ssym
= ssym
;
7699 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
7705 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
7706 ssym
< ssymend
; ssym
++, symp
++)
7708 symp
->u
.ssym
= ssym
;
7709 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
7714 /* Sort symbol by name. */
7715 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7716 elf_sym_name_compare
);
7717 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7718 elf_sym_name_compare
);
7720 for (i
= 0; i
< count1
; i
++)
7721 /* Two symbols must have the same binding, type and name. */
7722 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
7723 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
7724 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7731 symtable1
= (struct elf_symbol
*)
7732 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
7733 symtable2
= (struct elf_symbol
*)
7734 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
7735 if (symtable1
== NULL
|| symtable2
== NULL
)
7738 /* Count definitions in the section. */
7740 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
7741 if (isym
->st_shndx
== shndx1
)
7742 symtable1
[count1
++].u
.isym
= isym
;
7745 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
7746 if (isym
->st_shndx
== shndx2
)
7747 symtable2
[count2
++].u
.isym
= isym
;
7749 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7752 for (i
= 0; i
< count1
; i
++)
7754 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
7755 symtable1
[i
].u
.isym
->st_name
);
7757 for (i
= 0; i
< count2
; i
++)
7759 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
7760 symtable2
[i
].u
.isym
->st_name
);
7762 /* Sort symbol by name. */
7763 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7764 elf_sym_name_compare
);
7765 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7766 elf_sym_name_compare
);
7768 for (i
= 0; i
< count1
; i
++)
7769 /* Two symbols must have the same binding, type and name. */
7770 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
7771 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
7772 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7790 /* Return TRUE if 2 section types are compatible. */
7793 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
7794 bfd
*bbfd
, const asection
*bsec
)
7798 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
7799 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7802 return elf_section_type (asec
) == elf_section_type (bsec
);
7805 /* Final phase of ELF linker. */
7807 /* A structure we use to avoid passing large numbers of arguments. */
7809 struct elf_final_link_info
7811 /* General link information. */
7812 struct bfd_link_info
*info
;
7815 /* Symbol string table. */
7816 struct elf_strtab_hash
*symstrtab
;
7817 /* .hash section. */
7819 /* symbol version section (.gnu.version). */
7820 asection
*symver_sec
;
7821 /* Buffer large enough to hold contents of any section. */
7823 /* Buffer large enough to hold external relocs of any section. */
7824 void *external_relocs
;
7825 /* Buffer large enough to hold internal relocs of any section. */
7826 Elf_Internal_Rela
*internal_relocs
;
7827 /* Buffer large enough to hold external local symbols of any input
7829 bfd_byte
*external_syms
;
7830 /* And a buffer for symbol section indices. */
7831 Elf_External_Sym_Shndx
*locsym_shndx
;
7832 /* Buffer large enough to hold internal local symbols of any input
7834 Elf_Internal_Sym
*internal_syms
;
7835 /* Array large enough to hold a symbol index for each local symbol
7836 of any input BFD. */
7838 /* Array large enough to hold a section pointer for each local
7839 symbol of any input BFD. */
7840 asection
**sections
;
7841 /* Buffer for SHT_SYMTAB_SHNDX section. */
7842 Elf_External_Sym_Shndx
*symshndxbuf
;
7843 /* Number of STT_FILE syms seen. */
7844 size_t filesym_count
;
7847 /* This struct is used to pass information to elf_link_output_extsym. */
7849 struct elf_outext_info
7852 bfd_boolean localsyms
;
7853 bfd_boolean file_sym_done
;
7854 struct elf_final_link_info
*flinfo
;
7858 /* Support for evaluating a complex relocation.
7860 Complex relocations are generalized, self-describing relocations. The
7861 implementation of them consists of two parts: complex symbols, and the
7862 relocations themselves.
7864 The relocations are use a reserved elf-wide relocation type code (R_RELC
7865 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7866 information (start bit, end bit, word width, etc) into the addend. This
7867 information is extracted from CGEN-generated operand tables within gas.
7869 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7870 internal) representing prefix-notation expressions, including but not
7871 limited to those sorts of expressions normally encoded as addends in the
7872 addend field. The symbol mangling format is:
7875 | <unary-operator> ':' <node>
7876 | <binary-operator> ':' <node> ':' <node>
7879 <literal> := 's' <digits=N> ':' <N character symbol name>
7880 | 'S' <digits=N> ':' <N character section name>
7884 <binary-operator> := as in C
7885 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7888 set_symbol_value (bfd
*bfd_with_globals
,
7889 Elf_Internal_Sym
*isymbuf
,
7894 struct elf_link_hash_entry
**sym_hashes
;
7895 struct elf_link_hash_entry
*h
;
7896 size_t extsymoff
= locsymcount
;
7898 if (symidx
< locsymcount
)
7900 Elf_Internal_Sym
*sym
;
7902 sym
= isymbuf
+ symidx
;
7903 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
7905 /* It is a local symbol: move it to the
7906 "absolute" section and give it a value. */
7907 sym
->st_shndx
= SHN_ABS
;
7908 sym
->st_value
= val
;
7911 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
7915 /* It is a global symbol: set its link type
7916 to "defined" and give it a value. */
7918 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
7919 h
= sym_hashes
[symidx
- extsymoff
];
7920 while (h
->root
.type
== bfd_link_hash_indirect
7921 || h
->root
.type
== bfd_link_hash_warning
)
7922 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7923 h
->root
.type
= bfd_link_hash_defined
;
7924 h
->root
.u
.def
.value
= val
;
7925 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
7929 resolve_symbol (const char *name
,
7931 struct elf_final_link_info
*flinfo
,
7933 Elf_Internal_Sym
*isymbuf
,
7936 Elf_Internal_Sym
*sym
;
7937 struct bfd_link_hash_entry
*global_entry
;
7938 const char *candidate
= NULL
;
7939 Elf_Internal_Shdr
*symtab_hdr
;
7942 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
7944 for (i
= 0; i
< locsymcount
; ++ i
)
7948 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
7951 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
7952 symtab_hdr
->sh_link
,
7955 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7956 name
, candidate
, (unsigned long) sym
->st_value
);
7958 if (candidate
&& strcmp (candidate
, name
) == 0)
7960 asection
*sec
= flinfo
->sections
[i
];
7962 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
7963 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
7965 printf ("Found symbol with value %8.8lx\n",
7966 (unsigned long) *result
);
7972 /* Hmm, haven't found it yet. perhaps it is a global. */
7973 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
7974 FALSE
, FALSE
, TRUE
);
7978 if (global_entry
->type
== bfd_link_hash_defined
7979 || global_entry
->type
== bfd_link_hash_defweak
)
7981 *result
= (global_entry
->u
.def
.value
7982 + global_entry
->u
.def
.section
->output_section
->vma
7983 + global_entry
->u
.def
.section
->output_offset
);
7985 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7986 global_entry
->root
.string
, (unsigned long) *result
);
7994 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7995 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7996 names like "foo.end" which is the end address of section "foo". */
7999 resolve_section (const char *name
,
8007 for (curr
= sections
; curr
; curr
= curr
->next
)
8008 if (strcmp (curr
->name
, name
) == 0)
8010 *result
= curr
->vma
;
8014 /* Hmm. still haven't found it. try pseudo-section names. */
8015 /* FIXME: This could be coded more efficiently... */
8016 for (curr
= sections
; curr
; curr
= curr
->next
)
8018 len
= strlen (curr
->name
);
8019 if (len
> strlen (name
))
8022 if (strncmp (curr
->name
, name
, len
) == 0)
8024 if (strncmp (".end", name
+ len
, 4) == 0)
8026 *result
= curr
->vma
+ curr
->size
/ bfd_octets_per_byte (abfd
);
8030 /* Insert more pseudo-section names here, if you like. */
8038 undefined_reference (const char *reftype
, const char *name
)
8040 /* xgettext:c-format */
8041 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8046 eval_symbol (bfd_vma
*result
,
8049 struct elf_final_link_info
*flinfo
,
8051 Elf_Internal_Sym
*isymbuf
,
8060 const char *sym
= *symp
;
8062 bfd_boolean symbol_is_section
= FALSE
;
8067 if (len
< 1 || len
> sizeof (symbuf
))
8069 bfd_set_error (bfd_error_invalid_operation
);
8082 *result
= strtoul (sym
, (char **) symp
, 16);
8086 symbol_is_section
= TRUE
;
8090 symlen
= strtol (sym
, (char **) symp
, 10);
8091 sym
= *symp
+ 1; /* Skip the trailing ':'. */
8093 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
8095 bfd_set_error (bfd_error_invalid_operation
);
8099 memcpy (symbuf
, sym
, symlen
);
8100 symbuf
[symlen
] = '\0';
8101 *symp
= sym
+ symlen
;
8103 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8104 the symbol as a section, or vice-versa. so we're pretty liberal in our
8105 interpretation here; section means "try section first", not "must be a
8106 section", and likewise with symbol. */
8108 if (symbol_is_section
)
8110 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
8111 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8112 isymbuf
, locsymcount
))
8114 undefined_reference ("section", symbuf
);
8120 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8121 isymbuf
, locsymcount
)
8122 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8125 undefined_reference ("symbol", symbuf
);
8132 /* All that remains are operators. */
8134 #define UNARY_OP(op) \
8135 if (strncmp (sym, #op, strlen (#op)) == 0) \
8137 sym += strlen (#op); \
8141 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8142 isymbuf, locsymcount, signed_p)) \
8145 *result = op ((bfd_signed_vma) a); \
8151 #define BINARY_OP(op) \
8152 if (strncmp (sym, #op, strlen (#op)) == 0) \
8154 sym += strlen (#op); \
8158 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8159 isymbuf, locsymcount, signed_p)) \
8162 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8163 isymbuf, locsymcount, signed_p)) \
8166 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8196 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
8197 bfd_set_error (bfd_error_invalid_operation
);
8203 put_value (bfd_vma size
,
8204 unsigned long chunksz
,
8209 location
+= (size
- chunksz
);
8211 for (; size
; size
-= chunksz
, location
-= chunksz
)
8216 bfd_put_8 (input_bfd
, x
, location
);
8220 bfd_put_16 (input_bfd
, x
, location
);
8224 bfd_put_32 (input_bfd
, x
, location
);
8225 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8231 bfd_put_64 (input_bfd
, x
, location
);
8232 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8245 get_value (bfd_vma size
,
8246 unsigned long chunksz
,
8253 /* Sanity checks. */
8254 BFD_ASSERT (chunksz
<= sizeof (x
)
8257 && (size
% chunksz
) == 0
8258 && input_bfd
!= NULL
8259 && location
!= NULL
);
8261 if (chunksz
== sizeof (x
))
8263 BFD_ASSERT (size
== chunksz
);
8265 /* Make sure that we do not perform an undefined shift operation.
8266 We know that size == chunksz so there will only be one iteration
8267 of the loop below. */
8271 shift
= 8 * chunksz
;
8273 for (; size
; size
-= chunksz
, location
+= chunksz
)
8278 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
8281 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
8284 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
8288 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
8299 decode_complex_addend (unsigned long *start
, /* in bits */
8300 unsigned long *oplen
, /* in bits */
8301 unsigned long *len
, /* in bits */
8302 unsigned long *wordsz
, /* in bytes */
8303 unsigned long *chunksz
, /* in bytes */
8304 unsigned long *lsb0_p
,
8305 unsigned long *signed_p
,
8306 unsigned long *trunc_p
,
8307 unsigned long encoded
)
8309 * start
= encoded
& 0x3F;
8310 * len
= (encoded
>> 6) & 0x3F;
8311 * oplen
= (encoded
>> 12) & 0x3F;
8312 * wordsz
= (encoded
>> 18) & 0xF;
8313 * chunksz
= (encoded
>> 22) & 0xF;
8314 * lsb0_p
= (encoded
>> 27) & 1;
8315 * signed_p
= (encoded
>> 28) & 1;
8316 * trunc_p
= (encoded
>> 29) & 1;
8319 bfd_reloc_status_type
8320 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
8321 asection
*input_section ATTRIBUTE_UNUSED
,
8323 Elf_Internal_Rela
*rel
,
8326 bfd_vma shift
, x
, mask
;
8327 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
8328 bfd_reloc_status_type r
;
8330 /* Perform this reloc, since it is complex.
8331 (this is not to say that it necessarily refers to a complex
8332 symbol; merely that it is a self-describing CGEN based reloc.
8333 i.e. the addend has the complete reloc information (bit start, end,
8334 word size, etc) encoded within it.). */
8336 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
8337 &chunksz
, &lsb0_p
, &signed_p
,
8338 &trunc_p
, rel
->r_addend
);
8340 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
8343 shift
= (start
+ 1) - len
;
8345 shift
= (8 * wordsz
) - (start
+ len
);
8347 x
= get_value (wordsz
, chunksz
, input_bfd
,
8348 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8351 printf ("Doing complex reloc: "
8352 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8353 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8354 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8355 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
8356 oplen
, (unsigned long) x
, (unsigned long) mask
,
8357 (unsigned long) relocation
);
8362 /* Now do an overflow check. */
8363 r
= bfd_check_overflow ((signed_p
8364 ? complain_overflow_signed
8365 : complain_overflow_unsigned
),
8366 len
, 0, (8 * wordsz
),
8370 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
8373 printf (" relocation: %8.8lx\n"
8374 " shifted mask: %8.8lx\n"
8375 " shifted/masked reloc: %8.8lx\n"
8376 " result: %8.8lx\n",
8377 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
8378 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
8380 put_value (wordsz
, chunksz
, input_bfd
, x
,
8381 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8385 /* Functions to read r_offset from external (target order) reloc
8386 entry. Faster than bfd_getl32 et al, because we let the compiler
8387 know the value is aligned. */
8390 ext32l_r_offset (const void *p
)
8397 const union aligned32
*a
8398 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8400 uint32_t aval
= ( (uint32_t) a
->c
[0]
8401 | (uint32_t) a
->c
[1] << 8
8402 | (uint32_t) a
->c
[2] << 16
8403 | (uint32_t) a
->c
[3] << 24);
8408 ext32b_r_offset (const void *p
)
8415 const union aligned32
*a
8416 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8418 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
8419 | (uint32_t) a
->c
[1] << 16
8420 | (uint32_t) a
->c
[2] << 8
8421 | (uint32_t) a
->c
[3]);
8425 #ifdef BFD_HOST_64_BIT
8427 ext64l_r_offset (const void *p
)
8434 const union aligned64
*a
8435 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8437 uint64_t aval
= ( (uint64_t) a
->c
[0]
8438 | (uint64_t) a
->c
[1] << 8
8439 | (uint64_t) a
->c
[2] << 16
8440 | (uint64_t) a
->c
[3] << 24
8441 | (uint64_t) a
->c
[4] << 32
8442 | (uint64_t) a
->c
[5] << 40
8443 | (uint64_t) a
->c
[6] << 48
8444 | (uint64_t) a
->c
[7] << 56);
8449 ext64b_r_offset (const void *p
)
8456 const union aligned64
*a
8457 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8459 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
8460 | (uint64_t) a
->c
[1] << 48
8461 | (uint64_t) a
->c
[2] << 40
8462 | (uint64_t) a
->c
[3] << 32
8463 | (uint64_t) a
->c
[4] << 24
8464 | (uint64_t) a
->c
[5] << 16
8465 | (uint64_t) a
->c
[6] << 8
8466 | (uint64_t) a
->c
[7]);
8471 /* When performing a relocatable link, the input relocations are
8472 preserved. But, if they reference global symbols, the indices
8473 referenced must be updated. Update all the relocations found in
8477 elf_link_adjust_relocs (bfd
*abfd
,
8479 struct bfd_elf_section_reloc_data
*reldata
,
8483 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8485 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8486 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8487 bfd_vma r_type_mask
;
8489 unsigned int count
= reldata
->count
;
8490 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
8492 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
8494 swap_in
= bed
->s
->swap_reloc_in
;
8495 swap_out
= bed
->s
->swap_reloc_out
;
8497 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
8499 swap_in
= bed
->s
->swap_reloca_in
;
8500 swap_out
= bed
->s
->swap_reloca_out
;
8505 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
8508 if (bed
->s
->arch_size
== 32)
8515 r_type_mask
= 0xffffffff;
8519 erela
= reldata
->hdr
->contents
;
8520 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
8522 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
8525 if (*rel_hash
== NULL
)
8528 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
8530 (*swap_in
) (abfd
, erela
, irela
);
8531 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
8532 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
8533 | (irela
[j
].r_info
& r_type_mask
));
8534 (*swap_out
) (abfd
, irela
, erela
);
8537 if (bed
->elf_backend_update_relocs
)
8538 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
8540 if (sort
&& count
!= 0)
8542 bfd_vma (*ext_r_off
) (const void *);
8545 bfd_byte
*base
, *end
, *p
, *loc
;
8546 bfd_byte
*buf
= NULL
;
8548 if (bed
->s
->arch_size
== 32)
8550 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8551 ext_r_off
= ext32l_r_offset
;
8552 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8553 ext_r_off
= ext32b_r_offset
;
8559 #ifdef BFD_HOST_64_BIT
8560 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8561 ext_r_off
= ext64l_r_offset
;
8562 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8563 ext_r_off
= ext64b_r_offset
;
8569 /* Must use a stable sort here. A modified insertion sort,
8570 since the relocs are mostly sorted already. */
8571 elt_size
= reldata
->hdr
->sh_entsize
;
8572 base
= reldata
->hdr
->contents
;
8573 end
= base
+ count
* elt_size
;
8574 if (elt_size
> sizeof (Elf64_External_Rela
))
8577 /* Ensure the first element is lowest. This acts as a sentinel,
8578 speeding the main loop below. */
8579 r_off
= (*ext_r_off
) (base
);
8580 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
8582 bfd_vma r_off2
= (*ext_r_off
) (p
);
8591 /* Don't just swap *base and *loc as that changes the order
8592 of the original base[0] and base[1] if they happen to
8593 have the same r_offset. */
8594 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
8595 memcpy (onebuf
, loc
, elt_size
);
8596 memmove (base
+ elt_size
, base
, loc
- base
);
8597 memcpy (base
, onebuf
, elt_size
);
8600 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
8602 /* base to p is sorted, *p is next to insert. */
8603 r_off
= (*ext_r_off
) (p
);
8604 /* Search the sorted region for location to insert. */
8606 while (r_off
< (*ext_r_off
) (loc
))
8611 /* Chances are there is a run of relocs to insert here,
8612 from one of more input files. Files are not always
8613 linked in order due to the way elf_link_input_bfd is
8614 called. See pr17666. */
8615 size_t sortlen
= p
- loc
;
8616 bfd_vma r_off2
= (*ext_r_off
) (loc
);
8617 size_t runlen
= elt_size
;
8618 size_t buf_size
= 96 * 1024;
8619 while (p
+ runlen
< end
8620 && (sortlen
<= buf_size
8621 || runlen
+ elt_size
<= buf_size
)
8622 && r_off2
> (*ext_r_off
) (p
+ runlen
))
8626 buf
= bfd_malloc (buf_size
);
8630 if (runlen
< sortlen
)
8632 memcpy (buf
, p
, runlen
);
8633 memmove (loc
+ runlen
, loc
, sortlen
);
8634 memcpy (loc
, buf
, runlen
);
8638 memcpy (buf
, loc
, sortlen
);
8639 memmove (loc
, p
, runlen
);
8640 memcpy (loc
+ runlen
, buf
, sortlen
);
8642 p
+= runlen
- elt_size
;
8645 /* Hashes are no longer valid. */
8646 free (reldata
->hashes
);
8647 reldata
->hashes
= NULL
;
8653 struct elf_link_sort_rela
8659 enum elf_reloc_type_class type
;
8660 /* We use this as an array of size int_rels_per_ext_rel. */
8661 Elf_Internal_Rela rela
[1];
8665 elf_link_sort_cmp1 (const void *A
, const void *B
)
8667 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8668 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8669 int relativea
, relativeb
;
8671 relativea
= a
->type
== reloc_class_relative
;
8672 relativeb
= b
->type
== reloc_class_relative
;
8674 if (relativea
< relativeb
)
8676 if (relativea
> relativeb
)
8678 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
8680 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
8682 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8684 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8690 elf_link_sort_cmp2 (const void *A
, const void *B
)
8692 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8693 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8695 if (a
->type
< b
->type
)
8697 if (a
->type
> b
->type
)
8699 if (a
->u
.offset
< b
->u
.offset
)
8701 if (a
->u
.offset
> b
->u
.offset
)
8703 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8705 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8711 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
8713 asection
*dynamic_relocs
;
8716 bfd_size_type count
, size
;
8717 size_t i
, ret
, sort_elt
, ext_size
;
8718 bfd_byte
*sort
, *s_non_relative
, *p
;
8719 struct elf_link_sort_rela
*sq
;
8720 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8721 int i2e
= bed
->s
->int_rels_per_ext_rel
;
8722 unsigned int opb
= bfd_octets_per_byte (abfd
);
8723 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8724 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8725 struct bfd_link_order
*lo
;
8727 bfd_boolean use_rela
;
8729 /* Find a dynamic reloc section. */
8730 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
8731 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
8732 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
8733 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8735 bfd_boolean use_rela_initialised
= FALSE
;
8737 /* This is just here to stop gcc from complaining.
8738 Its initialization checking code is not perfect. */
8741 /* Both sections are present. Examine the sizes
8742 of the indirect sections to help us choose. */
8743 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8744 if (lo
->type
== bfd_indirect_link_order
)
8746 asection
*o
= lo
->u
.indirect
.section
;
8748 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8750 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8751 /* Section size is divisible by both rel and rela sizes.
8752 It is of no help to us. */
8756 /* Section size is only divisible by rela. */
8757 if (use_rela_initialised
&& (use_rela
== FALSE
))
8759 _bfd_error_handler (_("%B: Unable to sort relocs - "
8760 "they are in more than one size"),
8762 bfd_set_error (bfd_error_invalid_operation
);
8768 use_rela_initialised
= TRUE
;
8772 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8774 /* Section size is only divisible by rel. */
8775 if (use_rela_initialised
&& (use_rela
== TRUE
))
8777 _bfd_error_handler (_("%B: Unable to sort relocs - "
8778 "they are in more than one size"),
8780 bfd_set_error (bfd_error_invalid_operation
);
8786 use_rela_initialised
= TRUE
;
8791 /* The section size is not divisible by either -
8792 something is wrong. */
8793 _bfd_error_handler (_("%B: Unable to sort relocs - "
8794 "they are of an unknown size"), abfd
);
8795 bfd_set_error (bfd_error_invalid_operation
);
8800 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8801 if (lo
->type
== bfd_indirect_link_order
)
8803 asection
*o
= lo
->u
.indirect
.section
;
8805 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8807 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8808 /* Section size is divisible by both rel and rela sizes.
8809 It is of no help to us. */
8813 /* Section size is only divisible by rela. */
8814 if (use_rela_initialised
&& (use_rela
== FALSE
))
8816 _bfd_error_handler (_("%B: Unable to sort relocs - "
8817 "they are in more than one size"),
8819 bfd_set_error (bfd_error_invalid_operation
);
8825 use_rela_initialised
= TRUE
;
8829 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8831 /* Section size is only divisible by rel. */
8832 if (use_rela_initialised
&& (use_rela
== TRUE
))
8834 _bfd_error_handler (_("%B: Unable to sort relocs - "
8835 "they are in more than one size"),
8837 bfd_set_error (bfd_error_invalid_operation
);
8843 use_rela_initialised
= TRUE
;
8848 /* The section size is not divisible by either -
8849 something is wrong. */
8850 _bfd_error_handler (_("%B: Unable to sort relocs - "
8851 "they are of an unknown size"), abfd
);
8852 bfd_set_error (bfd_error_invalid_operation
);
8857 if (! use_rela_initialised
)
8861 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
8863 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8870 dynamic_relocs
= rela_dyn
;
8871 ext_size
= bed
->s
->sizeof_rela
;
8872 swap_in
= bed
->s
->swap_reloca_in
;
8873 swap_out
= bed
->s
->swap_reloca_out
;
8877 dynamic_relocs
= rel_dyn
;
8878 ext_size
= bed
->s
->sizeof_rel
;
8879 swap_in
= bed
->s
->swap_reloc_in
;
8880 swap_out
= bed
->s
->swap_reloc_out
;
8884 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8885 if (lo
->type
== bfd_indirect_link_order
)
8886 size
+= lo
->u
.indirect
.section
->size
;
8888 if (size
!= dynamic_relocs
->size
)
8891 sort_elt
= (sizeof (struct elf_link_sort_rela
)
8892 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
8894 count
= dynamic_relocs
->size
/ ext_size
;
8897 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
8901 (*info
->callbacks
->warning
)
8902 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
8906 if (bed
->s
->arch_size
== 32)
8907 r_sym_mask
= ~(bfd_vma
) 0xff;
8909 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
8911 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8912 if (lo
->type
== bfd_indirect_link_order
)
8914 bfd_byte
*erel
, *erelend
;
8915 asection
*o
= lo
->u
.indirect
.section
;
8917 if (o
->contents
== NULL
&& o
->size
!= 0)
8919 /* This is a reloc section that is being handled as a normal
8920 section. See bfd_section_from_shdr. We can't combine
8921 relocs in this case. */
8926 erelend
= o
->contents
+ o
->size
;
8927 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
8929 while (erel
< erelend
)
8931 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8933 (*swap_in
) (abfd
, erel
, s
->rela
);
8934 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
8935 s
->u
.sym_mask
= r_sym_mask
;
8941 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
8943 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
8945 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8946 if (s
->type
!= reloc_class_relative
)
8952 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
8953 for (; i
< count
; i
++, p
+= sort_elt
)
8955 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
8956 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
8958 sp
->u
.offset
= sq
->rela
->r_offset
;
8961 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
8963 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
8964 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
8966 /* We have plt relocs in .rela.dyn. */
8967 sq
= (struct elf_link_sort_rela
*) sort
;
8968 for (i
= 0; i
< count
; i
++)
8969 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
8971 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
8973 struct bfd_link_order
**plo
;
8974 /* Put srelplt link_order last. This is so the output_offset
8975 set in the next loop is correct for DT_JMPREL. */
8976 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
8977 if ((*plo
)->type
== bfd_indirect_link_order
8978 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
8984 plo
= &(*plo
)->next
;
8987 dynamic_relocs
->map_tail
.link_order
= lo
;
8992 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8993 if (lo
->type
== bfd_indirect_link_order
)
8995 bfd_byte
*erel
, *erelend
;
8996 asection
*o
= lo
->u
.indirect
.section
;
8999 erelend
= o
->contents
+ o
->size
;
9000 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
9001 while (erel
< erelend
)
9003 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9004 (*swap_out
) (abfd
, s
->rela
, erel
);
9011 *psec
= dynamic_relocs
;
9015 /* Add a symbol to the output symbol string table. */
9018 elf_link_output_symstrtab (struct elf_final_link_info
*flinfo
,
9020 Elf_Internal_Sym
*elfsym
,
9021 asection
*input_sec
,
9022 struct elf_link_hash_entry
*h
)
9024 int (*output_symbol_hook
)
9025 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
9026 struct elf_link_hash_entry
*);
9027 struct elf_link_hash_table
*hash_table
;
9028 const struct elf_backend_data
*bed
;
9029 bfd_size_type strtabsize
;
9031 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9033 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9034 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
9035 if (output_symbol_hook
!= NULL
)
9037 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
9044 || (input_sec
->flags
& SEC_EXCLUDE
))
9045 elfsym
->st_name
= (unsigned long) -1;
9048 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9049 to get the final offset for st_name. */
9051 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
9053 if (elfsym
->st_name
== (unsigned long) -1)
9057 hash_table
= elf_hash_table (flinfo
->info
);
9058 strtabsize
= hash_table
->strtabsize
;
9059 if (strtabsize
<= hash_table
->strtabcount
)
9061 strtabsize
+= strtabsize
;
9062 hash_table
->strtabsize
= strtabsize
;
9063 strtabsize
*= sizeof (*hash_table
->strtab
);
9065 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
9067 if (hash_table
->strtab
== NULL
)
9070 hash_table
->strtab
[hash_table
->strtabcount
].sym
= *elfsym
;
9071 hash_table
->strtab
[hash_table
->strtabcount
].dest_index
9072 = hash_table
->strtabcount
;
9073 hash_table
->strtab
[hash_table
->strtabcount
].destshndx_index
9074 = flinfo
->symshndxbuf
? bfd_get_symcount (flinfo
->output_bfd
) : 0;
9076 bfd_get_symcount (flinfo
->output_bfd
) += 1;
9077 hash_table
->strtabcount
+= 1;
9082 /* Swap symbols out to the symbol table and flush the output symbols to
9086 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
9088 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
9091 const struct elf_backend_data
*bed
;
9093 Elf_Internal_Shdr
*hdr
;
9097 if (!hash_table
->strtabcount
)
9100 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9102 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9104 amt
= bed
->s
->sizeof_sym
* hash_table
->strtabcount
;
9105 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
9109 if (flinfo
->symshndxbuf
)
9111 amt
= sizeof (Elf_External_Sym_Shndx
);
9112 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
9113 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
9114 if (flinfo
->symshndxbuf
== NULL
)
9121 for (i
= 0; i
< hash_table
->strtabcount
; i
++)
9123 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
9124 if (elfsym
->sym
.st_name
== (unsigned long) -1)
9125 elfsym
->sym
.st_name
= 0;
9128 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
9129 elfsym
->sym
.st_name
);
9130 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
9131 ((bfd_byte
*) symbuf
9132 + (elfsym
->dest_index
9133 * bed
->s
->sizeof_sym
)),
9134 (flinfo
->symshndxbuf
9135 + elfsym
->destshndx_index
));
9138 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
9139 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
9140 amt
= hash_table
->strtabcount
* bed
->s
->sizeof_sym
;
9141 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
9142 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
9144 hdr
->sh_size
+= amt
;
9152 free (hash_table
->strtab
);
9153 hash_table
->strtab
= NULL
;
9158 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9161 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
9163 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
9164 && sym
->st_shndx
< SHN_LORESERVE
)
9166 /* The gABI doesn't support dynamic symbols in output sections
9169 /* xgettext:c-format */
9170 (_("%B: Too many sections: %d (>= %d)"),
9171 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
9172 bfd_set_error (bfd_error_nonrepresentable_section
);
9178 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9179 allowing an unsatisfied unversioned symbol in the DSO to match a
9180 versioned symbol that would normally require an explicit version.
9181 We also handle the case that a DSO references a hidden symbol
9182 which may be satisfied by a versioned symbol in another DSO. */
9185 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
9186 const struct elf_backend_data
*bed
,
9187 struct elf_link_hash_entry
*h
)
9190 struct elf_link_loaded_list
*loaded
;
9192 if (!is_elf_hash_table (info
->hash
))
9195 /* Check indirect symbol. */
9196 while (h
->root
.type
== bfd_link_hash_indirect
)
9197 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9199 switch (h
->root
.type
)
9205 case bfd_link_hash_undefined
:
9206 case bfd_link_hash_undefweak
:
9207 abfd
= h
->root
.u
.undef
.abfd
;
9209 || (abfd
->flags
& DYNAMIC
) == 0
9210 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
9214 case bfd_link_hash_defined
:
9215 case bfd_link_hash_defweak
:
9216 abfd
= h
->root
.u
.def
.section
->owner
;
9219 case bfd_link_hash_common
:
9220 abfd
= h
->root
.u
.c
.p
->section
->owner
;
9223 BFD_ASSERT (abfd
!= NULL
);
9225 for (loaded
= elf_hash_table (info
)->loaded
;
9227 loaded
= loaded
->next
)
9230 Elf_Internal_Shdr
*hdr
;
9234 Elf_Internal_Shdr
*versymhdr
;
9235 Elf_Internal_Sym
*isym
;
9236 Elf_Internal_Sym
*isymend
;
9237 Elf_Internal_Sym
*isymbuf
;
9238 Elf_External_Versym
*ever
;
9239 Elf_External_Versym
*extversym
;
9241 input
= loaded
->abfd
;
9243 /* We check each DSO for a possible hidden versioned definition. */
9245 || (input
->flags
& DYNAMIC
) == 0
9246 || elf_dynversym (input
) == 0)
9249 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
9251 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9252 if (elf_bad_symtab (input
))
9254 extsymcount
= symcount
;
9259 extsymcount
= symcount
- hdr
->sh_info
;
9260 extsymoff
= hdr
->sh_info
;
9263 if (extsymcount
== 0)
9266 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
9268 if (isymbuf
== NULL
)
9271 /* Read in any version definitions. */
9272 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
9273 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
9274 if (extversym
== NULL
)
9277 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
9278 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
9279 != versymhdr
->sh_size
))
9287 ever
= extversym
+ extsymoff
;
9288 isymend
= isymbuf
+ extsymcount
;
9289 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
9292 Elf_Internal_Versym iver
;
9293 unsigned short version_index
;
9295 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
9296 || isym
->st_shndx
== SHN_UNDEF
)
9299 name
= bfd_elf_string_from_elf_section (input
,
9302 if (strcmp (name
, h
->root
.root
.string
) != 0)
9305 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
9307 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
9309 && h
->forced_local
))
9311 /* If we have a non-hidden versioned sym, then it should
9312 have provided a definition for the undefined sym unless
9313 it is defined in a non-shared object and forced local.
9318 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
9319 if (version_index
== 1 || version_index
== 2)
9321 /* This is the base or first version. We can use it. */
9335 /* Convert ELF common symbol TYPE. */
9338 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
9340 /* Commom symbol can only appear in relocatable link. */
9341 if (!bfd_link_relocatable (info
))
9343 switch (info
->elf_stt_common
)
9347 case elf_stt_common
:
9350 case no_elf_stt_common
:
9357 /* Add an external symbol to the symbol table. This is called from
9358 the hash table traversal routine. When generating a shared object,
9359 we go through the symbol table twice. The first time we output
9360 anything that might have been forced to local scope in a version
9361 script. The second time we output the symbols that are still
9365 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
9367 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
9368 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
9369 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
9371 Elf_Internal_Sym sym
;
9372 asection
*input_sec
;
9373 const struct elf_backend_data
*bed
;
9378 if (h
->root
.type
== bfd_link_hash_warning
)
9380 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9381 if (h
->root
.type
== bfd_link_hash_new
)
9385 /* Decide whether to output this symbol in this pass. */
9386 if (eoinfo
->localsyms
)
9388 if (!h
->forced_local
)
9393 if (h
->forced_local
)
9397 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9399 if (h
->root
.type
== bfd_link_hash_undefined
)
9401 /* If we have an undefined symbol reference here then it must have
9402 come from a shared library that is being linked in. (Undefined
9403 references in regular files have already been handled unless
9404 they are in unreferenced sections which are removed by garbage
9406 bfd_boolean ignore_undef
= FALSE
;
9408 /* Some symbols may be special in that the fact that they're
9409 undefined can be safely ignored - let backend determine that. */
9410 if (bed
->elf_backend_ignore_undef_symbol
)
9411 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
9413 /* If we are reporting errors for this situation then do so now. */
9416 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
9417 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
9418 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
9419 (*flinfo
->info
->callbacks
->undefined_symbol
)
9420 (flinfo
->info
, h
->root
.root
.string
,
9421 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
,
9423 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
);
9425 /* Strip a global symbol defined in a discarded section. */
9430 /* We should also warn if a forced local symbol is referenced from
9431 shared libraries. */
9432 if (bfd_link_executable (flinfo
->info
)
9437 && h
->ref_dynamic_nonweak
9438 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
9442 struct elf_link_hash_entry
*hi
= h
;
9444 /* Check indirect symbol. */
9445 while (hi
->root
.type
== bfd_link_hash_indirect
)
9446 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
9448 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
9449 /* xgettext:c-format */
9450 msg
= _("%B: internal symbol `%s' in %B is referenced by DSO");
9451 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
9452 /* xgettext:c-format */
9453 msg
= _("%B: hidden symbol `%s' in %B is referenced by DSO");
9455 /* xgettext:c-format */
9456 msg
= _("%B: local symbol `%s' in %B is referenced by DSO");
9457 def_bfd
= flinfo
->output_bfd
;
9458 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
9459 def_bfd
= hi
->root
.u
.def
.section
->owner
;
9460 _bfd_error_handler (msg
, flinfo
->output_bfd
, def_bfd
,
9461 h
->root
.root
.string
);
9462 bfd_set_error (bfd_error_bad_value
);
9463 eoinfo
->failed
= TRUE
;
9467 /* We don't want to output symbols that have never been mentioned by
9468 a regular file, or that we have been told to strip. However, if
9469 h->indx is set to -2, the symbol is used by a reloc and we must
9474 else if ((h
->def_dynamic
9476 || h
->root
.type
== bfd_link_hash_new
)
9480 else if (flinfo
->info
->strip
== strip_all
)
9482 else if (flinfo
->info
->strip
== strip_some
9483 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
9484 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
9486 else if ((h
->root
.type
== bfd_link_hash_defined
9487 || h
->root
.type
== bfd_link_hash_defweak
)
9488 && ((flinfo
->info
->strip_discarded
9489 && discarded_section (h
->root
.u
.def
.section
))
9490 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
9491 && h
->root
.u
.def
.section
->owner
!= NULL
9492 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
9494 else if ((h
->root
.type
== bfd_link_hash_undefined
9495 || h
->root
.type
== bfd_link_hash_undefweak
)
9496 && h
->root
.u
.undef
.abfd
!= NULL
9497 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
9502 /* If we're stripping it, and it's not a dynamic symbol, there's
9503 nothing else to do. However, if it is a forced local symbol or
9504 an ifunc symbol we need to give the backend finish_dynamic_symbol
9505 function a chance to make it dynamic. */
9508 && type
!= STT_GNU_IFUNC
9509 && !h
->forced_local
)
9513 sym
.st_size
= h
->size
;
9514 sym
.st_other
= h
->other
;
9515 switch (h
->root
.type
)
9518 case bfd_link_hash_new
:
9519 case bfd_link_hash_warning
:
9523 case bfd_link_hash_undefined
:
9524 case bfd_link_hash_undefweak
:
9525 input_sec
= bfd_und_section_ptr
;
9526 sym
.st_shndx
= SHN_UNDEF
;
9529 case bfd_link_hash_defined
:
9530 case bfd_link_hash_defweak
:
9532 input_sec
= h
->root
.u
.def
.section
;
9533 if (input_sec
->output_section
!= NULL
)
9536 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
9537 input_sec
->output_section
);
9538 if (sym
.st_shndx
== SHN_BAD
)
9541 /* xgettext:c-format */
9542 (_("%B: could not find output section %A for input section %A"),
9543 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
9544 bfd_set_error (bfd_error_nonrepresentable_section
);
9545 eoinfo
->failed
= TRUE
;
9549 /* ELF symbols in relocatable files are section relative,
9550 but in nonrelocatable files they are virtual
9552 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
9553 if (!bfd_link_relocatable (flinfo
->info
))
9555 sym
.st_value
+= input_sec
->output_section
->vma
;
9556 if (h
->type
== STT_TLS
)
9558 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
9559 if (tls_sec
!= NULL
)
9560 sym
.st_value
-= tls_sec
->vma
;
9566 BFD_ASSERT (input_sec
->owner
== NULL
9567 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
9568 sym
.st_shndx
= SHN_UNDEF
;
9569 input_sec
= bfd_und_section_ptr
;
9574 case bfd_link_hash_common
:
9575 input_sec
= h
->root
.u
.c
.p
->section
;
9576 sym
.st_shndx
= bed
->common_section_index (input_sec
);
9577 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
9580 case bfd_link_hash_indirect
:
9581 /* These symbols are created by symbol versioning. They point
9582 to the decorated version of the name. For example, if the
9583 symbol foo@@GNU_1.2 is the default, which should be used when
9584 foo is used with no version, then we add an indirect symbol
9585 foo which points to foo@@GNU_1.2. We ignore these symbols,
9586 since the indirected symbol is already in the hash table. */
9590 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
9591 switch (h
->root
.type
)
9593 case bfd_link_hash_common
:
9594 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9596 case bfd_link_hash_defined
:
9597 case bfd_link_hash_defweak
:
9598 if (bed
->common_definition (&sym
))
9599 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9603 case bfd_link_hash_undefined
:
9604 case bfd_link_hash_undefweak
:
9610 if (h
->forced_local
)
9612 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
9613 /* Turn off visibility on local symbol. */
9614 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
9616 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9617 else if (h
->unique_global
&& h
->def_regular
)
9618 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
9619 else if (h
->root
.type
== bfd_link_hash_undefweak
9620 || h
->root
.type
== bfd_link_hash_defweak
)
9621 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
9623 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
9624 sym
.st_target_internal
= h
->target_internal
;
9626 /* Give the processor backend a chance to tweak the symbol value,
9627 and also to finish up anything that needs to be done for this
9628 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9629 forced local syms when non-shared is due to a historical quirk.
9630 STT_GNU_IFUNC symbol must go through PLT. */
9631 if ((h
->type
== STT_GNU_IFUNC
9633 && !bfd_link_relocatable (flinfo
->info
))
9634 || ((h
->dynindx
!= -1
9636 && ((bfd_link_pic (flinfo
->info
)
9637 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
9638 || h
->root
.type
!= bfd_link_hash_undefweak
))
9639 || !h
->forced_local
)
9640 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
9642 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
9643 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
9645 eoinfo
->failed
= TRUE
;
9650 /* If we are marking the symbol as undefined, and there are no
9651 non-weak references to this symbol from a regular object, then
9652 mark the symbol as weak undefined; if there are non-weak
9653 references, mark the symbol as strong. We can't do this earlier,
9654 because it might not be marked as undefined until the
9655 finish_dynamic_symbol routine gets through with it. */
9656 if (sym
.st_shndx
== SHN_UNDEF
9658 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
9659 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
9662 type
= ELF_ST_TYPE (sym
.st_info
);
9664 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9665 if (type
== STT_GNU_IFUNC
)
9668 if (h
->ref_regular_nonweak
)
9669 bindtype
= STB_GLOBAL
;
9671 bindtype
= STB_WEAK
;
9672 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
9675 /* If this is a symbol defined in a dynamic library, don't use the
9676 symbol size from the dynamic library. Relinking an executable
9677 against a new library may introduce gratuitous changes in the
9678 executable's symbols if we keep the size. */
9679 if (sym
.st_shndx
== SHN_UNDEF
9684 /* If a non-weak symbol with non-default visibility is not defined
9685 locally, it is a fatal error. */
9686 if (!bfd_link_relocatable (flinfo
->info
)
9687 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
9688 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
9689 && h
->root
.type
== bfd_link_hash_undefined
9694 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
9695 /* xgettext:c-format */
9696 msg
= _("%B: protected symbol `%s' isn't defined");
9697 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
9698 /* xgettext:c-format */
9699 msg
= _("%B: internal symbol `%s' isn't defined");
9701 /* xgettext:c-format */
9702 msg
= _("%B: hidden symbol `%s' isn't defined");
9703 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
9704 bfd_set_error (bfd_error_bad_value
);
9705 eoinfo
->failed
= TRUE
;
9709 /* If this symbol should be put in the .dynsym section, then put it
9710 there now. We already know the symbol index. We also fill in
9711 the entry in the .hash section. */
9712 if (elf_hash_table (flinfo
->info
)->dynsym
!= NULL
9714 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
)
9718 /* Since there is no version information in the dynamic string,
9719 if there is no version info in symbol version section, we will
9720 have a run-time problem if not linking executable, referenced
9721 by shared library, or not bound locally. */
9722 if (h
->verinfo
.verdef
== NULL
9723 && (!bfd_link_executable (flinfo
->info
)
9725 || !h
->def_regular
))
9727 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
9729 if (p
&& p
[1] != '\0')
9732 /* xgettext:c-format */
9733 (_("%B: No symbol version section for versioned symbol `%s'"),
9734 flinfo
->output_bfd
, h
->root
.root
.string
);
9735 eoinfo
->failed
= TRUE
;
9740 sym
.st_name
= h
->dynstr_index
;
9741 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
9742 + h
->dynindx
* bed
->s
->sizeof_sym
);
9743 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
9745 eoinfo
->failed
= TRUE
;
9748 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
9750 if (flinfo
->hash_sec
!= NULL
)
9752 size_t hash_entry_size
;
9753 bfd_byte
*bucketpos
;
9758 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
9759 bucket
= h
->u
.elf_hash_value
% bucketcount
;
9762 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
9763 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
9764 + (bucket
+ 2) * hash_entry_size
);
9765 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
9766 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
9768 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
9769 ((bfd_byte
*) flinfo
->hash_sec
->contents
9770 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
9773 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
9775 Elf_Internal_Versym iversym
;
9776 Elf_External_Versym
*eversym
;
9778 if (!h
->def_regular
)
9780 if (h
->verinfo
.verdef
== NULL
9781 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
9782 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
9783 iversym
.vs_vers
= 0;
9785 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
9789 if (h
->verinfo
.vertree
== NULL
)
9790 iversym
.vs_vers
= 1;
9792 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
9793 if (flinfo
->info
->create_default_symver
)
9797 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9799 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
9800 iversym
.vs_vers
|= VERSYM_HIDDEN
;
9802 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
9803 eversym
+= h
->dynindx
;
9804 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
9808 /* If the symbol is undefined, and we didn't output it to .dynsym,
9809 strip it from .symtab too. Obviously we can't do this for
9810 relocatable output or when needed for --emit-relocs. */
9811 else if (input_sec
== bfd_und_section_ptr
9813 && !bfd_link_relocatable (flinfo
->info
))
9815 /* Also strip others that we couldn't earlier due to dynamic symbol
9819 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
9822 /* Output a FILE symbol so that following locals are not associated
9823 with the wrong input file. We need one for forced local symbols
9824 if we've seen more than one FILE symbol or when we have exactly
9825 one FILE symbol but global symbols are present in a file other
9826 than the one with the FILE symbol. We also need one if linker
9827 defined symbols are present. In practice these conditions are
9828 always met, so just emit the FILE symbol unconditionally. */
9829 if (eoinfo
->localsyms
9830 && !eoinfo
->file_sym_done
9831 && eoinfo
->flinfo
->filesym_count
!= 0)
9833 Elf_Internal_Sym fsym
;
9835 memset (&fsym
, 0, sizeof (fsym
));
9836 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
9837 fsym
.st_shndx
= SHN_ABS
;
9838 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
9839 bfd_und_section_ptr
, NULL
))
9842 eoinfo
->file_sym_done
= TRUE
;
9845 indx
= bfd_get_symcount (flinfo
->output_bfd
);
9846 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
9850 eoinfo
->failed
= TRUE
;
9855 else if (h
->indx
== -2)
9861 /* Return TRUE if special handling is done for relocs in SEC against
9862 symbols defined in discarded sections. */
9865 elf_section_ignore_discarded_relocs (asection
*sec
)
9867 const struct elf_backend_data
*bed
;
9869 switch (sec
->sec_info_type
)
9871 case SEC_INFO_TYPE_STABS
:
9872 case SEC_INFO_TYPE_EH_FRAME
:
9873 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
9879 bed
= get_elf_backend_data (sec
->owner
);
9880 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
9881 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
9887 /* Return a mask saying how ld should treat relocations in SEC against
9888 symbols defined in discarded sections. If this function returns
9889 COMPLAIN set, ld will issue a warning message. If this function
9890 returns PRETEND set, and the discarded section was link-once and the
9891 same size as the kept link-once section, ld will pretend that the
9892 symbol was actually defined in the kept section. Otherwise ld will
9893 zero the reloc (at least that is the intent, but some cooperation by
9894 the target dependent code is needed, particularly for REL targets). */
9897 _bfd_elf_default_action_discarded (asection
*sec
)
9899 if (sec
->flags
& SEC_DEBUGGING
)
9902 if (strcmp (".eh_frame", sec
->name
) == 0)
9905 if (strcmp (".gcc_except_table", sec
->name
) == 0)
9908 return COMPLAIN
| PRETEND
;
9911 /* Find a match between a section and a member of a section group. */
9914 match_group_member (asection
*sec
, asection
*group
,
9915 struct bfd_link_info
*info
)
9917 asection
*first
= elf_next_in_group (group
);
9918 asection
*s
= first
;
9922 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
9925 s
= elf_next_in_group (s
);
9933 /* Check if the kept section of a discarded section SEC can be used
9934 to replace it. Return the replacement if it is OK. Otherwise return
9938 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
9942 kept
= sec
->kept_section
;
9945 if ((kept
->flags
& SEC_GROUP
) != 0)
9946 kept
= match_group_member (sec
, kept
, info
);
9948 && ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
9949 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
)))
9951 sec
->kept_section
= kept
;
9956 /* Link an input file into the linker output file. This function
9957 handles all the sections and relocations of the input file at once.
9958 This is so that we only have to read the local symbols once, and
9959 don't have to keep them in memory. */
9962 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
9964 int (*relocate_section
)
9965 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
9966 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
9968 Elf_Internal_Shdr
*symtab_hdr
;
9971 Elf_Internal_Sym
*isymbuf
;
9972 Elf_Internal_Sym
*isym
;
9973 Elf_Internal_Sym
*isymend
;
9975 asection
**ppsection
;
9977 const struct elf_backend_data
*bed
;
9978 struct elf_link_hash_entry
**sym_hashes
;
9979 bfd_size_type address_size
;
9980 bfd_vma r_type_mask
;
9982 bfd_boolean have_file_sym
= FALSE
;
9984 output_bfd
= flinfo
->output_bfd
;
9985 bed
= get_elf_backend_data (output_bfd
);
9986 relocate_section
= bed
->elf_backend_relocate_section
;
9988 /* If this is a dynamic object, we don't want to do anything here:
9989 we don't want the local symbols, and we don't want the section
9991 if ((input_bfd
->flags
& DYNAMIC
) != 0)
9994 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
9995 if (elf_bad_symtab (input_bfd
))
9997 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10002 locsymcount
= symtab_hdr
->sh_info
;
10003 extsymoff
= symtab_hdr
->sh_info
;
10006 /* Read the local symbols. */
10007 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10008 if (isymbuf
== NULL
&& locsymcount
!= 0)
10010 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
10011 flinfo
->internal_syms
,
10012 flinfo
->external_syms
,
10013 flinfo
->locsym_shndx
);
10014 if (isymbuf
== NULL
)
10018 /* Find local symbol sections and adjust values of symbols in
10019 SEC_MERGE sections. Write out those local symbols we know are
10020 going into the output file. */
10021 isymend
= isymbuf
+ locsymcount
;
10022 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
10024 isym
++, pindex
++, ppsection
++)
10028 Elf_Internal_Sym osym
;
10034 if (elf_bad_symtab (input_bfd
))
10036 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
10043 if (isym
->st_shndx
== SHN_UNDEF
)
10044 isec
= bfd_und_section_ptr
;
10045 else if (isym
->st_shndx
== SHN_ABS
)
10046 isec
= bfd_abs_section_ptr
;
10047 else if (isym
->st_shndx
== SHN_COMMON
)
10048 isec
= bfd_com_section_ptr
;
10051 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
10054 /* Don't attempt to output symbols with st_shnx in the
10055 reserved range other than SHN_ABS and SHN_COMMON. */
10059 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
10060 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
10062 _bfd_merged_section_offset (output_bfd
, &isec
,
10063 elf_section_data (isec
)->sec_info
,
10069 /* Don't output the first, undefined, symbol. In fact, don't
10070 output any undefined local symbol. */
10071 if (isec
== bfd_und_section_ptr
)
10074 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
10076 /* We never output section symbols. Instead, we use the
10077 section symbol of the corresponding section in the output
10082 /* If we are stripping all symbols, we don't want to output this
10084 if (flinfo
->info
->strip
== strip_all
)
10087 /* If we are discarding all local symbols, we don't want to
10088 output this one. If we are generating a relocatable output
10089 file, then some of the local symbols may be required by
10090 relocs; we output them below as we discover that they are
10092 if (flinfo
->info
->discard
== discard_all
)
10095 /* If this symbol is defined in a section which we are
10096 discarding, we don't need to keep it. */
10097 if (isym
->st_shndx
!= SHN_UNDEF
10098 && isym
->st_shndx
< SHN_LORESERVE
10099 && bfd_section_removed_from_list (output_bfd
,
10100 isec
->output_section
))
10103 /* Get the name of the symbol. */
10104 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
10109 /* See if we are discarding symbols with this name. */
10110 if ((flinfo
->info
->strip
== strip_some
10111 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
10113 || (((flinfo
->info
->discard
== discard_sec_merge
10114 && (isec
->flags
& SEC_MERGE
)
10115 && !bfd_link_relocatable (flinfo
->info
))
10116 || flinfo
->info
->discard
== discard_l
)
10117 && bfd_is_local_label_name (input_bfd
, name
)))
10120 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
10122 if (input_bfd
->lto_output
)
10123 /* -flto puts a temp file name here. This means builds
10124 are not reproducible. Discard the symbol. */
10126 have_file_sym
= TRUE
;
10127 flinfo
->filesym_count
+= 1;
10129 if (!have_file_sym
)
10131 /* In the absence of debug info, bfd_find_nearest_line uses
10132 FILE symbols to determine the source file for local
10133 function symbols. Provide a FILE symbol here if input
10134 files lack such, so that their symbols won't be
10135 associated with a previous input file. It's not the
10136 source file, but the best we can do. */
10137 have_file_sym
= TRUE
;
10138 flinfo
->filesym_count
+= 1;
10139 memset (&osym
, 0, sizeof (osym
));
10140 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10141 osym
.st_shndx
= SHN_ABS
;
10142 if (!elf_link_output_symstrtab (flinfo
,
10143 (input_bfd
->lto_output
? NULL
10144 : input_bfd
->filename
),
10145 &osym
, bfd_abs_section_ptr
,
10152 /* Adjust the section index for the output file. */
10153 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10154 isec
->output_section
);
10155 if (osym
.st_shndx
== SHN_BAD
)
10158 /* ELF symbols in relocatable files are section relative, but
10159 in executable files they are virtual addresses. Note that
10160 this code assumes that all ELF sections have an associated
10161 BFD section with a reasonable value for output_offset; below
10162 we assume that they also have a reasonable value for
10163 output_section. Any special sections must be set up to meet
10164 these requirements. */
10165 osym
.st_value
+= isec
->output_offset
;
10166 if (!bfd_link_relocatable (flinfo
->info
))
10168 osym
.st_value
+= isec
->output_section
->vma
;
10169 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
10171 /* STT_TLS symbols are relative to PT_TLS segment base. */
10172 BFD_ASSERT (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
);
10173 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
10177 indx
= bfd_get_symcount (output_bfd
);
10178 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
10185 if (bed
->s
->arch_size
== 32)
10187 r_type_mask
= 0xff;
10193 r_type_mask
= 0xffffffff;
10198 /* Relocate the contents of each section. */
10199 sym_hashes
= elf_sym_hashes (input_bfd
);
10200 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
10202 bfd_byte
*contents
;
10204 if (! o
->linker_mark
)
10206 /* This section was omitted from the link. */
10210 if (bfd_link_relocatable (flinfo
->info
)
10211 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
10213 /* Deal with the group signature symbol. */
10214 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
10215 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
10216 asection
*osec
= o
->output_section
;
10218 if (symndx
>= locsymcount
10219 || (elf_bad_symtab (input_bfd
)
10220 && flinfo
->sections
[symndx
] == NULL
))
10222 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
10223 while (h
->root
.type
== bfd_link_hash_indirect
10224 || h
->root
.type
== bfd_link_hash_warning
)
10225 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10226 /* Arrange for symbol to be output. */
10228 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
10230 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
10232 /* We'll use the output section target_index. */
10233 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10234 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
10238 if (flinfo
->indices
[symndx
] == -1)
10240 /* Otherwise output the local symbol now. */
10241 Elf_Internal_Sym sym
= isymbuf
[symndx
];
10242 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10247 name
= bfd_elf_string_from_elf_section (input_bfd
,
10248 symtab_hdr
->sh_link
,
10253 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10255 if (sym
.st_shndx
== SHN_BAD
)
10258 sym
.st_value
+= o
->output_offset
;
10260 indx
= bfd_get_symcount (output_bfd
);
10261 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
10266 flinfo
->indices
[symndx
] = indx
;
10270 elf_section_data (osec
)->this_hdr
.sh_info
10271 = flinfo
->indices
[symndx
];
10275 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
10276 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
10279 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
10281 /* Section was created by _bfd_elf_link_create_dynamic_sections
10286 /* Get the contents of the section. They have been cached by a
10287 relaxation routine. Note that o is a section in an input
10288 file, so the contents field will not have been set by any of
10289 the routines which work on output files. */
10290 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
10292 contents
= elf_section_data (o
)->this_hdr
.contents
;
10293 if (bed
->caches_rawsize
10295 && o
->rawsize
< o
->size
)
10297 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
10298 contents
= flinfo
->contents
;
10303 contents
= flinfo
->contents
;
10304 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
10308 if ((o
->flags
& SEC_RELOC
) != 0)
10310 Elf_Internal_Rela
*internal_relocs
;
10311 Elf_Internal_Rela
*rel
, *relend
;
10312 int action_discarded
;
10315 /* Get the swapped relocs. */
10317 = _bfd_elf_link_read_relocs (input_bfd
, o
, flinfo
->external_relocs
,
10318 flinfo
->internal_relocs
, FALSE
);
10319 if (internal_relocs
== NULL
10320 && o
->reloc_count
> 0)
10323 /* We need to reverse-copy input .ctors/.dtors sections if
10324 they are placed in .init_array/.finit_array for output. */
10325 if (o
->size
> address_size
10326 && ((strncmp (o
->name
, ".ctors", 6) == 0
10327 && strcmp (o
->output_section
->name
,
10328 ".init_array") == 0)
10329 || (strncmp (o
->name
, ".dtors", 6) == 0
10330 && strcmp (o
->output_section
->name
,
10331 ".fini_array") == 0))
10332 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
10334 if (o
->size
!= o
->reloc_count
* address_size
)
10337 /* xgettext:c-format */
10338 (_("error: %B: size of section %A is not "
10339 "multiple of address size"),
10341 bfd_set_error (bfd_error_on_input
);
10344 o
->flags
|= SEC_ELF_REVERSE_COPY
;
10347 action_discarded
= -1;
10348 if (!elf_section_ignore_discarded_relocs (o
))
10349 action_discarded
= (*bed
->action_discarded
) (o
);
10351 /* Run through the relocs evaluating complex reloc symbols and
10352 looking for relocs against symbols from discarded sections
10353 or section symbols from removed link-once sections.
10354 Complain about relocs against discarded sections. Zero
10355 relocs against removed link-once sections. */
10357 rel
= internal_relocs
;
10358 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10359 for ( ; rel
< relend
; rel
++)
10361 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
10362 unsigned int s_type
;
10363 asection
**ps
, *sec
;
10364 struct elf_link_hash_entry
*h
= NULL
;
10365 const char *sym_name
;
10367 if (r_symndx
== STN_UNDEF
)
10370 if (r_symndx
>= locsymcount
10371 || (elf_bad_symtab (input_bfd
)
10372 && flinfo
->sections
[r_symndx
] == NULL
))
10374 h
= sym_hashes
[r_symndx
- extsymoff
];
10376 /* Badly formatted input files can contain relocs that
10377 reference non-existant symbols. Check here so that
10378 we do not seg fault. */
10383 sprintf_vma (buffer
, rel
->r_info
);
10385 /* xgettext:c-format */
10386 (_("error: %B contains a reloc (0x%s) for section %A "
10387 "that references a non-existent global symbol"),
10388 input_bfd
, o
, buffer
);
10389 bfd_set_error (bfd_error_bad_value
);
10393 while (h
->root
.type
== bfd_link_hash_indirect
10394 || h
->root
.type
== bfd_link_hash_warning
)
10395 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10399 /* If a plugin symbol is referenced from a non-IR file,
10400 mark the symbol as undefined. Note that the
10401 linker may attach linker created dynamic sections
10402 to the plugin bfd. Symbols defined in linker
10403 created sections are not plugin symbols. */
10404 if (h
->root
.non_ir_ref
10405 && (h
->root
.type
== bfd_link_hash_defined
10406 || h
->root
.type
== bfd_link_hash_defweak
)
10407 && (h
->root
.u
.def
.section
->flags
10408 & SEC_LINKER_CREATED
) == 0
10409 && h
->root
.u
.def
.section
->owner
!= NULL
10410 && (h
->root
.u
.def
.section
->owner
->flags
10411 & BFD_PLUGIN
) != 0)
10413 h
->root
.type
= bfd_link_hash_undefined
;
10414 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
10418 if (h
->root
.type
== bfd_link_hash_defined
10419 || h
->root
.type
== bfd_link_hash_defweak
)
10420 ps
= &h
->root
.u
.def
.section
;
10422 sym_name
= h
->root
.root
.string
;
10426 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
10428 s_type
= ELF_ST_TYPE (sym
->st_info
);
10429 ps
= &flinfo
->sections
[r_symndx
];
10430 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
10434 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
10435 && !bfd_link_relocatable (flinfo
->info
))
10438 bfd_vma dot
= (rel
->r_offset
10439 + o
->output_offset
+ o
->output_section
->vma
);
10441 printf ("Encountered a complex symbol!");
10442 printf (" (input_bfd %s, section %s, reloc %ld\n",
10443 input_bfd
->filename
, o
->name
,
10444 (long) (rel
- internal_relocs
));
10445 printf (" symbol: idx %8.8lx, name %s\n",
10446 r_symndx
, sym_name
);
10447 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10448 (unsigned long) rel
->r_info
,
10449 (unsigned long) rel
->r_offset
);
10451 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
10452 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
10455 /* Symbol evaluated OK. Update to absolute value. */
10456 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
10461 if (action_discarded
!= -1 && ps
!= NULL
)
10463 /* Complain if the definition comes from a
10464 discarded section. */
10465 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
10467 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
10468 if (action_discarded
& COMPLAIN
)
10469 (*flinfo
->info
->callbacks
->einfo
)
10470 /* xgettext:c-format */
10471 (_("%X`%s' referenced in section `%A' of %B: "
10472 "defined in discarded section `%A' of %B\n"),
10473 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
10475 /* Try to do the best we can to support buggy old
10476 versions of gcc. Pretend that the symbol is
10477 really defined in the kept linkonce section.
10478 FIXME: This is quite broken. Modifying the
10479 symbol here means we will be changing all later
10480 uses of the symbol, not just in this section. */
10481 if (action_discarded
& PRETEND
)
10485 kept
= _bfd_elf_check_kept_section (sec
,
10497 /* Relocate the section by invoking a back end routine.
10499 The back end routine is responsible for adjusting the
10500 section contents as necessary, and (if using Rela relocs
10501 and generating a relocatable output file) adjusting the
10502 reloc addend as necessary.
10504 The back end routine does not have to worry about setting
10505 the reloc address or the reloc symbol index.
10507 The back end routine is given a pointer to the swapped in
10508 internal symbols, and can access the hash table entries
10509 for the external symbols via elf_sym_hashes (input_bfd).
10511 When generating relocatable output, the back end routine
10512 must handle STB_LOCAL/STT_SECTION symbols specially. The
10513 output symbol is going to be a section symbol
10514 corresponding to the output section, which will require
10515 the addend to be adjusted. */
10517 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
10518 input_bfd
, o
, contents
,
10526 || bfd_link_relocatable (flinfo
->info
)
10527 || flinfo
->info
->emitrelocations
)
10529 Elf_Internal_Rela
*irela
;
10530 Elf_Internal_Rela
*irelaend
, *irelamid
;
10531 bfd_vma last_offset
;
10532 struct elf_link_hash_entry
**rel_hash
;
10533 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
10534 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
10535 unsigned int next_erel
;
10536 bfd_boolean rela_normal
;
10537 struct bfd_elf_section_data
*esdi
, *esdo
;
10539 esdi
= elf_section_data (o
);
10540 esdo
= elf_section_data (o
->output_section
);
10541 rela_normal
= FALSE
;
10543 /* Adjust the reloc addresses and symbol indices. */
10545 irela
= internal_relocs
;
10546 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10547 rel_hash
= esdo
->rel
.hashes
+ esdo
->rel
.count
;
10548 /* We start processing the REL relocs, if any. When we reach
10549 IRELAMID in the loop, we switch to the RELA relocs. */
10551 if (esdi
->rel
.hdr
!= NULL
)
10552 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
10553 * bed
->s
->int_rels_per_ext_rel
);
10554 rel_hash_list
= rel_hash
;
10555 rela_hash_list
= NULL
;
10556 last_offset
= o
->output_offset
;
10557 if (!bfd_link_relocatable (flinfo
->info
))
10558 last_offset
+= o
->output_section
->vma
;
10559 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
10561 unsigned long r_symndx
;
10563 Elf_Internal_Sym sym
;
10565 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
10571 if (irela
== irelamid
)
10573 rel_hash
= esdo
->rela
.hashes
+ esdo
->rela
.count
;
10574 rela_hash_list
= rel_hash
;
10575 rela_normal
= bed
->rela_normal
;
10578 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
10581 if (irela
->r_offset
>= (bfd_vma
) -2)
10583 /* This is a reloc for a deleted entry or somesuch.
10584 Turn it into an R_*_NONE reloc, at the same
10585 offset as the last reloc. elf_eh_frame.c and
10586 bfd_elf_discard_info rely on reloc offsets
10588 irela
->r_offset
= last_offset
;
10590 irela
->r_addend
= 0;
10594 irela
->r_offset
+= o
->output_offset
;
10596 /* Relocs in an executable have to be virtual addresses. */
10597 if (!bfd_link_relocatable (flinfo
->info
))
10598 irela
->r_offset
+= o
->output_section
->vma
;
10600 last_offset
= irela
->r_offset
;
10602 r_symndx
= irela
->r_info
>> r_sym_shift
;
10603 if (r_symndx
== STN_UNDEF
)
10606 if (r_symndx
>= locsymcount
10607 || (elf_bad_symtab (input_bfd
)
10608 && flinfo
->sections
[r_symndx
] == NULL
))
10610 struct elf_link_hash_entry
*rh
;
10611 unsigned long indx
;
10613 /* This is a reloc against a global symbol. We
10614 have not yet output all the local symbols, so
10615 we do not know the symbol index of any global
10616 symbol. We set the rel_hash entry for this
10617 reloc to point to the global hash table entry
10618 for this symbol. The symbol index is then
10619 set at the end of bfd_elf_final_link. */
10620 indx
= r_symndx
- extsymoff
;
10621 rh
= elf_sym_hashes (input_bfd
)[indx
];
10622 while (rh
->root
.type
== bfd_link_hash_indirect
10623 || rh
->root
.type
== bfd_link_hash_warning
)
10624 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
10626 /* Setting the index to -2 tells
10627 elf_link_output_extsym that this symbol is
10628 used by a reloc. */
10629 BFD_ASSERT (rh
->indx
< 0);
10637 /* This is a reloc against a local symbol. */
10640 sym
= isymbuf
[r_symndx
];
10641 sec
= flinfo
->sections
[r_symndx
];
10642 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
10644 /* I suppose the backend ought to fill in the
10645 section of any STT_SECTION symbol against a
10646 processor specific section. */
10647 r_symndx
= STN_UNDEF
;
10648 if (bfd_is_abs_section (sec
))
10650 else if (sec
== NULL
|| sec
->owner
== NULL
)
10652 bfd_set_error (bfd_error_bad_value
);
10657 asection
*osec
= sec
->output_section
;
10659 /* If we have discarded a section, the output
10660 section will be the absolute section. In
10661 case of discarded SEC_MERGE sections, use
10662 the kept section. relocate_section should
10663 have already handled discarded linkonce
10665 if (bfd_is_abs_section (osec
)
10666 && sec
->kept_section
!= NULL
10667 && sec
->kept_section
->output_section
!= NULL
)
10669 osec
= sec
->kept_section
->output_section
;
10670 irela
->r_addend
-= osec
->vma
;
10673 if (!bfd_is_abs_section (osec
))
10675 r_symndx
= osec
->target_index
;
10676 if (r_symndx
== STN_UNDEF
)
10678 irela
->r_addend
+= osec
->vma
;
10679 osec
= _bfd_nearby_section (output_bfd
, osec
,
10681 irela
->r_addend
-= osec
->vma
;
10682 r_symndx
= osec
->target_index
;
10687 /* Adjust the addend according to where the
10688 section winds up in the output section. */
10690 irela
->r_addend
+= sec
->output_offset
;
10694 if (flinfo
->indices
[r_symndx
] == -1)
10696 unsigned long shlink
;
10701 if (flinfo
->info
->strip
== strip_all
)
10703 /* You can't do ld -r -s. */
10704 bfd_set_error (bfd_error_invalid_operation
);
10708 /* This symbol was skipped earlier, but
10709 since it is needed by a reloc, we
10710 must output it now. */
10711 shlink
= symtab_hdr
->sh_link
;
10712 name
= (bfd_elf_string_from_elf_section
10713 (input_bfd
, shlink
, sym
.st_name
));
10717 osec
= sec
->output_section
;
10719 _bfd_elf_section_from_bfd_section (output_bfd
,
10721 if (sym
.st_shndx
== SHN_BAD
)
10724 sym
.st_value
+= sec
->output_offset
;
10725 if (!bfd_link_relocatable (flinfo
->info
))
10727 sym
.st_value
+= osec
->vma
;
10728 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
10730 /* STT_TLS symbols are relative to PT_TLS
10732 BFD_ASSERT (elf_hash_table (flinfo
->info
)
10733 ->tls_sec
!= NULL
);
10734 sym
.st_value
-= (elf_hash_table (flinfo
->info
)
10739 indx
= bfd_get_symcount (output_bfd
);
10740 ret
= elf_link_output_symstrtab (flinfo
, name
,
10746 flinfo
->indices
[r_symndx
] = indx
;
10751 r_symndx
= flinfo
->indices
[r_symndx
];
10754 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
10755 | (irela
->r_info
& r_type_mask
));
10758 /* Swap out the relocs. */
10759 input_rel_hdr
= esdi
->rel
.hdr
;
10760 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
10762 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10767 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
10768 * bed
->s
->int_rels_per_ext_rel
);
10769 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
10772 input_rela_hdr
= esdi
->rela
.hdr
;
10773 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
10775 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10784 /* Write out the modified section contents. */
10785 if (bed
->elf_backend_write_section
10786 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
10789 /* Section written out. */
10791 else switch (o
->sec_info_type
)
10793 case SEC_INFO_TYPE_STABS
:
10794 if (! (_bfd_write_section_stabs
10796 &elf_hash_table (flinfo
->info
)->stab_info
,
10797 o
, &elf_section_data (o
)->sec_info
, contents
)))
10800 case SEC_INFO_TYPE_MERGE
:
10801 if (! _bfd_write_merged_section (output_bfd
, o
,
10802 elf_section_data (o
)->sec_info
))
10805 case SEC_INFO_TYPE_EH_FRAME
:
10807 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
10812 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10814 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
10822 if (! (o
->flags
& SEC_EXCLUDE
))
10824 file_ptr offset
= (file_ptr
) o
->output_offset
;
10825 bfd_size_type todo
= o
->size
;
10827 offset
*= bfd_octets_per_byte (output_bfd
);
10829 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
10831 /* Reverse-copy input section to output. */
10834 todo
-= address_size
;
10835 if (! bfd_set_section_contents (output_bfd
,
10843 offset
+= address_size
;
10847 else if (! bfd_set_section_contents (output_bfd
,
10861 /* Generate a reloc when linking an ELF file. This is a reloc
10862 requested by the linker, and does not come from any input file. This
10863 is used to build constructor and destructor tables when linking
10867 elf_reloc_link_order (bfd
*output_bfd
,
10868 struct bfd_link_info
*info
,
10869 asection
*output_section
,
10870 struct bfd_link_order
*link_order
)
10872 reloc_howto_type
*howto
;
10876 struct bfd_elf_section_reloc_data
*reldata
;
10877 struct elf_link_hash_entry
**rel_hash_ptr
;
10878 Elf_Internal_Shdr
*rel_hdr
;
10879 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
10880 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
10883 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
10885 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
10888 bfd_set_error (bfd_error_bad_value
);
10892 addend
= link_order
->u
.reloc
.p
->addend
;
10895 reldata
= &esdo
->rel
;
10896 else if (esdo
->rela
.hdr
)
10897 reldata
= &esdo
->rela
;
10904 /* Figure out the symbol index. */
10905 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
10906 if (link_order
->type
== bfd_section_reloc_link_order
)
10908 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
10909 BFD_ASSERT (indx
!= 0);
10910 *rel_hash_ptr
= NULL
;
10914 struct elf_link_hash_entry
*h
;
10916 /* Treat a reloc against a defined symbol as though it were
10917 actually against the section. */
10918 h
= ((struct elf_link_hash_entry
*)
10919 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
10920 link_order
->u
.reloc
.p
->u
.name
,
10921 FALSE
, FALSE
, TRUE
));
10923 && (h
->root
.type
== bfd_link_hash_defined
10924 || h
->root
.type
== bfd_link_hash_defweak
))
10928 section
= h
->root
.u
.def
.section
;
10929 indx
= section
->output_section
->target_index
;
10930 *rel_hash_ptr
= NULL
;
10931 /* It seems that we ought to add the symbol value to the
10932 addend here, but in practice it has already been added
10933 because it was passed to constructor_callback. */
10934 addend
+= section
->output_section
->vma
+ section
->output_offset
;
10936 else if (h
!= NULL
)
10938 /* Setting the index to -2 tells elf_link_output_extsym that
10939 this symbol is used by a reloc. */
10946 (*info
->callbacks
->unattached_reloc
)
10947 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
10952 /* If this is an inplace reloc, we must write the addend into the
10954 if (howto
->partial_inplace
&& addend
!= 0)
10956 bfd_size_type size
;
10957 bfd_reloc_status_type rstat
;
10960 const char *sym_name
;
10962 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
10963 buf
= (bfd_byte
*) bfd_zmalloc (size
);
10964 if (buf
== NULL
&& size
!= 0)
10966 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
10973 case bfd_reloc_outofrange
:
10976 case bfd_reloc_overflow
:
10977 if (link_order
->type
== bfd_section_reloc_link_order
)
10978 sym_name
= bfd_section_name (output_bfd
,
10979 link_order
->u
.reloc
.p
->u
.section
);
10981 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
10982 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
10983 howto
->name
, addend
, NULL
, NULL
,
10988 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
10990 * bfd_octets_per_byte (output_bfd
),
10997 /* The address of a reloc is relative to the section in a
10998 relocatable file, and is a virtual address in an executable
11000 offset
= link_order
->offset
;
11001 if (! bfd_link_relocatable (info
))
11002 offset
+= output_section
->vma
;
11004 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
11006 irel
[i
].r_offset
= offset
;
11007 irel
[i
].r_info
= 0;
11008 irel
[i
].r_addend
= 0;
11010 if (bed
->s
->arch_size
== 32)
11011 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
11013 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
11015 rel_hdr
= reldata
->hdr
;
11016 erel
= rel_hdr
->contents
;
11017 if (rel_hdr
->sh_type
== SHT_REL
)
11019 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
11020 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
11024 irel
[0].r_addend
= addend
;
11025 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
11026 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
11035 /* Get the output vma of the section pointed to by the sh_link field. */
11038 elf_get_linked_section_vma (struct bfd_link_order
*p
)
11040 Elf_Internal_Shdr
**elf_shdrp
;
11044 s
= p
->u
.indirect
.section
;
11045 elf_shdrp
= elf_elfsections (s
->owner
);
11046 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
11047 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
11049 The Intel C compiler generates SHT_IA_64_UNWIND with
11050 SHF_LINK_ORDER. But it doesn't set the sh_link or
11051 sh_info fields. Hence we could get the situation
11052 where elfsec is 0. */
11055 const struct elf_backend_data
*bed
11056 = get_elf_backend_data (s
->owner
);
11057 if (bed
->link_order_error_handler
)
11058 bed
->link_order_error_handler
11059 /* xgettext:c-format */
11060 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
11065 s
= elf_shdrp
[elfsec
]->bfd_section
;
11066 return s
->output_section
->vma
+ s
->output_offset
;
11071 /* Compare two sections based on the locations of the sections they are
11072 linked to. Used by elf_fixup_link_order. */
11075 compare_link_order (const void * a
, const void * b
)
11080 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
11081 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
11084 return apos
> bpos
;
11088 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11089 order as their linked sections. Returns false if this could not be done
11090 because an output section includes both ordered and unordered
11091 sections. Ideally we'd do this in the linker proper. */
11094 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
11096 int seen_linkorder
;
11099 struct bfd_link_order
*p
;
11101 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11103 struct bfd_link_order
**sections
;
11104 asection
*s
, *other_sec
, *linkorder_sec
;
11108 linkorder_sec
= NULL
;
11110 seen_linkorder
= 0;
11111 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11113 if (p
->type
== bfd_indirect_link_order
)
11115 s
= p
->u
.indirect
.section
;
11117 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
11118 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
11119 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
11120 && elfsec
< elf_numsections (sub
)
11121 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
11122 && elf_elfsections (sub
)[elfsec
]->sh_link
< elf_numsections (sub
))
11136 if (seen_other
&& seen_linkorder
)
11138 if (other_sec
&& linkorder_sec
)
11140 /* xgettext:c-format */
11141 (_("%A has both ordered [`%A' in %B] "
11142 "and unordered [`%A' in %B] sections"),
11144 linkorder_sec
->owner
, other_sec
,
11148 (_("%A has both ordered and unordered sections"), o
);
11149 bfd_set_error (bfd_error_bad_value
);
11154 if (!seen_linkorder
)
11157 sections
= (struct bfd_link_order
**)
11158 bfd_malloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
11159 if (sections
== NULL
)
11161 seen_linkorder
= 0;
11163 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11165 sections
[seen_linkorder
++] = p
;
11167 /* Sort the input sections in the order of their linked section. */
11168 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
11169 compare_link_order
);
11171 /* Change the offsets of the sections. */
11173 for (n
= 0; n
< seen_linkorder
; n
++)
11175 s
= sections
[n
]->u
.indirect
.section
;
11176 offset
&= ~(bfd_vma
) 0 << s
->alignment_power
;
11177 s
->output_offset
= offset
/ bfd_octets_per_byte (abfd
);
11178 sections
[n
]->offset
= offset
;
11179 offset
+= sections
[n
]->size
;
11186 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11187 Returns TRUE upon success, FALSE otherwise. */
11190 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
11192 bfd_boolean ret
= FALSE
;
11194 const struct elf_backend_data
*bed
;
11196 enum bfd_architecture arch
;
11198 asymbol
**sympp
= NULL
;
11202 elf_symbol_type
*osymbuf
;
11204 implib_bfd
= info
->out_implib_bfd
;
11205 bed
= get_elf_backend_data (abfd
);
11207 if (!bfd_set_format (implib_bfd
, bfd_object
))
11210 flags
= bfd_get_file_flags (abfd
);
11211 flags
&= ~HAS_RELOC
;
11212 if (!bfd_set_start_address (implib_bfd
, 0)
11213 || !bfd_set_file_flags (implib_bfd
, flags
))
11216 /* Copy architecture of output file to import library file. */
11217 arch
= bfd_get_arch (abfd
);
11218 mach
= bfd_get_mach (abfd
);
11219 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
11220 && (abfd
->target_defaulted
11221 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
11224 /* Get symbol table size. */
11225 symsize
= bfd_get_symtab_upper_bound (abfd
);
11229 /* Read in the symbol table. */
11230 sympp
= (asymbol
**) xmalloc (symsize
);
11231 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
11235 /* Allow the BFD backend to copy any private header data it
11236 understands from the output BFD to the import library BFD. */
11237 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
11240 /* Filter symbols to appear in the import library. */
11241 if (bed
->elf_backend_filter_implib_symbols
)
11242 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
11245 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
11248 bfd_set_error (bfd_error_no_symbols
);
11249 _bfd_error_handler (_("%B: no symbol found for import library"),
11255 /* Make symbols absolute. */
11256 osymbuf
= (elf_symbol_type
*) bfd_alloc2 (implib_bfd
, symcount
,
11257 sizeof (*osymbuf
));
11258 for (src_count
= 0; src_count
< symcount
; src_count
++)
11260 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
11261 sizeof (*osymbuf
));
11262 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
11263 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
11264 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
11265 osymbuf
[src_count
].internal_elf_sym
.st_value
=
11266 osymbuf
[src_count
].symbol
.value
;
11267 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
11270 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
11272 /* Allow the BFD backend to copy any private data it understands
11273 from the output BFD to the import library BFD. This is done last
11274 to permit the routine to look at the filtered symbol table. */
11275 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
11278 if (!bfd_close (implib_bfd
))
11289 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
11293 if (flinfo
->symstrtab
!= NULL
)
11294 _bfd_elf_strtab_free (flinfo
->symstrtab
);
11295 if (flinfo
->contents
!= NULL
)
11296 free (flinfo
->contents
);
11297 if (flinfo
->external_relocs
!= NULL
)
11298 free (flinfo
->external_relocs
);
11299 if (flinfo
->internal_relocs
!= NULL
)
11300 free (flinfo
->internal_relocs
);
11301 if (flinfo
->external_syms
!= NULL
)
11302 free (flinfo
->external_syms
);
11303 if (flinfo
->locsym_shndx
!= NULL
)
11304 free (flinfo
->locsym_shndx
);
11305 if (flinfo
->internal_syms
!= NULL
)
11306 free (flinfo
->internal_syms
);
11307 if (flinfo
->indices
!= NULL
)
11308 free (flinfo
->indices
);
11309 if (flinfo
->sections
!= NULL
)
11310 free (flinfo
->sections
);
11311 if (flinfo
->symshndxbuf
!= NULL
)
11312 free (flinfo
->symshndxbuf
);
11313 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
11315 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11316 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rel
.hashes
!= NULL
)
11317 free (esdo
->rel
.hashes
);
11318 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rela
.hashes
!= NULL
)
11319 free (esdo
->rela
.hashes
);
11323 /* Do the final step of an ELF link. */
11326 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
11328 bfd_boolean dynamic
;
11329 bfd_boolean emit_relocs
;
11331 struct elf_final_link_info flinfo
;
11333 struct bfd_link_order
*p
;
11335 bfd_size_type max_contents_size
;
11336 bfd_size_type max_external_reloc_size
;
11337 bfd_size_type max_internal_reloc_count
;
11338 bfd_size_type max_sym_count
;
11339 bfd_size_type max_sym_shndx_count
;
11340 Elf_Internal_Sym elfsym
;
11342 Elf_Internal_Shdr
*symtab_hdr
;
11343 Elf_Internal_Shdr
*symtab_shndx_hdr
;
11344 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11345 struct elf_outext_info eoinfo
;
11346 bfd_boolean merged
;
11347 size_t relativecount
= 0;
11348 asection
*reldyn
= 0;
11350 asection
*attr_section
= NULL
;
11351 bfd_vma attr_size
= 0;
11352 const char *std_attrs_section
;
11353 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
11355 if (!is_elf_hash_table (htab
))
11358 if (bfd_link_pic (info
))
11359 abfd
->flags
|= DYNAMIC
;
11361 dynamic
= htab
->dynamic_sections_created
;
11362 dynobj
= htab
->dynobj
;
11364 emit_relocs
= (bfd_link_relocatable (info
)
11365 || info
->emitrelocations
);
11367 flinfo
.info
= info
;
11368 flinfo
.output_bfd
= abfd
;
11369 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
11370 if (flinfo
.symstrtab
== NULL
)
11375 flinfo
.hash_sec
= NULL
;
11376 flinfo
.symver_sec
= NULL
;
11380 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
11381 /* Note that dynsym_sec can be NULL (on VMS). */
11382 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
11383 /* Note that it is OK if symver_sec is NULL. */
11386 flinfo
.contents
= NULL
;
11387 flinfo
.external_relocs
= NULL
;
11388 flinfo
.internal_relocs
= NULL
;
11389 flinfo
.external_syms
= NULL
;
11390 flinfo
.locsym_shndx
= NULL
;
11391 flinfo
.internal_syms
= NULL
;
11392 flinfo
.indices
= NULL
;
11393 flinfo
.sections
= NULL
;
11394 flinfo
.symshndxbuf
= NULL
;
11395 flinfo
.filesym_count
= 0;
11397 /* The object attributes have been merged. Remove the input
11398 sections from the link, and set the contents of the output
11400 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
11401 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11403 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
11404 || strcmp (o
->name
, ".gnu.attributes") == 0)
11406 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11408 asection
*input_section
;
11410 if (p
->type
!= bfd_indirect_link_order
)
11412 input_section
= p
->u
.indirect
.section
;
11413 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11414 elf_link_input_bfd ignores this section. */
11415 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
11418 attr_size
= bfd_elf_obj_attr_size (abfd
);
11421 bfd_set_section_size (abfd
, o
, attr_size
);
11423 /* Skip this section later on. */
11424 o
->map_head
.link_order
= NULL
;
11427 o
->flags
|= SEC_EXCLUDE
;
11431 /* Count up the number of relocations we will output for each output
11432 section, so that we know the sizes of the reloc sections. We
11433 also figure out some maximum sizes. */
11434 max_contents_size
= 0;
11435 max_external_reloc_size
= 0;
11436 max_internal_reloc_count
= 0;
11438 max_sym_shndx_count
= 0;
11440 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11442 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11443 o
->reloc_count
= 0;
11445 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11447 unsigned int reloc_count
= 0;
11448 unsigned int additional_reloc_count
= 0;
11449 struct bfd_elf_section_data
*esdi
= NULL
;
11451 if (p
->type
== bfd_section_reloc_link_order
11452 || p
->type
== bfd_symbol_reloc_link_order
)
11454 else if (p
->type
== bfd_indirect_link_order
)
11458 sec
= p
->u
.indirect
.section
;
11460 /* Mark all sections which are to be included in the
11461 link. This will normally be every section. We need
11462 to do this so that we can identify any sections which
11463 the linker has decided to not include. */
11464 sec
->linker_mark
= TRUE
;
11466 if (sec
->flags
& SEC_MERGE
)
11469 if (sec
->rawsize
> max_contents_size
)
11470 max_contents_size
= sec
->rawsize
;
11471 if (sec
->size
> max_contents_size
)
11472 max_contents_size
= sec
->size
;
11474 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
11475 && (sec
->owner
->flags
& DYNAMIC
) == 0)
11479 /* We are interested in just local symbols, not all
11481 if (elf_bad_symtab (sec
->owner
))
11482 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
11483 / bed
->s
->sizeof_sym
);
11485 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
11487 if (sym_count
> max_sym_count
)
11488 max_sym_count
= sym_count
;
11490 if (sym_count
> max_sym_shndx_count
11491 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
11492 max_sym_shndx_count
= sym_count
;
11494 if (esdo
->this_hdr
.sh_type
== SHT_REL
11495 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
11496 /* Some backends use reloc_count in relocation sections
11497 to count particular types of relocs. Of course,
11498 reloc sections themselves can't have relocations. */
11500 else if (emit_relocs
)
11502 reloc_count
= sec
->reloc_count
;
11503 if (bed
->elf_backend_count_additional_relocs
)
11506 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
11507 additional_reloc_count
+= c
;
11510 else if (bed
->elf_backend_count_relocs
)
11511 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
11513 esdi
= elf_section_data (sec
);
11515 if ((sec
->flags
& SEC_RELOC
) != 0)
11517 size_t ext_size
= 0;
11519 if (esdi
->rel
.hdr
!= NULL
)
11520 ext_size
= esdi
->rel
.hdr
->sh_size
;
11521 if (esdi
->rela
.hdr
!= NULL
)
11522 ext_size
+= esdi
->rela
.hdr
->sh_size
;
11524 if (ext_size
> max_external_reloc_size
)
11525 max_external_reloc_size
= ext_size
;
11526 if (sec
->reloc_count
> max_internal_reloc_count
)
11527 max_internal_reloc_count
= sec
->reloc_count
;
11532 if (reloc_count
== 0)
11535 reloc_count
+= additional_reloc_count
;
11536 o
->reloc_count
+= reloc_count
;
11538 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
11542 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
11543 esdo
->rel
.count
+= additional_reloc_count
;
11545 if (esdi
->rela
.hdr
)
11547 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
11548 esdo
->rela
.count
+= additional_reloc_count
;
11554 esdo
->rela
.count
+= reloc_count
;
11556 esdo
->rel
.count
+= reloc_count
;
11560 if (o
->reloc_count
> 0)
11561 o
->flags
|= SEC_RELOC
;
11564 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11565 set it (this is probably a bug) and if it is set
11566 assign_section_numbers will create a reloc section. */
11567 o
->flags
&=~ SEC_RELOC
;
11570 /* If the SEC_ALLOC flag is not set, force the section VMA to
11571 zero. This is done in elf_fake_sections as well, but forcing
11572 the VMA to 0 here will ensure that relocs against these
11573 sections are handled correctly. */
11574 if ((o
->flags
& SEC_ALLOC
) == 0
11575 && ! o
->user_set_vma
)
11579 if (! bfd_link_relocatable (info
) && merged
)
11580 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
11582 /* Figure out the file positions for everything but the symbol table
11583 and the relocs. We set symcount to force assign_section_numbers
11584 to create a symbol table. */
11585 bfd_get_symcount (abfd
) = info
->strip
!= strip_all
|| emit_relocs
;
11586 BFD_ASSERT (! abfd
->output_has_begun
);
11587 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
11590 /* Set sizes, and assign file positions for reloc sections. */
11591 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11593 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11594 if ((o
->flags
& SEC_RELOC
) != 0)
11597 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
11601 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
11605 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11606 to count upwards while actually outputting the relocations. */
11607 esdo
->rel
.count
= 0;
11608 esdo
->rela
.count
= 0;
11610 if (esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
11612 /* Cache the section contents so that they can be compressed
11613 later. Use bfd_malloc since it will be freed by
11614 bfd_compress_section_contents. */
11615 unsigned char *contents
= esdo
->this_hdr
.contents
;
11616 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
11619 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
11620 if (contents
== NULL
)
11622 esdo
->this_hdr
.contents
= contents
;
11626 /* We have now assigned file positions for all the sections except
11627 .symtab, .strtab, and non-loaded reloc sections. We start the
11628 .symtab section at the current file position, and write directly
11629 to it. We build the .strtab section in memory. */
11630 bfd_get_symcount (abfd
) = 0;
11631 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11632 /* sh_name is set in prep_headers. */
11633 symtab_hdr
->sh_type
= SHT_SYMTAB
;
11634 /* sh_flags, sh_addr and sh_size all start off zero. */
11635 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
11636 /* sh_link is set in assign_section_numbers. */
11637 /* sh_info is set below. */
11638 /* sh_offset is set just below. */
11639 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
11641 if (max_sym_count
< 20)
11642 max_sym_count
= 20;
11643 htab
->strtabsize
= max_sym_count
;
11644 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
11645 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
11646 if (htab
->strtab
== NULL
)
11648 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11650 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
11651 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
11653 if (info
->strip
!= strip_all
|| emit_relocs
)
11655 file_ptr off
= elf_next_file_pos (abfd
);
11657 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
11659 /* Note that at this point elf_next_file_pos (abfd) is
11660 incorrect. We do not yet know the size of the .symtab section.
11661 We correct next_file_pos below, after we do know the size. */
11663 /* Start writing out the symbol table. The first symbol is always a
11665 elfsym
.st_value
= 0;
11666 elfsym
.st_size
= 0;
11667 elfsym
.st_info
= 0;
11668 elfsym
.st_other
= 0;
11669 elfsym
.st_shndx
= SHN_UNDEF
;
11670 elfsym
.st_target_internal
= 0;
11671 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
11672 bfd_und_section_ptr
, NULL
) != 1)
11675 /* Output a symbol for each section. We output these even if we are
11676 discarding local symbols, since they are used for relocs. These
11677 symbols have no names. We store the index of each one in the
11678 index field of the section, so that we can find it again when
11679 outputting relocs. */
11681 elfsym
.st_size
= 0;
11682 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
11683 elfsym
.st_other
= 0;
11684 elfsym
.st_value
= 0;
11685 elfsym
.st_target_internal
= 0;
11686 for (i
= 1; i
< elf_numsections (abfd
); i
++)
11688 o
= bfd_section_from_elf_index (abfd
, i
);
11691 o
->target_index
= bfd_get_symcount (abfd
);
11692 elfsym
.st_shndx
= i
;
11693 if (!bfd_link_relocatable (info
))
11694 elfsym
.st_value
= o
->vma
;
11695 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
, o
,
11702 /* Allocate some memory to hold information read in from the input
11704 if (max_contents_size
!= 0)
11706 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
11707 if (flinfo
.contents
== NULL
)
11711 if (max_external_reloc_size
!= 0)
11713 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
11714 if (flinfo
.external_relocs
== NULL
)
11718 if (max_internal_reloc_count
!= 0)
11720 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
11721 amt
*= sizeof (Elf_Internal_Rela
);
11722 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
11723 if (flinfo
.internal_relocs
== NULL
)
11727 if (max_sym_count
!= 0)
11729 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
11730 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
11731 if (flinfo
.external_syms
== NULL
)
11734 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
11735 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
11736 if (flinfo
.internal_syms
== NULL
)
11739 amt
= max_sym_count
* sizeof (long);
11740 flinfo
.indices
= (long int *) bfd_malloc (amt
);
11741 if (flinfo
.indices
== NULL
)
11744 amt
= max_sym_count
* sizeof (asection
*);
11745 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
11746 if (flinfo
.sections
== NULL
)
11750 if (max_sym_shndx_count
!= 0)
11752 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
11753 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
11754 if (flinfo
.locsym_shndx
== NULL
)
11760 bfd_vma base
, end
= 0;
11763 for (sec
= htab
->tls_sec
;
11764 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
11767 bfd_size_type size
= sec
->size
;
11770 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
11772 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
11775 size
= ord
->offset
+ ord
->size
;
11777 end
= sec
->vma
+ size
;
11779 base
= htab
->tls_sec
->vma
;
11780 /* Only align end of TLS section if static TLS doesn't have special
11781 alignment requirements. */
11782 if (bed
->static_tls_alignment
== 1)
11783 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
11784 htab
->tls_size
= end
- base
;
11787 /* Reorder SHF_LINK_ORDER sections. */
11788 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11790 if (!elf_fixup_link_order (abfd
, o
))
11794 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
11797 /* Since ELF permits relocations to be against local symbols, we
11798 must have the local symbols available when we do the relocations.
11799 Since we would rather only read the local symbols once, and we
11800 would rather not keep them in memory, we handle all the
11801 relocations for a single input file at the same time.
11803 Unfortunately, there is no way to know the total number of local
11804 symbols until we have seen all of them, and the local symbol
11805 indices precede the global symbol indices. This means that when
11806 we are generating relocatable output, and we see a reloc against
11807 a global symbol, we can not know the symbol index until we have
11808 finished examining all the local symbols to see which ones we are
11809 going to output. To deal with this, we keep the relocations in
11810 memory, and don't output them until the end of the link. This is
11811 an unfortunate waste of memory, but I don't see a good way around
11812 it. Fortunately, it only happens when performing a relocatable
11813 link, which is not the common case. FIXME: If keep_memory is set
11814 we could write the relocs out and then read them again; I don't
11815 know how bad the memory loss will be. */
11817 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11818 sub
->output_has_begun
= FALSE
;
11819 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11821 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11823 if (p
->type
== bfd_indirect_link_order
11824 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
11825 == bfd_target_elf_flavour
)
11826 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
11828 if (! sub
->output_has_begun
)
11830 if (! elf_link_input_bfd (&flinfo
, sub
))
11832 sub
->output_has_begun
= TRUE
;
11835 else if (p
->type
== bfd_section_reloc_link_order
11836 || p
->type
== bfd_symbol_reloc_link_order
)
11838 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
11843 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
11845 if (p
->type
== bfd_indirect_link_order
11846 && (bfd_get_flavour (sub
)
11847 == bfd_target_elf_flavour
)
11848 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
11849 != bed
->s
->elfclass
))
11851 const char *iclass
, *oclass
;
11853 switch (bed
->s
->elfclass
)
11855 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
11856 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
11857 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
11861 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
11863 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
11864 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
11865 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
11869 bfd_set_error (bfd_error_wrong_format
);
11871 /* xgettext:c-format */
11872 (_("%B: file class %s incompatible with %s"),
11873 sub
, iclass
, oclass
);
11882 /* Free symbol buffer if needed. */
11883 if (!info
->reduce_memory_overheads
)
11885 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11886 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
11887 && elf_tdata (sub
)->symbuf
)
11889 free (elf_tdata (sub
)->symbuf
);
11890 elf_tdata (sub
)->symbuf
= NULL
;
11894 /* Output any global symbols that got converted to local in a
11895 version script or due to symbol visibility. We do this in a
11896 separate step since ELF requires all local symbols to appear
11897 prior to any global symbols. FIXME: We should only do this if
11898 some global symbols were, in fact, converted to become local.
11899 FIXME: Will this work correctly with the Irix 5 linker? */
11900 eoinfo
.failed
= FALSE
;
11901 eoinfo
.flinfo
= &flinfo
;
11902 eoinfo
.localsyms
= TRUE
;
11903 eoinfo
.file_sym_done
= FALSE
;
11904 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
11908 /* If backend needs to output some local symbols not present in the hash
11909 table, do it now. */
11910 if (bed
->elf_backend_output_arch_local_syms
11911 && (info
->strip
!= strip_all
|| emit_relocs
))
11913 typedef int (*out_sym_func
)
11914 (void *, const char *, Elf_Internal_Sym
*, asection
*,
11915 struct elf_link_hash_entry
*);
11917 if (! ((*bed
->elf_backend_output_arch_local_syms
)
11918 (abfd
, info
, &flinfo
,
11919 (out_sym_func
) elf_link_output_symstrtab
)))
11923 /* That wrote out all the local symbols. Finish up the symbol table
11924 with the global symbols. Even if we want to strip everything we
11925 can, we still need to deal with those global symbols that got
11926 converted to local in a version script. */
11928 /* The sh_info field records the index of the first non local symbol. */
11929 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
11932 && htab
->dynsym
!= NULL
11933 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
11935 Elf_Internal_Sym sym
;
11936 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
11938 o
= htab
->dynsym
->output_section
;
11939 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
11941 /* Write out the section symbols for the output sections. */
11942 if (bfd_link_pic (info
)
11943 || htab
->is_relocatable_executable
)
11949 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
11951 sym
.st_target_internal
= 0;
11953 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
11959 dynindx
= elf_section_data (s
)->dynindx
;
11962 indx
= elf_section_data (s
)->this_idx
;
11963 BFD_ASSERT (indx
> 0);
11964 sym
.st_shndx
= indx
;
11965 if (! check_dynsym (abfd
, &sym
))
11967 sym
.st_value
= s
->vma
;
11968 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
11969 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
11973 /* Write out the local dynsyms. */
11974 if (htab
->dynlocal
)
11976 struct elf_link_local_dynamic_entry
*e
;
11977 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
11982 /* Copy the internal symbol and turn off visibility.
11983 Note that we saved a word of storage and overwrote
11984 the original st_name with the dynstr_index. */
11986 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
11988 s
= bfd_section_from_elf_index (e
->input_bfd
,
11993 elf_section_data (s
->output_section
)->this_idx
;
11994 if (! check_dynsym (abfd
, &sym
))
11996 sym
.st_value
= (s
->output_section
->vma
11998 + e
->isym
.st_value
);
12001 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
12002 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12007 /* We get the global symbols from the hash table. */
12008 eoinfo
.failed
= FALSE
;
12009 eoinfo
.localsyms
= FALSE
;
12010 eoinfo
.flinfo
= &flinfo
;
12011 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12015 /* If backend needs to output some symbols not present in the hash
12016 table, do it now. */
12017 if (bed
->elf_backend_output_arch_syms
12018 && (info
->strip
!= strip_all
|| emit_relocs
))
12020 typedef int (*out_sym_func
)
12021 (void *, const char *, Elf_Internal_Sym
*, asection
*,
12022 struct elf_link_hash_entry
*);
12024 if (! ((*bed
->elf_backend_output_arch_syms
)
12025 (abfd
, info
, &flinfo
,
12026 (out_sym_func
) elf_link_output_symstrtab
)))
12030 /* Finalize the .strtab section. */
12031 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
12033 /* Swap out the .strtab section. */
12034 if (!elf_link_swap_symbols_out (&flinfo
))
12037 /* Now we know the size of the symtab section. */
12038 if (bfd_get_symcount (abfd
) > 0)
12040 /* Finish up and write out the symbol string table (.strtab)
12042 Elf_Internal_Shdr
*symstrtab_hdr
;
12043 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
12045 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
12046 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
12048 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
12049 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
12050 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
12051 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
12052 symtab_shndx_hdr
->sh_size
= amt
;
12054 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
12057 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
12058 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
12062 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
12063 /* sh_name was set in prep_headers. */
12064 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
12065 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
12066 symstrtab_hdr
->sh_addr
= 0;
12067 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
12068 symstrtab_hdr
->sh_entsize
= 0;
12069 symstrtab_hdr
->sh_link
= 0;
12070 symstrtab_hdr
->sh_info
= 0;
12071 /* sh_offset is set just below. */
12072 symstrtab_hdr
->sh_addralign
= 1;
12074 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
12076 elf_next_file_pos (abfd
) = off
;
12078 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
12079 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
12083 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
12085 _bfd_error_handler (_("%B: failed to generate import library"),
12086 info
->out_implib_bfd
);
12090 /* Adjust the relocs to have the correct symbol indices. */
12091 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12093 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12095 if ((o
->flags
& SEC_RELOC
) == 0)
12098 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
12099 if (esdo
->rel
.hdr
!= NULL
12100 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
))
12102 if (esdo
->rela
.hdr
!= NULL
12103 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
))
12106 /* Set the reloc_count field to 0 to prevent write_relocs from
12107 trying to swap the relocs out itself. */
12108 o
->reloc_count
= 0;
12111 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
12112 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
12114 /* If we are linking against a dynamic object, or generating a
12115 shared library, finish up the dynamic linking information. */
12118 bfd_byte
*dyncon
, *dynconend
;
12120 /* Fix up .dynamic entries. */
12121 o
= bfd_get_linker_section (dynobj
, ".dynamic");
12122 BFD_ASSERT (o
!= NULL
);
12124 dyncon
= o
->contents
;
12125 dynconend
= o
->contents
+ o
->size
;
12126 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12128 Elf_Internal_Dyn dyn
;
12131 bfd_size_type sh_size
;
12134 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12141 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
12143 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
12145 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
12146 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
12149 dyn
.d_un
.d_val
= relativecount
;
12156 name
= info
->init_function
;
12159 name
= info
->fini_function
;
12162 struct elf_link_hash_entry
*h
;
12164 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
12166 && (h
->root
.type
== bfd_link_hash_defined
12167 || h
->root
.type
== bfd_link_hash_defweak
))
12169 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
12170 o
= h
->root
.u
.def
.section
;
12171 if (o
->output_section
!= NULL
)
12172 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
12173 + o
->output_offset
);
12176 /* The symbol is imported from another shared
12177 library and does not apply to this one. */
12178 dyn
.d_un
.d_ptr
= 0;
12185 case DT_PREINIT_ARRAYSZ
:
12186 name
= ".preinit_array";
12188 case DT_INIT_ARRAYSZ
:
12189 name
= ".init_array";
12191 case DT_FINI_ARRAYSZ
:
12192 name
= ".fini_array";
12194 o
= bfd_get_section_by_name (abfd
, name
);
12198 (_("could not find section %s"), name
);
12203 (_("warning: %s section has zero size"), name
);
12204 dyn
.d_un
.d_val
= o
->size
;
12207 case DT_PREINIT_ARRAY
:
12208 name
= ".preinit_array";
12210 case DT_INIT_ARRAY
:
12211 name
= ".init_array";
12213 case DT_FINI_ARRAY
:
12214 name
= ".fini_array";
12216 o
= bfd_get_section_by_name (abfd
, name
);
12223 name
= ".gnu.hash";
12232 name
= ".gnu.version_d";
12235 name
= ".gnu.version_r";
12238 name
= ".gnu.version";
12240 o
= bfd_get_linker_section (dynobj
, name
);
12245 (_("could not find section %s"), name
);
12248 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
12251 (_("warning: section '%s' is being made into a note"), name
);
12252 bfd_set_error (bfd_error_nonrepresentable_section
);
12255 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
12262 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
12268 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12270 Elf_Internal_Shdr
*hdr
;
12272 hdr
= elf_elfsections (abfd
)[i
];
12273 if (hdr
->sh_type
== type
12274 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
12276 sh_size
+= hdr
->sh_size
;
12278 || sh_addr
> hdr
->sh_addr
)
12279 sh_addr
= hdr
->sh_addr
;
12283 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
12285 /* Don't count procedure linkage table relocs in the
12286 overall reloc count. */
12287 sh_size
-= htab
->srelplt
->size
;
12289 /* If the size is zero, make the address zero too.
12290 This is to avoid a glibc bug. If the backend
12291 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12292 zero, then we'll put DT_RELA at the end of
12293 DT_JMPREL. glibc will interpret the end of
12294 DT_RELA matching the end of DT_JMPREL as the
12295 case where DT_RELA includes DT_JMPREL, and for
12296 LD_BIND_NOW will decide that processing DT_RELA
12297 will process the PLT relocs too. Net result:
12298 No PLT relocs applied. */
12301 /* If .rela.plt is the first .rela section, exclude
12302 it from DT_RELA. */
12303 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
12304 + htab
->srelplt
->output_offset
))
12305 sh_addr
+= htab
->srelplt
->size
;
12308 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
12309 dyn
.d_un
.d_val
= sh_size
;
12311 dyn
.d_un
.d_ptr
= sh_addr
;
12314 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
12318 /* If we have created any dynamic sections, then output them. */
12319 if (dynobj
!= NULL
)
12321 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
12324 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12325 if (((info
->warn_shared_textrel
&& bfd_link_pic (info
))
12326 || info
->error_textrel
)
12327 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
)
12329 bfd_byte
*dyncon
, *dynconend
;
12331 dyncon
= o
->contents
;
12332 dynconend
= o
->contents
+ o
->size
;
12333 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12335 Elf_Internal_Dyn dyn
;
12337 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12339 if (dyn
.d_tag
== DT_TEXTREL
)
12341 if (info
->error_textrel
)
12342 info
->callbacks
->einfo
12343 (_("%P%X: read-only segment has dynamic relocations.\n"));
12345 info
->callbacks
->einfo
12346 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12352 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
12354 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
12356 || o
->output_section
== bfd_abs_section_ptr
)
12358 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
12360 /* At this point, we are only interested in sections
12361 created by _bfd_elf_link_create_dynamic_sections. */
12364 if (htab
->stab_info
.stabstr
== o
)
12366 if (htab
->eh_info
.hdr_sec
== o
)
12368 if (strcmp (o
->name
, ".dynstr") != 0)
12370 if (! bfd_set_section_contents (abfd
, o
->output_section
,
12372 (file_ptr
) o
->output_offset
12373 * bfd_octets_per_byte (abfd
),
12379 /* The contents of the .dynstr section are actually in a
12383 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
12384 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
12385 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
12391 if (bfd_link_relocatable (info
))
12393 bfd_boolean failed
= FALSE
;
12395 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
12400 /* If we have optimized stabs strings, output them. */
12401 if (htab
->stab_info
.stabstr
!= NULL
)
12403 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
12407 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
12410 elf_final_link_free (abfd
, &flinfo
);
12412 elf_linker (abfd
) = TRUE
;
12416 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
12417 if (contents
== NULL
)
12418 return FALSE
; /* Bail out and fail. */
12419 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
12420 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
12427 elf_final_link_free (abfd
, &flinfo
);
12431 /* Initialize COOKIE for input bfd ABFD. */
12434 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
12435 struct bfd_link_info
*info
, bfd
*abfd
)
12437 Elf_Internal_Shdr
*symtab_hdr
;
12438 const struct elf_backend_data
*bed
;
12440 bed
= get_elf_backend_data (abfd
);
12441 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12443 cookie
->abfd
= abfd
;
12444 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
12445 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
12446 if (cookie
->bad_symtab
)
12448 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
12449 cookie
->extsymoff
= 0;
12453 cookie
->locsymcount
= symtab_hdr
->sh_info
;
12454 cookie
->extsymoff
= symtab_hdr
->sh_info
;
12457 if (bed
->s
->arch_size
== 32)
12458 cookie
->r_sym_shift
= 8;
12460 cookie
->r_sym_shift
= 32;
12462 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
12463 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
12465 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
12466 cookie
->locsymcount
, 0,
12468 if (cookie
->locsyms
== NULL
)
12470 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
12473 if (info
->keep_memory
)
12474 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
12479 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12482 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
12484 Elf_Internal_Shdr
*symtab_hdr
;
12486 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12487 if (cookie
->locsyms
!= NULL
12488 && symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
12489 free (cookie
->locsyms
);
12492 /* Initialize the relocation information in COOKIE for input section SEC
12493 of input bfd ABFD. */
12496 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12497 struct bfd_link_info
*info
, bfd
*abfd
,
12500 const struct elf_backend_data
*bed
;
12502 if (sec
->reloc_count
== 0)
12504 cookie
->rels
= NULL
;
12505 cookie
->relend
= NULL
;
12509 bed
= get_elf_backend_data (abfd
);
12511 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
12512 info
->keep_memory
);
12513 if (cookie
->rels
== NULL
)
12515 cookie
->rel
= cookie
->rels
;
12516 cookie
->relend
= (cookie
->rels
12517 + sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
);
12519 cookie
->rel
= cookie
->rels
;
12523 /* Free the memory allocated by init_reloc_cookie_rels,
12527 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12530 if (cookie
->rels
&& elf_section_data (sec
)->relocs
!= cookie
->rels
)
12531 free (cookie
->rels
);
12534 /* Initialize the whole of COOKIE for input section SEC. */
12537 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12538 struct bfd_link_info
*info
,
12541 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
12543 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
12548 fini_reloc_cookie (cookie
, sec
->owner
);
12553 /* Free the memory allocated by init_reloc_cookie_for_section,
12557 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12560 fini_reloc_cookie_rels (cookie
, sec
);
12561 fini_reloc_cookie (cookie
, sec
->owner
);
12564 /* Garbage collect unused sections. */
12566 /* Default gc_mark_hook. */
12569 _bfd_elf_gc_mark_hook (asection
*sec
,
12570 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12571 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
12572 struct elf_link_hash_entry
*h
,
12573 Elf_Internal_Sym
*sym
)
12577 switch (h
->root
.type
)
12579 case bfd_link_hash_defined
:
12580 case bfd_link_hash_defweak
:
12581 return h
->root
.u
.def
.section
;
12583 case bfd_link_hash_common
:
12584 return h
->root
.u
.c
.p
->section
;
12591 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
12596 /* For undefined __start_<name> and __stop_<name> symbols, return the
12597 first input section matching <name>. Return NULL otherwise. */
12600 _bfd_elf_is_start_stop (const struct bfd_link_info
*info
,
12601 struct elf_link_hash_entry
*h
)
12604 const char *sec_name
;
12606 if (h
->root
.type
!= bfd_link_hash_undefined
12607 && h
->root
.type
!= bfd_link_hash_undefweak
)
12610 s
= h
->root
.u
.undef
.section
;
12613 if (s
== (asection
*) 0 - 1)
12619 if (strncmp (h
->root
.root
.string
, "__start_", 8) == 0)
12620 sec_name
= h
->root
.root
.string
+ 8;
12621 else if (strncmp (h
->root
.root
.string
, "__stop_", 7) == 0)
12622 sec_name
= h
->root
.root
.string
+ 7;
12624 if (sec_name
!= NULL
&& *sec_name
!= '\0')
12628 for (i
= info
->input_bfds
; i
!= NULL
; i
= i
->link
.next
)
12630 s
= bfd_get_section_by_name (i
, sec_name
);
12633 h
->root
.u
.undef
.section
= s
;
12640 h
->root
.u
.undef
.section
= (asection
*) 0 - 1;
12645 /* COOKIE->rel describes a relocation against section SEC, which is
12646 a section we've decided to keep. Return the section that contains
12647 the relocation symbol, or NULL if no section contains it. */
12650 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
12651 elf_gc_mark_hook_fn gc_mark_hook
,
12652 struct elf_reloc_cookie
*cookie
,
12653 bfd_boolean
*start_stop
)
12655 unsigned long r_symndx
;
12656 struct elf_link_hash_entry
*h
;
12658 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
12659 if (r_symndx
== STN_UNDEF
)
12662 if (r_symndx
>= cookie
->locsymcount
12663 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
12665 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
12668 info
->callbacks
->einfo (_("%F%P: corrupt input: %B\n"),
12672 while (h
->root
.type
== bfd_link_hash_indirect
12673 || h
->root
.type
== bfd_link_hash_warning
)
12674 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
12676 /* If this symbol is weak and there is a non-weak definition, we
12677 keep the non-weak definition because many backends put
12678 dynamic reloc info on the non-weak definition for code
12679 handling copy relocs. */
12680 if (h
->u
.weakdef
!= NULL
)
12681 h
->u
.weakdef
->mark
= 1;
12683 if (start_stop
!= NULL
)
12685 /* To work around a glibc bug, mark all XXX input sections
12686 when there is an as yet undefined reference to __start_XXX
12687 or __stop_XXX symbols. The linker will later define such
12688 symbols for orphan input sections that have a name
12689 representable as a C identifier. */
12690 asection
*s
= _bfd_elf_is_start_stop (info
, h
);
12694 *start_stop
= !s
->gc_mark
;
12699 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
12702 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
12703 &cookie
->locsyms
[r_symndx
]);
12706 /* COOKIE->rel describes a relocation against section SEC, which is
12707 a section we've decided to keep. Mark the section that contains
12708 the relocation symbol. */
12711 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
12713 elf_gc_mark_hook_fn gc_mark_hook
,
12714 struct elf_reloc_cookie
*cookie
)
12717 bfd_boolean start_stop
= FALSE
;
12719 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
12720 while (rsec
!= NULL
)
12722 if (!rsec
->gc_mark
)
12724 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
12725 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
12727 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
12732 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
12737 /* The mark phase of garbage collection. For a given section, mark
12738 it and any sections in this section's group, and all the sections
12739 which define symbols to which it refers. */
12742 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
12744 elf_gc_mark_hook_fn gc_mark_hook
)
12747 asection
*group_sec
, *eh_frame
;
12751 /* Mark all the sections in the group. */
12752 group_sec
= elf_section_data (sec
)->next_in_group
;
12753 if (group_sec
&& !group_sec
->gc_mark
)
12754 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
12757 /* Look through the section relocs. */
12759 eh_frame
= elf_eh_frame_section (sec
->owner
);
12760 if ((sec
->flags
& SEC_RELOC
) != 0
12761 && sec
->reloc_count
> 0
12762 && sec
!= eh_frame
)
12764 struct elf_reloc_cookie cookie
;
12766 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
12770 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
12771 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
12776 fini_reloc_cookie_for_section (&cookie
, sec
);
12780 if (ret
&& eh_frame
&& elf_fde_list (sec
))
12782 struct elf_reloc_cookie cookie
;
12784 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
12788 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
12789 gc_mark_hook
, &cookie
))
12791 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
12795 eh_frame
= elf_section_eh_frame_entry (sec
);
12796 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
12797 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
12803 /* Scan and mark sections in a special or debug section group. */
12806 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
12808 /* Point to first section of section group. */
12810 /* Used to iterate the section group. */
12813 bfd_boolean is_special_grp
= TRUE
;
12814 bfd_boolean is_debug_grp
= TRUE
;
12816 /* First scan to see if group contains any section other than debug
12817 and special section. */
12818 ssec
= msec
= elf_next_in_group (grp
);
12821 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
12822 is_debug_grp
= FALSE
;
12824 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
12825 is_special_grp
= FALSE
;
12827 msec
= elf_next_in_group (msec
);
12829 while (msec
!= ssec
);
12831 /* If this is a pure debug section group or pure special section group,
12832 keep all sections in this group. */
12833 if (is_debug_grp
|| is_special_grp
)
12838 msec
= elf_next_in_group (msec
);
12840 while (msec
!= ssec
);
12844 /* Keep debug and special sections. */
12847 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
12848 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED
)
12852 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
12855 bfd_boolean some_kept
;
12856 bfd_boolean debug_frag_seen
;
12858 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
12861 /* Ensure all linker created sections are kept,
12862 see if any other section is already marked,
12863 and note if we have any fragmented debug sections. */
12864 debug_frag_seen
= some_kept
= FALSE
;
12865 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12867 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
12869 else if (isec
->gc_mark
)
12872 if (debug_frag_seen
== FALSE
12873 && (isec
->flags
& SEC_DEBUGGING
)
12874 && CONST_STRNEQ (isec
->name
, ".debug_line."))
12875 debug_frag_seen
= TRUE
;
12878 /* If no section in this file will be kept, then we can
12879 toss out the debug and special sections. */
12883 /* Keep debug and special sections like .comment when they are
12884 not part of a group. Also keep section groups that contain
12885 just debug sections or special sections. */
12886 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12888 if ((isec
->flags
& SEC_GROUP
) != 0)
12889 _bfd_elf_gc_mark_debug_special_section_group (isec
);
12890 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
12891 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
12892 && elf_next_in_group (isec
) == NULL
)
12896 if (! debug_frag_seen
)
12899 /* Look for CODE sections which are going to be discarded,
12900 and find and discard any fragmented debug sections which
12901 are associated with that code section. */
12902 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12903 if ((isec
->flags
& SEC_CODE
) != 0
12904 && isec
->gc_mark
== 0)
12909 ilen
= strlen (isec
->name
);
12911 /* Association is determined by the name of the debug section
12912 containing the name of the code section as a suffix. For
12913 example .debug_line.text.foo is a debug section associated
12915 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
12919 if (dsec
->gc_mark
== 0
12920 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
12923 dlen
= strlen (dsec
->name
);
12926 && strncmp (dsec
->name
+ (dlen
- ilen
),
12927 isec
->name
, ilen
) == 0)
12937 /* The sweep phase of garbage collection. Remove all garbage sections. */
12939 typedef bfd_boolean (*gc_sweep_hook_fn
)
12940 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
12943 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
12946 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12947 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
12949 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12953 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
12954 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
12957 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
12959 /* When any section in a section group is kept, we keep all
12960 sections in the section group. If the first member of
12961 the section group is excluded, we will also exclude the
12963 if (o
->flags
& SEC_GROUP
)
12965 asection
*first
= elf_next_in_group (o
);
12966 o
->gc_mark
= first
->gc_mark
;
12972 /* Skip sweeping sections already excluded. */
12973 if (o
->flags
& SEC_EXCLUDE
)
12976 /* Since this is early in the link process, it is simple
12977 to remove a section from the output. */
12978 o
->flags
|= SEC_EXCLUDE
;
12980 if (info
->print_gc_sections
&& o
->size
!= 0)
12981 /* xgettext:c-format */
12982 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"),
12985 /* But we also have to update some of the relocation
12986 info we collected before. */
12988 && (o
->flags
& SEC_RELOC
) != 0
12989 && o
->reloc_count
!= 0
12990 && !((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
12991 && (o
->flags
& SEC_DEBUGGING
) != 0)
12992 && !bfd_is_abs_section (o
->output_section
))
12994 Elf_Internal_Rela
*internal_relocs
;
12998 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
12999 info
->keep_memory
);
13000 if (internal_relocs
== NULL
)
13003 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
13005 if (elf_section_data (o
)->relocs
!= internal_relocs
)
13006 free (internal_relocs
);
13017 /* Propagate collected vtable information. This is called through
13018 elf_link_hash_traverse. */
13021 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
13023 /* Those that are not vtables. */
13024 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
13027 /* Those vtables that do not have parents, we cannot merge. */
13028 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
13031 /* If we've already been done, exit. */
13032 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
13035 /* Make sure the parent's table is up to date. */
13036 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
13038 if (h
->vtable
->used
== NULL
)
13040 /* None of this table's entries were referenced. Re-use the
13042 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
13043 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
13048 bfd_boolean
*cu
, *pu
;
13050 /* Or the parent's entries into ours. */
13051 cu
= h
->vtable
->used
;
13053 pu
= h
->vtable
->parent
->vtable
->used
;
13056 const struct elf_backend_data
*bed
;
13057 unsigned int log_file_align
;
13059 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
13060 log_file_align
= bed
->s
->log_file_align
;
13061 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
13076 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
13079 bfd_vma hstart
, hend
;
13080 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
13081 const struct elf_backend_data
*bed
;
13082 unsigned int log_file_align
;
13084 /* Take care of both those symbols that do not describe vtables as
13085 well as those that are not loaded. */
13086 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
13089 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
13090 || h
->root
.type
== bfd_link_hash_defweak
);
13092 sec
= h
->root
.u
.def
.section
;
13093 hstart
= h
->root
.u
.def
.value
;
13094 hend
= hstart
+ h
->size
;
13096 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
13098 return *(bfd_boolean
*) okp
= FALSE
;
13099 bed
= get_elf_backend_data (sec
->owner
);
13100 log_file_align
= bed
->s
->log_file_align
;
13102 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
13104 for (rel
= relstart
; rel
< relend
; ++rel
)
13105 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
13107 /* If the entry is in use, do nothing. */
13108 if (h
->vtable
->used
13109 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
13111 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
13112 if (h
->vtable
->used
[entry
])
13115 /* Otherwise, kill it. */
13116 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
13122 /* Mark sections containing dynamically referenced symbols. When
13123 building shared libraries, we must assume that any visible symbol is
13127 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
13129 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
13130 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
13132 if ((h
->root
.type
== bfd_link_hash_defined
13133 || h
->root
.type
== bfd_link_hash_defweak
)
13135 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
13136 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
13137 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
13138 && (!bfd_link_executable (info
)
13139 || info
->gc_keep_exported
13140 || info
->export_dynamic
13143 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
13144 && (h
->versioned
>= versioned
13145 || !bfd_hide_sym_by_version (info
->version_info
,
13146 h
->root
.root
.string
)))))
13147 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13152 /* Keep all sections containing symbols undefined on the command-line,
13153 and the section containing the entry symbol. */
13156 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
13158 struct bfd_sym_chain
*sym
;
13160 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
13162 struct elf_link_hash_entry
*h
;
13164 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
13165 FALSE
, FALSE
, FALSE
);
13168 && (h
->root
.type
== bfd_link_hash_defined
13169 || h
->root
.type
== bfd_link_hash_defweak
)
13170 && !bfd_is_abs_section (h
->root
.u
.def
.section
)
13171 && !bfd_is_und_section (h
->root
.u
.def
.section
))
13172 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13177 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
13178 struct bfd_link_info
*info
)
13180 bfd
*ibfd
= info
->input_bfds
;
13182 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13185 struct elf_reloc_cookie cookie
;
13187 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13190 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
13193 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
13195 if (CONST_STRNEQ (bfd_section_name (ibfd
, sec
), ".eh_frame_entry")
13196 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
13198 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
13199 fini_reloc_cookie_rels (&cookie
, sec
);
13206 /* Do mark and sweep of unused sections. */
13209 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
13211 bfd_boolean ok
= TRUE
;
13213 elf_gc_mark_hook_fn gc_mark_hook
;
13214 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13215 struct elf_link_hash_table
*htab
;
13217 if (!bed
->can_gc_sections
13218 || !is_elf_hash_table (info
->hash
))
13220 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13224 bed
->gc_keep (info
);
13225 htab
= elf_hash_table (info
);
13227 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13228 at the .eh_frame section if we can mark the FDEs individually. */
13229 for (sub
= info
->input_bfds
;
13230 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
13231 sub
= sub
->link
.next
)
13234 struct elf_reloc_cookie cookie
;
13236 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
13237 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
13239 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
13240 if (elf_section_data (sec
)->sec_info
13241 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
13242 elf_eh_frame_section (sub
) = sec
;
13243 fini_reloc_cookie_for_section (&cookie
, sec
);
13244 sec
= bfd_get_next_section_by_name (NULL
, sec
);
13248 /* Apply transitive closure to the vtable entry usage info. */
13249 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
13253 /* Kill the vtable relocations that were not used. */
13254 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &ok
);
13258 /* Mark dynamically referenced symbols. */
13259 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
13260 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
13262 /* Grovel through relocs to find out who stays ... */
13263 gc_mark_hook
= bed
->gc_mark_hook
;
13264 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13268 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13269 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13272 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13273 Also treat note sections as a root, if the section is not part
13275 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13277 && (o
->flags
& SEC_EXCLUDE
) == 0
13278 && ((o
->flags
& SEC_KEEP
) != 0
13279 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
13280 && elf_next_in_group (o
) == NULL
)))
13282 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
13287 /* Allow the backend to mark additional target specific sections. */
13288 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
13290 /* ... and mark SEC_EXCLUDE for those that go. */
13291 return elf_gc_sweep (abfd
, info
);
13294 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13297 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
13299 struct elf_link_hash_entry
*h
,
13302 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
13303 struct elf_link_hash_entry
**search
, *child
;
13304 size_t extsymcount
;
13305 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13307 /* The sh_info field of the symtab header tells us where the
13308 external symbols start. We don't care about the local symbols at
13310 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
13311 if (!elf_bad_symtab (abfd
))
13312 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
13314 sym_hashes
= elf_sym_hashes (abfd
);
13315 sym_hashes_end
= sym_hashes
+ extsymcount
;
13317 /* Hunt down the child symbol, which is in this section at the same
13318 offset as the relocation. */
13319 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
13321 if ((child
= *search
) != NULL
13322 && (child
->root
.type
== bfd_link_hash_defined
13323 || child
->root
.type
== bfd_link_hash_defweak
)
13324 && child
->root
.u
.def
.section
== sec
13325 && child
->root
.u
.def
.value
== offset
)
13329 /* xgettext:c-format */
13330 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"),
13331 abfd
, sec
, (unsigned long) offset
);
13332 bfd_set_error (bfd_error_invalid_operation
);
13336 if (!child
->vtable
)
13338 child
->vtable
= ((struct elf_link_virtual_table_entry
*)
13339 bfd_zalloc (abfd
, sizeof (*child
->vtable
)));
13340 if (!child
->vtable
)
13345 /* This *should* only be the absolute section. It could potentially
13346 be that someone has defined a non-global vtable though, which
13347 would be bad. It isn't worth paging in the local symbols to be
13348 sure though; that case should simply be handled by the assembler. */
13350 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
13353 child
->vtable
->parent
= h
;
13358 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13361 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
13362 asection
*sec ATTRIBUTE_UNUSED
,
13363 struct elf_link_hash_entry
*h
,
13366 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13367 unsigned int log_file_align
= bed
->s
->log_file_align
;
13371 h
->vtable
= ((struct elf_link_virtual_table_entry
*)
13372 bfd_zalloc (abfd
, sizeof (*h
->vtable
)));
13377 if (addend
>= h
->vtable
->size
)
13379 size_t size
, bytes
, file_align
;
13380 bfd_boolean
*ptr
= h
->vtable
->used
;
13382 /* While the symbol is undefined, we have to be prepared to handle
13384 file_align
= 1 << log_file_align
;
13385 if (h
->root
.type
== bfd_link_hash_undefined
)
13386 size
= addend
+ file_align
;
13390 if (addend
>= size
)
13392 /* Oops! We've got a reference past the defined end of
13393 the table. This is probably a bug -- shall we warn? */
13394 size
= addend
+ file_align
;
13397 size
= (size
+ file_align
- 1) & -file_align
;
13399 /* Allocate one extra entry for use as a "done" flag for the
13400 consolidation pass. */
13401 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
13405 ptr
= (bfd_boolean
*) bfd_realloc (ptr
- 1, bytes
);
13411 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
13412 * sizeof (bfd_boolean
));
13413 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
13417 ptr
= (bfd_boolean
*) bfd_zmalloc (bytes
);
13422 /* And arrange for that done flag to be at index -1. */
13423 h
->vtable
->used
= ptr
+ 1;
13424 h
->vtable
->size
= size
;
13427 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
13432 /* Map an ELF section header flag to its corresponding string. */
13436 flagword flag_value
;
13437 } elf_flags_to_name_table
;
13439 static elf_flags_to_name_table elf_flags_to_names
[] =
13441 { "SHF_WRITE", SHF_WRITE
},
13442 { "SHF_ALLOC", SHF_ALLOC
},
13443 { "SHF_EXECINSTR", SHF_EXECINSTR
},
13444 { "SHF_MERGE", SHF_MERGE
},
13445 { "SHF_STRINGS", SHF_STRINGS
},
13446 { "SHF_INFO_LINK", SHF_INFO_LINK
},
13447 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
13448 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
13449 { "SHF_GROUP", SHF_GROUP
},
13450 { "SHF_TLS", SHF_TLS
},
13451 { "SHF_MASKOS", SHF_MASKOS
},
13452 { "SHF_EXCLUDE", SHF_EXCLUDE
},
13455 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13457 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
13458 struct flag_info
*flaginfo
,
13461 const bfd_vma sh_flags
= elf_section_flags (section
);
13463 if (!flaginfo
->flags_initialized
)
13465 bfd
*obfd
= info
->output_bfd
;
13466 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13467 struct flag_info_list
*tf
= flaginfo
->flag_list
;
13469 int without_hex
= 0;
13471 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
13474 flagword (*lookup
) (char *);
13476 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
13477 if (lookup
!= NULL
)
13479 flagword hexval
= (*lookup
) ((char *) tf
->name
);
13483 if (tf
->with
== with_flags
)
13484 with_hex
|= hexval
;
13485 else if (tf
->with
== without_flags
)
13486 without_hex
|= hexval
;
13491 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
13493 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
13495 if (tf
->with
== with_flags
)
13496 with_hex
|= elf_flags_to_names
[i
].flag_value
;
13497 else if (tf
->with
== without_flags
)
13498 without_hex
|= elf_flags_to_names
[i
].flag_value
;
13505 info
->callbacks
->einfo
13506 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
13510 flaginfo
->flags_initialized
= TRUE
;
13511 flaginfo
->only_with_flags
|= with_hex
;
13512 flaginfo
->not_with_flags
|= without_hex
;
13515 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
13518 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
13524 struct alloc_got_off_arg
{
13526 struct bfd_link_info
*info
;
13529 /* We need a special top-level link routine to convert got reference counts
13530 to real got offsets. */
13533 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
13535 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
13536 bfd
*obfd
= gofarg
->info
->output_bfd
;
13537 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13539 if (h
->got
.refcount
> 0)
13541 h
->got
.offset
= gofarg
->gotoff
;
13542 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
13545 h
->got
.offset
= (bfd_vma
) -1;
13550 /* And an accompanying bit to work out final got entry offsets once
13551 we're done. Should be called from final_link. */
13554 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
13555 struct bfd_link_info
*info
)
13558 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13560 struct alloc_got_off_arg gofarg
;
13562 BFD_ASSERT (abfd
== info
->output_bfd
);
13564 if (! is_elf_hash_table (info
->hash
))
13567 /* The GOT offset is relative to the .got section, but the GOT header is
13568 put into the .got.plt section, if the backend uses it. */
13569 if (bed
->want_got_plt
)
13572 gotoff
= bed
->got_header_size
;
13574 /* Do the local .got entries first. */
13575 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
13577 bfd_signed_vma
*local_got
;
13578 size_t j
, locsymcount
;
13579 Elf_Internal_Shdr
*symtab_hdr
;
13581 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
13584 local_got
= elf_local_got_refcounts (i
);
13588 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
13589 if (elf_bad_symtab (i
))
13590 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13592 locsymcount
= symtab_hdr
->sh_info
;
13594 for (j
= 0; j
< locsymcount
; ++j
)
13596 if (local_got
[j
] > 0)
13598 local_got
[j
] = gotoff
;
13599 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
13602 local_got
[j
] = (bfd_vma
) -1;
13606 /* Then the global .got entries. .plt refcounts are handled by
13607 adjust_dynamic_symbol */
13608 gofarg
.gotoff
= gotoff
;
13609 gofarg
.info
= info
;
13610 elf_link_hash_traverse (elf_hash_table (info
),
13611 elf_gc_allocate_got_offsets
,
13616 /* Many folk need no more in the way of final link than this, once
13617 got entry reference counting is enabled. */
13620 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
13622 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
13625 /* Invoke the regular ELF backend linker to do all the work. */
13626 return bfd_elf_final_link (abfd
, info
);
13630 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
13632 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
13634 if (rcookie
->bad_symtab
)
13635 rcookie
->rel
= rcookie
->rels
;
13637 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
13639 unsigned long r_symndx
;
13641 if (! rcookie
->bad_symtab
)
13642 if (rcookie
->rel
->r_offset
> offset
)
13644 if (rcookie
->rel
->r_offset
!= offset
)
13647 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
13648 if (r_symndx
== STN_UNDEF
)
13651 if (r_symndx
>= rcookie
->locsymcount
13652 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13654 struct elf_link_hash_entry
*h
;
13656 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
13658 while (h
->root
.type
== bfd_link_hash_indirect
13659 || h
->root
.type
== bfd_link_hash_warning
)
13660 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13662 if ((h
->root
.type
== bfd_link_hash_defined
13663 || h
->root
.type
== bfd_link_hash_defweak
)
13664 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
13665 || h
->root
.u
.def
.section
->kept_section
!= NULL
13666 || discarded_section (h
->root
.u
.def
.section
)))
13671 /* It's not a relocation against a global symbol,
13672 but it could be a relocation against a local
13673 symbol for a discarded section. */
13675 Elf_Internal_Sym
*isym
;
13677 /* Need to: get the symbol; get the section. */
13678 isym
= &rcookie
->locsyms
[r_symndx
];
13679 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
13681 && (isec
->kept_section
!= NULL
13682 || discarded_section (isec
)))
13690 /* Discard unneeded references to discarded sections.
13691 Returns -1 on error, 1 if any section's size was changed, 0 if
13692 nothing changed. This function assumes that the relocations are in
13693 sorted order, which is true for all known assemblers. */
13696 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
13698 struct elf_reloc_cookie cookie
;
13703 if (info
->traditional_format
13704 || !is_elf_hash_table (info
->hash
))
13707 o
= bfd_get_section_by_name (output_bfd
, ".stab");
13712 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13715 || i
->reloc_count
== 0
13716 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
13720 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13723 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13726 if (_bfd_discard_section_stabs (abfd
, i
,
13727 elf_section_data (i
)->sec_info
,
13728 bfd_elf_reloc_symbol_deleted_p
,
13732 fini_reloc_cookie_for_section (&cookie
, i
);
13737 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
13738 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
13743 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13749 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13752 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13755 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
13756 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
13757 bfd_elf_reloc_symbol_deleted_p
,
13761 fini_reloc_cookie_for_section (&cookie
, i
);
13765 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
13767 const struct elf_backend_data
*bed
;
13769 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13772 bed
= get_elf_backend_data (abfd
);
13774 if (bed
->elf_backend_discard_info
!= NULL
)
13776 if (!init_reloc_cookie (&cookie
, info
, abfd
))
13779 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
13782 fini_reloc_cookie (&cookie
, abfd
);
13786 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
13787 _bfd_elf_end_eh_frame_parsing (info
);
13789 if (info
->eh_frame_hdr_type
13790 && !bfd_link_relocatable (info
)
13791 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
13798 _bfd_elf_section_already_linked (bfd
*abfd
,
13800 struct bfd_link_info
*info
)
13803 const char *name
, *key
;
13804 struct bfd_section_already_linked
*l
;
13805 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
13807 if (sec
->output_section
== bfd_abs_section_ptr
)
13810 flags
= sec
->flags
;
13812 /* Return if it isn't a linkonce section. A comdat group section
13813 also has SEC_LINK_ONCE set. */
13814 if ((flags
& SEC_LINK_ONCE
) == 0)
13817 /* Don't put group member sections on our list of already linked
13818 sections. They are handled as a group via their group section. */
13819 if (elf_sec_group (sec
) != NULL
)
13822 /* For a SHT_GROUP section, use the group signature as the key. */
13824 if ((flags
& SEC_GROUP
) != 0
13825 && elf_next_in_group (sec
) != NULL
13826 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
13827 key
= elf_group_name (elf_next_in_group (sec
));
13830 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13831 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
13832 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
13835 /* Must be a user linkonce section that doesn't follow gcc's
13836 naming convention. In this case we won't be matching
13837 single member groups. */
13841 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
13843 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13845 /* We may have 2 different types of sections on the list: group
13846 sections with a signature of <key> (<key> is some string),
13847 and linkonce sections named .gnu.linkonce.<type>.<key>.
13848 Match like sections. LTO plugin sections are an exception.
13849 They are always named .gnu.linkonce.t.<key> and match either
13850 type of section. */
13851 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
13852 && ((flags
& SEC_GROUP
) != 0
13853 || strcmp (name
, l
->sec
->name
) == 0))
13854 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0)
13856 /* The section has already been linked. See if we should
13857 issue a warning. */
13858 if (!_bfd_handle_already_linked (sec
, l
, info
))
13861 if (flags
& SEC_GROUP
)
13863 asection
*first
= elf_next_in_group (sec
);
13864 asection
*s
= first
;
13868 s
->output_section
= bfd_abs_section_ptr
;
13869 /* Record which group discards it. */
13870 s
->kept_section
= l
->sec
;
13871 s
= elf_next_in_group (s
);
13872 /* These lists are circular. */
13882 /* A single member comdat group section may be discarded by a
13883 linkonce section and vice versa. */
13884 if ((flags
& SEC_GROUP
) != 0)
13886 asection
*first
= elf_next_in_group (sec
);
13888 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
13889 /* Check this single member group against linkonce sections. */
13890 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13891 if ((l
->sec
->flags
& SEC_GROUP
) == 0
13892 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
13894 first
->output_section
= bfd_abs_section_ptr
;
13895 first
->kept_section
= l
->sec
;
13896 sec
->output_section
= bfd_abs_section_ptr
;
13901 /* Check this linkonce section against single member groups. */
13902 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13903 if (l
->sec
->flags
& SEC_GROUP
)
13905 asection
*first
= elf_next_in_group (l
->sec
);
13908 && elf_next_in_group (first
) == first
13909 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
13911 sec
->output_section
= bfd_abs_section_ptr
;
13912 sec
->kept_section
= first
;
13917 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13918 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13919 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13920 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13921 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13922 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13923 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13924 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13925 The reverse order cannot happen as there is never a bfd with only the
13926 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13927 matter as here were are looking only for cross-bfd sections. */
13929 if ((flags
& SEC_GROUP
) == 0 && CONST_STRNEQ (name
, ".gnu.linkonce.r."))
13930 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13931 if ((l
->sec
->flags
& SEC_GROUP
) == 0
13932 && CONST_STRNEQ (l
->sec
->name
, ".gnu.linkonce.t."))
13934 if (abfd
!= l
->sec
->owner
)
13935 sec
->output_section
= bfd_abs_section_ptr
;
13939 /* This is the first section with this name. Record it. */
13940 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
13941 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
13942 return sec
->output_section
== bfd_abs_section_ptr
;
13946 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
13948 return sym
->st_shndx
== SHN_COMMON
;
13952 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
13958 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
13960 return bfd_com_section_ptr
;
13964 _bfd_elf_default_got_elt_size (bfd
*abfd
,
13965 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13966 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
13967 bfd
*ibfd ATTRIBUTE_UNUSED
,
13968 unsigned long symndx ATTRIBUTE_UNUSED
)
13970 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13971 return bed
->s
->arch_size
/ 8;
13974 /* Routines to support the creation of dynamic relocs. */
13976 /* Returns the name of the dynamic reloc section associated with SEC. */
13978 static const char *
13979 get_dynamic_reloc_section_name (bfd
* abfd
,
13981 bfd_boolean is_rela
)
13984 const char *old_name
= bfd_get_section_name (NULL
, sec
);
13985 const char *prefix
= is_rela
? ".rela" : ".rel";
13987 if (old_name
== NULL
)
13990 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
13991 sprintf (name
, "%s%s", prefix
, old_name
);
13996 /* Returns the dynamic reloc section associated with SEC.
13997 If necessary compute the name of the dynamic reloc section based
13998 on SEC's name (looked up in ABFD's string table) and the setting
14002 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
14004 bfd_boolean is_rela
)
14006 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14008 if (reloc_sec
== NULL
)
14010 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14014 reloc_sec
= bfd_get_linker_section (abfd
, name
);
14016 if (reloc_sec
!= NULL
)
14017 elf_section_data (sec
)->sreloc
= reloc_sec
;
14024 /* Returns the dynamic reloc section associated with SEC. If the
14025 section does not exist it is created and attached to the DYNOBJ
14026 bfd and stored in the SRELOC field of SEC's elf_section_data
14029 ALIGNMENT is the alignment for the newly created section and
14030 IS_RELA defines whether the name should be .rela.<SEC's name>
14031 or .rel.<SEC's name>. The section name is looked up in the
14032 string table associated with ABFD. */
14035 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
14037 unsigned int alignment
,
14039 bfd_boolean is_rela
)
14041 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14043 if (reloc_sec
== NULL
)
14045 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14050 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
14052 if (reloc_sec
== NULL
)
14054 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
14055 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
14056 if ((sec
->flags
& SEC_ALLOC
) != 0)
14057 flags
|= SEC_ALLOC
| SEC_LOAD
;
14059 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
14060 if (reloc_sec
!= NULL
)
14062 /* _bfd_elf_get_sec_type_attr chooses a section type by
14063 name. Override as it may be wrong, eg. for a user
14064 section named "auto" we'll get ".relauto" which is
14065 seen to be a .rela section. */
14066 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
14067 if (! bfd_set_section_alignment (dynobj
, reloc_sec
, alignment
))
14072 elf_section_data (sec
)->sreloc
= reloc_sec
;
14078 /* Copy the ELF symbol type and other attributes for a linker script
14079 assignment from HSRC to HDEST. Generally this should be treated as
14080 if we found a strong non-dynamic definition for HDEST (except that
14081 ld ignores multiple definition errors). */
14083 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
14084 struct bfd_link_hash_entry
*hdest
,
14085 struct bfd_link_hash_entry
*hsrc
)
14087 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
14088 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
14089 Elf_Internal_Sym isym
;
14091 ehdest
->type
= ehsrc
->type
;
14092 ehdest
->target_internal
= ehsrc
->target_internal
;
14094 isym
.st_other
= ehsrc
->other
;
14095 elf_merge_st_other (abfd
, ehdest
, &isym
, NULL
, TRUE
, FALSE
);
14098 /* Append a RELA relocation REL to section S in BFD. */
14101 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14103 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14104 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
14105 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
14106 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
14109 /* Append a REL relocation REL to section S in BFD. */
14112 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14114 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14115 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
14116 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
14117 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);