1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2017 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
23 #include "bfd_stdint.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
31 #if BFD_SUPPORTS_PLUGINS
32 #include "plugin-api.h"
36 /* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
39 struct elf_info_failed
41 struct bfd_link_info
*info
;
45 /* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
48 struct elf_find_verdep_info
50 /* General link information. */
51 struct bfd_link_info
*info
;
52 /* The number of dependencies. */
54 /* Whether we had a failure. */
58 static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry
*, struct elf_info_failed
*);
62 _bfd_elf_section_for_symbol (struct elf_reloc_cookie
*cookie
,
63 unsigned long r_symndx
,
66 if (r_symndx
>= cookie
->locsymcount
67 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
69 struct elf_link_hash_entry
*h
;
71 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
73 while (h
->root
.type
== bfd_link_hash_indirect
74 || h
->root
.type
== bfd_link_hash_warning
)
75 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
77 if ((h
->root
.type
== bfd_link_hash_defined
78 || h
->root
.type
== bfd_link_hash_defweak
)
79 && discarded_section (h
->root
.u
.def
.section
))
80 return h
->root
.u
.def
.section
;
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
90 Elf_Internal_Sym
*isym
;
92 /* Need to: get the symbol; get the section. */
93 isym
= &cookie
->locsyms
[r_symndx
];
94 isec
= bfd_section_from_elf_index (cookie
->abfd
, isym
->st_shndx
);
96 && discard
? discarded_section (isec
) : 1)
102 /* Define a symbol in a dynamic linkage section. */
104 struct elf_link_hash_entry
*
105 _bfd_elf_define_linkage_sym (bfd
*abfd
,
106 struct bfd_link_info
*info
,
110 struct elf_link_hash_entry
*h
;
111 struct bfd_link_hash_entry
*bh
;
112 const struct elf_backend_data
*bed
;
114 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h
->root
.type
= bfd_link_hash_new
;
127 bed
= get_elf_backend_data (abfd
);
128 if (!_bfd_generic_link_add_one_symbol (info
, abfd
, name
, BSF_GLOBAL
,
129 sec
, 0, NULL
, FALSE
, bed
->collect
,
132 h
= (struct elf_link_hash_entry
*) bh
;
133 BFD_ASSERT (h
!= NULL
);
136 h
->root
.linker_def
= 1;
137 h
->type
= STT_OBJECT
;
138 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
139 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
141 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
146 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
150 struct elf_link_hash_entry
*h
;
151 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
152 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
154 /* This function may be called more than once. */
155 if (htab
->sgot
!= NULL
)
158 flags
= bed
->dynamic_sec_flags
;
160 s
= bfd_make_section_anyway_with_flags (abfd
,
161 (bed
->rela_plts_and_copies_p
162 ? ".rela.got" : ".rel.got"),
163 (bed
->dynamic_sec_flags
166 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
170 s
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
172 || !bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
176 if (bed
->want_got_plt
)
178 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
180 || !bfd_set_section_alignment (abfd
, s
,
181 bed
->s
->log_file_align
))
186 /* The first bit of the global offset table is the header. */
187 s
->size
+= bed
->got_header_size
;
189 if (bed
->want_got_sym
)
191 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
192 (or .got.plt) section. We don't do this in the linker script
193 because we don't want to define the symbol if we are not creating
194 a global offset table. */
195 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
196 "_GLOBAL_OFFSET_TABLE_");
197 elf_hash_table (info
)->hgot
= h
;
205 /* Create a strtab to hold the dynamic symbol names. */
207 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
209 struct elf_link_hash_table
*hash_table
;
211 hash_table
= elf_hash_table (info
);
212 if (hash_table
->dynobj
== NULL
)
214 /* We may not set dynobj, an input file holding linker created
215 dynamic sections to abfd, which may be a dynamic object with
216 its own dynamic sections. We need to find a normal input file
217 to hold linker created sections if possible. */
218 if ((abfd
->flags
& (DYNAMIC
| BFD_PLUGIN
)) != 0)
221 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link
.next
)
223 & (DYNAMIC
| BFD_LINKER_CREATED
| BFD_PLUGIN
)) == 0)
229 hash_table
->dynobj
= abfd
;
232 if (hash_table
->dynstr
== NULL
)
234 hash_table
->dynstr
= _bfd_elf_strtab_init ();
235 if (hash_table
->dynstr
== NULL
)
241 /* Create some sections which will be filled in with dynamic linking
242 information. ABFD is an input file which requires dynamic sections
243 to be created. The dynamic sections take up virtual memory space
244 when the final executable is run, so we need to create them before
245 addresses are assigned to the output sections. We work out the
246 actual contents and size of these sections later. */
249 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
253 const struct elf_backend_data
*bed
;
254 struct elf_link_hash_entry
*h
;
256 if (! is_elf_hash_table (info
->hash
))
259 if (elf_hash_table (info
)->dynamic_sections_created
)
262 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
265 abfd
= elf_hash_table (info
)->dynobj
;
266 bed
= get_elf_backend_data (abfd
);
268 flags
= bed
->dynamic_sec_flags
;
270 /* A dynamically linked executable has a .interp section, but a
271 shared library does not. */
272 if (bfd_link_executable (info
) && !info
->nointerp
)
274 s
= bfd_make_section_anyway_with_flags (abfd
, ".interp",
275 flags
| SEC_READONLY
);
280 /* Create sections to hold version informations. These are removed
281 if they are not needed. */
282 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_d",
283 flags
| SEC_READONLY
);
285 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
288 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version",
289 flags
| SEC_READONLY
);
291 || ! bfd_set_section_alignment (abfd
, s
, 1))
294 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_r",
295 flags
| SEC_READONLY
);
297 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
300 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynsym",
301 flags
| SEC_READONLY
);
303 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
305 elf_hash_table (info
)->dynsym
= s
;
307 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynstr",
308 flags
| SEC_READONLY
);
312 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynamic", flags
);
314 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
317 /* The special symbol _DYNAMIC is always set to the start of the
318 .dynamic section. We could set _DYNAMIC in a linker script, but we
319 only want to define it if we are, in fact, creating a .dynamic
320 section. We don't want to define it if there is no .dynamic
321 section, since on some ELF platforms the start up code examines it
322 to decide how to initialize the process. */
323 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC");
324 elf_hash_table (info
)->hdynamic
= h
;
330 s
= bfd_make_section_anyway_with_flags (abfd
, ".hash",
331 flags
| SEC_READONLY
);
333 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
335 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
338 if (info
->emit_gnu_hash
)
340 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.hash",
341 flags
| SEC_READONLY
);
343 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
345 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
346 4 32-bit words followed by variable count of 64-bit words, then
347 variable count of 32-bit words. */
348 if (bed
->s
->arch_size
== 64)
349 elf_section_data (s
)->this_hdr
.sh_entsize
= 0;
351 elf_section_data (s
)->this_hdr
.sh_entsize
= 4;
354 /* Let the backend create the rest of the sections. This lets the
355 backend set the right flags. The backend will normally create
356 the .got and .plt sections. */
357 if (bed
->elf_backend_create_dynamic_sections
== NULL
358 || ! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
361 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
366 /* Create dynamic sections when linking against a dynamic object. */
369 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
371 flagword flags
, pltflags
;
372 struct elf_link_hash_entry
*h
;
374 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
375 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
377 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
378 .rel[a].bss sections. */
379 flags
= bed
->dynamic_sec_flags
;
382 if (bed
->plt_not_loaded
)
383 /* We do not clear SEC_ALLOC here because we still want the OS to
384 allocate space for the section; it's just that there's nothing
385 to read in from the object file. */
386 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
388 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
389 if (bed
->plt_readonly
)
390 pltflags
|= SEC_READONLY
;
392 s
= bfd_make_section_anyway_with_flags (abfd
, ".plt", pltflags
);
394 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
398 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
400 if (bed
->want_plt_sym
)
402 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
403 "_PROCEDURE_LINKAGE_TABLE_");
404 elf_hash_table (info
)->hplt
= h
;
409 s
= bfd_make_section_anyway_with_flags (abfd
,
410 (bed
->rela_plts_and_copies_p
411 ? ".rela.plt" : ".rel.plt"),
412 flags
| SEC_READONLY
);
414 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
418 if (! _bfd_elf_create_got_section (abfd
, info
))
421 if (bed
->want_dynbss
)
423 /* The .dynbss section is a place to put symbols which are defined
424 by dynamic objects, are referenced by regular objects, and are
425 not functions. We must allocate space for them in the process
426 image and use a R_*_COPY reloc to tell the dynamic linker to
427 initialize them at run time. The linker script puts the .dynbss
428 section into the .bss section of the final image. */
429 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynbss",
430 SEC_ALLOC
| SEC_LINKER_CREATED
);
435 if (bed
->want_dynrelro
)
437 /* Similarly, but for symbols that were originally in read-only
438 sections. This section doesn't really need to have contents,
439 but make it like other .data.rel.ro sections. */
440 s
= bfd_make_section_anyway_with_flags (abfd
, ".data.rel.ro",
447 /* The .rel[a].bss section holds copy relocs. This section is not
448 normally needed. We need to create it here, though, so that the
449 linker will map it to an output section. We can't just create it
450 only if we need it, because we will not know whether we need it
451 until we have seen all the input files, and the first time the
452 main linker code calls BFD after examining all the input files
453 (size_dynamic_sections) the input sections have already been
454 mapped to the output sections. If the section turns out not to
455 be needed, we can discard it later. We will never need this
456 section when generating a shared object, since they do not use
458 if (bfd_link_executable (info
))
460 s
= bfd_make_section_anyway_with_flags (abfd
,
461 (bed
->rela_plts_and_copies_p
462 ? ".rela.bss" : ".rel.bss"),
463 flags
| SEC_READONLY
);
465 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
469 if (bed
->want_dynrelro
)
471 s
= (bfd_make_section_anyway_with_flags
472 (abfd
, (bed
->rela_plts_and_copies_p
473 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
474 flags
| SEC_READONLY
));
476 || ! bfd_set_section_alignment (abfd
, s
,
477 bed
->s
->log_file_align
))
479 htab
->sreldynrelro
= s
;
487 /* Record a new dynamic symbol. We record the dynamic symbols as we
488 read the input files, since we need to have a list of all of them
489 before we can determine the final sizes of the output sections.
490 Note that we may actually call this function even though we are not
491 going to output any dynamic symbols; in some cases we know that a
492 symbol should be in the dynamic symbol table, but only if there is
496 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
497 struct elf_link_hash_entry
*h
)
499 if (h
->dynindx
== -1)
501 struct elf_strtab_hash
*dynstr
;
506 /* XXX: The ABI draft says the linker must turn hidden and
507 internal symbols into STB_LOCAL symbols when producing the
508 DSO. However, if ld.so honors st_other in the dynamic table,
509 this would not be necessary. */
510 switch (ELF_ST_VISIBILITY (h
->other
))
514 if (h
->root
.type
!= bfd_link_hash_undefined
515 && h
->root
.type
!= bfd_link_hash_undefweak
)
518 if (!elf_hash_table (info
)->is_relocatable_executable
)
526 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
527 ++elf_hash_table (info
)->dynsymcount
;
529 dynstr
= elf_hash_table (info
)->dynstr
;
532 /* Create a strtab to hold the dynamic symbol names. */
533 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
538 /* We don't put any version information in the dynamic string
540 name
= h
->root
.root
.string
;
541 p
= strchr (name
, ELF_VER_CHR
);
543 /* We know that the p points into writable memory. In fact,
544 there are only a few symbols that have read-only names, being
545 those like _GLOBAL_OFFSET_TABLE_ that are created specially
546 by the backends. Most symbols will have names pointing into
547 an ELF string table read from a file, or to objalloc memory. */
550 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
555 if (indx
== (size_t) -1)
557 h
->dynstr_index
= indx
;
563 /* Mark a symbol dynamic. */
566 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
567 struct elf_link_hash_entry
*h
,
568 Elf_Internal_Sym
*sym
)
570 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
572 /* It may be called more than once on the same H. */
573 if(h
->dynamic
|| bfd_link_relocatable (info
))
576 if ((info
->dynamic_data
577 && (h
->type
== STT_OBJECT
578 || h
->type
== STT_COMMON
580 && (ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
581 || ELF_ST_TYPE (sym
->st_info
) == STT_COMMON
))))
583 && h
->root
.type
== bfd_link_hash_new
584 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
588 /* Record an assignment to a symbol made by a linker script. We need
589 this in case some dynamic object refers to this symbol. */
592 bfd_elf_record_link_assignment (bfd
*output_bfd
,
593 struct bfd_link_info
*info
,
598 struct elf_link_hash_entry
*h
, *hv
;
599 struct elf_link_hash_table
*htab
;
600 const struct elf_backend_data
*bed
;
602 if (!is_elf_hash_table (info
->hash
))
605 htab
= elf_hash_table (info
);
606 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
610 if (h
->root
.type
== bfd_link_hash_warning
)
611 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
613 if (h
->versioned
== unknown
)
615 /* Set versioned if symbol version is unknown. */
616 char *version
= strrchr (name
, ELF_VER_CHR
);
619 if (version
> name
&& version
[-1] != ELF_VER_CHR
)
620 h
->versioned
= versioned_hidden
;
622 h
->versioned
= versioned
;
626 switch (h
->root
.type
)
628 case bfd_link_hash_defined
:
629 case bfd_link_hash_defweak
:
630 case bfd_link_hash_common
:
632 case bfd_link_hash_undefweak
:
633 case bfd_link_hash_undefined
:
634 /* Since we're defining the symbol, don't let it seem to have not
635 been defined. record_dynamic_symbol and size_dynamic_sections
636 may depend on this. */
637 h
->root
.type
= bfd_link_hash_new
;
638 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
639 bfd_link_repair_undef_list (&htab
->root
);
641 case bfd_link_hash_new
:
642 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
645 case bfd_link_hash_indirect
:
646 /* We had a versioned symbol in a dynamic library. We make the
647 the versioned symbol point to this one. */
648 bed
= get_elf_backend_data (output_bfd
);
650 while (hv
->root
.type
== bfd_link_hash_indirect
651 || hv
->root
.type
== bfd_link_hash_warning
)
652 hv
= (struct elf_link_hash_entry
*) hv
->root
.u
.i
.link
;
653 /* We don't need to update h->root.u since linker will set them
655 h
->root
.type
= bfd_link_hash_undefined
;
656 hv
->root
.type
= bfd_link_hash_indirect
;
657 hv
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
658 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hv
);
665 /* If this symbol is being provided by the linker script, and it is
666 currently defined by a dynamic object, but not by a regular
667 object, then mark it as undefined so that the generic linker will
668 force the correct value. */
672 h
->root
.type
= bfd_link_hash_undefined
;
674 /* If this symbol is not being provided by the linker script, and it is
675 currently defined by a dynamic object, but not by a regular object,
676 then clear out any version information because the symbol will not be
677 associated with the dynamic object any more. */
681 h
->verinfo
.verdef
= NULL
;
683 /* Make sure this symbol is not garbage collected. */
690 bed
= get_elf_backend_data (output_bfd
);
691 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
692 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
693 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
696 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
698 if (!bfd_link_relocatable (info
)
700 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
701 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
706 || bfd_link_dll (info
)
707 || elf_hash_table (info
)->is_relocatable_executable
)
710 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
713 /* If this is a weak defined symbol, and we know a corresponding
714 real symbol from the same dynamic object, make sure the real
715 symbol is also made into a dynamic symbol. */
716 if (h
->u
.weakdef
!= NULL
717 && h
->u
.weakdef
->dynindx
== -1)
719 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
727 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
728 success, and 2 on a failure caused by attempting to record a symbol
729 in a discarded section, eg. a discarded link-once section symbol. */
732 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
737 struct elf_link_local_dynamic_entry
*entry
;
738 struct elf_link_hash_table
*eht
;
739 struct elf_strtab_hash
*dynstr
;
742 Elf_External_Sym_Shndx eshndx
;
743 char esym
[sizeof (Elf64_External_Sym
)];
745 if (! is_elf_hash_table (info
->hash
))
748 /* See if the entry exists already. */
749 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
750 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
753 amt
= sizeof (*entry
);
754 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
758 /* Go find the symbol, so that we can find it's name. */
759 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
760 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
762 bfd_release (input_bfd
, entry
);
766 if (entry
->isym
.st_shndx
!= SHN_UNDEF
767 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
771 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
772 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
774 /* We can still bfd_release here as nothing has done another
775 bfd_alloc. We can't do this later in this function. */
776 bfd_release (input_bfd
, entry
);
781 name
= (bfd_elf_string_from_elf_section
782 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
783 entry
->isym
.st_name
));
785 dynstr
= elf_hash_table (info
)->dynstr
;
788 /* Create a strtab to hold the dynamic symbol names. */
789 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
794 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
795 if (dynstr_index
== (size_t) -1)
797 entry
->isym
.st_name
= dynstr_index
;
799 eht
= elf_hash_table (info
);
801 entry
->next
= eht
->dynlocal
;
802 eht
->dynlocal
= entry
;
803 entry
->input_bfd
= input_bfd
;
804 entry
->input_indx
= input_indx
;
807 /* Whatever binding the symbol had before, it's now local. */
809 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
811 /* The dynindx will be set at the end of size_dynamic_sections. */
816 /* Return the dynindex of a local dynamic symbol. */
819 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
823 struct elf_link_local_dynamic_entry
*e
;
825 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
826 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
831 /* This function is used to renumber the dynamic symbols, if some of
832 them are removed because they are marked as local. This is called
833 via elf_link_hash_traverse. */
836 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
839 size_t *count
= (size_t *) data
;
844 if (h
->dynindx
!= -1)
845 h
->dynindx
= ++(*count
);
851 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
852 STB_LOCAL binding. */
855 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
858 size_t *count
= (size_t *) data
;
860 if (!h
->forced_local
)
863 if (h
->dynindx
!= -1)
864 h
->dynindx
= ++(*count
);
869 /* Return true if the dynamic symbol for a given section should be
870 omitted when creating a shared library. */
872 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
873 struct bfd_link_info
*info
,
876 struct elf_link_hash_table
*htab
;
879 switch (elf_section_data (p
)->this_hdr
.sh_type
)
883 /* If sh_type is yet undecided, assume it could be
884 SHT_PROGBITS/SHT_NOBITS. */
886 htab
= elf_hash_table (info
);
887 if (p
== htab
->tls_sec
)
890 if (htab
->text_index_section
!= NULL
)
891 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
893 return (htab
->dynobj
!= NULL
894 && (ip
= bfd_get_linker_section (htab
->dynobj
, p
->name
)) != NULL
895 && ip
->output_section
== p
);
897 /* There shouldn't be section relative relocations
898 against any other section. */
904 /* Assign dynsym indices. In a shared library we generate a section
905 symbol for each output section, which come first. Next come symbols
906 which have been forced to local binding. Then all of the back-end
907 allocated local dynamic syms, followed by the rest of the global
911 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
912 struct bfd_link_info
*info
,
913 unsigned long *section_sym_count
)
915 unsigned long dynsymcount
= 0;
917 if (bfd_link_pic (info
)
918 || elf_hash_table (info
)->is_relocatable_executable
)
920 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
922 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
923 if ((p
->flags
& SEC_EXCLUDE
) == 0
924 && (p
->flags
& SEC_ALLOC
) != 0
925 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
926 elf_section_data (p
)->dynindx
= ++dynsymcount
;
928 elf_section_data (p
)->dynindx
= 0;
930 *section_sym_count
= dynsymcount
;
932 elf_link_hash_traverse (elf_hash_table (info
),
933 elf_link_renumber_local_hash_table_dynsyms
,
936 if (elf_hash_table (info
)->dynlocal
)
938 struct elf_link_local_dynamic_entry
*p
;
939 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
940 p
->dynindx
= ++dynsymcount
;
942 elf_hash_table (info
)->local_dynsymcount
= dynsymcount
;
944 elf_link_hash_traverse (elf_hash_table (info
),
945 elf_link_renumber_hash_table_dynsyms
,
948 /* There is an unused NULL entry at the head of the table which we
949 must account for in our count even if the table is empty since it
950 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
954 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
958 /* Merge st_other field. */
961 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
962 const Elf_Internal_Sym
*isym
, asection
*sec
,
963 bfd_boolean definition
, bfd_boolean dynamic
)
965 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
967 /* If st_other has a processor-specific meaning, specific
968 code might be needed here. */
969 if (bed
->elf_backend_merge_symbol_attribute
)
970 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
975 unsigned symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
976 unsigned hvis
= ELF_ST_VISIBILITY (h
->other
);
978 /* Keep the most constraining visibility. Leave the remainder
979 of the st_other field to elf_backend_merge_symbol_attribute. */
980 if (symvis
- 1 < hvis
- 1)
981 h
->other
= symvis
| (h
->other
& ~ELF_ST_VISIBILITY (-1));
984 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_DEFAULT
985 && (sec
->flags
& SEC_READONLY
) == 0)
986 h
->protected_def
= 1;
989 /* This function is called when we want to merge a new symbol with an
990 existing symbol. It handles the various cases which arise when we
991 find a definition in a dynamic object, or when there is already a
992 definition in a dynamic object. The new symbol is described by
993 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
994 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
995 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
996 of an old common symbol. We set OVERRIDE if the old symbol is
997 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
998 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
999 to change. By OK to change, we mean that we shouldn't warn if the
1000 type or size does change. */
1003 _bfd_elf_merge_symbol (bfd
*abfd
,
1004 struct bfd_link_info
*info
,
1006 Elf_Internal_Sym
*sym
,
1009 struct elf_link_hash_entry
**sym_hash
,
1011 bfd_boolean
*pold_weak
,
1012 unsigned int *pold_alignment
,
1014 bfd_boolean
*override
,
1015 bfd_boolean
*type_change_ok
,
1016 bfd_boolean
*size_change_ok
,
1017 bfd_boolean
*matched
)
1019 asection
*sec
, *oldsec
;
1020 struct elf_link_hash_entry
*h
;
1021 struct elf_link_hash_entry
*hi
;
1022 struct elf_link_hash_entry
*flip
;
1025 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
1026 bfd_boolean newweak
, oldweak
, newfunc
, oldfunc
;
1027 const struct elf_backend_data
*bed
;
1034 bind
= ELF_ST_BIND (sym
->st_info
);
1036 if (! bfd_is_und_section (sec
))
1037 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
1039 h
= ((struct elf_link_hash_entry
*)
1040 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
1045 bed
= get_elf_backend_data (abfd
);
1047 /* NEW_VERSION is the symbol version of the new symbol. */
1048 if (h
->versioned
!= unversioned
)
1050 /* Symbol version is unknown or versioned. */
1051 new_version
= strrchr (name
, ELF_VER_CHR
);
1054 if (h
->versioned
== unknown
)
1056 if (new_version
> name
&& new_version
[-1] != ELF_VER_CHR
)
1057 h
->versioned
= versioned_hidden
;
1059 h
->versioned
= versioned
;
1062 if (new_version
[0] == '\0')
1066 h
->versioned
= unversioned
;
1071 /* For merging, we only care about real symbols. But we need to make
1072 sure that indirect symbol dynamic flags are updated. */
1074 while (h
->root
.type
== bfd_link_hash_indirect
1075 || h
->root
.type
== bfd_link_hash_warning
)
1076 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1080 if (hi
== h
|| h
->root
.type
== bfd_link_hash_new
)
1084 /* OLD_HIDDEN is true if the existing symbol is only visible
1085 to the symbol with the same symbol version. NEW_HIDDEN is
1086 true if the new symbol is only visible to the symbol with
1087 the same symbol version. */
1088 bfd_boolean old_hidden
= h
->versioned
== versioned_hidden
;
1089 bfd_boolean new_hidden
= hi
->versioned
== versioned_hidden
;
1090 if (!old_hidden
&& !new_hidden
)
1091 /* The new symbol matches the existing symbol if both
1096 /* OLD_VERSION is the symbol version of the existing
1100 if (h
->versioned
>= versioned
)
1101 old_version
= strrchr (h
->root
.root
.string
,
1106 /* The new symbol matches the existing symbol if they
1107 have the same symbol version. */
1108 *matched
= (old_version
== new_version
1109 || (old_version
!= NULL
1110 && new_version
!= NULL
1111 && strcmp (old_version
, new_version
) == 0));
1116 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1121 switch (h
->root
.type
)
1126 case bfd_link_hash_undefined
:
1127 case bfd_link_hash_undefweak
:
1128 oldbfd
= h
->root
.u
.undef
.abfd
;
1131 case bfd_link_hash_defined
:
1132 case bfd_link_hash_defweak
:
1133 oldbfd
= h
->root
.u
.def
.section
->owner
;
1134 oldsec
= h
->root
.u
.def
.section
;
1137 case bfd_link_hash_common
:
1138 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1139 oldsec
= h
->root
.u
.c
.p
->section
;
1141 *pold_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1144 if (poldbfd
&& *poldbfd
== NULL
)
1147 /* Differentiate strong and weak symbols. */
1148 newweak
= bind
== STB_WEAK
;
1149 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1150 || h
->root
.type
== bfd_link_hash_undefweak
);
1152 *pold_weak
= oldweak
;
1154 /* This code is for coping with dynamic objects, and is only useful
1155 if we are doing an ELF link. */
1156 if (!(*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
1159 /* We have to check it for every instance since the first few may be
1160 references and not all compilers emit symbol type for undefined
1162 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1164 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1165 respectively, is from a dynamic object. */
1167 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1169 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1170 syms and defined syms in dynamic libraries respectively.
1171 ref_dynamic on the other hand can be set for a symbol defined in
1172 a dynamic library, and def_dynamic may not be set; When the
1173 definition in a dynamic lib is overridden by a definition in the
1174 executable use of the symbol in the dynamic lib becomes a
1175 reference to the executable symbol. */
1178 if (bfd_is_und_section (sec
))
1180 if (bind
!= STB_WEAK
)
1182 h
->ref_dynamic_nonweak
= 1;
1183 hi
->ref_dynamic_nonweak
= 1;
1188 /* Update the existing symbol only if they match. */
1191 hi
->dynamic_def
= 1;
1195 /* If we just created the symbol, mark it as being an ELF symbol.
1196 Other than that, there is nothing to do--there is no merge issue
1197 with a newly defined symbol--so we just return. */
1199 if (h
->root
.type
== bfd_link_hash_new
)
1205 /* In cases involving weak versioned symbols, we may wind up trying
1206 to merge a symbol with itself. Catch that here, to avoid the
1207 confusion that results if we try to override a symbol with
1208 itself. The additional tests catch cases like
1209 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1210 dynamic object, which we do want to handle here. */
1212 && (newweak
|| oldweak
)
1213 && ((abfd
->flags
& DYNAMIC
) == 0
1214 || !h
->def_regular
))
1219 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1220 else if (oldsec
!= NULL
)
1222 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1223 indices used by MIPS ELF. */
1224 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1227 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1228 respectively, appear to be a definition rather than reference. */
1230 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1232 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1233 && h
->root
.type
!= bfd_link_hash_undefweak
1234 && h
->root
.type
!= bfd_link_hash_common
);
1236 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1237 respectively, appear to be a function. */
1239 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1240 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1242 oldfunc
= (h
->type
!= STT_NOTYPE
1243 && bed
->is_function_type (h
->type
));
1245 if (!(newfunc
&& oldfunc
)
1246 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1247 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1248 && h
->type
!= STT_NOTYPE
1249 && (newdef
|| bfd_is_com_section (sec
))
1250 && (olddef
|| h
->root
.type
== bfd_link_hash_common
))
1252 /* If creating a default indirect symbol ("foo" or "foo@") from
1253 a dynamic versioned definition ("foo@@") skip doing so if
1254 there is an existing regular definition with a different
1255 type. We don't want, for example, a "time" variable in the
1256 executable overriding a "time" function in a shared library. */
1264 /* When adding a symbol from a regular object file after we have
1265 created indirect symbols, undo the indirection and any
1272 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1273 h
->forced_local
= 0;
1277 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1279 h
->root
.type
= bfd_link_hash_undefined
;
1280 h
->root
.u
.undef
.abfd
= abfd
;
1284 h
->root
.type
= bfd_link_hash_new
;
1285 h
->root
.u
.undef
.abfd
= NULL
;
1291 /* Check TLS symbols. We don't check undefined symbols introduced
1292 by "ld -u" which have no type (and oldbfd NULL), and we don't
1293 check symbols from plugins because they also have no type. */
1295 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1296 && (abfd
->flags
& BFD_PLUGIN
) == 0
1297 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1298 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1301 bfd_boolean ntdef
, tdef
;
1302 asection
*ntsec
, *tsec
;
1304 if (h
->type
== STT_TLS
)
1325 /* xgettext:c-format */
1326 (_("%s: TLS definition in %B section %A "
1327 "mismatches non-TLS definition in %B section %A"),
1328 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
, ntsec
);
1329 else if (!tdef
&& !ntdef
)
1331 /* xgettext:c-format */
1332 (_("%s: TLS reference in %B "
1333 "mismatches non-TLS reference in %B"),
1334 h
->root
.root
.string
, tbfd
, ntbfd
);
1337 /* xgettext:c-format */
1338 (_("%s: TLS definition in %B section %A "
1339 "mismatches non-TLS reference in %B"),
1340 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
);
1343 /* xgettext:c-format */
1344 (_("%s: TLS reference in %B "
1345 "mismatches non-TLS definition in %B section %A"),
1346 h
->root
.root
.string
, tbfd
, ntbfd
, ntsec
);
1348 bfd_set_error (bfd_error_bad_value
);
1352 /* If the old symbol has non-default visibility, we ignore the new
1353 definition from a dynamic object. */
1355 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1356 && !bfd_is_und_section (sec
))
1359 /* Make sure this symbol is dynamic. */
1361 hi
->ref_dynamic
= 1;
1362 /* A protected symbol has external availability. Make sure it is
1363 recorded as dynamic.
1365 FIXME: Should we check type and size for protected symbol? */
1366 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1367 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1372 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1375 /* If the new symbol with non-default visibility comes from a
1376 relocatable file and the old definition comes from a dynamic
1377 object, we remove the old definition. */
1378 if (hi
->root
.type
== bfd_link_hash_indirect
)
1380 /* Handle the case where the old dynamic definition is
1381 default versioned. We need to copy the symbol info from
1382 the symbol with default version to the normal one if it
1383 was referenced before. */
1386 hi
->root
.type
= h
->root
.type
;
1387 h
->root
.type
= bfd_link_hash_indirect
;
1388 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1390 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1391 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1393 /* If the new symbol is hidden or internal, completely undo
1394 any dynamic link state. */
1395 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1396 h
->forced_local
= 0;
1403 /* FIXME: Should we check type and size for protected symbol? */
1413 /* If the old symbol was undefined before, then it will still be
1414 on the undefs list. If the new symbol is undefined or
1415 common, we can't make it bfd_link_hash_new here, because new
1416 undefined or common symbols will be added to the undefs list
1417 by _bfd_generic_link_add_one_symbol. Symbols may not be
1418 added twice to the undefs list. Also, if the new symbol is
1419 undefweak then we don't want to lose the strong undef. */
1420 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1422 h
->root
.type
= bfd_link_hash_undefined
;
1423 h
->root
.u
.undef
.abfd
= abfd
;
1427 h
->root
.type
= bfd_link_hash_new
;
1428 h
->root
.u
.undef
.abfd
= NULL
;
1431 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1433 /* If the new symbol is hidden or internal, completely undo
1434 any dynamic link state. */
1435 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1436 h
->forced_local
= 0;
1442 /* FIXME: Should we check type and size for protected symbol? */
1448 /* If a new weak symbol definition comes from a regular file and the
1449 old symbol comes from a dynamic library, we treat the new one as
1450 strong. Similarly, an old weak symbol definition from a regular
1451 file is treated as strong when the new symbol comes from a dynamic
1452 library. Further, an old weak symbol from a dynamic library is
1453 treated as strong if the new symbol is from a dynamic library.
1454 This reflects the way glibc's ld.so works.
1456 Do this before setting *type_change_ok or *size_change_ok so that
1457 we warn properly when dynamic library symbols are overridden. */
1459 if (newdef
&& !newdyn
&& olddyn
)
1461 if (olddef
&& newdyn
)
1464 /* Allow changes between different types of function symbol. */
1465 if (newfunc
&& oldfunc
)
1466 *type_change_ok
= TRUE
;
1468 /* It's OK to change the type if either the existing symbol or the
1469 new symbol is weak. A type change is also OK if the old symbol
1470 is undefined and the new symbol is defined. */
1475 && h
->root
.type
== bfd_link_hash_undefined
))
1476 *type_change_ok
= TRUE
;
1478 /* It's OK to change the size if either the existing symbol or the
1479 new symbol is weak, or if the old symbol is undefined. */
1482 || h
->root
.type
== bfd_link_hash_undefined
)
1483 *size_change_ok
= TRUE
;
1485 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1486 symbol, respectively, appears to be a common symbol in a dynamic
1487 object. If a symbol appears in an uninitialized section, and is
1488 not weak, and is not a function, then it may be a common symbol
1489 which was resolved when the dynamic object was created. We want
1490 to treat such symbols specially, because they raise special
1491 considerations when setting the symbol size: if the symbol
1492 appears as a common symbol in a regular object, and the size in
1493 the regular object is larger, we must make sure that we use the
1494 larger size. This problematic case can always be avoided in C,
1495 but it must be handled correctly when using Fortran shared
1498 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1499 likewise for OLDDYNCOMMON and OLDDEF.
1501 Note that this test is just a heuristic, and that it is quite
1502 possible to have an uninitialized symbol in a shared object which
1503 is really a definition, rather than a common symbol. This could
1504 lead to some minor confusion when the symbol really is a common
1505 symbol in some regular object. However, I think it will be
1511 && (sec
->flags
& SEC_ALLOC
) != 0
1512 && (sec
->flags
& SEC_LOAD
) == 0
1515 newdyncommon
= TRUE
;
1517 newdyncommon
= FALSE
;
1521 && h
->root
.type
== bfd_link_hash_defined
1523 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1524 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1527 olddyncommon
= TRUE
;
1529 olddyncommon
= FALSE
;
1531 /* We now know everything about the old and new symbols. We ask the
1532 backend to check if we can merge them. */
1533 if (bed
->merge_symbol
!= NULL
)
1535 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1540 /* If both the old and the new symbols look like common symbols in a
1541 dynamic object, set the size of the symbol to the larger of the
1546 && sym
->st_size
!= h
->size
)
1548 /* Since we think we have two common symbols, issue a multiple
1549 common warning if desired. Note that we only warn if the
1550 size is different. If the size is the same, we simply let
1551 the old symbol override the new one as normally happens with
1552 symbols defined in dynamic objects. */
1554 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1555 bfd_link_hash_common
, sym
->st_size
);
1556 if (sym
->st_size
> h
->size
)
1557 h
->size
= sym
->st_size
;
1559 *size_change_ok
= TRUE
;
1562 /* If we are looking at a dynamic object, and we have found a
1563 definition, we need to see if the symbol was already defined by
1564 some other object. If so, we want to use the existing
1565 definition, and we do not want to report a multiple symbol
1566 definition error; we do this by clobbering *PSEC to be
1567 bfd_und_section_ptr.
1569 We treat a common symbol as a definition if the symbol in the
1570 shared library is a function, since common symbols always
1571 represent variables; this can cause confusion in principle, but
1572 any such confusion would seem to indicate an erroneous program or
1573 shared library. We also permit a common symbol in a regular
1574 object to override a weak symbol in a shared object. */
1579 || (h
->root
.type
== bfd_link_hash_common
1580 && (newweak
|| newfunc
))))
1584 newdyncommon
= FALSE
;
1586 *psec
= sec
= bfd_und_section_ptr
;
1587 *size_change_ok
= TRUE
;
1589 /* If we get here when the old symbol is a common symbol, then
1590 we are explicitly letting it override a weak symbol or
1591 function in a dynamic object, and we don't want to warn about
1592 a type change. If the old symbol is a defined symbol, a type
1593 change warning may still be appropriate. */
1595 if (h
->root
.type
== bfd_link_hash_common
)
1596 *type_change_ok
= TRUE
;
1599 /* Handle the special case of an old common symbol merging with a
1600 new symbol which looks like a common symbol in a shared object.
1601 We change *PSEC and *PVALUE to make the new symbol look like a
1602 common symbol, and let _bfd_generic_link_add_one_symbol do the
1606 && h
->root
.type
== bfd_link_hash_common
)
1610 newdyncommon
= FALSE
;
1611 *pvalue
= sym
->st_size
;
1612 *psec
= sec
= bed
->common_section (oldsec
);
1613 *size_change_ok
= TRUE
;
1616 /* Skip weak definitions of symbols that are already defined. */
1617 if (newdef
&& olddef
&& newweak
)
1619 /* Don't skip new non-IR weak syms. */
1620 if (!(oldbfd
!= NULL
1621 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1622 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1628 /* Merge st_other. If the symbol already has a dynamic index,
1629 but visibility says it should not be visible, turn it into a
1631 elf_merge_st_other (abfd
, h
, sym
, sec
, newdef
, newdyn
);
1632 if (h
->dynindx
!= -1)
1633 switch (ELF_ST_VISIBILITY (h
->other
))
1637 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1642 /* If the old symbol is from a dynamic object, and the new symbol is
1643 a definition which is not from a dynamic object, then the new
1644 symbol overrides the old symbol. Symbols from regular files
1645 always take precedence over symbols from dynamic objects, even if
1646 they are defined after the dynamic object in the link.
1648 As above, we again permit a common symbol in a regular object to
1649 override a definition in a shared object if the shared object
1650 symbol is a function or is weak. */
1655 || (bfd_is_com_section (sec
)
1656 && (oldweak
|| oldfunc
)))
1661 /* Change the hash table entry to undefined, and let
1662 _bfd_generic_link_add_one_symbol do the right thing with the
1665 h
->root
.type
= bfd_link_hash_undefined
;
1666 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1667 *size_change_ok
= TRUE
;
1670 olddyncommon
= FALSE
;
1672 /* We again permit a type change when a common symbol may be
1673 overriding a function. */
1675 if (bfd_is_com_section (sec
))
1679 /* If a common symbol overrides a function, make sure
1680 that it isn't defined dynamically nor has type
1683 h
->type
= STT_NOTYPE
;
1685 *type_change_ok
= TRUE
;
1688 if (hi
->root
.type
== bfd_link_hash_indirect
)
1691 /* This union may have been set to be non-NULL when this symbol
1692 was seen in a dynamic object. We must force the union to be
1693 NULL, so that it is correct for a regular symbol. */
1694 h
->verinfo
.vertree
= NULL
;
1697 /* Handle the special case of a new common symbol merging with an
1698 old symbol that looks like it might be a common symbol defined in
1699 a shared object. Note that we have already handled the case in
1700 which a new common symbol should simply override the definition
1701 in the shared library. */
1704 && bfd_is_com_section (sec
)
1707 /* It would be best if we could set the hash table entry to a
1708 common symbol, but we don't know what to use for the section
1709 or the alignment. */
1710 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1711 bfd_link_hash_common
, sym
->st_size
);
1713 /* If the presumed common symbol in the dynamic object is
1714 larger, pretend that the new symbol has its size. */
1716 if (h
->size
> *pvalue
)
1719 /* We need to remember the alignment required by the symbol
1720 in the dynamic object. */
1721 BFD_ASSERT (pold_alignment
);
1722 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1725 olddyncommon
= FALSE
;
1727 h
->root
.type
= bfd_link_hash_undefined
;
1728 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1730 *size_change_ok
= TRUE
;
1731 *type_change_ok
= TRUE
;
1733 if (hi
->root
.type
== bfd_link_hash_indirect
)
1736 h
->verinfo
.vertree
= NULL
;
1741 /* Handle the case where we had a versioned symbol in a dynamic
1742 library and now find a definition in a normal object. In this
1743 case, we make the versioned symbol point to the normal one. */
1744 flip
->root
.type
= h
->root
.type
;
1745 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1746 h
->root
.type
= bfd_link_hash_indirect
;
1747 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1748 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1752 flip
->ref_dynamic
= 1;
1759 /* This function is called to create an indirect symbol from the
1760 default for the symbol with the default version if needed. The
1761 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1762 set DYNSYM if the new indirect symbol is dynamic. */
1765 _bfd_elf_add_default_symbol (bfd
*abfd
,
1766 struct bfd_link_info
*info
,
1767 struct elf_link_hash_entry
*h
,
1769 Elf_Internal_Sym
*sym
,
1773 bfd_boolean
*dynsym
)
1775 bfd_boolean type_change_ok
;
1776 bfd_boolean size_change_ok
;
1779 struct elf_link_hash_entry
*hi
;
1780 struct bfd_link_hash_entry
*bh
;
1781 const struct elf_backend_data
*bed
;
1782 bfd_boolean collect
;
1783 bfd_boolean dynamic
;
1784 bfd_boolean override
;
1786 size_t len
, shortlen
;
1788 bfd_boolean matched
;
1790 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1793 /* If this symbol has a version, and it is the default version, we
1794 create an indirect symbol from the default name to the fully
1795 decorated name. This will cause external references which do not
1796 specify a version to be bound to this version of the symbol. */
1797 p
= strchr (name
, ELF_VER_CHR
);
1798 if (h
->versioned
== unknown
)
1802 h
->versioned
= unversioned
;
1807 if (p
[1] != ELF_VER_CHR
)
1809 h
->versioned
= versioned_hidden
;
1813 h
->versioned
= versioned
;
1818 /* PR ld/19073: We may see an unversioned definition after the
1824 bed
= get_elf_backend_data (abfd
);
1825 collect
= bed
->collect
;
1826 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1828 shortlen
= p
- name
;
1829 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1830 if (shortname
== NULL
)
1832 memcpy (shortname
, name
, shortlen
);
1833 shortname
[shortlen
] = '\0';
1835 /* We are going to create a new symbol. Merge it with any existing
1836 symbol with this name. For the purposes of the merge, act as
1837 though we were defining the symbol we just defined, although we
1838 actually going to define an indirect symbol. */
1839 type_change_ok
= FALSE
;
1840 size_change_ok
= FALSE
;
1843 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1844 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1845 &type_change_ok
, &size_change_ok
, &matched
))
1851 if (hi
->def_regular
)
1853 /* If the undecorated symbol will have a version added by a
1854 script different to H, then don't indirect to/from the
1855 undecorated symbol. This isn't ideal because we may not yet
1856 have seen symbol versions, if given by a script on the
1857 command line rather than via --version-script. */
1858 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
1863 = bfd_find_version_for_sym (info
->version_info
,
1864 hi
->root
.root
.string
, &hide
);
1865 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
1867 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
1871 if (hi
->verinfo
.vertree
!= NULL
1872 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
1878 /* Add the default symbol if not performing a relocatable link. */
1879 if (! bfd_link_relocatable (info
))
1882 if (! (_bfd_generic_link_add_one_symbol
1883 (info
, abfd
, shortname
, BSF_INDIRECT
,
1884 bfd_ind_section_ptr
,
1885 0, name
, FALSE
, collect
, &bh
)))
1887 hi
= (struct elf_link_hash_entry
*) bh
;
1892 /* In this case the symbol named SHORTNAME is overriding the
1893 indirect symbol we want to add. We were planning on making
1894 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1895 is the name without a version. NAME is the fully versioned
1896 name, and it is the default version.
1898 Overriding means that we already saw a definition for the
1899 symbol SHORTNAME in a regular object, and it is overriding
1900 the symbol defined in the dynamic object.
1902 When this happens, we actually want to change NAME, the
1903 symbol we just added, to refer to SHORTNAME. This will cause
1904 references to NAME in the shared object to become references
1905 to SHORTNAME in the regular object. This is what we expect
1906 when we override a function in a shared object: that the
1907 references in the shared object will be mapped to the
1908 definition in the regular object. */
1910 while (hi
->root
.type
== bfd_link_hash_indirect
1911 || hi
->root
.type
== bfd_link_hash_warning
)
1912 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1914 h
->root
.type
= bfd_link_hash_indirect
;
1915 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1919 hi
->ref_dynamic
= 1;
1923 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1928 /* Now set HI to H, so that the following code will set the
1929 other fields correctly. */
1933 /* Check if HI is a warning symbol. */
1934 if (hi
->root
.type
== bfd_link_hash_warning
)
1935 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1937 /* If there is a duplicate definition somewhere, then HI may not
1938 point to an indirect symbol. We will have reported an error to
1939 the user in that case. */
1941 if (hi
->root
.type
== bfd_link_hash_indirect
)
1943 struct elf_link_hash_entry
*ht
;
1945 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1946 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
1948 /* A reference to the SHORTNAME symbol from a dynamic library
1949 will be satisfied by the versioned symbol at runtime. In
1950 effect, we have a reference to the versioned symbol. */
1951 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
1952 hi
->dynamic_def
|= ht
->dynamic_def
;
1954 /* See if the new flags lead us to realize that the symbol must
1960 if (! bfd_link_executable (info
)
1967 if (hi
->ref_regular
)
1973 /* We also need to define an indirection from the nondefault version
1977 len
= strlen (name
);
1978 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
1979 if (shortname
== NULL
)
1981 memcpy (shortname
, name
, shortlen
);
1982 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1984 /* Once again, merge with any existing symbol. */
1985 type_change_ok
= FALSE
;
1986 size_change_ok
= FALSE
;
1988 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1989 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1990 &type_change_ok
, &size_change_ok
, &matched
))
1998 /* Here SHORTNAME is a versioned name, so we don't expect to see
1999 the type of override we do in the case above unless it is
2000 overridden by a versioned definition. */
2001 if (hi
->root
.type
!= bfd_link_hash_defined
2002 && hi
->root
.type
!= bfd_link_hash_defweak
)
2004 /* xgettext:c-format */
2005 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
2011 if (! (_bfd_generic_link_add_one_symbol
2012 (info
, abfd
, shortname
, BSF_INDIRECT
,
2013 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
2015 hi
= (struct elf_link_hash_entry
*) bh
;
2017 /* If there is a duplicate definition somewhere, then HI may not
2018 point to an indirect symbol. We will have reported an error
2019 to the user in that case. */
2021 if (hi
->root
.type
== bfd_link_hash_indirect
)
2023 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
2024 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2025 hi
->dynamic_def
|= h
->dynamic_def
;
2027 /* See if the new flags lead us to realize that the symbol
2033 if (! bfd_link_executable (info
)
2039 if (hi
->ref_regular
)
2049 /* This routine is used to export all defined symbols into the dynamic
2050 symbol table. It is called via elf_link_hash_traverse. */
2053 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
2055 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2057 /* Ignore indirect symbols. These are added by the versioning code. */
2058 if (h
->root
.type
== bfd_link_hash_indirect
)
2061 /* Ignore this if we won't export it. */
2062 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2065 if (h
->dynindx
== -1
2066 && (h
->def_regular
|| h
->ref_regular
)
2067 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2068 h
->root
.root
.string
))
2070 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2080 /* Look through the symbols which are defined in other shared
2081 libraries and referenced here. Update the list of version
2082 dependencies. This will be put into the .gnu.version_r section.
2083 This function is called via elf_link_hash_traverse. */
2086 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2089 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2090 Elf_Internal_Verneed
*t
;
2091 Elf_Internal_Vernaux
*a
;
2094 /* We only care about symbols defined in shared objects with version
2099 || h
->verinfo
.verdef
== NULL
2100 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2101 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2104 /* See if we already know about this version. */
2105 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2109 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2112 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2113 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2119 /* This is a new version. Add it to tree we are building. */
2124 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2127 rinfo
->failed
= TRUE
;
2131 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2132 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2133 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2137 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2140 rinfo
->failed
= TRUE
;
2144 /* Note that we are copying a string pointer here, and testing it
2145 above. If bfd_elf_string_from_elf_section is ever changed to
2146 discard the string data when low in memory, this will have to be
2148 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2150 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2151 a
->vna_nextptr
= t
->vn_auxptr
;
2153 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2156 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2163 /* Figure out appropriate versions for all the symbols. We may not
2164 have the version number script until we have read all of the input
2165 files, so until that point we don't know which symbols should be
2166 local. This function is called via elf_link_hash_traverse. */
2169 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2171 struct elf_info_failed
*sinfo
;
2172 struct bfd_link_info
*info
;
2173 const struct elf_backend_data
*bed
;
2174 struct elf_info_failed eif
;
2177 sinfo
= (struct elf_info_failed
*) data
;
2180 /* Fix the symbol flags. */
2183 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2186 sinfo
->failed
= TRUE
;
2190 /* We only need version numbers for symbols defined in regular
2192 if (!h
->def_regular
)
2195 bed
= get_elf_backend_data (info
->output_bfd
);
2196 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2197 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2199 struct bfd_elf_version_tree
*t
;
2202 if (*p
== ELF_VER_CHR
)
2205 /* If there is no version string, we can just return out. */
2209 /* Look for the version. If we find it, it is no longer weak. */
2210 for (t
= sinfo
->info
->version_info
; t
!= NULL
; t
= t
->next
)
2212 if (strcmp (t
->name
, p
) == 0)
2216 struct bfd_elf_version_expr
*d
;
2218 len
= p
- h
->root
.root
.string
;
2219 alc
= (char *) bfd_malloc (len
);
2222 sinfo
->failed
= TRUE
;
2225 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2226 alc
[len
- 1] = '\0';
2227 if (alc
[len
- 2] == ELF_VER_CHR
)
2228 alc
[len
- 2] = '\0';
2230 h
->verinfo
.vertree
= t
;
2234 if (t
->globals
.list
!= NULL
)
2235 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2237 /* See if there is anything to force this symbol to
2239 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2241 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2244 && ! info
->export_dynamic
)
2245 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2253 /* If we are building an application, we need to create a
2254 version node for this version. */
2255 if (t
== NULL
&& bfd_link_executable (info
))
2257 struct bfd_elf_version_tree
**pp
;
2260 /* If we aren't going to export this symbol, we don't need
2261 to worry about it. */
2262 if (h
->dynindx
== -1)
2265 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2269 sinfo
->failed
= TRUE
;
2274 t
->name_indx
= (unsigned int) -1;
2278 /* Don't count anonymous version tag. */
2279 if (sinfo
->info
->version_info
!= NULL
2280 && sinfo
->info
->version_info
->vernum
== 0)
2282 for (pp
= &sinfo
->info
->version_info
;
2286 t
->vernum
= version_index
;
2290 h
->verinfo
.vertree
= t
;
2294 /* We could not find the version for a symbol when
2295 generating a shared archive. Return an error. */
2297 /* xgettext:c-format */
2298 (_("%B: version node not found for symbol %s"),
2299 info
->output_bfd
, h
->root
.root
.string
);
2300 bfd_set_error (bfd_error_bad_value
);
2301 sinfo
->failed
= TRUE
;
2306 /* If we don't have a version for this symbol, see if we can find
2308 if (h
->verinfo
.vertree
== NULL
&& sinfo
->info
->version_info
!= NULL
)
2313 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2314 h
->root
.root
.string
, &hide
);
2315 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2316 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2322 /* Read and swap the relocs from the section indicated by SHDR. This
2323 may be either a REL or a RELA section. The relocations are
2324 translated into RELA relocations and stored in INTERNAL_RELOCS,
2325 which should have already been allocated to contain enough space.
2326 The EXTERNAL_RELOCS are a buffer where the external form of the
2327 relocations should be stored.
2329 Returns FALSE if something goes wrong. */
2332 elf_link_read_relocs_from_section (bfd
*abfd
,
2334 Elf_Internal_Shdr
*shdr
,
2335 void *external_relocs
,
2336 Elf_Internal_Rela
*internal_relocs
)
2338 const struct elf_backend_data
*bed
;
2339 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2340 const bfd_byte
*erela
;
2341 const bfd_byte
*erelaend
;
2342 Elf_Internal_Rela
*irela
;
2343 Elf_Internal_Shdr
*symtab_hdr
;
2346 /* Position ourselves at the start of the section. */
2347 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2350 /* Read the relocations. */
2351 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2354 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2355 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2357 bed
= get_elf_backend_data (abfd
);
2359 /* Convert the external relocations to the internal format. */
2360 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2361 swap_in
= bed
->s
->swap_reloc_in
;
2362 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2363 swap_in
= bed
->s
->swap_reloca_in
;
2366 bfd_set_error (bfd_error_wrong_format
);
2370 erela
= (const bfd_byte
*) external_relocs
;
2371 erelaend
= erela
+ shdr
->sh_size
;
2372 irela
= internal_relocs
;
2373 while (erela
< erelaend
)
2377 (*swap_in
) (abfd
, erela
, irela
);
2378 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2379 if (bed
->s
->arch_size
== 64)
2383 if ((size_t) r_symndx
>= nsyms
)
2386 /* xgettext:c-format */
2387 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2388 " for offset 0x%lx in section `%A'"),
2389 abfd
, (unsigned long) r_symndx
, (unsigned long) nsyms
,
2390 irela
->r_offset
, sec
);
2391 bfd_set_error (bfd_error_bad_value
);
2395 else if (r_symndx
!= STN_UNDEF
)
2398 /* xgettext:c-format */
2399 (_("%B: non-zero symbol index (0x%lx)"
2400 " for offset 0x%lx in section `%A'"
2401 " when the object file has no symbol table"),
2402 abfd
, (unsigned long) r_symndx
, (unsigned long) nsyms
,
2403 irela
->r_offset
, sec
);
2404 bfd_set_error (bfd_error_bad_value
);
2407 irela
+= bed
->s
->int_rels_per_ext_rel
;
2408 erela
+= shdr
->sh_entsize
;
2414 /* Read and swap the relocs for a section O. They may have been
2415 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2416 not NULL, they are used as buffers to read into. They are known to
2417 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2418 the return value is allocated using either malloc or bfd_alloc,
2419 according to the KEEP_MEMORY argument. If O has two relocation
2420 sections (both REL and RELA relocations), then the REL_HDR
2421 relocations will appear first in INTERNAL_RELOCS, followed by the
2422 RELA_HDR relocations. */
2425 _bfd_elf_link_read_relocs (bfd
*abfd
,
2427 void *external_relocs
,
2428 Elf_Internal_Rela
*internal_relocs
,
2429 bfd_boolean keep_memory
)
2431 void *alloc1
= NULL
;
2432 Elf_Internal_Rela
*alloc2
= NULL
;
2433 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2434 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2435 Elf_Internal_Rela
*internal_rela_relocs
;
2437 if (esdo
->relocs
!= NULL
)
2438 return esdo
->relocs
;
2440 if (o
->reloc_count
== 0)
2443 if (internal_relocs
== NULL
)
2447 size
= o
->reloc_count
;
2448 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2450 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2452 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2453 if (internal_relocs
== NULL
)
2457 if (external_relocs
== NULL
)
2459 bfd_size_type size
= 0;
2462 size
+= esdo
->rel
.hdr
->sh_size
;
2464 size
+= esdo
->rela
.hdr
->sh_size
;
2466 alloc1
= bfd_malloc (size
);
2469 external_relocs
= alloc1
;
2472 internal_rela_relocs
= internal_relocs
;
2475 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2479 external_relocs
= (((bfd_byte
*) external_relocs
)
2480 + esdo
->rel
.hdr
->sh_size
);
2481 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2482 * bed
->s
->int_rels_per_ext_rel
);
2486 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2488 internal_rela_relocs
)))
2491 /* Cache the results for next time, if we can. */
2493 esdo
->relocs
= internal_relocs
;
2498 /* Don't free alloc2, since if it was allocated we are passing it
2499 back (under the name of internal_relocs). */
2501 return internal_relocs
;
2509 bfd_release (abfd
, alloc2
);
2516 /* Compute the size of, and allocate space for, REL_HDR which is the
2517 section header for a section containing relocations for O. */
2520 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2521 struct bfd_elf_section_reloc_data
*reldata
)
2523 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2525 /* That allows us to calculate the size of the section. */
2526 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2528 /* The contents field must last into write_object_contents, so we
2529 allocate it with bfd_alloc rather than malloc. Also since we
2530 cannot be sure that the contents will actually be filled in,
2531 we zero the allocated space. */
2532 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2533 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2536 if (reldata
->hashes
== NULL
&& reldata
->count
)
2538 struct elf_link_hash_entry
**p
;
2540 p
= ((struct elf_link_hash_entry
**)
2541 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2545 reldata
->hashes
= p
;
2551 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2552 originated from the section given by INPUT_REL_HDR) to the
2556 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2557 asection
*input_section
,
2558 Elf_Internal_Shdr
*input_rel_hdr
,
2559 Elf_Internal_Rela
*internal_relocs
,
2560 struct elf_link_hash_entry
**rel_hash
2563 Elf_Internal_Rela
*irela
;
2564 Elf_Internal_Rela
*irelaend
;
2566 struct bfd_elf_section_reloc_data
*output_reldata
;
2567 asection
*output_section
;
2568 const struct elf_backend_data
*bed
;
2569 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2570 struct bfd_elf_section_data
*esdo
;
2572 output_section
= input_section
->output_section
;
2574 bed
= get_elf_backend_data (output_bfd
);
2575 esdo
= elf_section_data (output_section
);
2576 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2578 output_reldata
= &esdo
->rel
;
2579 swap_out
= bed
->s
->swap_reloc_out
;
2581 else if (esdo
->rela
.hdr
2582 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2584 output_reldata
= &esdo
->rela
;
2585 swap_out
= bed
->s
->swap_reloca_out
;
2590 /* xgettext:c-format */
2591 (_("%B: relocation size mismatch in %B section %A"),
2592 output_bfd
, input_section
->owner
, input_section
);
2593 bfd_set_error (bfd_error_wrong_format
);
2597 erel
= output_reldata
->hdr
->contents
;
2598 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2599 irela
= internal_relocs
;
2600 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2601 * bed
->s
->int_rels_per_ext_rel
);
2602 while (irela
< irelaend
)
2604 (*swap_out
) (output_bfd
, irela
, erel
);
2605 irela
+= bed
->s
->int_rels_per_ext_rel
;
2606 erel
+= input_rel_hdr
->sh_entsize
;
2609 /* Bump the counter, so that we know where to add the next set of
2611 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2616 /* Make weak undefined symbols in PIE dynamic. */
2619 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2620 struct elf_link_hash_entry
*h
)
2622 if (bfd_link_pie (info
)
2624 && h
->root
.type
== bfd_link_hash_undefweak
)
2625 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2630 /* Fix up the flags for a symbol. This handles various cases which
2631 can only be fixed after all the input files are seen. This is
2632 currently called by both adjust_dynamic_symbol and
2633 assign_sym_version, which is unnecessary but perhaps more robust in
2634 the face of future changes. */
2637 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2638 struct elf_info_failed
*eif
)
2640 const struct elf_backend_data
*bed
;
2642 /* If this symbol was mentioned in a non-ELF file, try to set
2643 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2644 permit a non-ELF file to correctly refer to a symbol defined in
2645 an ELF dynamic object. */
2648 while (h
->root
.type
== bfd_link_hash_indirect
)
2649 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2651 if (h
->root
.type
!= bfd_link_hash_defined
2652 && h
->root
.type
!= bfd_link_hash_defweak
)
2655 h
->ref_regular_nonweak
= 1;
2659 if (h
->root
.u
.def
.section
->owner
!= NULL
2660 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2661 == bfd_target_elf_flavour
))
2664 h
->ref_regular_nonweak
= 1;
2670 if (h
->dynindx
== -1
2674 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2683 /* Unfortunately, NON_ELF is only correct if the symbol
2684 was first seen in a non-ELF file. Fortunately, if the symbol
2685 was first seen in an ELF file, we're probably OK unless the
2686 symbol was defined in a non-ELF file. Catch that case here.
2687 FIXME: We're still in trouble if the symbol was first seen in
2688 a dynamic object, and then later in a non-ELF regular object. */
2689 if ((h
->root
.type
== bfd_link_hash_defined
2690 || h
->root
.type
== bfd_link_hash_defweak
)
2692 && (h
->root
.u
.def
.section
->owner
!= NULL
2693 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2694 != bfd_target_elf_flavour
)
2695 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2696 && !h
->def_dynamic
)))
2700 /* Backend specific symbol fixup. */
2701 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2702 if (bed
->elf_backend_fixup_symbol
2703 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2706 /* If this is a final link, and the symbol was defined as a common
2707 symbol in a regular object file, and there was no definition in
2708 any dynamic object, then the linker will have allocated space for
2709 the symbol in a common section but the DEF_REGULAR
2710 flag will not have been set. */
2711 if (h
->root
.type
== bfd_link_hash_defined
2715 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
2718 /* If a weak undefined symbol has non-default visibility, we also
2719 hide it from the dynamic linker. */
2720 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2721 && h
->root
.type
== bfd_link_hash_undefweak
)
2722 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2724 /* A hidden versioned symbol in executable should be forced local if
2725 it is is locally defined, not referenced by shared library and not
2727 else if (bfd_link_executable (eif
->info
)
2728 && h
->versioned
== versioned_hidden
2729 && !eif
->info
->export_dynamic
2733 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2735 /* If -Bsymbolic was used (which means to bind references to global
2736 symbols to the definition within the shared object), and this
2737 symbol was defined in a regular object, then it actually doesn't
2738 need a PLT entry. Likewise, if the symbol has non-default
2739 visibility. If the symbol has hidden or internal visibility, we
2740 will force it local. */
2741 else if (h
->needs_plt
2742 && bfd_link_pic (eif
->info
)
2743 && is_elf_hash_table (eif
->info
->hash
)
2744 && (SYMBOLIC_BIND (eif
->info
, h
)
2745 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2748 bfd_boolean force_local
;
2750 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2751 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2752 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2755 /* If this is a weak defined symbol in a dynamic object, and we know
2756 the real definition in the dynamic object, copy interesting flags
2757 over to the real definition. */
2758 if (h
->u
.weakdef
!= NULL
)
2760 /* If the real definition is defined by a regular object file,
2761 don't do anything special. See the longer description in
2762 _bfd_elf_adjust_dynamic_symbol, below. */
2763 if (h
->u
.weakdef
->def_regular
)
2764 h
->u
.weakdef
= NULL
;
2767 struct elf_link_hash_entry
*weakdef
= h
->u
.weakdef
;
2769 while (h
->root
.type
== bfd_link_hash_indirect
)
2770 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2772 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2773 || h
->root
.type
== bfd_link_hash_defweak
);
2774 BFD_ASSERT (weakdef
->def_dynamic
);
2775 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2776 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2777 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
, h
);
2784 /* Make the backend pick a good value for a dynamic symbol. This is
2785 called via elf_link_hash_traverse, and also calls itself
2789 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2791 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2793 const struct elf_backend_data
*bed
;
2795 if (! is_elf_hash_table (eif
->info
->hash
))
2798 /* Ignore indirect symbols. These are added by the versioning code. */
2799 if (h
->root
.type
== bfd_link_hash_indirect
)
2802 /* Fix the symbol flags. */
2803 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2806 if (h
->root
.type
== bfd_link_hash_undefweak
)
2808 if (eif
->info
->dynamic_undefined_weak
== 0)
2809 _bfd_elf_link_hash_hide_symbol (eif
->info
, h
, TRUE
);
2810 else if (eif
->info
->dynamic_undefined_weak
> 0
2812 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2813 && !bfd_hide_sym_by_version (eif
->info
->version_info
,
2814 h
->root
.root
.string
))
2816 if (!bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2824 /* If this symbol does not require a PLT entry, and it is not
2825 defined by a dynamic object, or is not referenced by a regular
2826 object, ignore it. We do have to handle a weak defined symbol,
2827 even if no regular object refers to it, if we decided to add it
2828 to the dynamic symbol table. FIXME: Do we normally need to worry
2829 about symbols which are defined by one dynamic object and
2830 referenced by another one? */
2832 && h
->type
!= STT_GNU_IFUNC
2836 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2838 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2842 /* If we've already adjusted this symbol, don't do it again. This
2843 can happen via a recursive call. */
2844 if (h
->dynamic_adjusted
)
2847 /* Don't look at this symbol again. Note that we must set this
2848 after checking the above conditions, because we may look at a
2849 symbol once, decide not to do anything, and then get called
2850 recursively later after REF_REGULAR is set below. */
2851 h
->dynamic_adjusted
= 1;
2853 /* If this is a weak definition, and we know a real definition, and
2854 the real symbol is not itself defined by a regular object file,
2855 then get a good value for the real definition. We handle the
2856 real symbol first, for the convenience of the backend routine.
2858 Note that there is a confusing case here. If the real definition
2859 is defined by a regular object file, we don't get the real symbol
2860 from the dynamic object, but we do get the weak symbol. If the
2861 processor backend uses a COPY reloc, then if some routine in the
2862 dynamic object changes the real symbol, we will not see that
2863 change in the corresponding weak symbol. This is the way other
2864 ELF linkers work as well, and seems to be a result of the shared
2867 I will clarify this issue. Most SVR4 shared libraries define the
2868 variable _timezone and define timezone as a weak synonym. The
2869 tzset call changes _timezone. If you write
2870 extern int timezone;
2872 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2873 you might expect that, since timezone is a synonym for _timezone,
2874 the same number will print both times. However, if the processor
2875 backend uses a COPY reloc, then actually timezone will be copied
2876 into your process image, and, since you define _timezone
2877 yourself, _timezone will not. Thus timezone and _timezone will
2878 wind up at different memory locations. The tzset call will set
2879 _timezone, leaving timezone unchanged. */
2881 if (h
->u
.weakdef
!= NULL
)
2883 /* If we get to this point, there is an implicit reference to
2884 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2885 h
->u
.weakdef
->ref_regular
= 1;
2887 /* Ensure that the backend adjust_dynamic_symbol function sees
2888 H->U.WEAKDEF before H by recursively calling ourselves. */
2889 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2893 /* If a symbol has no type and no size and does not require a PLT
2894 entry, then we are probably about to do the wrong thing here: we
2895 are probably going to create a COPY reloc for an empty object.
2896 This case can arise when a shared object is built with assembly
2897 code, and the assembly code fails to set the symbol type. */
2899 && h
->type
== STT_NOTYPE
2902 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2903 h
->root
.root
.string
);
2905 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2906 bed
= get_elf_backend_data (dynobj
);
2908 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2917 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2921 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
2922 struct elf_link_hash_entry
*h
,
2925 unsigned int power_of_two
;
2927 asection
*sec
= h
->root
.u
.def
.section
;
2929 /* The section aligment of definition is the maximum alignment
2930 requirement of symbols defined in the section. Since we don't
2931 know the symbol alignment requirement, we start with the
2932 maximum alignment and check low bits of the symbol address
2933 for the minimum alignment. */
2934 power_of_two
= bfd_get_section_alignment (sec
->owner
, sec
);
2935 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
2936 while ((h
->root
.u
.def
.value
& mask
) != 0)
2942 if (power_of_two
> bfd_get_section_alignment (dynbss
->owner
,
2945 /* Adjust the section alignment if needed. */
2946 if (! bfd_set_section_alignment (dynbss
->owner
, dynbss
,
2951 /* We make sure that the symbol will be aligned properly. */
2952 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
2954 /* Define the symbol as being at this point in DYNBSS. */
2955 h
->root
.u
.def
.section
= dynbss
;
2956 h
->root
.u
.def
.value
= dynbss
->size
;
2958 /* Increment the size of DYNBSS to make room for the symbol. */
2959 dynbss
->size
+= h
->size
;
2961 /* No error if extern_protected_data is true. */
2962 if (h
->protected_def
2963 && (!info
->extern_protected_data
2964 || (info
->extern_protected_data
< 0
2965 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
2966 info
->callbacks
->einfo
2967 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2968 h
->root
.root
.string
);
2973 /* Adjust all external symbols pointing into SEC_MERGE sections
2974 to reflect the object merging within the sections. */
2977 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2981 if ((h
->root
.type
== bfd_link_hash_defined
2982 || h
->root
.type
== bfd_link_hash_defweak
)
2983 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2984 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
2986 bfd
*output_bfd
= (bfd
*) data
;
2988 h
->root
.u
.def
.value
=
2989 _bfd_merged_section_offset (output_bfd
,
2990 &h
->root
.u
.def
.section
,
2991 elf_section_data (sec
)->sec_info
,
2992 h
->root
.u
.def
.value
);
2998 /* Returns false if the symbol referred to by H should be considered
2999 to resolve local to the current module, and true if it should be
3000 considered to bind dynamically. */
3003 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
3004 struct bfd_link_info
*info
,
3005 bfd_boolean not_local_protected
)
3007 bfd_boolean binding_stays_local_p
;
3008 const struct elf_backend_data
*bed
;
3009 struct elf_link_hash_table
*hash_table
;
3014 while (h
->root
.type
== bfd_link_hash_indirect
3015 || h
->root
.type
== bfd_link_hash_warning
)
3016 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3018 /* If it was forced local, then clearly it's not dynamic. */
3019 if (h
->dynindx
== -1)
3021 if (h
->forced_local
)
3024 /* Identify the cases where name binding rules say that a
3025 visible symbol resolves locally. */
3026 binding_stays_local_p
= (bfd_link_executable (info
)
3027 || SYMBOLIC_BIND (info
, h
));
3029 switch (ELF_ST_VISIBILITY (h
->other
))
3036 hash_table
= elf_hash_table (info
);
3037 if (!is_elf_hash_table (hash_table
))
3040 bed
= get_elf_backend_data (hash_table
->dynobj
);
3042 /* Proper resolution for function pointer equality may require
3043 that these symbols perhaps be resolved dynamically, even though
3044 we should be resolving them to the current module. */
3045 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
3046 binding_stays_local_p
= TRUE
;
3053 /* If it isn't defined locally, then clearly it's dynamic. */
3054 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
3057 /* Otherwise, the symbol is dynamic if binding rules don't tell
3058 us that it remains local. */
3059 return !binding_stays_local_p
;
3062 /* Return true if the symbol referred to by H should be considered
3063 to resolve local to the current module, and false otherwise. Differs
3064 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3065 undefined symbols. The two functions are virtually identical except
3066 for the place where forced_local and dynindx == -1 are tested. If
3067 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
3068 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
3069 the symbol is local only for defined symbols.
3070 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3071 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3072 treatment of undefined weak symbols. For those that do not make
3073 undefined weak symbols dynamic, both functions may return false. */
3076 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3077 struct bfd_link_info
*info
,
3078 bfd_boolean local_protected
)
3080 const struct elf_backend_data
*bed
;
3081 struct elf_link_hash_table
*hash_table
;
3083 /* If it's a local sym, of course we resolve locally. */
3087 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3088 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3089 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3092 /* Common symbols that become definitions don't get the DEF_REGULAR
3093 flag set, so test it first, and don't bail out. */
3094 if (ELF_COMMON_DEF_P (h
))
3096 /* If we don't have a definition in a regular file, then we can't
3097 resolve locally. The sym is either undefined or dynamic. */
3098 else if (!h
->def_regular
)
3101 /* Forced local symbols resolve locally. */
3102 if (h
->forced_local
)
3105 /* As do non-dynamic symbols. */
3106 if (h
->dynindx
== -1)
3109 /* At this point, we know the symbol is defined and dynamic. In an
3110 executable it must resolve locally, likewise when building symbolic
3111 shared libraries. */
3112 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3115 /* Now deal with defined dynamic symbols in shared libraries. Ones
3116 with default visibility might not resolve locally. */
3117 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3120 hash_table
= elf_hash_table (info
);
3121 if (!is_elf_hash_table (hash_table
))
3124 bed
= get_elf_backend_data (hash_table
->dynobj
);
3126 /* If extern_protected_data is false, STV_PROTECTED non-function
3127 symbols are local. */
3128 if ((!info
->extern_protected_data
3129 || (info
->extern_protected_data
< 0
3130 && !bed
->extern_protected_data
))
3131 && !bed
->is_function_type (h
->type
))
3134 /* Function pointer equality tests may require that STV_PROTECTED
3135 symbols be treated as dynamic symbols. If the address of a
3136 function not defined in an executable is set to that function's
3137 plt entry in the executable, then the address of the function in
3138 a shared library must also be the plt entry in the executable. */
3139 return local_protected
;
3142 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3143 aligned. Returns the first TLS output section. */
3145 struct bfd_section
*
3146 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3148 struct bfd_section
*sec
, *tls
;
3149 unsigned int align
= 0;
3151 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3152 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3156 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3157 if (sec
->alignment_power
> align
)
3158 align
= sec
->alignment_power
;
3160 elf_hash_table (info
)->tls_sec
= tls
;
3162 /* Ensure the alignment of the first section is the largest alignment,
3163 so that the tls segment starts aligned. */
3165 tls
->alignment_power
= align
;
3170 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3172 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3173 Elf_Internal_Sym
*sym
)
3175 const struct elf_backend_data
*bed
;
3177 /* Local symbols do not count, but target specific ones might. */
3178 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3179 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3182 bed
= get_elf_backend_data (abfd
);
3183 /* Function symbols do not count. */
3184 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3187 /* If the section is undefined, then so is the symbol. */
3188 if (sym
->st_shndx
== SHN_UNDEF
)
3191 /* If the symbol is defined in the common section, then
3192 it is a common definition and so does not count. */
3193 if (bed
->common_definition (sym
))
3196 /* If the symbol is in a target specific section then we
3197 must rely upon the backend to tell us what it is. */
3198 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3199 /* FIXME - this function is not coded yet:
3201 return _bfd_is_global_symbol_definition (abfd, sym);
3203 Instead for now assume that the definition is not global,
3204 Even if this is wrong, at least the linker will behave
3205 in the same way that it used to do. */
3211 /* Search the symbol table of the archive element of the archive ABFD
3212 whose archive map contains a mention of SYMDEF, and determine if
3213 the symbol is defined in this element. */
3215 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3217 Elf_Internal_Shdr
* hdr
;
3221 Elf_Internal_Sym
*isymbuf
;
3222 Elf_Internal_Sym
*isym
;
3223 Elf_Internal_Sym
*isymend
;
3226 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
3230 if (! bfd_check_format (abfd
, bfd_object
))
3233 /* Select the appropriate symbol table. If we don't know if the
3234 object file is an IR object, give linker LTO plugin a chance to
3235 get the correct symbol table. */
3236 if (abfd
->plugin_format
== bfd_plugin_yes
3237 #if BFD_SUPPORTS_PLUGINS
3238 || (abfd
->plugin_format
== bfd_plugin_unknown
3239 && bfd_link_plugin_object_p (abfd
))
3243 /* Use the IR symbol table if the object has been claimed by
3245 abfd
= abfd
->plugin_dummy_bfd
;
3246 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3248 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3249 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3251 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3253 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3255 /* The sh_info field of the symtab header tells us where the
3256 external symbols start. We don't care about the local symbols. */
3257 if (elf_bad_symtab (abfd
))
3259 extsymcount
= symcount
;
3264 extsymcount
= symcount
- hdr
->sh_info
;
3265 extsymoff
= hdr
->sh_info
;
3268 if (extsymcount
== 0)
3271 /* Read in the symbol table. */
3272 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3274 if (isymbuf
== NULL
)
3277 /* Scan the symbol table looking for SYMDEF. */
3279 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3283 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3288 if (strcmp (name
, symdef
->name
) == 0)
3290 result
= is_global_data_symbol_definition (abfd
, isym
);
3300 /* Add an entry to the .dynamic table. */
3303 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3307 struct elf_link_hash_table
*hash_table
;
3308 const struct elf_backend_data
*bed
;
3310 bfd_size_type newsize
;
3311 bfd_byte
*newcontents
;
3312 Elf_Internal_Dyn dyn
;
3314 hash_table
= elf_hash_table (info
);
3315 if (! is_elf_hash_table (hash_table
))
3318 bed
= get_elf_backend_data (hash_table
->dynobj
);
3319 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3320 BFD_ASSERT (s
!= NULL
);
3322 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3323 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3324 if (newcontents
== NULL
)
3328 dyn
.d_un
.d_val
= val
;
3329 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3332 s
->contents
= newcontents
;
3337 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3338 otherwise just check whether one already exists. Returns -1 on error,
3339 1 if a DT_NEEDED tag already exists, and 0 on success. */
3342 elf_add_dt_needed_tag (bfd
*abfd
,
3343 struct bfd_link_info
*info
,
3347 struct elf_link_hash_table
*hash_table
;
3350 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3353 hash_table
= elf_hash_table (info
);
3354 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3355 if (strindex
== (size_t) -1)
3358 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3361 const struct elf_backend_data
*bed
;
3364 bed
= get_elf_backend_data (hash_table
->dynobj
);
3365 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3367 for (extdyn
= sdyn
->contents
;
3368 extdyn
< sdyn
->contents
+ sdyn
->size
;
3369 extdyn
+= bed
->s
->sizeof_dyn
)
3371 Elf_Internal_Dyn dyn
;
3373 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3374 if (dyn
.d_tag
== DT_NEEDED
3375 && dyn
.d_un
.d_val
== strindex
)
3377 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3385 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3388 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3392 /* We were just checking for existence of the tag. */
3393 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3398 /* Return true if SONAME is on the needed list between NEEDED and STOP
3399 (or the end of list if STOP is NULL), and needed by a library that
3403 on_needed_list (const char *soname
,
3404 struct bfd_link_needed_list
*needed
,
3405 struct bfd_link_needed_list
*stop
)
3407 struct bfd_link_needed_list
*look
;
3408 for (look
= needed
; look
!= stop
; look
= look
->next
)
3409 if (strcmp (soname
, look
->name
) == 0
3410 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3411 /* If needed by a library that itself is not directly
3412 needed, recursively check whether that library is
3413 indirectly needed. Since we add DT_NEEDED entries to
3414 the end of the list, library dependencies appear after
3415 the library. Therefore search prior to the current
3416 LOOK, preventing possible infinite recursion. */
3417 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3423 /* Sort symbol by value, section, and size. */
3425 elf_sort_symbol (const void *arg1
, const void *arg2
)
3427 const struct elf_link_hash_entry
*h1
;
3428 const struct elf_link_hash_entry
*h2
;
3429 bfd_signed_vma vdiff
;
3431 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3432 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3433 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3435 return vdiff
> 0 ? 1 : -1;
3438 int sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3440 return sdiff
> 0 ? 1 : -1;
3442 vdiff
= h1
->size
- h2
->size
;
3443 return vdiff
== 0 ? 0 : vdiff
> 0 ? 1 : -1;
3446 /* This function is used to adjust offsets into .dynstr for
3447 dynamic symbols. This is called via elf_link_hash_traverse. */
3450 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3452 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3454 if (h
->dynindx
!= -1)
3455 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3459 /* Assign string offsets in .dynstr, update all structures referencing
3463 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3465 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3466 struct elf_link_local_dynamic_entry
*entry
;
3467 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3468 bfd
*dynobj
= hash_table
->dynobj
;
3471 const struct elf_backend_data
*bed
;
3474 _bfd_elf_strtab_finalize (dynstr
);
3475 size
= _bfd_elf_strtab_size (dynstr
);
3477 bed
= get_elf_backend_data (dynobj
);
3478 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3479 BFD_ASSERT (sdyn
!= NULL
);
3481 /* Update all .dynamic entries referencing .dynstr strings. */
3482 for (extdyn
= sdyn
->contents
;
3483 extdyn
< sdyn
->contents
+ sdyn
->size
;
3484 extdyn
+= bed
->s
->sizeof_dyn
)
3486 Elf_Internal_Dyn dyn
;
3488 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3492 dyn
.d_un
.d_val
= size
;
3502 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3507 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3510 /* Now update local dynamic symbols. */
3511 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3512 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3513 entry
->isym
.st_name
);
3515 /* And the rest of dynamic symbols. */
3516 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3518 /* Adjust version definitions. */
3519 if (elf_tdata (output_bfd
)->cverdefs
)
3524 Elf_Internal_Verdef def
;
3525 Elf_Internal_Verdaux defaux
;
3527 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3531 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3533 p
+= sizeof (Elf_External_Verdef
);
3534 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3536 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3538 _bfd_elf_swap_verdaux_in (output_bfd
,
3539 (Elf_External_Verdaux
*) p
, &defaux
);
3540 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3542 _bfd_elf_swap_verdaux_out (output_bfd
,
3543 &defaux
, (Elf_External_Verdaux
*) p
);
3544 p
+= sizeof (Elf_External_Verdaux
);
3547 while (def
.vd_next
);
3550 /* Adjust version references. */
3551 if (elf_tdata (output_bfd
)->verref
)
3556 Elf_Internal_Verneed need
;
3557 Elf_Internal_Vernaux needaux
;
3559 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
3563 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3565 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3566 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3567 (Elf_External_Verneed
*) p
);
3568 p
+= sizeof (Elf_External_Verneed
);
3569 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3571 _bfd_elf_swap_vernaux_in (output_bfd
,
3572 (Elf_External_Vernaux
*) p
, &needaux
);
3573 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3575 _bfd_elf_swap_vernaux_out (output_bfd
,
3577 (Elf_External_Vernaux
*) p
);
3578 p
+= sizeof (Elf_External_Vernaux
);
3581 while (need
.vn_next
);
3587 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3588 The default is to only match when the INPUT and OUTPUT are exactly
3592 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3593 const bfd_target
*output
)
3595 return input
== output
;
3598 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3599 This version is used when different targets for the same architecture
3600 are virtually identical. */
3603 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3604 const bfd_target
*output
)
3606 const struct elf_backend_data
*obed
, *ibed
;
3608 if (input
== output
)
3611 ibed
= xvec_get_elf_backend_data (input
);
3612 obed
= xvec_get_elf_backend_data (output
);
3614 if (ibed
->arch
!= obed
->arch
)
3617 /* If both backends are using this function, deem them compatible. */
3618 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3621 /* Make a special call to the linker "notice" function to tell it that
3622 we are about to handle an as-needed lib, or have finished
3623 processing the lib. */
3626 _bfd_elf_notice_as_needed (bfd
*ibfd
,
3627 struct bfd_link_info
*info
,
3628 enum notice_asneeded_action act
)
3630 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
3633 /* Check relocations an ELF object file. */
3636 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
3638 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3639 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
3641 /* If this object is the same format as the output object, and it is
3642 not a shared library, then let the backend look through the
3645 This is required to build global offset table entries and to
3646 arrange for dynamic relocs. It is not required for the
3647 particular common case of linking non PIC code, even when linking
3648 against shared libraries, but unfortunately there is no way of
3649 knowing whether an object file has been compiled PIC or not.
3650 Looking through the relocs is not particularly time consuming.
3651 The problem is that we must either (1) keep the relocs in memory,
3652 which causes the linker to require additional runtime memory or
3653 (2) read the relocs twice from the input file, which wastes time.
3654 This would be a good case for using mmap.
3656 I have no idea how to handle linking PIC code into a file of a
3657 different format. It probably can't be done. */
3658 if ((abfd
->flags
& DYNAMIC
) == 0
3659 && is_elf_hash_table (htab
)
3660 && bed
->check_relocs
!= NULL
3661 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
3662 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
3666 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3668 Elf_Internal_Rela
*internal_relocs
;
3671 /* Don't check relocations in excluded sections. */
3672 if ((o
->flags
& SEC_RELOC
) == 0
3673 || (o
->flags
& SEC_EXCLUDE
) != 0
3674 || o
->reloc_count
== 0
3675 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
3676 && (o
->flags
& SEC_DEBUGGING
) != 0)
3677 || bfd_is_abs_section (o
->output_section
))
3680 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
3682 if (internal_relocs
== NULL
)
3685 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
3687 if (elf_section_data (o
)->relocs
!= internal_relocs
)
3688 free (internal_relocs
);
3698 /* Add symbols from an ELF object file to the linker hash table. */
3701 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3703 Elf_Internal_Ehdr
*ehdr
;
3704 Elf_Internal_Shdr
*hdr
;
3708 struct elf_link_hash_entry
**sym_hash
;
3709 bfd_boolean dynamic
;
3710 Elf_External_Versym
*extversym
= NULL
;
3711 Elf_External_Versym
*ever
;
3712 struct elf_link_hash_entry
*weaks
;
3713 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3714 size_t nondeflt_vers_cnt
= 0;
3715 Elf_Internal_Sym
*isymbuf
= NULL
;
3716 Elf_Internal_Sym
*isym
;
3717 Elf_Internal_Sym
*isymend
;
3718 const struct elf_backend_data
*bed
;
3719 bfd_boolean add_needed
;
3720 struct elf_link_hash_table
*htab
;
3722 void *alloc_mark
= NULL
;
3723 struct bfd_hash_entry
**old_table
= NULL
;
3724 unsigned int old_size
= 0;
3725 unsigned int old_count
= 0;
3726 void *old_tab
= NULL
;
3728 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3729 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3730 void *old_strtab
= NULL
;
3733 bfd_boolean just_syms
;
3735 htab
= elf_hash_table (info
);
3736 bed
= get_elf_backend_data (abfd
);
3738 if ((abfd
->flags
& DYNAMIC
) == 0)
3744 /* You can't use -r against a dynamic object. Also, there's no
3745 hope of using a dynamic object which does not exactly match
3746 the format of the output file. */
3747 if (bfd_link_relocatable (info
)
3748 || !is_elf_hash_table (htab
)
3749 || info
->output_bfd
->xvec
!= abfd
->xvec
)
3751 if (bfd_link_relocatable (info
))
3752 bfd_set_error (bfd_error_invalid_operation
);
3754 bfd_set_error (bfd_error_wrong_format
);
3759 ehdr
= elf_elfheader (abfd
);
3760 if (info
->warn_alternate_em
3761 && bed
->elf_machine_code
!= ehdr
->e_machine
3762 && ((bed
->elf_machine_alt1
!= 0
3763 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
3764 || (bed
->elf_machine_alt2
!= 0
3765 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
3766 info
->callbacks
->einfo
3767 /* xgettext:c-format */
3768 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3769 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
3771 /* As a GNU extension, any input sections which are named
3772 .gnu.warning.SYMBOL are treated as warning symbols for the given
3773 symbol. This differs from .gnu.warning sections, which generate
3774 warnings when they are included in an output file. */
3775 /* PR 12761: Also generate this warning when building shared libraries. */
3776 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3780 name
= bfd_get_section_name (abfd
, s
);
3781 if (CONST_STRNEQ (name
, ".gnu.warning."))
3786 name
+= sizeof ".gnu.warning." - 1;
3788 /* If this is a shared object, then look up the symbol
3789 in the hash table. If it is there, and it is already
3790 been defined, then we will not be using the entry
3791 from this shared object, so we don't need to warn.
3792 FIXME: If we see the definition in a regular object
3793 later on, we will warn, but we shouldn't. The only
3794 fix is to keep track of what warnings we are supposed
3795 to emit, and then handle them all at the end of the
3799 struct elf_link_hash_entry
*h
;
3801 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3803 /* FIXME: What about bfd_link_hash_common? */
3805 && (h
->root
.type
== bfd_link_hash_defined
3806 || h
->root
.type
== bfd_link_hash_defweak
))
3811 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
3815 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3820 if (! (_bfd_generic_link_add_one_symbol
3821 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3822 FALSE
, bed
->collect
, NULL
)))
3825 if (bfd_link_executable (info
))
3827 /* Clobber the section size so that the warning does
3828 not get copied into the output file. */
3831 /* Also set SEC_EXCLUDE, so that symbols defined in
3832 the warning section don't get copied to the output. */
3833 s
->flags
|= SEC_EXCLUDE
;
3838 just_syms
= ((s
= abfd
->sections
) != NULL
3839 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
3844 /* If we are creating a shared library, create all the dynamic
3845 sections immediately. We need to attach them to something,
3846 so we attach them to this BFD, provided it is the right
3847 format and is not from ld --just-symbols. Always create the
3848 dynamic sections for -E/--dynamic-list. FIXME: If there
3849 are no input BFD's of the same format as the output, we can't
3850 make a shared library. */
3852 && (bfd_link_pic (info
)
3853 || (!bfd_link_relocatable (info
)
3854 && (info
->export_dynamic
|| info
->dynamic
)))
3855 && is_elf_hash_table (htab
)
3856 && info
->output_bfd
->xvec
== abfd
->xvec
3857 && !htab
->dynamic_sections_created
)
3859 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3863 else if (!is_elf_hash_table (htab
))
3867 const char *soname
= NULL
;
3869 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3870 const Elf_Internal_Phdr
*phdr
;
3873 /* ld --just-symbols and dynamic objects don't mix very well.
3874 ld shouldn't allow it. */
3878 /* If this dynamic lib was specified on the command line with
3879 --as-needed in effect, then we don't want to add a DT_NEEDED
3880 tag unless the lib is actually used. Similary for libs brought
3881 in by another lib's DT_NEEDED. When --no-add-needed is used
3882 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3883 any dynamic library in DT_NEEDED tags in the dynamic lib at
3885 add_needed
= (elf_dyn_lib_class (abfd
)
3886 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3887 | DYN_NO_NEEDED
)) == 0;
3889 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3894 unsigned int elfsec
;
3895 unsigned long shlink
;
3897 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3904 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3905 if (elfsec
== SHN_BAD
)
3906 goto error_free_dyn
;
3907 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3909 for (extdyn
= dynbuf
;
3910 extdyn
< dynbuf
+ s
->size
;
3911 extdyn
+= bed
->s
->sizeof_dyn
)
3913 Elf_Internal_Dyn dyn
;
3915 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3916 if (dyn
.d_tag
== DT_SONAME
)
3918 unsigned int tagv
= dyn
.d_un
.d_val
;
3919 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3921 goto error_free_dyn
;
3923 if (dyn
.d_tag
== DT_NEEDED
)
3925 struct bfd_link_needed_list
*n
, **pn
;
3927 unsigned int tagv
= dyn
.d_un
.d_val
;
3929 amt
= sizeof (struct bfd_link_needed_list
);
3930 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3931 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3932 if (n
== NULL
|| fnm
== NULL
)
3933 goto error_free_dyn
;
3934 amt
= strlen (fnm
) + 1;
3935 anm
= (char *) bfd_alloc (abfd
, amt
);
3937 goto error_free_dyn
;
3938 memcpy (anm
, fnm
, amt
);
3942 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3946 if (dyn
.d_tag
== DT_RUNPATH
)
3948 struct bfd_link_needed_list
*n
, **pn
;
3950 unsigned int tagv
= dyn
.d_un
.d_val
;
3952 amt
= sizeof (struct bfd_link_needed_list
);
3953 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3954 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3955 if (n
== NULL
|| fnm
== NULL
)
3956 goto error_free_dyn
;
3957 amt
= strlen (fnm
) + 1;
3958 anm
= (char *) bfd_alloc (abfd
, amt
);
3960 goto error_free_dyn
;
3961 memcpy (anm
, fnm
, amt
);
3965 for (pn
= & runpath
;
3971 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3972 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3974 struct bfd_link_needed_list
*n
, **pn
;
3976 unsigned int tagv
= dyn
.d_un
.d_val
;
3978 amt
= sizeof (struct bfd_link_needed_list
);
3979 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3980 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3981 if (n
== NULL
|| fnm
== NULL
)
3982 goto error_free_dyn
;
3983 amt
= strlen (fnm
) + 1;
3984 anm
= (char *) bfd_alloc (abfd
, amt
);
3986 goto error_free_dyn
;
3987 memcpy (anm
, fnm
, amt
);
3997 if (dyn
.d_tag
== DT_AUDIT
)
3999 unsigned int tagv
= dyn
.d_un
.d_val
;
4000 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4007 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4008 frees all more recently bfd_alloc'd blocks as well. */
4014 struct bfd_link_needed_list
**pn
;
4015 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4020 /* If we have a PT_GNU_RELRO program header, mark as read-only
4021 all sections contained fully therein. This makes relro
4022 shared library sections appear as they will at run-time. */
4023 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4024 while (--phdr
>= elf_tdata (abfd
)->phdr
)
4025 if (phdr
->p_type
== PT_GNU_RELRO
)
4027 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4028 if ((s
->flags
& SEC_ALLOC
) != 0
4029 && s
->vma
>= phdr
->p_vaddr
4030 && s
->vma
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4031 s
->flags
|= SEC_READONLY
;
4035 /* We do not want to include any of the sections in a dynamic
4036 object in the output file. We hack by simply clobbering the
4037 list of sections in the BFD. This could be handled more
4038 cleanly by, say, a new section flag; the existing
4039 SEC_NEVER_LOAD flag is not the one we want, because that one
4040 still implies that the section takes up space in the output
4042 bfd_section_list_clear (abfd
);
4044 /* Find the name to use in a DT_NEEDED entry that refers to this
4045 object. If the object has a DT_SONAME entry, we use it.
4046 Otherwise, if the generic linker stuck something in
4047 elf_dt_name, we use that. Otherwise, we just use the file
4049 if (soname
== NULL
|| *soname
== '\0')
4051 soname
= elf_dt_name (abfd
);
4052 if (soname
== NULL
|| *soname
== '\0')
4053 soname
= bfd_get_filename (abfd
);
4056 /* Save the SONAME because sometimes the linker emulation code
4057 will need to know it. */
4058 elf_dt_name (abfd
) = soname
;
4060 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4064 /* If we have already included this dynamic object in the
4065 link, just ignore it. There is no reason to include a
4066 particular dynamic object more than once. */
4070 /* Save the DT_AUDIT entry for the linker emulation code. */
4071 elf_dt_audit (abfd
) = audit
;
4074 /* If this is a dynamic object, we always link against the .dynsym
4075 symbol table, not the .symtab symbol table. The dynamic linker
4076 will only see the .dynsym symbol table, so there is no reason to
4077 look at .symtab for a dynamic object. */
4079 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4080 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4082 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4084 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4086 /* The sh_info field of the symtab header tells us where the
4087 external symbols start. We don't care about the local symbols at
4089 if (elf_bad_symtab (abfd
))
4091 extsymcount
= symcount
;
4096 extsymcount
= symcount
- hdr
->sh_info
;
4097 extsymoff
= hdr
->sh_info
;
4100 sym_hash
= elf_sym_hashes (abfd
);
4101 if (extsymcount
!= 0)
4103 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4105 if (isymbuf
== NULL
)
4108 if (sym_hash
== NULL
)
4110 /* We store a pointer to the hash table entry for each
4113 amt
*= sizeof (struct elf_link_hash_entry
*);
4114 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4115 if (sym_hash
== NULL
)
4116 goto error_free_sym
;
4117 elf_sym_hashes (abfd
) = sym_hash
;
4123 /* Read in any version definitions. */
4124 if (!_bfd_elf_slurp_version_tables (abfd
,
4125 info
->default_imported_symver
))
4126 goto error_free_sym
;
4128 /* Read in the symbol versions, but don't bother to convert them
4129 to internal format. */
4130 if (elf_dynversym (abfd
) != 0)
4132 Elf_Internal_Shdr
*versymhdr
;
4134 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4135 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
4136 if (extversym
== NULL
)
4137 goto error_free_sym
;
4138 amt
= versymhdr
->sh_size
;
4139 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
4140 || bfd_bread (extversym
, amt
, abfd
) != amt
)
4141 goto error_free_vers
;
4145 /* If we are loading an as-needed shared lib, save the symbol table
4146 state before we start adding symbols. If the lib turns out
4147 to be unneeded, restore the state. */
4148 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4153 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4155 struct bfd_hash_entry
*p
;
4156 struct elf_link_hash_entry
*h
;
4158 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4160 h
= (struct elf_link_hash_entry
*) p
;
4161 entsize
+= htab
->root
.table
.entsize
;
4162 if (h
->root
.type
== bfd_link_hash_warning
)
4163 entsize
+= htab
->root
.table
.entsize
;
4167 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4168 old_tab
= bfd_malloc (tabsize
+ entsize
);
4169 if (old_tab
== NULL
)
4170 goto error_free_vers
;
4172 /* Remember the current objalloc pointer, so that all mem for
4173 symbols added can later be reclaimed. */
4174 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4175 if (alloc_mark
== NULL
)
4176 goto error_free_vers
;
4178 /* Make a special call to the linker "notice" function to
4179 tell it that we are about to handle an as-needed lib. */
4180 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4181 goto error_free_vers
;
4183 /* Clone the symbol table. Remember some pointers into the
4184 symbol table, and dynamic symbol count. */
4185 old_ent
= (char *) old_tab
+ tabsize
;
4186 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4187 old_undefs
= htab
->root
.undefs
;
4188 old_undefs_tail
= htab
->root
.undefs_tail
;
4189 old_table
= htab
->root
.table
.table
;
4190 old_size
= htab
->root
.table
.size
;
4191 old_count
= htab
->root
.table
.count
;
4192 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4193 if (old_strtab
== NULL
)
4194 goto error_free_vers
;
4196 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4198 struct bfd_hash_entry
*p
;
4199 struct elf_link_hash_entry
*h
;
4201 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4203 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
4204 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4205 h
= (struct elf_link_hash_entry
*) p
;
4206 if (h
->root
.type
== bfd_link_hash_warning
)
4208 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
4209 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4216 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
4217 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
4219 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4223 asection
*sec
, *new_sec
;
4226 struct elf_link_hash_entry
*h
;
4227 struct elf_link_hash_entry
*hi
;
4228 bfd_boolean definition
;
4229 bfd_boolean size_change_ok
;
4230 bfd_boolean type_change_ok
;
4231 bfd_boolean new_weakdef
;
4232 bfd_boolean new_weak
;
4233 bfd_boolean old_weak
;
4234 bfd_boolean override
;
4236 bfd_boolean discarded
;
4237 unsigned int old_alignment
;
4239 bfd_boolean matched
;
4243 flags
= BSF_NO_FLAGS
;
4245 value
= isym
->st_value
;
4246 common
= bed
->common_definition (isym
);
4249 bind
= ELF_ST_BIND (isym
->st_info
);
4253 /* This should be impossible, since ELF requires that all
4254 global symbols follow all local symbols, and that sh_info
4255 point to the first global symbol. Unfortunately, Irix 5
4260 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4268 case STB_GNU_UNIQUE
:
4269 flags
= BSF_GNU_UNIQUE
;
4273 /* Leave it up to the processor backend. */
4277 if (isym
->st_shndx
== SHN_UNDEF
)
4278 sec
= bfd_und_section_ptr
;
4279 else if (isym
->st_shndx
== SHN_ABS
)
4280 sec
= bfd_abs_section_ptr
;
4281 else if (isym
->st_shndx
== SHN_COMMON
)
4283 sec
= bfd_com_section_ptr
;
4284 /* What ELF calls the size we call the value. What ELF
4285 calls the value we call the alignment. */
4286 value
= isym
->st_size
;
4290 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4292 sec
= bfd_abs_section_ptr
;
4293 else if (discarded_section (sec
))
4295 /* Symbols from discarded section are undefined. We keep
4297 sec
= bfd_und_section_ptr
;
4299 isym
->st_shndx
= SHN_UNDEF
;
4301 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4305 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4308 goto error_free_vers
;
4310 if (isym
->st_shndx
== SHN_COMMON
4311 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4313 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4317 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4319 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4321 goto error_free_vers
;
4325 else if (isym
->st_shndx
== SHN_COMMON
4326 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4327 && !bfd_link_relocatable (info
))
4329 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4333 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4334 | SEC_LINKER_CREATED
);
4335 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4337 goto error_free_vers
;
4341 else if (bed
->elf_add_symbol_hook
)
4343 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4345 goto error_free_vers
;
4347 /* The hook function sets the name to NULL if this symbol
4348 should be skipped for some reason. */
4353 /* Sanity check that all possibilities were handled. */
4356 bfd_set_error (bfd_error_bad_value
);
4357 goto error_free_vers
;
4360 /* Silently discard TLS symbols from --just-syms. There's
4361 no way to combine a static TLS block with a new TLS block
4362 for this executable. */
4363 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4364 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4367 if (bfd_is_und_section (sec
)
4368 || bfd_is_com_section (sec
))
4373 size_change_ok
= FALSE
;
4374 type_change_ok
= bed
->type_change_ok
;
4381 if (is_elf_hash_table (htab
))
4383 Elf_Internal_Versym iver
;
4384 unsigned int vernum
= 0;
4389 if (info
->default_imported_symver
)
4390 /* Use the default symbol version created earlier. */
4391 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4396 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4398 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4400 /* If this is a hidden symbol, or if it is not version
4401 1, we append the version name to the symbol name.
4402 However, we do not modify a non-hidden absolute symbol
4403 if it is not a function, because it might be the version
4404 symbol itself. FIXME: What if it isn't? */
4405 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4407 && (!bfd_is_abs_section (sec
)
4408 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4411 size_t namelen
, verlen
, newlen
;
4414 if (isym
->st_shndx
!= SHN_UNDEF
)
4416 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4418 else if (vernum
> 1)
4420 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4427 /* xgettext:c-format */
4428 (_("%B: %s: invalid version %u (max %d)"),
4430 elf_tdata (abfd
)->cverdefs
);
4431 bfd_set_error (bfd_error_bad_value
);
4432 goto error_free_vers
;
4437 /* We cannot simply test for the number of
4438 entries in the VERNEED section since the
4439 numbers for the needed versions do not start
4441 Elf_Internal_Verneed
*t
;
4444 for (t
= elf_tdata (abfd
)->verref
;
4448 Elf_Internal_Vernaux
*a
;
4450 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4452 if (a
->vna_other
== vernum
)
4454 verstr
= a
->vna_nodename
;
4464 /* xgettext:c-format */
4465 (_("%B: %s: invalid needed version %d"),
4466 abfd
, name
, vernum
);
4467 bfd_set_error (bfd_error_bad_value
);
4468 goto error_free_vers
;
4472 namelen
= strlen (name
);
4473 verlen
= strlen (verstr
);
4474 newlen
= namelen
+ verlen
+ 2;
4475 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4476 && isym
->st_shndx
!= SHN_UNDEF
)
4479 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4480 if (newname
== NULL
)
4481 goto error_free_vers
;
4482 memcpy (newname
, name
, namelen
);
4483 p
= newname
+ namelen
;
4485 /* If this is a defined non-hidden version symbol,
4486 we add another @ to the name. This indicates the
4487 default version of the symbol. */
4488 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4489 && isym
->st_shndx
!= SHN_UNDEF
)
4491 memcpy (p
, verstr
, verlen
+ 1);
4496 /* If this symbol has default visibility and the user has
4497 requested we not re-export it, then mark it as hidden. */
4498 if (!bfd_is_und_section (sec
)
4501 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4502 isym
->st_other
= (STV_HIDDEN
4503 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4505 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4506 sym_hash
, &old_bfd
, &old_weak
,
4507 &old_alignment
, &skip
, &override
,
4508 &type_change_ok
, &size_change_ok
,
4510 goto error_free_vers
;
4515 /* Override a definition only if the new symbol matches the
4517 if (override
&& matched
)
4521 while (h
->root
.type
== bfd_link_hash_indirect
4522 || h
->root
.type
== bfd_link_hash_warning
)
4523 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4525 if (elf_tdata (abfd
)->verdef
!= NULL
4528 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4531 if (! (_bfd_generic_link_add_one_symbol
4532 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4533 (struct bfd_link_hash_entry
**) sym_hash
)))
4534 goto error_free_vers
;
4536 if ((flags
& BSF_GNU_UNIQUE
)
4537 && (abfd
->flags
& DYNAMIC
) == 0
4538 && bfd_get_flavour (info
->output_bfd
) == bfd_target_elf_flavour
)
4539 elf_tdata (info
->output_bfd
)->has_gnu_symbols
|= elf_gnu_symbol_unique
;
4542 /* We need to make sure that indirect symbol dynamic flags are
4545 while (h
->root
.type
== bfd_link_hash_indirect
4546 || h
->root
.type
== bfd_link_hash_warning
)
4547 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4549 /* Setting the index to -3 tells elf_link_output_extsym that
4550 this symbol is defined in a discarded section. */
4556 new_weak
= (flags
& BSF_WEAK
) != 0;
4557 new_weakdef
= FALSE
;
4561 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4562 && is_elf_hash_table (htab
)
4563 && h
->u
.weakdef
== NULL
)
4565 /* Keep a list of all weak defined non function symbols from
4566 a dynamic object, using the weakdef field. Later in this
4567 function we will set the weakdef field to the correct
4568 value. We only put non-function symbols from dynamic
4569 objects on this list, because that happens to be the only
4570 time we need to know the normal symbol corresponding to a
4571 weak symbol, and the information is time consuming to
4572 figure out. If the weakdef field is not already NULL,
4573 then this symbol was already defined by some previous
4574 dynamic object, and we will be using that previous
4575 definition anyhow. */
4577 h
->u
.weakdef
= weaks
;
4582 /* Set the alignment of a common symbol. */
4583 if ((common
|| bfd_is_com_section (sec
))
4584 && h
->root
.type
== bfd_link_hash_common
)
4589 align
= bfd_log2 (isym
->st_value
);
4592 /* The new symbol is a common symbol in a shared object.
4593 We need to get the alignment from the section. */
4594 align
= new_sec
->alignment_power
;
4596 if (align
> old_alignment
)
4597 h
->root
.u
.c
.p
->alignment_power
= align
;
4599 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4602 if (is_elf_hash_table (htab
))
4604 /* Set a flag in the hash table entry indicating the type of
4605 reference or definition we just found. A dynamic symbol
4606 is one which is referenced or defined by both a regular
4607 object and a shared object. */
4608 bfd_boolean dynsym
= FALSE
;
4610 /* Plugin symbols aren't normal. Don't set def_regular or
4611 ref_regular for them, or make them dynamic. */
4612 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4619 if (bind
!= STB_WEAK
)
4620 h
->ref_regular_nonweak
= 1;
4632 /* If the indirect symbol has been forced local, don't
4633 make the real symbol dynamic. */
4634 if ((h
== hi
|| !hi
->forced_local
)
4635 && (bfd_link_dll (info
)
4645 hi
->ref_dynamic
= 1;
4650 hi
->def_dynamic
= 1;
4653 /* If the indirect symbol has been forced local, don't
4654 make the real symbol dynamic. */
4655 if ((h
== hi
|| !hi
->forced_local
)
4658 || (h
->u
.weakdef
!= NULL
4660 && h
->u
.weakdef
->dynindx
!= -1)))
4664 /* Check to see if we need to add an indirect symbol for
4665 the default name. */
4667 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
4668 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4669 sec
, value
, &old_bfd
, &dynsym
))
4670 goto error_free_vers
;
4672 /* Check the alignment when a common symbol is involved. This
4673 can change when a common symbol is overridden by a normal
4674 definition or a common symbol is ignored due to the old
4675 normal definition. We need to make sure the maximum
4676 alignment is maintained. */
4677 if ((old_alignment
|| common
)
4678 && h
->root
.type
!= bfd_link_hash_common
)
4680 unsigned int common_align
;
4681 unsigned int normal_align
;
4682 unsigned int symbol_align
;
4686 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
4687 || h
->root
.type
== bfd_link_hash_defweak
);
4689 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4690 if (h
->root
.u
.def
.section
->owner
!= NULL
4691 && (h
->root
.u
.def
.section
->owner
->flags
4692 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
4694 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4695 if (normal_align
> symbol_align
)
4696 normal_align
= symbol_align
;
4699 normal_align
= symbol_align
;
4703 common_align
= old_alignment
;
4704 common_bfd
= old_bfd
;
4709 common_align
= bfd_log2 (isym
->st_value
);
4711 normal_bfd
= old_bfd
;
4714 if (normal_align
< common_align
)
4716 /* PR binutils/2735 */
4717 if (normal_bfd
== NULL
)
4719 /* xgettext:c-format */
4720 (_("Warning: alignment %u of common symbol `%s' in %B is"
4721 " greater than the alignment (%u) of its section %A"),
4722 1 << common_align
, name
, common_bfd
,
4723 1 << normal_align
, h
->root
.u
.def
.section
);
4726 /* xgettext:c-format */
4727 (_("Warning: alignment %u of symbol `%s' in %B"
4728 " is smaller than %u in %B"),
4729 1 << normal_align
, name
, normal_bfd
,
4730 1 << common_align
, common_bfd
);
4734 /* Remember the symbol size if it isn't undefined. */
4735 if (isym
->st_size
!= 0
4736 && isym
->st_shndx
!= SHN_UNDEF
4737 && (definition
|| h
->size
== 0))
4740 && h
->size
!= isym
->st_size
4741 && ! size_change_ok
)
4743 /* xgettext:c-format */
4744 (_("Warning: size of symbol `%s' changed"
4745 " from %lu in %B to %lu in %B"),
4746 name
, (unsigned long) h
->size
, old_bfd
,
4747 (unsigned long) isym
->st_size
, abfd
);
4749 h
->size
= isym
->st_size
;
4752 /* If this is a common symbol, then we always want H->SIZE
4753 to be the size of the common symbol. The code just above
4754 won't fix the size if a common symbol becomes larger. We
4755 don't warn about a size change here, because that is
4756 covered by --warn-common. Allow changes between different
4758 if (h
->root
.type
== bfd_link_hash_common
)
4759 h
->size
= h
->root
.u
.c
.size
;
4761 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4762 && ((definition
&& !new_weak
)
4763 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
4764 || h
->type
== STT_NOTYPE
))
4766 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
4768 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4770 if (type
== STT_GNU_IFUNC
4771 && (abfd
->flags
& DYNAMIC
) != 0)
4774 if (h
->type
!= type
)
4776 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
4777 /* xgettext:c-format */
4779 (_("Warning: type of symbol `%s' changed"
4780 " from %d to %d in %B"),
4781 name
, h
->type
, type
, abfd
);
4787 /* Merge st_other field. */
4788 elf_merge_st_other (abfd
, h
, isym
, sec
, definition
, dynamic
);
4790 /* We don't want to make debug symbol dynamic. */
4792 && (sec
->flags
& SEC_DEBUGGING
)
4793 && !bfd_link_relocatable (info
))
4796 /* Nor should we make plugin symbols dynamic. */
4797 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4802 h
->target_internal
= isym
->st_target_internal
;
4803 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
4806 if (definition
&& !dynamic
)
4808 char *p
= strchr (name
, ELF_VER_CHR
);
4809 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4811 /* Queue non-default versions so that .symver x, x@FOO
4812 aliases can be checked. */
4815 amt
= ((isymend
- isym
+ 1)
4816 * sizeof (struct elf_link_hash_entry
*));
4818 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
4820 goto error_free_vers
;
4822 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4826 if (dynsym
&& h
->dynindx
== -1)
4828 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4829 goto error_free_vers
;
4830 if (h
->u
.weakdef
!= NULL
4832 && h
->u
.weakdef
->dynindx
== -1)
4834 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4835 goto error_free_vers
;
4838 else if (h
->dynindx
!= -1)
4839 /* If the symbol already has a dynamic index, but
4840 visibility says it should not be visible, turn it into
4842 switch (ELF_ST_VISIBILITY (h
->other
))
4846 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4851 /* Don't add DT_NEEDED for references from the dummy bfd nor
4852 for unmatched symbol. */
4857 && h
->ref_regular_nonweak
4859 || (old_bfd
->flags
& BFD_PLUGIN
) == 0))
4860 || (h
->ref_dynamic_nonweak
4861 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
4862 && !on_needed_list (elf_dt_name (abfd
),
4863 htab
->needed
, NULL
))))
4866 const char *soname
= elf_dt_name (abfd
);
4868 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
4869 h
->root
.root
.string
);
4871 /* A symbol from a library loaded via DT_NEEDED of some
4872 other library is referenced by a regular object.
4873 Add a DT_NEEDED entry for it. Issue an error if
4874 --no-add-needed is used and the reference was not
4877 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4880 /* xgettext:c-format */
4881 (_("%B: undefined reference to symbol '%s'"),
4883 bfd_set_error (bfd_error_missing_dso
);
4884 goto error_free_vers
;
4887 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
4888 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
4891 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4893 goto error_free_vers
;
4895 BFD_ASSERT (ret
== 0);
4900 if (extversym
!= NULL
)
4906 if (isymbuf
!= NULL
)
4912 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4916 /* Restore the symbol table. */
4917 old_ent
= (char *) old_tab
+ tabsize
;
4918 memset (elf_sym_hashes (abfd
), 0,
4919 extsymcount
* sizeof (struct elf_link_hash_entry
*));
4920 htab
->root
.table
.table
= old_table
;
4921 htab
->root
.table
.size
= old_size
;
4922 htab
->root
.table
.count
= old_count
;
4923 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4924 htab
->root
.undefs
= old_undefs
;
4925 htab
->root
.undefs_tail
= old_undefs_tail
;
4926 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
4929 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4931 struct bfd_hash_entry
*p
;
4932 struct elf_link_hash_entry
*h
;
4934 unsigned int alignment_power
;
4936 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4938 h
= (struct elf_link_hash_entry
*) p
;
4939 if (h
->root
.type
== bfd_link_hash_warning
)
4940 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4942 /* Preserve the maximum alignment and size for common
4943 symbols even if this dynamic lib isn't on DT_NEEDED
4944 since it can still be loaded at run time by another
4946 if (h
->root
.type
== bfd_link_hash_common
)
4948 size
= h
->root
.u
.c
.size
;
4949 alignment_power
= h
->root
.u
.c
.p
->alignment_power
;
4954 alignment_power
= 0;
4956 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4957 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4958 h
= (struct elf_link_hash_entry
*) p
;
4959 if (h
->root
.type
== bfd_link_hash_warning
)
4961 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4962 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4963 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4965 if (h
->root
.type
== bfd_link_hash_common
)
4967 if (size
> h
->root
.u
.c
.size
)
4968 h
->root
.u
.c
.size
= size
;
4969 if (alignment_power
> h
->root
.u
.c
.p
->alignment_power
)
4970 h
->root
.u
.c
.p
->alignment_power
= alignment_power
;
4975 /* Make a special call to the linker "notice" function to
4976 tell it that symbols added for crefs may need to be removed. */
4977 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
4978 goto error_free_vers
;
4981 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4983 if (nondeflt_vers
!= NULL
)
4984 free (nondeflt_vers
);
4988 if (old_tab
!= NULL
)
4990 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
4991 goto error_free_vers
;
4996 /* Now that all the symbols from this input file are created, if
4997 not performing a relocatable link, handle .symver foo, foo@BAR
4998 such that any relocs against foo become foo@BAR. */
4999 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5003 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5005 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5006 char *shortname
, *p
;
5008 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5010 || (h
->root
.type
!= bfd_link_hash_defined
5011 && h
->root
.type
!= bfd_link_hash_defweak
))
5014 amt
= p
- h
->root
.root
.string
;
5015 shortname
= (char *) bfd_malloc (amt
+ 1);
5017 goto error_free_vers
;
5018 memcpy (shortname
, h
->root
.root
.string
, amt
);
5019 shortname
[amt
] = '\0';
5021 hi
= (struct elf_link_hash_entry
*)
5022 bfd_link_hash_lookup (&htab
->root
, shortname
,
5023 FALSE
, FALSE
, FALSE
);
5025 && hi
->root
.type
== h
->root
.type
5026 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5027 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5029 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
5030 hi
->root
.type
= bfd_link_hash_indirect
;
5031 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5032 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5033 sym_hash
= elf_sym_hashes (abfd
);
5035 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5036 if (sym_hash
[symidx
] == hi
)
5038 sym_hash
[symidx
] = h
;
5044 free (nondeflt_vers
);
5045 nondeflt_vers
= NULL
;
5048 /* Now set the weakdefs field correctly for all the weak defined
5049 symbols we found. The only way to do this is to search all the
5050 symbols. Since we only need the information for non functions in
5051 dynamic objects, that's the only time we actually put anything on
5052 the list WEAKS. We need this information so that if a regular
5053 object refers to a symbol defined weakly in a dynamic object, the
5054 real symbol in the dynamic object is also put in the dynamic
5055 symbols; we also must arrange for both symbols to point to the
5056 same memory location. We could handle the general case of symbol
5057 aliasing, but a general symbol alias can only be generated in
5058 assembler code, handling it correctly would be very time
5059 consuming, and other ELF linkers don't handle general aliasing
5063 struct elf_link_hash_entry
**hpp
;
5064 struct elf_link_hash_entry
**hppend
;
5065 struct elf_link_hash_entry
**sorted_sym_hash
;
5066 struct elf_link_hash_entry
*h
;
5069 /* Since we have to search the whole symbol list for each weak
5070 defined symbol, search time for N weak defined symbols will be
5071 O(N^2). Binary search will cut it down to O(NlogN). */
5073 amt
*= sizeof (struct elf_link_hash_entry
*);
5074 sorted_sym_hash
= (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5075 if (sorted_sym_hash
== NULL
)
5077 sym_hash
= sorted_sym_hash
;
5078 hpp
= elf_sym_hashes (abfd
);
5079 hppend
= hpp
+ extsymcount
;
5081 for (; hpp
< hppend
; hpp
++)
5085 && h
->root
.type
== bfd_link_hash_defined
5086 && !bed
->is_function_type (h
->type
))
5094 qsort (sorted_sym_hash
, sym_count
,
5095 sizeof (struct elf_link_hash_entry
*),
5098 while (weaks
!= NULL
)
5100 struct elf_link_hash_entry
*hlook
;
5103 size_t i
, j
, idx
= 0;
5106 weaks
= hlook
->u
.weakdef
;
5107 hlook
->u
.weakdef
= NULL
;
5109 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
5110 || hlook
->root
.type
== bfd_link_hash_defweak
5111 || hlook
->root
.type
== bfd_link_hash_common
5112 || hlook
->root
.type
== bfd_link_hash_indirect
);
5113 slook
= hlook
->root
.u
.def
.section
;
5114 vlook
= hlook
->root
.u
.def
.value
;
5120 bfd_signed_vma vdiff
;
5122 h
= sorted_sym_hash
[idx
];
5123 vdiff
= vlook
- h
->root
.u
.def
.value
;
5130 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5140 /* We didn't find a value/section match. */
5144 /* With multiple aliases, or when the weak symbol is already
5145 strongly defined, we have multiple matching symbols and
5146 the binary search above may land on any of them. Step
5147 one past the matching symbol(s). */
5150 h
= sorted_sym_hash
[idx
];
5151 if (h
->root
.u
.def
.section
!= slook
5152 || h
->root
.u
.def
.value
!= vlook
)
5156 /* Now look back over the aliases. Since we sorted by size
5157 as well as value and section, we'll choose the one with
5158 the largest size. */
5161 h
= sorted_sym_hash
[idx
];
5163 /* Stop if value or section doesn't match. */
5164 if (h
->root
.u
.def
.section
!= slook
5165 || h
->root
.u
.def
.value
!= vlook
)
5167 else if (h
!= hlook
)
5169 hlook
->u
.weakdef
= h
;
5171 /* If the weak definition is in the list of dynamic
5172 symbols, make sure the real definition is put
5174 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5176 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5179 free (sorted_sym_hash
);
5184 /* If the real definition is in the list of dynamic
5185 symbols, make sure the weak definition is put
5186 there as well. If we don't do this, then the
5187 dynamic loader might not merge the entries for the
5188 real definition and the weak definition. */
5189 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5191 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5192 goto err_free_sym_hash
;
5199 free (sorted_sym_hash
);
5202 if (bed
->check_directives
5203 && !(*bed
->check_directives
) (abfd
, info
))
5206 if (!info
->check_relocs_after_open_input
5207 && !_bfd_elf_link_check_relocs (abfd
, info
))
5210 /* If this is a non-traditional link, try to optimize the handling
5211 of the .stab/.stabstr sections. */
5213 && ! info
->traditional_format
5214 && is_elf_hash_table (htab
)
5215 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5219 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5220 if (stabstr
!= NULL
)
5222 bfd_size_type string_offset
= 0;
5225 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5226 if (CONST_STRNEQ (stab
->name
, ".stab")
5227 && (!stab
->name
[5] ||
5228 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5229 && (stab
->flags
& SEC_MERGE
) == 0
5230 && !bfd_is_abs_section (stab
->output_section
))
5232 struct bfd_elf_section_data
*secdata
;
5234 secdata
= elf_section_data (stab
);
5235 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5236 stabstr
, &secdata
->sec_info
,
5239 if (secdata
->sec_info
)
5240 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5245 if (is_elf_hash_table (htab
) && add_needed
)
5247 /* Add this bfd to the loaded list. */
5248 struct elf_link_loaded_list
*n
;
5250 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5254 n
->next
= htab
->loaded
;
5261 if (old_tab
!= NULL
)
5263 if (old_strtab
!= NULL
)
5265 if (nondeflt_vers
!= NULL
)
5266 free (nondeflt_vers
);
5267 if (extversym
!= NULL
)
5270 if (isymbuf
!= NULL
)
5276 /* Return the linker hash table entry of a symbol that might be
5277 satisfied by an archive symbol. Return -1 on error. */
5279 struct elf_link_hash_entry
*
5280 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5281 struct bfd_link_info
*info
,
5284 struct elf_link_hash_entry
*h
;
5288 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, TRUE
);
5292 /* If this is a default version (the name contains @@), look up the
5293 symbol again with only one `@' as well as without the version.
5294 The effect is that references to the symbol with and without the
5295 version will be matched by the default symbol in the archive. */
5297 p
= strchr (name
, ELF_VER_CHR
);
5298 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5301 /* First check with only one `@'. */
5302 len
= strlen (name
);
5303 copy
= (char *) bfd_alloc (abfd
, len
);
5305 return (struct elf_link_hash_entry
*) 0 - 1;
5307 first
= p
- name
+ 1;
5308 memcpy (copy
, name
, first
);
5309 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5311 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, TRUE
);
5314 /* We also need to check references to the symbol without the
5316 copy
[first
- 1] = '\0';
5317 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
5318 FALSE
, FALSE
, TRUE
);
5321 bfd_release (abfd
, copy
);
5325 /* Add symbols from an ELF archive file to the linker hash table. We
5326 don't use _bfd_generic_link_add_archive_symbols because we need to
5327 handle versioned symbols.
5329 Fortunately, ELF archive handling is simpler than that done by
5330 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5331 oddities. In ELF, if we find a symbol in the archive map, and the
5332 symbol is currently undefined, we know that we must pull in that
5335 Unfortunately, we do have to make multiple passes over the symbol
5336 table until nothing further is resolved. */
5339 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5342 unsigned char *included
= NULL
;
5346 const struct elf_backend_data
*bed
;
5347 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
5348 (bfd
*, struct bfd_link_info
*, const char *);
5350 if (! bfd_has_map (abfd
))
5352 /* An empty archive is a special case. */
5353 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5355 bfd_set_error (bfd_error_no_armap
);
5359 /* Keep track of all symbols we know to be already defined, and all
5360 files we know to be already included. This is to speed up the
5361 second and subsequent passes. */
5362 c
= bfd_ardata (abfd
)->symdef_count
;
5366 amt
*= sizeof (*included
);
5367 included
= (unsigned char *) bfd_zmalloc (amt
);
5368 if (included
== NULL
)
5371 symdefs
= bfd_ardata (abfd
)->symdefs
;
5372 bed
= get_elf_backend_data (abfd
);
5373 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5386 symdefend
= symdef
+ c
;
5387 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5389 struct elf_link_hash_entry
*h
;
5391 struct bfd_link_hash_entry
*undefs_tail
;
5396 if (symdef
->file_offset
== last
)
5402 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5403 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
5409 if (h
->root
.type
== bfd_link_hash_common
)
5411 /* We currently have a common symbol. The archive map contains
5412 a reference to this symbol, so we may want to include it. We
5413 only want to include it however, if this archive element
5414 contains a definition of the symbol, not just another common
5417 Unfortunately some archivers (including GNU ar) will put
5418 declarations of common symbols into their archive maps, as
5419 well as real definitions, so we cannot just go by the archive
5420 map alone. Instead we must read in the element's symbol
5421 table and check that to see what kind of symbol definition
5423 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5426 else if (h
->root
.type
!= bfd_link_hash_undefined
)
5428 if (h
->root
.type
!= bfd_link_hash_undefweak
)
5429 /* Symbol must be defined. Don't check it again. */
5434 /* We need to include this archive member. */
5435 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5436 if (element
== NULL
)
5439 if (! bfd_check_format (element
, bfd_object
))
5442 undefs_tail
= info
->hash
->undefs_tail
;
5444 if (!(*info
->callbacks
5445 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5447 if (!bfd_link_add_symbols (element
, info
))
5450 /* If there are any new undefined symbols, we need to make
5451 another pass through the archive in order to see whether
5452 they can be defined. FIXME: This isn't perfect, because
5453 common symbols wind up on undefs_tail and because an
5454 undefined symbol which is defined later on in this pass
5455 does not require another pass. This isn't a bug, but it
5456 does make the code less efficient than it could be. */
5457 if (undefs_tail
!= info
->hash
->undefs_tail
)
5460 /* Look backward to mark all symbols from this object file
5461 which we have already seen in this pass. */
5465 included
[mark
] = TRUE
;
5470 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5472 /* We mark subsequent symbols from this object file as we go
5473 on through the loop. */
5474 last
= symdef
->file_offset
;
5484 if (included
!= NULL
)
5489 /* Given an ELF BFD, add symbols to the global hash table as
5493 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5495 switch (bfd_get_format (abfd
))
5498 return elf_link_add_object_symbols (abfd
, info
);
5500 return elf_link_add_archive_symbols (abfd
, info
);
5502 bfd_set_error (bfd_error_wrong_format
);
5507 struct hash_codes_info
5509 unsigned long *hashcodes
;
5513 /* This function will be called though elf_link_hash_traverse to store
5514 all hash value of the exported symbols in an array. */
5517 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5519 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
5524 /* Ignore indirect symbols. These are added by the versioning code. */
5525 if (h
->dynindx
== -1)
5528 name
= h
->root
.root
.string
;
5529 if (h
->versioned
>= versioned
)
5531 char *p
= strchr (name
, ELF_VER_CHR
);
5534 alc
= (char *) bfd_malloc (p
- name
+ 1);
5540 memcpy (alc
, name
, p
- name
);
5541 alc
[p
- name
] = '\0';
5546 /* Compute the hash value. */
5547 ha
= bfd_elf_hash (name
);
5549 /* Store the found hash value in the array given as the argument. */
5550 *(inf
->hashcodes
)++ = ha
;
5552 /* And store it in the struct so that we can put it in the hash table
5554 h
->u
.elf_hash_value
= ha
;
5562 struct collect_gnu_hash_codes
5565 const struct elf_backend_data
*bed
;
5566 unsigned long int nsyms
;
5567 unsigned long int maskbits
;
5568 unsigned long int *hashcodes
;
5569 unsigned long int *hashval
;
5570 unsigned long int *indx
;
5571 unsigned long int *counts
;
5574 long int min_dynindx
;
5575 unsigned long int bucketcount
;
5576 unsigned long int symindx
;
5577 long int local_indx
;
5578 long int shift1
, shift2
;
5579 unsigned long int mask
;
5583 /* This function will be called though elf_link_hash_traverse to store
5584 all hash value of the exported symbols in an array. */
5587 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5589 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5594 /* Ignore indirect symbols. These are added by the versioning code. */
5595 if (h
->dynindx
== -1)
5598 /* Ignore also local symbols and undefined symbols. */
5599 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5602 name
= h
->root
.root
.string
;
5603 if (h
->versioned
>= versioned
)
5605 char *p
= strchr (name
, ELF_VER_CHR
);
5608 alc
= (char *) bfd_malloc (p
- name
+ 1);
5614 memcpy (alc
, name
, p
- name
);
5615 alc
[p
- name
] = '\0';
5620 /* Compute the hash value. */
5621 ha
= bfd_elf_gnu_hash (name
);
5623 /* Store the found hash value in the array for compute_bucket_count,
5624 and also for .dynsym reordering purposes. */
5625 s
->hashcodes
[s
->nsyms
] = ha
;
5626 s
->hashval
[h
->dynindx
] = ha
;
5628 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5629 s
->min_dynindx
= h
->dynindx
;
5637 /* This function will be called though elf_link_hash_traverse to do
5638 final dynaminc symbol renumbering. */
5641 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5643 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5644 unsigned long int bucket
;
5645 unsigned long int val
;
5647 /* Ignore indirect symbols. */
5648 if (h
->dynindx
== -1)
5651 /* Ignore also local symbols and undefined symbols. */
5652 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5654 if (h
->dynindx
>= s
->min_dynindx
)
5655 h
->dynindx
= s
->local_indx
++;
5659 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5660 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5661 & ((s
->maskbits
>> s
->shift1
) - 1);
5662 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5664 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5665 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5666 if (s
->counts
[bucket
] == 1)
5667 /* Last element terminates the chain. */
5669 bfd_put_32 (s
->output_bfd
, val
,
5670 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5671 --s
->counts
[bucket
];
5672 h
->dynindx
= s
->indx
[bucket
]++;
5676 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5679 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5681 return !(h
->forced_local
5682 || h
->root
.type
== bfd_link_hash_undefined
5683 || h
->root
.type
== bfd_link_hash_undefweak
5684 || ((h
->root
.type
== bfd_link_hash_defined
5685 || h
->root
.type
== bfd_link_hash_defweak
)
5686 && h
->root
.u
.def
.section
->output_section
== NULL
));
5689 /* Array used to determine the number of hash table buckets to use
5690 based on the number of symbols there are. If there are fewer than
5691 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5692 fewer than 37 we use 17 buckets, and so forth. We never use more
5693 than 32771 buckets. */
5695 static const size_t elf_buckets
[] =
5697 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5701 /* Compute bucket count for hashing table. We do not use a static set
5702 of possible tables sizes anymore. Instead we determine for all
5703 possible reasonable sizes of the table the outcome (i.e., the
5704 number of collisions etc) and choose the best solution. The
5705 weighting functions are not too simple to allow the table to grow
5706 without bounds. Instead one of the weighting factors is the size.
5707 Therefore the result is always a good payoff between few collisions
5708 (= short chain lengths) and table size. */
5710 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
5711 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
5712 unsigned long int nsyms
,
5715 size_t best_size
= 0;
5716 unsigned long int i
;
5718 /* We have a problem here. The following code to optimize the table
5719 size requires an integer type with more the 32 bits. If
5720 BFD_HOST_U_64_BIT is set we know about such a type. */
5721 #ifdef BFD_HOST_U_64_BIT
5726 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5727 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5728 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5729 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5730 unsigned long int *counts
;
5732 unsigned int no_improvement_count
= 0;
5734 /* Possible optimization parameters: if we have NSYMS symbols we say
5735 that the hashing table must at least have NSYMS/4 and at most
5737 minsize
= nsyms
/ 4;
5740 best_size
= maxsize
= nsyms
* 2;
5745 if ((best_size
& 31) == 0)
5749 /* Create array where we count the collisions in. We must use bfd_malloc
5750 since the size could be large. */
5752 amt
*= sizeof (unsigned long int);
5753 counts
= (unsigned long int *) bfd_malloc (amt
);
5757 /* Compute the "optimal" size for the hash table. The criteria is a
5758 minimal chain length. The minor criteria is (of course) the size
5760 for (i
= minsize
; i
< maxsize
; ++i
)
5762 /* Walk through the array of hashcodes and count the collisions. */
5763 BFD_HOST_U_64_BIT max
;
5764 unsigned long int j
;
5765 unsigned long int fact
;
5767 if (gnu_hash
&& (i
& 31) == 0)
5770 memset (counts
, '\0', i
* sizeof (unsigned long int));
5772 /* Determine how often each hash bucket is used. */
5773 for (j
= 0; j
< nsyms
; ++j
)
5774 ++counts
[hashcodes
[j
] % i
];
5776 /* For the weight function we need some information about the
5777 pagesize on the target. This is information need not be 100%
5778 accurate. Since this information is not available (so far) we
5779 define it here to a reasonable default value. If it is crucial
5780 to have a better value some day simply define this value. */
5781 # ifndef BFD_TARGET_PAGESIZE
5782 # define BFD_TARGET_PAGESIZE (4096)
5785 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5787 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5790 /* Variant 1: optimize for short chains. We add the squares
5791 of all the chain lengths (which favors many small chain
5792 over a few long chains). */
5793 for (j
= 0; j
< i
; ++j
)
5794 max
+= counts
[j
] * counts
[j
];
5796 /* This adds penalties for the overall size of the table. */
5797 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5800 /* Variant 2: Optimize a lot more for small table. Here we
5801 also add squares of the size but we also add penalties for
5802 empty slots (the +1 term). */
5803 for (j
= 0; j
< i
; ++j
)
5804 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5806 /* The overall size of the table is considered, but not as
5807 strong as in variant 1, where it is squared. */
5808 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5812 /* Compare with current best results. */
5813 if (max
< best_chlen
)
5817 no_improvement_count
= 0;
5819 /* PR 11843: Avoid futile long searches for the best bucket size
5820 when there are a large number of symbols. */
5821 else if (++no_improvement_count
== 100)
5828 #endif /* defined (BFD_HOST_U_64_BIT) */
5830 /* This is the fallback solution if no 64bit type is available or if we
5831 are not supposed to spend much time on optimizations. We select the
5832 bucket count using a fixed set of numbers. */
5833 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5835 best_size
= elf_buckets
[i
];
5836 if (nsyms
< elf_buckets
[i
+ 1])
5839 if (gnu_hash
&& best_size
< 2)
5846 /* Size any SHT_GROUP section for ld -r. */
5849 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
5853 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
5854 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
5855 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
5860 /* Set a default stack segment size. The value in INFO wins. If it
5861 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5862 undefined it is initialized. */
5865 bfd_elf_stack_segment_size (bfd
*output_bfd
,
5866 struct bfd_link_info
*info
,
5867 const char *legacy_symbol
,
5868 bfd_vma default_size
)
5870 struct elf_link_hash_entry
*h
= NULL
;
5872 /* Look for legacy symbol. */
5874 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
5875 FALSE
, FALSE
, FALSE
);
5876 if (h
&& (h
->root
.type
== bfd_link_hash_defined
5877 || h
->root
.type
== bfd_link_hash_defweak
)
5879 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
5881 /* The symbol has no type if specified on the command line. */
5882 h
->type
= STT_OBJECT
;
5883 if (info
->stacksize
)
5884 /* xgettext:c-format */
5885 _bfd_error_handler (_("%B: stack size specified and %s set"),
5886 output_bfd
, legacy_symbol
);
5887 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
5888 /* xgettext:c-format */
5889 _bfd_error_handler (_("%B: %s not absolute"),
5890 output_bfd
, legacy_symbol
);
5892 info
->stacksize
= h
->root
.u
.def
.value
;
5895 if (!info
->stacksize
)
5896 /* If the user didn't set a size, or explicitly inhibit the
5897 size, set it now. */
5898 info
->stacksize
= default_size
;
5900 /* Provide the legacy symbol, if it is referenced. */
5901 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
5902 || h
->root
.type
== bfd_link_hash_undefweak
))
5904 struct bfd_link_hash_entry
*bh
= NULL
;
5906 if (!(_bfd_generic_link_add_one_symbol
5907 (info
, output_bfd
, legacy_symbol
,
5908 BSF_GLOBAL
, bfd_abs_section_ptr
,
5909 info
->stacksize
>= 0 ? info
->stacksize
: 0,
5910 NULL
, FALSE
, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
5913 h
= (struct elf_link_hash_entry
*) bh
;
5915 h
->type
= STT_OBJECT
;
5921 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5923 struct elf_gc_sweep_symbol_info
5925 struct bfd_link_info
*info
;
5926 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
5931 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
5934 && (((h
->root
.type
== bfd_link_hash_defined
5935 || h
->root
.type
== bfd_link_hash_defweak
)
5936 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
5937 && h
->root
.u
.def
.section
->gc_mark
))
5938 || h
->root
.type
== bfd_link_hash_undefined
5939 || h
->root
.type
== bfd_link_hash_undefweak
))
5941 struct elf_gc_sweep_symbol_info
*inf
;
5943 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
5944 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
5947 h
->ref_regular_nonweak
= 0;
5953 /* Set up the sizes and contents of the ELF dynamic sections. This is
5954 called by the ELF linker emulation before_allocation routine. We
5955 must set the sizes of the sections before the linker sets the
5956 addresses of the various sections. */
5959 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5962 const char *filter_shlib
,
5964 const char *depaudit
,
5965 const char * const *auxiliary_filters
,
5966 struct bfd_link_info
*info
,
5967 asection
**sinterpptr
)
5970 const struct elf_backend_data
*bed
;
5974 if (!is_elf_hash_table (info
->hash
))
5977 dynobj
= elf_hash_table (info
)->dynobj
;
5979 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
5981 struct bfd_elf_version_tree
*verdefs
;
5982 struct elf_info_failed asvinfo
;
5983 struct bfd_elf_version_tree
*t
;
5984 struct bfd_elf_version_expr
*d
;
5985 struct elf_info_failed eif
;
5986 bfd_boolean all_defined
;
5993 /* If we are supposed to export all symbols into the dynamic symbol
5994 table (this is not the normal case), then do so. */
5995 if (info
->export_dynamic
5996 || (bfd_link_executable (info
) && info
->dynamic
))
5998 elf_link_hash_traverse (elf_hash_table (info
),
5999 _bfd_elf_export_symbol
,
6007 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6009 if (soname_indx
== (size_t) -1
6010 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6014 soname_indx
= (size_t) -1;
6016 /* Make all global versions with definition. */
6017 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6018 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6019 if (!d
->symver
&& d
->literal
)
6021 const char *verstr
, *name
;
6022 size_t namelen
, verlen
, newlen
;
6023 char *newname
, *p
, leading_char
;
6024 struct elf_link_hash_entry
*newh
;
6026 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6028 namelen
= strlen (name
) + (leading_char
!= '\0');
6030 verlen
= strlen (verstr
);
6031 newlen
= namelen
+ verlen
+ 3;
6033 newname
= (char *) bfd_malloc (newlen
);
6034 if (newname
== NULL
)
6036 newname
[0] = leading_char
;
6037 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6039 /* Check the hidden versioned definition. */
6040 p
= newname
+ namelen
;
6042 memcpy (p
, verstr
, verlen
+ 1);
6043 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6044 newname
, FALSE
, FALSE
,
6047 || (newh
->root
.type
!= bfd_link_hash_defined
6048 && newh
->root
.type
!= bfd_link_hash_defweak
))
6050 /* Check the default versioned definition. */
6052 memcpy (p
, verstr
, verlen
+ 1);
6053 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6054 newname
, FALSE
, FALSE
,
6059 /* Mark this version if there is a definition and it is
6060 not defined in a shared object. */
6062 && !newh
->def_dynamic
6063 && (newh
->root
.type
== bfd_link_hash_defined
6064 || newh
->root
.type
== bfd_link_hash_defweak
))
6068 /* Attach all the symbols to their version information. */
6069 asvinfo
.info
= info
;
6070 asvinfo
.failed
= FALSE
;
6072 elf_link_hash_traverse (elf_hash_table (info
),
6073 _bfd_elf_link_assign_sym_version
,
6078 if (!info
->allow_undefined_version
)
6080 /* Check if all global versions have a definition. */
6082 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6083 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6084 if (d
->literal
&& !d
->symver
&& !d
->script
)
6087 (_("%s: undefined version: %s"),
6088 d
->pattern
, t
->name
);
6089 all_defined
= FALSE
;
6094 bfd_set_error (bfd_error_bad_value
);
6099 /* Set up the version definition section. */
6100 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6101 BFD_ASSERT (s
!= NULL
);
6103 /* We may have created additional version definitions if we are
6104 just linking a regular application. */
6105 verdefs
= info
->version_info
;
6107 /* Skip anonymous version tag. */
6108 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6109 verdefs
= verdefs
->next
;
6111 if (verdefs
== NULL
&& !info
->create_default_symver
)
6112 s
->flags
|= SEC_EXCLUDE
;
6118 Elf_Internal_Verdef def
;
6119 Elf_Internal_Verdaux defaux
;
6120 struct bfd_link_hash_entry
*bh
;
6121 struct elf_link_hash_entry
*h
;
6127 /* Make space for the base version. */
6128 size
+= sizeof (Elf_External_Verdef
);
6129 size
+= sizeof (Elf_External_Verdaux
);
6132 /* Make space for the default version. */
6133 if (info
->create_default_symver
)
6135 size
+= sizeof (Elf_External_Verdef
);
6139 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6141 struct bfd_elf_version_deps
*n
;
6143 /* Don't emit base version twice. */
6147 size
+= sizeof (Elf_External_Verdef
);
6148 size
+= sizeof (Elf_External_Verdaux
);
6151 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6152 size
+= sizeof (Elf_External_Verdaux
);
6156 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6157 if (s
->contents
== NULL
&& s
->size
!= 0)
6160 /* Fill in the version definition section. */
6164 def
.vd_version
= VER_DEF_CURRENT
;
6165 def
.vd_flags
= VER_FLG_BASE
;
6168 if (info
->create_default_symver
)
6170 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6171 def
.vd_next
= sizeof (Elf_External_Verdef
);
6175 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6176 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6177 + sizeof (Elf_External_Verdaux
));
6180 if (soname_indx
!= (size_t) -1)
6182 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6184 def
.vd_hash
= bfd_elf_hash (soname
);
6185 defaux
.vda_name
= soname_indx
;
6192 name
= lbasename (output_bfd
->filename
);
6193 def
.vd_hash
= bfd_elf_hash (name
);
6194 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6196 if (indx
== (size_t) -1)
6198 defaux
.vda_name
= indx
;
6200 defaux
.vda_next
= 0;
6202 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6203 (Elf_External_Verdef
*) p
);
6204 p
+= sizeof (Elf_External_Verdef
);
6205 if (info
->create_default_symver
)
6207 /* Add a symbol representing this version. */
6209 if (! (_bfd_generic_link_add_one_symbol
6210 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6212 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6214 h
= (struct elf_link_hash_entry
*) bh
;
6217 h
->type
= STT_OBJECT
;
6218 h
->verinfo
.vertree
= NULL
;
6220 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6223 /* Create a duplicate of the base version with the same
6224 aux block, but different flags. */
6227 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6229 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6230 + sizeof (Elf_External_Verdaux
));
6233 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6234 (Elf_External_Verdef
*) p
);
6235 p
+= sizeof (Elf_External_Verdef
);
6237 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6238 (Elf_External_Verdaux
*) p
);
6239 p
+= sizeof (Elf_External_Verdaux
);
6241 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6244 struct bfd_elf_version_deps
*n
;
6246 /* Don't emit the base version twice. */
6251 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6254 /* Add a symbol representing this version. */
6256 if (! (_bfd_generic_link_add_one_symbol
6257 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6259 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6261 h
= (struct elf_link_hash_entry
*) bh
;
6264 h
->type
= STT_OBJECT
;
6265 h
->verinfo
.vertree
= t
;
6267 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6270 def
.vd_version
= VER_DEF_CURRENT
;
6272 if (t
->globals
.list
== NULL
6273 && t
->locals
.list
== NULL
6275 def
.vd_flags
|= VER_FLG_WEAK
;
6276 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6277 def
.vd_cnt
= cdeps
+ 1;
6278 def
.vd_hash
= bfd_elf_hash (t
->name
);
6279 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6282 /* If a basever node is next, it *must* be the last node in
6283 the chain, otherwise Verdef construction breaks. */
6284 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6285 BFD_ASSERT (t
->next
->next
== NULL
);
6287 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6288 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6289 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6291 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6292 (Elf_External_Verdef
*) p
);
6293 p
+= sizeof (Elf_External_Verdef
);
6295 defaux
.vda_name
= h
->dynstr_index
;
6296 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6298 defaux
.vda_next
= 0;
6299 if (t
->deps
!= NULL
)
6300 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6301 t
->name_indx
= defaux
.vda_name
;
6303 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6304 (Elf_External_Verdaux
*) p
);
6305 p
+= sizeof (Elf_External_Verdaux
);
6307 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6309 if (n
->version_needed
== NULL
)
6311 /* This can happen if there was an error in the
6313 defaux
.vda_name
= 0;
6317 defaux
.vda_name
= n
->version_needed
->name_indx
;
6318 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6321 if (n
->next
== NULL
)
6322 defaux
.vda_next
= 0;
6324 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6326 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6327 (Elf_External_Verdaux
*) p
);
6328 p
+= sizeof (Elf_External_Verdaux
);
6332 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6335 /* Work out the size of the version reference section. */
6337 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6338 BFD_ASSERT (s
!= NULL
);
6340 struct elf_find_verdep_info sinfo
;
6343 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6344 if (sinfo
.vers
== 0)
6346 sinfo
.failed
= FALSE
;
6348 elf_link_hash_traverse (elf_hash_table (info
),
6349 _bfd_elf_link_find_version_dependencies
,
6354 if (elf_tdata (output_bfd
)->verref
== NULL
)
6355 s
->flags
|= SEC_EXCLUDE
;
6358 Elf_Internal_Verneed
*vn
;
6363 /* Build the version dependency section. */
6366 for (vn
= elf_tdata (output_bfd
)->verref
;
6368 vn
= vn
->vn_nextref
)
6370 Elf_Internal_Vernaux
*a
;
6372 size
+= sizeof (Elf_External_Verneed
);
6374 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6375 size
+= sizeof (Elf_External_Vernaux
);
6379 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6380 if (s
->contents
== NULL
)
6384 for (vn
= elf_tdata (output_bfd
)->verref
;
6386 vn
= vn
->vn_nextref
)
6389 Elf_Internal_Vernaux
*a
;
6393 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6396 vn
->vn_version
= VER_NEED_CURRENT
;
6398 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6399 elf_dt_name (vn
->vn_bfd
) != NULL
6400 ? elf_dt_name (vn
->vn_bfd
)
6401 : lbasename (vn
->vn_bfd
->filename
),
6403 if (indx
== (size_t) -1)
6406 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
6407 if (vn
->vn_nextref
== NULL
)
6410 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
6411 + caux
* sizeof (Elf_External_Vernaux
));
6413 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
6414 (Elf_External_Verneed
*) p
);
6415 p
+= sizeof (Elf_External_Verneed
);
6417 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6419 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6420 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6421 a
->vna_nodename
, FALSE
);
6422 if (indx
== (size_t) -1)
6425 if (a
->vna_nextptr
== NULL
)
6428 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6430 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6431 (Elf_External_Vernaux
*) p
);
6432 p
+= sizeof (Elf_External_Vernaux
);
6436 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6441 bed
= get_elf_backend_data (output_bfd
);
6443 if (info
->gc_sections
&& bed
->can_gc_sections
)
6445 struct elf_gc_sweep_symbol_info sweep_info
;
6446 unsigned long section_sym_count
;
6448 /* Remove the symbols that were in the swept sections from the
6449 dynamic symbol table. GCFIXME: Anyone know how to get them
6450 out of the static symbol table as well? */
6451 sweep_info
.info
= info
;
6452 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
6453 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
6456 _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, §ion_sym_count
);
6459 /* Any syms created from now on start with -1 in
6460 got.refcount/offset and plt.refcount/offset. */
6461 elf_hash_table (info
)->init_got_refcount
6462 = elf_hash_table (info
)->init_got_offset
;
6463 elf_hash_table (info
)->init_plt_refcount
6464 = elf_hash_table (info
)->init_plt_offset
;
6466 if (bfd_link_relocatable (info
)
6467 && !_bfd_elf_size_group_sections (info
))
6470 /* The backend may have to create some sections regardless of whether
6471 we're dynamic or not. */
6472 if (bed
->elf_backend_always_size_sections
6473 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
6476 /* Determine any GNU_STACK segment requirements, after the backend
6477 has had a chance to set a default segment size. */
6478 if (info
->execstack
)
6479 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
6480 else if (info
->noexecstack
)
6481 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
6485 asection
*notesec
= NULL
;
6488 for (inputobj
= info
->input_bfds
;
6490 inputobj
= inputobj
->link
.next
)
6495 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
6497 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
6500 if (s
->flags
& SEC_CODE
)
6504 else if (bed
->default_execstack
)
6507 if (notesec
|| info
->stacksize
> 0)
6508 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
6509 if (notesec
&& exec
&& bfd_link_relocatable (info
)
6510 && notesec
->output_section
!= bfd_abs_section_ptr
)
6511 notesec
->output_section
->flags
|= SEC_CODE
;
6514 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6516 struct elf_info_failed eif
;
6517 struct elf_link_hash_entry
*h
;
6521 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
6522 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
6526 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
6528 info
->flags
|= DF_SYMBOLIC
;
6536 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
6538 if (indx
== (size_t) -1)
6541 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
6542 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
6546 if (filter_shlib
!= NULL
)
6550 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6551 filter_shlib
, TRUE
);
6552 if (indx
== (size_t) -1
6553 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
6557 if (auxiliary_filters
!= NULL
)
6559 const char * const *p
;
6561 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
6565 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6567 if (indx
== (size_t) -1
6568 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
6577 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
6579 if (indx
== (size_t) -1
6580 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
6584 if (depaudit
!= NULL
)
6588 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
6590 if (indx
== (size_t) -1
6591 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
6598 /* Find all symbols which were defined in a dynamic object and make
6599 the backend pick a reasonable value for them. */
6600 elf_link_hash_traverse (elf_hash_table (info
),
6601 _bfd_elf_adjust_dynamic_symbol
,
6606 /* Add some entries to the .dynamic section. We fill in some of the
6607 values later, in bfd_elf_final_link, but we must add the entries
6608 now so that we know the final size of the .dynamic section. */
6610 /* If there are initialization and/or finalization functions to
6611 call then add the corresponding DT_INIT/DT_FINI entries. */
6612 h
= (info
->init_function
6613 ? elf_link_hash_lookup (elf_hash_table (info
),
6614 info
->init_function
, FALSE
,
6621 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
6624 h
= (info
->fini_function
6625 ? elf_link_hash_lookup (elf_hash_table (info
),
6626 info
->fini_function
, FALSE
,
6633 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
6637 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
6638 if (s
!= NULL
&& s
->linker_has_input
)
6640 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6641 if (! bfd_link_executable (info
))
6646 for (sub
= info
->input_bfds
; sub
!= NULL
;
6647 sub
= sub
->link
.next
)
6648 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
6649 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
6650 if (elf_section_data (o
)->this_hdr
.sh_type
6651 == SHT_PREINIT_ARRAY
)
6654 (_("%B: .preinit_array section is not allowed in DSO"),
6659 bfd_set_error (bfd_error_nonrepresentable_section
);
6663 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
6664 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
6667 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
6668 if (s
!= NULL
&& s
->linker_has_input
)
6670 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
6671 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
6674 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
6675 if (s
!= NULL
&& s
->linker_has_input
)
6677 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
6678 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
6682 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
6683 /* If .dynstr is excluded from the link, we don't want any of
6684 these tags. Strictly, we should be checking each section
6685 individually; This quick check covers for the case where
6686 someone does a /DISCARD/ : { *(*) }. */
6687 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
6689 bfd_size_type strsize
;
6691 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6692 if ((info
->emit_hash
6693 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
6694 || (info
->emit_gnu_hash
6695 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
6696 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
6697 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
6698 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
6699 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
6700 bed
->s
->sizeof_sym
))
6705 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
6708 /* The backend must work out the sizes of all the other dynamic
6711 && bed
->elf_backend_size_dynamic_sections
!= NULL
6712 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
6715 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6717 unsigned long section_sym_count
;
6719 if (elf_tdata (output_bfd
)->cverdefs
)
6721 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
6723 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
6724 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
6728 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
6730 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
6733 else if (info
->flags
& DF_BIND_NOW
)
6735 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
6741 if (bfd_link_executable (info
))
6742 info
->flags_1
&= ~ (DF_1_INITFIRST
6745 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
6749 if (elf_tdata (output_bfd
)->cverrefs
)
6751 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
6753 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
6754 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
6758 if ((elf_tdata (output_bfd
)->cverrefs
== 0
6759 && elf_tdata (output_bfd
)->cverdefs
== 0)
6760 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6761 §ion_sym_count
) == 0)
6765 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6766 s
->flags
|= SEC_EXCLUDE
;
6772 /* Find the first non-excluded output section. We'll use its
6773 section symbol for some emitted relocs. */
6775 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
6779 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6780 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
6781 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6783 elf_hash_table (info
)->text_index_section
= s
;
6788 /* Find two non-excluded output sections, one for code, one for data.
6789 We'll use their section symbols for some emitted relocs. */
6791 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6795 /* Data first, since setting text_index_section changes
6796 _bfd_elf_link_omit_section_dynsym. */
6797 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6798 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
6799 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6801 elf_hash_table (info
)->data_index_section
= s
;
6805 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6806 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
6807 == (SEC_ALLOC
| SEC_READONLY
))
6808 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6810 elf_hash_table (info
)->text_index_section
= s
;
6814 if (elf_hash_table (info
)->text_index_section
== NULL
)
6815 elf_hash_table (info
)->text_index_section
6816 = elf_hash_table (info
)->data_index_section
;
6820 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6822 const struct elf_backend_data
*bed
;
6824 if (!is_elf_hash_table (info
->hash
))
6827 bed
= get_elf_backend_data (output_bfd
);
6828 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6830 if (elf_hash_table (info
)->dynamic_sections_created
)
6834 bfd_size_type dynsymcount
;
6835 unsigned long section_sym_count
;
6836 unsigned int dtagcount
;
6838 dynobj
= elf_hash_table (info
)->dynobj
;
6840 /* Assign dynsym indicies. In a shared library we generate a
6841 section symbol for each output section, which come first.
6842 Next come all of the back-end allocated local dynamic syms,
6843 followed by the rest of the global symbols. */
6845 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6846 §ion_sym_count
);
6848 /* Work out the size of the symbol version section. */
6849 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6850 BFD_ASSERT (s
!= NULL
);
6851 if ((s
->flags
& SEC_EXCLUDE
) == 0)
6853 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6854 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6855 if (s
->contents
== NULL
)
6858 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
6862 /* Set the size of the .dynsym and .hash sections. We counted
6863 the number of dynamic symbols in elf_link_add_object_symbols.
6864 We will build the contents of .dynsym and .hash when we build
6865 the final symbol table, because until then we do not know the
6866 correct value to give the symbols. We built the .dynstr
6867 section as we went along in elf_link_add_object_symbols. */
6868 s
= elf_hash_table (info
)->dynsym
;
6869 BFD_ASSERT (s
!= NULL
);
6870 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
6872 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6873 if (s
->contents
== NULL
)
6876 /* The first entry in .dynsym is a dummy symbol. Clear all the
6877 section syms, in case we don't output them all. */
6878 ++section_sym_count
;
6879 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
6881 elf_hash_table (info
)->bucketcount
= 0;
6883 /* Compute the size of the hashing table. As a side effect this
6884 computes the hash values for all the names we export. */
6885 if (info
->emit_hash
)
6887 unsigned long int *hashcodes
;
6888 struct hash_codes_info hashinf
;
6890 unsigned long int nsyms
;
6892 size_t hash_entry_size
;
6894 /* Compute the hash values for all exported symbols. At the same
6895 time store the values in an array so that we could use them for
6897 amt
= dynsymcount
* sizeof (unsigned long int);
6898 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
6899 if (hashcodes
== NULL
)
6901 hashinf
.hashcodes
= hashcodes
;
6902 hashinf
.error
= FALSE
;
6904 /* Put all hash values in HASHCODES. */
6905 elf_link_hash_traverse (elf_hash_table (info
),
6906 elf_collect_hash_codes
, &hashinf
);
6913 nsyms
= hashinf
.hashcodes
- hashcodes
;
6915 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6918 if (bucketcount
== 0)
6921 elf_hash_table (info
)->bucketcount
= bucketcount
;
6923 s
= bfd_get_linker_section (dynobj
, ".hash");
6924 BFD_ASSERT (s
!= NULL
);
6925 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6926 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6927 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6928 if (s
->contents
== NULL
)
6931 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6932 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6933 s
->contents
+ hash_entry_size
);
6936 if (info
->emit_gnu_hash
)
6939 unsigned char *contents
;
6940 struct collect_gnu_hash_codes cinfo
;
6944 memset (&cinfo
, 0, sizeof (cinfo
));
6946 /* Compute the hash values for all exported symbols. At the same
6947 time store the values in an array so that we could use them for
6949 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6950 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
6951 if (cinfo
.hashcodes
== NULL
)
6954 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6955 cinfo
.min_dynindx
= -1;
6956 cinfo
.output_bfd
= output_bfd
;
6959 /* Put all hash values in HASHCODES. */
6960 elf_link_hash_traverse (elf_hash_table (info
),
6961 elf_collect_gnu_hash_codes
, &cinfo
);
6964 free (cinfo
.hashcodes
);
6969 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6971 if (bucketcount
== 0)
6973 free (cinfo
.hashcodes
);
6977 s
= bfd_get_linker_section (dynobj
, ".gnu.hash");
6978 BFD_ASSERT (s
!= NULL
);
6980 if (cinfo
.nsyms
== 0)
6982 /* Empty .gnu.hash section is special. */
6983 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6984 free (cinfo
.hashcodes
);
6985 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6986 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6987 if (contents
== NULL
)
6989 s
->contents
= contents
;
6990 /* 1 empty bucket. */
6991 bfd_put_32 (output_bfd
, 1, contents
);
6992 /* SYMIDX above the special symbol 0. */
6993 bfd_put_32 (output_bfd
, 1, contents
+ 4);
6994 /* Just one word for bitmask. */
6995 bfd_put_32 (output_bfd
, 1, contents
+ 8);
6996 /* Only hash fn bloom filter. */
6997 bfd_put_32 (output_bfd
, 0, contents
+ 12);
6998 /* No hashes are valid - empty bitmask. */
6999 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
7000 /* No hashes in the only bucket. */
7001 bfd_put_32 (output_bfd
, 0,
7002 contents
+ 16 + bed
->s
->arch_size
/ 8);
7006 unsigned long int maskwords
, maskbitslog2
, x
;
7007 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
7011 while ((x
>>= 1) != 0)
7013 if (maskbitslog2
< 3)
7015 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
7016 maskbitslog2
= maskbitslog2
+ 3;
7018 maskbitslog2
= maskbitslog2
+ 2;
7019 if (bed
->s
->arch_size
== 64)
7021 if (maskbitslog2
== 5)
7027 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
7028 cinfo
.shift2
= maskbitslog2
;
7029 cinfo
.maskbits
= 1 << maskbitslog2
;
7030 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
7031 amt
= bucketcount
* sizeof (unsigned long int) * 2;
7032 amt
+= maskwords
* sizeof (bfd_vma
);
7033 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
7034 if (cinfo
.bitmask
== NULL
)
7036 free (cinfo
.hashcodes
);
7040 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
7041 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
7042 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
7043 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
7045 /* Determine how often each hash bucket is used. */
7046 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
7047 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
7048 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
7050 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
7051 if (cinfo
.counts
[i
] != 0)
7053 cinfo
.indx
[i
] = cnt
;
7054 cnt
+= cinfo
.counts
[i
];
7056 BFD_ASSERT (cnt
== dynsymcount
);
7057 cinfo
.bucketcount
= bucketcount
;
7058 cinfo
.local_indx
= cinfo
.min_dynindx
;
7060 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
7061 s
->size
+= cinfo
.maskbits
/ 8;
7062 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7063 if (contents
== NULL
)
7065 free (cinfo
.bitmask
);
7066 free (cinfo
.hashcodes
);
7070 s
->contents
= contents
;
7071 bfd_put_32 (output_bfd
, bucketcount
, contents
);
7072 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
7073 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
7074 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
7075 contents
+= 16 + cinfo
.maskbits
/ 8;
7077 for (i
= 0; i
< bucketcount
; ++i
)
7079 if (cinfo
.counts
[i
] == 0)
7080 bfd_put_32 (output_bfd
, 0, contents
);
7082 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
7086 cinfo
.contents
= contents
;
7088 /* Renumber dynamic symbols, populate .gnu.hash section. */
7089 elf_link_hash_traverse (elf_hash_table (info
),
7090 elf_renumber_gnu_hash_syms
, &cinfo
);
7092 contents
= s
->contents
+ 16;
7093 for (i
= 0; i
< maskwords
; ++i
)
7095 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
7097 contents
+= bed
->s
->arch_size
/ 8;
7100 free (cinfo
.bitmask
);
7101 free (cinfo
.hashcodes
);
7105 s
= bfd_get_linker_section (dynobj
, ".dynstr");
7106 BFD_ASSERT (s
!= NULL
);
7108 elf_finalize_dynstr (output_bfd
, info
);
7110 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7112 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
7113 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
7120 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7123 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
7126 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
7127 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
7130 /* Finish SHF_MERGE section merging. */
7133 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
7138 if (!is_elf_hash_table (info
->hash
))
7141 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
7142 if ((ibfd
->flags
& DYNAMIC
) == 0
7143 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
7144 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
7145 == get_elf_backend_data (obfd
)->s
->elfclass
))
7146 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7147 if ((sec
->flags
& SEC_MERGE
) != 0
7148 && !bfd_is_abs_section (sec
->output_section
))
7150 struct bfd_elf_section_data
*secdata
;
7152 secdata
= elf_section_data (sec
);
7153 if (! _bfd_add_merge_section (obfd
,
7154 &elf_hash_table (info
)->merge_info
,
7155 sec
, &secdata
->sec_info
))
7157 else if (secdata
->sec_info
)
7158 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7161 if (elf_hash_table (info
)->merge_info
!= NULL
)
7162 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7163 merge_sections_remove_hook
);
7167 /* Create an entry in an ELF linker hash table. */
7169 struct bfd_hash_entry
*
7170 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7171 struct bfd_hash_table
*table
,
7174 /* Allocate the structure if it has not already been allocated by a
7178 entry
= (struct bfd_hash_entry
*)
7179 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7184 /* Call the allocation method of the superclass. */
7185 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7188 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7189 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7191 /* Set local fields. */
7194 ret
->got
= htab
->init_got_refcount
;
7195 ret
->plt
= htab
->init_plt_refcount
;
7196 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7197 - offsetof (struct elf_link_hash_entry
, size
)));
7198 /* Assume that we have been called by a non-ELF symbol reader.
7199 This flag is then reset by the code which reads an ELF input
7200 file. This ensures that a symbol created by a non-ELF symbol
7201 reader will have the flag set correctly. */
7208 /* Copy data from an indirect symbol to its direct symbol, hiding the
7209 old indirect symbol. Also used for copying flags to a weakdef. */
7212 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7213 struct elf_link_hash_entry
*dir
,
7214 struct elf_link_hash_entry
*ind
)
7216 struct elf_link_hash_table
*htab
;
7218 /* Copy down any references that we may have already seen to the
7219 symbol which just became indirect. */
7221 if (dir
->versioned
!= versioned_hidden
)
7222 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7223 dir
->ref_regular
|= ind
->ref_regular
;
7224 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7225 dir
->non_got_ref
|= ind
->non_got_ref
;
7226 dir
->needs_plt
|= ind
->needs_plt
;
7227 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7229 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7232 /* Copy over the global and procedure linkage table refcount entries.
7233 These may have been already set up by a check_relocs routine. */
7234 htab
= elf_hash_table (info
);
7235 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7237 if (dir
->got
.refcount
< 0)
7238 dir
->got
.refcount
= 0;
7239 dir
->got
.refcount
+= ind
->got
.refcount
;
7240 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7243 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7245 if (dir
->plt
.refcount
< 0)
7246 dir
->plt
.refcount
= 0;
7247 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7248 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7251 if (ind
->dynindx
!= -1)
7253 if (dir
->dynindx
!= -1)
7254 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7255 dir
->dynindx
= ind
->dynindx
;
7256 dir
->dynstr_index
= ind
->dynstr_index
;
7258 ind
->dynstr_index
= 0;
7263 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7264 struct elf_link_hash_entry
*h
,
7265 bfd_boolean force_local
)
7267 /* STT_GNU_IFUNC symbol must go through PLT. */
7268 if (h
->type
!= STT_GNU_IFUNC
)
7270 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7275 h
->forced_local
= 1;
7276 if (h
->dynindx
!= -1)
7279 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7285 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7289 _bfd_elf_link_hash_table_init
7290 (struct elf_link_hash_table
*table
,
7292 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
7293 struct bfd_hash_table
*,
7295 unsigned int entsize
,
7296 enum elf_target_id target_id
)
7299 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
7301 table
->init_got_refcount
.refcount
= can_refcount
- 1;
7302 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
7303 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
7304 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
7305 /* The first dynamic symbol is a dummy. */
7306 table
->dynsymcount
= 1;
7308 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
7310 table
->root
.type
= bfd_link_elf_hash_table
;
7311 table
->hash_table_id
= target_id
;
7316 /* Create an ELF linker hash table. */
7318 struct bfd_link_hash_table
*
7319 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7321 struct elf_link_hash_table
*ret
;
7322 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
7324 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7328 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7329 sizeof (struct elf_link_hash_entry
),
7335 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7340 /* Destroy an ELF linker hash table. */
7343 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7345 struct elf_link_hash_table
*htab
;
7347 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7348 if (htab
->dynstr
!= NULL
)
7349 _bfd_elf_strtab_free (htab
->dynstr
);
7350 _bfd_merge_sections_free (htab
->merge_info
);
7351 _bfd_generic_link_hash_table_free (obfd
);
7354 /* This is a hook for the ELF emulation code in the generic linker to
7355 tell the backend linker what file name to use for the DT_NEEDED
7356 entry for a dynamic object. */
7359 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7361 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7362 && bfd_get_format (abfd
) == bfd_object
)
7363 elf_dt_name (abfd
) = name
;
7367 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
7370 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7371 && bfd_get_format (abfd
) == bfd_object
)
7372 lib_class
= elf_dyn_lib_class (abfd
);
7379 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
7381 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7382 && bfd_get_format (abfd
) == bfd_object
)
7383 elf_dyn_lib_class (abfd
) = lib_class
;
7386 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7387 the linker ELF emulation code. */
7389 struct bfd_link_needed_list
*
7390 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7391 struct bfd_link_info
*info
)
7393 if (! is_elf_hash_table (info
->hash
))
7395 return elf_hash_table (info
)->needed
;
7398 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7399 hook for the linker ELF emulation code. */
7401 struct bfd_link_needed_list
*
7402 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7403 struct bfd_link_info
*info
)
7405 if (! is_elf_hash_table (info
->hash
))
7407 return elf_hash_table (info
)->runpath
;
7410 /* Get the name actually used for a dynamic object for a link. This
7411 is the SONAME entry if there is one. Otherwise, it is the string
7412 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7415 bfd_elf_get_dt_soname (bfd
*abfd
)
7417 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7418 && bfd_get_format (abfd
) == bfd_object
)
7419 return elf_dt_name (abfd
);
7423 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7424 the ELF linker emulation code. */
7427 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
7428 struct bfd_link_needed_list
**pneeded
)
7431 bfd_byte
*dynbuf
= NULL
;
7432 unsigned int elfsec
;
7433 unsigned long shlink
;
7434 bfd_byte
*extdyn
, *extdynend
;
7436 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
7440 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
7441 || bfd_get_format (abfd
) != bfd_object
)
7444 s
= bfd_get_section_by_name (abfd
, ".dynamic");
7445 if (s
== NULL
|| s
->size
== 0)
7448 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
7451 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
7452 if (elfsec
== SHN_BAD
)
7455 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
7457 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
7458 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
7461 extdynend
= extdyn
+ s
->size
;
7462 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
7464 Elf_Internal_Dyn dyn
;
7466 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
7468 if (dyn
.d_tag
== DT_NULL
)
7471 if (dyn
.d_tag
== DT_NEEDED
)
7474 struct bfd_link_needed_list
*l
;
7475 unsigned int tagv
= dyn
.d_un
.d_val
;
7478 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
7483 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
7504 struct elf_symbuf_symbol
7506 unsigned long st_name
; /* Symbol name, index in string tbl */
7507 unsigned char st_info
; /* Type and binding attributes */
7508 unsigned char st_other
; /* Visibilty, and target specific */
7511 struct elf_symbuf_head
7513 struct elf_symbuf_symbol
*ssym
;
7515 unsigned int st_shndx
;
7522 Elf_Internal_Sym
*isym
;
7523 struct elf_symbuf_symbol
*ssym
;
7528 /* Sort references to symbols by ascending section number. */
7531 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
7533 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
7534 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
7536 return s1
->st_shndx
- s2
->st_shndx
;
7540 elf_sym_name_compare (const void *arg1
, const void *arg2
)
7542 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
7543 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
7544 return strcmp (s1
->name
, s2
->name
);
7547 static struct elf_symbuf_head
*
7548 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
7550 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
7551 struct elf_symbuf_symbol
*ssym
;
7552 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
7553 size_t i
, shndx_count
, total_size
;
7555 indbuf
= (Elf_Internal_Sym
**) bfd_malloc2 (symcount
, sizeof (*indbuf
));
7559 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
7560 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
7561 *ind
++ = &isymbuf
[i
];
7564 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
7565 elf_sort_elf_symbol
);
7568 if (indbufend
> indbuf
)
7569 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
7570 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
7573 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
7574 + (indbufend
- indbuf
) * sizeof (*ssym
));
7575 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
7576 if (ssymbuf
== NULL
)
7582 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
7583 ssymbuf
->ssym
= NULL
;
7584 ssymbuf
->count
= shndx_count
;
7585 ssymbuf
->st_shndx
= 0;
7586 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
7588 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
7591 ssymhead
->ssym
= ssym
;
7592 ssymhead
->count
= 0;
7593 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
7595 ssym
->st_name
= (*ind
)->st_name
;
7596 ssym
->st_info
= (*ind
)->st_info
;
7597 ssym
->st_other
= (*ind
)->st_other
;
7600 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
7601 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
7608 /* Check if 2 sections define the same set of local and global
7612 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
7613 struct bfd_link_info
*info
)
7616 const struct elf_backend_data
*bed1
, *bed2
;
7617 Elf_Internal_Shdr
*hdr1
, *hdr2
;
7618 size_t symcount1
, symcount2
;
7619 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
7620 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
7621 Elf_Internal_Sym
*isym
, *isymend
;
7622 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
7623 size_t count1
, count2
, i
;
7624 unsigned int shndx1
, shndx2
;
7630 /* Both sections have to be in ELF. */
7631 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
7632 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
7635 if (elf_section_type (sec1
) != elf_section_type (sec2
))
7638 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
7639 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
7640 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
7643 bed1
= get_elf_backend_data (bfd1
);
7644 bed2
= get_elf_backend_data (bfd2
);
7645 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
7646 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
7647 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
7648 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
7650 if (symcount1
== 0 || symcount2
== 0)
7656 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
7657 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
7659 if (ssymbuf1
== NULL
)
7661 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
7663 if (isymbuf1
== NULL
)
7666 if (!info
->reduce_memory_overheads
)
7667 elf_tdata (bfd1
)->symbuf
= ssymbuf1
7668 = elf_create_symbuf (symcount1
, isymbuf1
);
7671 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
7673 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
7675 if (isymbuf2
== NULL
)
7678 if (ssymbuf1
!= NULL
&& !info
->reduce_memory_overheads
)
7679 elf_tdata (bfd2
)->symbuf
= ssymbuf2
7680 = elf_create_symbuf (symcount2
, isymbuf2
);
7683 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
7685 /* Optimized faster version. */
7687 struct elf_symbol
*symp
;
7688 struct elf_symbuf_symbol
*ssym
, *ssymend
;
7691 hi
= ssymbuf1
->count
;
7696 mid
= (lo
+ hi
) / 2;
7697 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
7699 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
7703 count1
= ssymbuf1
[mid
].count
;
7710 hi
= ssymbuf2
->count
;
7715 mid
= (lo
+ hi
) / 2;
7716 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
7718 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
7722 count2
= ssymbuf2
[mid
].count
;
7728 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7732 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
7734 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
7735 if (symtable1
== NULL
|| symtable2
== NULL
)
7739 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
7740 ssym
< ssymend
; ssym
++, symp
++)
7742 symp
->u
.ssym
= ssym
;
7743 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
7749 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
7750 ssym
< ssymend
; ssym
++, symp
++)
7752 symp
->u
.ssym
= ssym
;
7753 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
7758 /* Sort symbol by name. */
7759 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7760 elf_sym_name_compare
);
7761 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7762 elf_sym_name_compare
);
7764 for (i
= 0; i
< count1
; i
++)
7765 /* Two symbols must have the same binding, type and name. */
7766 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
7767 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
7768 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7775 symtable1
= (struct elf_symbol
*)
7776 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
7777 symtable2
= (struct elf_symbol
*)
7778 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
7779 if (symtable1
== NULL
|| symtable2
== NULL
)
7782 /* Count definitions in the section. */
7784 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
7785 if (isym
->st_shndx
== shndx1
)
7786 symtable1
[count1
++].u
.isym
= isym
;
7789 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
7790 if (isym
->st_shndx
== shndx2
)
7791 symtable2
[count2
++].u
.isym
= isym
;
7793 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7796 for (i
= 0; i
< count1
; i
++)
7798 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
7799 symtable1
[i
].u
.isym
->st_name
);
7801 for (i
= 0; i
< count2
; i
++)
7803 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
7804 symtable2
[i
].u
.isym
->st_name
);
7806 /* Sort symbol by name. */
7807 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7808 elf_sym_name_compare
);
7809 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7810 elf_sym_name_compare
);
7812 for (i
= 0; i
< count1
; i
++)
7813 /* Two symbols must have the same binding, type and name. */
7814 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
7815 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
7816 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7834 /* Return TRUE if 2 section types are compatible. */
7837 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
7838 bfd
*bbfd
, const asection
*bsec
)
7842 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
7843 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7846 return elf_section_type (asec
) == elf_section_type (bsec
);
7849 /* Final phase of ELF linker. */
7851 /* A structure we use to avoid passing large numbers of arguments. */
7853 struct elf_final_link_info
7855 /* General link information. */
7856 struct bfd_link_info
*info
;
7859 /* Symbol string table. */
7860 struct elf_strtab_hash
*symstrtab
;
7861 /* .hash section. */
7863 /* symbol version section (.gnu.version). */
7864 asection
*symver_sec
;
7865 /* Buffer large enough to hold contents of any section. */
7867 /* Buffer large enough to hold external relocs of any section. */
7868 void *external_relocs
;
7869 /* Buffer large enough to hold internal relocs of any section. */
7870 Elf_Internal_Rela
*internal_relocs
;
7871 /* Buffer large enough to hold external local symbols of any input
7873 bfd_byte
*external_syms
;
7874 /* And a buffer for symbol section indices. */
7875 Elf_External_Sym_Shndx
*locsym_shndx
;
7876 /* Buffer large enough to hold internal local symbols of any input
7878 Elf_Internal_Sym
*internal_syms
;
7879 /* Array large enough to hold a symbol index for each local symbol
7880 of any input BFD. */
7882 /* Array large enough to hold a section pointer for each local
7883 symbol of any input BFD. */
7884 asection
**sections
;
7885 /* Buffer for SHT_SYMTAB_SHNDX section. */
7886 Elf_External_Sym_Shndx
*symshndxbuf
;
7887 /* Number of STT_FILE syms seen. */
7888 size_t filesym_count
;
7891 /* This struct is used to pass information to elf_link_output_extsym. */
7893 struct elf_outext_info
7896 bfd_boolean localsyms
;
7897 bfd_boolean file_sym_done
;
7898 struct elf_final_link_info
*flinfo
;
7902 /* Support for evaluating a complex relocation.
7904 Complex relocations are generalized, self-describing relocations. The
7905 implementation of them consists of two parts: complex symbols, and the
7906 relocations themselves.
7908 The relocations are use a reserved elf-wide relocation type code (R_RELC
7909 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7910 information (start bit, end bit, word width, etc) into the addend. This
7911 information is extracted from CGEN-generated operand tables within gas.
7913 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7914 internal) representing prefix-notation expressions, including but not
7915 limited to those sorts of expressions normally encoded as addends in the
7916 addend field. The symbol mangling format is:
7919 | <unary-operator> ':' <node>
7920 | <binary-operator> ':' <node> ':' <node>
7923 <literal> := 's' <digits=N> ':' <N character symbol name>
7924 | 'S' <digits=N> ':' <N character section name>
7928 <binary-operator> := as in C
7929 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7932 set_symbol_value (bfd
*bfd_with_globals
,
7933 Elf_Internal_Sym
*isymbuf
,
7938 struct elf_link_hash_entry
**sym_hashes
;
7939 struct elf_link_hash_entry
*h
;
7940 size_t extsymoff
= locsymcount
;
7942 if (symidx
< locsymcount
)
7944 Elf_Internal_Sym
*sym
;
7946 sym
= isymbuf
+ symidx
;
7947 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
7949 /* It is a local symbol: move it to the
7950 "absolute" section and give it a value. */
7951 sym
->st_shndx
= SHN_ABS
;
7952 sym
->st_value
= val
;
7955 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
7959 /* It is a global symbol: set its link type
7960 to "defined" and give it a value. */
7962 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
7963 h
= sym_hashes
[symidx
- extsymoff
];
7964 while (h
->root
.type
== bfd_link_hash_indirect
7965 || h
->root
.type
== bfd_link_hash_warning
)
7966 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7967 h
->root
.type
= bfd_link_hash_defined
;
7968 h
->root
.u
.def
.value
= val
;
7969 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
7973 resolve_symbol (const char *name
,
7975 struct elf_final_link_info
*flinfo
,
7977 Elf_Internal_Sym
*isymbuf
,
7980 Elf_Internal_Sym
*sym
;
7981 struct bfd_link_hash_entry
*global_entry
;
7982 const char *candidate
= NULL
;
7983 Elf_Internal_Shdr
*symtab_hdr
;
7986 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
7988 for (i
= 0; i
< locsymcount
; ++ i
)
7992 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
7995 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
7996 symtab_hdr
->sh_link
,
7999 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8000 name
, candidate
, (unsigned long) sym
->st_value
);
8002 if (candidate
&& strcmp (candidate
, name
) == 0)
8004 asection
*sec
= flinfo
->sections
[i
];
8006 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
8007 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
8009 printf ("Found symbol with value %8.8lx\n",
8010 (unsigned long) *result
);
8016 /* Hmm, haven't found it yet. perhaps it is a global. */
8017 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
8018 FALSE
, FALSE
, TRUE
);
8022 if (global_entry
->type
== bfd_link_hash_defined
8023 || global_entry
->type
== bfd_link_hash_defweak
)
8025 *result
= (global_entry
->u
.def
.value
8026 + global_entry
->u
.def
.section
->output_section
->vma
8027 + global_entry
->u
.def
.section
->output_offset
);
8029 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8030 global_entry
->root
.string
, (unsigned long) *result
);
8038 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8039 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8040 names like "foo.end" which is the end address of section "foo". */
8043 resolve_section (const char *name
,
8051 for (curr
= sections
; curr
; curr
= curr
->next
)
8052 if (strcmp (curr
->name
, name
) == 0)
8054 *result
= curr
->vma
;
8058 /* Hmm. still haven't found it. try pseudo-section names. */
8059 /* FIXME: This could be coded more efficiently... */
8060 for (curr
= sections
; curr
; curr
= curr
->next
)
8062 len
= strlen (curr
->name
);
8063 if (len
> strlen (name
))
8066 if (strncmp (curr
->name
, name
, len
) == 0)
8068 if (strncmp (".end", name
+ len
, 4) == 0)
8070 *result
= curr
->vma
+ curr
->size
/ bfd_octets_per_byte (abfd
);
8074 /* Insert more pseudo-section names here, if you like. */
8082 undefined_reference (const char *reftype
, const char *name
)
8084 /* xgettext:c-format */
8085 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8090 eval_symbol (bfd_vma
*result
,
8093 struct elf_final_link_info
*flinfo
,
8095 Elf_Internal_Sym
*isymbuf
,
8104 const char *sym
= *symp
;
8106 bfd_boolean symbol_is_section
= FALSE
;
8111 if (len
< 1 || len
> sizeof (symbuf
))
8113 bfd_set_error (bfd_error_invalid_operation
);
8126 *result
= strtoul (sym
, (char **) symp
, 16);
8130 symbol_is_section
= TRUE
;
8134 symlen
= strtol (sym
, (char **) symp
, 10);
8135 sym
= *symp
+ 1; /* Skip the trailing ':'. */
8137 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
8139 bfd_set_error (bfd_error_invalid_operation
);
8143 memcpy (symbuf
, sym
, symlen
);
8144 symbuf
[symlen
] = '\0';
8145 *symp
= sym
+ symlen
;
8147 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8148 the symbol as a section, or vice-versa. so we're pretty liberal in our
8149 interpretation here; section means "try section first", not "must be a
8150 section", and likewise with symbol. */
8152 if (symbol_is_section
)
8154 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
8155 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8156 isymbuf
, locsymcount
))
8158 undefined_reference ("section", symbuf
);
8164 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8165 isymbuf
, locsymcount
)
8166 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8169 undefined_reference ("symbol", symbuf
);
8176 /* All that remains are operators. */
8178 #define UNARY_OP(op) \
8179 if (strncmp (sym, #op, strlen (#op)) == 0) \
8181 sym += strlen (#op); \
8185 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8186 isymbuf, locsymcount, signed_p)) \
8189 *result = op ((bfd_signed_vma) a); \
8195 #define BINARY_OP(op) \
8196 if (strncmp (sym, #op, strlen (#op)) == 0) \
8198 sym += strlen (#op); \
8202 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8203 isymbuf, locsymcount, signed_p)) \
8206 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8207 isymbuf, locsymcount, signed_p)) \
8210 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8240 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
8241 bfd_set_error (bfd_error_invalid_operation
);
8247 put_value (bfd_vma size
,
8248 unsigned long chunksz
,
8253 location
+= (size
- chunksz
);
8255 for (; size
; size
-= chunksz
, location
-= chunksz
)
8260 bfd_put_8 (input_bfd
, x
, location
);
8264 bfd_put_16 (input_bfd
, x
, location
);
8268 bfd_put_32 (input_bfd
, x
, location
);
8269 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8275 bfd_put_64 (input_bfd
, x
, location
);
8276 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8289 get_value (bfd_vma size
,
8290 unsigned long chunksz
,
8297 /* Sanity checks. */
8298 BFD_ASSERT (chunksz
<= sizeof (x
)
8301 && (size
% chunksz
) == 0
8302 && input_bfd
!= NULL
8303 && location
!= NULL
);
8305 if (chunksz
== sizeof (x
))
8307 BFD_ASSERT (size
== chunksz
);
8309 /* Make sure that we do not perform an undefined shift operation.
8310 We know that size == chunksz so there will only be one iteration
8311 of the loop below. */
8315 shift
= 8 * chunksz
;
8317 for (; size
; size
-= chunksz
, location
+= chunksz
)
8322 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
8325 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
8328 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
8332 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
8343 decode_complex_addend (unsigned long *start
, /* in bits */
8344 unsigned long *oplen
, /* in bits */
8345 unsigned long *len
, /* in bits */
8346 unsigned long *wordsz
, /* in bytes */
8347 unsigned long *chunksz
, /* in bytes */
8348 unsigned long *lsb0_p
,
8349 unsigned long *signed_p
,
8350 unsigned long *trunc_p
,
8351 unsigned long encoded
)
8353 * start
= encoded
& 0x3F;
8354 * len
= (encoded
>> 6) & 0x3F;
8355 * oplen
= (encoded
>> 12) & 0x3F;
8356 * wordsz
= (encoded
>> 18) & 0xF;
8357 * chunksz
= (encoded
>> 22) & 0xF;
8358 * lsb0_p
= (encoded
>> 27) & 1;
8359 * signed_p
= (encoded
>> 28) & 1;
8360 * trunc_p
= (encoded
>> 29) & 1;
8363 bfd_reloc_status_type
8364 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
8365 asection
*input_section ATTRIBUTE_UNUSED
,
8367 Elf_Internal_Rela
*rel
,
8370 bfd_vma shift
, x
, mask
;
8371 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
8372 bfd_reloc_status_type r
;
8374 /* Perform this reloc, since it is complex.
8375 (this is not to say that it necessarily refers to a complex
8376 symbol; merely that it is a self-describing CGEN based reloc.
8377 i.e. the addend has the complete reloc information (bit start, end,
8378 word size, etc) encoded within it.). */
8380 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
8381 &chunksz
, &lsb0_p
, &signed_p
,
8382 &trunc_p
, rel
->r_addend
);
8384 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
8387 shift
= (start
+ 1) - len
;
8389 shift
= (8 * wordsz
) - (start
+ len
);
8391 x
= get_value (wordsz
, chunksz
, input_bfd
,
8392 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8395 printf ("Doing complex reloc: "
8396 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8397 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8398 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8399 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
8400 oplen
, (unsigned long) x
, (unsigned long) mask
,
8401 (unsigned long) relocation
);
8406 /* Now do an overflow check. */
8407 r
= bfd_check_overflow ((signed_p
8408 ? complain_overflow_signed
8409 : complain_overflow_unsigned
),
8410 len
, 0, (8 * wordsz
),
8414 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
8417 printf (" relocation: %8.8lx\n"
8418 " shifted mask: %8.8lx\n"
8419 " shifted/masked reloc: %8.8lx\n"
8420 " result: %8.8lx\n",
8421 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
8422 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
8424 put_value (wordsz
, chunksz
, input_bfd
, x
,
8425 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8429 /* Functions to read r_offset from external (target order) reloc
8430 entry. Faster than bfd_getl32 et al, because we let the compiler
8431 know the value is aligned. */
8434 ext32l_r_offset (const void *p
)
8441 const union aligned32
*a
8442 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8444 uint32_t aval
= ( (uint32_t) a
->c
[0]
8445 | (uint32_t) a
->c
[1] << 8
8446 | (uint32_t) a
->c
[2] << 16
8447 | (uint32_t) a
->c
[3] << 24);
8452 ext32b_r_offset (const void *p
)
8459 const union aligned32
*a
8460 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8462 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
8463 | (uint32_t) a
->c
[1] << 16
8464 | (uint32_t) a
->c
[2] << 8
8465 | (uint32_t) a
->c
[3]);
8469 #ifdef BFD_HOST_64_BIT
8471 ext64l_r_offset (const void *p
)
8478 const union aligned64
*a
8479 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8481 uint64_t aval
= ( (uint64_t) a
->c
[0]
8482 | (uint64_t) a
->c
[1] << 8
8483 | (uint64_t) a
->c
[2] << 16
8484 | (uint64_t) a
->c
[3] << 24
8485 | (uint64_t) a
->c
[4] << 32
8486 | (uint64_t) a
->c
[5] << 40
8487 | (uint64_t) a
->c
[6] << 48
8488 | (uint64_t) a
->c
[7] << 56);
8493 ext64b_r_offset (const void *p
)
8500 const union aligned64
*a
8501 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8503 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
8504 | (uint64_t) a
->c
[1] << 48
8505 | (uint64_t) a
->c
[2] << 40
8506 | (uint64_t) a
->c
[3] << 32
8507 | (uint64_t) a
->c
[4] << 24
8508 | (uint64_t) a
->c
[5] << 16
8509 | (uint64_t) a
->c
[6] << 8
8510 | (uint64_t) a
->c
[7]);
8515 /* When performing a relocatable link, the input relocations are
8516 preserved. But, if they reference global symbols, the indices
8517 referenced must be updated. Update all the relocations found in
8521 elf_link_adjust_relocs (bfd
*abfd
,
8523 struct bfd_elf_section_reloc_data
*reldata
,
8527 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8529 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8530 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8531 bfd_vma r_type_mask
;
8533 unsigned int count
= reldata
->count
;
8534 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
8536 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
8538 swap_in
= bed
->s
->swap_reloc_in
;
8539 swap_out
= bed
->s
->swap_reloc_out
;
8541 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
8543 swap_in
= bed
->s
->swap_reloca_in
;
8544 swap_out
= bed
->s
->swap_reloca_out
;
8549 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
8552 if (bed
->s
->arch_size
== 32)
8559 r_type_mask
= 0xffffffff;
8563 erela
= reldata
->hdr
->contents
;
8564 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
8566 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
8569 if (*rel_hash
== NULL
)
8572 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
8574 (*swap_in
) (abfd
, erela
, irela
);
8575 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
8576 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
8577 | (irela
[j
].r_info
& r_type_mask
));
8578 (*swap_out
) (abfd
, irela
, erela
);
8581 if (bed
->elf_backend_update_relocs
)
8582 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
8584 if (sort
&& count
!= 0)
8586 bfd_vma (*ext_r_off
) (const void *);
8589 bfd_byte
*base
, *end
, *p
, *loc
;
8590 bfd_byte
*buf
= NULL
;
8592 if (bed
->s
->arch_size
== 32)
8594 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8595 ext_r_off
= ext32l_r_offset
;
8596 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8597 ext_r_off
= ext32b_r_offset
;
8603 #ifdef BFD_HOST_64_BIT
8604 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8605 ext_r_off
= ext64l_r_offset
;
8606 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8607 ext_r_off
= ext64b_r_offset
;
8613 /* Must use a stable sort here. A modified insertion sort,
8614 since the relocs are mostly sorted already. */
8615 elt_size
= reldata
->hdr
->sh_entsize
;
8616 base
= reldata
->hdr
->contents
;
8617 end
= base
+ count
* elt_size
;
8618 if (elt_size
> sizeof (Elf64_External_Rela
))
8621 /* Ensure the first element is lowest. This acts as a sentinel,
8622 speeding the main loop below. */
8623 r_off
= (*ext_r_off
) (base
);
8624 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
8626 bfd_vma r_off2
= (*ext_r_off
) (p
);
8635 /* Don't just swap *base and *loc as that changes the order
8636 of the original base[0] and base[1] if they happen to
8637 have the same r_offset. */
8638 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
8639 memcpy (onebuf
, loc
, elt_size
);
8640 memmove (base
+ elt_size
, base
, loc
- base
);
8641 memcpy (base
, onebuf
, elt_size
);
8644 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
8646 /* base to p is sorted, *p is next to insert. */
8647 r_off
= (*ext_r_off
) (p
);
8648 /* Search the sorted region for location to insert. */
8650 while (r_off
< (*ext_r_off
) (loc
))
8655 /* Chances are there is a run of relocs to insert here,
8656 from one of more input files. Files are not always
8657 linked in order due to the way elf_link_input_bfd is
8658 called. See pr17666. */
8659 size_t sortlen
= p
- loc
;
8660 bfd_vma r_off2
= (*ext_r_off
) (loc
);
8661 size_t runlen
= elt_size
;
8662 size_t buf_size
= 96 * 1024;
8663 while (p
+ runlen
< end
8664 && (sortlen
<= buf_size
8665 || runlen
+ elt_size
<= buf_size
)
8666 && r_off2
> (*ext_r_off
) (p
+ runlen
))
8670 buf
= bfd_malloc (buf_size
);
8674 if (runlen
< sortlen
)
8676 memcpy (buf
, p
, runlen
);
8677 memmove (loc
+ runlen
, loc
, sortlen
);
8678 memcpy (loc
, buf
, runlen
);
8682 memcpy (buf
, loc
, sortlen
);
8683 memmove (loc
, p
, runlen
);
8684 memcpy (loc
+ runlen
, buf
, sortlen
);
8686 p
+= runlen
- elt_size
;
8689 /* Hashes are no longer valid. */
8690 free (reldata
->hashes
);
8691 reldata
->hashes
= NULL
;
8697 struct elf_link_sort_rela
8703 enum elf_reloc_type_class type
;
8704 /* We use this as an array of size int_rels_per_ext_rel. */
8705 Elf_Internal_Rela rela
[1];
8709 elf_link_sort_cmp1 (const void *A
, const void *B
)
8711 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8712 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8713 int relativea
, relativeb
;
8715 relativea
= a
->type
== reloc_class_relative
;
8716 relativeb
= b
->type
== reloc_class_relative
;
8718 if (relativea
< relativeb
)
8720 if (relativea
> relativeb
)
8722 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
8724 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
8726 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8728 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8734 elf_link_sort_cmp2 (const void *A
, const void *B
)
8736 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8737 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8739 if (a
->type
< b
->type
)
8741 if (a
->type
> b
->type
)
8743 if (a
->u
.offset
< b
->u
.offset
)
8745 if (a
->u
.offset
> b
->u
.offset
)
8747 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8749 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8755 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
8757 asection
*dynamic_relocs
;
8760 bfd_size_type count
, size
;
8761 size_t i
, ret
, sort_elt
, ext_size
;
8762 bfd_byte
*sort
, *s_non_relative
, *p
;
8763 struct elf_link_sort_rela
*sq
;
8764 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8765 int i2e
= bed
->s
->int_rels_per_ext_rel
;
8766 unsigned int opb
= bfd_octets_per_byte (abfd
);
8767 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8768 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8769 struct bfd_link_order
*lo
;
8771 bfd_boolean use_rela
;
8773 /* Find a dynamic reloc section. */
8774 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
8775 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
8776 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
8777 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8779 bfd_boolean use_rela_initialised
= FALSE
;
8781 /* This is just here to stop gcc from complaining.
8782 Its initialization checking code is not perfect. */
8785 /* Both sections are present. Examine the sizes
8786 of the indirect sections to help us choose. */
8787 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8788 if (lo
->type
== bfd_indirect_link_order
)
8790 asection
*o
= lo
->u
.indirect
.section
;
8792 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8794 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8795 /* Section size is divisible by both rel and rela sizes.
8796 It is of no help to us. */
8800 /* Section size is only divisible by rela. */
8801 if (use_rela_initialised
&& (use_rela
== FALSE
))
8803 _bfd_error_handler (_("%B: Unable to sort relocs - "
8804 "they are in more than one size"),
8806 bfd_set_error (bfd_error_invalid_operation
);
8812 use_rela_initialised
= TRUE
;
8816 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8818 /* Section size is only divisible by rel. */
8819 if (use_rela_initialised
&& (use_rela
== TRUE
))
8821 _bfd_error_handler (_("%B: Unable to sort relocs - "
8822 "they are in more than one size"),
8824 bfd_set_error (bfd_error_invalid_operation
);
8830 use_rela_initialised
= TRUE
;
8835 /* The section size is not divisible by either -
8836 something is wrong. */
8837 _bfd_error_handler (_("%B: Unable to sort relocs - "
8838 "they are of an unknown size"), abfd
);
8839 bfd_set_error (bfd_error_invalid_operation
);
8844 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8845 if (lo
->type
== bfd_indirect_link_order
)
8847 asection
*o
= lo
->u
.indirect
.section
;
8849 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8851 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8852 /* Section size is divisible by both rel and rela sizes.
8853 It is of no help to us. */
8857 /* Section size is only divisible by rela. */
8858 if (use_rela_initialised
&& (use_rela
== FALSE
))
8860 _bfd_error_handler (_("%B: Unable to sort relocs - "
8861 "they are in more than one size"),
8863 bfd_set_error (bfd_error_invalid_operation
);
8869 use_rela_initialised
= TRUE
;
8873 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8875 /* Section size is only divisible by rel. */
8876 if (use_rela_initialised
&& (use_rela
== TRUE
))
8878 _bfd_error_handler (_("%B: Unable to sort relocs - "
8879 "they are in more than one size"),
8881 bfd_set_error (bfd_error_invalid_operation
);
8887 use_rela_initialised
= TRUE
;
8892 /* The section size is not divisible by either -
8893 something is wrong. */
8894 _bfd_error_handler (_("%B: Unable to sort relocs - "
8895 "they are of an unknown size"), abfd
);
8896 bfd_set_error (bfd_error_invalid_operation
);
8901 if (! use_rela_initialised
)
8905 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
8907 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8914 dynamic_relocs
= rela_dyn
;
8915 ext_size
= bed
->s
->sizeof_rela
;
8916 swap_in
= bed
->s
->swap_reloca_in
;
8917 swap_out
= bed
->s
->swap_reloca_out
;
8921 dynamic_relocs
= rel_dyn
;
8922 ext_size
= bed
->s
->sizeof_rel
;
8923 swap_in
= bed
->s
->swap_reloc_in
;
8924 swap_out
= bed
->s
->swap_reloc_out
;
8928 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8929 if (lo
->type
== bfd_indirect_link_order
)
8930 size
+= lo
->u
.indirect
.section
->size
;
8932 if (size
!= dynamic_relocs
->size
)
8935 sort_elt
= (sizeof (struct elf_link_sort_rela
)
8936 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
8938 count
= dynamic_relocs
->size
/ ext_size
;
8941 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
8945 (*info
->callbacks
->warning
)
8946 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
8950 if (bed
->s
->arch_size
== 32)
8951 r_sym_mask
= ~(bfd_vma
) 0xff;
8953 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
8955 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8956 if (lo
->type
== bfd_indirect_link_order
)
8958 bfd_byte
*erel
, *erelend
;
8959 asection
*o
= lo
->u
.indirect
.section
;
8961 if (o
->contents
== NULL
&& o
->size
!= 0)
8963 /* This is a reloc section that is being handled as a normal
8964 section. See bfd_section_from_shdr. We can't combine
8965 relocs in this case. */
8970 erelend
= o
->contents
+ o
->size
;
8971 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
8973 while (erel
< erelend
)
8975 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8977 (*swap_in
) (abfd
, erel
, s
->rela
);
8978 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
8979 s
->u
.sym_mask
= r_sym_mask
;
8985 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
8987 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
8989 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8990 if (s
->type
!= reloc_class_relative
)
8996 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
8997 for (; i
< count
; i
++, p
+= sort_elt
)
8999 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
9000 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
9002 sp
->u
.offset
= sq
->rela
->r_offset
;
9005 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
9007 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
9008 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
9010 /* We have plt relocs in .rela.dyn. */
9011 sq
= (struct elf_link_sort_rela
*) sort
;
9012 for (i
= 0; i
< count
; i
++)
9013 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
9015 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
9017 struct bfd_link_order
**plo
;
9018 /* Put srelplt link_order last. This is so the output_offset
9019 set in the next loop is correct for DT_JMPREL. */
9020 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
9021 if ((*plo
)->type
== bfd_indirect_link_order
9022 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
9028 plo
= &(*plo
)->next
;
9031 dynamic_relocs
->map_tail
.link_order
= lo
;
9036 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9037 if (lo
->type
== bfd_indirect_link_order
)
9039 bfd_byte
*erel
, *erelend
;
9040 asection
*o
= lo
->u
.indirect
.section
;
9043 erelend
= o
->contents
+ o
->size
;
9044 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
9045 while (erel
< erelend
)
9047 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9048 (*swap_out
) (abfd
, s
->rela
, erel
);
9055 *psec
= dynamic_relocs
;
9059 /* Add a symbol to the output symbol string table. */
9062 elf_link_output_symstrtab (struct elf_final_link_info
*flinfo
,
9064 Elf_Internal_Sym
*elfsym
,
9065 asection
*input_sec
,
9066 struct elf_link_hash_entry
*h
)
9068 int (*output_symbol_hook
)
9069 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
9070 struct elf_link_hash_entry
*);
9071 struct elf_link_hash_table
*hash_table
;
9072 const struct elf_backend_data
*bed
;
9073 bfd_size_type strtabsize
;
9075 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9077 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9078 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
9079 if (output_symbol_hook
!= NULL
)
9081 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
9088 || (input_sec
->flags
& SEC_EXCLUDE
))
9089 elfsym
->st_name
= (unsigned long) -1;
9092 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9093 to get the final offset for st_name. */
9095 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
9097 if (elfsym
->st_name
== (unsigned long) -1)
9101 hash_table
= elf_hash_table (flinfo
->info
);
9102 strtabsize
= hash_table
->strtabsize
;
9103 if (strtabsize
<= hash_table
->strtabcount
)
9105 strtabsize
+= strtabsize
;
9106 hash_table
->strtabsize
= strtabsize
;
9107 strtabsize
*= sizeof (*hash_table
->strtab
);
9109 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
9111 if (hash_table
->strtab
== NULL
)
9114 hash_table
->strtab
[hash_table
->strtabcount
].sym
= *elfsym
;
9115 hash_table
->strtab
[hash_table
->strtabcount
].dest_index
9116 = hash_table
->strtabcount
;
9117 hash_table
->strtab
[hash_table
->strtabcount
].destshndx_index
9118 = flinfo
->symshndxbuf
? bfd_get_symcount (flinfo
->output_bfd
) : 0;
9120 bfd_get_symcount (flinfo
->output_bfd
) += 1;
9121 hash_table
->strtabcount
+= 1;
9126 /* Swap symbols out to the symbol table and flush the output symbols to
9130 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
9132 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
9135 const struct elf_backend_data
*bed
;
9137 Elf_Internal_Shdr
*hdr
;
9141 if (!hash_table
->strtabcount
)
9144 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9146 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9148 amt
= bed
->s
->sizeof_sym
* hash_table
->strtabcount
;
9149 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
9153 if (flinfo
->symshndxbuf
)
9155 amt
= sizeof (Elf_External_Sym_Shndx
);
9156 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
9157 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
9158 if (flinfo
->symshndxbuf
== NULL
)
9165 for (i
= 0; i
< hash_table
->strtabcount
; i
++)
9167 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
9168 if (elfsym
->sym
.st_name
== (unsigned long) -1)
9169 elfsym
->sym
.st_name
= 0;
9172 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
9173 elfsym
->sym
.st_name
);
9174 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
9175 ((bfd_byte
*) symbuf
9176 + (elfsym
->dest_index
9177 * bed
->s
->sizeof_sym
)),
9178 (flinfo
->symshndxbuf
9179 + elfsym
->destshndx_index
));
9182 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
9183 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
9184 amt
= hash_table
->strtabcount
* bed
->s
->sizeof_sym
;
9185 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
9186 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
9188 hdr
->sh_size
+= amt
;
9196 free (hash_table
->strtab
);
9197 hash_table
->strtab
= NULL
;
9202 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9205 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
9207 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
9208 && sym
->st_shndx
< SHN_LORESERVE
)
9210 /* The gABI doesn't support dynamic symbols in output sections
9213 /* xgettext:c-format */
9214 (_("%B: Too many sections: %d (>= %d)"),
9215 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
9216 bfd_set_error (bfd_error_nonrepresentable_section
);
9222 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9223 allowing an unsatisfied unversioned symbol in the DSO to match a
9224 versioned symbol that would normally require an explicit version.
9225 We also handle the case that a DSO references a hidden symbol
9226 which may be satisfied by a versioned symbol in another DSO. */
9229 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
9230 const struct elf_backend_data
*bed
,
9231 struct elf_link_hash_entry
*h
)
9234 struct elf_link_loaded_list
*loaded
;
9236 if (!is_elf_hash_table (info
->hash
))
9239 /* Check indirect symbol. */
9240 while (h
->root
.type
== bfd_link_hash_indirect
)
9241 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9243 switch (h
->root
.type
)
9249 case bfd_link_hash_undefined
:
9250 case bfd_link_hash_undefweak
:
9251 abfd
= h
->root
.u
.undef
.abfd
;
9253 || (abfd
->flags
& DYNAMIC
) == 0
9254 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
9258 case bfd_link_hash_defined
:
9259 case bfd_link_hash_defweak
:
9260 abfd
= h
->root
.u
.def
.section
->owner
;
9263 case bfd_link_hash_common
:
9264 abfd
= h
->root
.u
.c
.p
->section
->owner
;
9267 BFD_ASSERT (abfd
!= NULL
);
9269 for (loaded
= elf_hash_table (info
)->loaded
;
9271 loaded
= loaded
->next
)
9274 Elf_Internal_Shdr
*hdr
;
9278 Elf_Internal_Shdr
*versymhdr
;
9279 Elf_Internal_Sym
*isym
;
9280 Elf_Internal_Sym
*isymend
;
9281 Elf_Internal_Sym
*isymbuf
;
9282 Elf_External_Versym
*ever
;
9283 Elf_External_Versym
*extversym
;
9285 input
= loaded
->abfd
;
9287 /* We check each DSO for a possible hidden versioned definition. */
9289 || (input
->flags
& DYNAMIC
) == 0
9290 || elf_dynversym (input
) == 0)
9293 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
9295 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9296 if (elf_bad_symtab (input
))
9298 extsymcount
= symcount
;
9303 extsymcount
= symcount
- hdr
->sh_info
;
9304 extsymoff
= hdr
->sh_info
;
9307 if (extsymcount
== 0)
9310 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
9312 if (isymbuf
== NULL
)
9315 /* Read in any version definitions. */
9316 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
9317 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
9318 if (extversym
== NULL
)
9321 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
9322 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
9323 != versymhdr
->sh_size
))
9331 ever
= extversym
+ extsymoff
;
9332 isymend
= isymbuf
+ extsymcount
;
9333 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
9336 Elf_Internal_Versym iver
;
9337 unsigned short version_index
;
9339 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
9340 || isym
->st_shndx
== SHN_UNDEF
)
9343 name
= bfd_elf_string_from_elf_section (input
,
9346 if (strcmp (name
, h
->root
.root
.string
) != 0)
9349 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
9351 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
9353 && h
->forced_local
))
9355 /* If we have a non-hidden versioned sym, then it should
9356 have provided a definition for the undefined sym unless
9357 it is defined in a non-shared object and forced local.
9362 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
9363 if (version_index
== 1 || version_index
== 2)
9365 /* This is the base or first version. We can use it. */
9379 /* Convert ELF common symbol TYPE. */
9382 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
9384 /* Commom symbol can only appear in relocatable link. */
9385 if (!bfd_link_relocatable (info
))
9387 switch (info
->elf_stt_common
)
9391 case elf_stt_common
:
9394 case no_elf_stt_common
:
9401 /* Add an external symbol to the symbol table. This is called from
9402 the hash table traversal routine. When generating a shared object,
9403 we go through the symbol table twice. The first time we output
9404 anything that might have been forced to local scope in a version
9405 script. The second time we output the symbols that are still
9409 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
9411 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
9412 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
9413 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
9415 Elf_Internal_Sym sym
;
9416 asection
*input_sec
;
9417 const struct elf_backend_data
*bed
;
9422 if (h
->root
.type
== bfd_link_hash_warning
)
9424 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9425 if (h
->root
.type
== bfd_link_hash_new
)
9429 /* Decide whether to output this symbol in this pass. */
9430 if (eoinfo
->localsyms
)
9432 if (!h
->forced_local
)
9437 if (h
->forced_local
)
9441 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9443 if (h
->root
.type
== bfd_link_hash_undefined
)
9445 /* If we have an undefined symbol reference here then it must have
9446 come from a shared library that is being linked in. (Undefined
9447 references in regular files have already been handled unless
9448 they are in unreferenced sections which are removed by garbage
9450 bfd_boolean ignore_undef
= FALSE
;
9452 /* Some symbols may be special in that the fact that they're
9453 undefined can be safely ignored - let backend determine that. */
9454 if (bed
->elf_backend_ignore_undef_symbol
)
9455 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
9457 /* If we are reporting errors for this situation then do so now. */
9460 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
9461 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
9462 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
9463 (*flinfo
->info
->callbacks
->undefined_symbol
)
9464 (flinfo
->info
, h
->root
.root
.string
,
9465 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
,
9467 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
);
9469 /* Strip a global symbol defined in a discarded section. */
9474 /* We should also warn if a forced local symbol is referenced from
9475 shared libraries. */
9476 if (bfd_link_executable (flinfo
->info
)
9481 && h
->ref_dynamic_nonweak
9482 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
9486 struct elf_link_hash_entry
*hi
= h
;
9488 /* Check indirect symbol. */
9489 while (hi
->root
.type
== bfd_link_hash_indirect
)
9490 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
9492 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
9493 /* xgettext:c-format */
9494 msg
= _("%B: internal symbol `%s' in %B is referenced by DSO");
9495 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
9496 /* xgettext:c-format */
9497 msg
= _("%B: hidden symbol `%s' in %B is referenced by DSO");
9499 /* xgettext:c-format */
9500 msg
= _("%B: local symbol `%s' in %B is referenced by DSO");
9501 def_bfd
= flinfo
->output_bfd
;
9502 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
9503 def_bfd
= hi
->root
.u
.def
.section
->owner
;
9504 _bfd_error_handler (msg
, flinfo
->output_bfd
,
9505 h
->root
.root
.string
, def_bfd
);
9506 bfd_set_error (bfd_error_bad_value
);
9507 eoinfo
->failed
= TRUE
;
9511 /* We don't want to output symbols that have never been mentioned by
9512 a regular file, or that we have been told to strip. However, if
9513 h->indx is set to -2, the symbol is used by a reloc and we must
9518 else if ((h
->def_dynamic
9520 || h
->root
.type
== bfd_link_hash_new
)
9524 else if (flinfo
->info
->strip
== strip_all
)
9526 else if (flinfo
->info
->strip
== strip_some
9527 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
9528 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
9530 else if ((h
->root
.type
== bfd_link_hash_defined
9531 || h
->root
.type
== bfd_link_hash_defweak
)
9532 && ((flinfo
->info
->strip_discarded
9533 && discarded_section (h
->root
.u
.def
.section
))
9534 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
9535 && h
->root
.u
.def
.section
->owner
!= NULL
9536 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
9538 else if ((h
->root
.type
== bfd_link_hash_undefined
9539 || h
->root
.type
== bfd_link_hash_undefweak
)
9540 && h
->root
.u
.undef
.abfd
!= NULL
9541 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
9546 /* If we're stripping it, and it's not a dynamic symbol, there's
9547 nothing else to do. However, if it is a forced local symbol or
9548 an ifunc symbol we need to give the backend finish_dynamic_symbol
9549 function a chance to make it dynamic. */
9552 && type
!= STT_GNU_IFUNC
9553 && !h
->forced_local
)
9557 sym
.st_size
= h
->size
;
9558 sym
.st_other
= h
->other
;
9559 switch (h
->root
.type
)
9562 case bfd_link_hash_new
:
9563 case bfd_link_hash_warning
:
9567 case bfd_link_hash_undefined
:
9568 case bfd_link_hash_undefweak
:
9569 input_sec
= bfd_und_section_ptr
;
9570 sym
.st_shndx
= SHN_UNDEF
;
9573 case bfd_link_hash_defined
:
9574 case bfd_link_hash_defweak
:
9576 input_sec
= h
->root
.u
.def
.section
;
9577 if (input_sec
->output_section
!= NULL
)
9580 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
9581 input_sec
->output_section
);
9582 if (sym
.st_shndx
== SHN_BAD
)
9585 /* xgettext:c-format */
9586 (_("%B: could not find output section %A for input section %A"),
9587 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
9588 bfd_set_error (bfd_error_nonrepresentable_section
);
9589 eoinfo
->failed
= TRUE
;
9593 /* ELF symbols in relocatable files are section relative,
9594 but in nonrelocatable files they are virtual
9596 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
9597 if (!bfd_link_relocatable (flinfo
->info
))
9599 sym
.st_value
+= input_sec
->output_section
->vma
;
9600 if (h
->type
== STT_TLS
)
9602 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
9603 if (tls_sec
!= NULL
)
9604 sym
.st_value
-= tls_sec
->vma
;
9610 BFD_ASSERT (input_sec
->owner
== NULL
9611 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
9612 sym
.st_shndx
= SHN_UNDEF
;
9613 input_sec
= bfd_und_section_ptr
;
9618 case bfd_link_hash_common
:
9619 input_sec
= h
->root
.u
.c
.p
->section
;
9620 sym
.st_shndx
= bed
->common_section_index (input_sec
);
9621 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
9624 case bfd_link_hash_indirect
:
9625 /* These symbols are created by symbol versioning. They point
9626 to the decorated version of the name. For example, if the
9627 symbol foo@@GNU_1.2 is the default, which should be used when
9628 foo is used with no version, then we add an indirect symbol
9629 foo which points to foo@@GNU_1.2. We ignore these symbols,
9630 since the indirected symbol is already in the hash table. */
9634 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
9635 switch (h
->root
.type
)
9637 case bfd_link_hash_common
:
9638 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9640 case bfd_link_hash_defined
:
9641 case bfd_link_hash_defweak
:
9642 if (bed
->common_definition (&sym
))
9643 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9647 case bfd_link_hash_undefined
:
9648 case bfd_link_hash_undefweak
:
9654 if (h
->forced_local
)
9656 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
9657 /* Turn off visibility on local symbol. */
9658 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
9660 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9661 else if (h
->unique_global
&& h
->def_regular
)
9662 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
9663 else if (h
->root
.type
== bfd_link_hash_undefweak
9664 || h
->root
.type
== bfd_link_hash_defweak
)
9665 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
9667 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
9668 sym
.st_target_internal
= h
->target_internal
;
9670 /* Give the processor backend a chance to tweak the symbol value,
9671 and also to finish up anything that needs to be done for this
9672 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9673 forced local syms when non-shared is due to a historical quirk.
9674 STT_GNU_IFUNC symbol must go through PLT. */
9675 if ((h
->type
== STT_GNU_IFUNC
9677 && !bfd_link_relocatable (flinfo
->info
))
9678 || ((h
->dynindx
!= -1
9680 && ((bfd_link_pic (flinfo
->info
)
9681 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
9682 || h
->root
.type
!= bfd_link_hash_undefweak
))
9683 || !h
->forced_local
)
9684 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
9686 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
9687 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
9689 eoinfo
->failed
= TRUE
;
9694 /* If we are marking the symbol as undefined, and there are no
9695 non-weak references to this symbol from a regular object, then
9696 mark the symbol as weak undefined; if there are non-weak
9697 references, mark the symbol as strong. We can't do this earlier,
9698 because it might not be marked as undefined until the
9699 finish_dynamic_symbol routine gets through with it. */
9700 if (sym
.st_shndx
== SHN_UNDEF
9702 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
9703 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
9706 type
= ELF_ST_TYPE (sym
.st_info
);
9708 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9709 if (type
== STT_GNU_IFUNC
)
9712 if (h
->ref_regular_nonweak
)
9713 bindtype
= STB_GLOBAL
;
9715 bindtype
= STB_WEAK
;
9716 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
9719 /* If this is a symbol defined in a dynamic library, don't use the
9720 symbol size from the dynamic library. Relinking an executable
9721 against a new library may introduce gratuitous changes in the
9722 executable's symbols if we keep the size. */
9723 if (sym
.st_shndx
== SHN_UNDEF
9728 /* If a non-weak symbol with non-default visibility is not defined
9729 locally, it is a fatal error. */
9730 if (!bfd_link_relocatable (flinfo
->info
)
9731 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
9732 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
9733 && h
->root
.type
== bfd_link_hash_undefined
9738 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
9739 /* xgettext:c-format */
9740 msg
= _("%B: protected symbol `%s' isn't defined");
9741 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
9742 /* xgettext:c-format */
9743 msg
= _("%B: internal symbol `%s' isn't defined");
9745 /* xgettext:c-format */
9746 msg
= _("%B: hidden symbol `%s' isn't defined");
9747 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
9748 bfd_set_error (bfd_error_bad_value
);
9749 eoinfo
->failed
= TRUE
;
9753 /* If this symbol should be put in the .dynsym section, then put it
9754 there now. We already know the symbol index. We also fill in
9755 the entry in the .hash section. */
9756 if (elf_hash_table (flinfo
->info
)->dynsym
!= NULL
9758 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
)
9762 /* Since there is no version information in the dynamic string,
9763 if there is no version info in symbol version section, we will
9764 have a run-time problem if not linking executable, referenced
9765 by shared library, or not bound locally. */
9766 if (h
->verinfo
.verdef
== NULL
9767 && (!bfd_link_executable (flinfo
->info
)
9769 || !h
->def_regular
))
9771 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
9773 if (p
&& p
[1] != '\0')
9776 /* xgettext:c-format */
9777 (_("%B: No symbol version section for versioned symbol `%s'"),
9778 flinfo
->output_bfd
, h
->root
.root
.string
);
9779 eoinfo
->failed
= TRUE
;
9784 sym
.st_name
= h
->dynstr_index
;
9785 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
9786 + h
->dynindx
* bed
->s
->sizeof_sym
);
9787 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
9789 eoinfo
->failed
= TRUE
;
9792 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
9794 if (flinfo
->hash_sec
!= NULL
)
9796 size_t hash_entry_size
;
9797 bfd_byte
*bucketpos
;
9802 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
9803 bucket
= h
->u
.elf_hash_value
% bucketcount
;
9806 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
9807 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
9808 + (bucket
+ 2) * hash_entry_size
);
9809 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
9810 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
9812 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
9813 ((bfd_byte
*) flinfo
->hash_sec
->contents
9814 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
9817 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
9819 Elf_Internal_Versym iversym
;
9820 Elf_External_Versym
*eversym
;
9822 if (!h
->def_regular
)
9824 if (h
->verinfo
.verdef
== NULL
9825 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
9826 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
9827 iversym
.vs_vers
= 0;
9829 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
9833 if (h
->verinfo
.vertree
== NULL
)
9834 iversym
.vs_vers
= 1;
9836 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
9837 if (flinfo
->info
->create_default_symver
)
9841 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9843 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
9844 iversym
.vs_vers
|= VERSYM_HIDDEN
;
9846 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
9847 eversym
+= h
->dynindx
;
9848 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
9852 /* If the symbol is undefined, and we didn't output it to .dynsym,
9853 strip it from .symtab too. Obviously we can't do this for
9854 relocatable output or when needed for --emit-relocs. */
9855 else if (input_sec
== bfd_und_section_ptr
9857 && !bfd_link_relocatable (flinfo
->info
))
9859 /* Also strip others that we couldn't earlier due to dynamic symbol
9863 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
9866 /* Output a FILE symbol so that following locals are not associated
9867 with the wrong input file. We need one for forced local symbols
9868 if we've seen more than one FILE symbol or when we have exactly
9869 one FILE symbol but global symbols are present in a file other
9870 than the one with the FILE symbol. We also need one if linker
9871 defined symbols are present. In practice these conditions are
9872 always met, so just emit the FILE symbol unconditionally. */
9873 if (eoinfo
->localsyms
9874 && !eoinfo
->file_sym_done
9875 && eoinfo
->flinfo
->filesym_count
!= 0)
9877 Elf_Internal_Sym fsym
;
9879 memset (&fsym
, 0, sizeof (fsym
));
9880 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
9881 fsym
.st_shndx
= SHN_ABS
;
9882 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
9883 bfd_und_section_ptr
, NULL
))
9886 eoinfo
->file_sym_done
= TRUE
;
9889 indx
= bfd_get_symcount (flinfo
->output_bfd
);
9890 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
9894 eoinfo
->failed
= TRUE
;
9899 else if (h
->indx
== -2)
9905 /* Return TRUE if special handling is done for relocs in SEC against
9906 symbols defined in discarded sections. */
9909 elf_section_ignore_discarded_relocs (asection
*sec
)
9911 const struct elf_backend_data
*bed
;
9913 switch (sec
->sec_info_type
)
9915 case SEC_INFO_TYPE_STABS
:
9916 case SEC_INFO_TYPE_EH_FRAME
:
9917 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
9923 bed
= get_elf_backend_data (sec
->owner
);
9924 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
9925 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
9931 /* Return a mask saying how ld should treat relocations in SEC against
9932 symbols defined in discarded sections. If this function returns
9933 COMPLAIN set, ld will issue a warning message. If this function
9934 returns PRETEND set, and the discarded section was link-once and the
9935 same size as the kept link-once section, ld will pretend that the
9936 symbol was actually defined in the kept section. Otherwise ld will
9937 zero the reloc (at least that is the intent, but some cooperation by
9938 the target dependent code is needed, particularly for REL targets). */
9941 _bfd_elf_default_action_discarded (asection
*sec
)
9943 if (sec
->flags
& SEC_DEBUGGING
)
9946 if (strcmp (".eh_frame", sec
->name
) == 0)
9949 if (strcmp (".gcc_except_table", sec
->name
) == 0)
9952 return COMPLAIN
| PRETEND
;
9955 /* Find a match between a section and a member of a section group. */
9958 match_group_member (asection
*sec
, asection
*group
,
9959 struct bfd_link_info
*info
)
9961 asection
*first
= elf_next_in_group (group
);
9962 asection
*s
= first
;
9966 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
9969 s
= elf_next_in_group (s
);
9977 /* Check if the kept section of a discarded section SEC can be used
9978 to replace it. Return the replacement if it is OK. Otherwise return
9982 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
9986 kept
= sec
->kept_section
;
9989 if ((kept
->flags
& SEC_GROUP
) != 0)
9990 kept
= match_group_member (sec
, kept
, info
);
9992 && ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
9993 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
)))
9995 sec
->kept_section
= kept
;
10000 /* Link an input file into the linker output file. This function
10001 handles all the sections and relocations of the input file at once.
10002 This is so that we only have to read the local symbols once, and
10003 don't have to keep them in memory. */
10006 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
10008 int (*relocate_section
)
10009 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
10010 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
10012 Elf_Internal_Shdr
*symtab_hdr
;
10013 size_t locsymcount
;
10015 Elf_Internal_Sym
*isymbuf
;
10016 Elf_Internal_Sym
*isym
;
10017 Elf_Internal_Sym
*isymend
;
10019 asection
**ppsection
;
10021 const struct elf_backend_data
*bed
;
10022 struct elf_link_hash_entry
**sym_hashes
;
10023 bfd_size_type address_size
;
10024 bfd_vma r_type_mask
;
10026 bfd_boolean have_file_sym
= FALSE
;
10028 output_bfd
= flinfo
->output_bfd
;
10029 bed
= get_elf_backend_data (output_bfd
);
10030 relocate_section
= bed
->elf_backend_relocate_section
;
10032 /* If this is a dynamic object, we don't want to do anything here:
10033 we don't want the local symbols, and we don't want the section
10035 if ((input_bfd
->flags
& DYNAMIC
) != 0)
10038 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
10039 if (elf_bad_symtab (input_bfd
))
10041 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10046 locsymcount
= symtab_hdr
->sh_info
;
10047 extsymoff
= symtab_hdr
->sh_info
;
10050 /* Read the local symbols. */
10051 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10052 if (isymbuf
== NULL
&& locsymcount
!= 0)
10054 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
10055 flinfo
->internal_syms
,
10056 flinfo
->external_syms
,
10057 flinfo
->locsym_shndx
);
10058 if (isymbuf
== NULL
)
10062 /* Find local symbol sections and adjust values of symbols in
10063 SEC_MERGE sections. Write out those local symbols we know are
10064 going into the output file. */
10065 isymend
= isymbuf
+ locsymcount
;
10066 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
10068 isym
++, pindex
++, ppsection
++)
10072 Elf_Internal_Sym osym
;
10078 if (elf_bad_symtab (input_bfd
))
10080 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
10087 if (isym
->st_shndx
== SHN_UNDEF
)
10088 isec
= bfd_und_section_ptr
;
10089 else if (isym
->st_shndx
== SHN_ABS
)
10090 isec
= bfd_abs_section_ptr
;
10091 else if (isym
->st_shndx
== SHN_COMMON
)
10092 isec
= bfd_com_section_ptr
;
10095 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
10098 /* Don't attempt to output symbols with st_shnx in the
10099 reserved range other than SHN_ABS and SHN_COMMON. */
10103 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
10104 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
10106 _bfd_merged_section_offset (output_bfd
, &isec
,
10107 elf_section_data (isec
)->sec_info
,
10113 /* Don't output the first, undefined, symbol. In fact, don't
10114 output any undefined local symbol. */
10115 if (isec
== bfd_und_section_ptr
)
10118 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
10120 /* We never output section symbols. Instead, we use the
10121 section symbol of the corresponding section in the output
10126 /* If we are stripping all symbols, we don't want to output this
10128 if (flinfo
->info
->strip
== strip_all
)
10131 /* If we are discarding all local symbols, we don't want to
10132 output this one. If we are generating a relocatable output
10133 file, then some of the local symbols may be required by
10134 relocs; we output them below as we discover that they are
10136 if (flinfo
->info
->discard
== discard_all
)
10139 /* If this symbol is defined in a section which we are
10140 discarding, we don't need to keep it. */
10141 if (isym
->st_shndx
!= SHN_UNDEF
10142 && isym
->st_shndx
< SHN_LORESERVE
10143 && bfd_section_removed_from_list (output_bfd
,
10144 isec
->output_section
))
10147 /* Get the name of the symbol. */
10148 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
10153 /* See if we are discarding symbols with this name. */
10154 if ((flinfo
->info
->strip
== strip_some
10155 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
10157 || (((flinfo
->info
->discard
== discard_sec_merge
10158 && (isec
->flags
& SEC_MERGE
)
10159 && !bfd_link_relocatable (flinfo
->info
))
10160 || flinfo
->info
->discard
== discard_l
)
10161 && bfd_is_local_label_name (input_bfd
, name
)))
10164 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
10166 if (input_bfd
->lto_output
)
10167 /* -flto puts a temp file name here. This means builds
10168 are not reproducible. Discard the symbol. */
10170 have_file_sym
= TRUE
;
10171 flinfo
->filesym_count
+= 1;
10173 if (!have_file_sym
)
10175 /* In the absence of debug info, bfd_find_nearest_line uses
10176 FILE symbols to determine the source file for local
10177 function symbols. Provide a FILE symbol here if input
10178 files lack such, so that their symbols won't be
10179 associated with a previous input file. It's not the
10180 source file, but the best we can do. */
10181 have_file_sym
= TRUE
;
10182 flinfo
->filesym_count
+= 1;
10183 memset (&osym
, 0, sizeof (osym
));
10184 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10185 osym
.st_shndx
= SHN_ABS
;
10186 if (!elf_link_output_symstrtab (flinfo
,
10187 (input_bfd
->lto_output
? NULL
10188 : input_bfd
->filename
),
10189 &osym
, bfd_abs_section_ptr
,
10196 /* Adjust the section index for the output file. */
10197 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10198 isec
->output_section
);
10199 if (osym
.st_shndx
== SHN_BAD
)
10202 /* ELF symbols in relocatable files are section relative, but
10203 in executable files they are virtual addresses. Note that
10204 this code assumes that all ELF sections have an associated
10205 BFD section with a reasonable value for output_offset; below
10206 we assume that they also have a reasonable value for
10207 output_section. Any special sections must be set up to meet
10208 these requirements. */
10209 osym
.st_value
+= isec
->output_offset
;
10210 if (!bfd_link_relocatable (flinfo
->info
))
10212 osym
.st_value
+= isec
->output_section
->vma
;
10213 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
10215 /* STT_TLS symbols are relative to PT_TLS segment base. */
10216 BFD_ASSERT (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
);
10217 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
10221 indx
= bfd_get_symcount (output_bfd
);
10222 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
10229 if (bed
->s
->arch_size
== 32)
10231 r_type_mask
= 0xff;
10237 r_type_mask
= 0xffffffff;
10242 /* Relocate the contents of each section. */
10243 sym_hashes
= elf_sym_hashes (input_bfd
);
10244 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
10246 bfd_byte
*contents
;
10248 if (! o
->linker_mark
)
10250 /* This section was omitted from the link. */
10254 if (bfd_link_relocatable (flinfo
->info
)
10255 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
10257 /* Deal with the group signature symbol. */
10258 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
10259 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
10260 asection
*osec
= o
->output_section
;
10262 if (symndx
>= locsymcount
10263 || (elf_bad_symtab (input_bfd
)
10264 && flinfo
->sections
[symndx
] == NULL
))
10266 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
10267 while (h
->root
.type
== bfd_link_hash_indirect
10268 || h
->root
.type
== bfd_link_hash_warning
)
10269 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10270 /* Arrange for symbol to be output. */
10272 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
10274 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
10276 /* We'll use the output section target_index. */
10277 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10278 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
10282 if (flinfo
->indices
[symndx
] == -1)
10284 /* Otherwise output the local symbol now. */
10285 Elf_Internal_Sym sym
= isymbuf
[symndx
];
10286 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10291 name
= bfd_elf_string_from_elf_section (input_bfd
,
10292 symtab_hdr
->sh_link
,
10297 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10299 if (sym
.st_shndx
== SHN_BAD
)
10302 sym
.st_value
+= o
->output_offset
;
10304 indx
= bfd_get_symcount (output_bfd
);
10305 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
10310 flinfo
->indices
[symndx
] = indx
;
10314 elf_section_data (osec
)->this_hdr
.sh_info
10315 = flinfo
->indices
[symndx
];
10319 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
10320 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
10323 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
10325 /* Section was created by _bfd_elf_link_create_dynamic_sections
10330 /* Get the contents of the section. They have been cached by a
10331 relaxation routine. Note that o is a section in an input
10332 file, so the contents field will not have been set by any of
10333 the routines which work on output files. */
10334 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
10336 contents
= elf_section_data (o
)->this_hdr
.contents
;
10337 if (bed
->caches_rawsize
10339 && o
->rawsize
< o
->size
)
10341 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
10342 contents
= flinfo
->contents
;
10347 contents
= flinfo
->contents
;
10348 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
10352 if ((o
->flags
& SEC_RELOC
) != 0)
10354 Elf_Internal_Rela
*internal_relocs
;
10355 Elf_Internal_Rela
*rel
, *relend
;
10356 int action_discarded
;
10359 /* Get the swapped relocs. */
10361 = _bfd_elf_link_read_relocs (input_bfd
, o
, flinfo
->external_relocs
,
10362 flinfo
->internal_relocs
, FALSE
);
10363 if (internal_relocs
== NULL
10364 && o
->reloc_count
> 0)
10367 /* We need to reverse-copy input .ctors/.dtors sections if
10368 they are placed in .init_array/.finit_array for output. */
10369 if (o
->size
> address_size
10370 && ((strncmp (o
->name
, ".ctors", 6) == 0
10371 && strcmp (o
->output_section
->name
,
10372 ".init_array") == 0)
10373 || (strncmp (o
->name
, ".dtors", 6) == 0
10374 && strcmp (o
->output_section
->name
,
10375 ".fini_array") == 0))
10376 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
10378 if (o
->size
!= o
->reloc_count
* address_size
)
10381 /* xgettext:c-format */
10382 (_("error: %B: size of section %A is not "
10383 "multiple of address size"),
10385 bfd_set_error (bfd_error_on_input
);
10388 o
->flags
|= SEC_ELF_REVERSE_COPY
;
10391 action_discarded
= -1;
10392 if (!elf_section_ignore_discarded_relocs (o
))
10393 action_discarded
= (*bed
->action_discarded
) (o
);
10395 /* Run through the relocs evaluating complex reloc symbols and
10396 looking for relocs against symbols from discarded sections
10397 or section symbols from removed link-once sections.
10398 Complain about relocs against discarded sections. Zero
10399 relocs against removed link-once sections. */
10401 rel
= internal_relocs
;
10402 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10403 for ( ; rel
< relend
; rel
++)
10405 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
10406 unsigned int s_type
;
10407 asection
**ps
, *sec
;
10408 struct elf_link_hash_entry
*h
= NULL
;
10409 const char *sym_name
;
10411 if (r_symndx
== STN_UNDEF
)
10414 if (r_symndx
>= locsymcount
10415 || (elf_bad_symtab (input_bfd
)
10416 && flinfo
->sections
[r_symndx
] == NULL
))
10418 h
= sym_hashes
[r_symndx
- extsymoff
];
10420 /* Badly formatted input files can contain relocs that
10421 reference non-existant symbols. Check here so that
10422 we do not seg fault. */
10427 sprintf_vma (buffer
, rel
->r_info
);
10429 /* xgettext:c-format */
10430 (_("error: %B contains a reloc (0x%s) for section %A "
10431 "that references a non-existent global symbol"),
10432 input_bfd
, buffer
, o
);
10433 bfd_set_error (bfd_error_bad_value
);
10437 while (h
->root
.type
== bfd_link_hash_indirect
10438 || h
->root
.type
== bfd_link_hash_warning
)
10439 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10443 /* If a plugin symbol is referenced from a non-IR file,
10444 mark the symbol as undefined. Note that the
10445 linker may attach linker created dynamic sections
10446 to the plugin bfd. Symbols defined in linker
10447 created sections are not plugin symbols. */
10448 if (h
->root
.non_ir_ref
10449 && (h
->root
.type
== bfd_link_hash_defined
10450 || h
->root
.type
== bfd_link_hash_defweak
)
10451 && (h
->root
.u
.def
.section
->flags
10452 & SEC_LINKER_CREATED
) == 0
10453 && h
->root
.u
.def
.section
->owner
!= NULL
10454 && (h
->root
.u
.def
.section
->owner
->flags
10455 & BFD_PLUGIN
) != 0)
10457 h
->root
.type
= bfd_link_hash_undefined
;
10458 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
10462 if (h
->root
.type
== bfd_link_hash_defined
10463 || h
->root
.type
== bfd_link_hash_defweak
)
10464 ps
= &h
->root
.u
.def
.section
;
10466 sym_name
= h
->root
.root
.string
;
10470 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
10472 s_type
= ELF_ST_TYPE (sym
->st_info
);
10473 ps
= &flinfo
->sections
[r_symndx
];
10474 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
10478 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
10479 && !bfd_link_relocatable (flinfo
->info
))
10482 bfd_vma dot
= (rel
->r_offset
10483 + o
->output_offset
+ o
->output_section
->vma
);
10485 printf ("Encountered a complex symbol!");
10486 printf (" (input_bfd %s, section %s, reloc %ld\n",
10487 input_bfd
->filename
, o
->name
,
10488 (long) (rel
- internal_relocs
));
10489 printf (" symbol: idx %8.8lx, name %s\n",
10490 r_symndx
, sym_name
);
10491 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10492 (unsigned long) rel
->r_info
,
10493 (unsigned long) rel
->r_offset
);
10495 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
10496 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
10499 /* Symbol evaluated OK. Update to absolute value. */
10500 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
10505 if (action_discarded
!= -1 && ps
!= NULL
)
10507 /* Complain if the definition comes from a
10508 discarded section. */
10509 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
10511 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
10512 if (action_discarded
& COMPLAIN
)
10513 (*flinfo
->info
->callbacks
->einfo
)
10514 /* xgettext:c-format */
10515 (_("%X`%s' referenced in section `%A' of %B: "
10516 "defined in discarded section `%A' of %B\n"),
10517 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
10519 /* Try to do the best we can to support buggy old
10520 versions of gcc. Pretend that the symbol is
10521 really defined in the kept linkonce section.
10522 FIXME: This is quite broken. Modifying the
10523 symbol here means we will be changing all later
10524 uses of the symbol, not just in this section. */
10525 if (action_discarded
& PRETEND
)
10529 kept
= _bfd_elf_check_kept_section (sec
,
10541 /* Relocate the section by invoking a back end routine.
10543 The back end routine is responsible for adjusting the
10544 section contents as necessary, and (if using Rela relocs
10545 and generating a relocatable output file) adjusting the
10546 reloc addend as necessary.
10548 The back end routine does not have to worry about setting
10549 the reloc address or the reloc symbol index.
10551 The back end routine is given a pointer to the swapped in
10552 internal symbols, and can access the hash table entries
10553 for the external symbols via elf_sym_hashes (input_bfd).
10555 When generating relocatable output, the back end routine
10556 must handle STB_LOCAL/STT_SECTION symbols specially. The
10557 output symbol is going to be a section symbol
10558 corresponding to the output section, which will require
10559 the addend to be adjusted. */
10561 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
10562 input_bfd
, o
, contents
,
10570 || bfd_link_relocatable (flinfo
->info
)
10571 || flinfo
->info
->emitrelocations
)
10573 Elf_Internal_Rela
*irela
;
10574 Elf_Internal_Rela
*irelaend
, *irelamid
;
10575 bfd_vma last_offset
;
10576 struct elf_link_hash_entry
**rel_hash
;
10577 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
10578 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
10579 unsigned int next_erel
;
10580 bfd_boolean rela_normal
;
10581 struct bfd_elf_section_data
*esdi
, *esdo
;
10583 esdi
= elf_section_data (o
);
10584 esdo
= elf_section_data (o
->output_section
);
10585 rela_normal
= FALSE
;
10587 /* Adjust the reloc addresses and symbol indices. */
10589 irela
= internal_relocs
;
10590 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10591 rel_hash
= esdo
->rel
.hashes
+ esdo
->rel
.count
;
10592 /* We start processing the REL relocs, if any. When we reach
10593 IRELAMID in the loop, we switch to the RELA relocs. */
10595 if (esdi
->rel
.hdr
!= NULL
)
10596 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
10597 * bed
->s
->int_rels_per_ext_rel
);
10598 rel_hash_list
= rel_hash
;
10599 rela_hash_list
= NULL
;
10600 last_offset
= o
->output_offset
;
10601 if (!bfd_link_relocatable (flinfo
->info
))
10602 last_offset
+= o
->output_section
->vma
;
10603 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
10605 unsigned long r_symndx
;
10607 Elf_Internal_Sym sym
;
10609 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
10615 if (irela
== irelamid
)
10617 rel_hash
= esdo
->rela
.hashes
+ esdo
->rela
.count
;
10618 rela_hash_list
= rel_hash
;
10619 rela_normal
= bed
->rela_normal
;
10622 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
10625 if (irela
->r_offset
>= (bfd_vma
) -2)
10627 /* This is a reloc for a deleted entry or somesuch.
10628 Turn it into an R_*_NONE reloc, at the same
10629 offset as the last reloc. elf_eh_frame.c and
10630 bfd_elf_discard_info rely on reloc offsets
10632 irela
->r_offset
= last_offset
;
10634 irela
->r_addend
= 0;
10638 irela
->r_offset
+= o
->output_offset
;
10640 /* Relocs in an executable have to be virtual addresses. */
10641 if (!bfd_link_relocatable (flinfo
->info
))
10642 irela
->r_offset
+= o
->output_section
->vma
;
10644 last_offset
= irela
->r_offset
;
10646 r_symndx
= irela
->r_info
>> r_sym_shift
;
10647 if (r_symndx
== STN_UNDEF
)
10650 if (r_symndx
>= locsymcount
10651 || (elf_bad_symtab (input_bfd
)
10652 && flinfo
->sections
[r_symndx
] == NULL
))
10654 struct elf_link_hash_entry
*rh
;
10655 unsigned long indx
;
10657 /* This is a reloc against a global symbol. We
10658 have not yet output all the local symbols, so
10659 we do not know the symbol index of any global
10660 symbol. We set the rel_hash entry for this
10661 reloc to point to the global hash table entry
10662 for this symbol. The symbol index is then
10663 set at the end of bfd_elf_final_link. */
10664 indx
= r_symndx
- extsymoff
;
10665 rh
= elf_sym_hashes (input_bfd
)[indx
];
10666 while (rh
->root
.type
== bfd_link_hash_indirect
10667 || rh
->root
.type
== bfd_link_hash_warning
)
10668 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
10670 /* Setting the index to -2 tells
10671 elf_link_output_extsym that this symbol is
10672 used by a reloc. */
10673 BFD_ASSERT (rh
->indx
< 0);
10681 /* This is a reloc against a local symbol. */
10684 sym
= isymbuf
[r_symndx
];
10685 sec
= flinfo
->sections
[r_symndx
];
10686 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
10688 /* I suppose the backend ought to fill in the
10689 section of any STT_SECTION symbol against a
10690 processor specific section. */
10691 r_symndx
= STN_UNDEF
;
10692 if (bfd_is_abs_section (sec
))
10694 else if (sec
== NULL
|| sec
->owner
== NULL
)
10696 bfd_set_error (bfd_error_bad_value
);
10701 asection
*osec
= sec
->output_section
;
10703 /* If we have discarded a section, the output
10704 section will be the absolute section. In
10705 case of discarded SEC_MERGE sections, use
10706 the kept section. relocate_section should
10707 have already handled discarded linkonce
10709 if (bfd_is_abs_section (osec
)
10710 && sec
->kept_section
!= NULL
10711 && sec
->kept_section
->output_section
!= NULL
)
10713 osec
= sec
->kept_section
->output_section
;
10714 irela
->r_addend
-= osec
->vma
;
10717 if (!bfd_is_abs_section (osec
))
10719 r_symndx
= osec
->target_index
;
10720 if (r_symndx
== STN_UNDEF
)
10722 irela
->r_addend
+= osec
->vma
;
10723 osec
= _bfd_nearby_section (output_bfd
, osec
,
10725 irela
->r_addend
-= osec
->vma
;
10726 r_symndx
= osec
->target_index
;
10731 /* Adjust the addend according to where the
10732 section winds up in the output section. */
10734 irela
->r_addend
+= sec
->output_offset
;
10738 if (flinfo
->indices
[r_symndx
] == -1)
10740 unsigned long shlink
;
10745 if (flinfo
->info
->strip
== strip_all
)
10747 /* You can't do ld -r -s. */
10748 bfd_set_error (bfd_error_invalid_operation
);
10752 /* This symbol was skipped earlier, but
10753 since it is needed by a reloc, we
10754 must output it now. */
10755 shlink
= symtab_hdr
->sh_link
;
10756 name
= (bfd_elf_string_from_elf_section
10757 (input_bfd
, shlink
, sym
.st_name
));
10761 osec
= sec
->output_section
;
10763 _bfd_elf_section_from_bfd_section (output_bfd
,
10765 if (sym
.st_shndx
== SHN_BAD
)
10768 sym
.st_value
+= sec
->output_offset
;
10769 if (!bfd_link_relocatable (flinfo
->info
))
10771 sym
.st_value
+= osec
->vma
;
10772 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
10774 /* STT_TLS symbols are relative to PT_TLS
10776 BFD_ASSERT (elf_hash_table (flinfo
->info
)
10777 ->tls_sec
!= NULL
);
10778 sym
.st_value
-= (elf_hash_table (flinfo
->info
)
10783 indx
= bfd_get_symcount (output_bfd
);
10784 ret
= elf_link_output_symstrtab (flinfo
, name
,
10790 flinfo
->indices
[r_symndx
] = indx
;
10795 r_symndx
= flinfo
->indices
[r_symndx
];
10798 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
10799 | (irela
->r_info
& r_type_mask
));
10802 /* Swap out the relocs. */
10803 input_rel_hdr
= esdi
->rel
.hdr
;
10804 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
10806 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10811 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
10812 * bed
->s
->int_rels_per_ext_rel
);
10813 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
10816 input_rela_hdr
= esdi
->rela
.hdr
;
10817 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
10819 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10828 /* Write out the modified section contents. */
10829 if (bed
->elf_backend_write_section
10830 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
10833 /* Section written out. */
10835 else switch (o
->sec_info_type
)
10837 case SEC_INFO_TYPE_STABS
:
10838 if (! (_bfd_write_section_stabs
10840 &elf_hash_table (flinfo
->info
)->stab_info
,
10841 o
, &elf_section_data (o
)->sec_info
, contents
)))
10844 case SEC_INFO_TYPE_MERGE
:
10845 if (! _bfd_write_merged_section (output_bfd
, o
,
10846 elf_section_data (o
)->sec_info
))
10849 case SEC_INFO_TYPE_EH_FRAME
:
10851 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
10856 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10858 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
10866 if (! (o
->flags
& SEC_EXCLUDE
))
10868 file_ptr offset
= (file_ptr
) o
->output_offset
;
10869 bfd_size_type todo
= o
->size
;
10871 offset
*= bfd_octets_per_byte (output_bfd
);
10873 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
10875 /* Reverse-copy input section to output. */
10878 todo
-= address_size
;
10879 if (! bfd_set_section_contents (output_bfd
,
10887 offset
+= address_size
;
10891 else if (! bfd_set_section_contents (output_bfd
,
10905 /* Generate a reloc when linking an ELF file. This is a reloc
10906 requested by the linker, and does not come from any input file. This
10907 is used to build constructor and destructor tables when linking
10911 elf_reloc_link_order (bfd
*output_bfd
,
10912 struct bfd_link_info
*info
,
10913 asection
*output_section
,
10914 struct bfd_link_order
*link_order
)
10916 reloc_howto_type
*howto
;
10920 struct bfd_elf_section_reloc_data
*reldata
;
10921 struct elf_link_hash_entry
**rel_hash_ptr
;
10922 Elf_Internal_Shdr
*rel_hdr
;
10923 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
10924 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
10927 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
10929 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
10932 bfd_set_error (bfd_error_bad_value
);
10936 addend
= link_order
->u
.reloc
.p
->addend
;
10939 reldata
= &esdo
->rel
;
10940 else if (esdo
->rela
.hdr
)
10941 reldata
= &esdo
->rela
;
10948 /* Figure out the symbol index. */
10949 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
10950 if (link_order
->type
== bfd_section_reloc_link_order
)
10952 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
10953 BFD_ASSERT (indx
!= 0);
10954 *rel_hash_ptr
= NULL
;
10958 struct elf_link_hash_entry
*h
;
10960 /* Treat a reloc against a defined symbol as though it were
10961 actually against the section. */
10962 h
= ((struct elf_link_hash_entry
*)
10963 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
10964 link_order
->u
.reloc
.p
->u
.name
,
10965 FALSE
, FALSE
, TRUE
));
10967 && (h
->root
.type
== bfd_link_hash_defined
10968 || h
->root
.type
== bfd_link_hash_defweak
))
10972 section
= h
->root
.u
.def
.section
;
10973 indx
= section
->output_section
->target_index
;
10974 *rel_hash_ptr
= NULL
;
10975 /* It seems that we ought to add the symbol value to the
10976 addend here, but in practice it has already been added
10977 because it was passed to constructor_callback. */
10978 addend
+= section
->output_section
->vma
+ section
->output_offset
;
10980 else if (h
!= NULL
)
10982 /* Setting the index to -2 tells elf_link_output_extsym that
10983 this symbol is used by a reloc. */
10990 (*info
->callbacks
->unattached_reloc
)
10991 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
10996 /* If this is an inplace reloc, we must write the addend into the
10998 if (howto
->partial_inplace
&& addend
!= 0)
11000 bfd_size_type size
;
11001 bfd_reloc_status_type rstat
;
11004 const char *sym_name
;
11006 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
11007 buf
= (bfd_byte
*) bfd_zmalloc (size
);
11008 if (buf
== NULL
&& size
!= 0)
11010 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
11017 case bfd_reloc_outofrange
:
11020 case bfd_reloc_overflow
:
11021 if (link_order
->type
== bfd_section_reloc_link_order
)
11022 sym_name
= bfd_section_name (output_bfd
,
11023 link_order
->u
.reloc
.p
->u
.section
);
11025 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
11026 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
11027 howto
->name
, addend
, NULL
, NULL
,
11032 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
11034 * bfd_octets_per_byte (output_bfd
),
11041 /* The address of a reloc is relative to the section in a
11042 relocatable file, and is a virtual address in an executable
11044 offset
= link_order
->offset
;
11045 if (! bfd_link_relocatable (info
))
11046 offset
+= output_section
->vma
;
11048 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
11050 irel
[i
].r_offset
= offset
;
11051 irel
[i
].r_info
= 0;
11052 irel
[i
].r_addend
= 0;
11054 if (bed
->s
->arch_size
== 32)
11055 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
11057 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
11059 rel_hdr
= reldata
->hdr
;
11060 erel
= rel_hdr
->contents
;
11061 if (rel_hdr
->sh_type
== SHT_REL
)
11063 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
11064 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
11068 irel
[0].r_addend
= addend
;
11069 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
11070 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
11079 /* Get the output vma of the section pointed to by the sh_link field. */
11082 elf_get_linked_section_vma (struct bfd_link_order
*p
)
11084 Elf_Internal_Shdr
**elf_shdrp
;
11088 s
= p
->u
.indirect
.section
;
11089 elf_shdrp
= elf_elfsections (s
->owner
);
11090 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
11091 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
11093 The Intel C compiler generates SHT_IA_64_UNWIND with
11094 SHF_LINK_ORDER. But it doesn't set the sh_link or
11095 sh_info fields. Hence we could get the situation
11096 where elfsec is 0. */
11099 const struct elf_backend_data
*bed
11100 = get_elf_backend_data (s
->owner
);
11101 if (bed
->link_order_error_handler
)
11102 bed
->link_order_error_handler
11103 /* xgettext:c-format */
11104 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
11109 s
= elf_shdrp
[elfsec
]->bfd_section
;
11110 return s
->output_section
->vma
+ s
->output_offset
;
11115 /* Compare two sections based on the locations of the sections they are
11116 linked to. Used by elf_fixup_link_order. */
11119 compare_link_order (const void * a
, const void * b
)
11124 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
11125 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
11128 return apos
> bpos
;
11132 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11133 order as their linked sections. Returns false if this could not be done
11134 because an output section includes both ordered and unordered
11135 sections. Ideally we'd do this in the linker proper. */
11138 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
11140 int seen_linkorder
;
11143 struct bfd_link_order
*p
;
11145 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11147 struct bfd_link_order
**sections
;
11148 asection
*s
, *other_sec
, *linkorder_sec
;
11152 linkorder_sec
= NULL
;
11154 seen_linkorder
= 0;
11155 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11157 if (p
->type
== bfd_indirect_link_order
)
11159 s
= p
->u
.indirect
.section
;
11161 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
11162 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
11163 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
11164 && elfsec
< elf_numsections (sub
)
11165 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
11166 && elf_elfsections (sub
)[elfsec
]->sh_link
< elf_numsections (sub
))
11180 if (seen_other
&& seen_linkorder
)
11182 if (other_sec
&& linkorder_sec
)
11184 /* xgettext:c-format */
11185 (_("%A has both ordered [`%A' in %B] "
11186 "and unordered [`%A' in %B] sections"),
11187 o
, linkorder_sec
, linkorder_sec
->owner
,
11188 other_sec
, other_sec
->owner
);
11191 (_("%A has both ordered and unordered sections"), o
);
11192 bfd_set_error (bfd_error_bad_value
);
11197 if (!seen_linkorder
)
11200 sections
= (struct bfd_link_order
**)
11201 bfd_malloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
11202 if (sections
== NULL
)
11204 seen_linkorder
= 0;
11206 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11208 sections
[seen_linkorder
++] = p
;
11210 /* Sort the input sections in the order of their linked section. */
11211 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
11212 compare_link_order
);
11214 /* Change the offsets of the sections. */
11216 for (n
= 0; n
< seen_linkorder
; n
++)
11218 s
= sections
[n
]->u
.indirect
.section
;
11219 offset
&= ~(bfd_vma
) 0 << s
->alignment_power
;
11220 s
->output_offset
= offset
/ bfd_octets_per_byte (abfd
);
11221 sections
[n
]->offset
= offset
;
11222 offset
+= sections
[n
]->size
;
11229 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11230 Returns TRUE upon success, FALSE otherwise. */
11233 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
11235 bfd_boolean ret
= FALSE
;
11237 const struct elf_backend_data
*bed
;
11239 enum bfd_architecture arch
;
11241 asymbol
**sympp
= NULL
;
11245 elf_symbol_type
*osymbuf
;
11247 implib_bfd
= info
->out_implib_bfd
;
11248 bed
= get_elf_backend_data (abfd
);
11250 if (!bfd_set_format (implib_bfd
, bfd_object
))
11253 flags
= bfd_get_file_flags (abfd
);
11254 flags
&= ~HAS_RELOC
;
11255 if (!bfd_set_start_address (implib_bfd
, 0)
11256 || !bfd_set_file_flags (implib_bfd
, flags
))
11259 /* Copy architecture of output file to import library file. */
11260 arch
= bfd_get_arch (abfd
);
11261 mach
= bfd_get_mach (abfd
);
11262 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
11263 && (abfd
->target_defaulted
11264 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
11267 /* Get symbol table size. */
11268 symsize
= bfd_get_symtab_upper_bound (abfd
);
11272 /* Read in the symbol table. */
11273 sympp
= (asymbol
**) xmalloc (symsize
);
11274 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
11278 /* Allow the BFD backend to copy any private header data it
11279 understands from the output BFD to the import library BFD. */
11280 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
11283 /* Filter symbols to appear in the import library. */
11284 if (bed
->elf_backend_filter_implib_symbols
)
11285 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
11288 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
11291 bfd_set_error (bfd_error_no_symbols
);
11292 _bfd_error_handler (_("%B: no symbol found for import library"),
11298 /* Make symbols absolute. */
11299 osymbuf
= (elf_symbol_type
*) bfd_alloc2 (implib_bfd
, symcount
,
11300 sizeof (*osymbuf
));
11301 for (src_count
= 0; src_count
< symcount
; src_count
++)
11303 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
11304 sizeof (*osymbuf
));
11305 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
11306 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
11307 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
11308 osymbuf
[src_count
].internal_elf_sym
.st_value
=
11309 osymbuf
[src_count
].symbol
.value
;
11310 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
11313 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
11315 /* Allow the BFD backend to copy any private data it understands
11316 from the output BFD to the import library BFD. This is done last
11317 to permit the routine to look at the filtered symbol table. */
11318 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
11321 if (!bfd_close (implib_bfd
))
11332 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
11336 if (flinfo
->symstrtab
!= NULL
)
11337 _bfd_elf_strtab_free (flinfo
->symstrtab
);
11338 if (flinfo
->contents
!= NULL
)
11339 free (flinfo
->contents
);
11340 if (flinfo
->external_relocs
!= NULL
)
11341 free (flinfo
->external_relocs
);
11342 if (flinfo
->internal_relocs
!= NULL
)
11343 free (flinfo
->internal_relocs
);
11344 if (flinfo
->external_syms
!= NULL
)
11345 free (flinfo
->external_syms
);
11346 if (flinfo
->locsym_shndx
!= NULL
)
11347 free (flinfo
->locsym_shndx
);
11348 if (flinfo
->internal_syms
!= NULL
)
11349 free (flinfo
->internal_syms
);
11350 if (flinfo
->indices
!= NULL
)
11351 free (flinfo
->indices
);
11352 if (flinfo
->sections
!= NULL
)
11353 free (flinfo
->sections
);
11354 if (flinfo
->symshndxbuf
!= NULL
)
11355 free (flinfo
->symshndxbuf
);
11356 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
11358 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11359 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rel
.hashes
!= NULL
)
11360 free (esdo
->rel
.hashes
);
11361 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rela
.hashes
!= NULL
)
11362 free (esdo
->rela
.hashes
);
11366 /* Do the final step of an ELF link. */
11369 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
11371 bfd_boolean dynamic
;
11372 bfd_boolean emit_relocs
;
11374 struct elf_final_link_info flinfo
;
11376 struct bfd_link_order
*p
;
11378 bfd_size_type max_contents_size
;
11379 bfd_size_type max_external_reloc_size
;
11380 bfd_size_type max_internal_reloc_count
;
11381 bfd_size_type max_sym_count
;
11382 bfd_size_type max_sym_shndx_count
;
11383 Elf_Internal_Sym elfsym
;
11385 Elf_Internal_Shdr
*symtab_hdr
;
11386 Elf_Internal_Shdr
*symtab_shndx_hdr
;
11387 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11388 struct elf_outext_info eoinfo
;
11389 bfd_boolean merged
;
11390 size_t relativecount
= 0;
11391 asection
*reldyn
= 0;
11393 asection
*attr_section
= NULL
;
11394 bfd_vma attr_size
= 0;
11395 const char *std_attrs_section
;
11396 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
11398 if (!is_elf_hash_table (htab
))
11401 if (bfd_link_pic (info
))
11402 abfd
->flags
|= DYNAMIC
;
11404 dynamic
= htab
->dynamic_sections_created
;
11405 dynobj
= htab
->dynobj
;
11407 emit_relocs
= (bfd_link_relocatable (info
)
11408 || info
->emitrelocations
);
11410 flinfo
.info
= info
;
11411 flinfo
.output_bfd
= abfd
;
11412 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
11413 if (flinfo
.symstrtab
== NULL
)
11418 flinfo
.hash_sec
= NULL
;
11419 flinfo
.symver_sec
= NULL
;
11423 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
11424 /* Note that dynsym_sec can be NULL (on VMS). */
11425 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
11426 /* Note that it is OK if symver_sec is NULL. */
11429 flinfo
.contents
= NULL
;
11430 flinfo
.external_relocs
= NULL
;
11431 flinfo
.internal_relocs
= NULL
;
11432 flinfo
.external_syms
= NULL
;
11433 flinfo
.locsym_shndx
= NULL
;
11434 flinfo
.internal_syms
= NULL
;
11435 flinfo
.indices
= NULL
;
11436 flinfo
.sections
= NULL
;
11437 flinfo
.symshndxbuf
= NULL
;
11438 flinfo
.filesym_count
= 0;
11440 /* The object attributes have been merged. Remove the input
11441 sections from the link, and set the contents of the output
11443 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
11444 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11446 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
11447 || strcmp (o
->name
, ".gnu.attributes") == 0)
11449 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11451 asection
*input_section
;
11453 if (p
->type
!= bfd_indirect_link_order
)
11455 input_section
= p
->u
.indirect
.section
;
11456 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11457 elf_link_input_bfd ignores this section. */
11458 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
11461 attr_size
= bfd_elf_obj_attr_size (abfd
);
11464 bfd_set_section_size (abfd
, o
, attr_size
);
11466 /* Skip this section later on. */
11467 o
->map_head
.link_order
= NULL
;
11470 o
->flags
|= SEC_EXCLUDE
;
11474 /* Count up the number of relocations we will output for each output
11475 section, so that we know the sizes of the reloc sections. We
11476 also figure out some maximum sizes. */
11477 max_contents_size
= 0;
11478 max_external_reloc_size
= 0;
11479 max_internal_reloc_count
= 0;
11481 max_sym_shndx_count
= 0;
11483 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11485 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11486 o
->reloc_count
= 0;
11488 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11490 unsigned int reloc_count
= 0;
11491 unsigned int additional_reloc_count
= 0;
11492 struct bfd_elf_section_data
*esdi
= NULL
;
11494 if (p
->type
== bfd_section_reloc_link_order
11495 || p
->type
== bfd_symbol_reloc_link_order
)
11497 else if (p
->type
== bfd_indirect_link_order
)
11501 sec
= p
->u
.indirect
.section
;
11503 /* Mark all sections which are to be included in the
11504 link. This will normally be every section. We need
11505 to do this so that we can identify any sections which
11506 the linker has decided to not include. */
11507 sec
->linker_mark
= TRUE
;
11509 if (sec
->flags
& SEC_MERGE
)
11512 if (sec
->rawsize
> max_contents_size
)
11513 max_contents_size
= sec
->rawsize
;
11514 if (sec
->size
> max_contents_size
)
11515 max_contents_size
= sec
->size
;
11517 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
11518 && (sec
->owner
->flags
& DYNAMIC
) == 0)
11522 /* We are interested in just local symbols, not all
11524 if (elf_bad_symtab (sec
->owner
))
11525 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
11526 / bed
->s
->sizeof_sym
);
11528 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
11530 if (sym_count
> max_sym_count
)
11531 max_sym_count
= sym_count
;
11533 if (sym_count
> max_sym_shndx_count
11534 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
11535 max_sym_shndx_count
= sym_count
;
11537 if (esdo
->this_hdr
.sh_type
== SHT_REL
11538 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
11539 /* Some backends use reloc_count in relocation sections
11540 to count particular types of relocs. Of course,
11541 reloc sections themselves can't have relocations. */
11543 else if (emit_relocs
)
11545 reloc_count
= sec
->reloc_count
;
11546 if (bed
->elf_backend_count_additional_relocs
)
11549 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
11550 additional_reloc_count
+= c
;
11553 else if (bed
->elf_backend_count_relocs
)
11554 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
11556 esdi
= elf_section_data (sec
);
11558 if ((sec
->flags
& SEC_RELOC
) != 0)
11560 size_t ext_size
= 0;
11562 if (esdi
->rel
.hdr
!= NULL
)
11563 ext_size
= esdi
->rel
.hdr
->sh_size
;
11564 if (esdi
->rela
.hdr
!= NULL
)
11565 ext_size
+= esdi
->rela
.hdr
->sh_size
;
11567 if (ext_size
> max_external_reloc_size
)
11568 max_external_reloc_size
= ext_size
;
11569 if (sec
->reloc_count
> max_internal_reloc_count
)
11570 max_internal_reloc_count
= sec
->reloc_count
;
11575 if (reloc_count
== 0)
11578 reloc_count
+= additional_reloc_count
;
11579 o
->reloc_count
+= reloc_count
;
11581 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
11585 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
11586 esdo
->rel
.count
+= additional_reloc_count
;
11588 if (esdi
->rela
.hdr
)
11590 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
11591 esdo
->rela
.count
+= additional_reloc_count
;
11597 esdo
->rela
.count
+= reloc_count
;
11599 esdo
->rel
.count
+= reloc_count
;
11603 if (o
->reloc_count
> 0)
11604 o
->flags
|= SEC_RELOC
;
11607 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11608 set it (this is probably a bug) and if it is set
11609 assign_section_numbers will create a reloc section. */
11610 o
->flags
&=~ SEC_RELOC
;
11613 /* If the SEC_ALLOC flag is not set, force the section VMA to
11614 zero. This is done in elf_fake_sections as well, but forcing
11615 the VMA to 0 here will ensure that relocs against these
11616 sections are handled correctly. */
11617 if ((o
->flags
& SEC_ALLOC
) == 0
11618 && ! o
->user_set_vma
)
11622 if (! bfd_link_relocatable (info
) && merged
)
11623 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
11625 /* Figure out the file positions for everything but the symbol table
11626 and the relocs. We set symcount to force assign_section_numbers
11627 to create a symbol table. */
11628 bfd_get_symcount (abfd
) = info
->strip
!= strip_all
|| emit_relocs
;
11629 BFD_ASSERT (! abfd
->output_has_begun
);
11630 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
11633 /* Set sizes, and assign file positions for reloc sections. */
11634 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11636 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11637 if ((o
->flags
& SEC_RELOC
) != 0)
11640 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
11644 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
11648 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11649 to count upwards while actually outputting the relocations. */
11650 esdo
->rel
.count
= 0;
11651 esdo
->rela
.count
= 0;
11653 if (esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
11655 /* Cache the section contents so that they can be compressed
11656 later. Use bfd_malloc since it will be freed by
11657 bfd_compress_section_contents. */
11658 unsigned char *contents
= esdo
->this_hdr
.contents
;
11659 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
11662 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
11663 if (contents
== NULL
)
11665 esdo
->this_hdr
.contents
= contents
;
11669 /* We have now assigned file positions for all the sections except
11670 .symtab, .strtab, and non-loaded reloc sections. We start the
11671 .symtab section at the current file position, and write directly
11672 to it. We build the .strtab section in memory. */
11673 bfd_get_symcount (abfd
) = 0;
11674 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11675 /* sh_name is set in prep_headers. */
11676 symtab_hdr
->sh_type
= SHT_SYMTAB
;
11677 /* sh_flags, sh_addr and sh_size all start off zero. */
11678 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
11679 /* sh_link is set in assign_section_numbers. */
11680 /* sh_info is set below. */
11681 /* sh_offset is set just below. */
11682 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
11684 if (max_sym_count
< 20)
11685 max_sym_count
= 20;
11686 htab
->strtabsize
= max_sym_count
;
11687 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
11688 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
11689 if (htab
->strtab
== NULL
)
11691 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11693 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
11694 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
11696 if (info
->strip
!= strip_all
|| emit_relocs
)
11698 file_ptr off
= elf_next_file_pos (abfd
);
11700 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
11702 /* Note that at this point elf_next_file_pos (abfd) is
11703 incorrect. We do not yet know the size of the .symtab section.
11704 We correct next_file_pos below, after we do know the size. */
11706 /* Start writing out the symbol table. The first symbol is always a
11708 elfsym
.st_value
= 0;
11709 elfsym
.st_size
= 0;
11710 elfsym
.st_info
= 0;
11711 elfsym
.st_other
= 0;
11712 elfsym
.st_shndx
= SHN_UNDEF
;
11713 elfsym
.st_target_internal
= 0;
11714 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
11715 bfd_und_section_ptr
, NULL
) != 1)
11718 /* Output a symbol for each section. We output these even if we are
11719 discarding local symbols, since they are used for relocs. These
11720 symbols have no names. We store the index of each one in the
11721 index field of the section, so that we can find it again when
11722 outputting relocs. */
11724 elfsym
.st_size
= 0;
11725 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
11726 elfsym
.st_other
= 0;
11727 elfsym
.st_value
= 0;
11728 elfsym
.st_target_internal
= 0;
11729 for (i
= 1; i
< elf_numsections (abfd
); i
++)
11731 o
= bfd_section_from_elf_index (abfd
, i
);
11734 o
->target_index
= bfd_get_symcount (abfd
);
11735 elfsym
.st_shndx
= i
;
11736 if (!bfd_link_relocatable (info
))
11737 elfsym
.st_value
= o
->vma
;
11738 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
, o
,
11745 /* Allocate some memory to hold information read in from the input
11747 if (max_contents_size
!= 0)
11749 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
11750 if (flinfo
.contents
== NULL
)
11754 if (max_external_reloc_size
!= 0)
11756 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
11757 if (flinfo
.external_relocs
== NULL
)
11761 if (max_internal_reloc_count
!= 0)
11763 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
11764 amt
*= sizeof (Elf_Internal_Rela
);
11765 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
11766 if (flinfo
.internal_relocs
== NULL
)
11770 if (max_sym_count
!= 0)
11772 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
11773 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
11774 if (flinfo
.external_syms
== NULL
)
11777 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
11778 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
11779 if (flinfo
.internal_syms
== NULL
)
11782 amt
= max_sym_count
* sizeof (long);
11783 flinfo
.indices
= (long int *) bfd_malloc (amt
);
11784 if (flinfo
.indices
== NULL
)
11787 amt
= max_sym_count
* sizeof (asection
*);
11788 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
11789 if (flinfo
.sections
== NULL
)
11793 if (max_sym_shndx_count
!= 0)
11795 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
11796 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
11797 if (flinfo
.locsym_shndx
== NULL
)
11803 bfd_vma base
, end
= 0;
11806 for (sec
= htab
->tls_sec
;
11807 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
11810 bfd_size_type size
= sec
->size
;
11813 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
11815 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
11818 size
= ord
->offset
+ ord
->size
;
11820 end
= sec
->vma
+ size
;
11822 base
= htab
->tls_sec
->vma
;
11823 /* Only align end of TLS section if static TLS doesn't have special
11824 alignment requirements. */
11825 if (bed
->static_tls_alignment
== 1)
11826 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
11827 htab
->tls_size
= end
- base
;
11830 /* Reorder SHF_LINK_ORDER sections. */
11831 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11833 if (!elf_fixup_link_order (abfd
, o
))
11837 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
11840 /* Since ELF permits relocations to be against local symbols, we
11841 must have the local symbols available when we do the relocations.
11842 Since we would rather only read the local symbols once, and we
11843 would rather not keep them in memory, we handle all the
11844 relocations for a single input file at the same time.
11846 Unfortunately, there is no way to know the total number of local
11847 symbols until we have seen all of them, and the local symbol
11848 indices precede the global symbol indices. This means that when
11849 we are generating relocatable output, and we see a reloc against
11850 a global symbol, we can not know the symbol index until we have
11851 finished examining all the local symbols to see which ones we are
11852 going to output. To deal with this, we keep the relocations in
11853 memory, and don't output them until the end of the link. This is
11854 an unfortunate waste of memory, but I don't see a good way around
11855 it. Fortunately, it only happens when performing a relocatable
11856 link, which is not the common case. FIXME: If keep_memory is set
11857 we could write the relocs out and then read them again; I don't
11858 know how bad the memory loss will be. */
11860 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11861 sub
->output_has_begun
= FALSE
;
11862 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11864 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11866 if (p
->type
== bfd_indirect_link_order
11867 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
11868 == bfd_target_elf_flavour
)
11869 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
11871 if (! sub
->output_has_begun
)
11873 if (! elf_link_input_bfd (&flinfo
, sub
))
11875 sub
->output_has_begun
= TRUE
;
11878 else if (p
->type
== bfd_section_reloc_link_order
11879 || p
->type
== bfd_symbol_reloc_link_order
)
11881 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
11886 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
11888 if (p
->type
== bfd_indirect_link_order
11889 && (bfd_get_flavour (sub
)
11890 == bfd_target_elf_flavour
)
11891 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
11892 != bed
->s
->elfclass
))
11894 const char *iclass
, *oclass
;
11896 switch (bed
->s
->elfclass
)
11898 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
11899 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
11900 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
11904 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
11906 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
11907 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
11908 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
11912 bfd_set_error (bfd_error_wrong_format
);
11914 /* xgettext:c-format */
11915 (_("%B: file class %s incompatible with %s"),
11916 sub
, iclass
, oclass
);
11925 /* Free symbol buffer if needed. */
11926 if (!info
->reduce_memory_overheads
)
11928 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11929 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
11930 && elf_tdata (sub
)->symbuf
)
11932 free (elf_tdata (sub
)->symbuf
);
11933 elf_tdata (sub
)->symbuf
= NULL
;
11937 /* Output any global symbols that got converted to local in a
11938 version script or due to symbol visibility. We do this in a
11939 separate step since ELF requires all local symbols to appear
11940 prior to any global symbols. FIXME: We should only do this if
11941 some global symbols were, in fact, converted to become local.
11942 FIXME: Will this work correctly with the Irix 5 linker? */
11943 eoinfo
.failed
= FALSE
;
11944 eoinfo
.flinfo
= &flinfo
;
11945 eoinfo
.localsyms
= TRUE
;
11946 eoinfo
.file_sym_done
= FALSE
;
11947 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
11951 /* If backend needs to output some local symbols not present in the hash
11952 table, do it now. */
11953 if (bed
->elf_backend_output_arch_local_syms
11954 && (info
->strip
!= strip_all
|| emit_relocs
))
11956 typedef int (*out_sym_func
)
11957 (void *, const char *, Elf_Internal_Sym
*, asection
*,
11958 struct elf_link_hash_entry
*);
11960 if (! ((*bed
->elf_backend_output_arch_local_syms
)
11961 (abfd
, info
, &flinfo
,
11962 (out_sym_func
) elf_link_output_symstrtab
)))
11966 /* That wrote out all the local symbols. Finish up the symbol table
11967 with the global symbols. Even if we want to strip everything we
11968 can, we still need to deal with those global symbols that got
11969 converted to local in a version script. */
11971 /* The sh_info field records the index of the first non local symbol. */
11972 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
11975 && htab
->dynsym
!= NULL
11976 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
11978 Elf_Internal_Sym sym
;
11979 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
11981 o
= htab
->dynsym
->output_section
;
11982 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
11984 /* Write out the section symbols for the output sections. */
11985 if (bfd_link_pic (info
)
11986 || htab
->is_relocatable_executable
)
11992 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
11994 sym
.st_target_internal
= 0;
11996 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
12002 dynindx
= elf_section_data (s
)->dynindx
;
12005 indx
= elf_section_data (s
)->this_idx
;
12006 BFD_ASSERT (indx
> 0);
12007 sym
.st_shndx
= indx
;
12008 if (! check_dynsym (abfd
, &sym
))
12010 sym
.st_value
= s
->vma
;
12011 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
12012 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12016 /* Write out the local dynsyms. */
12017 if (htab
->dynlocal
)
12019 struct elf_link_local_dynamic_entry
*e
;
12020 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
12025 /* Copy the internal symbol and turn off visibility.
12026 Note that we saved a word of storage and overwrote
12027 the original st_name with the dynstr_index. */
12029 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
12031 s
= bfd_section_from_elf_index (e
->input_bfd
,
12036 elf_section_data (s
->output_section
)->this_idx
;
12037 if (! check_dynsym (abfd
, &sym
))
12039 sym
.st_value
= (s
->output_section
->vma
12041 + e
->isym
.st_value
);
12044 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
12045 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12050 /* We get the global symbols from the hash table. */
12051 eoinfo
.failed
= FALSE
;
12052 eoinfo
.localsyms
= FALSE
;
12053 eoinfo
.flinfo
= &flinfo
;
12054 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12058 /* If backend needs to output some symbols not present in the hash
12059 table, do it now. */
12060 if (bed
->elf_backend_output_arch_syms
12061 && (info
->strip
!= strip_all
|| emit_relocs
))
12063 typedef int (*out_sym_func
)
12064 (void *, const char *, Elf_Internal_Sym
*, asection
*,
12065 struct elf_link_hash_entry
*);
12067 if (! ((*bed
->elf_backend_output_arch_syms
)
12068 (abfd
, info
, &flinfo
,
12069 (out_sym_func
) elf_link_output_symstrtab
)))
12073 /* Finalize the .strtab section. */
12074 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
12076 /* Swap out the .strtab section. */
12077 if (!elf_link_swap_symbols_out (&flinfo
))
12080 /* Now we know the size of the symtab section. */
12081 if (bfd_get_symcount (abfd
) > 0)
12083 /* Finish up and write out the symbol string table (.strtab)
12085 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
12086 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
12088 if (elf_symtab_shndx_list (abfd
))
12090 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
12092 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
12094 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
12095 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
12096 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
12097 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
12098 symtab_shndx_hdr
->sh_size
= amt
;
12100 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
12103 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
12104 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
12109 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
12110 /* sh_name was set in prep_headers. */
12111 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
12112 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
12113 symstrtab_hdr
->sh_addr
= 0;
12114 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
12115 symstrtab_hdr
->sh_entsize
= 0;
12116 symstrtab_hdr
->sh_link
= 0;
12117 symstrtab_hdr
->sh_info
= 0;
12118 /* sh_offset is set just below. */
12119 symstrtab_hdr
->sh_addralign
= 1;
12121 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
12123 elf_next_file_pos (abfd
) = off
;
12125 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
12126 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
12130 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
12132 _bfd_error_handler (_("%B: failed to generate import library"),
12133 info
->out_implib_bfd
);
12137 /* Adjust the relocs to have the correct symbol indices. */
12138 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12140 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12142 if ((o
->flags
& SEC_RELOC
) == 0)
12145 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
12146 if (esdo
->rel
.hdr
!= NULL
12147 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
))
12149 if (esdo
->rela
.hdr
!= NULL
12150 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
))
12153 /* Set the reloc_count field to 0 to prevent write_relocs from
12154 trying to swap the relocs out itself. */
12155 o
->reloc_count
= 0;
12158 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
12159 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
12161 /* If we are linking against a dynamic object, or generating a
12162 shared library, finish up the dynamic linking information. */
12165 bfd_byte
*dyncon
, *dynconend
;
12167 /* Fix up .dynamic entries. */
12168 o
= bfd_get_linker_section (dynobj
, ".dynamic");
12169 BFD_ASSERT (o
!= NULL
);
12171 dyncon
= o
->contents
;
12172 dynconend
= o
->contents
+ o
->size
;
12173 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12175 Elf_Internal_Dyn dyn
;
12178 bfd_size_type sh_size
;
12181 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12188 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
12190 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
12192 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
12193 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
12196 dyn
.d_un
.d_val
= relativecount
;
12203 name
= info
->init_function
;
12206 name
= info
->fini_function
;
12209 struct elf_link_hash_entry
*h
;
12211 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
12213 && (h
->root
.type
== bfd_link_hash_defined
12214 || h
->root
.type
== bfd_link_hash_defweak
))
12216 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
12217 o
= h
->root
.u
.def
.section
;
12218 if (o
->output_section
!= NULL
)
12219 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
12220 + o
->output_offset
);
12223 /* The symbol is imported from another shared
12224 library and does not apply to this one. */
12225 dyn
.d_un
.d_ptr
= 0;
12232 case DT_PREINIT_ARRAYSZ
:
12233 name
= ".preinit_array";
12235 case DT_INIT_ARRAYSZ
:
12236 name
= ".init_array";
12238 case DT_FINI_ARRAYSZ
:
12239 name
= ".fini_array";
12241 o
= bfd_get_section_by_name (abfd
, name
);
12245 (_("could not find section %s"), name
);
12250 (_("warning: %s section has zero size"), name
);
12251 dyn
.d_un
.d_val
= o
->size
;
12254 case DT_PREINIT_ARRAY
:
12255 name
= ".preinit_array";
12257 case DT_INIT_ARRAY
:
12258 name
= ".init_array";
12260 case DT_FINI_ARRAY
:
12261 name
= ".fini_array";
12263 o
= bfd_get_section_by_name (abfd
, name
);
12270 name
= ".gnu.hash";
12279 name
= ".gnu.version_d";
12282 name
= ".gnu.version_r";
12285 name
= ".gnu.version";
12287 o
= bfd_get_linker_section (dynobj
, name
);
12292 (_("could not find section %s"), name
);
12295 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
12298 (_("warning: section '%s' is being made into a note"), name
);
12299 bfd_set_error (bfd_error_nonrepresentable_section
);
12302 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
12309 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
12315 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12317 Elf_Internal_Shdr
*hdr
;
12319 hdr
= elf_elfsections (abfd
)[i
];
12320 if (hdr
->sh_type
== type
12321 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
12323 sh_size
+= hdr
->sh_size
;
12325 || sh_addr
> hdr
->sh_addr
)
12326 sh_addr
= hdr
->sh_addr
;
12330 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
12332 /* Don't count procedure linkage table relocs in the
12333 overall reloc count. */
12334 sh_size
-= htab
->srelplt
->size
;
12336 /* If the size is zero, make the address zero too.
12337 This is to avoid a glibc bug. If the backend
12338 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12339 zero, then we'll put DT_RELA at the end of
12340 DT_JMPREL. glibc will interpret the end of
12341 DT_RELA matching the end of DT_JMPREL as the
12342 case where DT_RELA includes DT_JMPREL, and for
12343 LD_BIND_NOW will decide that processing DT_RELA
12344 will process the PLT relocs too. Net result:
12345 No PLT relocs applied. */
12348 /* If .rela.plt is the first .rela section, exclude
12349 it from DT_RELA. */
12350 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
12351 + htab
->srelplt
->output_offset
))
12352 sh_addr
+= htab
->srelplt
->size
;
12355 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
12356 dyn
.d_un
.d_val
= sh_size
;
12358 dyn
.d_un
.d_ptr
= sh_addr
;
12361 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
12365 /* If we have created any dynamic sections, then output them. */
12366 if (dynobj
!= NULL
)
12368 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
12371 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12372 if (((info
->warn_shared_textrel
&& bfd_link_pic (info
))
12373 || info
->error_textrel
)
12374 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
)
12376 bfd_byte
*dyncon
, *dynconend
;
12378 dyncon
= o
->contents
;
12379 dynconend
= o
->contents
+ o
->size
;
12380 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12382 Elf_Internal_Dyn dyn
;
12384 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12386 if (dyn
.d_tag
== DT_TEXTREL
)
12388 if (info
->error_textrel
)
12389 info
->callbacks
->einfo
12390 (_("%P%X: read-only segment has dynamic relocations.\n"));
12392 info
->callbacks
->einfo
12393 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12399 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
12401 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
12403 || o
->output_section
== bfd_abs_section_ptr
)
12405 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
12407 /* At this point, we are only interested in sections
12408 created by _bfd_elf_link_create_dynamic_sections. */
12411 if (htab
->stab_info
.stabstr
== o
)
12413 if (htab
->eh_info
.hdr_sec
== o
)
12415 if (strcmp (o
->name
, ".dynstr") != 0)
12417 if (! bfd_set_section_contents (abfd
, o
->output_section
,
12419 (file_ptr
) o
->output_offset
12420 * bfd_octets_per_byte (abfd
),
12426 /* The contents of the .dynstr section are actually in a
12430 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
12431 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
12432 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
12438 if (bfd_link_relocatable (info
))
12440 bfd_boolean failed
= FALSE
;
12442 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
12447 /* If we have optimized stabs strings, output them. */
12448 if (htab
->stab_info
.stabstr
!= NULL
)
12450 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
12454 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
12457 elf_final_link_free (abfd
, &flinfo
);
12459 elf_linker (abfd
) = TRUE
;
12463 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
12464 if (contents
== NULL
)
12465 return FALSE
; /* Bail out and fail. */
12466 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
12467 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
12474 elf_final_link_free (abfd
, &flinfo
);
12478 /* Initialize COOKIE for input bfd ABFD. */
12481 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
12482 struct bfd_link_info
*info
, bfd
*abfd
)
12484 Elf_Internal_Shdr
*symtab_hdr
;
12485 const struct elf_backend_data
*bed
;
12487 bed
= get_elf_backend_data (abfd
);
12488 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12490 cookie
->abfd
= abfd
;
12491 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
12492 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
12493 if (cookie
->bad_symtab
)
12495 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
12496 cookie
->extsymoff
= 0;
12500 cookie
->locsymcount
= symtab_hdr
->sh_info
;
12501 cookie
->extsymoff
= symtab_hdr
->sh_info
;
12504 if (bed
->s
->arch_size
== 32)
12505 cookie
->r_sym_shift
= 8;
12507 cookie
->r_sym_shift
= 32;
12509 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
12510 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
12512 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
12513 cookie
->locsymcount
, 0,
12515 if (cookie
->locsyms
== NULL
)
12517 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
12520 if (info
->keep_memory
)
12521 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
12526 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12529 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
12531 Elf_Internal_Shdr
*symtab_hdr
;
12533 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12534 if (cookie
->locsyms
!= NULL
12535 && symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
12536 free (cookie
->locsyms
);
12539 /* Initialize the relocation information in COOKIE for input section SEC
12540 of input bfd ABFD. */
12543 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12544 struct bfd_link_info
*info
, bfd
*abfd
,
12547 const struct elf_backend_data
*bed
;
12549 if (sec
->reloc_count
== 0)
12551 cookie
->rels
= NULL
;
12552 cookie
->relend
= NULL
;
12556 bed
= get_elf_backend_data (abfd
);
12558 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
12559 info
->keep_memory
);
12560 if (cookie
->rels
== NULL
)
12562 cookie
->rel
= cookie
->rels
;
12563 cookie
->relend
= (cookie
->rels
12564 + sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
);
12566 cookie
->rel
= cookie
->rels
;
12570 /* Free the memory allocated by init_reloc_cookie_rels,
12574 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12577 if (cookie
->rels
&& elf_section_data (sec
)->relocs
!= cookie
->rels
)
12578 free (cookie
->rels
);
12581 /* Initialize the whole of COOKIE for input section SEC. */
12584 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12585 struct bfd_link_info
*info
,
12588 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
12590 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
12595 fini_reloc_cookie (cookie
, sec
->owner
);
12600 /* Free the memory allocated by init_reloc_cookie_for_section,
12604 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12607 fini_reloc_cookie_rels (cookie
, sec
);
12608 fini_reloc_cookie (cookie
, sec
->owner
);
12611 /* Garbage collect unused sections. */
12613 /* Default gc_mark_hook. */
12616 _bfd_elf_gc_mark_hook (asection
*sec
,
12617 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12618 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
12619 struct elf_link_hash_entry
*h
,
12620 Elf_Internal_Sym
*sym
)
12624 switch (h
->root
.type
)
12626 case bfd_link_hash_defined
:
12627 case bfd_link_hash_defweak
:
12628 return h
->root
.u
.def
.section
;
12630 case bfd_link_hash_common
:
12631 return h
->root
.u
.c
.p
->section
;
12638 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
12643 /* For undefined __start_<name> and __stop_<name> symbols, return the
12644 first input section matching <name>. Return NULL otherwise. */
12647 _bfd_elf_is_start_stop (const struct bfd_link_info
*info
,
12648 struct elf_link_hash_entry
*h
)
12651 const char *sec_name
;
12653 if (h
->root
.type
!= bfd_link_hash_undefined
12654 && h
->root
.type
!= bfd_link_hash_undefweak
)
12657 s
= h
->root
.u
.undef
.section
;
12660 if (s
== (asection
*) 0 - 1)
12666 if (strncmp (h
->root
.root
.string
, "__start_", 8) == 0)
12667 sec_name
= h
->root
.root
.string
+ 8;
12668 else if (strncmp (h
->root
.root
.string
, "__stop_", 7) == 0)
12669 sec_name
= h
->root
.root
.string
+ 7;
12671 if (sec_name
!= NULL
&& *sec_name
!= '\0')
12675 for (i
= info
->input_bfds
; i
!= NULL
; i
= i
->link
.next
)
12677 s
= bfd_get_section_by_name (i
, sec_name
);
12680 h
->root
.u
.undef
.section
= s
;
12687 h
->root
.u
.undef
.section
= (asection
*) 0 - 1;
12692 /* COOKIE->rel describes a relocation against section SEC, which is
12693 a section we've decided to keep. Return the section that contains
12694 the relocation symbol, or NULL if no section contains it. */
12697 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
12698 elf_gc_mark_hook_fn gc_mark_hook
,
12699 struct elf_reloc_cookie
*cookie
,
12700 bfd_boolean
*start_stop
)
12702 unsigned long r_symndx
;
12703 struct elf_link_hash_entry
*h
;
12705 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
12706 if (r_symndx
== STN_UNDEF
)
12709 if (r_symndx
>= cookie
->locsymcount
12710 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
12712 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
12715 info
->callbacks
->einfo (_("%F%P: corrupt input: %B\n"),
12719 while (h
->root
.type
== bfd_link_hash_indirect
12720 || h
->root
.type
== bfd_link_hash_warning
)
12721 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
12723 /* If this symbol is weak and there is a non-weak definition, we
12724 keep the non-weak definition because many backends put
12725 dynamic reloc info on the non-weak definition for code
12726 handling copy relocs. */
12727 if (h
->u
.weakdef
!= NULL
)
12728 h
->u
.weakdef
->mark
= 1;
12730 if (start_stop
!= NULL
)
12732 /* To work around a glibc bug, mark all XXX input sections
12733 when there is an as yet undefined reference to __start_XXX
12734 or __stop_XXX symbols. The linker will later define such
12735 symbols for orphan input sections that have a name
12736 representable as a C identifier. */
12737 asection
*s
= _bfd_elf_is_start_stop (info
, h
);
12741 *start_stop
= !s
->gc_mark
;
12746 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
12749 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
12750 &cookie
->locsyms
[r_symndx
]);
12753 /* COOKIE->rel describes a relocation against section SEC, which is
12754 a section we've decided to keep. Mark the section that contains
12755 the relocation symbol. */
12758 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
12760 elf_gc_mark_hook_fn gc_mark_hook
,
12761 struct elf_reloc_cookie
*cookie
)
12764 bfd_boolean start_stop
= FALSE
;
12766 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
12767 while (rsec
!= NULL
)
12769 if (!rsec
->gc_mark
)
12771 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
12772 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
12774 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
12779 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
12784 /* The mark phase of garbage collection. For a given section, mark
12785 it and any sections in this section's group, and all the sections
12786 which define symbols to which it refers. */
12789 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
12791 elf_gc_mark_hook_fn gc_mark_hook
)
12794 asection
*group_sec
, *eh_frame
;
12798 /* Mark all the sections in the group. */
12799 group_sec
= elf_section_data (sec
)->next_in_group
;
12800 if (group_sec
&& !group_sec
->gc_mark
)
12801 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
12804 /* Look through the section relocs. */
12806 eh_frame
= elf_eh_frame_section (sec
->owner
);
12807 if ((sec
->flags
& SEC_RELOC
) != 0
12808 && sec
->reloc_count
> 0
12809 && sec
!= eh_frame
)
12811 struct elf_reloc_cookie cookie
;
12813 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
12817 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
12818 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
12823 fini_reloc_cookie_for_section (&cookie
, sec
);
12827 if (ret
&& eh_frame
&& elf_fde_list (sec
))
12829 struct elf_reloc_cookie cookie
;
12831 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
12835 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
12836 gc_mark_hook
, &cookie
))
12838 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
12842 eh_frame
= elf_section_eh_frame_entry (sec
);
12843 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
12844 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
12850 /* Scan and mark sections in a special or debug section group. */
12853 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
12855 /* Point to first section of section group. */
12857 /* Used to iterate the section group. */
12860 bfd_boolean is_special_grp
= TRUE
;
12861 bfd_boolean is_debug_grp
= TRUE
;
12863 /* First scan to see if group contains any section other than debug
12864 and special section. */
12865 ssec
= msec
= elf_next_in_group (grp
);
12868 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
12869 is_debug_grp
= FALSE
;
12871 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
12872 is_special_grp
= FALSE
;
12874 msec
= elf_next_in_group (msec
);
12876 while (msec
!= ssec
);
12878 /* If this is a pure debug section group or pure special section group,
12879 keep all sections in this group. */
12880 if (is_debug_grp
|| is_special_grp
)
12885 msec
= elf_next_in_group (msec
);
12887 while (msec
!= ssec
);
12891 /* Keep debug and special sections. */
12894 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
12895 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED
)
12899 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
12902 bfd_boolean some_kept
;
12903 bfd_boolean debug_frag_seen
;
12905 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
12908 /* Ensure all linker created sections are kept,
12909 see if any other section is already marked,
12910 and note if we have any fragmented debug sections. */
12911 debug_frag_seen
= some_kept
= FALSE
;
12912 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12914 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
12916 else if (isec
->gc_mark
)
12919 if (debug_frag_seen
== FALSE
12920 && (isec
->flags
& SEC_DEBUGGING
)
12921 && CONST_STRNEQ (isec
->name
, ".debug_line."))
12922 debug_frag_seen
= TRUE
;
12925 /* If no section in this file will be kept, then we can
12926 toss out the debug and special sections. */
12930 /* Keep debug and special sections like .comment when they are
12931 not part of a group. Also keep section groups that contain
12932 just debug sections or special sections. */
12933 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12935 if ((isec
->flags
& SEC_GROUP
) != 0)
12936 _bfd_elf_gc_mark_debug_special_section_group (isec
);
12937 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
12938 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
12939 && elf_next_in_group (isec
) == NULL
)
12943 if (! debug_frag_seen
)
12946 /* Look for CODE sections which are going to be discarded,
12947 and find and discard any fragmented debug sections which
12948 are associated with that code section. */
12949 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12950 if ((isec
->flags
& SEC_CODE
) != 0
12951 && isec
->gc_mark
== 0)
12956 ilen
= strlen (isec
->name
);
12958 /* Association is determined by the name of the debug section
12959 containing the name of the code section as a suffix. For
12960 example .debug_line.text.foo is a debug section associated
12962 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
12966 if (dsec
->gc_mark
== 0
12967 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
12970 dlen
= strlen (dsec
->name
);
12973 && strncmp (dsec
->name
+ (dlen
- ilen
),
12974 isec
->name
, ilen
) == 0)
12984 /* The sweep phase of garbage collection. Remove all garbage sections. */
12986 typedef bfd_boolean (*gc_sweep_hook_fn
)
12987 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
12990 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
12993 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12994 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
12996 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13000 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13001 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13004 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13006 /* When any section in a section group is kept, we keep all
13007 sections in the section group. If the first member of
13008 the section group is excluded, we will also exclude the
13010 if (o
->flags
& SEC_GROUP
)
13012 asection
*first
= elf_next_in_group (o
);
13013 o
->gc_mark
= first
->gc_mark
;
13019 /* Skip sweeping sections already excluded. */
13020 if (o
->flags
& SEC_EXCLUDE
)
13023 /* Since this is early in the link process, it is simple
13024 to remove a section from the output. */
13025 o
->flags
|= SEC_EXCLUDE
;
13027 if (info
->print_gc_sections
&& o
->size
!= 0)
13028 /* xgettext:c-format */
13029 _bfd_error_handler (_("Removing unused section '%A' in file '%B'"),
13032 /* But we also have to update some of the relocation
13033 info we collected before. */
13035 && (o
->flags
& SEC_RELOC
) != 0
13036 && o
->reloc_count
!= 0
13037 && !((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
13038 && (o
->flags
& SEC_DEBUGGING
) != 0)
13039 && !bfd_is_abs_section (o
->output_section
))
13041 Elf_Internal_Rela
*internal_relocs
;
13045 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
13046 info
->keep_memory
);
13047 if (internal_relocs
== NULL
)
13050 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
13052 if (elf_section_data (o
)->relocs
!= internal_relocs
)
13053 free (internal_relocs
);
13064 /* Propagate collected vtable information. This is called through
13065 elf_link_hash_traverse. */
13068 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
13070 /* Those that are not vtables. */
13071 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
13074 /* Those vtables that do not have parents, we cannot merge. */
13075 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
13078 /* If we've already been done, exit. */
13079 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
13082 /* Make sure the parent's table is up to date. */
13083 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
13085 if (h
->vtable
->used
== NULL
)
13087 /* None of this table's entries were referenced. Re-use the
13089 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
13090 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
13095 bfd_boolean
*cu
, *pu
;
13097 /* Or the parent's entries into ours. */
13098 cu
= h
->vtable
->used
;
13100 pu
= h
->vtable
->parent
->vtable
->used
;
13103 const struct elf_backend_data
*bed
;
13104 unsigned int log_file_align
;
13106 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
13107 log_file_align
= bed
->s
->log_file_align
;
13108 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
13123 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
13126 bfd_vma hstart
, hend
;
13127 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
13128 const struct elf_backend_data
*bed
;
13129 unsigned int log_file_align
;
13131 /* Take care of both those symbols that do not describe vtables as
13132 well as those that are not loaded. */
13133 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
13136 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
13137 || h
->root
.type
== bfd_link_hash_defweak
);
13139 sec
= h
->root
.u
.def
.section
;
13140 hstart
= h
->root
.u
.def
.value
;
13141 hend
= hstart
+ h
->size
;
13143 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
13145 return *(bfd_boolean
*) okp
= FALSE
;
13146 bed
= get_elf_backend_data (sec
->owner
);
13147 log_file_align
= bed
->s
->log_file_align
;
13149 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
13151 for (rel
= relstart
; rel
< relend
; ++rel
)
13152 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
13154 /* If the entry is in use, do nothing. */
13155 if (h
->vtable
->used
13156 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
13158 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
13159 if (h
->vtable
->used
[entry
])
13162 /* Otherwise, kill it. */
13163 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
13169 /* Mark sections containing dynamically referenced symbols. When
13170 building shared libraries, we must assume that any visible symbol is
13174 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
13176 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
13177 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
13179 if ((h
->root
.type
== bfd_link_hash_defined
13180 || h
->root
.type
== bfd_link_hash_defweak
)
13182 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
13183 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
13184 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
13185 && (!bfd_link_executable (info
)
13186 || info
->gc_keep_exported
13187 || info
->export_dynamic
13190 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
13191 && (h
->versioned
>= versioned
13192 || !bfd_hide_sym_by_version (info
->version_info
,
13193 h
->root
.root
.string
)))))
13194 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13199 /* Keep all sections containing symbols undefined on the command-line,
13200 and the section containing the entry symbol. */
13203 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
13205 struct bfd_sym_chain
*sym
;
13207 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
13209 struct elf_link_hash_entry
*h
;
13211 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
13212 FALSE
, FALSE
, FALSE
);
13215 && (h
->root
.type
== bfd_link_hash_defined
13216 || h
->root
.type
== bfd_link_hash_defweak
)
13217 && !bfd_is_abs_section (h
->root
.u
.def
.section
)
13218 && !bfd_is_und_section (h
->root
.u
.def
.section
))
13219 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13224 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
13225 struct bfd_link_info
*info
)
13227 bfd
*ibfd
= info
->input_bfds
;
13229 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13232 struct elf_reloc_cookie cookie
;
13234 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13237 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
13240 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
13242 if (CONST_STRNEQ (bfd_section_name (ibfd
, sec
), ".eh_frame_entry")
13243 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
13245 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
13246 fini_reloc_cookie_rels (&cookie
, sec
);
13253 /* Do mark and sweep of unused sections. */
13256 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
13258 bfd_boolean ok
= TRUE
;
13260 elf_gc_mark_hook_fn gc_mark_hook
;
13261 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13262 struct elf_link_hash_table
*htab
;
13264 if (!bed
->can_gc_sections
13265 || !is_elf_hash_table (info
->hash
))
13267 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13271 bed
->gc_keep (info
);
13272 htab
= elf_hash_table (info
);
13274 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13275 at the .eh_frame section if we can mark the FDEs individually. */
13276 for (sub
= info
->input_bfds
;
13277 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
13278 sub
= sub
->link
.next
)
13281 struct elf_reloc_cookie cookie
;
13283 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
13284 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
13286 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
13287 if (elf_section_data (sec
)->sec_info
13288 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
13289 elf_eh_frame_section (sub
) = sec
;
13290 fini_reloc_cookie_for_section (&cookie
, sec
);
13291 sec
= bfd_get_next_section_by_name (NULL
, sec
);
13295 /* Apply transitive closure to the vtable entry usage info. */
13296 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
13300 /* Kill the vtable relocations that were not used. */
13301 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &ok
);
13305 /* Mark dynamically referenced symbols. */
13306 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
13307 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
13309 /* Grovel through relocs to find out who stays ... */
13310 gc_mark_hook
= bed
->gc_mark_hook
;
13311 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13315 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13316 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13319 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13320 Also treat note sections as a root, if the section is not part
13322 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13324 && (o
->flags
& SEC_EXCLUDE
) == 0
13325 && ((o
->flags
& SEC_KEEP
) != 0
13326 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
13327 && elf_next_in_group (o
) == NULL
)))
13329 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
13334 /* Allow the backend to mark additional target specific sections. */
13335 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
13337 /* ... and mark SEC_EXCLUDE for those that go. */
13338 return elf_gc_sweep (abfd
, info
);
13341 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13344 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
13346 struct elf_link_hash_entry
*h
,
13349 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
13350 struct elf_link_hash_entry
**search
, *child
;
13351 size_t extsymcount
;
13352 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13354 /* The sh_info field of the symtab header tells us where the
13355 external symbols start. We don't care about the local symbols at
13357 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
13358 if (!elf_bad_symtab (abfd
))
13359 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
13361 sym_hashes
= elf_sym_hashes (abfd
);
13362 sym_hashes_end
= sym_hashes
+ extsymcount
;
13364 /* Hunt down the child symbol, which is in this section at the same
13365 offset as the relocation. */
13366 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
13368 if ((child
= *search
) != NULL
13369 && (child
->root
.type
== bfd_link_hash_defined
13370 || child
->root
.type
== bfd_link_hash_defweak
)
13371 && child
->root
.u
.def
.section
== sec
13372 && child
->root
.u
.def
.value
== offset
)
13376 /* xgettext:c-format */
13377 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"),
13378 abfd
, sec
, (unsigned long) offset
);
13379 bfd_set_error (bfd_error_invalid_operation
);
13383 if (!child
->vtable
)
13385 child
->vtable
= ((struct elf_link_virtual_table_entry
*)
13386 bfd_zalloc (abfd
, sizeof (*child
->vtable
)));
13387 if (!child
->vtable
)
13392 /* This *should* only be the absolute section. It could potentially
13393 be that someone has defined a non-global vtable though, which
13394 would be bad. It isn't worth paging in the local symbols to be
13395 sure though; that case should simply be handled by the assembler. */
13397 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
13400 child
->vtable
->parent
= h
;
13405 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13408 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
13409 asection
*sec ATTRIBUTE_UNUSED
,
13410 struct elf_link_hash_entry
*h
,
13413 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13414 unsigned int log_file_align
= bed
->s
->log_file_align
;
13418 h
->vtable
= ((struct elf_link_virtual_table_entry
*)
13419 bfd_zalloc (abfd
, sizeof (*h
->vtable
)));
13424 if (addend
>= h
->vtable
->size
)
13426 size_t size
, bytes
, file_align
;
13427 bfd_boolean
*ptr
= h
->vtable
->used
;
13429 /* While the symbol is undefined, we have to be prepared to handle
13431 file_align
= 1 << log_file_align
;
13432 if (h
->root
.type
== bfd_link_hash_undefined
)
13433 size
= addend
+ file_align
;
13437 if (addend
>= size
)
13439 /* Oops! We've got a reference past the defined end of
13440 the table. This is probably a bug -- shall we warn? */
13441 size
= addend
+ file_align
;
13444 size
= (size
+ file_align
- 1) & -file_align
;
13446 /* Allocate one extra entry for use as a "done" flag for the
13447 consolidation pass. */
13448 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
13452 ptr
= (bfd_boolean
*) bfd_realloc (ptr
- 1, bytes
);
13458 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
13459 * sizeof (bfd_boolean
));
13460 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
13464 ptr
= (bfd_boolean
*) bfd_zmalloc (bytes
);
13469 /* And arrange for that done flag to be at index -1. */
13470 h
->vtable
->used
= ptr
+ 1;
13471 h
->vtable
->size
= size
;
13474 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
13479 /* Map an ELF section header flag to its corresponding string. */
13483 flagword flag_value
;
13484 } elf_flags_to_name_table
;
13486 static elf_flags_to_name_table elf_flags_to_names
[] =
13488 { "SHF_WRITE", SHF_WRITE
},
13489 { "SHF_ALLOC", SHF_ALLOC
},
13490 { "SHF_EXECINSTR", SHF_EXECINSTR
},
13491 { "SHF_MERGE", SHF_MERGE
},
13492 { "SHF_STRINGS", SHF_STRINGS
},
13493 { "SHF_INFO_LINK", SHF_INFO_LINK
},
13494 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
13495 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
13496 { "SHF_GROUP", SHF_GROUP
},
13497 { "SHF_TLS", SHF_TLS
},
13498 { "SHF_MASKOS", SHF_MASKOS
},
13499 { "SHF_EXCLUDE", SHF_EXCLUDE
},
13502 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13504 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
13505 struct flag_info
*flaginfo
,
13508 const bfd_vma sh_flags
= elf_section_flags (section
);
13510 if (!flaginfo
->flags_initialized
)
13512 bfd
*obfd
= info
->output_bfd
;
13513 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13514 struct flag_info_list
*tf
= flaginfo
->flag_list
;
13516 int without_hex
= 0;
13518 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
13521 flagword (*lookup
) (char *);
13523 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
13524 if (lookup
!= NULL
)
13526 flagword hexval
= (*lookup
) ((char *) tf
->name
);
13530 if (tf
->with
== with_flags
)
13531 with_hex
|= hexval
;
13532 else if (tf
->with
== without_flags
)
13533 without_hex
|= hexval
;
13538 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
13540 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
13542 if (tf
->with
== with_flags
)
13543 with_hex
|= elf_flags_to_names
[i
].flag_value
;
13544 else if (tf
->with
== without_flags
)
13545 without_hex
|= elf_flags_to_names
[i
].flag_value
;
13552 info
->callbacks
->einfo
13553 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
13557 flaginfo
->flags_initialized
= TRUE
;
13558 flaginfo
->only_with_flags
|= with_hex
;
13559 flaginfo
->not_with_flags
|= without_hex
;
13562 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
13565 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
13571 struct alloc_got_off_arg
{
13573 struct bfd_link_info
*info
;
13576 /* We need a special top-level link routine to convert got reference counts
13577 to real got offsets. */
13580 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
13582 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
13583 bfd
*obfd
= gofarg
->info
->output_bfd
;
13584 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13586 if (h
->got
.refcount
> 0)
13588 h
->got
.offset
= gofarg
->gotoff
;
13589 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
13592 h
->got
.offset
= (bfd_vma
) -1;
13597 /* And an accompanying bit to work out final got entry offsets once
13598 we're done. Should be called from final_link. */
13601 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
13602 struct bfd_link_info
*info
)
13605 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13607 struct alloc_got_off_arg gofarg
;
13609 BFD_ASSERT (abfd
== info
->output_bfd
);
13611 if (! is_elf_hash_table (info
->hash
))
13614 /* The GOT offset is relative to the .got section, but the GOT header is
13615 put into the .got.plt section, if the backend uses it. */
13616 if (bed
->want_got_plt
)
13619 gotoff
= bed
->got_header_size
;
13621 /* Do the local .got entries first. */
13622 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
13624 bfd_signed_vma
*local_got
;
13625 size_t j
, locsymcount
;
13626 Elf_Internal_Shdr
*symtab_hdr
;
13628 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
13631 local_got
= elf_local_got_refcounts (i
);
13635 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
13636 if (elf_bad_symtab (i
))
13637 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13639 locsymcount
= symtab_hdr
->sh_info
;
13641 for (j
= 0; j
< locsymcount
; ++j
)
13643 if (local_got
[j
] > 0)
13645 local_got
[j
] = gotoff
;
13646 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
13649 local_got
[j
] = (bfd_vma
) -1;
13653 /* Then the global .got entries. .plt refcounts are handled by
13654 adjust_dynamic_symbol */
13655 gofarg
.gotoff
= gotoff
;
13656 gofarg
.info
= info
;
13657 elf_link_hash_traverse (elf_hash_table (info
),
13658 elf_gc_allocate_got_offsets
,
13663 /* Many folk need no more in the way of final link than this, once
13664 got entry reference counting is enabled. */
13667 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
13669 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
13672 /* Invoke the regular ELF backend linker to do all the work. */
13673 return bfd_elf_final_link (abfd
, info
);
13677 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
13679 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
13681 if (rcookie
->bad_symtab
)
13682 rcookie
->rel
= rcookie
->rels
;
13684 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
13686 unsigned long r_symndx
;
13688 if (! rcookie
->bad_symtab
)
13689 if (rcookie
->rel
->r_offset
> offset
)
13691 if (rcookie
->rel
->r_offset
!= offset
)
13694 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
13695 if (r_symndx
== STN_UNDEF
)
13698 if (r_symndx
>= rcookie
->locsymcount
13699 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13701 struct elf_link_hash_entry
*h
;
13703 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
13705 while (h
->root
.type
== bfd_link_hash_indirect
13706 || h
->root
.type
== bfd_link_hash_warning
)
13707 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13709 if ((h
->root
.type
== bfd_link_hash_defined
13710 || h
->root
.type
== bfd_link_hash_defweak
)
13711 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
13712 || h
->root
.u
.def
.section
->kept_section
!= NULL
13713 || discarded_section (h
->root
.u
.def
.section
)))
13718 /* It's not a relocation against a global symbol,
13719 but it could be a relocation against a local
13720 symbol for a discarded section. */
13722 Elf_Internal_Sym
*isym
;
13724 /* Need to: get the symbol; get the section. */
13725 isym
= &rcookie
->locsyms
[r_symndx
];
13726 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
13728 && (isec
->kept_section
!= NULL
13729 || discarded_section (isec
)))
13737 /* Discard unneeded references to discarded sections.
13738 Returns -1 on error, 1 if any section's size was changed, 0 if
13739 nothing changed. This function assumes that the relocations are in
13740 sorted order, which is true for all known assemblers. */
13743 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
13745 struct elf_reloc_cookie cookie
;
13750 if (info
->traditional_format
13751 || !is_elf_hash_table (info
->hash
))
13754 o
= bfd_get_section_by_name (output_bfd
, ".stab");
13759 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13762 || i
->reloc_count
== 0
13763 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
13767 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13770 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13773 if (_bfd_discard_section_stabs (abfd
, i
,
13774 elf_section_data (i
)->sec_info
,
13775 bfd_elf_reloc_symbol_deleted_p
,
13779 fini_reloc_cookie_for_section (&cookie
, i
);
13784 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
13785 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
13790 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13796 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13799 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13802 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
13803 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
13804 bfd_elf_reloc_symbol_deleted_p
,
13808 fini_reloc_cookie_for_section (&cookie
, i
);
13812 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
13814 const struct elf_backend_data
*bed
;
13816 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13819 bed
= get_elf_backend_data (abfd
);
13821 if (bed
->elf_backend_discard_info
!= NULL
)
13823 if (!init_reloc_cookie (&cookie
, info
, abfd
))
13826 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
13829 fini_reloc_cookie (&cookie
, abfd
);
13833 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
13834 _bfd_elf_end_eh_frame_parsing (info
);
13836 if (info
->eh_frame_hdr_type
13837 && !bfd_link_relocatable (info
)
13838 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
13845 _bfd_elf_section_already_linked (bfd
*abfd
,
13847 struct bfd_link_info
*info
)
13850 const char *name
, *key
;
13851 struct bfd_section_already_linked
*l
;
13852 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
13854 if (sec
->output_section
== bfd_abs_section_ptr
)
13857 flags
= sec
->flags
;
13859 /* Return if it isn't a linkonce section. A comdat group section
13860 also has SEC_LINK_ONCE set. */
13861 if ((flags
& SEC_LINK_ONCE
) == 0)
13864 /* Don't put group member sections on our list of already linked
13865 sections. They are handled as a group via their group section. */
13866 if (elf_sec_group (sec
) != NULL
)
13869 /* For a SHT_GROUP section, use the group signature as the key. */
13871 if ((flags
& SEC_GROUP
) != 0
13872 && elf_next_in_group (sec
) != NULL
13873 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
13874 key
= elf_group_name (elf_next_in_group (sec
));
13877 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13878 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
13879 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
13882 /* Must be a user linkonce section that doesn't follow gcc's
13883 naming convention. In this case we won't be matching
13884 single member groups. */
13888 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
13890 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13892 /* We may have 2 different types of sections on the list: group
13893 sections with a signature of <key> (<key> is some string),
13894 and linkonce sections named .gnu.linkonce.<type>.<key>.
13895 Match like sections. LTO plugin sections are an exception.
13896 They are always named .gnu.linkonce.t.<key> and match either
13897 type of section. */
13898 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
13899 && ((flags
& SEC_GROUP
) != 0
13900 || strcmp (name
, l
->sec
->name
) == 0))
13901 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0)
13903 /* The section has already been linked. See if we should
13904 issue a warning. */
13905 if (!_bfd_handle_already_linked (sec
, l
, info
))
13908 if (flags
& SEC_GROUP
)
13910 asection
*first
= elf_next_in_group (sec
);
13911 asection
*s
= first
;
13915 s
->output_section
= bfd_abs_section_ptr
;
13916 /* Record which group discards it. */
13917 s
->kept_section
= l
->sec
;
13918 s
= elf_next_in_group (s
);
13919 /* These lists are circular. */
13929 /* A single member comdat group section may be discarded by a
13930 linkonce section and vice versa. */
13931 if ((flags
& SEC_GROUP
) != 0)
13933 asection
*first
= elf_next_in_group (sec
);
13935 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
13936 /* Check this single member group against linkonce sections. */
13937 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13938 if ((l
->sec
->flags
& SEC_GROUP
) == 0
13939 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
13941 first
->output_section
= bfd_abs_section_ptr
;
13942 first
->kept_section
= l
->sec
;
13943 sec
->output_section
= bfd_abs_section_ptr
;
13948 /* Check this linkonce section against single member groups. */
13949 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13950 if (l
->sec
->flags
& SEC_GROUP
)
13952 asection
*first
= elf_next_in_group (l
->sec
);
13955 && elf_next_in_group (first
) == first
13956 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
13958 sec
->output_section
= bfd_abs_section_ptr
;
13959 sec
->kept_section
= first
;
13964 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13965 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13966 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13967 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13968 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13969 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13970 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13971 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13972 The reverse order cannot happen as there is never a bfd with only the
13973 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13974 matter as here were are looking only for cross-bfd sections. */
13976 if ((flags
& SEC_GROUP
) == 0 && CONST_STRNEQ (name
, ".gnu.linkonce.r."))
13977 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13978 if ((l
->sec
->flags
& SEC_GROUP
) == 0
13979 && CONST_STRNEQ (l
->sec
->name
, ".gnu.linkonce.t."))
13981 if (abfd
!= l
->sec
->owner
)
13982 sec
->output_section
= bfd_abs_section_ptr
;
13986 /* This is the first section with this name. Record it. */
13987 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
13988 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
13989 return sec
->output_section
== bfd_abs_section_ptr
;
13993 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
13995 return sym
->st_shndx
== SHN_COMMON
;
13999 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
14005 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
14007 return bfd_com_section_ptr
;
14011 _bfd_elf_default_got_elt_size (bfd
*abfd
,
14012 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
14013 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
14014 bfd
*ibfd ATTRIBUTE_UNUSED
,
14015 unsigned long symndx ATTRIBUTE_UNUSED
)
14017 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14018 return bed
->s
->arch_size
/ 8;
14021 /* Routines to support the creation of dynamic relocs. */
14023 /* Returns the name of the dynamic reloc section associated with SEC. */
14025 static const char *
14026 get_dynamic_reloc_section_name (bfd
* abfd
,
14028 bfd_boolean is_rela
)
14031 const char *old_name
= bfd_get_section_name (NULL
, sec
);
14032 const char *prefix
= is_rela
? ".rela" : ".rel";
14034 if (old_name
== NULL
)
14037 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
14038 sprintf (name
, "%s%s", prefix
, old_name
);
14043 /* Returns the dynamic reloc section associated with SEC.
14044 If necessary compute the name of the dynamic reloc section based
14045 on SEC's name (looked up in ABFD's string table) and the setting
14049 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
14051 bfd_boolean is_rela
)
14053 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14055 if (reloc_sec
== NULL
)
14057 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14061 reloc_sec
= bfd_get_linker_section (abfd
, name
);
14063 if (reloc_sec
!= NULL
)
14064 elf_section_data (sec
)->sreloc
= reloc_sec
;
14071 /* Returns the dynamic reloc section associated with SEC. If the
14072 section does not exist it is created and attached to the DYNOBJ
14073 bfd and stored in the SRELOC field of SEC's elf_section_data
14076 ALIGNMENT is the alignment for the newly created section and
14077 IS_RELA defines whether the name should be .rela.<SEC's name>
14078 or .rel.<SEC's name>. The section name is looked up in the
14079 string table associated with ABFD. */
14082 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
14084 unsigned int alignment
,
14086 bfd_boolean is_rela
)
14088 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14090 if (reloc_sec
== NULL
)
14092 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14097 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
14099 if (reloc_sec
== NULL
)
14101 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
14102 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
14103 if ((sec
->flags
& SEC_ALLOC
) != 0)
14104 flags
|= SEC_ALLOC
| SEC_LOAD
;
14106 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
14107 if (reloc_sec
!= NULL
)
14109 /* _bfd_elf_get_sec_type_attr chooses a section type by
14110 name. Override as it may be wrong, eg. for a user
14111 section named "auto" we'll get ".relauto" which is
14112 seen to be a .rela section. */
14113 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
14114 if (! bfd_set_section_alignment (dynobj
, reloc_sec
, alignment
))
14119 elf_section_data (sec
)->sreloc
= reloc_sec
;
14125 /* Copy the ELF symbol type and other attributes for a linker script
14126 assignment from HSRC to HDEST. Generally this should be treated as
14127 if we found a strong non-dynamic definition for HDEST (except that
14128 ld ignores multiple definition errors). */
14130 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
14131 struct bfd_link_hash_entry
*hdest
,
14132 struct bfd_link_hash_entry
*hsrc
)
14134 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
14135 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
14136 Elf_Internal_Sym isym
;
14138 ehdest
->type
= ehsrc
->type
;
14139 ehdest
->target_internal
= ehsrc
->target_internal
;
14141 isym
.st_other
= ehsrc
->other
;
14142 elf_merge_st_other (abfd
, ehdest
, &isym
, NULL
, TRUE
, FALSE
);
14145 /* Append a RELA relocation REL to section S in BFD. */
14148 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14150 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14151 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
14152 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
14153 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
14156 /* Append a REL relocation REL to section S in BFD. */
14159 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14161 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14162 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
14163 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
14164 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);