1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2016 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
));
431 /* The .rel[a].bss section holds copy relocs. This section is not
432 normally needed. We need to create it here, though, so that the
433 linker will map it to an output section. We can't just create it
434 only if we need it, because we will not know whether we need it
435 until we have seen all the input files, and the first time the
436 main linker code calls BFD after examining all the input files
437 (size_dynamic_sections) the input sections have already been
438 mapped to the output sections. If the section turns out not to
439 be needed, we can discard it later. We will never need this
440 section when generating a shared object, since they do not use
442 if (! bfd_link_pic (info
))
444 s
= bfd_make_section_anyway_with_flags (abfd
,
445 (bed
->rela_plts_and_copies_p
446 ? ".rela.bss" : ".rel.bss"),
447 flags
| SEC_READONLY
);
449 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
457 /* Record a new dynamic symbol. We record the dynamic symbols as we
458 read the input files, since we need to have a list of all of them
459 before we can determine the final sizes of the output sections.
460 Note that we may actually call this function even though we are not
461 going to output any dynamic symbols; in some cases we know that a
462 symbol should be in the dynamic symbol table, but only if there is
466 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
467 struct elf_link_hash_entry
*h
)
469 if (h
->dynindx
== -1)
471 struct elf_strtab_hash
*dynstr
;
476 /* XXX: The ABI draft says the linker must turn hidden and
477 internal symbols into STB_LOCAL symbols when producing the
478 DSO. However, if ld.so honors st_other in the dynamic table,
479 this would not be necessary. */
480 switch (ELF_ST_VISIBILITY (h
->other
))
484 if (h
->root
.type
!= bfd_link_hash_undefined
485 && h
->root
.type
!= bfd_link_hash_undefweak
)
488 if (!elf_hash_table (info
)->is_relocatable_executable
)
496 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
497 ++elf_hash_table (info
)->dynsymcount
;
499 dynstr
= elf_hash_table (info
)->dynstr
;
502 /* Create a strtab to hold the dynamic symbol names. */
503 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
508 /* We don't put any version information in the dynamic string
510 name
= h
->root
.root
.string
;
511 p
= strchr (name
, ELF_VER_CHR
);
513 /* We know that the p points into writable memory. In fact,
514 there are only a few symbols that have read-only names, being
515 those like _GLOBAL_OFFSET_TABLE_ that are created specially
516 by the backends. Most symbols will have names pointing into
517 an ELF string table read from a file, or to objalloc memory. */
520 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
525 if (indx
== (size_t) -1)
527 h
->dynstr_index
= indx
;
533 /* Mark a symbol dynamic. */
536 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
537 struct elf_link_hash_entry
*h
,
538 Elf_Internal_Sym
*sym
)
540 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
542 /* It may be called more than once on the same H. */
543 if(h
->dynamic
|| bfd_link_relocatable (info
))
546 if ((info
->dynamic_data
547 && (h
->type
== STT_OBJECT
548 || h
->type
== STT_COMMON
550 && (ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
551 || ELF_ST_TYPE (sym
->st_info
) == STT_COMMON
))))
553 && h
->root
.type
== bfd_link_hash_new
554 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
558 /* Record an assignment to a symbol made by a linker script. We need
559 this in case some dynamic object refers to this symbol. */
562 bfd_elf_record_link_assignment (bfd
*output_bfd
,
563 struct bfd_link_info
*info
,
568 struct elf_link_hash_entry
*h
, *hv
;
569 struct elf_link_hash_table
*htab
;
570 const struct elf_backend_data
*bed
;
572 if (!is_elf_hash_table (info
->hash
))
575 htab
= elf_hash_table (info
);
576 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
580 if (h
->root
.type
== bfd_link_hash_warning
)
581 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
583 if (h
->versioned
== unknown
)
585 /* Set versioned if symbol version is unknown. */
586 char *version
= strrchr (name
, ELF_VER_CHR
);
589 if (version
> name
&& version
[-1] != ELF_VER_CHR
)
590 h
->versioned
= versioned_hidden
;
592 h
->versioned
= versioned
;
596 switch (h
->root
.type
)
598 case bfd_link_hash_defined
:
599 case bfd_link_hash_defweak
:
600 case bfd_link_hash_common
:
602 case bfd_link_hash_undefweak
:
603 case bfd_link_hash_undefined
:
604 /* Since we're defining the symbol, don't let it seem to have not
605 been defined. record_dynamic_symbol and size_dynamic_sections
606 may depend on this. */
607 h
->root
.type
= bfd_link_hash_new
;
608 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
609 bfd_link_repair_undef_list (&htab
->root
);
611 case bfd_link_hash_new
:
612 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
615 case bfd_link_hash_indirect
:
616 /* We had a versioned symbol in a dynamic library. We make the
617 the versioned symbol point to this one. */
618 bed
= get_elf_backend_data (output_bfd
);
620 while (hv
->root
.type
== bfd_link_hash_indirect
621 || hv
->root
.type
== bfd_link_hash_warning
)
622 hv
= (struct elf_link_hash_entry
*) hv
->root
.u
.i
.link
;
623 /* We don't need to update h->root.u since linker will set them
625 h
->root
.type
= bfd_link_hash_undefined
;
626 hv
->root
.type
= bfd_link_hash_indirect
;
627 hv
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
628 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hv
);
635 /* If this symbol is being provided by the linker script, and it is
636 currently defined by a dynamic object, but not by a regular
637 object, then mark it as undefined so that the generic linker will
638 force the correct value. */
642 h
->root
.type
= bfd_link_hash_undefined
;
644 /* If this symbol is not being provided by the linker script, and it is
645 currently defined by a dynamic object, but not by a regular object,
646 then clear out any version information because the symbol will not be
647 associated with the dynamic object any more. */
651 h
->verinfo
.verdef
= NULL
;
657 bed
= get_elf_backend_data (output_bfd
);
658 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
659 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
660 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
663 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
665 if (!bfd_link_relocatable (info
)
667 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
668 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
673 || bfd_link_dll (info
)
674 || elf_hash_table (info
)->is_relocatable_executable
)
677 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
680 /* If this is a weak defined symbol, and we know a corresponding
681 real symbol from the same dynamic object, make sure the real
682 symbol is also made into a dynamic symbol. */
683 if (h
->u
.weakdef
!= NULL
684 && h
->u
.weakdef
->dynindx
== -1)
686 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
694 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
695 success, and 2 on a failure caused by attempting to record a symbol
696 in a discarded section, eg. a discarded link-once section symbol. */
699 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
704 struct elf_link_local_dynamic_entry
*entry
;
705 struct elf_link_hash_table
*eht
;
706 struct elf_strtab_hash
*dynstr
;
709 Elf_External_Sym_Shndx eshndx
;
710 char esym
[sizeof (Elf64_External_Sym
)];
712 if (! is_elf_hash_table (info
->hash
))
715 /* See if the entry exists already. */
716 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
717 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
720 amt
= sizeof (*entry
);
721 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
725 /* Go find the symbol, so that we can find it's name. */
726 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
727 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
729 bfd_release (input_bfd
, entry
);
733 if (entry
->isym
.st_shndx
!= SHN_UNDEF
734 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
738 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
739 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
741 /* We can still bfd_release here as nothing has done another
742 bfd_alloc. We can't do this later in this function. */
743 bfd_release (input_bfd
, entry
);
748 name
= (bfd_elf_string_from_elf_section
749 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
750 entry
->isym
.st_name
));
752 dynstr
= elf_hash_table (info
)->dynstr
;
755 /* Create a strtab to hold the dynamic symbol names. */
756 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
761 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
762 if (dynstr_index
== (size_t) -1)
764 entry
->isym
.st_name
= dynstr_index
;
766 eht
= elf_hash_table (info
);
768 entry
->next
= eht
->dynlocal
;
769 eht
->dynlocal
= entry
;
770 entry
->input_bfd
= input_bfd
;
771 entry
->input_indx
= input_indx
;
774 /* Whatever binding the symbol had before, it's now local. */
776 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
778 /* The dynindx will be set at the end of size_dynamic_sections. */
783 /* Return the dynindex of a local dynamic symbol. */
786 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
790 struct elf_link_local_dynamic_entry
*e
;
792 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
793 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
798 /* This function is used to renumber the dynamic symbols, if some of
799 them are removed because they are marked as local. This is called
800 via elf_link_hash_traverse. */
803 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
806 size_t *count
= (size_t *) data
;
811 if (h
->dynindx
!= -1)
812 h
->dynindx
= ++(*count
);
818 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
819 STB_LOCAL binding. */
822 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
825 size_t *count
= (size_t *) data
;
827 if (!h
->forced_local
)
830 if (h
->dynindx
!= -1)
831 h
->dynindx
= ++(*count
);
836 /* Return true if the dynamic symbol for a given section should be
837 omitted when creating a shared library. */
839 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
840 struct bfd_link_info
*info
,
843 struct elf_link_hash_table
*htab
;
846 switch (elf_section_data (p
)->this_hdr
.sh_type
)
850 /* If sh_type is yet undecided, assume it could be
851 SHT_PROGBITS/SHT_NOBITS. */
853 htab
= elf_hash_table (info
);
854 if (p
== htab
->tls_sec
)
857 if (htab
->text_index_section
!= NULL
)
858 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
860 return (htab
->dynobj
!= NULL
861 && (ip
= bfd_get_linker_section (htab
->dynobj
, p
->name
)) != NULL
862 && ip
->output_section
== p
);
864 /* There shouldn't be section relative relocations
865 against any other section. */
871 /* Assign dynsym indices. In a shared library we generate a section
872 symbol for each output section, which come first. Next come symbols
873 which have been forced to local binding. Then all of the back-end
874 allocated local dynamic syms, followed by the rest of the global
878 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
879 struct bfd_link_info
*info
,
880 unsigned long *section_sym_count
)
882 unsigned long dynsymcount
= 0;
884 if (bfd_link_pic (info
)
885 || elf_hash_table (info
)->is_relocatable_executable
)
887 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
889 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
890 if ((p
->flags
& SEC_EXCLUDE
) == 0
891 && (p
->flags
& SEC_ALLOC
) != 0
892 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
893 elf_section_data (p
)->dynindx
= ++dynsymcount
;
895 elf_section_data (p
)->dynindx
= 0;
897 *section_sym_count
= dynsymcount
;
899 elf_link_hash_traverse (elf_hash_table (info
),
900 elf_link_renumber_local_hash_table_dynsyms
,
903 if (elf_hash_table (info
)->dynlocal
)
905 struct elf_link_local_dynamic_entry
*p
;
906 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
907 p
->dynindx
= ++dynsymcount
;
909 elf_hash_table (info
)->local_dynsymcount
= dynsymcount
;
911 elf_link_hash_traverse (elf_hash_table (info
),
912 elf_link_renumber_hash_table_dynsyms
,
915 /* There is an unused NULL entry at the head of the table which we
916 must account for in our count even if the table is empty since it
917 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
921 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
925 /* Merge st_other field. */
928 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
929 const Elf_Internal_Sym
*isym
, asection
*sec
,
930 bfd_boolean definition
, bfd_boolean dynamic
)
932 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
934 /* If st_other has a processor-specific meaning, specific
935 code might be needed here. */
936 if (bed
->elf_backend_merge_symbol_attribute
)
937 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
942 unsigned symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
943 unsigned hvis
= ELF_ST_VISIBILITY (h
->other
);
945 /* Keep the most constraining visibility. Leave the remainder
946 of the st_other field to elf_backend_merge_symbol_attribute. */
947 if (symvis
- 1 < hvis
- 1)
948 h
->other
= symvis
| (h
->other
& ~ELF_ST_VISIBILITY (-1));
951 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_DEFAULT
952 && (sec
->flags
& SEC_READONLY
) == 0)
953 h
->protected_def
= 1;
956 /* This function is called when we want to merge a new symbol with an
957 existing symbol. It handles the various cases which arise when we
958 find a definition in a dynamic object, or when there is already a
959 definition in a dynamic object. The new symbol is described by
960 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
961 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
962 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
963 of an old common symbol. We set OVERRIDE if the old symbol is
964 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
965 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
966 to change. By OK to change, we mean that we shouldn't warn if the
967 type or size does change. */
970 _bfd_elf_merge_symbol (bfd
*abfd
,
971 struct bfd_link_info
*info
,
973 Elf_Internal_Sym
*sym
,
976 struct elf_link_hash_entry
**sym_hash
,
978 bfd_boolean
*pold_weak
,
979 unsigned int *pold_alignment
,
981 bfd_boolean
*override
,
982 bfd_boolean
*type_change_ok
,
983 bfd_boolean
*size_change_ok
,
984 bfd_boolean
*matched
)
986 asection
*sec
, *oldsec
;
987 struct elf_link_hash_entry
*h
;
988 struct elf_link_hash_entry
*hi
;
989 struct elf_link_hash_entry
*flip
;
992 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
993 bfd_boolean newweak
, oldweak
, newfunc
, oldfunc
;
994 const struct elf_backend_data
*bed
;
1001 bind
= ELF_ST_BIND (sym
->st_info
);
1003 if (! bfd_is_und_section (sec
))
1004 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
1006 h
= ((struct elf_link_hash_entry
*)
1007 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
1012 bed
= get_elf_backend_data (abfd
);
1014 /* NEW_VERSION is the symbol version of the new symbol. */
1015 if (h
->versioned
!= unversioned
)
1017 /* Symbol version is unknown or versioned. */
1018 new_version
= strrchr (name
, ELF_VER_CHR
);
1021 if (h
->versioned
== unknown
)
1023 if (new_version
> name
&& new_version
[-1] != ELF_VER_CHR
)
1024 h
->versioned
= versioned_hidden
;
1026 h
->versioned
= versioned
;
1029 if (new_version
[0] == '\0')
1033 h
->versioned
= unversioned
;
1038 /* For merging, we only care about real symbols. But we need to make
1039 sure that indirect symbol dynamic flags are updated. */
1041 while (h
->root
.type
== bfd_link_hash_indirect
1042 || h
->root
.type
== bfd_link_hash_warning
)
1043 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1047 if (hi
== h
|| h
->root
.type
== bfd_link_hash_new
)
1051 /* OLD_HIDDEN is true if the existing symbol is only visible
1052 to the symbol with the same symbol version. NEW_HIDDEN is
1053 true if the new symbol is only visible to the symbol with
1054 the same symbol version. */
1055 bfd_boolean old_hidden
= h
->versioned
== versioned_hidden
;
1056 bfd_boolean new_hidden
= hi
->versioned
== versioned_hidden
;
1057 if (!old_hidden
&& !new_hidden
)
1058 /* The new symbol matches the existing symbol if both
1063 /* OLD_VERSION is the symbol version of the existing
1067 if (h
->versioned
>= versioned
)
1068 old_version
= strrchr (h
->root
.root
.string
,
1073 /* The new symbol matches the existing symbol if they
1074 have the same symbol version. */
1075 *matched
= (old_version
== new_version
1076 || (old_version
!= NULL
1077 && new_version
!= NULL
1078 && strcmp (old_version
, new_version
) == 0));
1083 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1088 switch (h
->root
.type
)
1093 case bfd_link_hash_undefined
:
1094 case bfd_link_hash_undefweak
:
1095 oldbfd
= h
->root
.u
.undef
.abfd
;
1098 case bfd_link_hash_defined
:
1099 case bfd_link_hash_defweak
:
1100 oldbfd
= h
->root
.u
.def
.section
->owner
;
1101 oldsec
= h
->root
.u
.def
.section
;
1104 case bfd_link_hash_common
:
1105 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1106 oldsec
= h
->root
.u
.c
.p
->section
;
1108 *pold_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1111 if (poldbfd
&& *poldbfd
== NULL
)
1114 /* Differentiate strong and weak symbols. */
1115 newweak
= bind
== STB_WEAK
;
1116 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1117 || h
->root
.type
== bfd_link_hash_undefweak
);
1119 *pold_weak
= oldweak
;
1121 /* This code is for coping with dynamic objects, and is only useful
1122 if we are doing an ELF link. */
1123 if (!(*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
1126 /* We have to check it for every instance since the first few may be
1127 references and not all compilers emit symbol type for undefined
1129 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1131 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1132 respectively, is from a dynamic object. */
1134 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1136 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1137 syms and defined syms in dynamic libraries respectively.
1138 ref_dynamic on the other hand can be set for a symbol defined in
1139 a dynamic library, and def_dynamic may not be set; When the
1140 definition in a dynamic lib is overridden by a definition in the
1141 executable use of the symbol in the dynamic lib becomes a
1142 reference to the executable symbol. */
1145 if (bfd_is_und_section (sec
))
1147 if (bind
!= STB_WEAK
)
1149 h
->ref_dynamic_nonweak
= 1;
1150 hi
->ref_dynamic_nonweak
= 1;
1155 /* Update the existing symbol only if they match. */
1158 hi
->dynamic_def
= 1;
1162 /* If we just created the symbol, mark it as being an ELF symbol.
1163 Other than that, there is nothing to do--there is no merge issue
1164 with a newly defined symbol--so we just return. */
1166 if (h
->root
.type
== bfd_link_hash_new
)
1172 /* In cases involving weak versioned symbols, we may wind up trying
1173 to merge a symbol with itself. Catch that here, to avoid the
1174 confusion that results if we try to override a symbol with
1175 itself. The additional tests catch cases like
1176 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1177 dynamic object, which we do want to handle here. */
1179 && (newweak
|| oldweak
)
1180 && ((abfd
->flags
& DYNAMIC
) == 0
1181 || !h
->def_regular
))
1186 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1187 else if (oldsec
!= NULL
)
1189 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1190 indices used by MIPS ELF. */
1191 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1194 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1195 respectively, appear to be a definition rather than reference. */
1197 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1199 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1200 && h
->root
.type
!= bfd_link_hash_undefweak
1201 && h
->root
.type
!= bfd_link_hash_common
);
1203 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1204 respectively, appear to be a function. */
1206 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1207 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1209 oldfunc
= (h
->type
!= STT_NOTYPE
1210 && bed
->is_function_type (h
->type
));
1212 /* If creating a default indirect symbol ("foo" or "foo@") from a
1213 dynamic versioned definition ("foo@@") skip doing so if there is
1214 an existing regular definition with a different type. We don't
1215 want, for example, a "time" variable in the executable overriding
1216 a "time" function in a shared library. */
1217 if (pold_alignment
== NULL
1221 && (olddef
|| h
->root
.type
== bfd_link_hash_common
)
1222 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1223 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1224 && h
->type
!= STT_NOTYPE
1225 && !(newfunc
&& oldfunc
))
1231 /* Check TLS symbols. We don't check undefined symbols introduced
1232 by "ld -u" which have no type (and oldbfd NULL), and we don't
1233 check symbols from plugins because they also have no type. */
1235 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1236 && (abfd
->flags
& BFD_PLUGIN
) == 0
1237 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1238 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1241 bfd_boolean ntdef
, tdef
;
1242 asection
*ntsec
, *tsec
;
1244 if (h
->type
== STT_TLS
)
1265 /* xgettext:c-format */
1266 (_("%s: TLS definition in %B section %A "
1267 "mismatches non-TLS definition in %B section %A"),
1268 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
1269 else if (!tdef
&& !ntdef
)
1271 /* xgettext:c-format */
1272 (_("%s: TLS reference in %B "
1273 "mismatches non-TLS reference in %B"),
1274 tbfd
, ntbfd
, h
->root
.root
.string
);
1277 /* xgettext:c-format */
1278 (_("%s: TLS definition in %B section %A "
1279 "mismatches non-TLS reference in %B"),
1280 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
1283 /* xgettext:c-format */
1284 (_("%s: TLS reference in %B "
1285 "mismatches non-TLS definition in %B section %A"),
1286 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
1288 bfd_set_error (bfd_error_bad_value
);
1292 /* If the old symbol has non-default visibility, we ignore the new
1293 definition from a dynamic object. */
1295 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1296 && !bfd_is_und_section (sec
))
1299 /* Make sure this symbol is dynamic. */
1301 hi
->ref_dynamic
= 1;
1302 /* A protected symbol has external availability. Make sure it is
1303 recorded as dynamic.
1305 FIXME: Should we check type and size for protected symbol? */
1306 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1307 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1312 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1315 /* If the new symbol with non-default visibility comes from a
1316 relocatable file and the old definition comes from a dynamic
1317 object, we remove the old definition. */
1318 if (hi
->root
.type
== bfd_link_hash_indirect
)
1320 /* Handle the case where the old dynamic definition is
1321 default versioned. We need to copy the symbol info from
1322 the symbol with default version to the normal one if it
1323 was referenced before. */
1326 hi
->root
.type
= h
->root
.type
;
1327 h
->root
.type
= bfd_link_hash_indirect
;
1328 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1330 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1331 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1333 /* If the new symbol is hidden or internal, completely undo
1334 any dynamic link state. */
1335 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1336 h
->forced_local
= 0;
1343 /* FIXME: Should we check type and size for protected symbol? */
1353 /* If the old symbol was undefined before, then it will still be
1354 on the undefs list. If the new symbol is undefined or
1355 common, we can't make it bfd_link_hash_new here, because new
1356 undefined or common symbols will be added to the undefs list
1357 by _bfd_generic_link_add_one_symbol. Symbols may not be
1358 added twice to the undefs list. Also, if the new symbol is
1359 undefweak then we don't want to lose the strong undef. */
1360 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1362 h
->root
.type
= bfd_link_hash_undefined
;
1363 h
->root
.u
.undef
.abfd
= abfd
;
1367 h
->root
.type
= bfd_link_hash_new
;
1368 h
->root
.u
.undef
.abfd
= NULL
;
1371 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1373 /* If the new symbol is hidden or internal, completely undo
1374 any dynamic link state. */
1375 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1376 h
->forced_local
= 0;
1382 /* FIXME: Should we check type and size for protected symbol? */
1388 /* If a new weak symbol definition comes from a regular file and the
1389 old symbol comes from a dynamic library, we treat the new one as
1390 strong. Similarly, an old weak symbol definition from a regular
1391 file is treated as strong when the new symbol comes from a dynamic
1392 library. Further, an old weak symbol from a dynamic library is
1393 treated as strong if the new symbol is from a dynamic library.
1394 This reflects the way glibc's ld.so works.
1396 Do this before setting *type_change_ok or *size_change_ok so that
1397 we warn properly when dynamic library symbols are overridden. */
1399 if (newdef
&& !newdyn
&& olddyn
)
1401 if (olddef
&& newdyn
)
1404 /* Allow changes between different types of function symbol. */
1405 if (newfunc
&& oldfunc
)
1406 *type_change_ok
= TRUE
;
1408 /* It's OK to change the type if either the existing symbol or the
1409 new symbol is weak. A type change is also OK if the old symbol
1410 is undefined and the new symbol is defined. */
1415 && h
->root
.type
== bfd_link_hash_undefined
))
1416 *type_change_ok
= TRUE
;
1418 /* It's OK to change the size if either the existing symbol or the
1419 new symbol is weak, or if the old symbol is undefined. */
1422 || h
->root
.type
== bfd_link_hash_undefined
)
1423 *size_change_ok
= TRUE
;
1425 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1426 symbol, respectively, appears to be a common symbol in a dynamic
1427 object. If a symbol appears in an uninitialized section, and is
1428 not weak, and is not a function, then it may be a common symbol
1429 which was resolved when the dynamic object was created. We want
1430 to treat such symbols specially, because they raise special
1431 considerations when setting the symbol size: if the symbol
1432 appears as a common symbol in a regular object, and the size in
1433 the regular object is larger, we must make sure that we use the
1434 larger size. This problematic case can always be avoided in C,
1435 but it must be handled correctly when using Fortran shared
1438 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1439 likewise for OLDDYNCOMMON and OLDDEF.
1441 Note that this test is just a heuristic, and that it is quite
1442 possible to have an uninitialized symbol in a shared object which
1443 is really a definition, rather than a common symbol. This could
1444 lead to some minor confusion when the symbol really is a common
1445 symbol in some regular object. However, I think it will be
1451 && (sec
->flags
& SEC_ALLOC
) != 0
1452 && (sec
->flags
& SEC_LOAD
) == 0
1455 newdyncommon
= TRUE
;
1457 newdyncommon
= FALSE
;
1461 && h
->root
.type
== bfd_link_hash_defined
1463 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1464 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1467 olddyncommon
= TRUE
;
1469 olddyncommon
= FALSE
;
1471 /* We now know everything about the old and new symbols. We ask the
1472 backend to check if we can merge them. */
1473 if (bed
->merge_symbol
!= NULL
)
1475 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1480 /* If both the old and the new symbols look like common symbols in a
1481 dynamic object, set the size of the symbol to the larger of the
1486 && sym
->st_size
!= h
->size
)
1488 /* Since we think we have two common symbols, issue a multiple
1489 common warning if desired. Note that we only warn if the
1490 size is different. If the size is the same, we simply let
1491 the old symbol override the new one as normally happens with
1492 symbols defined in dynamic objects. */
1494 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1495 bfd_link_hash_common
, sym
->st_size
);
1496 if (sym
->st_size
> h
->size
)
1497 h
->size
= sym
->st_size
;
1499 *size_change_ok
= TRUE
;
1502 /* If we are looking at a dynamic object, and we have found a
1503 definition, we need to see if the symbol was already defined by
1504 some other object. If so, we want to use the existing
1505 definition, and we do not want to report a multiple symbol
1506 definition error; we do this by clobbering *PSEC to be
1507 bfd_und_section_ptr.
1509 We treat a common symbol as a definition if the symbol in the
1510 shared library is a function, since common symbols always
1511 represent variables; this can cause confusion in principle, but
1512 any such confusion would seem to indicate an erroneous program or
1513 shared library. We also permit a common symbol in a regular
1514 object to override a weak symbol in a shared object. A common
1515 symbol in executable also overrides a symbol in a shared object. */
1520 || (h
->root
.type
== bfd_link_hash_common
1523 || (!olddyn
&& bfd_link_executable (info
))))))
1527 newdyncommon
= FALSE
;
1529 *psec
= sec
= bfd_und_section_ptr
;
1530 *size_change_ok
= TRUE
;
1532 /* If we get here when the old symbol is a common symbol, then
1533 we are explicitly letting it override a weak symbol or
1534 function in a dynamic object, and we don't want to warn about
1535 a type change. If the old symbol is a defined symbol, a type
1536 change warning may still be appropriate. */
1538 if (h
->root
.type
== bfd_link_hash_common
)
1539 *type_change_ok
= TRUE
;
1542 /* Handle the special case of an old common symbol merging with a
1543 new symbol which looks like a common symbol in a shared object.
1544 We change *PSEC and *PVALUE to make the new symbol look like a
1545 common symbol, and let _bfd_generic_link_add_one_symbol do the
1549 && h
->root
.type
== bfd_link_hash_common
)
1553 newdyncommon
= FALSE
;
1554 *pvalue
= sym
->st_size
;
1555 *psec
= sec
= bed
->common_section (oldsec
);
1556 *size_change_ok
= TRUE
;
1559 /* Skip weak definitions of symbols that are already defined. */
1560 if (newdef
&& olddef
&& newweak
)
1562 /* Don't skip new non-IR weak syms. */
1563 if (!(oldbfd
!= NULL
1564 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1565 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1571 /* Merge st_other. If the symbol already has a dynamic index,
1572 but visibility says it should not be visible, turn it into a
1574 elf_merge_st_other (abfd
, h
, sym
, sec
, newdef
, newdyn
);
1575 if (h
->dynindx
!= -1)
1576 switch (ELF_ST_VISIBILITY (h
->other
))
1580 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1585 /* If the old symbol is from a dynamic object, and the new symbol is
1586 a definition which is not from a dynamic object, then the new
1587 symbol overrides the old symbol. Symbols from regular files
1588 always take precedence over symbols from dynamic objects, even if
1589 they are defined after the dynamic object in the link.
1591 As above, we again permit a common symbol in a regular object to
1592 override a definition in a shared object if the shared object
1593 symbol is a function or is weak. */
1598 || (bfd_is_com_section (sec
)
1599 && (oldweak
|| oldfunc
)))
1604 /* Change the hash table entry to undefined, and let
1605 _bfd_generic_link_add_one_symbol do the right thing with the
1608 h
->root
.type
= bfd_link_hash_undefined
;
1609 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1610 *size_change_ok
= TRUE
;
1613 olddyncommon
= FALSE
;
1615 /* We again permit a type change when a common symbol may be
1616 overriding a function. */
1618 if (bfd_is_com_section (sec
))
1622 /* If a common symbol overrides a function, make sure
1623 that it isn't defined dynamically nor has type
1626 h
->type
= STT_NOTYPE
;
1628 *type_change_ok
= TRUE
;
1631 if (hi
->root
.type
== bfd_link_hash_indirect
)
1634 /* This union may have been set to be non-NULL when this symbol
1635 was seen in a dynamic object. We must force the union to be
1636 NULL, so that it is correct for a regular symbol. */
1637 h
->verinfo
.vertree
= NULL
;
1640 /* Handle the special case of a new common symbol merging with an
1641 old symbol that looks like it might be a common symbol defined in
1642 a shared object. Note that we have already handled the case in
1643 which a new common symbol should simply override the definition
1644 in the shared library. */
1647 && bfd_is_com_section (sec
)
1650 /* It would be best if we could set the hash table entry to a
1651 common symbol, but we don't know what to use for the section
1652 or the alignment. */
1653 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1654 bfd_link_hash_common
, sym
->st_size
);
1656 /* If the presumed common symbol in the dynamic object is
1657 larger, pretend that the new symbol has its size. */
1659 if (h
->size
> *pvalue
)
1662 /* We need to remember the alignment required by the symbol
1663 in the dynamic object. */
1664 BFD_ASSERT (pold_alignment
);
1665 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1668 olddyncommon
= FALSE
;
1670 h
->root
.type
= bfd_link_hash_undefined
;
1671 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1673 *size_change_ok
= TRUE
;
1674 *type_change_ok
= TRUE
;
1676 if (hi
->root
.type
== bfd_link_hash_indirect
)
1679 h
->verinfo
.vertree
= NULL
;
1684 /* Handle the case where we had a versioned symbol in a dynamic
1685 library and now find a definition in a normal object. In this
1686 case, we make the versioned symbol point to the normal one. */
1687 flip
->root
.type
= h
->root
.type
;
1688 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1689 h
->root
.type
= bfd_link_hash_indirect
;
1690 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1691 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1695 flip
->ref_dynamic
= 1;
1702 /* This function is called to create an indirect symbol from the
1703 default for the symbol with the default version if needed. The
1704 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1705 set DYNSYM if the new indirect symbol is dynamic. */
1708 _bfd_elf_add_default_symbol (bfd
*abfd
,
1709 struct bfd_link_info
*info
,
1710 struct elf_link_hash_entry
*h
,
1712 Elf_Internal_Sym
*sym
,
1716 bfd_boolean
*dynsym
)
1718 bfd_boolean type_change_ok
;
1719 bfd_boolean size_change_ok
;
1722 struct elf_link_hash_entry
*hi
;
1723 struct bfd_link_hash_entry
*bh
;
1724 const struct elf_backend_data
*bed
;
1725 bfd_boolean collect
;
1726 bfd_boolean dynamic
;
1727 bfd_boolean override
;
1729 size_t len
, shortlen
;
1731 bfd_boolean matched
;
1733 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1736 /* If this symbol has a version, and it is the default version, we
1737 create an indirect symbol from the default name to the fully
1738 decorated name. This will cause external references which do not
1739 specify a version to be bound to this version of the symbol. */
1740 p
= strchr (name
, ELF_VER_CHR
);
1741 if (h
->versioned
== unknown
)
1745 h
->versioned
= unversioned
;
1750 if (p
[1] != ELF_VER_CHR
)
1752 h
->versioned
= versioned_hidden
;
1756 h
->versioned
= versioned
;
1761 /* PR ld/19073: We may see an unversioned definition after the
1767 bed
= get_elf_backend_data (abfd
);
1768 collect
= bed
->collect
;
1769 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1771 shortlen
= p
- name
;
1772 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1773 if (shortname
== NULL
)
1775 memcpy (shortname
, name
, shortlen
);
1776 shortname
[shortlen
] = '\0';
1778 /* We are going to create a new symbol. Merge it with any existing
1779 symbol with this name. For the purposes of the merge, act as
1780 though we were defining the symbol we just defined, although we
1781 actually going to define an indirect symbol. */
1782 type_change_ok
= FALSE
;
1783 size_change_ok
= FALSE
;
1786 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1787 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1788 &type_change_ok
, &size_change_ok
, &matched
))
1794 if (hi
->def_regular
)
1796 /* If the undecorated symbol will have a version added by a
1797 script different to H, then don't indirect to/from the
1798 undecorated symbol. This isn't ideal because we may not yet
1799 have seen symbol versions, if given by a script on the
1800 command line rather than via --version-script. */
1801 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
1806 = bfd_find_version_for_sym (info
->version_info
,
1807 hi
->root
.root
.string
, &hide
);
1808 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
1810 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
1814 if (hi
->verinfo
.vertree
!= NULL
1815 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
1821 /* Add the default symbol if not performing a relocatable link. */
1822 if (! bfd_link_relocatable (info
))
1825 if (! (_bfd_generic_link_add_one_symbol
1826 (info
, abfd
, shortname
, BSF_INDIRECT
,
1827 bfd_ind_section_ptr
,
1828 0, name
, FALSE
, collect
, &bh
)))
1830 hi
= (struct elf_link_hash_entry
*) bh
;
1835 /* In this case the symbol named SHORTNAME is overriding the
1836 indirect symbol we want to add. We were planning on making
1837 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1838 is the name without a version. NAME is the fully versioned
1839 name, and it is the default version.
1841 Overriding means that we already saw a definition for the
1842 symbol SHORTNAME in a regular object, and it is overriding
1843 the symbol defined in the dynamic object.
1845 When this happens, we actually want to change NAME, the
1846 symbol we just added, to refer to SHORTNAME. This will cause
1847 references to NAME in the shared object to become references
1848 to SHORTNAME in the regular object. This is what we expect
1849 when we override a function in a shared object: that the
1850 references in the shared object will be mapped to the
1851 definition in the regular object. */
1853 while (hi
->root
.type
== bfd_link_hash_indirect
1854 || hi
->root
.type
== bfd_link_hash_warning
)
1855 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1857 h
->root
.type
= bfd_link_hash_indirect
;
1858 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1862 hi
->ref_dynamic
= 1;
1866 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1871 /* Now set HI to H, so that the following code will set the
1872 other fields correctly. */
1876 /* Check if HI is a warning symbol. */
1877 if (hi
->root
.type
== bfd_link_hash_warning
)
1878 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1880 /* If there is a duplicate definition somewhere, then HI may not
1881 point to an indirect symbol. We will have reported an error to
1882 the user in that case. */
1884 if (hi
->root
.type
== bfd_link_hash_indirect
)
1886 struct elf_link_hash_entry
*ht
;
1888 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1889 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
1891 /* A reference to the SHORTNAME symbol from a dynamic library
1892 will be satisfied by the versioned symbol at runtime. In
1893 effect, we have a reference to the versioned symbol. */
1894 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
1895 hi
->dynamic_def
|= ht
->dynamic_def
;
1897 /* See if the new flags lead us to realize that the symbol must
1903 if (! bfd_link_executable (info
)
1910 if (hi
->ref_regular
)
1916 /* We also need to define an indirection from the nondefault version
1920 len
= strlen (name
);
1921 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
1922 if (shortname
== NULL
)
1924 memcpy (shortname
, name
, shortlen
);
1925 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1927 /* Once again, merge with any existing symbol. */
1928 type_change_ok
= FALSE
;
1929 size_change_ok
= FALSE
;
1931 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1932 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1933 &type_change_ok
, &size_change_ok
, &matched
))
1941 /* Here SHORTNAME is a versioned name, so we don't expect to see
1942 the type of override we do in the case above unless it is
1943 overridden by a versioned definition. */
1944 if (hi
->root
.type
!= bfd_link_hash_defined
1945 && hi
->root
.type
!= bfd_link_hash_defweak
)
1947 /* xgettext:c-format */
1948 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1954 if (! (_bfd_generic_link_add_one_symbol
1955 (info
, abfd
, shortname
, BSF_INDIRECT
,
1956 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1958 hi
= (struct elf_link_hash_entry
*) bh
;
1960 /* If there is a duplicate definition somewhere, then HI may not
1961 point to an indirect symbol. We will have reported an error
1962 to the user in that case. */
1964 if (hi
->root
.type
== bfd_link_hash_indirect
)
1966 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
1967 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
1968 hi
->dynamic_def
|= h
->dynamic_def
;
1970 /* See if the new flags lead us to realize that the symbol
1976 if (! bfd_link_executable (info
)
1982 if (hi
->ref_regular
)
1992 /* This routine is used to export all defined symbols into the dynamic
1993 symbol table. It is called via elf_link_hash_traverse. */
1996 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1998 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2000 /* Ignore indirect symbols. These are added by the versioning code. */
2001 if (h
->root
.type
== bfd_link_hash_indirect
)
2004 /* Ignore this if we won't export it. */
2005 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2008 if (h
->dynindx
== -1
2009 && (h
->def_regular
|| h
->ref_regular
)
2010 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2011 h
->root
.root
.string
))
2013 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2023 /* Look through the symbols which are defined in other shared
2024 libraries and referenced here. Update the list of version
2025 dependencies. This will be put into the .gnu.version_r section.
2026 This function is called via elf_link_hash_traverse. */
2029 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2032 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2033 Elf_Internal_Verneed
*t
;
2034 Elf_Internal_Vernaux
*a
;
2037 /* We only care about symbols defined in shared objects with version
2042 || h
->verinfo
.verdef
== NULL
2043 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2044 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2047 /* See if we already know about this version. */
2048 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2052 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2055 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2056 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2062 /* This is a new version. Add it to tree we are building. */
2067 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2070 rinfo
->failed
= TRUE
;
2074 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2075 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2076 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2080 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2083 rinfo
->failed
= TRUE
;
2087 /* Note that we are copying a string pointer here, and testing it
2088 above. If bfd_elf_string_from_elf_section is ever changed to
2089 discard the string data when low in memory, this will have to be
2091 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2093 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2094 a
->vna_nextptr
= t
->vn_auxptr
;
2096 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2099 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2106 /* Figure out appropriate versions for all the symbols. We may not
2107 have the version number script until we have read all of the input
2108 files, so until that point we don't know which symbols should be
2109 local. This function is called via elf_link_hash_traverse. */
2112 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2114 struct elf_info_failed
*sinfo
;
2115 struct bfd_link_info
*info
;
2116 const struct elf_backend_data
*bed
;
2117 struct elf_info_failed eif
;
2120 sinfo
= (struct elf_info_failed
*) data
;
2123 /* Fix the symbol flags. */
2126 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2129 sinfo
->failed
= TRUE
;
2133 /* We only need version numbers for symbols defined in regular
2135 if (!h
->def_regular
)
2138 bed
= get_elf_backend_data (info
->output_bfd
);
2139 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2140 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2142 struct bfd_elf_version_tree
*t
;
2145 if (*p
== ELF_VER_CHR
)
2148 /* If there is no version string, we can just return out. */
2152 /* Look for the version. If we find it, it is no longer weak. */
2153 for (t
= sinfo
->info
->version_info
; t
!= NULL
; t
= t
->next
)
2155 if (strcmp (t
->name
, p
) == 0)
2159 struct bfd_elf_version_expr
*d
;
2161 len
= p
- h
->root
.root
.string
;
2162 alc
= (char *) bfd_malloc (len
);
2165 sinfo
->failed
= TRUE
;
2168 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2169 alc
[len
- 1] = '\0';
2170 if (alc
[len
- 2] == ELF_VER_CHR
)
2171 alc
[len
- 2] = '\0';
2173 h
->verinfo
.vertree
= t
;
2177 if (t
->globals
.list
!= NULL
)
2178 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2180 /* See if there is anything to force this symbol to
2182 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2184 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2187 && ! info
->export_dynamic
)
2188 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2196 /* If we are building an application, we need to create a
2197 version node for this version. */
2198 if (t
== NULL
&& bfd_link_executable (info
))
2200 struct bfd_elf_version_tree
**pp
;
2203 /* If we aren't going to export this symbol, we don't need
2204 to worry about it. */
2205 if (h
->dynindx
== -1)
2208 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2212 sinfo
->failed
= TRUE
;
2217 t
->name_indx
= (unsigned int) -1;
2221 /* Don't count anonymous version tag. */
2222 if (sinfo
->info
->version_info
!= NULL
2223 && sinfo
->info
->version_info
->vernum
== 0)
2225 for (pp
= &sinfo
->info
->version_info
;
2229 t
->vernum
= version_index
;
2233 h
->verinfo
.vertree
= t
;
2237 /* We could not find the version for a symbol when
2238 generating a shared archive. Return an error. */
2240 /* xgettext:c-format */
2241 (_("%B: version node not found for symbol %s"),
2242 info
->output_bfd
, h
->root
.root
.string
);
2243 bfd_set_error (bfd_error_bad_value
);
2244 sinfo
->failed
= TRUE
;
2249 /* If we don't have a version for this symbol, see if we can find
2251 if (h
->verinfo
.vertree
== NULL
&& sinfo
->info
->version_info
!= NULL
)
2256 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2257 h
->root
.root
.string
, &hide
);
2258 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2259 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2265 /* Read and swap the relocs from the section indicated by SHDR. This
2266 may be either a REL or a RELA section. The relocations are
2267 translated into RELA relocations and stored in INTERNAL_RELOCS,
2268 which should have already been allocated to contain enough space.
2269 The EXTERNAL_RELOCS are a buffer where the external form of the
2270 relocations should be stored.
2272 Returns FALSE if something goes wrong. */
2275 elf_link_read_relocs_from_section (bfd
*abfd
,
2277 Elf_Internal_Shdr
*shdr
,
2278 void *external_relocs
,
2279 Elf_Internal_Rela
*internal_relocs
)
2281 const struct elf_backend_data
*bed
;
2282 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2283 const bfd_byte
*erela
;
2284 const bfd_byte
*erelaend
;
2285 Elf_Internal_Rela
*irela
;
2286 Elf_Internal_Shdr
*symtab_hdr
;
2289 /* Position ourselves at the start of the section. */
2290 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2293 /* Read the relocations. */
2294 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2297 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2298 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2300 bed
= get_elf_backend_data (abfd
);
2302 /* Convert the external relocations to the internal format. */
2303 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2304 swap_in
= bed
->s
->swap_reloc_in
;
2305 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2306 swap_in
= bed
->s
->swap_reloca_in
;
2309 bfd_set_error (bfd_error_wrong_format
);
2313 erela
= (const bfd_byte
*) external_relocs
;
2314 erelaend
= erela
+ shdr
->sh_size
;
2315 irela
= internal_relocs
;
2316 while (erela
< erelaend
)
2320 (*swap_in
) (abfd
, erela
, irela
);
2321 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2322 if (bed
->s
->arch_size
== 64)
2326 if ((size_t) r_symndx
>= nsyms
)
2329 /* xgettext:c-format */
2330 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2331 " for offset 0x%lx in section `%A'"),
2333 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2334 bfd_set_error (bfd_error_bad_value
);
2338 else if (r_symndx
!= STN_UNDEF
)
2341 /* xgettext:c-format */
2342 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2343 " when the object file has no symbol table"),
2345 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2346 bfd_set_error (bfd_error_bad_value
);
2349 irela
+= bed
->s
->int_rels_per_ext_rel
;
2350 erela
+= shdr
->sh_entsize
;
2356 /* Read and swap the relocs for a section O. They may have been
2357 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2358 not NULL, they are used as buffers to read into. They are known to
2359 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2360 the return value is allocated using either malloc or bfd_alloc,
2361 according to the KEEP_MEMORY argument. If O has two relocation
2362 sections (both REL and RELA relocations), then the REL_HDR
2363 relocations will appear first in INTERNAL_RELOCS, followed by the
2364 RELA_HDR relocations. */
2367 _bfd_elf_link_read_relocs (bfd
*abfd
,
2369 void *external_relocs
,
2370 Elf_Internal_Rela
*internal_relocs
,
2371 bfd_boolean keep_memory
)
2373 void *alloc1
= NULL
;
2374 Elf_Internal_Rela
*alloc2
= NULL
;
2375 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2376 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2377 Elf_Internal_Rela
*internal_rela_relocs
;
2379 if (esdo
->relocs
!= NULL
)
2380 return esdo
->relocs
;
2382 if (o
->reloc_count
== 0)
2385 if (internal_relocs
== NULL
)
2389 size
= o
->reloc_count
;
2390 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2392 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2394 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2395 if (internal_relocs
== NULL
)
2399 if (external_relocs
== NULL
)
2401 bfd_size_type size
= 0;
2404 size
+= esdo
->rel
.hdr
->sh_size
;
2406 size
+= esdo
->rela
.hdr
->sh_size
;
2408 alloc1
= bfd_malloc (size
);
2411 external_relocs
= alloc1
;
2414 internal_rela_relocs
= internal_relocs
;
2417 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2421 external_relocs
= (((bfd_byte
*) external_relocs
)
2422 + esdo
->rel
.hdr
->sh_size
);
2423 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2424 * bed
->s
->int_rels_per_ext_rel
);
2428 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2430 internal_rela_relocs
)))
2433 /* Cache the results for next time, if we can. */
2435 esdo
->relocs
= internal_relocs
;
2440 /* Don't free alloc2, since if it was allocated we are passing it
2441 back (under the name of internal_relocs). */
2443 return internal_relocs
;
2451 bfd_release (abfd
, alloc2
);
2458 /* Compute the size of, and allocate space for, REL_HDR which is the
2459 section header for a section containing relocations for O. */
2462 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2463 struct bfd_elf_section_reloc_data
*reldata
)
2465 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2467 /* That allows us to calculate the size of the section. */
2468 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2470 /* The contents field must last into write_object_contents, so we
2471 allocate it with bfd_alloc rather than malloc. Also since we
2472 cannot be sure that the contents will actually be filled in,
2473 we zero the allocated space. */
2474 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2475 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2478 if (reldata
->hashes
== NULL
&& reldata
->count
)
2480 struct elf_link_hash_entry
**p
;
2482 p
= ((struct elf_link_hash_entry
**)
2483 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2487 reldata
->hashes
= p
;
2493 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2494 originated from the section given by INPUT_REL_HDR) to the
2498 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2499 asection
*input_section
,
2500 Elf_Internal_Shdr
*input_rel_hdr
,
2501 Elf_Internal_Rela
*internal_relocs
,
2502 struct elf_link_hash_entry
**rel_hash
2505 Elf_Internal_Rela
*irela
;
2506 Elf_Internal_Rela
*irelaend
;
2508 struct bfd_elf_section_reloc_data
*output_reldata
;
2509 asection
*output_section
;
2510 const struct elf_backend_data
*bed
;
2511 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2512 struct bfd_elf_section_data
*esdo
;
2514 output_section
= input_section
->output_section
;
2516 bed
= get_elf_backend_data (output_bfd
);
2517 esdo
= elf_section_data (output_section
);
2518 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2520 output_reldata
= &esdo
->rel
;
2521 swap_out
= bed
->s
->swap_reloc_out
;
2523 else if (esdo
->rela
.hdr
2524 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2526 output_reldata
= &esdo
->rela
;
2527 swap_out
= bed
->s
->swap_reloca_out
;
2532 /* xgettext:c-format */
2533 (_("%B: relocation size mismatch in %B section %A"),
2534 output_bfd
, input_section
->owner
, input_section
);
2535 bfd_set_error (bfd_error_wrong_format
);
2539 erel
= output_reldata
->hdr
->contents
;
2540 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2541 irela
= internal_relocs
;
2542 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2543 * bed
->s
->int_rels_per_ext_rel
);
2544 while (irela
< irelaend
)
2546 (*swap_out
) (output_bfd
, irela
, erel
);
2547 irela
+= bed
->s
->int_rels_per_ext_rel
;
2548 erel
+= input_rel_hdr
->sh_entsize
;
2551 /* Bump the counter, so that we know where to add the next set of
2553 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2558 /* Make weak undefined symbols in PIE dynamic. */
2561 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2562 struct elf_link_hash_entry
*h
)
2564 if (bfd_link_pie (info
)
2566 && h
->root
.type
== bfd_link_hash_undefweak
)
2567 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2572 /* Fix up the flags for a symbol. This handles various cases which
2573 can only be fixed after all the input files are seen. This is
2574 currently called by both adjust_dynamic_symbol and
2575 assign_sym_version, which is unnecessary but perhaps more robust in
2576 the face of future changes. */
2579 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2580 struct elf_info_failed
*eif
)
2582 const struct elf_backend_data
*bed
;
2584 /* If this symbol was mentioned in a non-ELF file, try to set
2585 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2586 permit a non-ELF file to correctly refer to a symbol defined in
2587 an ELF dynamic object. */
2590 while (h
->root
.type
== bfd_link_hash_indirect
)
2591 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2593 if (h
->root
.type
!= bfd_link_hash_defined
2594 && h
->root
.type
!= bfd_link_hash_defweak
)
2597 h
->ref_regular_nonweak
= 1;
2601 if (h
->root
.u
.def
.section
->owner
!= NULL
2602 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2603 == bfd_target_elf_flavour
))
2606 h
->ref_regular_nonweak
= 1;
2612 if (h
->dynindx
== -1
2616 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2625 /* Unfortunately, NON_ELF is only correct if the symbol
2626 was first seen in a non-ELF file. Fortunately, if the symbol
2627 was first seen in an ELF file, we're probably OK unless the
2628 symbol was defined in a non-ELF file. Catch that case here.
2629 FIXME: We're still in trouble if the symbol was first seen in
2630 a dynamic object, and then later in a non-ELF regular object. */
2631 if ((h
->root
.type
== bfd_link_hash_defined
2632 || h
->root
.type
== bfd_link_hash_defweak
)
2634 && (h
->root
.u
.def
.section
->owner
!= NULL
2635 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2636 != bfd_target_elf_flavour
)
2637 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2638 && !h
->def_dynamic
)))
2642 /* Backend specific symbol fixup. */
2643 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2644 if (bed
->elf_backend_fixup_symbol
2645 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2648 /* If this is a final link, and the symbol was defined as a common
2649 symbol in a regular object file, and there was no definition in
2650 any dynamic object, then the linker will have allocated space for
2651 the symbol in a common section but the DEF_REGULAR
2652 flag will not have been set. */
2653 if (h
->root
.type
== bfd_link_hash_defined
2657 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
2660 /* If a weak undefined symbol has non-default visibility, we also
2661 hide it from the dynamic linker. */
2662 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2663 && h
->root
.type
== bfd_link_hash_undefweak
)
2664 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2666 /* A hidden versioned symbol in executable should be forced local if
2667 it is is locally defined, not referenced by shared library and not
2669 else if (bfd_link_executable (eif
->info
)
2670 && h
->versioned
== versioned_hidden
2671 && !eif
->info
->export_dynamic
2675 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2677 /* If -Bsymbolic was used (which means to bind references to global
2678 symbols to the definition within the shared object), and this
2679 symbol was defined in a regular object, then it actually doesn't
2680 need a PLT entry. Likewise, if the symbol has non-default
2681 visibility. If the symbol has hidden or internal visibility, we
2682 will force it local. */
2683 else if (h
->needs_plt
2684 && bfd_link_pic (eif
->info
)
2685 && is_elf_hash_table (eif
->info
->hash
)
2686 && (SYMBOLIC_BIND (eif
->info
, h
)
2687 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2690 bfd_boolean force_local
;
2692 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2693 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2694 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2697 /* If this is a weak defined symbol in a dynamic object, and we know
2698 the real definition in the dynamic object, copy interesting flags
2699 over to the real definition. */
2700 if (h
->u
.weakdef
!= NULL
)
2702 /* If the real definition is defined by a regular object file,
2703 don't do anything special. See the longer description in
2704 _bfd_elf_adjust_dynamic_symbol, below. */
2705 if (h
->u
.weakdef
->def_regular
)
2706 h
->u
.weakdef
= NULL
;
2709 struct elf_link_hash_entry
*weakdef
= h
->u
.weakdef
;
2711 while (h
->root
.type
== bfd_link_hash_indirect
)
2712 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2714 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2715 || h
->root
.type
== bfd_link_hash_defweak
);
2716 BFD_ASSERT (weakdef
->def_dynamic
);
2717 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2718 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2719 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
, h
);
2726 /* Make the backend pick a good value for a dynamic symbol. This is
2727 called via elf_link_hash_traverse, and also calls itself
2731 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2733 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2735 const struct elf_backend_data
*bed
;
2737 if (! is_elf_hash_table (eif
->info
->hash
))
2740 /* Ignore indirect symbols. These are added by the versioning code. */
2741 if (h
->root
.type
== bfd_link_hash_indirect
)
2744 /* Fix the symbol flags. */
2745 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2748 /* If this symbol does not require a PLT entry, and it is not
2749 defined by a dynamic object, or is not referenced by a regular
2750 object, ignore it. We do have to handle a weak defined symbol,
2751 even if no regular object refers to it, if we decided to add it
2752 to the dynamic symbol table. FIXME: Do we normally need to worry
2753 about symbols which are defined by one dynamic object and
2754 referenced by another one? */
2756 && h
->type
!= STT_GNU_IFUNC
2760 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2762 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2766 /* If we've already adjusted this symbol, don't do it again. This
2767 can happen via a recursive call. */
2768 if (h
->dynamic_adjusted
)
2771 /* Don't look at this symbol again. Note that we must set this
2772 after checking the above conditions, because we may look at a
2773 symbol once, decide not to do anything, and then get called
2774 recursively later after REF_REGULAR is set below. */
2775 h
->dynamic_adjusted
= 1;
2777 /* If this is a weak definition, and we know a real definition, and
2778 the real symbol is not itself defined by a regular object file,
2779 then get a good value for the real definition. We handle the
2780 real symbol first, for the convenience of the backend routine.
2782 Note that there is a confusing case here. If the real definition
2783 is defined by a regular object file, we don't get the real symbol
2784 from the dynamic object, but we do get the weak symbol. If the
2785 processor backend uses a COPY reloc, then if some routine in the
2786 dynamic object changes the real symbol, we will not see that
2787 change in the corresponding weak symbol. This is the way other
2788 ELF linkers work as well, and seems to be a result of the shared
2791 I will clarify this issue. Most SVR4 shared libraries define the
2792 variable _timezone and define timezone as a weak synonym. The
2793 tzset call changes _timezone. If you write
2794 extern int timezone;
2796 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2797 you might expect that, since timezone is a synonym for _timezone,
2798 the same number will print both times. However, if the processor
2799 backend uses a COPY reloc, then actually timezone will be copied
2800 into your process image, and, since you define _timezone
2801 yourself, _timezone will not. Thus timezone and _timezone will
2802 wind up at different memory locations. The tzset call will set
2803 _timezone, leaving timezone unchanged. */
2805 if (h
->u
.weakdef
!= NULL
)
2807 /* If we get to this point, there is an implicit reference to
2808 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2809 h
->u
.weakdef
->ref_regular
= 1;
2811 /* Ensure that the backend adjust_dynamic_symbol function sees
2812 H->U.WEAKDEF before H by recursively calling ourselves. */
2813 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2817 /* If a symbol has no type and no size and does not require a PLT
2818 entry, then we are probably about to do the wrong thing here: we
2819 are probably going to create a COPY reloc for an empty object.
2820 This case can arise when a shared object is built with assembly
2821 code, and the assembly code fails to set the symbol type. */
2823 && h
->type
== STT_NOTYPE
2826 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2827 h
->root
.root
.string
);
2829 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2830 bed
= get_elf_backend_data (dynobj
);
2832 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2841 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2845 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
2846 struct elf_link_hash_entry
*h
,
2849 unsigned int power_of_two
;
2851 asection
*sec
= h
->root
.u
.def
.section
;
2853 /* The section aligment of definition is the maximum alignment
2854 requirement of symbols defined in the section. Since we don't
2855 know the symbol alignment requirement, we start with the
2856 maximum alignment and check low bits of the symbol address
2857 for the minimum alignment. */
2858 power_of_two
= bfd_get_section_alignment (sec
->owner
, sec
);
2859 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
2860 while ((h
->root
.u
.def
.value
& mask
) != 0)
2866 if (power_of_two
> bfd_get_section_alignment (dynbss
->owner
,
2869 /* Adjust the section alignment if needed. */
2870 if (! bfd_set_section_alignment (dynbss
->owner
, dynbss
,
2875 /* We make sure that the symbol will be aligned properly. */
2876 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
2878 /* Define the symbol as being at this point in DYNBSS. */
2879 h
->root
.u
.def
.section
= dynbss
;
2880 h
->root
.u
.def
.value
= dynbss
->size
;
2882 /* Increment the size of DYNBSS to make room for the symbol. */
2883 dynbss
->size
+= h
->size
;
2885 /* No error if extern_protected_data is true. */
2886 if (h
->protected_def
2887 && (!info
->extern_protected_data
2888 || (info
->extern_protected_data
< 0
2889 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
2890 info
->callbacks
->einfo
2891 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2892 h
->root
.root
.string
);
2897 /* Adjust all external symbols pointing into SEC_MERGE sections
2898 to reflect the object merging within the sections. */
2901 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2905 if ((h
->root
.type
== bfd_link_hash_defined
2906 || h
->root
.type
== bfd_link_hash_defweak
)
2907 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2908 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
2910 bfd
*output_bfd
= (bfd
*) data
;
2912 h
->root
.u
.def
.value
=
2913 _bfd_merged_section_offset (output_bfd
,
2914 &h
->root
.u
.def
.section
,
2915 elf_section_data (sec
)->sec_info
,
2916 h
->root
.u
.def
.value
);
2922 /* Returns false if the symbol referred to by H should be considered
2923 to resolve local to the current module, and true if it should be
2924 considered to bind dynamically. */
2927 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2928 struct bfd_link_info
*info
,
2929 bfd_boolean not_local_protected
)
2931 bfd_boolean binding_stays_local_p
;
2932 const struct elf_backend_data
*bed
;
2933 struct elf_link_hash_table
*hash_table
;
2938 while (h
->root
.type
== bfd_link_hash_indirect
2939 || h
->root
.type
== bfd_link_hash_warning
)
2940 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2942 /* If it was forced local, then clearly it's not dynamic. */
2943 if (h
->dynindx
== -1)
2945 if (h
->forced_local
)
2948 /* Identify the cases where name binding rules say that a
2949 visible symbol resolves locally. */
2950 binding_stays_local_p
= (bfd_link_executable (info
)
2951 || SYMBOLIC_BIND (info
, h
));
2953 switch (ELF_ST_VISIBILITY (h
->other
))
2960 hash_table
= elf_hash_table (info
);
2961 if (!is_elf_hash_table (hash_table
))
2964 bed
= get_elf_backend_data (hash_table
->dynobj
);
2966 /* Proper resolution for function pointer equality may require
2967 that these symbols perhaps be resolved dynamically, even though
2968 we should be resolving them to the current module. */
2969 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
2970 binding_stays_local_p
= TRUE
;
2977 /* If it isn't defined locally, then clearly it's dynamic. */
2978 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2981 /* Otherwise, the symbol is dynamic if binding rules don't tell
2982 us that it remains local. */
2983 return !binding_stays_local_p
;
2986 /* Return true if the symbol referred to by H should be considered
2987 to resolve local to the current module, and false otherwise. Differs
2988 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2989 undefined symbols. The two functions are virtually identical except
2990 for the place where forced_local and dynindx == -1 are tested. If
2991 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2992 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2993 the symbol is local only for defined symbols.
2994 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2995 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2996 treatment of undefined weak symbols. For those that do not make
2997 undefined weak symbols dynamic, both functions may return false. */
3000 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3001 struct bfd_link_info
*info
,
3002 bfd_boolean local_protected
)
3004 const struct elf_backend_data
*bed
;
3005 struct elf_link_hash_table
*hash_table
;
3007 /* If it's a local sym, of course we resolve locally. */
3011 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3012 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3013 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3016 /* Common symbols that become definitions don't get the DEF_REGULAR
3017 flag set, so test it first, and don't bail out. */
3018 if (ELF_COMMON_DEF_P (h
))
3020 /* If we don't have a definition in a regular file, then we can't
3021 resolve locally. The sym is either undefined or dynamic. */
3022 else if (!h
->def_regular
)
3025 /* Forced local symbols resolve locally. */
3026 if (h
->forced_local
)
3029 /* As do non-dynamic symbols. */
3030 if (h
->dynindx
== -1)
3033 /* At this point, we know the symbol is defined and dynamic. In an
3034 executable it must resolve locally, likewise when building symbolic
3035 shared libraries. */
3036 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3039 /* Now deal with defined dynamic symbols in shared libraries. Ones
3040 with default visibility might not resolve locally. */
3041 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3044 hash_table
= elf_hash_table (info
);
3045 if (!is_elf_hash_table (hash_table
))
3048 bed
= get_elf_backend_data (hash_table
->dynobj
);
3050 /* If extern_protected_data is false, STV_PROTECTED non-function
3051 symbols are local. */
3052 if ((!info
->extern_protected_data
3053 || (info
->extern_protected_data
< 0
3054 && !bed
->extern_protected_data
))
3055 && !bed
->is_function_type (h
->type
))
3058 /* Function pointer equality tests may require that STV_PROTECTED
3059 symbols be treated as dynamic symbols. If the address of a
3060 function not defined in an executable is set to that function's
3061 plt entry in the executable, then the address of the function in
3062 a shared library must also be the plt entry in the executable. */
3063 return local_protected
;
3066 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3067 aligned. Returns the first TLS output section. */
3069 struct bfd_section
*
3070 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3072 struct bfd_section
*sec
, *tls
;
3073 unsigned int align
= 0;
3075 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3076 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3080 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3081 if (sec
->alignment_power
> align
)
3082 align
= sec
->alignment_power
;
3084 elf_hash_table (info
)->tls_sec
= tls
;
3086 /* Ensure the alignment of the first section is the largest alignment,
3087 so that the tls segment starts aligned. */
3089 tls
->alignment_power
= align
;
3094 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3096 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3097 Elf_Internal_Sym
*sym
)
3099 const struct elf_backend_data
*bed
;
3101 /* Local symbols do not count, but target specific ones might. */
3102 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3103 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3106 bed
= get_elf_backend_data (abfd
);
3107 /* Function symbols do not count. */
3108 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3111 /* If the section is undefined, then so is the symbol. */
3112 if (sym
->st_shndx
== SHN_UNDEF
)
3115 /* If the symbol is defined in the common section, then
3116 it is a common definition and so does not count. */
3117 if (bed
->common_definition (sym
))
3120 /* If the symbol is in a target specific section then we
3121 must rely upon the backend to tell us what it is. */
3122 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3123 /* FIXME - this function is not coded yet:
3125 return _bfd_is_global_symbol_definition (abfd, sym);
3127 Instead for now assume that the definition is not global,
3128 Even if this is wrong, at least the linker will behave
3129 in the same way that it used to do. */
3135 /* Search the symbol table of the archive element of the archive ABFD
3136 whose archive map contains a mention of SYMDEF, and determine if
3137 the symbol is defined in this element. */
3139 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3141 Elf_Internal_Shdr
* hdr
;
3145 Elf_Internal_Sym
*isymbuf
;
3146 Elf_Internal_Sym
*isym
;
3147 Elf_Internal_Sym
*isymend
;
3150 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
3154 if (! bfd_check_format (abfd
, bfd_object
))
3157 /* Select the appropriate symbol table. If we don't know if the
3158 object file is an IR object, give linker LTO plugin a chance to
3159 get the correct symbol table. */
3160 if (abfd
->plugin_format
== bfd_plugin_yes
3161 #if BFD_SUPPORTS_PLUGINS
3162 || (abfd
->plugin_format
== bfd_plugin_unknown
3163 && bfd_link_plugin_object_p (abfd
))
3167 /* Use the IR symbol table if the object has been claimed by
3169 abfd
= abfd
->plugin_dummy_bfd
;
3170 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3172 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3173 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3175 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3177 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3179 /* The sh_info field of the symtab header tells us where the
3180 external symbols start. We don't care about the local symbols. */
3181 if (elf_bad_symtab (abfd
))
3183 extsymcount
= symcount
;
3188 extsymcount
= symcount
- hdr
->sh_info
;
3189 extsymoff
= hdr
->sh_info
;
3192 if (extsymcount
== 0)
3195 /* Read in the symbol table. */
3196 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3198 if (isymbuf
== NULL
)
3201 /* Scan the symbol table looking for SYMDEF. */
3203 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3207 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3212 if (strcmp (name
, symdef
->name
) == 0)
3214 result
= is_global_data_symbol_definition (abfd
, isym
);
3224 /* Add an entry to the .dynamic table. */
3227 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3231 struct elf_link_hash_table
*hash_table
;
3232 const struct elf_backend_data
*bed
;
3234 bfd_size_type newsize
;
3235 bfd_byte
*newcontents
;
3236 Elf_Internal_Dyn dyn
;
3238 hash_table
= elf_hash_table (info
);
3239 if (! is_elf_hash_table (hash_table
))
3242 bed
= get_elf_backend_data (hash_table
->dynobj
);
3243 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3244 BFD_ASSERT (s
!= NULL
);
3246 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3247 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3248 if (newcontents
== NULL
)
3252 dyn
.d_un
.d_val
= val
;
3253 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3256 s
->contents
= newcontents
;
3261 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3262 otherwise just check whether one already exists. Returns -1 on error,
3263 1 if a DT_NEEDED tag already exists, and 0 on success. */
3266 elf_add_dt_needed_tag (bfd
*abfd
,
3267 struct bfd_link_info
*info
,
3271 struct elf_link_hash_table
*hash_table
;
3274 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3277 hash_table
= elf_hash_table (info
);
3278 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3279 if (strindex
== (size_t) -1)
3282 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3285 const struct elf_backend_data
*bed
;
3288 bed
= get_elf_backend_data (hash_table
->dynobj
);
3289 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3291 for (extdyn
= sdyn
->contents
;
3292 extdyn
< sdyn
->contents
+ sdyn
->size
;
3293 extdyn
+= bed
->s
->sizeof_dyn
)
3295 Elf_Internal_Dyn dyn
;
3297 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3298 if (dyn
.d_tag
== DT_NEEDED
3299 && dyn
.d_un
.d_val
== strindex
)
3301 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3309 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3312 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3316 /* We were just checking for existence of the tag. */
3317 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3322 /* Return true if SONAME is on the needed list between NEEDED and STOP
3323 (or the end of list if STOP is NULL), and needed by a library that
3327 on_needed_list (const char *soname
,
3328 struct bfd_link_needed_list
*needed
,
3329 struct bfd_link_needed_list
*stop
)
3331 struct bfd_link_needed_list
*look
;
3332 for (look
= needed
; look
!= stop
; look
= look
->next
)
3333 if (strcmp (soname
, look
->name
) == 0
3334 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3335 /* If needed by a library that itself is not directly
3336 needed, recursively check whether that library is
3337 indirectly needed. Since we add DT_NEEDED entries to
3338 the end of the list, library dependencies appear after
3339 the library. Therefore search prior to the current
3340 LOOK, preventing possible infinite recursion. */
3341 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3347 /* Sort symbol by value, section, and size. */
3349 elf_sort_symbol (const void *arg1
, const void *arg2
)
3351 const struct elf_link_hash_entry
*h1
;
3352 const struct elf_link_hash_entry
*h2
;
3353 bfd_signed_vma vdiff
;
3355 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3356 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3357 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3359 return vdiff
> 0 ? 1 : -1;
3362 int sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3364 return sdiff
> 0 ? 1 : -1;
3366 vdiff
= h1
->size
- h2
->size
;
3367 return vdiff
== 0 ? 0 : vdiff
> 0 ? 1 : -1;
3370 /* This function is used to adjust offsets into .dynstr for
3371 dynamic symbols. This is called via elf_link_hash_traverse. */
3374 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3376 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3378 if (h
->dynindx
!= -1)
3379 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3383 /* Assign string offsets in .dynstr, update all structures referencing
3387 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3389 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3390 struct elf_link_local_dynamic_entry
*entry
;
3391 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3392 bfd
*dynobj
= hash_table
->dynobj
;
3395 const struct elf_backend_data
*bed
;
3398 _bfd_elf_strtab_finalize (dynstr
);
3399 size
= _bfd_elf_strtab_size (dynstr
);
3401 bed
= get_elf_backend_data (dynobj
);
3402 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3403 BFD_ASSERT (sdyn
!= NULL
);
3405 /* Update all .dynamic entries referencing .dynstr strings. */
3406 for (extdyn
= sdyn
->contents
;
3407 extdyn
< sdyn
->contents
+ sdyn
->size
;
3408 extdyn
+= bed
->s
->sizeof_dyn
)
3410 Elf_Internal_Dyn dyn
;
3412 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3416 dyn
.d_un
.d_val
= size
;
3426 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3431 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3434 /* Now update local dynamic symbols. */
3435 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3436 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3437 entry
->isym
.st_name
);
3439 /* And the rest of dynamic symbols. */
3440 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3442 /* Adjust version definitions. */
3443 if (elf_tdata (output_bfd
)->cverdefs
)
3448 Elf_Internal_Verdef def
;
3449 Elf_Internal_Verdaux defaux
;
3451 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3455 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3457 p
+= sizeof (Elf_External_Verdef
);
3458 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3460 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3462 _bfd_elf_swap_verdaux_in (output_bfd
,
3463 (Elf_External_Verdaux
*) p
, &defaux
);
3464 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3466 _bfd_elf_swap_verdaux_out (output_bfd
,
3467 &defaux
, (Elf_External_Verdaux
*) p
);
3468 p
+= sizeof (Elf_External_Verdaux
);
3471 while (def
.vd_next
);
3474 /* Adjust version references. */
3475 if (elf_tdata (output_bfd
)->verref
)
3480 Elf_Internal_Verneed need
;
3481 Elf_Internal_Vernaux needaux
;
3483 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
3487 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3489 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3490 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3491 (Elf_External_Verneed
*) p
);
3492 p
+= sizeof (Elf_External_Verneed
);
3493 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3495 _bfd_elf_swap_vernaux_in (output_bfd
,
3496 (Elf_External_Vernaux
*) p
, &needaux
);
3497 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3499 _bfd_elf_swap_vernaux_out (output_bfd
,
3501 (Elf_External_Vernaux
*) p
);
3502 p
+= sizeof (Elf_External_Vernaux
);
3505 while (need
.vn_next
);
3511 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3512 The default is to only match when the INPUT and OUTPUT are exactly
3516 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3517 const bfd_target
*output
)
3519 return input
== output
;
3522 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3523 This version is used when different targets for the same architecture
3524 are virtually identical. */
3527 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3528 const bfd_target
*output
)
3530 const struct elf_backend_data
*obed
, *ibed
;
3532 if (input
== output
)
3535 ibed
= xvec_get_elf_backend_data (input
);
3536 obed
= xvec_get_elf_backend_data (output
);
3538 if (ibed
->arch
!= obed
->arch
)
3541 /* If both backends are using this function, deem them compatible. */
3542 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3545 /* Make a special call to the linker "notice" function to tell it that
3546 we are about to handle an as-needed lib, or have finished
3547 processing the lib. */
3550 _bfd_elf_notice_as_needed (bfd
*ibfd
,
3551 struct bfd_link_info
*info
,
3552 enum notice_asneeded_action act
)
3554 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
3557 /* Check relocations an ELF object file. */
3560 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
3562 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3563 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
3565 /* If this object is the same format as the output object, and it is
3566 not a shared library, then let the backend look through the
3569 This is required to build global offset table entries and to
3570 arrange for dynamic relocs. It is not required for the
3571 particular common case of linking non PIC code, even when linking
3572 against shared libraries, but unfortunately there is no way of
3573 knowing whether an object file has been compiled PIC or not.
3574 Looking through the relocs is not particularly time consuming.
3575 The problem is that we must either (1) keep the relocs in memory,
3576 which causes the linker to require additional runtime memory or
3577 (2) read the relocs twice from the input file, which wastes time.
3578 This would be a good case for using mmap.
3580 I have no idea how to handle linking PIC code into a file of a
3581 different format. It probably can't be done. */
3582 if ((abfd
->flags
& DYNAMIC
) == 0
3583 && is_elf_hash_table (htab
)
3584 && bed
->check_relocs
!= NULL
3585 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
3586 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
3590 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3592 Elf_Internal_Rela
*internal_relocs
;
3595 /* Don't check relocations in excluded sections. */
3596 if ((o
->flags
& SEC_RELOC
) == 0
3597 || (o
->flags
& SEC_EXCLUDE
) != 0
3598 || o
->reloc_count
== 0
3599 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
3600 && (o
->flags
& SEC_DEBUGGING
) != 0)
3601 || bfd_is_abs_section (o
->output_section
))
3604 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
3606 if (internal_relocs
== NULL
)
3609 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
3611 if (elf_section_data (o
)->relocs
!= internal_relocs
)
3612 free (internal_relocs
);
3622 /* Add symbols from an ELF object file to the linker hash table. */
3625 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3627 Elf_Internal_Ehdr
*ehdr
;
3628 Elf_Internal_Shdr
*hdr
;
3632 struct elf_link_hash_entry
**sym_hash
;
3633 bfd_boolean dynamic
;
3634 Elf_External_Versym
*extversym
= NULL
;
3635 Elf_External_Versym
*ever
;
3636 struct elf_link_hash_entry
*weaks
;
3637 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3638 size_t nondeflt_vers_cnt
= 0;
3639 Elf_Internal_Sym
*isymbuf
= NULL
;
3640 Elf_Internal_Sym
*isym
;
3641 Elf_Internal_Sym
*isymend
;
3642 const struct elf_backend_data
*bed
;
3643 bfd_boolean add_needed
;
3644 struct elf_link_hash_table
*htab
;
3646 void *alloc_mark
= NULL
;
3647 struct bfd_hash_entry
**old_table
= NULL
;
3648 unsigned int old_size
= 0;
3649 unsigned int old_count
= 0;
3650 void *old_tab
= NULL
;
3652 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3653 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3654 void *old_strtab
= NULL
;
3657 bfd_boolean just_syms
;
3659 htab
= elf_hash_table (info
);
3660 bed
= get_elf_backend_data (abfd
);
3662 if ((abfd
->flags
& DYNAMIC
) == 0)
3668 /* You can't use -r against a dynamic object. Also, there's no
3669 hope of using a dynamic object which does not exactly match
3670 the format of the output file. */
3671 if (bfd_link_relocatable (info
)
3672 || !is_elf_hash_table (htab
)
3673 || info
->output_bfd
->xvec
!= abfd
->xvec
)
3675 if (bfd_link_relocatable (info
))
3676 bfd_set_error (bfd_error_invalid_operation
);
3678 bfd_set_error (bfd_error_wrong_format
);
3683 ehdr
= elf_elfheader (abfd
);
3684 if (info
->warn_alternate_em
3685 && bed
->elf_machine_code
!= ehdr
->e_machine
3686 && ((bed
->elf_machine_alt1
!= 0
3687 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
3688 || (bed
->elf_machine_alt2
!= 0
3689 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
3690 info
->callbacks
->einfo
3691 /* xgettext:c-format */
3692 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3693 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
3695 /* As a GNU extension, any input sections which are named
3696 .gnu.warning.SYMBOL are treated as warning symbols for the given
3697 symbol. This differs from .gnu.warning sections, which generate
3698 warnings when they are included in an output file. */
3699 /* PR 12761: Also generate this warning when building shared libraries. */
3700 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3704 name
= bfd_get_section_name (abfd
, s
);
3705 if (CONST_STRNEQ (name
, ".gnu.warning."))
3710 name
+= sizeof ".gnu.warning." - 1;
3712 /* If this is a shared object, then look up the symbol
3713 in the hash table. If it is there, and it is already
3714 been defined, then we will not be using the entry
3715 from this shared object, so we don't need to warn.
3716 FIXME: If we see the definition in a regular object
3717 later on, we will warn, but we shouldn't. The only
3718 fix is to keep track of what warnings we are supposed
3719 to emit, and then handle them all at the end of the
3723 struct elf_link_hash_entry
*h
;
3725 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3727 /* FIXME: What about bfd_link_hash_common? */
3729 && (h
->root
.type
== bfd_link_hash_defined
3730 || h
->root
.type
== bfd_link_hash_defweak
))
3735 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
3739 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3744 if (! (_bfd_generic_link_add_one_symbol
3745 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3746 FALSE
, bed
->collect
, NULL
)))
3749 if (bfd_link_executable (info
))
3751 /* Clobber the section size so that the warning does
3752 not get copied into the output file. */
3755 /* Also set SEC_EXCLUDE, so that symbols defined in
3756 the warning section don't get copied to the output. */
3757 s
->flags
|= SEC_EXCLUDE
;
3762 just_syms
= ((s
= abfd
->sections
) != NULL
3763 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
3768 /* If we are creating a shared library, create all the dynamic
3769 sections immediately. We need to attach them to something,
3770 so we attach them to this BFD, provided it is the right
3771 format and is not from ld --just-symbols. Always create the
3772 dynamic sections for -E/--dynamic-list. FIXME: If there
3773 are no input BFD's of the same format as the output, we can't
3774 make a shared library. */
3776 && (bfd_link_pic (info
)
3777 || (!bfd_link_relocatable (info
)
3778 && (info
->export_dynamic
|| info
->dynamic
)))
3779 && is_elf_hash_table (htab
)
3780 && info
->output_bfd
->xvec
== abfd
->xvec
3781 && !htab
->dynamic_sections_created
)
3783 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3787 else if (!is_elf_hash_table (htab
))
3791 const char *soname
= NULL
;
3793 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3796 /* ld --just-symbols and dynamic objects don't mix very well.
3797 ld shouldn't allow it. */
3801 /* If this dynamic lib was specified on the command line with
3802 --as-needed in effect, then we don't want to add a DT_NEEDED
3803 tag unless the lib is actually used. Similary for libs brought
3804 in by another lib's DT_NEEDED. When --no-add-needed is used
3805 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3806 any dynamic library in DT_NEEDED tags in the dynamic lib at
3808 add_needed
= (elf_dyn_lib_class (abfd
)
3809 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3810 | DYN_NO_NEEDED
)) == 0;
3812 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3817 unsigned int elfsec
;
3818 unsigned long shlink
;
3820 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3827 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3828 if (elfsec
== SHN_BAD
)
3829 goto error_free_dyn
;
3830 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3832 for (extdyn
= dynbuf
;
3833 extdyn
< dynbuf
+ s
->size
;
3834 extdyn
+= bed
->s
->sizeof_dyn
)
3836 Elf_Internal_Dyn dyn
;
3838 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3839 if (dyn
.d_tag
== DT_SONAME
)
3841 unsigned int tagv
= dyn
.d_un
.d_val
;
3842 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3844 goto error_free_dyn
;
3846 if (dyn
.d_tag
== DT_NEEDED
)
3848 struct bfd_link_needed_list
*n
, **pn
;
3850 unsigned int tagv
= dyn
.d_un
.d_val
;
3852 amt
= sizeof (struct bfd_link_needed_list
);
3853 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3854 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3855 if (n
== NULL
|| fnm
== NULL
)
3856 goto error_free_dyn
;
3857 amt
= strlen (fnm
) + 1;
3858 anm
= (char *) bfd_alloc (abfd
, amt
);
3860 goto error_free_dyn
;
3861 memcpy (anm
, fnm
, amt
);
3865 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3869 if (dyn
.d_tag
== DT_RUNPATH
)
3871 struct bfd_link_needed_list
*n
, **pn
;
3873 unsigned int tagv
= dyn
.d_un
.d_val
;
3875 amt
= sizeof (struct bfd_link_needed_list
);
3876 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3877 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3878 if (n
== NULL
|| fnm
== NULL
)
3879 goto error_free_dyn
;
3880 amt
= strlen (fnm
) + 1;
3881 anm
= (char *) bfd_alloc (abfd
, amt
);
3883 goto error_free_dyn
;
3884 memcpy (anm
, fnm
, amt
);
3888 for (pn
= & runpath
;
3894 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3895 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3897 struct bfd_link_needed_list
*n
, **pn
;
3899 unsigned int tagv
= dyn
.d_un
.d_val
;
3901 amt
= sizeof (struct bfd_link_needed_list
);
3902 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3903 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3904 if (n
== NULL
|| fnm
== NULL
)
3905 goto error_free_dyn
;
3906 amt
= strlen (fnm
) + 1;
3907 anm
= (char *) bfd_alloc (abfd
, amt
);
3909 goto error_free_dyn
;
3910 memcpy (anm
, fnm
, amt
);
3920 if (dyn
.d_tag
== DT_AUDIT
)
3922 unsigned int tagv
= dyn
.d_un
.d_val
;
3923 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3930 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3931 frees all more recently bfd_alloc'd blocks as well. */
3937 struct bfd_link_needed_list
**pn
;
3938 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3943 /* We do not want to include any of the sections in a dynamic
3944 object in the output file. We hack by simply clobbering the
3945 list of sections in the BFD. This could be handled more
3946 cleanly by, say, a new section flag; the existing
3947 SEC_NEVER_LOAD flag is not the one we want, because that one
3948 still implies that the section takes up space in the output
3950 bfd_section_list_clear (abfd
);
3952 /* Find the name to use in a DT_NEEDED entry that refers to this
3953 object. If the object has a DT_SONAME entry, we use it.
3954 Otherwise, if the generic linker stuck something in
3955 elf_dt_name, we use that. Otherwise, we just use the file
3957 if (soname
== NULL
|| *soname
== '\0')
3959 soname
= elf_dt_name (abfd
);
3960 if (soname
== NULL
|| *soname
== '\0')
3961 soname
= bfd_get_filename (abfd
);
3964 /* Save the SONAME because sometimes the linker emulation code
3965 will need to know it. */
3966 elf_dt_name (abfd
) = soname
;
3968 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3972 /* If we have already included this dynamic object in the
3973 link, just ignore it. There is no reason to include a
3974 particular dynamic object more than once. */
3978 /* Save the DT_AUDIT entry for the linker emulation code. */
3979 elf_dt_audit (abfd
) = audit
;
3982 /* If this is a dynamic object, we always link against the .dynsym
3983 symbol table, not the .symtab symbol table. The dynamic linker
3984 will only see the .dynsym symbol table, so there is no reason to
3985 look at .symtab for a dynamic object. */
3987 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3988 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3990 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3992 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3994 /* The sh_info field of the symtab header tells us where the
3995 external symbols start. We don't care about the local symbols at
3997 if (elf_bad_symtab (abfd
))
3999 extsymcount
= symcount
;
4004 extsymcount
= symcount
- hdr
->sh_info
;
4005 extsymoff
= hdr
->sh_info
;
4008 sym_hash
= elf_sym_hashes (abfd
);
4009 if (extsymcount
!= 0)
4011 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4013 if (isymbuf
== NULL
)
4016 if (sym_hash
== NULL
)
4018 /* We store a pointer to the hash table entry for each
4021 amt
*= sizeof (struct elf_link_hash_entry
*);
4022 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4023 if (sym_hash
== NULL
)
4024 goto error_free_sym
;
4025 elf_sym_hashes (abfd
) = sym_hash
;
4031 /* Read in any version definitions. */
4032 if (!_bfd_elf_slurp_version_tables (abfd
,
4033 info
->default_imported_symver
))
4034 goto error_free_sym
;
4036 /* Read in the symbol versions, but don't bother to convert them
4037 to internal format. */
4038 if (elf_dynversym (abfd
) != 0)
4040 Elf_Internal_Shdr
*versymhdr
;
4042 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4043 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
4044 if (extversym
== NULL
)
4045 goto error_free_sym
;
4046 amt
= versymhdr
->sh_size
;
4047 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
4048 || bfd_bread (extversym
, amt
, abfd
) != amt
)
4049 goto error_free_vers
;
4053 /* If we are loading an as-needed shared lib, save the symbol table
4054 state before we start adding symbols. If the lib turns out
4055 to be unneeded, restore the state. */
4056 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4061 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4063 struct bfd_hash_entry
*p
;
4064 struct elf_link_hash_entry
*h
;
4066 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4068 h
= (struct elf_link_hash_entry
*) p
;
4069 entsize
+= htab
->root
.table
.entsize
;
4070 if (h
->root
.type
== bfd_link_hash_warning
)
4071 entsize
+= htab
->root
.table
.entsize
;
4075 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4076 old_tab
= bfd_malloc (tabsize
+ entsize
);
4077 if (old_tab
== NULL
)
4078 goto error_free_vers
;
4080 /* Remember the current objalloc pointer, so that all mem for
4081 symbols added can later be reclaimed. */
4082 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4083 if (alloc_mark
== NULL
)
4084 goto error_free_vers
;
4086 /* Make a special call to the linker "notice" function to
4087 tell it that we are about to handle an as-needed lib. */
4088 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4089 goto error_free_vers
;
4091 /* Clone the symbol table. Remember some pointers into the
4092 symbol table, and dynamic symbol count. */
4093 old_ent
= (char *) old_tab
+ tabsize
;
4094 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4095 old_undefs
= htab
->root
.undefs
;
4096 old_undefs_tail
= htab
->root
.undefs_tail
;
4097 old_table
= htab
->root
.table
.table
;
4098 old_size
= htab
->root
.table
.size
;
4099 old_count
= htab
->root
.table
.count
;
4100 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4101 if (old_strtab
== NULL
)
4102 goto error_free_vers
;
4104 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4106 struct bfd_hash_entry
*p
;
4107 struct elf_link_hash_entry
*h
;
4109 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4111 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
4112 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4113 h
= (struct elf_link_hash_entry
*) p
;
4114 if (h
->root
.type
== bfd_link_hash_warning
)
4116 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
4117 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4124 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
4125 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
4127 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4131 asection
*sec
, *new_sec
;
4134 struct elf_link_hash_entry
*h
;
4135 struct elf_link_hash_entry
*hi
;
4136 bfd_boolean definition
;
4137 bfd_boolean size_change_ok
;
4138 bfd_boolean type_change_ok
;
4139 bfd_boolean new_weakdef
;
4140 bfd_boolean new_weak
;
4141 bfd_boolean old_weak
;
4142 bfd_boolean override
;
4144 bfd_boolean discarded
;
4145 unsigned int old_alignment
;
4147 bfd_boolean matched
;
4151 flags
= BSF_NO_FLAGS
;
4153 value
= isym
->st_value
;
4154 common
= bed
->common_definition (isym
);
4157 bind
= ELF_ST_BIND (isym
->st_info
);
4161 /* This should be impossible, since ELF requires that all
4162 global symbols follow all local symbols, and that sh_info
4163 point to the first global symbol. Unfortunately, Irix 5
4168 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4176 case STB_GNU_UNIQUE
:
4177 flags
= BSF_GNU_UNIQUE
;
4181 /* Leave it up to the processor backend. */
4185 if (isym
->st_shndx
== SHN_UNDEF
)
4186 sec
= bfd_und_section_ptr
;
4187 else if (isym
->st_shndx
== SHN_ABS
)
4188 sec
= bfd_abs_section_ptr
;
4189 else if (isym
->st_shndx
== SHN_COMMON
)
4191 sec
= bfd_com_section_ptr
;
4192 /* What ELF calls the size we call the value. What ELF
4193 calls the value we call the alignment. */
4194 value
= isym
->st_size
;
4198 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4200 sec
= bfd_abs_section_ptr
;
4201 else if (discarded_section (sec
))
4203 /* Symbols from discarded section are undefined. We keep
4205 sec
= bfd_und_section_ptr
;
4207 isym
->st_shndx
= SHN_UNDEF
;
4209 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4213 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4216 goto error_free_vers
;
4218 if (isym
->st_shndx
== SHN_COMMON
4219 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4221 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4225 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4227 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4229 goto error_free_vers
;
4233 else if (isym
->st_shndx
== SHN_COMMON
4234 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4235 && !bfd_link_relocatable (info
))
4237 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4241 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4242 | SEC_LINKER_CREATED
);
4243 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4245 goto error_free_vers
;
4249 else if (bed
->elf_add_symbol_hook
)
4251 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4253 goto error_free_vers
;
4255 /* The hook function sets the name to NULL if this symbol
4256 should be skipped for some reason. */
4261 /* Sanity check that all possibilities were handled. */
4264 bfd_set_error (bfd_error_bad_value
);
4265 goto error_free_vers
;
4268 /* Silently discard TLS symbols from --just-syms. There's
4269 no way to combine a static TLS block with a new TLS block
4270 for this executable. */
4271 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4272 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4275 if (bfd_is_und_section (sec
)
4276 || bfd_is_com_section (sec
))
4281 size_change_ok
= FALSE
;
4282 type_change_ok
= bed
->type_change_ok
;
4289 if (is_elf_hash_table (htab
))
4291 Elf_Internal_Versym iver
;
4292 unsigned int vernum
= 0;
4297 if (info
->default_imported_symver
)
4298 /* Use the default symbol version created earlier. */
4299 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4304 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4306 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4308 /* If this is a hidden symbol, or if it is not version
4309 1, we append the version name to the symbol name.
4310 However, we do not modify a non-hidden absolute symbol
4311 if it is not a function, because it might be the version
4312 symbol itself. FIXME: What if it isn't? */
4313 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4315 && (!bfd_is_abs_section (sec
)
4316 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4319 size_t namelen
, verlen
, newlen
;
4322 if (isym
->st_shndx
!= SHN_UNDEF
)
4324 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4326 else if (vernum
> 1)
4328 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4335 /* xgettext:c-format */
4336 (_("%B: %s: invalid version %u (max %d)"),
4338 elf_tdata (abfd
)->cverdefs
);
4339 bfd_set_error (bfd_error_bad_value
);
4340 goto error_free_vers
;
4345 /* We cannot simply test for the number of
4346 entries in the VERNEED section since the
4347 numbers for the needed versions do not start
4349 Elf_Internal_Verneed
*t
;
4352 for (t
= elf_tdata (abfd
)->verref
;
4356 Elf_Internal_Vernaux
*a
;
4358 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4360 if (a
->vna_other
== vernum
)
4362 verstr
= a
->vna_nodename
;
4372 /* xgettext:c-format */
4373 (_("%B: %s: invalid needed version %d"),
4374 abfd
, name
, vernum
);
4375 bfd_set_error (bfd_error_bad_value
);
4376 goto error_free_vers
;
4380 namelen
= strlen (name
);
4381 verlen
= strlen (verstr
);
4382 newlen
= namelen
+ verlen
+ 2;
4383 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4384 && isym
->st_shndx
!= SHN_UNDEF
)
4387 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4388 if (newname
== NULL
)
4389 goto error_free_vers
;
4390 memcpy (newname
, name
, namelen
);
4391 p
= newname
+ namelen
;
4393 /* If this is a defined non-hidden version symbol,
4394 we add another @ to the name. This indicates the
4395 default version of the symbol. */
4396 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4397 && isym
->st_shndx
!= SHN_UNDEF
)
4399 memcpy (p
, verstr
, verlen
+ 1);
4404 /* If this symbol has default visibility and the user has
4405 requested we not re-export it, then mark it as hidden. */
4406 if (!bfd_is_und_section (sec
)
4409 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4410 isym
->st_other
= (STV_HIDDEN
4411 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4413 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4414 sym_hash
, &old_bfd
, &old_weak
,
4415 &old_alignment
, &skip
, &override
,
4416 &type_change_ok
, &size_change_ok
,
4418 goto error_free_vers
;
4423 /* Override a definition only if the new symbol matches the
4425 if (override
&& matched
)
4429 while (h
->root
.type
== bfd_link_hash_indirect
4430 || h
->root
.type
== bfd_link_hash_warning
)
4431 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4433 if (elf_tdata (abfd
)->verdef
!= NULL
4436 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4439 if (! (_bfd_generic_link_add_one_symbol
4440 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4441 (struct bfd_link_hash_entry
**) sym_hash
)))
4442 goto error_free_vers
;
4444 if ((flags
& BSF_GNU_UNIQUE
)
4445 && (abfd
->flags
& DYNAMIC
) == 0
4446 && bfd_get_flavour (info
->output_bfd
) == bfd_target_elf_flavour
)
4447 elf_tdata (info
->output_bfd
)->has_gnu_symbols
|= elf_gnu_symbol_unique
;
4450 /* We need to make sure that indirect symbol dynamic flags are
4453 while (h
->root
.type
== bfd_link_hash_indirect
4454 || h
->root
.type
== bfd_link_hash_warning
)
4455 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4457 /* Setting the index to -3 tells elf_link_output_extsym that
4458 this symbol is defined in a discarded section. */
4464 new_weak
= (flags
& BSF_WEAK
) != 0;
4465 new_weakdef
= FALSE
;
4469 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4470 && is_elf_hash_table (htab
)
4471 && h
->u
.weakdef
== NULL
)
4473 /* Keep a list of all weak defined non function symbols from
4474 a dynamic object, using the weakdef field. Later in this
4475 function we will set the weakdef field to the correct
4476 value. We only put non-function symbols from dynamic
4477 objects on this list, because that happens to be the only
4478 time we need to know the normal symbol corresponding to a
4479 weak symbol, and the information is time consuming to
4480 figure out. If the weakdef field is not already NULL,
4481 then this symbol was already defined by some previous
4482 dynamic object, and we will be using that previous
4483 definition anyhow. */
4485 h
->u
.weakdef
= weaks
;
4490 /* Set the alignment of a common symbol. */
4491 if ((common
|| bfd_is_com_section (sec
))
4492 && h
->root
.type
== bfd_link_hash_common
)
4497 align
= bfd_log2 (isym
->st_value
);
4500 /* The new symbol is a common symbol in a shared object.
4501 We need to get the alignment from the section. */
4502 align
= new_sec
->alignment_power
;
4504 if (align
> old_alignment
)
4505 h
->root
.u
.c
.p
->alignment_power
= align
;
4507 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4510 if (is_elf_hash_table (htab
))
4512 /* Set a flag in the hash table entry indicating the type of
4513 reference or definition we just found. A dynamic symbol
4514 is one which is referenced or defined by both a regular
4515 object and a shared object. */
4516 bfd_boolean dynsym
= FALSE
;
4518 /* Plugin symbols aren't normal. Don't set def_regular or
4519 ref_regular for them, or make them dynamic. */
4520 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4527 if (bind
!= STB_WEAK
)
4528 h
->ref_regular_nonweak
= 1;
4540 /* If the indirect symbol has been forced local, don't
4541 make the real symbol dynamic. */
4542 if ((h
== hi
|| !hi
->forced_local
)
4543 && (bfd_link_dll (info
)
4553 hi
->ref_dynamic
= 1;
4558 hi
->def_dynamic
= 1;
4561 /* If the indirect symbol has been forced local, don't
4562 make the real symbol dynamic. */
4563 if ((h
== hi
|| !hi
->forced_local
)
4566 || (h
->u
.weakdef
!= NULL
4568 && h
->u
.weakdef
->dynindx
!= -1)))
4572 /* Check to see if we need to add an indirect symbol for
4573 the default name. */
4575 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
4576 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4577 sec
, value
, &old_bfd
, &dynsym
))
4578 goto error_free_vers
;
4580 /* Check the alignment when a common symbol is involved. This
4581 can change when a common symbol is overridden by a normal
4582 definition or a common symbol is ignored due to the old
4583 normal definition. We need to make sure the maximum
4584 alignment is maintained. */
4585 if ((old_alignment
|| common
)
4586 && h
->root
.type
!= bfd_link_hash_common
)
4588 unsigned int common_align
;
4589 unsigned int normal_align
;
4590 unsigned int symbol_align
;
4594 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
4595 || h
->root
.type
== bfd_link_hash_defweak
);
4597 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4598 if (h
->root
.u
.def
.section
->owner
!= NULL
4599 && (h
->root
.u
.def
.section
->owner
->flags
4600 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
4602 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4603 if (normal_align
> symbol_align
)
4604 normal_align
= symbol_align
;
4607 normal_align
= symbol_align
;
4611 common_align
= old_alignment
;
4612 common_bfd
= old_bfd
;
4617 common_align
= bfd_log2 (isym
->st_value
);
4619 normal_bfd
= old_bfd
;
4622 if (normal_align
< common_align
)
4624 /* PR binutils/2735 */
4625 if (normal_bfd
== NULL
)
4627 /* xgettext:c-format */
4628 (_("Warning: alignment %u of common symbol `%s' in %B is"
4629 " greater than the alignment (%u) of its section %A"),
4630 common_bfd
, h
->root
.u
.def
.section
,
4631 1 << common_align
, name
, 1 << normal_align
);
4634 /* xgettext:c-format */
4635 (_("Warning: alignment %u of symbol `%s' in %B"
4636 " is smaller than %u in %B"),
4637 normal_bfd
, common_bfd
,
4638 1 << normal_align
, name
, 1 << common_align
);
4642 /* Remember the symbol size if it isn't undefined. */
4643 if (isym
->st_size
!= 0
4644 && isym
->st_shndx
!= SHN_UNDEF
4645 && (definition
|| h
->size
== 0))
4648 && h
->size
!= isym
->st_size
4649 && ! size_change_ok
)
4651 /* xgettext:c-format */
4652 (_("Warning: size of symbol `%s' changed"
4653 " from %lu in %B to %lu in %B"),
4655 name
, (unsigned long) h
->size
,
4656 (unsigned long) isym
->st_size
);
4658 h
->size
= isym
->st_size
;
4661 /* If this is a common symbol, then we always want H->SIZE
4662 to be the size of the common symbol. The code just above
4663 won't fix the size if a common symbol becomes larger. We
4664 don't warn about a size change here, because that is
4665 covered by --warn-common. Allow changes between different
4667 if (h
->root
.type
== bfd_link_hash_common
)
4668 h
->size
= h
->root
.u
.c
.size
;
4670 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4671 && ((definition
&& !new_weak
)
4672 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
4673 || h
->type
== STT_NOTYPE
))
4675 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
4677 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4679 if (type
== STT_GNU_IFUNC
4680 && (abfd
->flags
& DYNAMIC
) != 0)
4683 if (h
->type
!= type
)
4685 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
4686 /* xgettext:c-format */
4688 (_("Warning: type of symbol `%s' changed"
4689 " from %d to %d in %B"),
4690 abfd
, name
, h
->type
, type
);
4696 /* Merge st_other field. */
4697 elf_merge_st_other (abfd
, h
, isym
, sec
, definition
, dynamic
);
4699 /* We don't want to make debug symbol dynamic. */
4701 && (sec
->flags
& SEC_DEBUGGING
)
4702 && !bfd_link_relocatable (info
))
4705 /* Nor should we make plugin symbols dynamic. */
4706 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4711 h
->target_internal
= isym
->st_target_internal
;
4712 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
4715 if (definition
&& !dynamic
)
4717 char *p
= strchr (name
, ELF_VER_CHR
);
4718 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4720 /* Queue non-default versions so that .symver x, x@FOO
4721 aliases can be checked. */
4724 amt
= ((isymend
- isym
+ 1)
4725 * sizeof (struct elf_link_hash_entry
*));
4727 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
4729 goto error_free_vers
;
4731 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4735 if (dynsym
&& h
->dynindx
== -1)
4737 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4738 goto error_free_vers
;
4739 if (h
->u
.weakdef
!= NULL
4741 && h
->u
.weakdef
->dynindx
== -1)
4743 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4744 goto error_free_vers
;
4747 else if (h
->dynindx
!= -1)
4748 /* If the symbol already has a dynamic index, but
4749 visibility says it should not be visible, turn it into
4751 switch (ELF_ST_VISIBILITY (h
->other
))
4755 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4760 /* Don't add DT_NEEDED for references from the dummy bfd nor
4761 for unmatched symbol. */
4766 && h
->ref_regular_nonweak
4768 || (old_bfd
->flags
& BFD_PLUGIN
) == 0))
4769 || (h
->ref_dynamic_nonweak
4770 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
4771 && !on_needed_list (elf_dt_name (abfd
),
4772 htab
->needed
, NULL
))))
4775 const char *soname
= elf_dt_name (abfd
);
4777 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
4778 h
->root
.root
.string
);
4780 /* A symbol from a library loaded via DT_NEEDED of some
4781 other library is referenced by a regular object.
4782 Add a DT_NEEDED entry for it. Issue an error if
4783 --no-add-needed is used and the reference was not
4786 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4789 /* xgettext:c-format */
4790 (_("%B: undefined reference to symbol '%s'"),
4792 bfd_set_error (bfd_error_missing_dso
);
4793 goto error_free_vers
;
4796 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
4797 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
4800 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4802 goto error_free_vers
;
4804 BFD_ASSERT (ret
== 0);
4809 if (extversym
!= NULL
)
4815 if (isymbuf
!= NULL
)
4821 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4825 /* Restore the symbol table. */
4826 old_ent
= (char *) old_tab
+ tabsize
;
4827 memset (elf_sym_hashes (abfd
), 0,
4828 extsymcount
* sizeof (struct elf_link_hash_entry
*));
4829 htab
->root
.table
.table
= old_table
;
4830 htab
->root
.table
.size
= old_size
;
4831 htab
->root
.table
.count
= old_count
;
4832 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4833 htab
->root
.undefs
= old_undefs
;
4834 htab
->root
.undefs_tail
= old_undefs_tail
;
4835 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
4838 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4840 struct bfd_hash_entry
*p
;
4841 struct elf_link_hash_entry
*h
;
4843 unsigned int alignment_power
;
4845 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4847 h
= (struct elf_link_hash_entry
*) p
;
4848 if (h
->root
.type
== bfd_link_hash_warning
)
4849 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4851 /* Preserve the maximum alignment and size for common
4852 symbols even if this dynamic lib isn't on DT_NEEDED
4853 since it can still be loaded at run time by another
4855 if (h
->root
.type
== bfd_link_hash_common
)
4857 size
= h
->root
.u
.c
.size
;
4858 alignment_power
= h
->root
.u
.c
.p
->alignment_power
;
4863 alignment_power
= 0;
4865 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4866 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4867 h
= (struct elf_link_hash_entry
*) p
;
4868 if (h
->root
.type
== bfd_link_hash_warning
)
4870 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4871 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4872 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4874 if (h
->root
.type
== bfd_link_hash_common
)
4876 if (size
> h
->root
.u
.c
.size
)
4877 h
->root
.u
.c
.size
= size
;
4878 if (alignment_power
> h
->root
.u
.c
.p
->alignment_power
)
4879 h
->root
.u
.c
.p
->alignment_power
= alignment_power
;
4884 /* Make a special call to the linker "notice" function to
4885 tell it that symbols added for crefs may need to be removed. */
4886 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
4887 goto error_free_vers
;
4890 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4892 if (nondeflt_vers
!= NULL
)
4893 free (nondeflt_vers
);
4897 if (old_tab
!= NULL
)
4899 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
4900 goto error_free_vers
;
4905 /* Now that all the symbols from this input file are created, if
4906 not performing a relocatable link, handle .symver foo, foo@BAR
4907 such that any relocs against foo become foo@BAR. */
4908 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
4912 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4914 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4915 char *shortname
, *p
;
4917 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4919 || (h
->root
.type
!= bfd_link_hash_defined
4920 && h
->root
.type
!= bfd_link_hash_defweak
))
4923 amt
= p
- h
->root
.root
.string
;
4924 shortname
= (char *) bfd_malloc (amt
+ 1);
4926 goto error_free_vers
;
4927 memcpy (shortname
, h
->root
.root
.string
, amt
);
4928 shortname
[amt
] = '\0';
4930 hi
= (struct elf_link_hash_entry
*)
4931 bfd_link_hash_lookup (&htab
->root
, shortname
,
4932 FALSE
, FALSE
, FALSE
);
4934 && hi
->root
.type
== h
->root
.type
4935 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4936 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4938 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4939 hi
->root
.type
= bfd_link_hash_indirect
;
4940 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4941 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4942 sym_hash
= elf_sym_hashes (abfd
);
4944 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4945 if (sym_hash
[symidx
] == hi
)
4947 sym_hash
[symidx
] = h
;
4953 free (nondeflt_vers
);
4954 nondeflt_vers
= NULL
;
4957 /* Now set the weakdefs field correctly for all the weak defined
4958 symbols we found. The only way to do this is to search all the
4959 symbols. Since we only need the information for non functions in
4960 dynamic objects, that's the only time we actually put anything on
4961 the list WEAKS. We need this information so that if a regular
4962 object refers to a symbol defined weakly in a dynamic object, the
4963 real symbol in the dynamic object is also put in the dynamic
4964 symbols; we also must arrange for both symbols to point to the
4965 same memory location. We could handle the general case of symbol
4966 aliasing, but a general symbol alias can only be generated in
4967 assembler code, handling it correctly would be very time
4968 consuming, and other ELF linkers don't handle general aliasing
4972 struct elf_link_hash_entry
**hpp
;
4973 struct elf_link_hash_entry
**hppend
;
4974 struct elf_link_hash_entry
**sorted_sym_hash
;
4975 struct elf_link_hash_entry
*h
;
4978 /* Since we have to search the whole symbol list for each weak
4979 defined symbol, search time for N weak defined symbols will be
4980 O(N^2). Binary search will cut it down to O(NlogN). */
4982 amt
*= sizeof (struct elf_link_hash_entry
*);
4983 sorted_sym_hash
= (struct elf_link_hash_entry
**) bfd_malloc (amt
);
4984 if (sorted_sym_hash
== NULL
)
4986 sym_hash
= sorted_sym_hash
;
4987 hpp
= elf_sym_hashes (abfd
);
4988 hppend
= hpp
+ extsymcount
;
4990 for (; hpp
< hppend
; hpp
++)
4994 && h
->root
.type
== bfd_link_hash_defined
4995 && !bed
->is_function_type (h
->type
))
5003 qsort (sorted_sym_hash
, sym_count
,
5004 sizeof (struct elf_link_hash_entry
*),
5007 while (weaks
!= NULL
)
5009 struct elf_link_hash_entry
*hlook
;
5012 size_t i
, j
, idx
= 0;
5015 weaks
= hlook
->u
.weakdef
;
5016 hlook
->u
.weakdef
= NULL
;
5018 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
5019 || hlook
->root
.type
== bfd_link_hash_defweak
5020 || hlook
->root
.type
== bfd_link_hash_common
5021 || hlook
->root
.type
== bfd_link_hash_indirect
);
5022 slook
= hlook
->root
.u
.def
.section
;
5023 vlook
= hlook
->root
.u
.def
.value
;
5029 bfd_signed_vma vdiff
;
5031 h
= sorted_sym_hash
[idx
];
5032 vdiff
= vlook
- h
->root
.u
.def
.value
;
5039 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5049 /* We didn't find a value/section match. */
5053 /* With multiple aliases, or when the weak symbol is already
5054 strongly defined, we have multiple matching symbols and
5055 the binary search above may land on any of them. Step
5056 one past the matching symbol(s). */
5059 h
= sorted_sym_hash
[idx
];
5060 if (h
->root
.u
.def
.section
!= slook
5061 || h
->root
.u
.def
.value
!= vlook
)
5065 /* Now look back over the aliases. Since we sorted by size
5066 as well as value and section, we'll choose the one with
5067 the largest size. */
5070 h
= sorted_sym_hash
[idx
];
5072 /* Stop if value or section doesn't match. */
5073 if (h
->root
.u
.def
.section
!= slook
5074 || h
->root
.u
.def
.value
!= vlook
)
5076 else if (h
!= hlook
)
5078 hlook
->u
.weakdef
= h
;
5080 /* If the weak definition is in the list of dynamic
5081 symbols, make sure the real definition is put
5083 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5085 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5088 free (sorted_sym_hash
);
5093 /* If the real definition is in the list of dynamic
5094 symbols, make sure the weak definition is put
5095 there as well. If we don't do this, then the
5096 dynamic loader might not merge the entries for the
5097 real definition and the weak definition. */
5098 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5100 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5101 goto err_free_sym_hash
;
5108 free (sorted_sym_hash
);
5111 if (bed
->check_directives
5112 && !(*bed
->check_directives
) (abfd
, info
))
5115 if (!info
->check_relocs_after_open_input
5116 && !_bfd_elf_link_check_relocs (abfd
, info
))
5119 /* If this is a non-traditional link, try to optimize the handling
5120 of the .stab/.stabstr sections. */
5122 && ! info
->traditional_format
5123 && is_elf_hash_table (htab
)
5124 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5128 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5129 if (stabstr
!= NULL
)
5131 bfd_size_type string_offset
= 0;
5134 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5135 if (CONST_STRNEQ (stab
->name
, ".stab")
5136 && (!stab
->name
[5] ||
5137 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5138 && (stab
->flags
& SEC_MERGE
) == 0
5139 && !bfd_is_abs_section (stab
->output_section
))
5141 struct bfd_elf_section_data
*secdata
;
5143 secdata
= elf_section_data (stab
);
5144 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5145 stabstr
, &secdata
->sec_info
,
5148 if (secdata
->sec_info
)
5149 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5154 if (is_elf_hash_table (htab
) && add_needed
)
5156 /* Add this bfd to the loaded list. */
5157 struct elf_link_loaded_list
*n
;
5159 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5163 n
->next
= htab
->loaded
;
5170 if (old_tab
!= NULL
)
5172 if (old_strtab
!= NULL
)
5174 if (nondeflt_vers
!= NULL
)
5175 free (nondeflt_vers
);
5176 if (extversym
!= NULL
)
5179 if (isymbuf
!= NULL
)
5185 /* Return the linker hash table entry of a symbol that might be
5186 satisfied by an archive symbol. Return -1 on error. */
5188 struct elf_link_hash_entry
*
5189 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5190 struct bfd_link_info
*info
,
5193 struct elf_link_hash_entry
*h
;
5197 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, TRUE
);
5201 /* If this is a default version (the name contains @@), look up the
5202 symbol again with only one `@' as well as without the version.
5203 The effect is that references to the symbol with and without the
5204 version will be matched by the default symbol in the archive. */
5206 p
= strchr (name
, ELF_VER_CHR
);
5207 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5210 /* First check with only one `@'. */
5211 len
= strlen (name
);
5212 copy
= (char *) bfd_alloc (abfd
, len
);
5214 return (struct elf_link_hash_entry
*) 0 - 1;
5216 first
= p
- name
+ 1;
5217 memcpy (copy
, name
, first
);
5218 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5220 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, TRUE
);
5223 /* We also need to check references to the symbol without the
5225 copy
[first
- 1] = '\0';
5226 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
5227 FALSE
, FALSE
, TRUE
);
5230 bfd_release (abfd
, copy
);
5234 /* Add symbols from an ELF archive file to the linker hash table. We
5235 don't use _bfd_generic_link_add_archive_symbols because we need to
5236 handle versioned symbols.
5238 Fortunately, ELF archive handling is simpler than that done by
5239 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5240 oddities. In ELF, if we find a symbol in the archive map, and the
5241 symbol is currently undefined, we know that we must pull in that
5244 Unfortunately, we do have to make multiple passes over the symbol
5245 table until nothing further is resolved. */
5248 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5251 unsigned char *included
= NULL
;
5255 const struct elf_backend_data
*bed
;
5256 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
5257 (bfd
*, struct bfd_link_info
*, const char *);
5259 if (! bfd_has_map (abfd
))
5261 /* An empty archive is a special case. */
5262 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5264 bfd_set_error (bfd_error_no_armap
);
5268 /* Keep track of all symbols we know to be already defined, and all
5269 files we know to be already included. This is to speed up the
5270 second and subsequent passes. */
5271 c
= bfd_ardata (abfd
)->symdef_count
;
5275 amt
*= sizeof (*included
);
5276 included
= (unsigned char *) bfd_zmalloc (amt
);
5277 if (included
== NULL
)
5280 symdefs
= bfd_ardata (abfd
)->symdefs
;
5281 bed
= get_elf_backend_data (abfd
);
5282 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5295 symdefend
= symdef
+ c
;
5296 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5298 struct elf_link_hash_entry
*h
;
5300 struct bfd_link_hash_entry
*undefs_tail
;
5305 if (symdef
->file_offset
== last
)
5311 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5312 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
5318 if (h
->root
.type
== bfd_link_hash_common
)
5320 /* We currently have a common symbol. The archive map contains
5321 a reference to this symbol, so we may want to include it. We
5322 only want to include it however, if this archive element
5323 contains a definition of the symbol, not just another common
5326 Unfortunately some archivers (including GNU ar) will put
5327 declarations of common symbols into their archive maps, as
5328 well as real definitions, so we cannot just go by the archive
5329 map alone. Instead we must read in the element's symbol
5330 table and check that to see what kind of symbol definition
5332 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5335 else if (h
->root
.type
!= bfd_link_hash_undefined
)
5337 if (h
->root
.type
!= bfd_link_hash_undefweak
)
5338 /* Symbol must be defined. Don't check it again. */
5343 /* We need to include this archive member. */
5344 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5345 if (element
== NULL
)
5348 if (! bfd_check_format (element
, bfd_object
))
5351 undefs_tail
= info
->hash
->undefs_tail
;
5353 if (!(*info
->callbacks
5354 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5356 if (!bfd_link_add_symbols (element
, info
))
5359 /* If there are any new undefined symbols, we need to make
5360 another pass through the archive in order to see whether
5361 they can be defined. FIXME: This isn't perfect, because
5362 common symbols wind up on undefs_tail and because an
5363 undefined symbol which is defined later on in this pass
5364 does not require another pass. This isn't a bug, but it
5365 does make the code less efficient than it could be. */
5366 if (undefs_tail
!= info
->hash
->undefs_tail
)
5369 /* Look backward to mark all symbols from this object file
5370 which we have already seen in this pass. */
5374 included
[mark
] = TRUE
;
5379 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5381 /* We mark subsequent symbols from this object file as we go
5382 on through the loop. */
5383 last
= symdef
->file_offset
;
5393 if (included
!= NULL
)
5398 /* Given an ELF BFD, add symbols to the global hash table as
5402 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5404 switch (bfd_get_format (abfd
))
5407 return elf_link_add_object_symbols (abfd
, info
);
5409 return elf_link_add_archive_symbols (abfd
, info
);
5411 bfd_set_error (bfd_error_wrong_format
);
5416 struct hash_codes_info
5418 unsigned long *hashcodes
;
5422 /* This function will be called though elf_link_hash_traverse to store
5423 all hash value of the exported symbols in an array. */
5426 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5428 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
5433 /* Ignore indirect symbols. These are added by the versioning code. */
5434 if (h
->dynindx
== -1)
5437 name
= h
->root
.root
.string
;
5438 if (h
->versioned
>= versioned
)
5440 char *p
= strchr (name
, ELF_VER_CHR
);
5443 alc
= (char *) bfd_malloc (p
- name
+ 1);
5449 memcpy (alc
, name
, p
- name
);
5450 alc
[p
- name
] = '\0';
5455 /* Compute the hash value. */
5456 ha
= bfd_elf_hash (name
);
5458 /* Store the found hash value in the array given as the argument. */
5459 *(inf
->hashcodes
)++ = ha
;
5461 /* And store it in the struct so that we can put it in the hash table
5463 h
->u
.elf_hash_value
= ha
;
5471 struct collect_gnu_hash_codes
5474 const struct elf_backend_data
*bed
;
5475 unsigned long int nsyms
;
5476 unsigned long int maskbits
;
5477 unsigned long int *hashcodes
;
5478 unsigned long int *hashval
;
5479 unsigned long int *indx
;
5480 unsigned long int *counts
;
5483 long int min_dynindx
;
5484 unsigned long int bucketcount
;
5485 unsigned long int symindx
;
5486 long int local_indx
;
5487 long int shift1
, shift2
;
5488 unsigned long int mask
;
5492 /* This function will be called though elf_link_hash_traverse to store
5493 all hash value of the exported symbols in an array. */
5496 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5498 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5503 /* Ignore indirect symbols. These are added by the versioning code. */
5504 if (h
->dynindx
== -1)
5507 /* Ignore also local symbols and undefined symbols. */
5508 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5511 name
= h
->root
.root
.string
;
5512 if (h
->versioned
>= versioned
)
5514 char *p
= strchr (name
, ELF_VER_CHR
);
5517 alc
= (char *) bfd_malloc (p
- name
+ 1);
5523 memcpy (alc
, name
, p
- name
);
5524 alc
[p
- name
] = '\0';
5529 /* Compute the hash value. */
5530 ha
= bfd_elf_gnu_hash (name
);
5532 /* Store the found hash value in the array for compute_bucket_count,
5533 and also for .dynsym reordering purposes. */
5534 s
->hashcodes
[s
->nsyms
] = ha
;
5535 s
->hashval
[h
->dynindx
] = ha
;
5537 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5538 s
->min_dynindx
= h
->dynindx
;
5546 /* This function will be called though elf_link_hash_traverse to do
5547 final dynaminc symbol renumbering. */
5550 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5552 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5553 unsigned long int bucket
;
5554 unsigned long int val
;
5556 /* Ignore indirect symbols. */
5557 if (h
->dynindx
== -1)
5560 /* Ignore also local symbols and undefined symbols. */
5561 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5563 if (h
->dynindx
>= s
->min_dynindx
)
5564 h
->dynindx
= s
->local_indx
++;
5568 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5569 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5570 & ((s
->maskbits
>> s
->shift1
) - 1);
5571 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5573 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5574 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5575 if (s
->counts
[bucket
] == 1)
5576 /* Last element terminates the chain. */
5578 bfd_put_32 (s
->output_bfd
, val
,
5579 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5580 --s
->counts
[bucket
];
5581 h
->dynindx
= s
->indx
[bucket
]++;
5585 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5588 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5590 return !(h
->forced_local
5591 || h
->root
.type
== bfd_link_hash_undefined
5592 || h
->root
.type
== bfd_link_hash_undefweak
5593 || ((h
->root
.type
== bfd_link_hash_defined
5594 || h
->root
.type
== bfd_link_hash_defweak
)
5595 && h
->root
.u
.def
.section
->output_section
== NULL
));
5598 /* Array used to determine the number of hash table buckets to use
5599 based on the number of symbols there are. If there are fewer than
5600 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5601 fewer than 37 we use 17 buckets, and so forth. We never use more
5602 than 32771 buckets. */
5604 static const size_t elf_buckets
[] =
5606 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5610 /* Compute bucket count for hashing table. We do not use a static set
5611 of possible tables sizes anymore. Instead we determine for all
5612 possible reasonable sizes of the table the outcome (i.e., the
5613 number of collisions etc) and choose the best solution. The
5614 weighting functions are not too simple to allow the table to grow
5615 without bounds. Instead one of the weighting factors is the size.
5616 Therefore the result is always a good payoff between few collisions
5617 (= short chain lengths) and table size. */
5619 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
5620 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
5621 unsigned long int nsyms
,
5624 size_t best_size
= 0;
5625 unsigned long int i
;
5627 /* We have a problem here. The following code to optimize the table
5628 size requires an integer type with more the 32 bits. If
5629 BFD_HOST_U_64_BIT is set we know about such a type. */
5630 #ifdef BFD_HOST_U_64_BIT
5635 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5636 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5637 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5638 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5639 unsigned long int *counts
;
5641 unsigned int no_improvement_count
= 0;
5643 /* Possible optimization parameters: if we have NSYMS symbols we say
5644 that the hashing table must at least have NSYMS/4 and at most
5646 minsize
= nsyms
/ 4;
5649 best_size
= maxsize
= nsyms
* 2;
5654 if ((best_size
& 31) == 0)
5658 /* Create array where we count the collisions in. We must use bfd_malloc
5659 since the size could be large. */
5661 amt
*= sizeof (unsigned long int);
5662 counts
= (unsigned long int *) bfd_malloc (amt
);
5666 /* Compute the "optimal" size for the hash table. The criteria is a
5667 minimal chain length. The minor criteria is (of course) the size
5669 for (i
= minsize
; i
< maxsize
; ++i
)
5671 /* Walk through the array of hashcodes and count the collisions. */
5672 BFD_HOST_U_64_BIT max
;
5673 unsigned long int j
;
5674 unsigned long int fact
;
5676 if (gnu_hash
&& (i
& 31) == 0)
5679 memset (counts
, '\0', i
* sizeof (unsigned long int));
5681 /* Determine how often each hash bucket is used. */
5682 for (j
= 0; j
< nsyms
; ++j
)
5683 ++counts
[hashcodes
[j
] % i
];
5685 /* For the weight function we need some information about the
5686 pagesize on the target. This is information need not be 100%
5687 accurate. Since this information is not available (so far) we
5688 define it here to a reasonable default value. If it is crucial
5689 to have a better value some day simply define this value. */
5690 # ifndef BFD_TARGET_PAGESIZE
5691 # define BFD_TARGET_PAGESIZE (4096)
5694 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5696 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5699 /* Variant 1: optimize for short chains. We add the squares
5700 of all the chain lengths (which favors many small chain
5701 over a few long chains). */
5702 for (j
= 0; j
< i
; ++j
)
5703 max
+= counts
[j
] * counts
[j
];
5705 /* This adds penalties for the overall size of the table. */
5706 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5709 /* Variant 2: Optimize a lot more for small table. Here we
5710 also add squares of the size but we also add penalties for
5711 empty slots (the +1 term). */
5712 for (j
= 0; j
< i
; ++j
)
5713 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5715 /* The overall size of the table is considered, but not as
5716 strong as in variant 1, where it is squared. */
5717 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5721 /* Compare with current best results. */
5722 if (max
< best_chlen
)
5726 no_improvement_count
= 0;
5728 /* PR 11843: Avoid futile long searches for the best bucket size
5729 when there are a large number of symbols. */
5730 else if (++no_improvement_count
== 100)
5737 #endif /* defined (BFD_HOST_U_64_BIT) */
5739 /* This is the fallback solution if no 64bit type is available or if we
5740 are not supposed to spend much time on optimizations. We select the
5741 bucket count using a fixed set of numbers. */
5742 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5744 best_size
= elf_buckets
[i
];
5745 if (nsyms
< elf_buckets
[i
+ 1])
5748 if (gnu_hash
&& best_size
< 2)
5755 /* Size any SHT_GROUP section for ld -r. */
5758 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
5762 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
5763 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
5764 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
5769 /* Set a default stack segment size. The value in INFO wins. If it
5770 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5771 undefined it is initialized. */
5774 bfd_elf_stack_segment_size (bfd
*output_bfd
,
5775 struct bfd_link_info
*info
,
5776 const char *legacy_symbol
,
5777 bfd_vma default_size
)
5779 struct elf_link_hash_entry
*h
= NULL
;
5781 /* Look for legacy symbol. */
5783 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
5784 FALSE
, FALSE
, FALSE
);
5785 if (h
&& (h
->root
.type
== bfd_link_hash_defined
5786 || h
->root
.type
== bfd_link_hash_defweak
)
5788 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
5790 /* The symbol has no type if specified on the command line. */
5791 h
->type
= STT_OBJECT
;
5792 if (info
->stacksize
)
5793 /* xgettext:c-format */
5794 _bfd_error_handler (_("%B: stack size specified and %s set"),
5795 output_bfd
, legacy_symbol
);
5796 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
5797 /* xgettext:c-format */
5798 _bfd_error_handler (_("%B: %s not absolute"),
5799 output_bfd
, legacy_symbol
);
5801 info
->stacksize
= h
->root
.u
.def
.value
;
5804 if (!info
->stacksize
)
5805 /* If the user didn't set a size, or explicitly inhibit the
5806 size, set it now. */
5807 info
->stacksize
= default_size
;
5809 /* Provide the legacy symbol, if it is referenced. */
5810 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
5811 || h
->root
.type
== bfd_link_hash_undefweak
))
5813 struct bfd_link_hash_entry
*bh
= NULL
;
5815 if (!(_bfd_generic_link_add_one_symbol
5816 (info
, output_bfd
, legacy_symbol
,
5817 BSF_GLOBAL
, bfd_abs_section_ptr
,
5818 info
->stacksize
>= 0 ? info
->stacksize
: 0,
5819 NULL
, FALSE
, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
5822 h
= (struct elf_link_hash_entry
*) bh
;
5824 h
->type
= STT_OBJECT
;
5830 /* Set up the sizes and contents of the ELF dynamic sections. This is
5831 called by the ELF linker emulation before_allocation routine. We
5832 must set the sizes of the sections before the linker sets the
5833 addresses of the various sections. */
5836 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5839 const char *filter_shlib
,
5841 const char *depaudit
,
5842 const char * const *auxiliary_filters
,
5843 struct bfd_link_info
*info
,
5844 asection
**sinterpptr
)
5848 const struct elf_backend_data
*bed
;
5849 struct elf_info_failed asvinfo
;
5853 soname_indx
= (size_t) -1;
5855 if (!is_elf_hash_table (info
->hash
))
5858 bed
= get_elf_backend_data (output_bfd
);
5860 /* Any syms created from now on start with -1 in
5861 got.refcount/offset and plt.refcount/offset. */
5862 elf_hash_table (info
)->init_got_refcount
5863 = elf_hash_table (info
)->init_got_offset
;
5864 elf_hash_table (info
)->init_plt_refcount
5865 = elf_hash_table (info
)->init_plt_offset
;
5867 if (bfd_link_relocatable (info
)
5868 && !_bfd_elf_size_group_sections (info
))
5871 /* The backend may have to create some sections regardless of whether
5872 we're dynamic or not. */
5873 if (bed
->elf_backend_always_size_sections
5874 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5877 /* Determine any GNU_STACK segment requirements, after the backend
5878 has had a chance to set a default segment size. */
5879 if (info
->execstack
)
5880 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
5881 else if (info
->noexecstack
)
5882 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
5886 asection
*notesec
= NULL
;
5889 for (inputobj
= info
->input_bfds
;
5891 inputobj
= inputobj
->link
.next
)
5896 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
5898 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
5901 if (s
->flags
& SEC_CODE
)
5905 else if (bed
->default_execstack
)
5908 if (notesec
|| info
->stacksize
> 0)
5909 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
5910 if (notesec
&& exec
&& bfd_link_relocatable (info
)
5911 && notesec
->output_section
!= bfd_abs_section_ptr
)
5912 notesec
->output_section
->flags
|= SEC_CODE
;
5915 dynobj
= elf_hash_table (info
)->dynobj
;
5917 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
5919 struct elf_info_failed eif
;
5920 struct elf_link_hash_entry
*h
;
5922 struct bfd_elf_version_tree
*t
;
5923 struct bfd_elf_version_expr
*d
;
5925 bfd_boolean all_defined
;
5927 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
5928 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
5932 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5934 if (soname_indx
== (size_t) -1
5935 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5941 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5943 info
->flags
|= DF_SYMBOLIC
;
5951 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5953 if (indx
== (size_t) -1)
5956 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
5957 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
5961 if (filter_shlib
!= NULL
)
5965 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5966 filter_shlib
, TRUE
);
5967 if (indx
== (size_t) -1
5968 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5972 if (auxiliary_filters
!= NULL
)
5974 const char * const *p
;
5976 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5980 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5982 if (indx
== (size_t) -1
5983 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5992 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
5994 if (indx
== (size_t) -1
5995 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
5999 if (depaudit
!= NULL
)
6003 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
6005 if (indx
== (size_t) -1
6006 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
6013 /* If we are supposed to export all symbols into the dynamic symbol
6014 table (this is not the normal case), then do so. */
6015 if (info
->export_dynamic
6016 || (bfd_link_executable (info
) && info
->dynamic
))
6018 elf_link_hash_traverse (elf_hash_table (info
),
6019 _bfd_elf_export_symbol
,
6025 /* Make all global versions with definition. */
6026 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6027 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6028 if (!d
->symver
&& d
->literal
)
6030 const char *verstr
, *name
;
6031 size_t namelen
, verlen
, newlen
;
6032 char *newname
, *p
, leading_char
;
6033 struct elf_link_hash_entry
*newh
;
6035 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6037 namelen
= strlen (name
) + (leading_char
!= '\0');
6039 verlen
= strlen (verstr
);
6040 newlen
= namelen
+ verlen
+ 3;
6042 newname
= (char *) bfd_malloc (newlen
);
6043 if (newname
== NULL
)
6045 newname
[0] = leading_char
;
6046 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6048 /* Check the hidden versioned definition. */
6049 p
= newname
+ namelen
;
6051 memcpy (p
, verstr
, verlen
+ 1);
6052 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6053 newname
, FALSE
, FALSE
,
6056 || (newh
->root
.type
!= bfd_link_hash_defined
6057 && newh
->root
.type
!= bfd_link_hash_defweak
))
6059 /* Check the default versioned definition. */
6061 memcpy (p
, verstr
, verlen
+ 1);
6062 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6063 newname
, FALSE
, FALSE
,
6068 /* Mark this version if there is a definition and it is
6069 not defined in a shared object. */
6071 && !newh
->def_dynamic
6072 && (newh
->root
.type
== bfd_link_hash_defined
6073 || newh
->root
.type
== bfd_link_hash_defweak
))
6077 /* Attach all the symbols to their version information. */
6078 asvinfo
.info
= info
;
6079 asvinfo
.failed
= FALSE
;
6081 elf_link_hash_traverse (elf_hash_table (info
),
6082 _bfd_elf_link_assign_sym_version
,
6087 if (!info
->allow_undefined_version
)
6089 /* Check if all global versions have a definition. */
6091 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6092 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6093 if (d
->literal
&& !d
->symver
&& !d
->script
)
6096 (_("%s: undefined version: %s"),
6097 d
->pattern
, t
->name
);
6098 all_defined
= FALSE
;
6103 bfd_set_error (bfd_error_bad_value
);
6108 /* Find all symbols which were defined in a dynamic object and make
6109 the backend pick a reasonable value for them. */
6110 elf_link_hash_traverse (elf_hash_table (info
),
6111 _bfd_elf_adjust_dynamic_symbol
,
6116 /* Add some entries to the .dynamic section. We fill in some of the
6117 values later, in bfd_elf_final_link, but we must add the entries
6118 now so that we know the final size of the .dynamic section. */
6120 /* If there are initialization and/or finalization functions to
6121 call then add the corresponding DT_INIT/DT_FINI entries. */
6122 h
= (info
->init_function
6123 ? elf_link_hash_lookup (elf_hash_table (info
),
6124 info
->init_function
, FALSE
,
6131 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
6134 h
= (info
->fini_function
6135 ? elf_link_hash_lookup (elf_hash_table (info
),
6136 info
->fini_function
, FALSE
,
6143 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
6147 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
6148 if (s
!= NULL
&& s
->linker_has_input
)
6150 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6151 if (! bfd_link_executable (info
))
6156 for (sub
= info
->input_bfds
; sub
!= NULL
;
6157 sub
= sub
->link
.next
)
6158 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
6159 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
6160 if (elf_section_data (o
)->this_hdr
.sh_type
6161 == SHT_PREINIT_ARRAY
)
6164 (_("%B: .preinit_array section is not allowed in DSO"),
6169 bfd_set_error (bfd_error_nonrepresentable_section
);
6173 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
6174 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
6177 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
6178 if (s
!= NULL
&& s
->linker_has_input
)
6180 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
6181 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
6184 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
6185 if (s
!= NULL
&& s
->linker_has_input
)
6187 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
6188 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
6192 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
6193 /* If .dynstr is excluded from the link, we don't want any of
6194 these tags. Strictly, we should be checking each section
6195 individually; This quick check covers for the case where
6196 someone does a /DISCARD/ : { *(*) }. */
6197 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
6199 bfd_size_type strsize
;
6201 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6202 if ((info
->emit_hash
6203 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
6204 || (info
->emit_gnu_hash
6205 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
6206 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
6207 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
6208 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
6209 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
6210 bed
->s
->sizeof_sym
))
6215 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
6218 /* The backend must work out the sizes of all the other dynamic
6221 && bed
->elf_backend_size_dynamic_sections
!= NULL
6222 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
6225 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6227 unsigned long section_sym_count
;
6228 struct bfd_elf_version_tree
*verdefs
;
6231 /* Set up the version definition section. */
6232 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6233 BFD_ASSERT (s
!= NULL
);
6235 /* We may have created additional version definitions if we are
6236 just linking a regular application. */
6237 verdefs
= info
->version_info
;
6239 /* Skip anonymous version tag. */
6240 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6241 verdefs
= verdefs
->next
;
6243 if (verdefs
== NULL
&& !info
->create_default_symver
)
6244 s
->flags
|= SEC_EXCLUDE
;
6249 struct bfd_elf_version_tree
*t
;
6251 Elf_Internal_Verdef def
;
6252 Elf_Internal_Verdaux defaux
;
6253 struct bfd_link_hash_entry
*bh
;
6254 struct elf_link_hash_entry
*h
;
6260 /* Make space for the base version. */
6261 size
+= sizeof (Elf_External_Verdef
);
6262 size
+= sizeof (Elf_External_Verdaux
);
6265 /* Make space for the default version. */
6266 if (info
->create_default_symver
)
6268 size
+= sizeof (Elf_External_Verdef
);
6272 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6274 struct bfd_elf_version_deps
*n
;
6276 /* Don't emit base version twice. */
6280 size
+= sizeof (Elf_External_Verdef
);
6281 size
+= sizeof (Elf_External_Verdaux
);
6284 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6285 size
+= sizeof (Elf_External_Verdaux
);
6289 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6290 if (s
->contents
== NULL
&& s
->size
!= 0)
6293 /* Fill in the version definition section. */
6297 def
.vd_version
= VER_DEF_CURRENT
;
6298 def
.vd_flags
= VER_FLG_BASE
;
6301 if (info
->create_default_symver
)
6303 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6304 def
.vd_next
= sizeof (Elf_External_Verdef
);
6308 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6309 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6310 + sizeof (Elf_External_Verdaux
));
6313 if (soname_indx
!= (size_t) -1)
6315 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6317 def
.vd_hash
= bfd_elf_hash (soname
);
6318 defaux
.vda_name
= soname_indx
;
6325 name
= lbasename (output_bfd
->filename
);
6326 def
.vd_hash
= bfd_elf_hash (name
);
6327 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6329 if (indx
== (size_t) -1)
6331 defaux
.vda_name
= indx
;
6333 defaux
.vda_next
= 0;
6335 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6336 (Elf_External_Verdef
*) p
);
6337 p
+= sizeof (Elf_External_Verdef
);
6338 if (info
->create_default_symver
)
6340 /* Add a symbol representing this version. */
6342 if (! (_bfd_generic_link_add_one_symbol
6343 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6345 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6347 h
= (struct elf_link_hash_entry
*) bh
;
6350 h
->type
= STT_OBJECT
;
6351 h
->verinfo
.vertree
= NULL
;
6353 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6356 /* Create a duplicate of the base version with the same
6357 aux block, but different flags. */
6360 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6362 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6363 + sizeof (Elf_External_Verdaux
));
6366 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6367 (Elf_External_Verdef
*) p
);
6368 p
+= sizeof (Elf_External_Verdef
);
6370 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6371 (Elf_External_Verdaux
*) p
);
6372 p
+= sizeof (Elf_External_Verdaux
);
6374 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6377 struct bfd_elf_version_deps
*n
;
6379 /* Don't emit the base version twice. */
6384 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6387 /* Add a symbol representing this version. */
6389 if (! (_bfd_generic_link_add_one_symbol
6390 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6392 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6394 h
= (struct elf_link_hash_entry
*) bh
;
6397 h
->type
= STT_OBJECT
;
6398 h
->verinfo
.vertree
= t
;
6400 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6403 def
.vd_version
= VER_DEF_CURRENT
;
6405 if (t
->globals
.list
== NULL
6406 && t
->locals
.list
== NULL
6408 def
.vd_flags
|= VER_FLG_WEAK
;
6409 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6410 def
.vd_cnt
= cdeps
+ 1;
6411 def
.vd_hash
= bfd_elf_hash (t
->name
);
6412 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6415 /* If a basever node is next, it *must* be the last node in
6416 the chain, otherwise Verdef construction breaks. */
6417 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6418 BFD_ASSERT (t
->next
->next
== NULL
);
6420 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6421 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6422 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6424 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6425 (Elf_External_Verdef
*) p
);
6426 p
+= sizeof (Elf_External_Verdef
);
6428 defaux
.vda_name
= h
->dynstr_index
;
6429 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6431 defaux
.vda_next
= 0;
6432 if (t
->deps
!= NULL
)
6433 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6434 t
->name_indx
= defaux
.vda_name
;
6436 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6437 (Elf_External_Verdaux
*) p
);
6438 p
+= sizeof (Elf_External_Verdaux
);
6440 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6442 if (n
->version_needed
== NULL
)
6444 /* This can happen if there was an error in the
6446 defaux
.vda_name
= 0;
6450 defaux
.vda_name
= n
->version_needed
->name_indx
;
6451 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6454 if (n
->next
== NULL
)
6455 defaux
.vda_next
= 0;
6457 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6459 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6460 (Elf_External_Verdaux
*) p
);
6461 p
+= sizeof (Elf_External_Verdaux
);
6465 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
6466 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
6469 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6472 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
6474 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
6477 else if (info
->flags
& DF_BIND_NOW
)
6479 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
6485 if (bfd_link_executable (info
))
6486 info
->flags_1
&= ~ (DF_1_INITFIRST
6489 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
6493 /* Work out the size of the version reference section. */
6495 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6496 BFD_ASSERT (s
!= NULL
);
6498 struct elf_find_verdep_info sinfo
;
6501 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6502 if (sinfo
.vers
== 0)
6504 sinfo
.failed
= FALSE
;
6506 elf_link_hash_traverse (elf_hash_table (info
),
6507 _bfd_elf_link_find_version_dependencies
,
6512 if (elf_tdata (output_bfd
)->verref
== NULL
)
6513 s
->flags
|= SEC_EXCLUDE
;
6516 Elf_Internal_Verneed
*t
;
6521 /* Build the version dependency section. */
6524 for (t
= elf_tdata (output_bfd
)->verref
;
6528 Elf_Internal_Vernaux
*a
;
6530 size
+= sizeof (Elf_External_Verneed
);
6532 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6533 size
+= sizeof (Elf_External_Vernaux
);
6537 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6538 if (s
->contents
== NULL
)
6542 for (t
= elf_tdata (output_bfd
)->verref
;
6547 Elf_Internal_Vernaux
*a
;
6551 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6554 t
->vn_version
= VER_NEED_CURRENT
;
6556 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6557 elf_dt_name (t
->vn_bfd
) != NULL
6558 ? elf_dt_name (t
->vn_bfd
)
6559 : lbasename (t
->vn_bfd
->filename
),
6561 if (indx
== (size_t) -1)
6564 t
->vn_aux
= sizeof (Elf_External_Verneed
);
6565 if (t
->vn_nextref
== NULL
)
6568 t
->vn_next
= (sizeof (Elf_External_Verneed
)
6569 + caux
* sizeof (Elf_External_Vernaux
));
6571 _bfd_elf_swap_verneed_out (output_bfd
, t
,
6572 (Elf_External_Verneed
*) p
);
6573 p
+= sizeof (Elf_External_Verneed
);
6575 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6577 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6578 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6579 a
->vna_nodename
, FALSE
);
6580 if (indx
== (size_t) -1)
6583 if (a
->vna_nextptr
== NULL
)
6586 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6588 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6589 (Elf_External_Vernaux
*) p
);
6590 p
+= sizeof (Elf_External_Vernaux
);
6594 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
6595 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
6598 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6602 if ((elf_tdata (output_bfd
)->cverrefs
== 0
6603 && elf_tdata (output_bfd
)->cverdefs
== 0)
6604 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6605 §ion_sym_count
) == 0)
6607 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6608 s
->flags
|= SEC_EXCLUDE
;
6614 /* Find the first non-excluded output section. We'll use its
6615 section symbol for some emitted relocs. */
6617 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
6621 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6622 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
6623 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6625 elf_hash_table (info
)->text_index_section
= s
;
6630 /* Find two non-excluded output sections, one for code, one for data.
6631 We'll use their section symbols for some emitted relocs. */
6633 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6637 /* Data first, since setting text_index_section changes
6638 _bfd_elf_link_omit_section_dynsym. */
6639 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6640 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
6641 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6643 elf_hash_table (info
)->data_index_section
= s
;
6647 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6648 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
6649 == (SEC_ALLOC
| SEC_READONLY
))
6650 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6652 elf_hash_table (info
)->text_index_section
= s
;
6656 if (elf_hash_table (info
)->text_index_section
== NULL
)
6657 elf_hash_table (info
)->text_index_section
6658 = elf_hash_table (info
)->data_index_section
;
6662 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6664 const struct elf_backend_data
*bed
;
6666 if (!is_elf_hash_table (info
->hash
))
6669 bed
= get_elf_backend_data (output_bfd
);
6670 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6672 if (elf_hash_table (info
)->dynamic_sections_created
)
6676 bfd_size_type dynsymcount
;
6677 unsigned long section_sym_count
;
6678 unsigned int dtagcount
;
6680 dynobj
= elf_hash_table (info
)->dynobj
;
6682 /* Assign dynsym indicies. In a shared library we generate a
6683 section symbol for each output section, which come first.
6684 Next come all of the back-end allocated local dynamic syms,
6685 followed by the rest of the global symbols. */
6687 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6688 §ion_sym_count
);
6690 /* Work out the size of the symbol version section. */
6691 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6692 BFD_ASSERT (s
!= NULL
);
6693 if ((s
->flags
& SEC_EXCLUDE
) == 0)
6695 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6696 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6697 if (s
->contents
== NULL
)
6700 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
6704 /* Set the size of the .dynsym and .hash sections. We counted
6705 the number of dynamic symbols in elf_link_add_object_symbols.
6706 We will build the contents of .dynsym and .hash when we build
6707 the final symbol table, because until then we do not know the
6708 correct value to give the symbols. We built the .dynstr
6709 section as we went along in elf_link_add_object_symbols. */
6710 s
= elf_hash_table (info
)->dynsym
;
6711 BFD_ASSERT (s
!= NULL
);
6712 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
6714 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6715 if (s
->contents
== NULL
)
6718 /* The first entry in .dynsym is a dummy symbol. Clear all the
6719 section syms, in case we don't output them all. */
6720 ++section_sym_count
;
6721 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
6723 elf_hash_table (info
)->bucketcount
= 0;
6725 /* Compute the size of the hashing table. As a side effect this
6726 computes the hash values for all the names we export. */
6727 if (info
->emit_hash
)
6729 unsigned long int *hashcodes
;
6730 struct hash_codes_info hashinf
;
6732 unsigned long int nsyms
;
6734 size_t hash_entry_size
;
6736 /* Compute the hash values for all exported symbols. At the same
6737 time store the values in an array so that we could use them for
6739 amt
= dynsymcount
* sizeof (unsigned long int);
6740 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
6741 if (hashcodes
== NULL
)
6743 hashinf
.hashcodes
= hashcodes
;
6744 hashinf
.error
= FALSE
;
6746 /* Put all hash values in HASHCODES. */
6747 elf_link_hash_traverse (elf_hash_table (info
),
6748 elf_collect_hash_codes
, &hashinf
);
6755 nsyms
= hashinf
.hashcodes
- hashcodes
;
6757 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6760 if (bucketcount
== 0)
6763 elf_hash_table (info
)->bucketcount
= bucketcount
;
6765 s
= bfd_get_linker_section (dynobj
, ".hash");
6766 BFD_ASSERT (s
!= NULL
);
6767 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6768 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6769 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6770 if (s
->contents
== NULL
)
6773 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6774 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6775 s
->contents
+ hash_entry_size
);
6778 if (info
->emit_gnu_hash
)
6781 unsigned char *contents
;
6782 struct collect_gnu_hash_codes cinfo
;
6786 memset (&cinfo
, 0, sizeof (cinfo
));
6788 /* Compute the hash values for all exported symbols. At the same
6789 time store the values in an array so that we could use them for
6791 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6792 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
6793 if (cinfo
.hashcodes
== NULL
)
6796 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6797 cinfo
.min_dynindx
= -1;
6798 cinfo
.output_bfd
= output_bfd
;
6801 /* Put all hash values in HASHCODES. */
6802 elf_link_hash_traverse (elf_hash_table (info
),
6803 elf_collect_gnu_hash_codes
, &cinfo
);
6806 free (cinfo
.hashcodes
);
6811 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6813 if (bucketcount
== 0)
6815 free (cinfo
.hashcodes
);
6819 s
= bfd_get_linker_section (dynobj
, ".gnu.hash");
6820 BFD_ASSERT (s
!= NULL
);
6822 if (cinfo
.nsyms
== 0)
6824 /* Empty .gnu.hash section is special. */
6825 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6826 free (cinfo
.hashcodes
);
6827 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6828 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6829 if (contents
== NULL
)
6831 s
->contents
= contents
;
6832 /* 1 empty bucket. */
6833 bfd_put_32 (output_bfd
, 1, contents
);
6834 /* SYMIDX above the special symbol 0. */
6835 bfd_put_32 (output_bfd
, 1, contents
+ 4);
6836 /* Just one word for bitmask. */
6837 bfd_put_32 (output_bfd
, 1, contents
+ 8);
6838 /* Only hash fn bloom filter. */
6839 bfd_put_32 (output_bfd
, 0, contents
+ 12);
6840 /* No hashes are valid - empty bitmask. */
6841 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
6842 /* No hashes in the only bucket. */
6843 bfd_put_32 (output_bfd
, 0,
6844 contents
+ 16 + bed
->s
->arch_size
/ 8);
6848 unsigned long int maskwords
, maskbitslog2
, x
;
6849 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
6853 while ((x
>>= 1) != 0)
6855 if (maskbitslog2
< 3)
6857 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
6858 maskbitslog2
= maskbitslog2
+ 3;
6860 maskbitslog2
= maskbitslog2
+ 2;
6861 if (bed
->s
->arch_size
== 64)
6863 if (maskbitslog2
== 5)
6869 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
6870 cinfo
.shift2
= maskbitslog2
;
6871 cinfo
.maskbits
= 1 << maskbitslog2
;
6872 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
6873 amt
= bucketcount
* sizeof (unsigned long int) * 2;
6874 amt
+= maskwords
* sizeof (bfd_vma
);
6875 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
6876 if (cinfo
.bitmask
== NULL
)
6878 free (cinfo
.hashcodes
);
6882 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
6883 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
6884 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
6885 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
6887 /* Determine how often each hash bucket is used. */
6888 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
6889 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
6890 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
6892 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
6893 if (cinfo
.counts
[i
] != 0)
6895 cinfo
.indx
[i
] = cnt
;
6896 cnt
+= cinfo
.counts
[i
];
6898 BFD_ASSERT (cnt
== dynsymcount
);
6899 cinfo
.bucketcount
= bucketcount
;
6900 cinfo
.local_indx
= cinfo
.min_dynindx
;
6902 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
6903 s
->size
+= cinfo
.maskbits
/ 8;
6904 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6905 if (contents
== NULL
)
6907 free (cinfo
.bitmask
);
6908 free (cinfo
.hashcodes
);
6912 s
->contents
= contents
;
6913 bfd_put_32 (output_bfd
, bucketcount
, contents
);
6914 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
6915 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
6916 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
6917 contents
+= 16 + cinfo
.maskbits
/ 8;
6919 for (i
= 0; i
< bucketcount
; ++i
)
6921 if (cinfo
.counts
[i
] == 0)
6922 bfd_put_32 (output_bfd
, 0, contents
);
6924 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
6928 cinfo
.contents
= contents
;
6930 /* Renumber dynamic symbols, populate .gnu.hash section. */
6931 elf_link_hash_traverse (elf_hash_table (info
),
6932 elf_renumber_gnu_hash_syms
, &cinfo
);
6934 contents
= s
->contents
+ 16;
6935 for (i
= 0; i
< maskwords
; ++i
)
6937 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
6939 contents
+= bed
->s
->arch_size
/ 8;
6942 free (cinfo
.bitmask
);
6943 free (cinfo
.hashcodes
);
6947 s
= bfd_get_linker_section (dynobj
, ".dynstr");
6948 BFD_ASSERT (s
!= NULL
);
6950 elf_finalize_dynstr (output_bfd
, info
);
6952 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6954 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
6955 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
6962 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6965 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
6968 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
6969 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
6972 /* Finish SHF_MERGE section merging. */
6975 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
6980 if (!is_elf_hash_table (info
->hash
))
6983 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6984 if ((ibfd
->flags
& DYNAMIC
) == 0
6985 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6986 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
6987 == get_elf_backend_data (obfd
)->s
->elfclass
))
6988 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6989 if ((sec
->flags
& SEC_MERGE
) != 0
6990 && !bfd_is_abs_section (sec
->output_section
))
6992 struct bfd_elf_section_data
*secdata
;
6994 secdata
= elf_section_data (sec
);
6995 if (! _bfd_add_merge_section (obfd
,
6996 &elf_hash_table (info
)->merge_info
,
6997 sec
, &secdata
->sec_info
))
6999 else if (secdata
->sec_info
)
7000 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7003 if (elf_hash_table (info
)->merge_info
!= NULL
)
7004 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7005 merge_sections_remove_hook
);
7009 /* Create an entry in an ELF linker hash table. */
7011 struct bfd_hash_entry
*
7012 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7013 struct bfd_hash_table
*table
,
7016 /* Allocate the structure if it has not already been allocated by a
7020 entry
= (struct bfd_hash_entry
*)
7021 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7026 /* Call the allocation method of the superclass. */
7027 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7030 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7031 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7033 /* Set local fields. */
7036 ret
->got
= htab
->init_got_refcount
;
7037 ret
->plt
= htab
->init_plt_refcount
;
7038 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7039 - offsetof (struct elf_link_hash_entry
, size
)));
7040 /* Assume that we have been called by a non-ELF symbol reader.
7041 This flag is then reset by the code which reads an ELF input
7042 file. This ensures that a symbol created by a non-ELF symbol
7043 reader will have the flag set correctly. */
7050 /* Copy data from an indirect symbol to its direct symbol, hiding the
7051 old indirect symbol. Also used for copying flags to a weakdef. */
7054 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7055 struct elf_link_hash_entry
*dir
,
7056 struct elf_link_hash_entry
*ind
)
7058 struct elf_link_hash_table
*htab
;
7060 /* Copy down any references that we may have already seen to the
7061 symbol which just became indirect if DIR isn't a hidden versioned
7064 if (dir
->versioned
!= versioned_hidden
)
7066 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7067 dir
->ref_regular
|= ind
->ref_regular
;
7068 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7069 dir
->non_got_ref
|= ind
->non_got_ref
;
7070 dir
->needs_plt
|= ind
->needs_plt
;
7071 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7074 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7077 /* Copy over the global and procedure linkage table refcount entries.
7078 These may have been already set up by a check_relocs routine. */
7079 htab
= elf_hash_table (info
);
7080 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7082 if (dir
->got
.refcount
< 0)
7083 dir
->got
.refcount
= 0;
7084 dir
->got
.refcount
+= ind
->got
.refcount
;
7085 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7088 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7090 if (dir
->plt
.refcount
< 0)
7091 dir
->plt
.refcount
= 0;
7092 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7093 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7096 if (ind
->dynindx
!= -1)
7098 if (dir
->dynindx
!= -1)
7099 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7100 dir
->dynindx
= ind
->dynindx
;
7101 dir
->dynstr_index
= ind
->dynstr_index
;
7103 ind
->dynstr_index
= 0;
7108 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7109 struct elf_link_hash_entry
*h
,
7110 bfd_boolean force_local
)
7112 /* STT_GNU_IFUNC symbol must go through PLT. */
7113 if (h
->type
!= STT_GNU_IFUNC
)
7115 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7120 h
->forced_local
= 1;
7121 if (h
->dynindx
!= -1)
7124 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7130 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7134 _bfd_elf_link_hash_table_init
7135 (struct elf_link_hash_table
*table
,
7137 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
7138 struct bfd_hash_table
*,
7140 unsigned int entsize
,
7141 enum elf_target_id target_id
)
7144 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
7146 table
->init_got_refcount
.refcount
= can_refcount
- 1;
7147 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
7148 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
7149 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
7150 /* The first dynamic symbol is a dummy. */
7151 table
->dynsymcount
= 1;
7153 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
7155 table
->root
.type
= bfd_link_elf_hash_table
;
7156 table
->hash_table_id
= target_id
;
7161 /* Create an ELF linker hash table. */
7163 struct bfd_link_hash_table
*
7164 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7166 struct elf_link_hash_table
*ret
;
7167 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
7169 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7173 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7174 sizeof (struct elf_link_hash_entry
),
7180 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7185 /* Destroy an ELF linker hash table. */
7188 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7190 struct elf_link_hash_table
*htab
;
7192 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7193 if (htab
->dynstr
!= NULL
)
7194 _bfd_elf_strtab_free (htab
->dynstr
);
7195 _bfd_merge_sections_free (htab
->merge_info
);
7196 _bfd_generic_link_hash_table_free (obfd
);
7199 /* This is a hook for the ELF emulation code in the generic linker to
7200 tell the backend linker what file name to use for the DT_NEEDED
7201 entry for a dynamic object. */
7204 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7206 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7207 && bfd_get_format (abfd
) == bfd_object
)
7208 elf_dt_name (abfd
) = name
;
7212 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
7215 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7216 && bfd_get_format (abfd
) == bfd_object
)
7217 lib_class
= elf_dyn_lib_class (abfd
);
7224 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
7226 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7227 && bfd_get_format (abfd
) == bfd_object
)
7228 elf_dyn_lib_class (abfd
) = lib_class
;
7231 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7232 the linker ELF emulation code. */
7234 struct bfd_link_needed_list
*
7235 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7236 struct bfd_link_info
*info
)
7238 if (! is_elf_hash_table (info
->hash
))
7240 return elf_hash_table (info
)->needed
;
7243 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7244 hook for the linker ELF emulation code. */
7246 struct bfd_link_needed_list
*
7247 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7248 struct bfd_link_info
*info
)
7250 if (! is_elf_hash_table (info
->hash
))
7252 return elf_hash_table (info
)->runpath
;
7255 /* Get the name actually used for a dynamic object for a link. This
7256 is the SONAME entry if there is one. Otherwise, it is the string
7257 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7260 bfd_elf_get_dt_soname (bfd
*abfd
)
7262 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7263 && bfd_get_format (abfd
) == bfd_object
)
7264 return elf_dt_name (abfd
);
7268 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7269 the ELF linker emulation code. */
7272 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
7273 struct bfd_link_needed_list
**pneeded
)
7276 bfd_byte
*dynbuf
= NULL
;
7277 unsigned int elfsec
;
7278 unsigned long shlink
;
7279 bfd_byte
*extdyn
, *extdynend
;
7281 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
7285 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
7286 || bfd_get_format (abfd
) != bfd_object
)
7289 s
= bfd_get_section_by_name (abfd
, ".dynamic");
7290 if (s
== NULL
|| s
->size
== 0)
7293 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
7296 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
7297 if (elfsec
== SHN_BAD
)
7300 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
7302 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
7303 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
7306 extdynend
= extdyn
+ s
->size
;
7307 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
7309 Elf_Internal_Dyn dyn
;
7311 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
7313 if (dyn
.d_tag
== DT_NULL
)
7316 if (dyn
.d_tag
== DT_NEEDED
)
7319 struct bfd_link_needed_list
*l
;
7320 unsigned int tagv
= dyn
.d_un
.d_val
;
7323 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
7328 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
7349 struct elf_symbuf_symbol
7351 unsigned long st_name
; /* Symbol name, index in string tbl */
7352 unsigned char st_info
; /* Type and binding attributes */
7353 unsigned char st_other
; /* Visibilty, and target specific */
7356 struct elf_symbuf_head
7358 struct elf_symbuf_symbol
*ssym
;
7360 unsigned int st_shndx
;
7367 Elf_Internal_Sym
*isym
;
7368 struct elf_symbuf_symbol
*ssym
;
7373 /* Sort references to symbols by ascending section number. */
7376 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
7378 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
7379 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
7381 return s1
->st_shndx
- s2
->st_shndx
;
7385 elf_sym_name_compare (const void *arg1
, const void *arg2
)
7387 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
7388 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
7389 return strcmp (s1
->name
, s2
->name
);
7392 static struct elf_symbuf_head
*
7393 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
7395 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
7396 struct elf_symbuf_symbol
*ssym
;
7397 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
7398 size_t i
, shndx_count
, total_size
;
7400 indbuf
= (Elf_Internal_Sym
**) bfd_malloc2 (symcount
, sizeof (*indbuf
));
7404 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
7405 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
7406 *ind
++ = &isymbuf
[i
];
7409 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
7410 elf_sort_elf_symbol
);
7413 if (indbufend
> indbuf
)
7414 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
7415 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
7418 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
7419 + (indbufend
- indbuf
) * sizeof (*ssym
));
7420 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
7421 if (ssymbuf
== NULL
)
7427 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
7428 ssymbuf
->ssym
= NULL
;
7429 ssymbuf
->count
= shndx_count
;
7430 ssymbuf
->st_shndx
= 0;
7431 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
7433 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
7436 ssymhead
->ssym
= ssym
;
7437 ssymhead
->count
= 0;
7438 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
7440 ssym
->st_name
= (*ind
)->st_name
;
7441 ssym
->st_info
= (*ind
)->st_info
;
7442 ssym
->st_other
= (*ind
)->st_other
;
7445 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
7446 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
7453 /* Check if 2 sections define the same set of local and global
7457 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
7458 struct bfd_link_info
*info
)
7461 const struct elf_backend_data
*bed1
, *bed2
;
7462 Elf_Internal_Shdr
*hdr1
, *hdr2
;
7463 size_t symcount1
, symcount2
;
7464 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
7465 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
7466 Elf_Internal_Sym
*isym
, *isymend
;
7467 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
7468 size_t count1
, count2
, i
;
7469 unsigned int shndx1
, shndx2
;
7475 /* Both sections have to be in ELF. */
7476 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
7477 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
7480 if (elf_section_type (sec1
) != elf_section_type (sec2
))
7483 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
7484 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
7485 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
7488 bed1
= get_elf_backend_data (bfd1
);
7489 bed2
= get_elf_backend_data (bfd2
);
7490 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
7491 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
7492 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
7493 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
7495 if (symcount1
== 0 || symcount2
== 0)
7501 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
7502 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
7504 if (ssymbuf1
== NULL
)
7506 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
7508 if (isymbuf1
== NULL
)
7511 if (!info
->reduce_memory_overheads
)
7512 elf_tdata (bfd1
)->symbuf
= ssymbuf1
7513 = elf_create_symbuf (symcount1
, isymbuf1
);
7516 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
7518 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
7520 if (isymbuf2
== NULL
)
7523 if (ssymbuf1
!= NULL
&& !info
->reduce_memory_overheads
)
7524 elf_tdata (bfd2
)->symbuf
= ssymbuf2
7525 = elf_create_symbuf (symcount2
, isymbuf2
);
7528 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
7530 /* Optimized faster version. */
7532 struct elf_symbol
*symp
;
7533 struct elf_symbuf_symbol
*ssym
, *ssymend
;
7536 hi
= ssymbuf1
->count
;
7541 mid
= (lo
+ hi
) / 2;
7542 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
7544 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
7548 count1
= ssymbuf1
[mid
].count
;
7555 hi
= ssymbuf2
->count
;
7560 mid
= (lo
+ hi
) / 2;
7561 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
7563 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
7567 count2
= ssymbuf2
[mid
].count
;
7573 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7577 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
7579 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
7580 if (symtable1
== NULL
|| symtable2
== NULL
)
7584 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
7585 ssym
< ssymend
; ssym
++, symp
++)
7587 symp
->u
.ssym
= ssym
;
7588 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
7594 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
7595 ssym
< ssymend
; ssym
++, symp
++)
7597 symp
->u
.ssym
= ssym
;
7598 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
7603 /* Sort symbol by name. */
7604 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7605 elf_sym_name_compare
);
7606 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7607 elf_sym_name_compare
);
7609 for (i
= 0; i
< count1
; i
++)
7610 /* Two symbols must have the same binding, type and name. */
7611 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
7612 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
7613 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7620 symtable1
= (struct elf_symbol
*)
7621 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
7622 symtable2
= (struct elf_symbol
*)
7623 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
7624 if (symtable1
== NULL
|| symtable2
== NULL
)
7627 /* Count definitions in the section. */
7629 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
7630 if (isym
->st_shndx
== shndx1
)
7631 symtable1
[count1
++].u
.isym
= isym
;
7634 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
7635 if (isym
->st_shndx
== shndx2
)
7636 symtable2
[count2
++].u
.isym
= isym
;
7638 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7641 for (i
= 0; i
< count1
; i
++)
7643 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
7644 symtable1
[i
].u
.isym
->st_name
);
7646 for (i
= 0; i
< count2
; i
++)
7648 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
7649 symtable2
[i
].u
.isym
->st_name
);
7651 /* Sort symbol by name. */
7652 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7653 elf_sym_name_compare
);
7654 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7655 elf_sym_name_compare
);
7657 for (i
= 0; i
< count1
; i
++)
7658 /* Two symbols must have the same binding, type and name. */
7659 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
7660 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
7661 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7679 /* Return TRUE if 2 section types are compatible. */
7682 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
7683 bfd
*bbfd
, const asection
*bsec
)
7687 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
7688 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7691 return elf_section_type (asec
) == elf_section_type (bsec
);
7694 /* Final phase of ELF linker. */
7696 /* A structure we use to avoid passing large numbers of arguments. */
7698 struct elf_final_link_info
7700 /* General link information. */
7701 struct bfd_link_info
*info
;
7704 /* Symbol string table. */
7705 struct elf_strtab_hash
*symstrtab
;
7706 /* .hash section. */
7708 /* symbol version section (.gnu.version). */
7709 asection
*symver_sec
;
7710 /* Buffer large enough to hold contents of any section. */
7712 /* Buffer large enough to hold external relocs of any section. */
7713 void *external_relocs
;
7714 /* Buffer large enough to hold internal relocs of any section. */
7715 Elf_Internal_Rela
*internal_relocs
;
7716 /* Buffer large enough to hold external local symbols of any input
7718 bfd_byte
*external_syms
;
7719 /* And a buffer for symbol section indices. */
7720 Elf_External_Sym_Shndx
*locsym_shndx
;
7721 /* Buffer large enough to hold internal local symbols of any input
7723 Elf_Internal_Sym
*internal_syms
;
7724 /* Array large enough to hold a symbol index for each local symbol
7725 of any input BFD. */
7727 /* Array large enough to hold a section pointer for each local
7728 symbol of any input BFD. */
7729 asection
**sections
;
7730 /* Buffer for SHT_SYMTAB_SHNDX section. */
7731 Elf_External_Sym_Shndx
*symshndxbuf
;
7732 /* Number of STT_FILE syms seen. */
7733 size_t filesym_count
;
7736 /* This struct is used to pass information to elf_link_output_extsym. */
7738 struct elf_outext_info
7741 bfd_boolean localsyms
;
7742 bfd_boolean file_sym_done
;
7743 struct elf_final_link_info
*flinfo
;
7747 /* Support for evaluating a complex relocation.
7749 Complex relocations are generalized, self-describing relocations. The
7750 implementation of them consists of two parts: complex symbols, and the
7751 relocations themselves.
7753 The relocations are use a reserved elf-wide relocation type code (R_RELC
7754 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7755 information (start bit, end bit, word width, etc) into the addend. This
7756 information is extracted from CGEN-generated operand tables within gas.
7758 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7759 internal) representing prefix-notation expressions, including but not
7760 limited to those sorts of expressions normally encoded as addends in the
7761 addend field. The symbol mangling format is:
7764 | <unary-operator> ':' <node>
7765 | <binary-operator> ':' <node> ':' <node>
7768 <literal> := 's' <digits=N> ':' <N character symbol name>
7769 | 'S' <digits=N> ':' <N character section name>
7773 <binary-operator> := as in C
7774 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7777 set_symbol_value (bfd
*bfd_with_globals
,
7778 Elf_Internal_Sym
*isymbuf
,
7783 struct elf_link_hash_entry
**sym_hashes
;
7784 struct elf_link_hash_entry
*h
;
7785 size_t extsymoff
= locsymcount
;
7787 if (symidx
< locsymcount
)
7789 Elf_Internal_Sym
*sym
;
7791 sym
= isymbuf
+ symidx
;
7792 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
7794 /* It is a local symbol: move it to the
7795 "absolute" section and give it a value. */
7796 sym
->st_shndx
= SHN_ABS
;
7797 sym
->st_value
= val
;
7800 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
7804 /* It is a global symbol: set its link type
7805 to "defined" and give it a value. */
7807 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
7808 h
= sym_hashes
[symidx
- extsymoff
];
7809 while (h
->root
.type
== bfd_link_hash_indirect
7810 || h
->root
.type
== bfd_link_hash_warning
)
7811 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7812 h
->root
.type
= bfd_link_hash_defined
;
7813 h
->root
.u
.def
.value
= val
;
7814 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
7818 resolve_symbol (const char *name
,
7820 struct elf_final_link_info
*flinfo
,
7822 Elf_Internal_Sym
*isymbuf
,
7825 Elf_Internal_Sym
*sym
;
7826 struct bfd_link_hash_entry
*global_entry
;
7827 const char *candidate
= NULL
;
7828 Elf_Internal_Shdr
*symtab_hdr
;
7831 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
7833 for (i
= 0; i
< locsymcount
; ++ i
)
7837 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
7840 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
7841 symtab_hdr
->sh_link
,
7844 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7845 name
, candidate
, (unsigned long) sym
->st_value
);
7847 if (candidate
&& strcmp (candidate
, name
) == 0)
7849 asection
*sec
= flinfo
->sections
[i
];
7851 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
7852 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
7854 printf ("Found symbol with value %8.8lx\n",
7855 (unsigned long) *result
);
7861 /* Hmm, haven't found it yet. perhaps it is a global. */
7862 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
7863 FALSE
, FALSE
, TRUE
);
7867 if (global_entry
->type
== bfd_link_hash_defined
7868 || global_entry
->type
== bfd_link_hash_defweak
)
7870 *result
= (global_entry
->u
.def
.value
7871 + global_entry
->u
.def
.section
->output_section
->vma
7872 + global_entry
->u
.def
.section
->output_offset
);
7874 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7875 global_entry
->root
.string
, (unsigned long) *result
);
7883 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7884 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7885 names like "foo.end" which is the end address of section "foo". */
7888 resolve_section (const char *name
,
7896 for (curr
= sections
; curr
; curr
= curr
->next
)
7897 if (strcmp (curr
->name
, name
) == 0)
7899 *result
= curr
->vma
;
7903 /* Hmm. still haven't found it. try pseudo-section names. */
7904 /* FIXME: This could be coded more efficiently... */
7905 for (curr
= sections
; curr
; curr
= curr
->next
)
7907 len
= strlen (curr
->name
);
7908 if (len
> strlen (name
))
7911 if (strncmp (curr
->name
, name
, len
) == 0)
7913 if (strncmp (".end", name
+ len
, 4) == 0)
7915 *result
= curr
->vma
+ curr
->size
/ bfd_octets_per_byte (abfd
);
7919 /* Insert more pseudo-section names here, if you like. */
7927 undefined_reference (const char *reftype
, const char *name
)
7929 /* xgettext:c-format */
7930 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7935 eval_symbol (bfd_vma
*result
,
7938 struct elf_final_link_info
*flinfo
,
7940 Elf_Internal_Sym
*isymbuf
,
7949 const char *sym
= *symp
;
7951 bfd_boolean symbol_is_section
= FALSE
;
7956 if (len
< 1 || len
> sizeof (symbuf
))
7958 bfd_set_error (bfd_error_invalid_operation
);
7971 *result
= strtoul (sym
, (char **) symp
, 16);
7975 symbol_is_section
= TRUE
;
7979 symlen
= strtol (sym
, (char **) symp
, 10);
7980 sym
= *symp
+ 1; /* Skip the trailing ':'. */
7982 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
7984 bfd_set_error (bfd_error_invalid_operation
);
7988 memcpy (symbuf
, sym
, symlen
);
7989 symbuf
[symlen
] = '\0';
7990 *symp
= sym
+ symlen
;
7992 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7993 the symbol as a section, or vice-versa. so we're pretty liberal in our
7994 interpretation here; section means "try section first", not "must be a
7995 section", and likewise with symbol. */
7997 if (symbol_is_section
)
7999 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
8000 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8001 isymbuf
, locsymcount
))
8003 undefined_reference ("section", symbuf
);
8009 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8010 isymbuf
, locsymcount
)
8011 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8014 undefined_reference ("symbol", symbuf
);
8021 /* All that remains are operators. */
8023 #define UNARY_OP(op) \
8024 if (strncmp (sym, #op, strlen (#op)) == 0) \
8026 sym += strlen (#op); \
8030 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8031 isymbuf, locsymcount, signed_p)) \
8034 *result = op ((bfd_signed_vma) a); \
8040 #define BINARY_OP(op) \
8041 if (strncmp (sym, #op, strlen (#op)) == 0) \
8043 sym += strlen (#op); \
8047 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8048 isymbuf, locsymcount, signed_p)) \
8051 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8052 isymbuf, locsymcount, signed_p)) \
8055 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8085 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
8086 bfd_set_error (bfd_error_invalid_operation
);
8092 put_value (bfd_vma size
,
8093 unsigned long chunksz
,
8098 location
+= (size
- chunksz
);
8100 for (; size
; size
-= chunksz
, location
-= chunksz
)
8105 bfd_put_8 (input_bfd
, x
, location
);
8109 bfd_put_16 (input_bfd
, x
, location
);
8113 bfd_put_32 (input_bfd
, x
, location
);
8114 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8120 bfd_put_64 (input_bfd
, x
, location
);
8121 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8134 get_value (bfd_vma size
,
8135 unsigned long chunksz
,
8142 /* Sanity checks. */
8143 BFD_ASSERT (chunksz
<= sizeof (x
)
8146 && (size
% chunksz
) == 0
8147 && input_bfd
!= NULL
8148 && location
!= NULL
);
8150 if (chunksz
== sizeof (x
))
8152 BFD_ASSERT (size
== chunksz
);
8154 /* Make sure that we do not perform an undefined shift operation.
8155 We know that size == chunksz so there will only be one iteration
8156 of the loop below. */
8160 shift
= 8 * chunksz
;
8162 for (; size
; size
-= chunksz
, location
+= chunksz
)
8167 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
8170 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
8173 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
8177 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
8188 decode_complex_addend (unsigned long *start
, /* in bits */
8189 unsigned long *oplen
, /* in bits */
8190 unsigned long *len
, /* in bits */
8191 unsigned long *wordsz
, /* in bytes */
8192 unsigned long *chunksz
, /* in bytes */
8193 unsigned long *lsb0_p
,
8194 unsigned long *signed_p
,
8195 unsigned long *trunc_p
,
8196 unsigned long encoded
)
8198 * start
= encoded
& 0x3F;
8199 * len
= (encoded
>> 6) & 0x3F;
8200 * oplen
= (encoded
>> 12) & 0x3F;
8201 * wordsz
= (encoded
>> 18) & 0xF;
8202 * chunksz
= (encoded
>> 22) & 0xF;
8203 * lsb0_p
= (encoded
>> 27) & 1;
8204 * signed_p
= (encoded
>> 28) & 1;
8205 * trunc_p
= (encoded
>> 29) & 1;
8208 bfd_reloc_status_type
8209 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
8210 asection
*input_section ATTRIBUTE_UNUSED
,
8212 Elf_Internal_Rela
*rel
,
8215 bfd_vma shift
, x
, mask
;
8216 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
8217 bfd_reloc_status_type r
;
8219 /* Perform this reloc, since it is complex.
8220 (this is not to say that it necessarily refers to a complex
8221 symbol; merely that it is a self-describing CGEN based reloc.
8222 i.e. the addend has the complete reloc information (bit start, end,
8223 word size, etc) encoded within it.). */
8225 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
8226 &chunksz
, &lsb0_p
, &signed_p
,
8227 &trunc_p
, rel
->r_addend
);
8229 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
8232 shift
= (start
+ 1) - len
;
8234 shift
= (8 * wordsz
) - (start
+ len
);
8236 x
= get_value (wordsz
, chunksz
, input_bfd
,
8237 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8240 printf ("Doing complex reloc: "
8241 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8242 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8243 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8244 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
8245 oplen
, (unsigned long) x
, (unsigned long) mask
,
8246 (unsigned long) relocation
);
8251 /* Now do an overflow check. */
8252 r
= bfd_check_overflow ((signed_p
8253 ? complain_overflow_signed
8254 : complain_overflow_unsigned
),
8255 len
, 0, (8 * wordsz
),
8259 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
8262 printf (" relocation: %8.8lx\n"
8263 " shifted mask: %8.8lx\n"
8264 " shifted/masked reloc: %8.8lx\n"
8265 " result: %8.8lx\n",
8266 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
8267 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
8269 put_value (wordsz
, chunksz
, input_bfd
, x
,
8270 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8274 /* Functions to read r_offset from external (target order) reloc
8275 entry. Faster than bfd_getl32 et al, because we let the compiler
8276 know the value is aligned. */
8279 ext32l_r_offset (const void *p
)
8286 const union aligned32
*a
8287 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8289 uint32_t aval
= ( (uint32_t) a
->c
[0]
8290 | (uint32_t) a
->c
[1] << 8
8291 | (uint32_t) a
->c
[2] << 16
8292 | (uint32_t) a
->c
[3] << 24);
8297 ext32b_r_offset (const void *p
)
8304 const union aligned32
*a
8305 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8307 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
8308 | (uint32_t) a
->c
[1] << 16
8309 | (uint32_t) a
->c
[2] << 8
8310 | (uint32_t) a
->c
[3]);
8314 #ifdef BFD_HOST_64_BIT
8316 ext64l_r_offset (const void *p
)
8323 const union aligned64
*a
8324 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8326 uint64_t aval
= ( (uint64_t) a
->c
[0]
8327 | (uint64_t) a
->c
[1] << 8
8328 | (uint64_t) a
->c
[2] << 16
8329 | (uint64_t) a
->c
[3] << 24
8330 | (uint64_t) a
->c
[4] << 32
8331 | (uint64_t) a
->c
[5] << 40
8332 | (uint64_t) a
->c
[6] << 48
8333 | (uint64_t) a
->c
[7] << 56);
8338 ext64b_r_offset (const void *p
)
8345 const union aligned64
*a
8346 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8348 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
8349 | (uint64_t) a
->c
[1] << 48
8350 | (uint64_t) a
->c
[2] << 40
8351 | (uint64_t) a
->c
[3] << 32
8352 | (uint64_t) a
->c
[4] << 24
8353 | (uint64_t) a
->c
[5] << 16
8354 | (uint64_t) a
->c
[6] << 8
8355 | (uint64_t) a
->c
[7]);
8360 /* When performing a relocatable link, the input relocations are
8361 preserved. But, if they reference global symbols, the indices
8362 referenced must be updated. Update all the relocations found in
8366 elf_link_adjust_relocs (bfd
*abfd
,
8368 struct bfd_elf_section_reloc_data
*reldata
,
8372 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8374 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8375 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8376 bfd_vma r_type_mask
;
8378 unsigned int count
= reldata
->count
;
8379 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
8381 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
8383 swap_in
= bed
->s
->swap_reloc_in
;
8384 swap_out
= bed
->s
->swap_reloc_out
;
8386 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
8388 swap_in
= bed
->s
->swap_reloca_in
;
8389 swap_out
= bed
->s
->swap_reloca_out
;
8394 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
8397 if (bed
->s
->arch_size
== 32)
8404 r_type_mask
= 0xffffffff;
8408 erela
= reldata
->hdr
->contents
;
8409 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
8411 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
8414 if (*rel_hash
== NULL
)
8417 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
8419 (*swap_in
) (abfd
, erela
, irela
);
8420 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
8421 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
8422 | (irela
[j
].r_info
& r_type_mask
));
8423 (*swap_out
) (abfd
, irela
, erela
);
8426 if (bed
->elf_backend_update_relocs
)
8427 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
8429 if (sort
&& count
!= 0)
8431 bfd_vma (*ext_r_off
) (const void *);
8434 bfd_byte
*base
, *end
, *p
, *loc
;
8435 bfd_byte
*buf
= NULL
;
8437 if (bed
->s
->arch_size
== 32)
8439 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8440 ext_r_off
= ext32l_r_offset
;
8441 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8442 ext_r_off
= ext32b_r_offset
;
8448 #ifdef BFD_HOST_64_BIT
8449 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8450 ext_r_off
= ext64l_r_offset
;
8451 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8452 ext_r_off
= ext64b_r_offset
;
8458 /* Must use a stable sort here. A modified insertion sort,
8459 since the relocs are mostly sorted already. */
8460 elt_size
= reldata
->hdr
->sh_entsize
;
8461 base
= reldata
->hdr
->contents
;
8462 end
= base
+ count
* elt_size
;
8463 if (elt_size
> sizeof (Elf64_External_Rela
))
8466 /* Ensure the first element is lowest. This acts as a sentinel,
8467 speeding the main loop below. */
8468 r_off
= (*ext_r_off
) (base
);
8469 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
8471 bfd_vma r_off2
= (*ext_r_off
) (p
);
8480 /* Don't just swap *base and *loc as that changes the order
8481 of the original base[0] and base[1] if they happen to
8482 have the same r_offset. */
8483 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
8484 memcpy (onebuf
, loc
, elt_size
);
8485 memmove (base
+ elt_size
, base
, loc
- base
);
8486 memcpy (base
, onebuf
, elt_size
);
8489 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
8491 /* base to p is sorted, *p is next to insert. */
8492 r_off
= (*ext_r_off
) (p
);
8493 /* Search the sorted region for location to insert. */
8495 while (r_off
< (*ext_r_off
) (loc
))
8500 /* Chances are there is a run of relocs to insert here,
8501 from one of more input files. Files are not always
8502 linked in order due to the way elf_link_input_bfd is
8503 called. See pr17666. */
8504 size_t sortlen
= p
- loc
;
8505 bfd_vma r_off2
= (*ext_r_off
) (loc
);
8506 size_t runlen
= elt_size
;
8507 size_t buf_size
= 96 * 1024;
8508 while (p
+ runlen
< end
8509 && (sortlen
<= buf_size
8510 || runlen
+ elt_size
<= buf_size
)
8511 && r_off2
> (*ext_r_off
) (p
+ runlen
))
8515 buf
= bfd_malloc (buf_size
);
8519 if (runlen
< sortlen
)
8521 memcpy (buf
, p
, runlen
);
8522 memmove (loc
+ runlen
, loc
, sortlen
);
8523 memcpy (loc
, buf
, runlen
);
8527 memcpy (buf
, loc
, sortlen
);
8528 memmove (loc
, p
, runlen
);
8529 memcpy (loc
+ runlen
, buf
, sortlen
);
8531 p
+= runlen
- elt_size
;
8534 /* Hashes are no longer valid. */
8535 free (reldata
->hashes
);
8536 reldata
->hashes
= NULL
;
8542 struct elf_link_sort_rela
8548 enum elf_reloc_type_class type
;
8549 /* We use this as an array of size int_rels_per_ext_rel. */
8550 Elf_Internal_Rela rela
[1];
8554 elf_link_sort_cmp1 (const void *A
, const void *B
)
8556 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8557 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8558 int relativea
, relativeb
;
8560 relativea
= a
->type
== reloc_class_relative
;
8561 relativeb
= b
->type
== reloc_class_relative
;
8563 if (relativea
< relativeb
)
8565 if (relativea
> relativeb
)
8567 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
8569 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
8571 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8573 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8579 elf_link_sort_cmp2 (const void *A
, const void *B
)
8581 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8582 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8584 if (a
->type
< b
->type
)
8586 if (a
->type
> b
->type
)
8588 if (a
->u
.offset
< b
->u
.offset
)
8590 if (a
->u
.offset
> b
->u
.offset
)
8592 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8594 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8600 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
8602 asection
*dynamic_relocs
;
8605 bfd_size_type count
, size
;
8606 size_t i
, ret
, sort_elt
, ext_size
;
8607 bfd_byte
*sort
, *s_non_relative
, *p
;
8608 struct elf_link_sort_rela
*sq
;
8609 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8610 int i2e
= bed
->s
->int_rels_per_ext_rel
;
8611 unsigned int opb
= bfd_octets_per_byte (abfd
);
8612 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8613 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8614 struct bfd_link_order
*lo
;
8616 bfd_boolean use_rela
;
8618 /* Find a dynamic reloc section. */
8619 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
8620 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
8621 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
8622 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8624 bfd_boolean use_rela_initialised
= FALSE
;
8626 /* This is just here to stop gcc from complaining.
8627 Its initialization checking code is not perfect. */
8630 /* Both sections are present. Examine the sizes
8631 of the indirect sections to help us choose. */
8632 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8633 if (lo
->type
== bfd_indirect_link_order
)
8635 asection
*o
= lo
->u
.indirect
.section
;
8637 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8639 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8640 /* Section size is divisible by both rel and rela sizes.
8641 It is of no help to us. */
8645 /* Section size is only divisible by rela. */
8646 if (use_rela_initialised
&& (use_rela
== FALSE
))
8648 _bfd_error_handler (_("%B: Unable to sort relocs - "
8649 "they are in more than one size"),
8651 bfd_set_error (bfd_error_invalid_operation
);
8657 use_rela_initialised
= TRUE
;
8661 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8663 /* Section size is only divisible by rel. */
8664 if (use_rela_initialised
&& (use_rela
== TRUE
))
8666 _bfd_error_handler (_("%B: Unable to sort relocs - "
8667 "they are in more than one size"),
8669 bfd_set_error (bfd_error_invalid_operation
);
8675 use_rela_initialised
= TRUE
;
8680 /* The section size is not divisible by either -
8681 something is wrong. */
8682 _bfd_error_handler (_("%B: Unable to sort relocs - "
8683 "they are of an unknown size"), abfd
);
8684 bfd_set_error (bfd_error_invalid_operation
);
8689 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8690 if (lo
->type
== bfd_indirect_link_order
)
8692 asection
*o
= lo
->u
.indirect
.section
;
8694 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8696 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8697 /* Section size is divisible by both rel and rela sizes.
8698 It is of no help to us. */
8702 /* Section size is only divisible by rela. */
8703 if (use_rela_initialised
&& (use_rela
== FALSE
))
8705 _bfd_error_handler (_("%B: Unable to sort relocs - "
8706 "they are in more than one size"),
8708 bfd_set_error (bfd_error_invalid_operation
);
8714 use_rela_initialised
= TRUE
;
8718 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8720 /* Section size is only divisible by rel. */
8721 if (use_rela_initialised
&& (use_rela
== TRUE
))
8723 _bfd_error_handler (_("%B: Unable to sort relocs - "
8724 "they are in more than one size"),
8726 bfd_set_error (bfd_error_invalid_operation
);
8732 use_rela_initialised
= TRUE
;
8737 /* The section size is not divisible by either -
8738 something is wrong. */
8739 _bfd_error_handler (_("%B: Unable to sort relocs - "
8740 "they are of an unknown size"), abfd
);
8741 bfd_set_error (bfd_error_invalid_operation
);
8746 if (! use_rela_initialised
)
8750 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
8752 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8759 dynamic_relocs
= rela_dyn
;
8760 ext_size
= bed
->s
->sizeof_rela
;
8761 swap_in
= bed
->s
->swap_reloca_in
;
8762 swap_out
= bed
->s
->swap_reloca_out
;
8766 dynamic_relocs
= rel_dyn
;
8767 ext_size
= bed
->s
->sizeof_rel
;
8768 swap_in
= bed
->s
->swap_reloc_in
;
8769 swap_out
= bed
->s
->swap_reloc_out
;
8773 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8774 if (lo
->type
== bfd_indirect_link_order
)
8775 size
+= lo
->u
.indirect
.section
->size
;
8777 if (size
!= dynamic_relocs
->size
)
8780 sort_elt
= (sizeof (struct elf_link_sort_rela
)
8781 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
8783 count
= dynamic_relocs
->size
/ ext_size
;
8786 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
8790 (*info
->callbacks
->warning
)
8791 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
8795 if (bed
->s
->arch_size
== 32)
8796 r_sym_mask
= ~(bfd_vma
) 0xff;
8798 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
8800 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8801 if (lo
->type
== bfd_indirect_link_order
)
8803 bfd_byte
*erel
, *erelend
;
8804 asection
*o
= lo
->u
.indirect
.section
;
8806 if (o
->contents
== NULL
&& o
->size
!= 0)
8808 /* This is a reloc section that is being handled as a normal
8809 section. See bfd_section_from_shdr. We can't combine
8810 relocs in this case. */
8815 erelend
= o
->contents
+ o
->size
;
8816 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
8818 while (erel
< erelend
)
8820 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8822 (*swap_in
) (abfd
, erel
, s
->rela
);
8823 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
8824 s
->u
.sym_mask
= r_sym_mask
;
8830 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
8832 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
8834 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8835 if (s
->type
!= reloc_class_relative
)
8841 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
8842 for (; i
< count
; i
++, p
+= sort_elt
)
8844 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
8845 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
8847 sp
->u
.offset
= sq
->rela
->r_offset
;
8850 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
8852 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
8853 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
8855 /* We have plt relocs in .rela.dyn. */
8856 sq
= (struct elf_link_sort_rela
*) sort
;
8857 for (i
= 0; i
< count
; i
++)
8858 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
8860 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
8862 struct bfd_link_order
**plo
;
8863 /* Put srelplt link_order last. This is so the output_offset
8864 set in the next loop is correct for DT_JMPREL. */
8865 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
8866 if ((*plo
)->type
== bfd_indirect_link_order
8867 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
8873 plo
= &(*plo
)->next
;
8876 dynamic_relocs
->map_tail
.link_order
= lo
;
8881 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8882 if (lo
->type
== bfd_indirect_link_order
)
8884 bfd_byte
*erel
, *erelend
;
8885 asection
*o
= lo
->u
.indirect
.section
;
8888 erelend
= o
->contents
+ o
->size
;
8889 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
8890 while (erel
< erelend
)
8892 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8893 (*swap_out
) (abfd
, s
->rela
, erel
);
8900 *psec
= dynamic_relocs
;
8904 /* Add a symbol to the output symbol string table. */
8907 elf_link_output_symstrtab (struct elf_final_link_info
*flinfo
,
8909 Elf_Internal_Sym
*elfsym
,
8910 asection
*input_sec
,
8911 struct elf_link_hash_entry
*h
)
8913 int (*output_symbol_hook
)
8914 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
8915 struct elf_link_hash_entry
*);
8916 struct elf_link_hash_table
*hash_table
;
8917 const struct elf_backend_data
*bed
;
8918 bfd_size_type strtabsize
;
8920 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
8922 bed
= get_elf_backend_data (flinfo
->output_bfd
);
8923 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
8924 if (output_symbol_hook
!= NULL
)
8926 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
8933 || (input_sec
->flags
& SEC_EXCLUDE
))
8934 elfsym
->st_name
= (unsigned long) -1;
8937 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8938 to get the final offset for st_name. */
8940 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
8942 if (elfsym
->st_name
== (unsigned long) -1)
8946 hash_table
= elf_hash_table (flinfo
->info
);
8947 strtabsize
= hash_table
->strtabsize
;
8948 if (strtabsize
<= hash_table
->strtabcount
)
8950 strtabsize
+= strtabsize
;
8951 hash_table
->strtabsize
= strtabsize
;
8952 strtabsize
*= sizeof (*hash_table
->strtab
);
8954 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
8956 if (hash_table
->strtab
== NULL
)
8959 hash_table
->strtab
[hash_table
->strtabcount
].sym
= *elfsym
;
8960 hash_table
->strtab
[hash_table
->strtabcount
].dest_index
8961 = hash_table
->strtabcount
;
8962 hash_table
->strtab
[hash_table
->strtabcount
].destshndx_index
8963 = flinfo
->symshndxbuf
? bfd_get_symcount (flinfo
->output_bfd
) : 0;
8965 bfd_get_symcount (flinfo
->output_bfd
) += 1;
8966 hash_table
->strtabcount
+= 1;
8971 /* Swap symbols out to the symbol table and flush the output symbols to
8975 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
8977 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
8980 const struct elf_backend_data
*bed
;
8982 Elf_Internal_Shdr
*hdr
;
8986 if (!hash_table
->strtabcount
)
8989 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
8991 bed
= get_elf_backend_data (flinfo
->output_bfd
);
8993 amt
= bed
->s
->sizeof_sym
* hash_table
->strtabcount
;
8994 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
8998 if (flinfo
->symshndxbuf
)
9000 amt
= sizeof (Elf_External_Sym_Shndx
);
9001 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
9002 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
9003 if (flinfo
->symshndxbuf
== NULL
)
9010 for (i
= 0; i
< hash_table
->strtabcount
; i
++)
9012 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
9013 if (elfsym
->sym
.st_name
== (unsigned long) -1)
9014 elfsym
->sym
.st_name
= 0;
9017 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
9018 elfsym
->sym
.st_name
);
9019 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
9020 ((bfd_byte
*) symbuf
9021 + (elfsym
->dest_index
9022 * bed
->s
->sizeof_sym
)),
9023 (flinfo
->symshndxbuf
9024 + elfsym
->destshndx_index
));
9027 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
9028 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
9029 amt
= hash_table
->strtabcount
* bed
->s
->sizeof_sym
;
9030 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
9031 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
9033 hdr
->sh_size
+= amt
;
9041 free (hash_table
->strtab
);
9042 hash_table
->strtab
= NULL
;
9047 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9050 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
9052 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
9053 && sym
->st_shndx
< SHN_LORESERVE
)
9055 /* The gABI doesn't support dynamic symbols in output sections
9058 /* xgettext:c-format */
9059 (_("%B: Too many sections: %d (>= %d)"),
9060 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
9061 bfd_set_error (bfd_error_nonrepresentable_section
);
9067 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9068 allowing an unsatisfied unversioned symbol in the DSO to match a
9069 versioned symbol that would normally require an explicit version.
9070 We also handle the case that a DSO references a hidden symbol
9071 which may be satisfied by a versioned symbol in another DSO. */
9074 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
9075 const struct elf_backend_data
*bed
,
9076 struct elf_link_hash_entry
*h
)
9079 struct elf_link_loaded_list
*loaded
;
9081 if (!is_elf_hash_table (info
->hash
))
9084 /* Check indirect symbol. */
9085 while (h
->root
.type
== bfd_link_hash_indirect
)
9086 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9088 switch (h
->root
.type
)
9094 case bfd_link_hash_undefined
:
9095 case bfd_link_hash_undefweak
:
9096 abfd
= h
->root
.u
.undef
.abfd
;
9098 || (abfd
->flags
& DYNAMIC
) == 0
9099 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
9103 case bfd_link_hash_defined
:
9104 case bfd_link_hash_defweak
:
9105 abfd
= h
->root
.u
.def
.section
->owner
;
9108 case bfd_link_hash_common
:
9109 abfd
= h
->root
.u
.c
.p
->section
->owner
;
9112 BFD_ASSERT (abfd
!= NULL
);
9114 for (loaded
= elf_hash_table (info
)->loaded
;
9116 loaded
= loaded
->next
)
9119 Elf_Internal_Shdr
*hdr
;
9123 Elf_Internal_Shdr
*versymhdr
;
9124 Elf_Internal_Sym
*isym
;
9125 Elf_Internal_Sym
*isymend
;
9126 Elf_Internal_Sym
*isymbuf
;
9127 Elf_External_Versym
*ever
;
9128 Elf_External_Versym
*extversym
;
9130 input
= loaded
->abfd
;
9132 /* We check each DSO for a possible hidden versioned definition. */
9134 || (input
->flags
& DYNAMIC
) == 0
9135 || elf_dynversym (input
) == 0)
9138 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
9140 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9141 if (elf_bad_symtab (input
))
9143 extsymcount
= symcount
;
9148 extsymcount
= symcount
- hdr
->sh_info
;
9149 extsymoff
= hdr
->sh_info
;
9152 if (extsymcount
== 0)
9155 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
9157 if (isymbuf
== NULL
)
9160 /* Read in any version definitions. */
9161 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
9162 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
9163 if (extversym
== NULL
)
9166 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
9167 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
9168 != versymhdr
->sh_size
))
9176 ever
= extversym
+ extsymoff
;
9177 isymend
= isymbuf
+ extsymcount
;
9178 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
9181 Elf_Internal_Versym iver
;
9182 unsigned short version_index
;
9184 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
9185 || isym
->st_shndx
== SHN_UNDEF
)
9188 name
= bfd_elf_string_from_elf_section (input
,
9191 if (strcmp (name
, h
->root
.root
.string
) != 0)
9194 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
9196 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
9198 && h
->forced_local
))
9200 /* If we have a non-hidden versioned sym, then it should
9201 have provided a definition for the undefined sym unless
9202 it is defined in a non-shared object and forced local.
9207 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
9208 if (version_index
== 1 || version_index
== 2)
9210 /* This is the base or first version. We can use it. */
9224 /* Convert ELF common symbol TYPE. */
9227 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
9229 /* Commom symbol can only appear in relocatable link. */
9230 if (!bfd_link_relocatable (info
))
9232 switch (info
->elf_stt_common
)
9236 case elf_stt_common
:
9239 case no_elf_stt_common
:
9246 /* Add an external symbol to the symbol table. This is called from
9247 the hash table traversal routine. When generating a shared object,
9248 we go through the symbol table twice. The first time we output
9249 anything that might have been forced to local scope in a version
9250 script. The second time we output the symbols that are still
9254 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
9256 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
9257 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
9258 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
9260 Elf_Internal_Sym sym
;
9261 asection
*input_sec
;
9262 const struct elf_backend_data
*bed
;
9267 if (h
->root
.type
== bfd_link_hash_warning
)
9269 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9270 if (h
->root
.type
== bfd_link_hash_new
)
9274 /* Decide whether to output this symbol in this pass. */
9275 if (eoinfo
->localsyms
)
9277 if (!h
->forced_local
)
9282 if (h
->forced_local
)
9286 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9288 if (h
->root
.type
== bfd_link_hash_undefined
)
9290 /* If we have an undefined symbol reference here then it must have
9291 come from a shared library that is being linked in. (Undefined
9292 references in regular files have already been handled unless
9293 they are in unreferenced sections which are removed by garbage
9295 bfd_boolean ignore_undef
= FALSE
;
9297 /* Some symbols may be special in that the fact that they're
9298 undefined can be safely ignored - let backend determine that. */
9299 if (bed
->elf_backend_ignore_undef_symbol
)
9300 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
9302 /* If we are reporting errors for this situation then do so now. */
9305 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
9306 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
9307 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
9308 (*flinfo
->info
->callbacks
->undefined_symbol
)
9309 (flinfo
->info
, h
->root
.root
.string
,
9310 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
,
9312 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
);
9314 /* Strip a global symbol defined in a discarded section. */
9319 /* We should also warn if a forced local symbol is referenced from
9320 shared libraries. */
9321 if (bfd_link_executable (flinfo
->info
)
9326 && h
->ref_dynamic_nonweak
9327 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
9331 struct elf_link_hash_entry
*hi
= h
;
9333 /* Check indirect symbol. */
9334 while (hi
->root
.type
== bfd_link_hash_indirect
)
9335 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
9337 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
9338 /* xgettext:c-format */
9339 msg
= _("%B: internal symbol `%s' in %B is referenced by DSO");
9340 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
9341 /* xgettext:c-format */
9342 msg
= _("%B: hidden symbol `%s' in %B is referenced by DSO");
9344 /* xgettext:c-format */
9345 msg
= _("%B: local symbol `%s' in %B is referenced by DSO");
9346 def_bfd
= flinfo
->output_bfd
;
9347 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
9348 def_bfd
= hi
->root
.u
.def
.section
->owner
;
9349 _bfd_error_handler (msg
, flinfo
->output_bfd
, def_bfd
,
9350 h
->root
.root
.string
);
9351 bfd_set_error (bfd_error_bad_value
);
9352 eoinfo
->failed
= TRUE
;
9356 /* We don't want to output symbols that have never been mentioned by
9357 a regular file, or that we have been told to strip. However, if
9358 h->indx is set to -2, the symbol is used by a reloc and we must
9363 else if ((h
->def_dynamic
9365 || h
->root
.type
== bfd_link_hash_new
)
9369 else if (flinfo
->info
->strip
== strip_all
)
9371 else if (flinfo
->info
->strip
== strip_some
9372 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
9373 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
9375 else if ((h
->root
.type
== bfd_link_hash_defined
9376 || h
->root
.type
== bfd_link_hash_defweak
)
9377 && ((flinfo
->info
->strip_discarded
9378 && discarded_section (h
->root
.u
.def
.section
))
9379 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
9380 && h
->root
.u
.def
.section
->owner
!= NULL
9381 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
9383 else if ((h
->root
.type
== bfd_link_hash_undefined
9384 || h
->root
.type
== bfd_link_hash_undefweak
)
9385 && h
->root
.u
.undef
.abfd
!= NULL
9386 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
9391 /* If we're stripping it, and it's not a dynamic symbol, there's
9392 nothing else to do. However, if it is a forced local symbol or
9393 an ifunc symbol we need to give the backend finish_dynamic_symbol
9394 function a chance to make it dynamic. */
9397 && type
!= STT_GNU_IFUNC
9398 && !h
->forced_local
)
9402 sym
.st_size
= h
->size
;
9403 sym
.st_other
= h
->other
;
9404 switch (h
->root
.type
)
9407 case bfd_link_hash_new
:
9408 case bfd_link_hash_warning
:
9412 case bfd_link_hash_undefined
:
9413 case bfd_link_hash_undefweak
:
9414 input_sec
= bfd_und_section_ptr
;
9415 sym
.st_shndx
= SHN_UNDEF
;
9418 case bfd_link_hash_defined
:
9419 case bfd_link_hash_defweak
:
9421 input_sec
= h
->root
.u
.def
.section
;
9422 if (input_sec
->output_section
!= NULL
)
9425 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
9426 input_sec
->output_section
);
9427 if (sym
.st_shndx
== SHN_BAD
)
9430 /* xgettext:c-format */
9431 (_("%B: could not find output section %A for input section %A"),
9432 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
9433 bfd_set_error (bfd_error_nonrepresentable_section
);
9434 eoinfo
->failed
= TRUE
;
9438 /* ELF symbols in relocatable files are section relative,
9439 but in nonrelocatable files they are virtual
9441 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
9442 if (!bfd_link_relocatable (flinfo
->info
))
9444 sym
.st_value
+= input_sec
->output_section
->vma
;
9445 if (h
->type
== STT_TLS
)
9447 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
9448 if (tls_sec
!= NULL
)
9449 sym
.st_value
-= tls_sec
->vma
;
9455 BFD_ASSERT (input_sec
->owner
== NULL
9456 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
9457 sym
.st_shndx
= SHN_UNDEF
;
9458 input_sec
= bfd_und_section_ptr
;
9463 case bfd_link_hash_common
:
9464 input_sec
= h
->root
.u
.c
.p
->section
;
9465 sym
.st_shndx
= bed
->common_section_index (input_sec
);
9466 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
9469 case bfd_link_hash_indirect
:
9470 /* These symbols are created by symbol versioning. They point
9471 to the decorated version of the name. For example, if the
9472 symbol foo@@GNU_1.2 is the default, which should be used when
9473 foo is used with no version, then we add an indirect symbol
9474 foo which points to foo@@GNU_1.2. We ignore these symbols,
9475 since the indirected symbol is already in the hash table. */
9479 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
9480 switch (h
->root
.type
)
9482 case bfd_link_hash_common
:
9483 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9485 case bfd_link_hash_defined
:
9486 case bfd_link_hash_defweak
:
9487 if (bed
->common_definition (&sym
))
9488 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9492 case bfd_link_hash_undefined
:
9493 case bfd_link_hash_undefweak
:
9499 if (h
->forced_local
)
9501 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
9502 /* Turn off visibility on local symbol. */
9503 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
9505 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9506 else if (h
->unique_global
&& h
->def_regular
)
9507 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
9508 else if (h
->root
.type
== bfd_link_hash_undefweak
9509 || h
->root
.type
== bfd_link_hash_defweak
)
9510 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
9512 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
9513 sym
.st_target_internal
= h
->target_internal
;
9515 /* Give the processor backend a chance to tweak the symbol value,
9516 and also to finish up anything that needs to be done for this
9517 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9518 forced local syms when non-shared is due to a historical quirk.
9519 STT_GNU_IFUNC symbol must go through PLT. */
9520 if ((h
->type
== STT_GNU_IFUNC
9522 && !bfd_link_relocatable (flinfo
->info
))
9523 || ((h
->dynindx
!= -1
9525 && ((bfd_link_pic (flinfo
->info
)
9526 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
9527 || h
->root
.type
!= bfd_link_hash_undefweak
))
9528 || !h
->forced_local
)
9529 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
9531 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
9532 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
9534 eoinfo
->failed
= TRUE
;
9539 /* If we are marking the symbol as undefined, and there are no
9540 non-weak references to this symbol from a regular object, then
9541 mark the symbol as weak undefined; if there are non-weak
9542 references, mark the symbol as strong. We can't do this earlier,
9543 because it might not be marked as undefined until the
9544 finish_dynamic_symbol routine gets through with it. */
9545 if (sym
.st_shndx
== SHN_UNDEF
9547 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
9548 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
9551 type
= ELF_ST_TYPE (sym
.st_info
);
9553 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9554 if (type
== STT_GNU_IFUNC
)
9557 if (h
->ref_regular_nonweak
)
9558 bindtype
= STB_GLOBAL
;
9560 bindtype
= STB_WEAK
;
9561 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
9564 /* If this is a symbol defined in a dynamic library, don't use the
9565 symbol size from the dynamic library. Relinking an executable
9566 against a new library may introduce gratuitous changes in the
9567 executable's symbols if we keep the size. */
9568 if (sym
.st_shndx
== SHN_UNDEF
9573 /* If a non-weak symbol with non-default visibility is not defined
9574 locally, it is a fatal error. */
9575 if (!bfd_link_relocatable (flinfo
->info
)
9576 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
9577 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
9578 && h
->root
.type
== bfd_link_hash_undefined
9583 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
9584 /* xgettext:c-format */
9585 msg
= _("%B: protected symbol `%s' isn't defined");
9586 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
9587 /* xgettext:c-format */
9588 msg
= _("%B: internal symbol `%s' isn't defined");
9590 /* xgettext:c-format */
9591 msg
= _("%B: hidden symbol `%s' isn't defined");
9592 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
9593 bfd_set_error (bfd_error_bad_value
);
9594 eoinfo
->failed
= TRUE
;
9598 /* If this symbol should be put in the .dynsym section, then put it
9599 there now. We already know the symbol index. We also fill in
9600 the entry in the .hash section. */
9601 if (elf_hash_table (flinfo
->info
)->dynsym
!= NULL
9603 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
)
9607 /* Since there is no version information in the dynamic string,
9608 if there is no version info in symbol version section, we will
9609 have a run-time problem if not linking executable, referenced
9610 by shared library, or not bound locally. */
9611 if (h
->verinfo
.verdef
== NULL
9612 && (!bfd_link_executable (flinfo
->info
)
9614 || !h
->def_regular
))
9616 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
9618 if (p
&& p
[1] != '\0')
9621 /* xgettext:c-format */
9622 (_("%B: No symbol version section for versioned symbol `%s'"),
9623 flinfo
->output_bfd
, h
->root
.root
.string
);
9624 eoinfo
->failed
= TRUE
;
9629 sym
.st_name
= h
->dynstr_index
;
9630 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
9631 + h
->dynindx
* bed
->s
->sizeof_sym
);
9632 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
9634 eoinfo
->failed
= TRUE
;
9637 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
9639 if (flinfo
->hash_sec
!= NULL
)
9641 size_t hash_entry_size
;
9642 bfd_byte
*bucketpos
;
9647 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
9648 bucket
= h
->u
.elf_hash_value
% bucketcount
;
9651 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
9652 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
9653 + (bucket
+ 2) * hash_entry_size
);
9654 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
9655 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
9657 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
9658 ((bfd_byte
*) flinfo
->hash_sec
->contents
9659 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
9662 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
9664 Elf_Internal_Versym iversym
;
9665 Elf_External_Versym
*eversym
;
9667 if (!h
->def_regular
)
9669 if (h
->verinfo
.verdef
== NULL
9670 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
9671 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
9672 iversym
.vs_vers
= 0;
9674 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
9678 if (h
->verinfo
.vertree
== NULL
)
9679 iversym
.vs_vers
= 1;
9681 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
9682 if (flinfo
->info
->create_default_symver
)
9686 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9688 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
9689 iversym
.vs_vers
|= VERSYM_HIDDEN
;
9691 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
9692 eversym
+= h
->dynindx
;
9693 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
9697 /* If the symbol is undefined, and we didn't output it to .dynsym,
9698 strip it from .symtab too. Obviously we can't do this for
9699 relocatable output or when needed for --emit-relocs. */
9700 else if (input_sec
== bfd_und_section_ptr
9702 && !bfd_link_relocatable (flinfo
->info
))
9704 /* Also strip others that we couldn't earlier due to dynamic symbol
9708 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
9711 /* Output a FILE symbol so that following locals are not associated
9712 with the wrong input file. We need one for forced local symbols
9713 if we've seen more than one FILE symbol or when we have exactly
9714 one FILE symbol but global symbols are present in a file other
9715 than the one with the FILE symbol. We also need one if linker
9716 defined symbols are present. In practice these conditions are
9717 always met, so just emit the FILE symbol unconditionally. */
9718 if (eoinfo
->localsyms
9719 && !eoinfo
->file_sym_done
9720 && eoinfo
->flinfo
->filesym_count
!= 0)
9722 Elf_Internal_Sym fsym
;
9724 memset (&fsym
, 0, sizeof (fsym
));
9725 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
9726 fsym
.st_shndx
= SHN_ABS
;
9727 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
9728 bfd_und_section_ptr
, NULL
))
9731 eoinfo
->file_sym_done
= TRUE
;
9734 indx
= bfd_get_symcount (flinfo
->output_bfd
);
9735 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
9739 eoinfo
->failed
= TRUE
;
9744 else if (h
->indx
== -2)
9750 /* Return TRUE if special handling is done for relocs in SEC against
9751 symbols defined in discarded sections. */
9754 elf_section_ignore_discarded_relocs (asection
*sec
)
9756 const struct elf_backend_data
*bed
;
9758 switch (sec
->sec_info_type
)
9760 case SEC_INFO_TYPE_STABS
:
9761 case SEC_INFO_TYPE_EH_FRAME
:
9762 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
9768 bed
= get_elf_backend_data (sec
->owner
);
9769 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
9770 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
9776 /* Return a mask saying how ld should treat relocations in SEC against
9777 symbols defined in discarded sections. If this function returns
9778 COMPLAIN set, ld will issue a warning message. If this function
9779 returns PRETEND set, and the discarded section was link-once and the
9780 same size as the kept link-once section, ld will pretend that the
9781 symbol was actually defined in the kept section. Otherwise ld will
9782 zero the reloc (at least that is the intent, but some cooperation by
9783 the target dependent code is needed, particularly for REL targets). */
9786 _bfd_elf_default_action_discarded (asection
*sec
)
9788 if (sec
->flags
& SEC_DEBUGGING
)
9791 if (strcmp (".eh_frame", sec
->name
) == 0)
9794 if (strcmp (".gcc_except_table", sec
->name
) == 0)
9797 return COMPLAIN
| PRETEND
;
9800 /* Find a match between a section and a member of a section group. */
9803 match_group_member (asection
*sec
, asection
*group
,
9804 struct bfd_link_info
*info
)
9806 asection
*first
= elf_next_in_group (group
);
9807 asection
*s
= first
;
9811 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
9814 s
= elf_next_in_group (s
);
9822 /* Check if the kept section of a discarded section SEC can be used
9823 to replace it. Return the replacement if it is OK. Otherwise return
9827 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
9831 kept
= sec
->kept_section
;
9834 if ((kept
->flags
& SEC_GROUP
) != 0)
9835 kept
= match_group_member (sec
, kept
, info
);
9837 && ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
9838 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
)))
9840 sec
->kept_section
= kept
;
9845 /* Link an input file into the linker output file. This function
9846 handles all the sections and relocations of the input file at once.
9847 This is so that we only have to read the local symbols once, and
9848 don't have to keep them in memory. */
9851 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
9853 int (*relocate_section
)
9854 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
9855 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
9857 Elf_Internal_Shdr
*symtab_hdr
;
9860 Elf_Internal_Sym
*isymbuf
;
9861 Elf_Internal_Sym
*isym
;
9862 Elf_Internal_Sym
*isymend
;
9864 asection
**ppsection
;
9866 const struct elf_backend_data
*bed
;
9867 struct elf_link_hash_entry
**sym_hashes
;
9868 bfd_size_type address_size
;
9869 bfd_vma r_type_mask
;
9871 bfd_boolean have_file_sym
= FALSE
;
9873 output_bfd
= flinfo
->output_bfd
;
9874 bed
= get_elf_backend_data (output_bfd
);
9875 relocate_section
= bed
->elf_backend_relocate_section
;
9877 /* If this is a dynamic object, we don't want to do anything here:
9878 we don't want the local symbols, and we don't want the section
9880 if ((input_bfd
->flags
& DYNAMIC
) != 0)
9883 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
9884 if (elf_bad_symtab (input_bfd
))
9886 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9891 locsymcount
= symtab_hdr
->sh_info
;
9892 extsymoff
= symtab_hdr
->sh_info
;
9895 /* Read the local symbols. */
9896 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9897 if (isymbuf
== NULL
&& locsymcount
!= 0)
9899 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
9900 flinfo
->internal_syms
,
9901 flinfo
->external_syms
,
9902 flinfo
->locsym_shndx
);
9903 if (isymbuf
== NULL
)
9907 /* Find local symbol sections and adjust values of symbols in
9908 SEC_MERGE sections. Write out those local symbols we know are
9909 going into the output file. */
9910 isymend
= isymbuf
+ locsymcount
;
9911 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
9913 isym
++, pindex
++, ppsection
++)
9917 Elf_Internal_Sym osym
;
9923 if (elf_bad_symtab (input_bfd
))
9925 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
9932 if (isym
->st_shndx
== SHN_UNDEF
)
9933 isec
= bfd_und_section_ptr
;
9934 else if (isym
->st_shndx
== SHN_ABS
)
9935 isec
= bfd_abs_section_ptr
;
9936 else if (isym
->st_shndx
== SHN_COMMON
)
9937 isec
= bfd_com_section_ptr
;
9940 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
9943 /* Don't attempt to output symbols with st_shnx in the
9944 reserved range other than SHN_ABS and SHN_COMMON. */
9948 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
9949 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
9951 _bfd_merged_section_offset (output_bfd
, &isec
,
9952 elf_section_data (isec
)->sec_info
,
9958 /* Don't output the first, undefined, symbol. In fact, don't
9959 output any undefined local symbol. */
9960 if (isec
== bfd_und_section_ptr
)
9963 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
9965 /* We never output section symbols. Instead, we use the
9966 section symbol of the corresponding section in the output
9971 /* If we are stripping all symbols, we don't want to output this
9973 if (flinfo
->info
->strip
== strip_all
)
9976 /* If we are discarding all local symbols, we don't want to
9977 output this one. If we are generating a relocatable output
9978 file, then some of the local symbols may be required by
9979 relocs; we output them below as we discover that they are
9981 if (flinfo
->info
->discard
== discard_all
)
9984 /* If this symbol is defined in a section which we are
9985 discarding, we don't need to keep it. */
9986 if (isym
->st_shndx
!= SHN_UNDEF
9987 && isym
->st_shndx
< SHN_LORESERVE
9988 && bfd_section_removed_from_list (output_bfd
,
9989 isec
->output_section
))
9992 /* Get the name of the symbol. */
9993 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
9998 /* See if we are discarding symbols with this name. */
9999 if ((flinfo
->info
->strip
== strip_some
10000 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
10002 || (((flinfo
->info
->discard
== discard_sec_merge
10003 && (isec
->flags
& SEC_MERGE
)
10004 && !bfd_link_relocatable (flinfo
->info
))
10005 || flinfo
->info
->discard
== discard_l
)
10006 && bfd_is_local_label_name (input_bfd
, name
)))
10009 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
10011 if (input_bfd
->lto_output
)
10012 /* -flto puts a temp file name here. This means builds
10013 are not reproducible. Discard the symbol. */
10015 have_file_sym
= TRUE
;
10016 flinfo
->filesym_count
+= 1;
10018 if (!have_file_sym
)
10020 /* In the absence of debug info, bfd_find_nearest_line uses
10021 FILE symbols to determine the source file for local
10022 function symbols. Provide a FILE symbol here if input
10023 files lack such, so that their symbols won't be
10024 associated with a previous input file. It's not the
10025 source file, but the best we can do. */
10026 have_file_sym
= TRUE
;
10027 flinfo
->filesym_count
+= 1;
10028 memset (&osym
, 0, sizeof (osym
));
10029 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10030 osym
.st_shndx
= SHN_ABS
;
10031 if (!elf_link_output_symstrtab (flinfo
,
10032 (input_bfd
->lto_output
? NULL
10033 : input_bfd
->filename
),
10034 &osym
, bfd_abs_section_ptr
,
10041 /* Adjust the section index for the output file. */
10042 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10043 isec
->output_section
);
10044 if (osym
.st_shndx
== SHN_BAD
)
10047 /* ELF symbols in relocatable files are section relative, but
10048 in executable files they are virtual addresses. Note that
10049 this code assumes that all ELF sections have an associated
10050 BFD section with a reasonable value for output_offset; below
10051 we assume that they also have a reasonable value for
10052 output_section. Any special sections must be set up to meet
10053 these requirements. */
10054 osym
.st_value
+= isec
->output_offset
;
10055 if (!bfd_link_relocatable (flinfo
->info
))
10057 osym
.st_value
+= isec
->output_section
->vma
;
10058 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
10060 /* STT_TLS symbols are relative to PT_TLS segment base. */
10061 BFD_ASSERT (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
);
10062 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
10066 indx
= bfd_get_symcount (output_bfd
);
10067 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
10074 if (bed
->s
->arch_size
== 32)
10076 r_type_mask
= 0xff;
10082 r_type_mask
= 0xffffffff;
10087 /* Relocate the contents of each section. */
10088 sym_hashes
= elf_sym_hashes (input_bfd
);
10089 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
10091 bfd_byte
*contents
;
10093 if (! o
->linker_mark
)
10095 /* This section was omitted from the link. */
10099 if (bfd_link_relocatable (flinfo
->info
)
10100 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
10102 /* Deal with the group signature symbol. */
10103 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
10104 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
10105 asection
*osec
= o
->output_section
;
10107 if (symndx
>= locsymcount
10108 || (elf_bad_symtab (input_bfd
)
10109 && flinfo
->sections
[symndx
] == NULL
))
10111 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
10112 while (h
->root
.type
== bfd_link_hash_indirect
10113 || h
->root
.type
== bfd_link_hash_warning
)
10114 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10115 /* Arrange for symbol to be output. */
10117 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
10119 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
10121 /* We'll use the output section target_index. */
10122 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10123 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
10127 if (flinfo
->indices
[symndx
] == -1)
10129 /* Otherwise output the local symbol now. */
10130 Elf_Internal_Sym sym
= isymbuf
[symndx
];
10131 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10136 name
= bfd_elf_string_from_elf_section (input_bfd
,
10137 symtab_hdr
->sh_link
,
10142 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10144 if (sym
.st_shndx
== SHN_BAD
)
10147 sym
.st_value
+= o
->output_offset
;
10149 indx
= bfd_get_symcount (output_bfd
);
10150 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
10155 flinfo
->indices
[symndx
] = indx
;
10159 elf_section_data (osec
)->this_hdr
.sh_info
10160 = flinfo
->indices
[symndx
];
10164 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
10165 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
10168 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
10170 /* Section was created by _bfd_elf_link_create_dynamic_sections
10175 /* Get the contents of the section. They have been cached by a
10176 relaxation routine. Note that o is a section in an input
10177 file, so the contents field will not have been set by any of
10178 the routines which work on output files. */
10179 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
10181 contents
= elf_section_data (o
)->this_hdr
.contents
;
10182 if (bed
->caches_rawsize
10184 && o
->rawsize
< o
->size
)
10186 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
10187 contents
= flinfo
->contents
;
10192 contents
= flinfo
->contents
;
10193 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
10197 if ((o
->flags
& SEC_RELOC
) != 0)
10199 Elf_Internal_Rela
*internal_relocs
;
10200 Elf_Internal_Rela
*rel
, *relend
;
10201 int action_discarded
;
10204 /* Get the swapped relocs. */
10206 = _bfd_elf_link_read_relocs (input_bfd
, o
, flinfo
->external_relocs
,
10207 flinfo
->internal_relocs
, FALSE
);
10208 if (internal_relocs
== NULL
10209 && o
->reloc_count
> 0)
10212 /* We need to reverse-copy input .ctors/.dtors sections if
10213 they are placed in .init_array/.finit_array for output. */
10214 if (o
->size
> address_size
10215 && ((strncmp (o
->name
, ".ctors", 6) == 0
10216 && strcmp (o
->output_section
->name
,
10217 ".init_array") == 0)
10218 || (strncmp (o
->name
, ".dtors", 6) == 0
10219 && strcmp (o
->output_section
->name
,
10220 ".fini_array") == 0))
10221 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
10223 if (o
->size
!= o
->reloc_count
* address_size
)
10226 /* xgettext:c-format */
10227 (_("error: %B: size of section %A is not "
10228 "multiple of address size"),
10230 bfd_set_error (bfd_error_on_input
);
10233 o
->flags
|= SEC_ELF_REVERSE_COPY
;
10236 action_discarded
= -1;
10237 if (!elf_section_ignore_discarded_relocs (o
))
10238 action_discarded
= (*bed
->action_discarded
) (o
);
10240 /* Run through the relocs evaluating complex reloc symbols and
10241 looking for relocs against symbols from discarded sections
10242 or section symbols from removed link-once sections.
10243 Complain about relocs against discarded sections. Zero
10244 relocs against removed link-once sections. */
10246 rel
= internal_relocs
;
10247 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10248 for ( ; rel
< relend
; rel
++)
10250 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
10251 unsigned int s_type
;
10252 asection
**ps
, *sec
;
10253 struct elf_link_hash_entry
*h
= NULL
;
10254 const char *sym_name
;
10256 if (r_symndx
== STN_UNDEF
)
10259 if (r_symndx
>= locsymcount
10260 || (elf_bad_symtab (input_bfd
)
10261 && flinfo
->sections
[r_symndx
] == NULL
))
10263 h
= sym_hashes
[r_symndx
- extsymoff
];
10265 /* Badly formatted input files can contain relocs that
10266 reference non-existant symbols. Check here so that
10267 we do not seg fault. */
10272 sprintf_vma (buffer
, rel
->r_info
);
10274 /* xgettext:c-format */
10275 (_("error: %B contains a reloc (0x%s) for section %A "
10276 "that references a non-existent global symbol"),
10277 input_bfd
, o
, buffer
);
10278 bfd_set_error (bfd_error_bad_value
);
10282 while (h
->root
.type
== bfd_link_hash_indirect
10283 || h
->root
.type
== bfd_link_hash_warning
)
10284 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10288 /* If a plugin symbol is referenced from a non-IR file,
10289 mark the symbol as undefined. Note that the
10290 linker may attach linker created dynamic sections
10291 to the plugin bfd. Symbols defined in linker
10292 created sections are not plugin symbols. */
10293 if (h
->root
.non_ir_ref
10294 && (h
->root
.type
== bfd_link_hash_defined
10295 || h
->root
.type
== bfd_link_hash_defweak
)
10296 && (h
->root
.u
.def
.section
->flags
10297 & SEC_LINKER_CREATED
) == 0
10298 && h
->root
.u
.def
.section
->owner
!= NULL
10299 && (h
->root
.u
.def
.section
->owner
->flags
10300 & BFD_PLUGIN
) != 0)
10302 h
->root
.type
= bfd_link_hash_undefined
;
10303 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
10307 if (h
->root
.type
== bfd_link_hash_defined
10308 || h
->root
.type
== bfd_link_hash_defweak
)
10309 ps
= &h
->root
.u
.def
.section
;
10311 sym_name
= h
->root
.root
.string
;
10315 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
10317 s_type
= ELF_ST_TYPE (sym
->st_info
);
10318 ps
= &flinfo
->sections
[r_symndx
];
10319 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
10323 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
10324 && !bfd_link_relocatable (flinfo
->info
))
10327 bfd_vma dot
= (rel
->r_offset
10328 + o
->output_offset
+ o
->output_section
->vma
);
10330 printf ("Encountered a complex symbol!");
10331 printf (" (input_bfd %s, section %s, reloc %ld\n",
10332 input_bfd
->filename
, o
->name
,
10333 (long) (rel
- internal_relocs
));
10334 printf (" symbol: idx %8.8lx, name %s\n",
10335 r_symndx
, sym_name
);
10336 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10337 (unsigned long) rel
->r_info
,
10338 (unsigned long) rel
->r_offset
);
10340 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
10341 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
10344 /* Symbol evaluated OK. Update to absolute value. */
10345 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
10350 if (action_discarded
!= -1 && ps
!= NULL
)
10352 /* Complain if the definition comes from a
10353 discarded section. */
10354 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
10356 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
10357 if (action_discarded
& COMPLAIN
)
10358 (*flinfo
->info
->callbacks
->einfo
)
10359 /* xgettext:c-format */
10360 (_("%X`%s' referenced in section `%A' of %B: "
10361 "defined in discarded section `%A' of %B\n"),
10362 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
10364 /* Try to do the best we can to support buggy old
10365 versions of gcc. Pretend that the symbol is
10366 really defined in the kept linkonce section.
10367 FIXME: This is quite broken. Modifying the
10368 symbol here means we will be changing all later
10369 uses of the symbol, not just in this section. */
10370 if (action_discarded
& PRETEND
)
10374 kept
= _bfd_elf_check_kept_section (sec
,
10386 /* Relocate the section by invoking a back end routine.
10388 The back end routine is responsible for adjusting the
10389 section contents as necessary, and (if using Rela relocs
10390 and generating a relocatable output file) adjusting the
10391 reloc addend as necessary.
10393 The back end routine does not have to worry about setting
10394 the reloc address or the reloc symbol index.
10396 The back end routine is given a pointer to the swapped in
10397 internal symbols, and can access the hash table entries
10398 for the external symbols via elf_sym_hashes (input_bfd).
10400 When generating relocatable output, the back end routine
10401 must handle STB_LOCAL/STT_SECTION symbols specially. The
10402 output symbol is going to be a section symbol
10403 corresponding to the output section, which will require
10404 the addend to be adjusted. */
10406 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
10407 input_bfd
, o
, contents
,
10415 || bfd_link_relocatable (flinfo
->info
)
10416 || flinfo
->info
->emitrelocations
)
10418 Elf_Internal_Rela
*irela
;
10419 Elf_Internal_Rela
*irelaend
, *irelamid
;
10420 bfd_vma last_offset
;
10421 struct elf_link_hash_entry
**rel_hash
;
10422 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
10423 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
10424 unsigned int next_erel
;
10425 bfd_boolean rela_normal
;
10426 struct bfd_elf_section_data
*esdi
, *esdo
;
10428 esdi
= elf_section_data (o
);
10429 esdo
= elf_section_data (o
->output_section
);
10430 rela_normal
= FALSE
;
10432 /* Adjust the reloc addresses and symbol indices. */
10434 irela
= internal_relocs
;
10435 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10436 rel_hash
= esdo
->rel
.hashes
+ esdo
->rel
.count
;
10437 /* We start processing the REL relocs, if any. When we reach
10438 IRELAMID in the loop, we switch to the RELA relocs. */
10440 if (esdi
->rel
.hdr
!= NULL
)
10441 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
10442 * bed
->s
->int_rels_per_ext_rel
);
10443 rel_hash_list
= rel_hash
;
10444 rela_hash_list
= NULL
;
10445 last_offset
= o
->output_offset
;
10446 if (!bfd_link_relocatable (flinfo
->info
))
10447 last_offset
+= o
->output_section
->vma
;
10448 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
10450 unsigned long r_symndx
;
10452 Elf_Internal_Sym sym
;
10454 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
10460 if (irela
== irelamid
)
10462 rel_hash
= esdo
->rela
.hashes
+ esdo
->rela
.count
;
10463 rela_hash_list
= rel_hash
;
10464 rela_normal
= bed
->rela_normal
;
10467 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
10470 if (irela
->r_offset
>= (bfd_vma
) -2)
10472 /* This is a reloc for a deleted entry or somesuch.
10473 Turn it into an R_*_NONE reloc, at the same
10474 offset as the last reloc. elf_eh_frame.c and
10475 bfd_elf_discard_info rely on reloc offsets
10477 irela
->r_offset
= last_offset
;
10479 irela
->r_addend
= 0;
10483 irela
->r_offset
+= o
->output_offset
;
10485 /* Relocs in an executable have to be virtual addresses. */
10486 if (!bfd_link_relocatable (flinfo
->info
))
10487 irela
->r_offset
+= o
->output_section
->vma
;
10489 last_offset
= irela
->r_offset
;
10491 r_symndx
= irela
->r_info
>> r_sym_shift
;
10492 if (r_symndx
== STN_UNDEF
)
10495 if (r_symndx
>= locsymcount
10496 || (elf_bad_symtab (input_bfd
)
10497 && flinfo
->sections
[r_symndx
] == NULL
))
10499 struct elf_link_hash_entry
*rh
;
10500 unsigned long indx
;
10502 /* This is a reloc against a global symbol. We
10503 have not yet output all the local symbols, so
10504 we do not know the symbol index of any global
10505 symbol. We set the rel_hash entry for this
10506 reloc to point to the global hash table entry
10507 for this symbol. The symbol index is then
10508 set at the end of bfd_elf_final_link. */
10509 indx
= r_symndx
- extsymoff
;
10510 rh
= elf_sym_hashes (input_bfd
)[indx
];
10511 while (rh
->root
.type
== bfd_link_hash_indirect
10512 || rh
->root
.type
== bfd_link_hash_warning
)
10513 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
10515 /* Setting the index to -2 tells
10516 elf_link_output_extsym that this symbol is
10517 used by a reloc. */
10518 BFD_ASSERT (rh
->indx
< 0);
10526 /* This is a reloc against a local symbol. */
10529 sym
= isymbuf
[r_symndx
];
10530 sec
= flinfo
->sections
[r_symndx
];
10531 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
10533 /* I suppose the backend ought to fill in the
10534 section of any STT_SECTION symbol against a
10535 processor specific section. */
10536 r_symndx
= STN_UNDEF
;
10537 if (bfd_is_abs_section (sec
))
10539 else if (sec
== NULL
|| sec
->owner
== NULL
)
10541 bfd_set_error (bfd_error_bad_value
);
10546 asection
*osec
= sec
->output_section
;
10548 /* If we have discarded a section, the output
10549 section will be the absolute section. In
10550 case of discarded SEC_MERGE sections, use
10551 the kept section. relocate_section should
10552 have already handled discarded linkonce
10554 if (bfd_is_abs_section (osec
)
10555 && sec
->kept_section
!= NULL
10556 && sec
->kept_section
->output_section
!= NULL
)
10558 osec
= sec
->kept_section
->output_section
;
10559 irela
->r_addend
-= osec
->vma
;
10562 if (!bfd_is_abs_section (osec
))
10564 r_symndx
= osec
->target_index
;
10565 if (r_symndx
== STN_UNDEF
)
10567 irela
->r_addend
+= osec
->vma
;
10568 osec
= _bfd_nearby_section (output_bfd
, osec
,
10570 irela
->r_addend
-= osec
->vma
;
10571 r_symndx
= osec
->target_index
;
10576 /* Adjust the addend according to where the
10577 section winds up in the output section. */
10579 irela
->r_addend
+= sec
->output_offset
;
10583 if (flinfo
->indices
[r_symndx
] == -1)
10585 unsigned long shlink
;
10590 if (flinfo
->info
->strip
== strip_all
)
10592 /* You can't do ld -r -s. */
10593 bfd_set_error (bfd_error_invalid_operation
);
10597 /* This symbol was skipped earlier, but
10598 since it is needed by a reloc, we
10599 must output it now. */
10600 shlink
= symtab_hdr
->sh_link
;
10601 name
= (bfd_elf_string_from_elf_section
10602 (input_bfd
, shlink
, sym
.st_name
));
10606 osec
= sec
->output_section
;
10608 _bfd_elf_section_from_bfd_section (output_bfd
,
10610 if (sym
.st_shndx
== SHN_BAD
)
10613 sym
.st_value
+= sec
->output_offset
;
10614 if (!bfd_link_relocatable (flinfo
->info
))
10616 sym
.st_value
+= osec
->vma
;
10617 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
10619 /* STT_TLS symbols are relative to PT_TLS
10621 BFD_ASSERT (elf_hash_table (flinfo
->info
)
10622 ->tls_sec
!= NULL
);
10623 sym
.st_value
-= (elf_hash_table (flinfo
->info
)
10628 indx
= bfd_get_symcount (output_bfd
);
10629 ret
= elf_link_output_symstrtab (flinfo
, name
,
10635 flinfo
->indices
[r_symndx
] = indx
;
10640 r_symndx
= flinfo
->indices
[r_symndx
];
10643 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
10644 | (irela
->r_info
& r_type_mask
));
10647 /* Swap out the relocs. */
10648 input_rel_hdr
= esdi
->rel
.hdr
;
10649 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
10651 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10656 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
10657 * bed
->s
->int_rels_per_ext_rel
);
10658 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
10661 input_rela_hdr
= esdi
->rela
.hdr
;
10662 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
10664 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10673 /* Write out the modified section contents. */
10674 if (bed
->elf_backend_write_section
10675 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
10678 /* Section written out. */
10680 else switch (o
->sec_info_type
)
10682 case SEC_INFO_TYPE_STABS
:
10683 if (! (_bfd_write_section_stabs
10685 &elf_hash_table (flinfo
->info
)->stab_info
,
10686 o
, &elf_section_data (o
)->sec_info
, contents
)))
10689 case SEC_INFO_TYPE_MERGE
:
10690 if (! _bfd_write_merged_section (output_bfd
, o
,
10691 elf_section_data (o
)->sec_info
))
10694 case SEC_INFO_TYPE_EH_FRAME
:
10696 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
10701 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10703 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
10711 if (! (o
->flags
& SEC_EXCLUDE
))
10713 file_ptr offset
= (file_ptr
) o
->output_offset
;
10714 bfd_size_type todo
= o
->size
;
10716 offset
*= bfd_octets_per_byte (output_bfd
);
10718 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
10720 /* Reverse-copy input section to output. */
10723 todo
-= address_size
;
10724 if (! bfd_set_section_contents (output_bfd
,
10732 offset
+= address_size
;
10736 else if (! bfd_set_section_contents (output_bfd
,
10750 /* Generate a reloc when linking an ELF file. This is a reloc
10751 requested by the linker, and does not come from any input file. This
10752 is used to build constructor and destructor tables when linking
10756 elf_reloc_link_order (bfd
*output_bfd
,
10757 struct bfd_link_info
*info
,
10758 asection
*output_section
,
10759 struct bfd_link_order
*link_order
)
10761 reloc_howto_type
*howto
;
10765 struct bfd_elf_section_reloc_data
*reldata
;
10766 struct elf_link_hash_entry
**rel_hash_ptr
;
10767 Elf_Internal_Shdr
*rel_hdr
;
10768 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
10769 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
10772 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
10774 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
10777 bfd_set_error (bfd_error_bad_value
);
10781 addend
= link_order
->u
.reloc
.p
->addend
;
10784 reldata
= &esdo
->rel
;
10785 else if (esdo
->rela
.hdr
)
10786 reldata
= &esdo
->rela
;
10793 /* Figure out the symbol index. */
10794 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
10795 if (link_order
->type
== bfd_section_reloc_link_order
)
10797 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
10798 BFD_ASSERT (indx
!= 0);
10799 *rel_hash_ptr
= NULL
;
10803 struct elf_link_hash_entry
*h
;
10805 /* Treat a reloc against a defined symbol as though it were
10806 actually against the section. */
10807 h
= ((struct elf_link_hash_entry
*)
10808 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
10809 link_order
->u
.reloc
.p
->u
.name
,
10810 FALSE
, FALSE
, TRUE
));
10812 && (h
->root
.type
== bfd_link_hash_defined
10813 || h
->root
.type
== bfd_link_hash_defweak
))
10817 section
= h
->root
.u
.def
.section
;
10818 indx
= section
->output_section
->target_index
;
10819 *rel_hash_ptr
= NULL
;
10820 /* It seems that we ought to add the symbol value to the
10821 addend here, but in practice it has already been added
10822 because it was passed to constructor_callback. */
10823 addend
+= section
->output_section
->vma
+ section
->output_offset
;
10825 else if (h
!= NULL
)
10827 /* Setting the index to -2 tells elf_link_output_extsym that
10828 this symbol is used by a reloc. */
10835 (*info
->callbacks
->unattached_reloc
)
10836 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
10841 /* If this is an inplace reloc, we must write the addend into the
10843 if (howto
->partial_inplace
&& addend
!= 0)
10845 bfd_size_type size
;
10846 bfd_reloc_status_type rstat
;
10849 const char *sym_name
;
10851 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
10852 buf
= (bfd_byte
*) bfd_zmalloc (size
);
10853 if (buf
== NULL
&& size
!= 0)
10855 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
10862 case bfd_reloc_outofrange
:
10865 case bfd_reloc_overflow
:
10866 if (link_order
->type
== bfd_section_reloc_link_order
)
10867 sym_name
= bfd_section_name (output_bfd
,
10868 link_order
->u
.reloc
.p
->u
.section
);
10870 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
10871 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
10872 howto
->name
, addend
, NULL
, NULL
,
10877 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
10879 * bfd_octets_per_byte (output_bfd
),
10886 /* The address of a reloc is relative to the section in a
10887 relocatable file, and is a virtual address in an executable
10889 offset
= link_order
->offset
;
10890 if (! bfd_link_relocatable (info
))
10891 offset
+= output_section
->vma
;
10893 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
10895 irel
[i
].r_offset
= offset
;
10896 irel
[i
].r_info
= 0;
10897 irel
[i
].r_addend
= 0;
10899 if (bed
->s
->arch_size
== 32)
10900 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
10902 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
10904 rel_hdr
= reldata
->hdr
;
10905 erel
= rel_hdr
->contents
;
10906 if (rel_hdr
->sh_type
== SHT_REL
)
10908 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
10909 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
10913 irel
[0].r_addend
= addend
;
10914 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
10915 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
10924 /* Get the output vma of the section pointed to by the sh_link field. */
10927 elf_get_linked_section_vma (struct bfd_link_order
*p
)
10929 Elf_Internal_Shdr
**elf_shdrp
;
10933 s
= p
->u
.indirect
.section
;
10934 elf_shdrp
= elf_elfsections (s
->owner
);
10935 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
10936 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
10938 The Intel C compiler generates SHT_IA_64_UNWIND with
10939 SHF_LINK_ORDER. But it doesn't set the sh_link or
10940 sh_info fields. Hence we could get the situation
10941 where elfsec is 0. */
10944 const struct elf_backend_data
*bed
10945 = get_elf_backend_data (s
->owner
);
10946 if (bed
->link_order_error_handler
)
10947 bed
->link_order_error_handler
10948 /* xgettext:c-format */
10949 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
10954 s
= elf_shdrp
[elfsec
]->bfd_section
;
10955 return s
->output_section
->vma
+ s
->output_offset
;
10960 /* Compare two sections based on the locations of the sections they are
10961 linked to. Used by elf_fixup_link_order. */
10964 compare_link_order (const void * a
, const void * b
)
10969 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
10970 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
10973 return apos
> bpos
;
10977 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10978 order as their linked sections. Returns false if this could not be done
10979 because an output section includes both ordered and unordered
10980 sections. Ideally we'd do this in the linker proper. */
10983 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
10985 int seen_linkorder
;
10988 struct bfd_link_order
*p
;
10990 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10992 struct bfd_link_order
**sections
;
10993 asection
*s
, *other_sec
, *linkorder_sec
;
10997 linkorder_sec
= NULL
;
10999 seen_linkorder
= 0;
11000 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11002 if (p
->type
== bfd_indirect_link_order
)
11004 s
= p
->u
.indirect
.section
;
11006 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
11007 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
11008 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
11009 && elfsec
< elf_numsections (sub
)
11010 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
11011 && elf_elfsections (sub
)[elfsec
]->sh_link
< elf_numsections (sub
))
11025 if (seen_other
&& seen_linkorder
)
11027 if (other_sec
&& linkorder_sec
)
11029 /* xgettext:c-format */
11030 (_("%A has both ordered [`%A' in %B] "
11031 "and unordered [`%A' in %B] sections"),
11033 linkorder_sec
->owner
, other_sec
,
11037 (_("%A has both ordered and unordered sections"), o
);
11038 bfd_set_error (bfd_error_bad_value
);
11043 if (!seen_linkorder
)
11046 sections
= (struct bfd_link_order
**)
11047 bfd_malloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
11048 if (sections
== NULL
)
11050 seen_linkorder
= 0;
11052 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11054 sections
[seen_linkorder
++] = p
;
11056 /* Sort the input sections in the order of their linked section. */
11057 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
11058 compare_link_order
);
11060 /* Change the offsets of the sections. */
11062 for (n
= 0; n
< seen_linkorder
; n
++)
11064 s
= sections
[n
]->u
.indirect
.section
;
11065 offset
&= ~(bfd_vma
) 0 << s
->alignment_power
;
11066 s
->output_offset
= offset
/ bfd_octets_per_byte (abfd
);
11067 sections
[n
]->offset
= offset
;
11068 offset
+= sections
[n
]->size
;
11075 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11076 Returns TRUE upon success, FALSE otherwise. */
11079 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
11081 bfd_boolean ret
= FALSE
;
11083 const struct elf_backend_data
*bed
;
11085 enum bfd_architecture arch
;
11087 asymbol
**sympp
= NULL
;
11091 elf_symbol_type
*osymbuf
;
11093 implib_bfd
= info
->out_implib_bfd
;
11094 bed
= get_elf_backend_data (abfd
);
11096 if (!bfd_set_format (implib_bfd
, bfd_object
))
11099 flags
= bfd_get_file_flags (abfd
);
11100 flags
&= ~HAS_RELOC
;
11101 if (!bfd_set_start_address (implib_bfd
, 0)
11102 || !bfd_set_file_flags (implib_bfd
, flags
))
11105 /* Copy architecture of output file to import library file. */
11106 arch
= bfd_get_arch (abfd
);
11107 mach
= bfd_get_mach (abfd
);
11108 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
11109 && (abfd
->target_defaulted
11110 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
11113 /* Get symbol table size. */
11114 symsize
= bfd_get_symtab_upper_bound (abfd
);
11118 /* Read in the symbol table. */
11119 sympp
= (asymbol
**) xmalloc (symsize
);
11120 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
11124 /* Allow the BFD backend to copy any private header data it
11125 understands from the output BFD to the import library BFD. */
11126 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
11129 /* Filter symbols to appear in the import library. */
11130 if (bed
->elf_backend_filter_implib_symbols
)
11131 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
11134 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
11137 bfd_set_error (bfd_error_no_symbols
);
11138 _bfd_error_handler (_("%B: no symbol found for import library"),
11144 /* Make symbols absolute. */
11145 osymbuf
= (elf_symbol_type
*) bfd_alloc2 (implib_bfd
, symcount
,
11146 sizeof (*osymbuf
));
11147 for (src_count
= 0; src_count
< symcount
; src_count
++)
11149 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
11150 sizeof (*osymbuf
));
11151 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
11152 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
11153 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
11154 osymbuf
[src_count
].internal_elf_sym
.st_value
=
11155 osymbuf
[src_count
].symbol
.value
;
11156 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
11159 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
11161 /* Allow the BFD backend to copy any private data it understands
11162 from the output BFD to the import library BFD. This is done last
11163 to permit the routine to look at the filtered symbol table. */
11164 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
11167 if (!bfd_close (implib_bfd
))
11178 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
11182 if (flinfo
->symstrtab
!= NULL
)
11183 _bfd_elf_strtab_free (flinfo
->symstrtab
);
11184 if (flinfo
->contents
!= NULL
)
11185 free (flinfo
->contents
);
11186 if (flinfo
->external_relocs
!= NULL
)
11187 free (flinfo
->external_relocs
);
11188 if (flinfo
->internal_relocs
!= NULL
)
11189 free (flinfo
->internal_relocs
);
11190 if (flinfo
->external_syms
!= NULL
)
11191 free (flinfo
->external_syms
);
11192 if (flinfo
->locsym_shndx
!= NULL
)
11193 free (flinfo
->locsym_shndx
);
11194 if (flinfo
->internal_syms
!= NULL
)
11195 free (flinfo
->internal_syms
);
11196 if (flinfo
->indices
!= NULL
)
11197 free (flinfo
->indices
);
11198 if (flinfo
->sections
!= NULL
)
11199 free (flinfo
->sections
);
11200 if (flinfo
->symshndxbuf
!= NULL
)
11201 free (flinfo
->symshndxbuf
);
11202 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
11204 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11205 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rel
.hashes
!= NULL
)
11206 free (esdo
->rel
.hashes
);
11207 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rela
.hashes
!= NULL
)
11208 free (esdo
->rela
.hashes
);
11212 /* Do the final step of an ELF link. */
11215 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
11217 bfd_boolean dynamic
;
11218 bfd_boolean emit_relocs
;
11220 struct elf_final_link_info flinfo
;
11222 struct bfd_link_order
*p
;
11224 bfd_size_type max_contents_size
;
11225 bfd_size_type max_external_reloc_size
;
11226 bfd_size_type max_internal_reloc_count
;
11227 bfd_size_type max_sym_count
;
11228 bfd_size_type max_sym_shndx_count
;
11229 Elf_Internal_Sym elfsym
;
11231 Elf_Internal_Shdr
*symtab_hdr
;
11232 Elf_Internal_Shdr
*symtab_shndx_hdr
;
11233 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11234 struct elf_outext_info eoinfo
;
11235 bfd_boolean merged
;
11236 size_t relativecount
= 0;
11237 asection
*reldyn
= 0;
11239 asection
*attr_section
= NULL
;
11240 bfd_vma attr_size
= 0;
11241 const char *std_attrs_section
;
11242 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
11244 if (!is_elf_hash_table (htab
))
11247 if (bfd_link_pic (info
))
11248 abfd
->flags
|= DYNAMIC
;
11250 dynamic
= htab
->dynamic_sections_created
;
11251 dynobj
= htab
->dynobj
;
11253 emit_relocs
= (bfd_link_relocatable (info
)
11254 || info
->emitrelocations
);
11256 flinfo
.info
= info
;
11257 flinfo
.output_bfd
= abfd
;
11258 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
11259 if (flinfo
.symstrtab
== NULL
)
11264 flinfo
.hash_sec
= NULL
;
11265 flinfo
.symver_sec
= NULL
;
11269 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
11270 /* Note that dynsym_sec can be NULL (on VMS). */
11271 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
11272 /* Note that it is OK if symver_sec is NULL. */
11275 flinfo
.contents
= NULL
;
11276 flinfo
.external_relocs
= NULL
;
11277 flinfo
.internal_relocs
= NULL
;
11278 flinfo
.external_syms
= NULL
;
11279 flinfo
.locsym_shndx
= NULL
;
11280 flinfo
.internal_syms
= NULL
;
11281 flinfo
.indices
= NULL
;
11282 flinfo
.sections
= NULL
;
11283 flinfo
.symshndxbuf
= NULL
;
11284 flinfo
.filesym_count
= 0;
11286 /* The object attributes have been merged. Remove the input
11287 sections from the link, and set the contents of the output
11289 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
11290 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11292 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
11293 || strcmp (o
->name
, ".gnu.attributes") == 0)
11295 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11297 asection
*input_section
;
11299 if (p
->type
!= bfd_indirect_link_order
)
11301 input_section
= p
->u
.indirect
.section
;
11302 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11303 elf_link_input_bfd ignores this section. */
11304 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
11307 attr_size
= bfd_elf_obj_attr_size (abfd
);
11310 bfd_set_section_size (abfd
, o
, attr_size
);
11312 /* Skip this section later on. */
11313 o
->map_head
.link_order
= NULL
;
11316 o
->flags
|= SEC_EXCLUDE
;
11320 /* Count up the number of relocations we will output for each output
11321 section, so that we know the sizes of the reloc sections. We
11322 also figure out some maximum sizes. */
11323 max_contents_size
= 0;
11324 max_external_reloc_size
= 0;
11325 max_internal_reloc_count
= 0;
11327 max_sym_shndx_count
= 0;
11329 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11331 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11332 o
->reloc_count
= 0;
11334 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11336 unsigned int reloc_count
= 0;
11337 unsigned int additional_reloc_count
= 0;
11338 struct bfd_elf_section_data
*esdi
= NULL
;
11340 if (p
->type
== bfd_section_reloc_link_order
11341 || p
->type
== bfd_symbol_reloc_link_order
)
11343 else if (p
->type
== bfd_indirect_link_order
)
11347 sec
= p
->u
.indirect
.section
;
11348 /* See PR 20908 for a reproducer. */
11349 if (bfd_get_flavour (sec
->owner
) != bfd_target_elf_flavour
)
11351 _bfd_error_handler (_("%B: not in ELF format"), sec
->owner
);
11354 esdi
= elf_section_data (sec
);
11356 /* Mark all sections which are to be included in the
11357 link. This will normally be every section. We need
11358 to do this so that we can identify any sections which
11359 the linker has decided to not include. */
11360 sec
->linker_mark
= TRUE
;
11362 if (sec
->flags
& SEC_MERGE
)
11365 if (esdo
->this_hdr
.sh_type
== SHT_REL
11366 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
11367 /* Some backends use reloc_count in relocation sections
11368 to count particular types of relocs. Of course,
11369 reloc sections themselves can't have relocations. */
11371 else if (emit_relocs
)
11373 reloc_count
= sec
->reloc_count
;
11374 if (bed
->elf_backend_count_additional_relocs
)
11377 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
11378 additional_reloc_count
+= c
;
11381 else if (bed
->elf_backend_count_relocs
)
11382 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
11384 if (sec
->rawsize
> max_contents_size
)
11385 max_contents_size
= sec
->rawsize
;
11386 if (sec
->size
> max_contents_size
)
11387 max_contents_size
= sec
->size
;
11389 /* We are interested in just local symbols, not all
11391 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
11392 && (sec
->owner
->flags
& DYNAMIC
) == 0)
11396 if (elf_bad_symtab (sec
->owner
))
11397 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
11398 / bed
->s
->sizeof_sym
);
11400 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
11402 if (sym_count
> max_sym_count
)
11403 max_sym_count
= sym_count
;
11405 if (sym_count
> max_sym_shndx_count
11406 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
11407 max_sym_shndx_count
= sym_count
;
11409 if ((sec
->flags
& SEC_RELOC
) != 0)
11411 size_t ext_size
= 0;
11413 if (esdi
->rel
.hdr
!= NULL
)
11414 ext_size
= esdi
->rel
.hdr
->sh_size
;
11415 if (esdi
->rela
.hdr
!= NULL
)
11416 ext_size
+= esdi
->rela
.hdr
->sh_size
;
11418 if (ext_size
> max_external_reloc_size
)
11419 max_external_reloc_size
= ext_size
;
11420 if (sec
->reloc_count
> max_internal_reloc_count
)
11421 max_internal_reloc_count
= sec
->reloc_count
;
11426 if (reloc_count
== 0)
11429 reloc_count
+= additional_reloc_count
;
11430 o
->reloc_count
+= reloc_count
;
11432 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
11436 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
11437 esdo
->rel
.count
+= additional_reloc_count
;
11439 if (esdi
->rela
.hdr
)
11441 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
11442 esdo
->rela
.count
+= additional_reloc_count
;
11448 esdo
->rela
.count
+= reloc_count
;
11450 esdo
->rel
.count
+= reloc_count
;
11454 if (o
->reloc_count
> 0)
11455 o
->flags
|= SEC_RELOC
;
11458 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11459 set it (this is probably a bug) and if it is set
11460 assign_section_numbers will create a reloc section. */
11461 o
->flags
&=~ SEC_RELOC
;
11464 /* If the SEC_ALLOC flag is not set, force the section VMA to
11465 zero. This is done in elf_fake_sections as well, but forcing
11466 the VMA to 0 here will ensure that relocs against these
11467 sections are handled correctly. */
11468 if ((o
->flags
& SEC_ALLOC
) == 0
11469 && ! o
->user_set_vma
)
11473 if (! bfd_link_relocatable (info
) && merged
)
11474 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
11476 /* Figure out the file positions for everything but the symbol table
11477 and the relocs. We set symcount to force assign_section_numbers
11478 to create a symbol table. */
11479 bfd_get_symcount (abfd
) = info
->strip
!= strip_all
|| emit_relocs
;
11480 BFD_ASSERT (! abfd
->output_has_begun
);
11481 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
11484 /* Set sizes, and assign file positions for reloc sections. */
11485 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11487 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11488 if ((o
->flags
& SEC_RELOC
) != 0)
11491 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
11495 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
11499 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11500 to count upwards while actually outputting the relocations. */
11501 esdo
->rel
.count
= 0;
11502 esdo
->rela
.count
= 0;
11504 if (esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
11506 /* Cache the section contents so that they can be compressed
11507 later. Use bfd_malloc since it will be freed by
11508 bfd_compress_section_contents. */
11509 unsigned char *contents
= esdo
->this_hdr
.contents
;
11510 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
11513 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
11514 if (contents
== NULL
)
11516 esdo
->this_hdr
.contents
= contents
;
11520 /* We have now assigned file positions for all the sections except
11521 .symtab, .strtab, and non-loaded reloc sections. We start the
11522 .symtab section at the current file position, and write directly
11523 to it. We build the .strtab section in memory. */
11524 bfd_get_symcount (abfd
) = 0;
11525 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11526 /* sh_name is set in prep_headers. */
11527 symtab_hdr
->sh_type
= SHT_SYMTAB
;
11528 /* sh_flags, sh_addr and sh_size all start off zero. */
11529 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
11530 /* sh_link is set in assign_section_numbers. */
11531 /* sh_info is set below. */
11532 /* sh_offset is set just below. */
11533 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
11535 if (max_sym_count
< 20)
11536 max_sym_count
= 20;
11537 htab
->strtabsize
= max_sym_count
;
11538 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
11539 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
11540 if (htab
->strtab
== NULL
)
11542 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11544 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
11545 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
11547 if (info
->strip
!= strip_all
|| emit_relocs
)
11549 file_ptr off
= elf_next_file_pos (abfd
);
11551 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
11553 /* Note that at this point elf_next_file_pos (abfd) is
11554 incorrect. We do not yet know the size of the .symtab section.
11555 We correct next_file_pos below, after we do know the size. */
11557 /* Start writing out the symbol table. The first symbol is always a
11559 elfsym
.st_value
= 0;
11560 elfsym
.st_size
= 0;
11561 elfsym
.st_info
= 0;
11562 elfsym
.st_other
= 0;
11563 elfsym
.st_shndx
= SHN_UNDEF
;
11564 elfsym
.st_target_internal
= 0;
11565 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
11566 bfd_und_section_ptr
, NULL
) != 1)
11569 /* Output a symbol for each section. We output these even if we are
11570 discarding local symbols, since they are used for relocs. These
11571 symbols have no names. We store the index of each one in the
11572 index field of the section, so that we can find it again when
11573 outputting relocs. */
11575 elfsym
.st_size
= 0;
11576 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
11577 elfsym
.st_other
= 0;
11578 elfsym
.st_value
= 0;
11579 elfsym
.st_target_internal
= 0;
11580 for (i
= 1; i
< elf_numsections (abfd
); i
++)
11582 o
= bfd_section_from_elf_index (abfd
, i
);
11585 o
->target_index
= bfd_get_symcount (abfd
);
11586 elfsym
.st_shndx
= i
;
11587 if (!bfd_link_relocatable (info
))
11588 elfsym
.st_value
= o
->vma
;
11589 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
, o
,
11596 /* Allocate some memory to hold information read in from the input
11598 if (max_contents_size
!= 0)
11600 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
11601 if (flinfo
.contents
== NULL
)
11605 if (max_external_reloc_size
!= 0)
11607 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
11608 if (flinfo
.external_relocs
== NULL
)
11612 if (max_internal_reloc_count
!= 0)
11614 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
11615 amt
*= sizeof (Elf_Internal_Rela
);
11616 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
11617 if (flinfo
.internal_relocs
== NULL
)
11621 if (max_sym_count
!= 0)
11623 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
11624 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
11625 if (flinfo
.external_syms
== NULL
)
11628 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
11629 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
11630 if (flinfo
.internal_syms
== NULL
)
11633 amt
= max_sym_count
* sizeof (long);
11634 flinfo
.indices
= (long int *) bfd_malloc (amt
);
11635 if (flinfo
.indices
== NULL
)
11638 amt
= max_sym_count
* sizeof (asection
*);
11639 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
11640 if (flinfo
.sections
== NULL
)
11644 if (max_sym_shndx_count
!= 0)
11646 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
11647 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
11648 if (flinfo
.locsym_shndx
== NULL
)
11654 bfd_vma base
, end
= 0;
11657 for (sec
= htab
->tls_sec
;
11658 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
11661 bfd_size_type size
= sec
->size
;
11664 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
11666 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
11669 size
= ord
->offset
+ ord
->size
;
11671 end
= sec
->vma
+ size
;
11673 base
= htab
->tls_sec
->vma
;
11674 /* Only align end of TLS section if static TLS doesn't have special
11675 alignment requirements. */
11676 if (bed
->static_tls_alignment
== 1)
11677 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
11678 htab
->tls_size
= end
- base
;
11681 /* Reorder SHF_LINK_ORDER sections. */
11682 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11684 if (!elf_fixup_link_order (abfd
, o
))
11688 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
11691 /* Since ELF permits relocations to be against local symbols, we
11692 must have the local symbols available when we do the relocations.
11693 Since we would rather only read the local symbols once, and we
11694 would rather not keep them in memory, we handle all the
11695 relocations for a single input file at the same time.
11697 Unfortunately, there is no way to know the total number of local
11698 symbols until we have seen all of them, and the local symbol
11699 indices precede the global symbol indices. This means that when
11700 we are generating relocatable output, and we see a reloc against
11701 a global symbol, we can not know the symbol index until we have
11702 finished examining all the local symbols to see which ones we are
11703 going to output. To deal with this, we keep the relocations in
11704 memory, and don't output them until the end of the link. This is
11705 an unfortunate waste of memory, but I don't see a good way around
11706 it. Fortunately, it only happens when performing a relocatable
11707 link, which is not the common case. FIXME: If keep_memory is set
11708 we could write the relocs out and then read them again; I don't
11709 know how bad the memory loss will be. */
11711 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11712 sub
->output_has_begun
= FALSE
;
11713 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11715 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11717 if (p
->type
== bfd_indirect_link_order
11718 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
11719 == bfd_target_elf_flavour
)
11720 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
11722 if (! sub
->output_has_begun
)
11724 if (! elf_link_input_bfd (&flinfo
, sub
))
11726 sub
->output_has_begun
= TRUE
;
11729 else if (p
->type
== bfd_section_reloc_link_order
11730 || p
->type
== bfd_symbol_reloc_link_order
)
11732 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
11737 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
11739 if (p
->type
== bfd_indirect_link_order
11740 && (bfd_get_flavour (sub
)
11741 == bfd_target_elf_flavour
)
11742 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
11743 != bed
->s
->elfclass
))
11745 const char *iclass
, *oclass
;
11747 switch (bed
->s
->elfclass
)
11749 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
11750 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
11751 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
11755 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
11757 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
11758 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
11759 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
11763 bfd_set_error (bfd_error_wrong_format
);
11765 /* xgettext:c-format */
11766 (_("%B: file class %s incompatible with %s"),
11767 sub
, iclass
, oclass
);
11776 /* Free symbol buffer if needed. */
11777 if (!info
->reduce_memory_overheads
)
11779 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11780 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
11781 && elf_tdata (sub
)->symbuf
)
11783 free (elf_tdata (sub
)->symbuf
);
11784 elf_tdata (sub
)->symbuf
= NULL
;
11788 /* Output any global symbols that got converted to local in a
11789 version script or due to symbol visibility. We do this in a
11790 separate step since ELF requires all local symbols to appear
11791 prior to any global symbols. FIXME: We should only do this if
11792 some global symbols were, in fact, converted to become local.
11793 FIXME: Will this work correctly with the Irix 5 linker? */
11794 eoinfo
.failed
= FALSE
;
11795 eoinfo
.flinfo
= &flinfo
;
11796 eoinfo
.localsyms
= TRUE
;
11797 eoinfo
.file_sym_done
= FALSE
;
11798 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
11802 /* If backend needs to output some local symbols not present in the hash
11803 table, do it now. */
11804 if (bed
->elf_backend_output_arch_local_syms
11805 && (info
->strip
!= strip_all
|| emit_relocs
))
11807 typedef int (*out_sym_func
)
11808 (void *, const char *, Elf_Internal_Sym
*, asection
*,
11809 struct elf_link_hash_entry
*);
11811 if (! ((*bed
->elf_backend_output_arch_local_syms
)
11812 (abfd
, info
, &flinfo
,
11813 (out_sym_func
) elf_link_output_symstrtab
)))
11817 /* That wrote out all the local symbols. Finish up the symbol table
11818 with the global symbols. Even if we want to strip everything we
11819 can, we still need to deal with those global symbols that got
11820 converted to local in a version script. */
11822 /* The sh_info field records the index of the first non local symbol. */
11823 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
11826 && htab
->dynsym
!= NULL
11827 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
11829 Elf_Internal_Sym sym
;
11830 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
11832 o
= htab
->dynsym
->output_section
;
11833 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
11835 /* Write out the section symbols for the output sections. */
11836 if (bfd_link_pic (info
)
11837 || htab
->is_relocatable_executable
)
11843 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
11845 sym
.st_target_internal
= 0;
11847 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
11853 dynindx
= elf_section_data (s
)->dynindx
;
11856 indx
= elf_section_data (s
)->this_idx
;
11857 BFD_ASSERT (indx
> 0);
11858 sym
.st_shndx
= indx
;
11859 if (! check_dynsym (abfd
, &sym
))
11861 sym
.st_value
= s
->vma
;
11862 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
11863 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
11867 /* Write out the local dynsyms. */
11868 if (htab
->dynlocal
)
11870 struct elf_link_local_dynamic_entry
*e
;
11871 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
11876 /* Copy the internal symbol and turn off visibility.
11877 Note that we saved a word of storage and overwrote
11878 the original st_name with the dynstr_index. */
11880 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
11882 s
= bfd_section_from_elf_index (e
->input_bfd
,
11887 elf_section_data (s
->output_section
)->this_idx
;
11888 if (! check_dynsym (abfd
, &sym
))
11890 sym
.st_value
= (s
->output_section
->vma
11892 + e
->isym
.st_value
);
11895 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
11896 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
11901 /* We get the global symbols from the hash table. */
11902 eoinfo
.failed
= FALSE
;
11903 eoinfo
.localsyms
= FALSE
;
11904 eoinfo
.flinfo
= &flinfo
;
11905 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
11909 /* If backend needs to output some symbols not present in the hash
11910 table, do it now. */
11911 if (bed
->elf_backend_output_arch_syms
11912 && (info
->strip
!= strip_all
|| emit_relocs
))
11914 typedef int (*out_sym_func
)
11915 (void *, const char *, Elf_Internal_Sym
*, asection
*,
11916 struct elf_link_hash_entry
*);
11918 if (! ((*bed
->elf_backend_output_arch_syms
)
11919 (abfd
, info
, &flinfo
,
11920 (out_sym_func
) elf_link_output_symstrtab
)))
11924 /* Finalize the .strtab section. */
11925 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
11927 /* Swap out the .strtab section. */
11928 if (!elf_link_swap_symbols_out (&flinfo
))
11931 /* Now we know the size of the symtab section. */
11932 if (bfd_get_symcount (abfd
) > 0)
11934 /* Finish up and write out the symbol string table (.strtab)
11936 Elf_Internal_Shdr
*symstrtab_hdr
;
11937 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
11939 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
11940 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
11942 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
11943 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
11944 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
11945 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
11946 symtab_shndx_hdr
->sh_size
= amt
;
11948 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
11951 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
11952 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
11956 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
11957 /* sh_name was set in prep_headers. */
11958 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
11959 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
11960 symstrtab_hdr
->sh_addr
= 0;
11961 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
11962 symstrtab_hdr
->sh_entsize
= 0;
11963 symstrtab_hdr
->sh_link
= 0;
11964 symstrtab_hdr
->sh_info
= 0;
11965 /* sh_offset is set just below. */
11966 symstrtab_hdr
->sh_addralign
= 1;
11968 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
11970 elf_next_file_pos (abfd
) = off
;
11972 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
11973 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
11977 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
11979 _bfd_error_handler (_("%B: failed to generate import library"),
11980 info
->out_implib_bfd
);
11984 /* Adjust the relocs to have the correct symbol indices. */
11985 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11987 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11989 if ((o
->flags
& SEC_RELOC
) == 0)
11992 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
11993 if (esdo
->rel
.hdr
!= NULL
11994 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
))
11996 if (esdo
->rela
.hdr
!= NULL
11997 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
))
12000 /* Set the reloc_count field to 0 to prevent write_relocs from
12001 trying to swap the relocs out itself. */
12002 o
->reloc_count
= 0;
12005 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
12006 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
12008 /* If we are linking against a dynamic object, or generating a
12009 shared library, finish up the dynamic linking information. */
12012 bfd_byte
*dyncon
, *dynconend
;
12014 /* Fix up .dynamic entries. */
12015 o
= bfd_get_linker_section (dynobj
, ".dynamic");
12016 BFD_ASSERT (o
!= NULL
);
12018 dyncon
= o
->contents
;
12019 dynconend
= o
->contents
+ o
->size
;
12020 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12022 Elf_Internal_Dyn dyn
;
12026 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12033 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
12035 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
12037 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
12038 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
12041 dyn
.d_un
.d_val
= relativecount
;
12048 name
= info
->init_function
;
12051 name
= info
->fini_function
;
12054 struct elf_link_hash_entry
*h
;
12056 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
12058 && (h
->root
.type
== bfd_link_hash_defined
12059 || h
->root
.type
== bfd_link_hash_defweak
))
12061 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
12062 o
= h
->root
.u
.def
.section
;
12063 if (o
->output_section
!= NULL
)
12064 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
12065 + o
->output_offset
);
12068 /* The symbol is imported from another shared
12069 library and does not apply to this one. */
12070 dyn
.d_un
.d_ptr
= 0;
12077 case DT_PREINIT_ARRAYSZ
:
12078 name
= ".preinit_array";
12080 case DT_INIT_ARRAYSZ
:
12081 name
= ".init_array";
12083 case DT_FINI_ARRAYSZ
:
12084 name
= ".fini_array";
12086 o
= bfd_get_section_by_name (abfd
, name
);
12090 (_("could not find section %s"), name
);
12095 (_("warning: %s section has zero size"), name
);
12096 dyn
.d_un
.d_val
= o
->size
;
12099 case DT_PREINIT_ARRAY
:
12100 name
= ".preinit_array";
12102 case DT_INIT_ARRAY
:
12103 name
= ".init_array";
12105 case DT_FINI_ARRAY
:
12106 name
= ".fini_array";
12108 o
= bfd_get_section_by_name (abfd
, name
);
12115 name
= ".gnu.hash";
12124 name
= ".gnu.version_d";
12127 name
= ".gnu.version_r";
12130 name
= ".gnu.version";
12132 o
= bfd_get_linker_section (dynobj
, name
);
12137 (_("could not find section %s"), name
);
12140 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
12143 (_("warning: section '%s' is being made into a note"), name
);
12144 bfd_set_error (bfd_error_nonrepresentable_section
);
12147 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
12154 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
12158 dyn
.d_un
.d_val
= 0;
12159 dyn
.d_un
.d_ptr
= 0;
12160 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12162 Elf_Internal_Shdr
*hdr
;
12164 hdr
= elf_elfsections (abfd
)[i
];
12165 if (hdr
->sh_type
== type
12166 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
12168 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
12169 dyn
.d_un
.d_val
+= hdr
->sh_size
;
12172 if (dyn
.d_un
.d_ptr
== 0
12173 || hdr
->sh_addr
< dyn
.d_un
.d_ptr
)
12174 dyn
.d_un
.d_ptr
= hdr
->sh_addr
;
12178 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
12180 /* Don't count procedure linkage table relocs in the
12181 overall reloc count. */
12182 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
12183 dyn
.d_un
.d_val
-= htab
->srelplt
->size
;
12184 /* If .rela.plt is the first .rela section, exclude
12185 it from DT_RELA. */
12186 else if (dyn
.d_un
.d_ptr
== (htab
->srelplt
->output_section
->vma
12187 + htab
->srelplt
->output_offset
))
12188 dyn
.d_un
.d_ptr
+= htab
->srelplt
->size
;
12192 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
12196 /* If we have created any dynamic sections, then output them. */
12197 if (dynobj
!= NULL
)
12199 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
12202 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12203 if (((info
->warn_shared_textrel
&& bfd_link_pic (info
))
12204 || info
->error_textrel
)
12205 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
)
12207 bfd_byte
*dyncon
, *dynconend
;
12209 dyncon
= o
->contents
;
12210 dynconend
= o
->contents
+ o
->size
;
12211 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12213 Elf_Internal_Dyn dyn
;
12215 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12217 if (dyn
.d_tag
== DT_TEXTREL
)
12219 if (info
->error_textrel
)
12220 info
->callbacks
->einfo
12221 (_("%P%X: read-only segment has dynamic relocations.\n"));
12223 info
->callbacks
->einfo
12224 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12230 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
12232 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
12234 || o
->output_section
== bfd_abs_section_ptr
)
12236 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
12238 /* At this point, we are only interested in sections
12239 created by _bfd_elf_link_create_dynamic_sections. */
12242 if (htab
->stab_info
.stabstr
== o
)
12244 if (htab
->eh_info
.hdr_sec
== o
)
12246 if (strcmp (o
->name
, ".dynstr") != 0)
12248 if (! bfd_set_section_contents (abfd
, o
->output_section
,
12250 (file_ptr
) o
->output_offset
12251 * bfd_octets_per_byte (abfd
),
12257 /* The contents of the .dynstr section are actually in a
12261 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
12262 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
12263 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
12269 if (bfd_link_relocatable (info
))
12271 bfd_boolean failed
= FALSE
;
12273 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
12278 /* If we have optimized stabs strings, output them. */
12279 if (htab
->stab_info
.stabstr
!= NULL
)
12281 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
12285 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
12288 elf_final_link_free (abfd
, &flinfo
);
12290 elf_linker (abfd
) = TRUE
;
12294 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
12295 if (contents
== NULL
)
12296 return FALSE
; /* Bail out and fail. */
12297 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
12298 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
12305 elf_final_link_free (abfd
, &flinfo
);
12309 /* Initialize COOKIE for input bfd ABFD. */
12312 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
12313 struct bfd_link_info
*info
, bfd
*abfd
)
12315 Elf_Internal_Shdr
*symtab_hdr
;
12316 const struct elf_backend_data
*bed
;
12318 bed
= get_elf_backend_data (abfd
);
12319 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12321 cookie
->abfd
= abfd
;
12322 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
12323 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
12324 if (cookie
->bad_symtab
)
12326 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
12327 cookie
->extsymoff
= 0;
12331 cookie
->locsymcount
= symtab_hdr
->sh_info
;
12332 cookie
->extsymoff
= symtab_hdr
->sh_info
;
12335 if (bed
->s
->arch_size
== 32)
12336 cookie
->r_sym_shift
= 8;
12338 cookie
->r_sym_shift
= 32;
12340 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
12341 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
12343 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
12344 cookie
->locsymcount
, 0,
12346 if (cookie
->locsyms
== NULL
)
12348 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
12351 if (info
->keep_memory
)
12352 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
12357 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12360 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
12362 Elf_Internal_Shdr
*symtab_hdr
;
12364 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12365 if (cookie
->locsyms
!= NULL
12366 && symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
12367 free (cookie
->locsyms
);
12370 /* Initialize the relocation information in COOKIE for input section SEC
12371 of input bfd ABFD. */
12374 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12375 struct bfd_link_info
*info
, bfd
*abfd
,
12378 const struct elf_backend_data
*bed
;
12380 if (sec
->reloc_count
== 0)
12382 cookie
->rels
= NULL
;
12383 cookie
->relend
= NULL
;
12387 bed
= get_elf_backend_data (abfd
);
12389 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
12390 info
->keep_memory
);
12391 if (cookie
->rels
== NULL
)
12393 cookie
->rel
= cookie
->rels
;
12394 cookie
->relend
= (cookie
->rels
12395 + sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
);
12397 cookie
->rel
= cookie
->rels
;
12401 /* Free the memory allocated by init_reloc_cookie_rels,
12405 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12408 if (cookie
->rels
&& elf_section_data (sec
)->relocs
!= cookie
->rels
)
12409 free (cookie
->rels
);
12412 /* Initialize the whole of COOKIE for input section SEC. */
12415 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12416 struct bfd_link_info
*info
,
12419 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
12421 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
12426 fini_reloc_cookie (cookie
, sec
->owner
);
12431 /* Free the memory allocated by init_reloc_cookie_for_section,
12435 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12438 fini_reloc_cookie_rels (cookie
, sec
);
12439 fini_reloc_cookie (cookie
, sec
->owner
);
12442 /* Garbage collect unused sections. */
12444 /* Default gc_mark_hook. */
12447 _bfd_elf_gc_mark_hook (asection
*sec
,
12448 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12449 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
12450 struct elf_link_hash_entry
*h
,
12451 Elf_Internal_Sym
*sym
)
12455 switch (h
->root
.type
)
12457 case bfd_link_hash_defined
:
12458 case bfd_link_hash_defweak
:
12459 return h
->root
.u
.def
.section
;
12461 case bfd_link_hash_common
:
12462 return h
->root
.u
.c
.p
->section
;
12469 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
12474 /* For undefined __start_<name> and __stop_<name> symbols, return the
12475 first input section matching <name>. Return NULL otherwise. */
12478 _bfd_elf_is_start_stop (const struct bfd_link_info
*info
,
12479 struct elf_link_hash_entry
*h
)
12482 const char *sec_name
;
12484 if (h
->root
.type
!= bfd_link_hash_undefined
12485 && h
->root
.type
!= bfd_link_hash_undefweak
)
12488 s
= h
->root
.u
.undef
.section
;
12491 if (s
== (asection
*) 0 - 1)
12497 if (strncmp (h
->root
.root
.string
, "__start_", 8) == 0)
12498 sec_name
= h
->root
.root
.string
+ 8;
12499 else if (strncmp (h
->root
.root
.string
, "__stop_", 7) == 0)
12500 sec_name
= h
->root
.root
.string
+ 7;
12502 if (sec_name
!= NULL
&& *sec_name
!= '\0')
12506 for (i
= info
->input_bfds
; i
!= NULL
; i
= i
->link
.next
)
12508 s
= bfd_get_section_by_name (i
, sec_name
);
12511 h
->root
.u
.undef
.section
= s
;
12518 h
->root
.u
.undef
.section
= (asection
*) 0 - 1;
12523 /* COOKIE->rel describes a relocation against section SEC, which is
12524 a section we've decided to keep. Return the section that contains
12525 the relocation symbol, or NULL if no section contains it. */
12528 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
12529 elf_gc_mark_hook_fn gc_mark_hook
,
12530 struct elf_reloc_cookie
*cookie
,
12531 bfd_boolean
*start_stop
)
12533 unsigned long r_symndx
;
12534 struct elf_link_hash_entry
*h
;
12536 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
12537 if (r_symndx
== STN_UNDEF
)
12540 if (r_symndx
>= cookie
->locsymcount
12541 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
12543 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
12546 info
->callbacks
->einfo (_("%F%P: corrupt input: %B\n"),
12550 while (h
->root
.type
== bfd_link_hash_indirect
12551 || h
->root
.type
== bfd_link_hash_warning
)
12552 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
12554 /* If this symbol is weak and there is a non-weak definition, we
12555 keep the non-weak definition because many backends put
12556 dynamic reloc info on the non-weak definition for code
12557 handling copy relocs. */
12558 if (h
->u
.weakdef
!= NULL
)
12559 h
->u
.weakdef
->mark
= 1;
12561 if (start_stop
!= NULL
)
12563 /* To work around a glibc bug, mark all XXX input sections
12564 when there is an as yet undefined reference to __start_XXX
12565 or __stop_XXX symbols. The linker will later define such
12566 symbols for orphan input sections that have a name
12567 representable as a C identifier. */
12568 asection
*s
= _bfd_elf_is_start_stop (info
, h
);
12572 *start_stop
= !s
->gc_mark
;
12577 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
12580 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
12581 &cookie
->locsyms
[r_symndx
]);
12584 /* COOKIE->rel describes a relocation against section SEC, which is
12585 a section we've decided to keep. Mark the section that contains
12586 the relocation symbol. */
12589 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
12591 elf_gc_mark_hook_fn gc_mark_hook
,
12592 struct elf_reloc_cookie
*cookie
)
12595 bfd_boolean start_stop
= FALSE
;
12597 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
12598 while (rsec
!= NULL
)
12600 if (!rsec
->gc_mark
)
12602 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
12603 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
12605 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
12610 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
12615 /* The mark phase of garbage collection. For a given section, mark
12616 it and any sections in this section's group, and all the sections
12617 which define symbols to which it refers. */
12620 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
12622 elf_gc_mark_hook_fn gc_mark_hook
)
12625 asection
*group_sec
, *eh_frame
;
12629 /* Mark all the sections in the group. */
12630 group_sec
= elf_section_data (sec
)->next_in_group
;
12631 if (group_sec
&& !group_sec
->gc_mark
)
12632 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
12635 /* Look through the section relocs. */
12637 eh_frame
= elf_eh_frame_section (sec
->owner
);
12638 if ((sec
->flags
& SEC_RELOC
) != 0
12639 && sec
->reloc_count
> 0
12640 && sec
!= eh_frame
)
12642 struct elf_reloc_cookie cookie
;
12644 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
12648 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
12649 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
12654 fini_reloc_cookie_for_section (&cookie
, sec
);
12658 if (ret
&& eh_frame
&& elf_fde_list (sec
))
12660 struct elf_reloc_cookie cookie
;
12662 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
12666 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
12667 gc_mark_hook
, &cookie
))
12669 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
12673 eh_frame
= elf_section_eh_frame_entry (sec
);
12674 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
12675 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
12681 /* Scan and mark sections in a special or debug section group. */
12684 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
12686 /* Point to first section of section group. */
12688 /* Used to iterate the section group. */
12691 bfd_boolean is_special_grp
= TRUE
;
12692 bfd_boolean is_debug_grp
= TRUE
;
12694 /* First scan to see if group contains any section other than debug
12695 and special section. */
12696 ssec
= msec
= elf_next_in_group (grp
);
12699 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
12700 is_debug_grp
= FALSE
;
12702 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
12703 is_special_grp
= FALSE
;
12705 msec
= elf_next_in_group (msec
);
12707 while (msec
!= ssec
);
12709 /* If this is a pure debug section group or pure special section group,
12710 keep all sections in this group. */
12711 if (is_debug_grp
|| is_special_grp
)
12716 msec
= elf_next_in_group (msec
);
12718 while (msec
!= ssec
);
12722 /* Keep debug and special sections. */
12725 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
12726 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED
)
12730 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
12733 bfd_boolean some_kept
;
12734 bfd_boolean debug_frag_seen
;
12736 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
12739 /* Ensure all linker created sections are kept,
12740 see if any other section is already marked,
12741 and note if we have any fragmented debug sections. */
12742 debug_frag_seen
= some_kept
= FALSE
;
12743 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12745 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
12747 else if (isec
->gc_mark
)
12750 if (debug_frag_seen
== FALSE
12751 && (isec
->flags
& SEC_DEBUGGING
)
12752 && CONST_STRNEQ (isec
->name
, ".debug_line."))
12753 debug_frag_seen
= TRUE
;
12756 /* If no section in this file will be kept, then we can
12757 toss out the debug and special sections. */
12761 /* Keep debug and special sections like .comment when they are
12762 not part of a group. Also keep section groups that contain
12763 just debug sections or special sections. */
12764 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12766 if ((isec
->flags
& SEC_GROUP
) != 0)
12767 _bfd_elf_gc_mark_debug_special_section_group (isec
);
12768 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
12769 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
12770 && elf_next_in_group (isec
) == NULL
)
12774 if (! debug_frag_seen
)
12777 /* Look for CODE sections which are going to be discarded,
12778 and find and discard any fragmented debug sections which
12779 are associated with that code section. */
12780 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12781 if ((isec
->flags
& SEC_CODE
) != 0
12782 && isec
->gc_mark
== 0)
12787 ilen
= strlen (isec
->name
);
12789 /* Association is determined by the name of the debug section
12790 containing the name of the code section as a suffix. For
12791 example .debug_line.text.foo is a debug section associated
12793 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
12797 if (dsec
->gc_mark
== 0
12798 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
12801 dlen
= strlen (dsec
->name
);
12804 && strncmp (dsec
->name
+ (dlen
- ilen
),
12805 isec
->name
, ilen
) == 0)
12815 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12817 struct elf_gc_sweep_symbol_info
12819 struct bfd_link_info
*info
;
12820 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
12825 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
12828 && (((h
->root
.type
== bfd_link_hash_defined
12829 || h
->root
.type
== bfd_link_hash_defweak
)
12830 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
12831 && h
->root
.u
.def
.section
->gc_mark
))
12832 || h
->root
.type
== bfd_link_hash_undefined
12833 || h
->root
.type
== bfd_link_hash_undefweak
))
12835 struct elf_gc_sweep_symbol_info
*inf
;
12837 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
12838 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
12839 h
->def_regular
= 0;
12840 h
->ref_regular
= 0;
12841 h
->ref_regular_nonweak
= 0;
12847 /* The sweep phase of garbage collection. Remove all garbage sections. */
12849 typedef bfd_boolean (*gc_sweep_hook_fn
)
12850 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
12853 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
12856 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12857 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
12858 unsigned long section_sym_count
;
12859 struct elf_gc_sweep_symbol_info sweep_info
;
12861 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12865 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
12866 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
12869 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
12871 /* When any section in a section group is kept, we keep all
12872 sections in the section group. If the first member of
12873 the section group is excluded, we will also exclude the
12875 if (o
->flags
& SEC_GROUP
)
12877 asection
*first
= elf_next_in_group (o
);
12878 o
->gc_mark
= first
->gc_mark
;
12884 /* Skip sweeping sections already excluded. */
12885 if (o
->flags
& SEC_EXCLUDE
)
12888 /* Since this is early in the link process, it is simple
12889 to remove a section from the output. */
12890 o
->flags
|= SEC_EXCLUDE
;
12892 if (info
->print_gc_sections
&& o
->size
!= 0)
12893 /* xgettext:c-format */
12894 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub
, o
->name
);
12896 /* But we also have to update some of the relocation
12897 info we collected before. */
12899 && (o
->flags
& SEC_RELOC
) != 0
12900 && o
->reloc_count
!= 0
12901 && !((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
12902 && (o
->flags
& SEC_DEBUGGING
) != 0)
12903 && !bfd_is_abs_section (o
->output_section
))
12905 Elf_Internal_Rela
*internal_relocs
;
12909 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
12910 info
->keep_memory
);
12911 if (internal_relocs
== NULL
)
12914 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
12916 if (elf_section_data (o
)->relocs
!= internal_relocs
)
12917 free (internal_relocs
);
12925 /* Remove the symbols that were in the swept sections from the dynamic
12926 symbol table. GCFIXME: Anyone know how to get them out of the
12927 static symbol table as well? */
12928 sweep_info
.info
= info
;
12929 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
12930 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
12933 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
12937 /* Propagate collected vtable information. This is called through
12938 elf_link_hash_traverse. */
12941 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
12943 /* Those that are not vtables. */
12944 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
12947 /* Those vtables that do not have parents, we cannot merge. */
12948 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
12951 /* If we've already been done, exit. */
12952 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
12955 /* Make sure the parent's table is up to date. */
12956 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
12958 if (h
->vtable
->used
== NULL
)
12960 /* None of this table's entries were referenced. Re-use the
12962 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
12963 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
12968 bfd_boolean
*cu
, *pu
;
12970 /* Or the parent's entries into ours. */
12971 cu
= h
->vtable
->used
;
12973 pu
= h
->vtable
->parent
->vtable
->used
;
12976 const struct elf_backend_data
*bed
;
12977 unsigned int log_file_align
;
12979 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
12980 log_file_align
= bed
->s
->log_file_align
;
12981 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
12996 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
12999 bfd_vma hstart
, hend
;
13000 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
13001 const struct elf_backend_data
*bed
;
13002 unsigned int log_file_align
;
13004 /* Take care of both those symbols that do not describe vtables as
13005 well as those that are not loaded. */
13006 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
13009 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
13010 || h
->root
.type
== bfd_link_hash_defweak
);
13012 sec
= h
->root
.u
.def
.section
;
13013 hstart
= h
->root
.u
.def
.value
;
13014 hend
= hstart
+ h
->size
;
13016 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
13018 return *(bfd_boolean
*) okp
= FALSE
;
13019 bed
= get_elf_backend_data (sec
->owner
);
13020 log_file_align
= bed
->s
->log_file_align
;
13022 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
13024 for (rel
= relstart
; rel
< relend
; ++rel
)
13025 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
13027 /* If the entry is in use, do nothing. */
13028 if (h
->vtable
->used
13029 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
13031 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
13032 if (h
->vtable
->used
[entry
])
13035 /* Otherwise, kill it. */
13036 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
13042 /* Mark sections containing dynamically referenced symbols. When
13043 building shared libraries, we must assume that any visible symbol is
13047 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
13049 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
13050 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
13052 if ((h
->root
.type
== bfd_link_hash_defined
13053 || h
->root
.type
== bfd_link_hash_defweak
)
13055 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
13056 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
13057 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
13058 && (!bfd_link_executable (info
)
13059 || info
->export_dynamic
13062 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
13063 && (h
->versioned
>= versioned
13064 || !bfd_hide_sym_by_version (info
->version_info
,
13065 h
->root
.root
.string
)))))
13066 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13071 /* Keep all sections containing symbols undefined on the command-line,
13072 and the section containing the entry symbol. */
13075 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
13077 struct bfd_sym_chain
*sym
;
13079 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
13081 struct elf_link_hash_entry
*h
;
13083 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
13084 FALSE
, FALSE
, FALSE
);
13087 && (h
->root
.type
== bfd_link_hash_defined
13088 || h
->root
.type
== bfd_link_hash_defweak
)
13089 && !bfd_is_abs_section (h
->root
.u
.def
.section
)
13090 && !bfd_is_und_section (h
->root
.u
.def
.section
))
13091 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13096 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
13097 struct bfd_link_info
*info
)
13099 bfd
*ibfd
= info
->input_bfds
;
13101 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13104 struct elf_reloc_cookie cookie
;
13106 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13109 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
13112 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
13114 if (CONST_STRNEQ (bfd_section_name (ibfd
, sec
), ".eh_frame_entry")
13115 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
13117 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
13118 fini_reloc_cookie_rels (&cookie
, sec
);
13125 /* Do mark and sweep of unused sections. */
13128 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
13130 bfd_boolean ok
= TRUE
;
13132 elf_gc_mark_hook_fn gc_mark_hook
;
13133 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13134 struct elf_link_hash_table
*htab
;
13136 if (!bed
->can_gc_sections
13137 || !is_elf_hash_table (info
->hash
))
13139 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13143 bed
->gc_keep (info
);
13144 htab
= elf_hash_table (info
);
13146 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13147 at the .eh_frame section if we can mark the FDEs individually. */
13148 for (sub
= info
->input_bfds
;
13149 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
13150 sub
= sub
->link
.next
)
13153 struct elf_reloc_cookie cookie
;
13155 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
13156 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
13158 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
13159 if (elf_section_data (sec
)->sec_info
13160 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
13161 elf_eh_frame_section (sub
) = sec
;
13162 fini_reloc_cookie_for_section (&cookie
, sec
);
13163 sec
= bfd_get_next_section_by_name (NULL
, sec
);
13167 /* Apply transitive closure to the vtable entry usage info. */
13168 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
13172 /* Kill the vtable relocations that were not used. */
13173 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &ok
);
13177 /* Mark dynamically referenced symbols. */
13178 if (htab
->dynamic_sections_created
)
13179 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
13181 /* Grovel through relocs to find out who stays ... */
13182 gc_mark_hook
= bed
->gc_mark_hook
;
13183 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13187 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13188 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13191 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13192 Also treat note sections as a root, if the section is not part
13194 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13196 && (o
->flags
& SEC_EXCLUDE
) == 0
13197 && ((o
->flags
& SEC_KEEP
) != 0
13198 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
13199 && elf_next_in_group (o
) == NULL
)))
13201 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
13206 /* Allow the backend to mark additional target specific sections. */
13207 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
13209 /* ... and mark SEC_EXCLUDE for those that go. */
13210 return elf_gc_sweep (abfd
, info
);
13213 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13216 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
13218 struct elf_link_hash_entry
*h
,
13221 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
13222 struct elf_link_hash_entry
**search
, *child
;
13223 size_t extsymcount
;
13224 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13226 /* The sh_info field of the symtab header tells us where the
13227 external symbols start. We don't care about the local symbols at
13229 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
13230 if (!elf_bad_symtab (abfd
))
13231 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
13233 sym_hashes
= elf_sym_hashes (abfd
);
13234 sym_hashes_end
= sym_hashes
+ extsymcount
;
13236 /* Hunt down the child symbol, which is in this section at the same
13237 offset as the relocation. */
13238 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
13240 if ((child
= *search
) != NULL
13241 && (child
->root
.type
== bfd_link_hash_defined
13242 || child
->root
.type
== bfd_link_hash_defweak
)
13243 && child
->root
.u
.def
.section
== sec
13244 && child
->root
.u
.def
.value
== offset
)
13248 /* xgettext:c-format */
13249 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"),
13250 abfd
, sec
, (unsigned long) offset
);
13251 bfd_set_error (bfd_error_invalid_operation
);
13255 if (!child
->vtable
)
13257 child
->vtable
= ((struct elf_link_virtual_table_entry
*)
13258 bfd_zalloc (abfd
, sizeof (*child
->vtable
)));
13259 if (!child
->vtable
)
13264 /* This *should* only be the absolute section. It could potentially
13265 be that someone has defined a non-global vtable though, which
13266 would be bad. It isn't worth paging in the local symbols to be
13267 sure though; that case should simply be handled by the assembler. */
13269 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
13272 child
->vtable
->parent
= h
;
13277 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13280 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
13281 asection
*sec ATTRIBUTE_UNUSED
,
13282 struct elf_link_hash_entry
*h
,
13285 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13286 unsigned int log_file_align
= bed
->s
->log_file_align
;
13290 h
->vtable
= ((struct elf_link_virtual_table_entry
*)
13291 bfd_zalloc (abfd
, sizeof (*h
->vtable
)));
13296 if (addend
>= h
->vtable
->size
)
13298 size_t size
, bytes
, file_align
;
13299 bfd_boolean
*ptr
= h
->vtable
->used
;
13301 /* While the symbol is undefined, we have to be prepared to handle
13303 file_align
= 1 << log_file_align
;
13304 if (h
->root
.type
== bfd_link_hash_undefined
)
13305 size
= addend
+ file_align
;
13309 if (addend
>= size
)
13311 /* Oops! We've got a reference past the defined end of
13312 the table. This is probably a bug -- shall we warn? */
13313 size
= addend
+ file_align
;
13316 size
= (size
+ file_align
- 1) & -file_align
;
13318 /* Allocate one extra entry for use as a "done" flag for the
13319 consolidation pass. */
13320 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
13324 ptr
= (bfd_boolean
*) bfd_realloc (ptr
- 1, bytes
);
13330 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
13331 * sizeof (bfd_boolean
));
13332 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
13336 ptr
= (bfd_boolean
*) bfd_zmalloc (bytes
);
13341 /* And arrange for that done flag to be at index -1. */
13342 h
->vtable
->used
= ptr
+ 1;
13343 h
->vtable
->size
= size
;
13346 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
13351 /* Map an ELF section header flag to its corresponding string. */
13355 flagword flag_value
;
13356 } elf_flags_to_name_table
;
13358 static elf_flags_to_name_table elf_flags_to_names
[] =
13360 { "SHF_WRITE", SHF_WRITE
},
13361 { "SHF_ALLOC", SHF_ALLOC
},
13362 { "SHF_EXECINSTR", SHF_EXECINSTR
},
13363 { "SHF_MERGE", SHF_MERGE
},
13364 { "SHF_STRINGS", SHF_STRINGS
},
13365 { "SHF_INFO_LINK", SHF_INFO_LINK
},
13366 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
13367 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
13368 { "SHF_GROUP", SHF_GROUP
},
13369 { "SHF_TLS", SHF_TLS
},
13370 { "SHF_MASKOS", SHF_MASKOS
},
13371 { "SHF_EXCLUDE", SHF_EXCLUDE
},
13374 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13376 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
13377 struct flag_info
*flaginfo
,
13380 const bfd_vma sh_flags
= elf_section_flags (section
);
13382 if (!flaginfo
->flags_initialized
)
13384 bfd
*obfd
= info
->output_bfd
;
13385 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13386 struct flag_info_list
*tf
= flaginfo
->flag_list
;
13388 int without_hex
= 0;
13390 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
13393 flagword (*lookup
) (char *);
13395 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
13396 if (lookup
!= NULL
)
13398 flagword hexval
= (*lookup
) ((char *) tf
->name
);
13402 if (tf
->with
== with_flags
)
13403 with_hex
|= hexval
;
13404 else if (tf
->with
== without_flags
)
13405 without_hex
|= hexval
;
13410 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
13412 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
13414 if (tf
->with
== with_flags
)
13415 with_hex
|= elf_flags_to_names
[i
].flag_value
;
13416 else if (tf
->with
== without_flags
)
13417 without_hex
|= elf_flags_to_names
[i
].flag_value
;
13424 info
->callbacks
->einfo
13425 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
13429 flaginfo
->flags_initialized
= TRUE
;
13430 flaginfo
->only_with_flags
|= with_hex
;
13431 flaginfo
->not_with_flags
|= without_hex
;
13434 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
13437 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
13443 struct alloc_got_off_arg
{
13445 struct bfd_link_info
*info
;
13448 /* We need a special top-level link routine to convert got reference counts
13449 to real got offsets. */
13452 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
13454 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
13455 bfd
*obfd
= gofarg
->info
->output_bfd
;
13456 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13458 if (h
->got
.refcount
> 0)
13460 h
->got
.offset
= gofarg
->gotoff
;
13461 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
13464 h
->got
.offset
= (bfd_vma
) -1;
13469 /* And an accompanying bit to work out final got entry offsets once
13470 we're done. Should be called from final_link. */
13473 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
13474 struct bfd_link_info
*info
)
13477 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13479 struct alloc_got_off_arg gofarg
;
13481 BFD_ASSERT (abfd
== info
->output_bfd
);
13483 if (! is_elf_hash_table (info
->hash
))
13486 /* The GOT offset is relative to the .got section, but the GOT header is
13487 put into the .got.plt section, if the backend uses it. */
13488 if (bed
->want_got_plt
)
13491 gotoff
= bed
->got_header_size
;
13493 /* Do the local .got entries first. */
13494 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
13496 bfd_signed_vma
*local_got
;
13497 size_t j
, locsymcount
;
13498 Elf_Internal_Shdr
*symtab_hdr
;
13500 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
13503 local_got
= elf_local_got_refcounts (i
);
13507 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
13508 if (elf_bad_symtab (i
))
13509 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13511 locsymcount
= symtab_hdr
->sh_info
;
13513 for (j
= 0; j
< locsymcount
; ++j
)
13515 if (local_got
[j
] > 0)
13517 local_got
[j
] = gotoff
;
13518 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
13521 local_got
[j
] = (bfd_vma
) -1;
13525 /* Then the global .got entries. .plt refcounts are handled by
13526 adjust_dynamic_symbol */
13527 gofarg
.gotoff
= gotoff
;
13528 gofarg
.info
= info
;
13529 elf_link_hash_traverse (elf_hash_table (info
),
13530 elf_gc_allocate_got_offsets
,
13535 /* Many folk need no more in the way of final link than this, once
13536 got entry reference counting is enabled. */
13539 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
13541 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
13544 /* Invoke the regular ELF backend linker to do all the work. */
13545 return bfd_elf_final_link (abfd
, info
);
13549 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
13551 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
13553 if (rcookie
->bad_symtab
)
13554 rcookie
->rel
= rcookie
->rels
;
13556 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
13558 unsigned long r_symndx
;
13560 if (! rcookie
->bad_symtab
)
13561 if (rcookie
->rel
->r_offset
> offset
)
13563 if (rcookie
->rel
->r_offset
!= offset
)
13566 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
13567 if (r_symndx
== STN_UNDEF
)
13570 if (r_symndx
>= rcookie
->locsymcount
13571 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13573 struct elf_link_hash_entry
*h
;
13575 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
13577 while (h
->root
.type
== bfd_link_hash_indirect
13578 || h
->root
.type
== bfd_link_hash_warning
)
13579 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13581 if ((h
->root
.type
== bfd_link_hash_defined
13582 || h
->root
.type
== bfd_link_hash_defweak
)
13583 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
13584 || h
->root
.u
.def
.section
->kept_section
!= NULL
13585 || discarded_section (h
->root
.u
.def
.section
)))
13590 /* It's not a relocation against a global symbol,
13591 but it could be a relocation against a local
13592 symbol for a discarded section. */
13594 Elf_Internal_Sym
*isym
;
13596 /* Need to: get the symbol; get the section. */
13597 isym
= &rcookie
->locsyms
[r_symndx
];
13598 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
13600 && (isec
->kept_section
!= NULL
13601 || discarded_section (isec
)))
13609 /* Discard unneeded references to discarded sections.
13610 Returns -1 on error, 1 if any section's size was changed, 0 if
13611 nothing changed. This function assumes that the relocations are in
13612 sorted order, which is true for all known assemblers. */
13615 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
13617 struct elf_reloc_cookie cookie
;
13622 if (info
->traditional_format
13623 || !is_elf_hash_table (info
->hash
))
13626 o
= bfd_get_section_by_name (output_bfd
, ".stab");
13631 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13634 || i
->reloc_count
== 0
13635 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
13639 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13642 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13645 if (_bfd_discard_section_stabs (abfd
, i
,
13646 elf_section_data (i
)->sec_info
,
13647 bfd_elf_reloc_symbol_deleted_p
,
13651 fini_reloc_cookie_for_section (&cookie
, i
);
13656 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
13657 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
13662 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13668 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13671 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13674 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
13675 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
13676 bfd_elf_reloc_symbol_deleted_p
,
13680 fini_reloc_cookie_for_section (&cookie
, i
);
13684 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
13686 const struct elf_backend_data
*bed
;
13688 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13691 bed
= get_elf_backend_data (abfd
);
13693 if (bed
->elf_backend_discard_info
!= NULL
)
13695 if (!init_reloc_cookie (&cookie
, info
, abfd
))
13698 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
13701 fini_reloc_cookie (&cookie
, abfd
);
13705 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
13706 _bfd_elf_end_eh_frame_parsing (info
);
13708 if (info
->eh_frame_hdr_type
13709 && !bfd_link_relocatable (info
)
13710 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
13717 _bfd_elf_section_already_linked (bfd
*abfd
,
13719 struct bfd_link_info
*info
)
13722 const char *name
, *key
;
13723 struct bfd_section_already_linked
*l
;
13724 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
13726 if (sec
->output_section
== bfd_abs_section_ptr
)
13729 flags
= sec
->flags
;
13731 /* Return if it isn't a linkonce section. A comdat group section
13732 also has SEC_LINK_ONCE set. */
13733 if ((flags
& SEC_LINK_ONCE
) == 0)
13736 /* Don't put group member sections on our list of already linked
13737 sections. They are handled as a group via their group section. */
13738 if (elf_sec_group (sec
) != NULL
)
13741 /* For a SHT_GROUP section, use the group signature as the key. */
13743 if ((flags
& SEC_GROUP
) != 0
13744 && elf_next_in_group (sec
) != NULL
13745 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
13746 key
= elf_group_name (elf_next_in_group (sec
));
13749 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13750 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
13751 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
13754 /* Must be a user linkonce section that doesn't follow gcc's
13755 naming convention. In this case we won't be matching
13756 single member groups. */
13760 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
13762 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13764 /* We may have 2 different types of sections on the list: group
13765 sections with a signature of <key> (<key> is some string),
13766 and linkonce sections named .gnu.linkonce.<type>.<key>.
13767 Match like sections. LTO plugin sections are an exception.
13768 They are always named .gnu.linkonce.t.<key> and match either
13769 type of section. */
13770 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
13771 && ((flags
& SEC_GROUP
) != 0
13772 || strcmp (name
, l
->sec
->name
) == 0))
13773 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0)
13775 /* The section has already been linked. See if we should
13776 issue a warning. */
13777 if (!_bfd_handle_already_linked (sec
, l
, info
))
13780 if (flags
& SEC_GROUP
)
13782 asection
*first
= elf_next_in_group (sec
);
13783 asection
*s
= first
;
13787 s
->output_section
= bfd_abs_section_ptr
;
13788 /* Record which group discards it. */
13789 s
->kept_section
= l
->sec
;
13790 s
= elf_next_in_group (s
);
13791 /* These lists are circular. */
13801 /* A single member comdat group section may be discarded by a
13802 linkonce section and vice versa. */
13803 if ((flags
& SEC_GROUP
) != 0)
13805 asection
*first
= elf_next_in_group (sec
);
13807 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
13808 /* Check this single member group against linkonce sections. */
13809 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13810 if ((l
->sec
->flags
& SEC_GROUP
) == 0
13811 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
13813 first
->output_section
= bfd_abs_section_ptr
;
13814 first
->kept_section
= l
->sec
;
13815 sec
->output_section
= bfd_abs_section_ptr
;
13820 /* Check this linkonce section against single member groups. */
13821 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13822 if (l
->sec
->flags
& SEC_GROUP
)
13824 asection
*first
= elf_next_in_group (l
->sec
);
13827 && elf_next_in_group (first
) == first
13828 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
13830 sec
->output_section
= bfd_abs_section_ptr
;
13831 sec
->kept_section
= first
;
13836 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13837 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13838 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13839 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13840 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13841 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13842 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13843 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13844 The reverse order cannot happen as there is never a bfd with only the
13845 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13846 matter as here were are looking only for cross-bfd sections. */
13848 if ((flags
& SEC_GROUP
) == 0 && CONST_STRNEQ (name
, ".gnu.linkonce.r."))
13849 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13850 if ((l
->sec
->flags
& SEC_GROUP
) == 0
13851 && CONST_STRNEQ (l
->sec
->name
, ".gnu.linkonce.t."))
13853 if (abfd
!= l
->sec
->owner
)
13854 sec
->output_section
= bfd_abs_section_ptr
;
13858 /* This is the first section with this name. Record it. */
13859 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
13860 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
13861 return sec
->output_section
== bfd_abs_section_ptr
;
13865 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
13867 return sym
->st_shndx
== SHN_COMMON
;
13871 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
13877 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
13879 return bfd_com_section_ptr
;
13883 _bfd_elf_default_got_elt_size (bfd
*abfd
,
13884 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13885 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
13886 bfd
*ibfd ATTRIBUTE_UNUSED
,
13887 unsigned long symndx ATTRIBUTE_UNUSED
)
13889 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13890 return bed
->s
->arch_size
/ 8;
13893 /* Routines to support the creation of dynamic relocs. */
13895 /* Returns the name of the dynamic reloc section associated with SEC. */
13897 static const char *
13898 get_dynamic_reloc_section_name (bfd
* abfd
,
13900 bfd_boolean is_rela
)
13903 const char *old_name
= bfd_get_section_name (NULL
, sec
);
13904 const char *prefix
= is_rela
? ".rela" : ".rel";
13906 if (old_name
== NULL
)
13909 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
13910 sprintf (name
, "%s%s", prefix
, old_name
);
13915 /* Returns the dynamic reloc section associated with SEC.
13916 If necessary compute the name of the dynamic reloc section based
13917 on SEC's name (looked up in ABFD's string table) and the setting
13921 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
13923 bfd_boolean is_rela
)
13925 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
13927 if (reloc_sec
== NULL
)
13929 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
13933 reloc_sec
= bfd_get_linker_section (abfd
, name
);
13935 if (reloc_sec
!= NULL
)
13936 elf_section_data (sec
)->sreloc
= reloc_sec
;
13943 /* Returns the dynamic reloc section associated with SEC. If the
13944 section does not exist it is created and attached to the DYNOBJ
13945 bfd and stored in the SRELOC field of SEC's elf_section_data
13948 ALIGNMENT is the alignment for the newly created section and
13949 IS_RELA defines whether the name should be .rela.<SEC's name>
13950 or .rel.<SEC's name>. The section name is looked up in the
13951 string table associated with ABFD. */
13954 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
13956 unsigned int alignment
,
13958 bfd_boolean is_rela
)
13960 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
13962 if (reloc_sec
== NULL
)
13964 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
13969 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
13971 if (reloc_sec
== NULL
)
13973 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
13974 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
13975 if ((sec
->flags
& SEC_ALLOC
) != 0)
13976 flags
|= SEC_ALLOC
| SEC_LOAD
;
13978 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
13979 if (reloc_sec
!= NULL
)
13981 /* _bfd_elf_get_sec_type_attr chooses a section type by
13982 name. Override as it may be wrong, eg. for a user
13983 section named "auto" we'll get ".relauto" which is
13984 seen to be a .rela section. */
13985 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
13986 if (! bfd_set_section_alignment (dynobj
, reloc_sec
, alignment
))
13991 elf_section_data (sec
)->sreloc
= reloc_sec
;
13997 /* Copy the ELF symbol type and other attributes for a linker script
13998 assignment from HSRC to HDEST. Generally this should be treated as
13999 if we found a strong non-dynamic definition for HDEST (except that
14000 ld ignores multiple definition errors). */
14002 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
14003 struct bfd_link_hash_entry
*hdest
,
14004 struct bfd_link_hash_entry
*hsrc
)
14006 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
14007 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
14008 Elf_Internal_Sym isym
;
14010 ehdest
->type
= ehsrc
->type
;
14011 ehdest
->target_internal
= ehsrc
->target_internal
;
14013 isym
.st_other
= ehsrc
->other
;
14014 elf_merge_st_other (abfd
, ehdest
, &isym
, NULL
, TRUE
, FALSE
);
14017 /* Append a RELA relocation REL to section S in BFD. */
14020 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14022 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14023 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
14024 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
14025 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
14028 /* Append a REL relocation REL to section S in BFD. */
14031 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14033 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14034 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
14035 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
14036 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);