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 dynindx == -1 is tested. If that test is true,
3067 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3068 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
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 /* Forced local symbols resolve locally. */
3093 if (h
->forced_local
)
3096 /* Common symbols that become definitions don't get the DEF_REGULAR
3097 flag set, so test it first, and don't bail out. */
3098 if (ELF_COMMON_DEF_P (h
))
3100 /* If we don't have a definition in a regular file, then we can't
3101 resolve locally. The sym is either undefined or dynamic. */
3102 else if (!h
->def_regular
)
3105 /* Non-dynamic symbols resolve locally. */
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
)
3855 && (info
->export_dynamic
|| info
->dynamic
)))
3856 && is_elf_hash_table (htab
)
3857 && info
->output_bfd
->xvec
== abfd
->xvec
3858 && !htab
->dynamic_sections_created
)
3860 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3864 else if (!is_elf_hash_table (htab
))
3868 const char *soname
= NULL
;
3870 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3871 const Elf_Internal_Phdr
*phdr
;
3874 /* ld --just-symbols and dynamic objects don't mix very well.
3875 ld shouldn't allow it. */
3879 /* If this dynamic lib was specified on the command line with
3880 --as-needed in effect, then we don't want to add a DT_NEEDED
3881 tag unless the lib is actually used. Similary for libs brought
3882 in by another lib's DT_NEEDED. When --no-add-needed is used
3883 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3884 any dynamic library in DT_NEEDED tags in the dynamic lib at
3886 add_needed
= (elf_dyn_lib_class (abfd
)
3887 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3888 | DYN_NO_NEEDED
)) == 0;
3890 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3895 unsigned int elfsec
;
3896 unsigned long shlink
;
3898 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3905 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3906 if (elfsec
== SHN_BAD
)
3907 goto error_free_dyn
;
3908 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3910 for (extdyn
= dynbuf
;
3911 extdyn
< dynbuf
+ s
->size
;
3912 extdyn
+= bed
->s
->sizeof_dyn
)
3914 Elf_Internal_Dyn dyn
;
3916 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3917 if (dyn
.d_tag
== DT_SONAME
)
3919 unsigned int tagv
= dyn
.d_un
.d_val
;
3920 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3922 goto error_free_dyn
;
3924 if (dyn
.d_tag
== DT_NEEDED
)
3926 struct bfd_link_needed_list
*n
, **pn
;
3928 unsigned int tagv
= dyn
.d_un
.d_val
;
3930 amt
= sizeof (struct bfd_link_needed_list
);
3931 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3932 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3933 if (n
== NULL
|| fnm
== NULL
)
3934 goto error_free_dyn
;
3935 amt
= strlen (fnm
) + 1;
3936 anm
= (char *) bfd_alloc (abfd
, amt
);
3938 goto error_free_dyn
;
3939 memcpy (anm
, fnm
, amt
);
3943 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3947 if (dyn
.d_tag
== DT_RUNPATH
)
3949 struct bfd_link_needed_list
*n
, **pn
;
3951 unsigned int tagv
= dyn
.d_un
.d_val
;
3953 amt
= sizeof (struct bfd_link_needed_list
);
3954 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3955 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3956 if (n
== NULL
|| fnm
== NULL
)
3957 goto error_free_dyn
;
3958 amt
= strlen (fnm
) + 1;
3959 anm
= (char *) bfd_alloc (abfd
, amt
);
3961 goto error_free_dyn
;
3962 memcpy (anm
, fnm
, amt
);
3966 for (pn
= & runpath
;
3972 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3973 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3975 struct bfd_link_needed_list
*n
, **pn
;
3977 unsigned int tagv
= dyn
.d_un
.d_val
;
3979 amt
= sizeof (struct bfd_link_needed_list
);
3980 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3981 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3982 if (n
== NULL
|| fnm
== NULL
)
3983 goto error_free_dyn
;
3984 amt
= strlen (fnm
) + 1;
3985 anm
= (char *) bfd_alloc (abfd
, amt
);
3987 goto error_free_dyn
;
3988 memcpy (anm
, fnm
, amt
);
3998 if (dyn
.d_tag
== DT_AUDIT
)
4000 unsigned int tagv
= dyn
.d_un
.d_val
;
4001 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4008 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4009 frees all more recently bfd_alloc'd blocks as well. */
4015 struct bfd_link_needed_list
**pn
;
4016 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4021 /* If we have a PT_GNU_RELRO program header, mark as read-only
4022 all sections contained fully therein. This makes relro
4023 shared library sections appear as they will at run-time. */
4024 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4025 while (--phdr
>= elf_tdata (abfd
)->phdr
)
4026 if (phdr
->p_type
== PT_GNU_RELRO
)
4028 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4029 if ((s
->flags
& SEC_ALLOC
) != 0
4030 && s
->vma
>= phdr
->p_vaddr
4031 && s
->vma
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4032 s
->flags
|= SEC_READONLY
;
4036 /* We do not want to include any of the sections in a dynamic
4037 object in the output file. We hack by simply clobbering the
4038 list of sections in the BFD. This could be handled more
4039 cleanly by, say, a new section flag; the existing
4040 SEC_NEVER_LOAD flag is not the one we want, because that one
4041 still implies that the section takes up space in the output
4043 bfd_section_list_clear (abfd
);
4045 /* Find the name to use in a DT_NEEDED entry that refers to this
4046 object. If the object has a DT_SONAME entry, we use it.
4047 Otherwise, if the generic linker stuck something in
4048 elf_dt_name, we use that. Otherwise, we just use the file
4050 if (soname
== NULL
|| *soname
== '\0')
4052 soname
= elf_dt_name (abfd
);
4053 if (soname
== NULL
|| *soname
== '\0')
4054 soname
= bfd_get_filename (abfd
);
4057 /* Save the SONAME because sometimes the linker emulation code
4058 will need to know it. */
4059 elf_dt_name (abfd
) = soname
;
4061 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4065 /* If we have already included this dynamic object in the
4066 link, just ignore it. There is no reason to include a
4067 particular dynamic object more than once. */
4071 /* Save the DT_AUDIT entry for the linker emulation code. */
4072 elf_dt_audit (abfd
) = audit
;
4075 /* If this is a dynamic object, we always link against the .dynsym
4076 symbol table, not the .symtab symbol table. The dynamic linker
4077 will only see the .dynsym symbol table, so there is no reason to
4078 look at .symtab for a dynamic object. */
4080 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4081 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4083 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4085 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4087 /* The sh_info field of the symtab header tells us where the
4088 external symbols start. We don't care about the local symbols at
4090 if (elf_bad_symtab (abfd
))
4092 extsymcount
= symcount
;
4097 extsymcount
= symcount
- hdr
->sh_info
;
4098 extsymoff
= hdr
->sh_info
;
4101 sym_hash
= elf_sym_hashes (abfd
);
4102 if (extsymcount
!= 0)
4104 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4106 if (isymbuf
== NULL
)
4109 if (sym_hash
== NULL
)
4111 /* We store a pointer to the hash table entry for each
4114 amt
*= sizeof (struct elf_link_hash_entry
*);
4115 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4116 if (sym_hash
== NULL
)
4117 goto error_free_sym
;
4118 elf_sym_hashes (abfd
) = sym_hash
;
4124 /* Read in any version definitions. */
4125 if (!_bfd_elf_slurp_version_tables (abfd
,
4126 info
->default_imported_symver
))
4127 goto error_free_sym
;
4129 /* Read in the symbol versions, but don't bother to convert them
4130 to internal format. */
4131 if (elf_dynversym (abfd
) != 0)
4133 Elf_Internal_Shdr
*versymhdr
;
4135 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4136 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
4137 if (extversym
== NULL
)
4138 goto error_free_sym
;
4139 amt
= versymhdr
->sh_size
;
4140 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
4141 || bfd_bread (extversym
, amt
, abfd
) != amt
)
4142 goto error_free_vers
;
4146 /* If we are loading an as-needed shared lib, save the symbol table
4147 state before we start adding symbols. If the lib turns out
4148 to be unneeded, restore the state. */
4149 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4154 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4156 struct bfd_hash_entry
*p
;
4157 struct elf_link_hash_entry
*h
;
4159 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4161 h
= (struct elf_link_hash_entry
*) p
;
4162 entsize
+= htab
->root
.table
.entsize
;
4163 if (h
->root
.type
== bfd_link_hash_warning
)
4164 entsize
+= htab
->root
.table
.entsize
;
4168 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4169 old_tab
= bfd_malloc (tabsize
+ entsize
);
4170 if (old_tab
== NULL
)
4171 goto error_free_vers
;
4173 /* Remember the current objalloc pointer, so that all mem for
4174 symbols added can later be reclaimed. */
4175 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4176 if (alloc_mark
== NULL
)
4177 goto error_free_vers
;
4179 /* Make a special call to the linker "notice" function to
4180 tell it that we are about to handle an as-needed lib. */
4181 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4182 goto error_free_vers
;
4184 /* Clone the symbol table. Remember some pointers into the
4185 symbol table, and dynamic symbol count. */
4186 old_ent
= (char *) old_tab
+ tabsize
;
4187 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4188 old_undefs
= htab
->root
.undefs
;
4189 old_undefs_tail
= htab
->root
.undefs_tail
;
4190 old_table
= htab
->root
.table
.table
;
4191 old_size
= htab
->root
.table
.size
;
4192 old_count
= htab
->root
.table
.count
;
4193 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4194 if (old_strtab
== NULL
)
4195 goto error_free_vers
;
4197 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4199 struct bfd_hash_entry
*p
;
4200 struct elf_link_hash_entry
*h
;
4202 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4204 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
4205 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4206 h
= (struct elf_link_hash_entry
*) p
;
4207 if (h
->root
.type
== bfd_link_hash_warning
)
4209 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
4210 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4217 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
4218 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
4220 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4224 asection
*sec
, *new_sec
;
4227 struct elf_link_hash_entry
*h
;
4228 struct elf_link_hash_entry
*hi
;
4229 bfd_boolean definition
;
4230 bfd_boolean size_change_ok
;
4231 bfd_boolean type_change_ok
;
4232 bfd_boolean new_weakdef
;
4233 bfd_boolean new_weak
;
4234 bfd_boolean old_weak
;
4235 bfd_boolean override
;
4237 bfd_boolean discarded
;
4238 unsigned int old_alignment
;
4240 bfd_boolean matched
;
4244 flags
= BSF_NO_FLAGS
;
4246 value
= isym
->st_value
;
4247 common
= bed
->common_definition (isym
);
4250 bind
= ELF_ST_BIND (isym
->st_info
);
4254 /* This should be impossible, since ELF requires that all
4255 global symbols follow all local symbols, and that sh_info
4256 point to the first global symbol. Unfortunately, Irix 5
4261 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4269 case STB_GNU_UNIQUE
:
4270 flags
= BSF_GNU_UNIQUE
;
4274 /* Leave it up to the processor backend. */
4278 if (isym
->st_shndx
== SHN_UNDEF
)
4279 sec
= bfd_und_section_ptr
;
4280 else if (isym
->st_shndx
== SHN_ABS
)
4281 sec
= bfd_abs_section_ptr
;
4282 else if (isym
->st_shndx
== SHN_COMMON
)
4284 sec
= bfd_com_section_ptr
;
4285 /* What ELF calls the size we call the value. What ELF
4286 calls the value we call the alignment. */
4287 value
= isym
->st_size
;
4291 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4293 sec
= bfd_abs_section_ptr
;
4294 else if (discarded_section (sec
))
4296 /* Symbols from discarded section are undefined. We keep
4298 sec
= bfd_und_section_ptr
;
4300 isym
->st_shndx
= SHN_UNDEF
;
4302 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4306 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4309 goto error_free_vers
;
4311 if (isym
->st_shndx
== SHN_COMMON
4312 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4314 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4318 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4320 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4322 goto error_free_vers
;
4326 else if (isym
->st_shndx
== SHN_COMMON
4327 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4328 && !bfd_link_relocatable (info
))
4330 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4334 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4335 | SEC_LINKER_CREATED
);
4336 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4338 goto error_free_vers
;
4342 else if (bed
->elf_add_symbol_hook
)
4344 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4346 goto error_free_vers
;
4348 /* The hook function sets the name to NULL if this symbol
4349 should be skipped for some reason. */
4354 /* Sanity check that all possibilities were handled. */
4357 bfd_set_error (bfd_error_bad_value
);
4358 goto error_free_vers
;
4361 /* Silently discard TLS symbols from --just-syms. There's
4362 no way to combine a static TLS block with a new TLS block
4363 for this executable. */
4364 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4365 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4368 if (bfd_is_und_section (sec
)
4369 || bfd_is_com_section (sec
))
4374 size_change_ok
= FALSE
;
4375 type_change_ok
= bed
->type_change_ok
;
4382 if (is_elf_hash_table (htab
))
4384 Elf_Internal_Versym iver
;
4385 unsigned int vernum
= 0;
4390 if (info
->default_imported_symver
)
4391 /* Use the default symbol version created earlier. */
4392 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4397 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4399 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4401 /* If this is a hidden symbol, or if it is not version
4402 1, we append the version name to the symbol name.
4403 However, we do not modify a non-hidden absolute symbol
4404 if it is not a function, because it might be the version
4405 symbol itself. FIXME: What if it isn't? */
4406 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4408 && (!bfd_is_abs_section (sec
)
4409 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4412 size_t namelen
, verlen
, newlen
;
4415 if (isym
->st_shndx
!= SHN_UNDEF
)
4417 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4419 else if (vernum
> 1)
4421 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4428 /* xgettext:c-format */
4429 (_("%B: %s: invalid version %u (max %d)"),
4431 elf_tdata (abfd
)->cverdefs
);
4432 bfd_set_error (bfd_error_bad_value
);
4433 goto error_free_vers
;
4438 /* We cannot simply test for the number of
4439 entries in the VERNEED section since the
4440 numbers for the needed versions do not start
4442 Elf_Internal_Verneed
*t
;
4445 for (t
= elf_tdata (abfd
)->verref
;
4449 Elf_Internal_Vernaux
*a
;
4451 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4453 if (a
->vna_other
== vernum
)
4455 verstr
= a
->vna_nodename
;
4465 /* xgettext:c-format */
4466 (_("%B: %s: invalid needed version %d"),
4467 abfd
, name
, vernum
);
4468 bfd_set_error (bfd_error_bad_value
);
4469 goto error_free_vers
;
4473 namelen
= strlen (name
);
4474 verlen
= strlen (verstr
);
4475 newlen
= namelen
+ verlen
+ 2;
4476 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4477 && isym
->st_shndx
!= SHN_UNDEF
)
4480 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4481 if (newname
== NULL
)
4482 goto error_free_vers
;
4483 memcpy (newname
, name
, namelen
);
4484 p
= newname
+ namelen
;
4486 /* If this is a defined non-hidden version symbol,
4487 we add another @ to the name. This indicates the
4488 default version of the symbol. */
4489 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4490 && isym
->st_shndx
!= SHN_UNDEF
)
4492 memcpy (p
, verstr
, verlen
+ 1);
4497 /* If this symbol has default visibility and the user has
4498 requested we not re-export it, then mark it as hidden. */
4499 if (!bfd_is_und_section (sec
)
4502 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4503 isym
->st_other
= (STV_HIDDEN
4504 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4506 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4507 sym_hash
, &old_bfd
, &old_weak
,
4508 &old_alignment
, &skip
, &override
,
4509 &type_change_ok
, &size_change_ok
,
4511 goto error_free_vers
;
4516 /* Override a definition only if the new symbol matches the
4518 if (override
&& matched
)
4522 while (h
->root
.type
== bfd_link_hash_indirect
4523 || h
->root
.type
== bfd_link_hash_warning
)
4524 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4526 if (elf_tdata (abfd
)->verdef
!= NULL
4529 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4532 if (! (_bfd_generic_link_add_one_symbol
4533 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4534 (struct bfd_link_hash_entry
**) sym_hash
)))
4535 goto error_free_vers
;
4537 if ((flags
& BSF_GNU_UNIQUE
)
4538 && (abfd
->flags
& DYNAMIC
) == 0
4539 && bfd_get_flavour (info
->output_bfd
) == bfd_target_elf_flavour
)
4540 elf_tdata (info
->output_bfd
)->has_gnu_symbols
|= elf_gnu_symbol_unique
;
4543 /* We need to make sure that indirect symbol dynamic flags are
4546 while (h
->root
.type
== bfd_link_hash_indirect
4547 || h
->root
.type
== bfd_link_hash_warning
)
4548 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4550 /* Setting the index to -3 tells elf_link_output_extsym that
4551 this symbol is defined in a discarded section. */
4557 new_weak
= (flags
& BSF_WEAK
) != 0;
4558 new_weakdef
= FALSE
;
4562 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4563 && is_elf_hash_table (htab
)
4564 && h
->u
.weakdef
== NULL
)
4566 /* Keep a list of all weak defined non function symbols from
4567 a dynamic object, using the weakdef field. Later in this
4568 function we will set the weakdef field to the correct
4569 value. We only put non-function symbols from dynamic
4570 objects on this list, because that happens to be the only
4571 time we need to know the normal symbol corresponding to a
4572 weak symbol, and the information is time consuming to
4573 figure out. If the weakdef field is not already NULL,
4574 then this symbol was already defined by some previous
4575 dynamic object, and we will be using that previous
4576 definition anyhow. */
4578 h
->u
.weakdef
= weaks
;
4583 /* Set the alignment of a common symbol. */
4584 if ((common
|| bfd_is_com_section (sec
))
4585 && h
->root
.type
== bfd_link_hash_common
)
4590 align
= bfd_log2 (isym
->st_value
);
4593 /* The new symbol is a common symbol in a shared object.
4594 We need to get the alignment from the section. */
4595 align
= new_sec
->alignment_power
;
4597 if (align
> old_alignment
)
4598 h
->root
.u
.c
.p
->alignment_power
= align
;
4600 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4603 if (is_elf_hash_table (htab
))
4605 /* Set a flag in the hash table entry indicating the type of
4606 reference or definition we just found. A dynamic symbol
4607 is one which is referenced or defined by both a regular
4608 object and a shared object. */
4609 bfd_boolean dynsym
= FALSE
;
4611 /* Plugin symbols aren't normal. Don't set def_regular or
4612 ref_regular for them, or make them dynamic. */
4613 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4620 if (bind
!= STB_WEAK
)
4621 h
->ref_regular_nonweak
= 1;
4633 /* If the indirect symbol has been forced local, don't
4634 make the real symbol dynamic. */
4635 if ((h
== hi
|| !hi
->forced_local
)
4636 && (bfd_link_dll (info
)
4646 hi
->ref_dynamic
= 1;
4651 hi
->def_dynamic
= 1;
4654 /* If the indirect symbol has been forced local, don't
4655 make the real symbol dynamic. */
4656 if ((h
== hi
|| !hi
->forced_local
)
4659 || (h
->u
.weakdef
!= NULL
4661 && h
->u
.weakdef
->dynindx
!= -1)))
4665 /* Check to see if we need to add an indirect symbol for
4666 the default name. */
4668 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
4669 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4670 sec
, value
, &old_bfd
, &dynsym
))
4671 goto error_free_vers
;
4673 /* Check the alignment when a common symbol is involved. This
4674 can change when a common symbol is overridden by a normal
4675 definition or a common symbol is ignored due to the old
4676 normal definition. We need to make sure the maximum
4677 alignment is maintained. */
4678 if ((old_alignment
|| common
)
4679 && h
->root
.type
!= bfd_link_hash_common
)
4681 unsigned int common_align
;
4682 unsigned int normal_align
;
4683 unsigned int symbol_align
;
4687 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
4688 || h
->root
.type
== bfd_link_hash_defweak
);
4690 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4691 if (h
->root
.u
.def
.section
->owner
!= NULL
4692 && (h
->root
.u
.def
.section
->owner
->flags
4693 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
4695 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4696 if (normal_align
> symbol_align
)
4697 normal_align
= symbol_align
;
4700 normal_align
= symbol_align
;
4704 common_align
= old_alignment
;
4705 common_bfd
= old_bfd
;
4710 common_align
= bfd_log2 (isym
->st_value
);
4712 normal_bfd
= old_bfd
;
4715 if (normal_align
< common_align
)
4717 /* PR binutils/2735 */
4718 if (normal_bfd
== NULL
)
4720 /* xgettext:c-format */
4721 (_("Warning: alignment %u of common symbol `%s' in %B is"
4722 " greater than the alignment (%u) of its section %A"),
4723 1 << common_align
, name
, common_bfd
,
4724 1 << normal_align
, h
->root
.u
.def
.section
);
4727 /* xgettext:c-format */
4728 (_("Warning: alignment %u of symbol `%s' in %B"
4729 " is smaller than %u in %B"),
4730 1 << normal_align
, name
, normal_bfd
,
4731 1 << common_align
, common_bfd
);
4735 /* Remember the symbol size if it isn't undefined. */
4736 if (isym
->st_size
!= 0
4737 && isym
->st_shndx
!= SHN_UNDEF
4738 && (definition
|| h
->size
== 0))
4741 && h
->size
!= isym
->st_size
4742 && ! size_change_ok
)
4744 /* xgettext:c-format */
4745 (_("Warning: size of symbol `%s' changed"
4746 " from %lu in %B to %lu in %B"),
4747 name
, (unsigned long) h
->size
, old_bfd
,
4748 (unsigned long) isym
->st_size
, abfd
);
4750 h
->size
= isym
->st_size
;
4753 /* If this is a common symbol, then we always want H->SIZE
4754 to be the size of the common symbol. The code just above
4755 won't fix the size if a common symbol becomes larger. We
4756 don't warn about a size change here, because that is
4757 covered by --warn-common. Allow changes between different
4759 if (h
->root
.type
== bfd_link_hash_common
)
4760 h
->size
= h
->root
.u
.c
.size
;
4762 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4763 && ((definition
&& !new_weak
)
4764 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
4765 || h
->type
== STT_NOTYPE
))
4767 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
4769 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4771 if (type
== STT_GNU_IFUNC
4772 && (abfd
->flags
& DYNAMIC
) != 0)
4775 if (h
->type
!= type
)
4777 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
4778 /* xgettext:c-format */
4780 (_("Warning: type of symbol `%s' changed"
4781 " from %d to %d in %B"),
4782 name
, h
->type
, type
, abfd
);
4788 /* Merge st_other field. */
4789 elf_merge_st_other (abfd
, h
, isym
, sec
, definition
, dynamic
);
4791 /* We don't want to make debug symbol dynamic. */
4793 && (sec
->flags
& SEC_DEBUGGING
)
4794 && !bfd_link_relocatable (info
))
4797 /* Nor should we make plugin symbols dynamic. */
4798 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4803 h
->target_internal
= isym
->st_target_internal
;
4804 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
4807 if (definition
&& !dynamic
)
4809 char *p
= strchr (name
, ELF_VER_CHR
);
4810 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4812 /* Queue non-default versions so that .symver x, x@FOO
4813 aliases can be checked. */
4816 amt
= ((isymend
- isym
+ 1)
4817 * sizeof (struct elf_link_hash_entry
*));
4819 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
4821 goto error_free_vers
;
4823 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4827 if (dynsym
&& h
->dynindx
== -1)
4829 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4830 goto error_free_vers
;
4831 if (h
->u
.weakdef
!= NULL
4833 && h
->u
.weakdef
->dynindx
== -1)
4835 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4836 goto error_free_vers
;
4839 else if (h
->dynindx
!= -1)
4840 /* If the symbol already has a dynamic index, but
4841 visibility says it should not be visible, turn it into
4843 switch (ELF_ST_VISIBILITY (h
->other
))
4847 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4852 /* Don't add DT_NEEDED for references from the dummy bfd nor
4853 for unmatched symbol. */
4858 && h
->ref_regular_nonweak
4860 || (old_bfd
->flags
& BFD_PLUGIN
) == 0))
4861 || (h
->ref_dynamic_nonweak
4862 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
4863 && !on_needed_list (elf_dt_name (abfd
),
4864 htab
->needed
, NULL
))))
4867 const char *soname
= elf_dt_name (abfd
);
4869 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
4870 h
->root
.root
.string
);
4872 /* A symbol from a library loaded via DT_NEEDED of some
4873 other library is referenced by a regular object.
4874 Add a DT_NEEDED entry for it. Issue an error if
4875 --no-add-needed is used and the reference was not
4878 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4881 /* xgettext:c-format */
4882 (_("%B: undefined reference to symbol '%s'"),
4884 bfd_set_error (bfd_error_missing_dso
);
4885 goto error_free_vers
;
4888 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
4889 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
4892 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4894 goto error_free_vers
;
4896 BFD_ASSERT (ret
== 0);
4901 if (extversym
!= NULL
)
4907 if (isymbuf
!= NULL
)
4913 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4917 /* Restore the symbol table. */
4918 old_ent
= (char *) old_tab
+ tabsize
;
4919 memset (elf_sym_hashes (abfd
), 0,
4920 extsymcount
* sizeof (struct elf_link_hash_entry
*));
4921 htab
->root
.table
.table
= old_table
;
4922 htab
->root
.table
.size
= old_size
;
4923 htab
->root
.table
.count
= old_count
;
4924 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4925 htab
->root
.undefs
= old_undefs
;
4926 htab
->root
.undefs_tail
= old_undefs_tail
;
4927 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
4930 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4932 struct bfd_hash_entry
*p
;
4933 struct elf_link_hash_entry
*h
;
4935 unsigned int alignment_power
;
4936 unsigned int dynamic_ref_after_ir_def
;
4938 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4940 h
= (struct elf_link_hash_entry
*) p
;
4941 if (h
->root
.type
== bfd_link_hash_warning
)
4942 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4944 /* Preserve the maximum alignment and size for common
4945 symbols even if this dynamic lib isn't on DT_NEEDED
4946 since it can still be loaded at run time by another
4948 if (h
->root
.type
== bfd_link_hash_common
)
4950 size
= h
->root
.u
.c
.size
;
4951 alignment_power
= h
->root
.u
.c
.p
->alignment_power
;
4956 alignment_power
= 0;
4958 /* Preserve dynamic_ref_after_ir_def so that this symbol
4959 will be exported when the dynamic lib becomes needed
4960 in the second pass. */
4961 dynamic_ref_after_ir_def
= h
->root
.dynamic_ref_after_ir_def
;
4962 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4963 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4964 h
= (struct elf_link_hash_entry
*) p
;
4965 if (h
->root
.type
== bfd_link_hash_warning
)
4967 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4968 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4969 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4971 if (h
->root
.type
== bfd_link_hash_common
)
4973 if (size
> h
->root
.u
.c
.size
)
4974 h
->root
.u
.c
.size
= size
;
4975 if (alignment_power
> h
->root
.u
.c
.p
->alignment_power
)
4976 h
->root
.u
.c
.p
->alignment_power
= alignment_power
;
4978 h
->root
.dynamic_ref_after_ir_def
= dynamic_ref_after_ir_def
;
4982 /* Make a special call to the linker "notice" function to
4983 tell it that symbols added for crefs may need to be removed. */
4984 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
4985 goto error_free_vers
;
4988 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4990 if (nondeflt_vers
!= NULL
)
4991 free (nondeflt_vers
);
4995 if (old_tab
!= NULL
)
4997 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
4998 goto error_free_vers
;
5003 /* Now that all the symbols from this input file are created, if
5004 not performing a relocatable link, handle .symver foo, foo@BAR
5005 such that any relocs against foo become foo@BAR. */
5006 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5010 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5012 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5013 char *shortname
, *p
;
5015 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5017 || (h
->root
.type
!= bfd_link_hash_defined
5018 && h
->root
.type
!= bfd_link_hash_defweak
))
5021 amt
= p
- h
->root
.root
.string
;
5022 shortname
= (char *) bfd_malloc (amt
+ 1);
5024 goto error_free_vers
;
5025 memcpy (shortname
, h
->root
.root
.string
, amt
);
5026 shortname
[amt
] = '\0';
5028 hi
= (struct elf_link_hash_entry
*)
5029 bfd_link_hash_lookup (&htab
->root
, shortname
,
5030 FALSE
, FALSE
, FALSE
);
5032 && hi
->root
.type
== h
->root
.type
5033 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5034 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5036 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
5037 hi
->root
.type
= bfd_link_hash_indirect
;
5038 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5039 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5040 sym_hash
= elf_sym_hashes (abfd
);
5042 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5043 if (sym_hash
[symidx
] == hi
)
5045 sym_hash
[symidx
] = h
;
5051 free (nondeflt_vers
);
5052 nondeflt_vers
= NULL
;
5055 /* Now set the weakdefs field correctly for all the weak defined
5056 symbols we found. The only way to do this is to search all the
5057 symbols. Since we only need the information for non functions in
5058 dynamic objects, that's the only time we actually put anything on
5059 the list WEAKS. We need this information so that if a regular
5060 object refers to a symbol defined weakly in a dynamic object, the
5061 real symbol in the dynamic object is also put in the dynamic
5062 symbols; we also must arrange for both symbols to point to the
5063 same memory location. We could handle the general case of symbol
5064 aliasing, but a general symbol alias can only be generated in
5065 assembler code, handling it correctly would be very time
5066 consuming, and other ELF linkers don't handle general aliasing
5070 struct elf_link_hash_entry
**hpp
;
5071 struct elf_link_hash_entry
**hppend
;
5072 struct elf_link_hash_entry
**sorted_sym_hash
;
5073 struct elf_link_hash_entry
*h
;
5076 /* Since we have to search the whole symbol list for each weak
5077 defined symbol, search time for N weak defined symbols will be
5078 O(N^2). Binary search will cut it down to O(NlogN). */
5080 amt
*= sizeof (struct elf_link_hash_entry
*);
5081 sorted_sym_hash
= (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5082 if (sorted_sym_hash
== NULL
)
5084 sym_hash
= sorted_sym_hash
;
5085 hpp
= elf_sym_hashes (abfd
);
5086 hppend
= hpp
+ extsymcount
;
5088 for (; hpp
< hppend
; hpp
++)
5092 && h
->root
.type
== bfd_link_hash_defined
5093 && !bed
->is_function_type (h
->type
))
5101 qsort (sorted_sym_hash
, sym_count
,
5102 sizeof (struct elf_link_hash_entry
*),
5105 while (weaks
!= NULL
)
5107 struct elf_link_hash_entry
*hlook
;
5110 size_t i
, j
, idx
= 0;
5113 weaks
= hlook
->u
.weakdef
;
5114 hlook
->u
.weakdef
= NULL
;
5116 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
5117 || hlook
->root
.type
== bfd_link_hash_defweak
5118 || hlook
->root
.type
== bfd_link_hash_common
5119 || hlook
->root
.type
== bfd_link_hash_indirect
);
5120 slook
= hlook
->root
.u
.def
.section
;
5121 vlook
= hlook
->root
.u
.def
.value
;
5127 bfd_signed_vma vdiff
;
5129 h
= sorted_sym_hash
[idx
];
5130 vdiff
= vlook
- h
->root
.u
.def
.value
;
5137 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5147 /* We didn't find a value/section match. */
5151 /* With multiple aliases, or when the weak symbol is already
5152 strongly defined, we have multiple matching symbols and
5153 the binary search above may land on any of them. Step
5154 one past the matching symbol(s). */
5157 h
= sorted_sym_hash
[idx
];
5158 if (h
->root
.u
.def
.section
!= slook
5159 || h
->root
.u
.def
.value
!= vlook
)
5163 /* Now look back over the aliases. Since we sorted by size
5164 as well as value and section, we'll choose the one with
5165 the largest size. */
5168 h
= sorted_sym_hash
[idx
];
5170 /* Stop if value or section doesn't match. */
5171 if (h
->root
.u
.def
.section
!= slook
5172 || h
->root
.u
.def
.value
!= vlook
)
5174 else if (h
!= hlook
)
5176 hlook
->u
.weakdef
= h
;
5178 /* If the weak definition is in the list of dynamic
5179 symbols, make sure the real definition is put
5181 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5183 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5186 free (sorted_sym_hash
);
5191 /* If the real definition is in the list of dynamic
5192 symbols, make sure the weak definition is put
5193 there as well. If we don't do this, then the
5194 dynamic loader might not merge the entries for the
5195 real definition and the weak definition. */
5196 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5198 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5199 goto err_free_sym_hash
;
5206 free (sorted_sym_hash
);
5209 if (bed
->check_directives
5210 && !(*bed
->check_directives
) (abfd
, info
))
5213 if (!info
->check_relocs_after_open_input
5214 && !_bfd_elf_link_check_relocs (abfd
, info
))
5217 /* If this is a non-traditional link, try to optimize the handling
5218 of the .stab/.stabstr sections. */
5220 && ! info
->traditional_format
5221 && is_elf_hash_table (htab
)
5222 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5226 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5227 if (stabstr
!= NULL
)
5229 bfd_size_type string_offset
= 0;
5232 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5233 if (CONST_STRNEQ (stab
->name
, ".stab")
5234 && (!stab
->name
[5] ||
5235 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5236 && (stab
->flags
& SEC_MERGE
) == 0
5237 && !bfd_is_abs_section (stab
->output_section
))
5239 struct bfd_elf_section_data
*secdata
;
5241 secdata
= elf_section_data (stab
);
5242 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5243 stabstr
, &secdata
->sec_info
,
5246 if (secdata
->sec_info
)
5247 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5252 if (is_elf_hash_table (htab
) && add_needed
)
5254 /* Add this bfd to the loaded list. */
5255 struct elf_link_loaded_list
*n
;
5257 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5261 n
->next
= htab
->loaded
;
5268 if (old_tab
!= NULL
)
5270 if (old_strtab
!= NULL
)
5272 if (nondeflt_vers
!= NULL
)
5273 free (nondeflt_vers
);
5274 if (extversym
!= NULL
)
5277 if (isymbuf
!= NULL
)
5283 /* Return the linker hash table entry of a symbol that might be
5284 satisfied by an archive symbol. Return -1 on error. */
5286 struct elf_link_hash_entry
*
5287 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5288 struct bfd_link_info
*info
,
5291 struct elf_link_hash_entry
*h
;
5295 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, TRUE
);
5299 /* If this is a default version (the name contains @@), look up the
5300 symbol again with only one `@' as well as without the version.
5301 The effect is that references to the symbol with and without the
5302 version will be matched by the default symbol in the archive. */
5304 p
= strchr (name
, ELF_VER_CHR
);
5305 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5308 /* First check with only one `@'. */
5309 len
= strlen (name
);
5310 copy
= (char *) bfd_alloc (abfd
, len
);
5312 return (struct elf_link_hash_entry
*) 0 - 1;
5314 first
= p
- name
+ 1;
5315 memcpy (copy
, name
, first
);
5316 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5318 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, TRUE
);
5321 /* We also need to check references to the symbol without the
5323 copy
[first
- 1] = '\0';
5324 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
5325 FALSE
, FALSE
, TRUE
);
5328 bfd_release (abfd
, copy
);
5332 /* Add symbols from an ELF archive file to the linker hash table. We
5333 don't use _bfd_generic_link_add_archive_symbols because we need to
5334 handle versioned symbols.
5336 Fortunately, ELF archive handling is simpler than that done by
5337 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5338 oddities. In ELF, if we find a symbol in the archive map, and the
5339 symbol is currently undefined, we know that we must pull in that
5342 Unfortunately, we do have to make multiple passes over the symbol
5343 table until nothing further is resolved. */
5346 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5349 unsigned char *included
= NULL
;
5353 const struct elf_backend_data
*bed
;
5354 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
5355 (bfd
*, struct bfd_link_info
*, const char *);
5357 if (! bfd_has_map (abfd
))
5359 /* An empty archive is a special case. */
5360 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5362 bfd_set_error (bfd_error_no_armap
);
5366 /* Keep track of all symbols we know to be already defined, and all
5367 files we know to be already included. This is to speed up the
5368 second and subsequent passes. */
5369 c
= bfd_ardata (abfd
)->symdef_count
;
5373 amt
*= sizeof (*included
);
5374 included
= (unsigned char *) bfd_zmalloc (amt
);
5375 if (included
== NULL
)
5378 symdefs
= bfd_ardata (abfd
)->symdefs
;
5379 bed
= get_elf_backend_data (abfd
);
5380 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5393 symdefend
= symdef
+ c
;
5394 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5396 struct elf_link_hash_entry
*h
;
5398 struct bfd_link_hash_entry
*undefs_tail
;
5403 if (symdef
->file_offset
== last
)
5409 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5410 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
5416 if (h
->root
.type
== bfd_link_hash_common
)
5418 /* We currently have a common symbol. The archive map contains
5419 a reference to this symbol, so we may want to include it. We
5420 only want to include it however, if this archive element
5421 contains a definition of the symbol, not just another common
5424 Unfortunately some archivers (including GNU ar) will put
5425 declarations of common symbols into their archive maps, as
5426 well as real definitions, so we cannot just go by the archive
5427 map alone. Instead we must read in the element's symbol
5428 table and check that to see what kind of symbol definition
5430 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5433 else if (h
->root
.type
!= bfd_link_hash_undefined
)
5435 if (h
->root
.type
!= bfd_link_hash_undefweak
)
5436 /* Symbol must be defined. Don't check it again. */
5441 /* We need to include this archive member. */
5442 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5443 if (element
== NULL
)
5446 if (! bfd_check_format (element
, bfd_object
))
5449 undefs_tail
= info
->hash
->undefs_tail
;
5451 if (!(*info
->callbacks
5452 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5454 if (!bfd_link_add_symbols (element
, info
))
5457 /* If there are any new undefined symbols, we need to make
5458 another pass through the archive in order to see whether
5459 they can be defined. FIXME: This isn't perfect, because
5460 common symbols wind up on undefs_tail and because an
5461 undefined symbol which is defined later on in this pass
5462 does not require another pass. This isn't a bug, but it
5463 does make the code less efficient than it could be. */
5464 if (undefs_tail
!= info
->hash
->undefs_tail
)
5467 /* Look backward to mark all symbols from this object file
5468 which we have already seen in this pass. */
5472 included
[mark
] = TRUE
;
5477 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5479 /* We mark subsequent symbols from this object file as we go
5480 on through the loop. */
5481 last
= symdef
->file_offset
;
5491 if (included
!= NULL
)
5496 /* Given an ELF BFD, add symbols to the global hash table as
5500 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5502 switch (bfd_get_format (abfd
))
5505 return elf_link_add_object_symbols (abfd
, info
);
5507 return elf_link_add_archive_symbols (abfd
, info
);
5509 bfd_set_error (bfd_error_wrong_format
);
5514 struct hash_codes_info
5516 unsigned long *hashcodes
;
5520 /* This function will be called though elf_link_hash_traverse to store
5521 all hash value of the exported symbols in an array. */
5524 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5526 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
5531 /* Ignore indirect symbols. These are added by the versioning code. */
5532 if (h
->dynindx
== -1)
5535 name
= h
->root
.root
.string
;
5536 if (h
->versioned
>= versioned
)
5538 char *p
= strchr (name
, ELF_VER_CHR
);
5541 alc
= (char *) bfd_malloc (p
- name
+ 1);
5547 memcpy (alc
, name
, p
- name
);
5548 alc
[p
- name
] = '\0';
5553 /* Compute the hash value. */
5554 ha
= bfd_elf_hash (name
);
5556 /* Store the found hash value in the array given as the argument. */
5557 *(inf
->hashcodes
)++ = ha
;
5559 /* And store it in the struct so that we can put it in the hash table
5561 h
->u
.elf_hash_value
= ha
;
5569 struct collect_gnu_hash_codes
5572 const struct elf_backend_data
*bed
;
5573 unsigned long int nsyms
;
5574 unsigned long int maskbits
;
5575 unsigned long int *hashcodes
;
5576 unsigned long int *hashval
;
5577 unsigned long int *indx
;
5578 unsigned long int *counts
;
5581 long int min_dynindx
;
5582 unsigned long int bucketcount
;
5583 unsigned long int symindx
;
5584 long int local_indx
;
5585 long int shift1
, shift2
;
5586 unsigned long int mask
;
5590 /* This function will be called though elf_link_hash_traverse to store
5591 all hash value of the exported symbols in an array. */
5594 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5596 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5601 /* Ignore indirect symbols. These are added by the versioning code. */
5602 if (h
->dynindx
== -1)
5605 /* Ignore also local symbols and undefined symbols. */
5606 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5609 name
= h
->root
.root
.string
;
5610 if (h
->versioned
>= versioned
)
5612 char *p
= strchr (name
, ELF_VER_CHR
);
5615 alc
= (char *) bfd_malloc (p
- name
+ 1);
5621 memcpy (alc
, name
, p
- name
);
5622 alc
[p
- name
] = '\0';
5627 /* Compute the hash value. */
5628 ha
= bfd_elf_gnu_hash (name
);
5630 /* Store the found hash value in the array for compute_bucket_count,
5631 and also for .dynsym reordering purposes. */
5632 s
->hashcodes
[s
->nsyms
] = ha
;
5633 s
->hashval
[h
->dynindx
] = ha
;
5635 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5636 s
->min_dynindx
= h
->dynindx
;
5644 /* This function will be called though elf_link_hash_traverse to do
5645 final dynaminc symbol renumbering. */
5648 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5650 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5651 unsigned long int bucket
;
5652 unsigned long int val
;
5654 /* Ignore indirect symbols. */
5655 if (h
->dynindx
== -1)
5658 /* Ignore also local symbols and undefined symbols. */
5659 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5661 if (h
->dynindx
>= s
->min_dynindx
)
5662 h
->dynindx
= s
->local_indx
++;
5666 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5667 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5668 & ((s
->maskbits
>> s
->shift1
) - 1);
5669 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5671 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5672 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5673 if (s
->counts
[bucket
] == 1)
5674 /* Last element terminates the chain. */
5676 bfd_put_32 (s
->output_bfd
, val
,
5677 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5678 --s
->counts
[bucket
];
5679 h
->dynindx
= s
->indx
[bucket
]++;
5683 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5686 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5688 return !(h
->forced_local
5689 || h
->root
.type
== bfd_link_hash_undefined
5690 || h
->root
.type
== bfd_link_hash_undefweak
5691 || ((h
->root
.type
== bfd_link_hash_defined
5692 || h
->root
.type
== bfd_link_hash_defweak
)
5693 && h
->root
.u
.def
.section
->output_section
== NULL
));
5696 /* Array used to determine the number of hash table buckets to use
5697 based on the number of symbols there are. If there are fewer than
5698 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5699 fewer than 37 we use 17 buckets, and so forth. We never use more
5700 than 32771 buckets. */
5702 static const size_t elf_buckets
[] =
5704 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5708 /* Compute bucket count for hashing table. We do not use a static set
5709 of possible tables sizes anymore. Instead we determine for all
5710 possible reasonable sizes of the table the outcome (i.e., the
5711 number of collisions etc) and choose the best solution. The
5712 weighting functions are not too simple to allow the table to grow
5713 without bounds. Instead one of the weighting factors is the size.
5714 Therefore the result is always a good payoff between few collisions
5715 (= short chain lengths) and table size. */
5717 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
5718 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
5719 unsigned long int nsyms
,
5722 size_t best_size
= 0;
5723 unsigned long int i
;
5725 /* We have a problem here. The following code to optimize the table
5726 size requires an integer type with more the 32 bits. If
5727 BFD_HOST_U_64_BIT is set we know about such a type. */
5728 #ifdef BFD_HOST_U_64_BIT
5733 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5734 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5735 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5736 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5737 unsigned long int *counts
;
5739 unsigned int no_improvement_count
= 0;
5741 /* Possible optimization parameters: if we have NSYMS symbols we say
5742 that the hashing table must at least have NSYMS/4 and at most
5744 minsize
= nsyms
/ 4;
5747 best_size
= maxsize
= nsyms
* 2;
5752 if ((best_size
& 31) == 0)
5756 /* Create array where we count the collisions in. We must use bfd_malloc
5757 since the size could be large. */
5759 amt
*= sizeof (unsigned long int);
5760 counts
= (unsigned long int *) bfd_malloc (amt
);
5764 /* Compute the "optimal" size for the hash table. The criteria is a
5765 minimal chain length. The minor criteria is (of course) the size
5767 for (i
= minsize
; i
< maxsize
; ++i
)
5769 /* Walk through the array of hashcodes and count the collisions. */
5770 BFD_HOST_U_64_BIT max
;
5771 unsigned long int j
;
5772 unsigned long int fact
;
5774 if (gnu_hash
&& (i
& 31) == 0)
5777 memset (counts
, '\0', i
* sizeof (unsigned long int));
5779 /* Determine how often each hash bucket is used. */
5780 for (j
= 0; j
< nsyms
; ++j
)
5781 ++counts
[hashcodes
[j
] % i
];
5783 /* For the weight function we need some information about the
5784 pagesize on the target. This is information need not be 100%
5785 accurate. Since this information is not available (so far) we
5786 define it here to a reasonable default value. If it is crucial
5787 to have a better value some day simply define this value. */
5788 # ifndef BFD_TARGET_PAGESIZE
5789 # define BFD_TARGET_PAGESIZE (4096)
5792 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5794 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5797 /* Variant 1: optimize for short chains. We add the squares
5798 of all the chain lengths (which favors many small chain
5799 over a few long chains). */
5800 for (j
= 0; j
< i
; ++j
)
5801 max
+= counts
[j
] * counts
[j
];
5803 /* This adds penalties for the overall size of the table. */
5804 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5807 /* Variant 2: Optimize a lot more for small table. Here we
5808 also add squares of the size but we also add penalties for
5809 empty slots (the +1 term). */
5810 for (j
= 0; j
< i
; ++j
)
5811 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5813 /* The overall size of the table is considered, but not as
5814 strong as in variant 1, where it is squared. */
5815 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5819 /* Compare with current best results. */
5820 if (max
< best_chlen
)
5824 no_improvement_count
= 0;
5826 /* PR 11843: Avoid futile long searches for the best bucket size
5827 when there are a large number of symbols. */
5828 else if (++no_improvement_count
== 100)
5835 #endif /* defined (BFD_HOST_U_64_BIT) */
5837 /* This is the fallback solution if no 64bit type is available or if we
5838 are not supposed to spend much time on optimizations. We select the
5839 bucket count using a fixed set of numbers. */
5840 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5842 best_size
= elf_buckets
[i
];
5843 if (nsyms
< elf_buckets
[i
+ 1])
5846 if (gnu_hash
&& best_size
< 2)
5853 /* Size any SHT_GROUP section for ld -r. */
5856 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
5860 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
5861 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
5862 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
5867 /* Set a default stack segment size. The value in INFO wins. If it
5868 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5869 undefined it is initialized. */
5872 bfd_elf_stack_segment_size (bfd
*output_bfd
,
5873 struct bfd_link_info
*info
,
5874 const char *legacy_symbol
,
5875 bfd_vma default_size
)
5877 struct elf_link_hash_entry
*h
= NULL
;
5879 /* Look for legacy symbol. */
5881 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
5882 FALSE
, FALSE
, FALSE
);
5883 if (h
&& (h
->root
.type
== bfd_link_hash_defined
5884 || h
->root
.type
== bfd_link_hash_defweak
)
5886 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
5888 /* The symbol has no type if specified on the command line. */
5889 h
->type
= STT_OBJECT
;
5890 if (info
->stacksize
)
5891 /* xgettext:c-format */
5892 _bfd_error_handler (_("%B: stack size specified and %s set"),
5893 output_bfd
, legacy_symbol
);
5894 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
5895 /* xgettext:c-format */
5896 _bfd_error_handler (_("%B: %s not absolute"),
5897 output_bfd
, legacy_symbol
);
5899 info
->stacksize
= h
->root
.u
.def
.value
;
5902 if (!info
->stacksize
)
5903 /* If the user didn't set a size, or explicitly inhibit the
5904 size, set it now. */
5905 info
->stacksize
= default_size
;
5907 /* Provide the legacy symbol, if it is referenced. */
5908 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
5909 || h
->root
.type
== bfd_link_hash_undefweak
))
5911 struct bfd_link_hash_entry
*bh
= NULL
;
5913 if (!(_bfd_generic_link_add_one_symbol
5914 (info
, output_bfd
, legacy_symbol
,
5915 BSF_GLOBAL
, bfd_abs_section_ptr
,
5916 info
->stacksize
>= 0 ? info
->stacksize
: 0,
5917 NULL
, FALSE
, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
5920 h
= (struct elf_link_hash_entry
*) bh
;
5922 h
->type
= STT_OBJECT
;
5928 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5930 struct elf_gc_sweep_symbol_info
5932 struct bfd_link_info
*info
;
5933 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
5938 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
5941 && (((h
->root
.type
== bfd_link_hash_defined
5942 || h
->root
.type
== bfd_link_hash_defweak
)
5943 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
5944 && h
->root
.u
.def
.section
->gc_mark
))
5945 || h
->root
.type
== bfd_link_hash_undefined
5946 || h
->root
.type
== bfd_link_hash_undefweak
))
5948 struct elf_gc_sweep_symbol_info
*inf
;
5950 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
5951 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
5954 h
->ref_regular_nonweak
= 0;
5960 /* Set up the sizes and contents of the ELF dynamic sections. This is
5961 called by the ELF linker emulation before_allocation routine. We
5962 must set the sizes of the sections before the linker sets the
5963 addresses of the various sections. */
5966 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5969 const char *filter_shlib
,
5971 const char *depaudit
,
5972 const char * const *auxiliary_filters
,
5973 struct bfd_link_info
*info
,
5974 asection
**sinterpptr
)
5977 const struct elf_backend_data
*bed
;
5981 if (!is_elf_hash_table (info
->hash
))
5984 dynobj
= elf_hash_table (info
)->dynobj
;
5986 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
5988 struct bfd_elf_version_tree
*verdefs
;
5989 struct elf_info_failed asvinfo
;
5990 struct bfd_elf_version_tree
*t
;
5991 struct bfd_elf_version_expr
*d
;
5992 struct elf_info_failed eif
;
5993 bfd_boolean all_defined
;
6000 /* If we are supposed to export all symbols into the dynamic symbol
6001 table (this is not the normal case), then do so. */
6002 if (info
->export_dynamic
6003 || (bfd_link_executable (info
) && info
->dynamic
))
6005 elf_link_hash_traverse (elf_hash_table (info
),
6006 _bfd_elf_export_symbol
,
6014 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6016 if (soname_indx
== (size_t) -1
6017 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6021 soname_indx
= (size_t) -1;
6023 /* Make all global versions with definition. */
6024 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6025 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6026 if (!d
->symver
&& d
->literal
)
6028 const char *verstr
, *name
;
6029 size_t namelen
, verlen
, newlen
;
6030 char *newname
, *p
, leading_char
;
6031 struct elf_link_hash_entry
*newh
;
6033 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6035 namelen
= strlen (name
) + (leading_char
!= '\0');
6037 verlen
= strlen (verstr
);
6038 newlen
= namelen
+ verlen
+ 3;
6040 newname
= (char *) bfd_malloc (newlen
);
6041 if (newname
== NULL
)
6043 newname
[0] = leading_char
;
6044 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6046 /* Check the hidden versioned definition. */
6047 p
= newname
+ namelen
;
6049 memcpy (p
, verstr
, verlen
+ 1);
6050 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6051 newname
, FALSE
, FALSE
,
6054 || (newh
->root
.type
!= bfd_link_hash_defined
6055 && newh
->root
.type
!= bfd_link_hash_defweak
))
6057 /* Check the default versioned definition. */
6059 memcpy (p
, verstr
, verlen
+ 1);
6060 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6061 newname
, FALSE
, FALSE
,
6066 /* Mark this version if there is a definition and it is
6067 not defined in a shared object. */
6069 && !newh
->def_dynamic
6070 && (newh
->root
.type
== bfd_link_hash_defined
6071 || newh
->root
.type
== bfd_link_hash_defweak
))
6075 /* Attach all the symbols to their version information. */
6076 asvinfo
.info
= info
;
6077 asvinfo
.failed
= FALSE
;
6079 elf_link_hash_traverse (elf_hash_table (info
),
6080 _bfd_elf_link_assign_sym_version
,
6085 if (!info
->allow_undefined_version
)
6087 /* Check if all global versions have a definition. */
6089 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6090 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6091 if (d
->literal
&& !d
->symver
&& !d
->script
)
6094 (_("%s: undefined version: %s"),
6095 d
->pattern
, t
->name
);
6096 all_defined
= FALSE
;
6101 bfd_set_error (bfd_error_bad_value
);
6106 /* Set up the version definition section. */
6107 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6108 BFD_ASSERT (s
!= NULL
);
6110 /* We may have created additional version definitions if we are
6111 just linking a regular application. */
6112 verdefs
= info
->version_info
;
6114 /* Skip anonymous version tag. */
6115 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6116 verdefs
= verdefs
->next
;
6118 if (verdefs
== NULL
&& !info
->create_default_symver
)
6119 s
->flags
|= SEC_EXCLUDE
;
6125 Elf_Internal_Verdef def
;
6126 Elf_Internal_Verdaux defaux
;
6127 struct bfd_link_hash_entry
*bh
;
6128 struct elf_link_hash_entry
*h
;
6134 /* Make space for the base version. */
6135 size
+= sizeof (Elf_External_Verdef
);
6136 size
+= sizeof (Elf_External_Verdaux
);
6139 /* Make space for the default version. */
6140 if (info
->create_default_symver
)
6142 size
+= sizeof (Elf_External_Verdef
);
6146 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6148 struct bfd_elf_version_deps
*n
;
6150 /* Don't emit base version twice. */
6154 size
+= sizeof (Elf_External_Verdef
);
6155 size
+= sizeof (Elf_External_Verdaux
);
6158 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6159 size
+= sizeof (Elf_External_Verdaux
);
6163 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6164 if (s
->contents
== NULL
&& s
->size
!= 0)
6167 /* Fill in the version definition section. */
6171 def
.vd_version
= VER_DEF_CURRENT
;
6172 def
.vd_flags
= VER_FLG_BASE
;
6175 if (info
->create_default_symver
)
6177 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6178 def
.vd_next
= sizeof (Elf_External_Verdef
);
6182 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6183 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6184 + sizeof (Elf_External_Verdaux
));
6187 if (soname_indx
!= (size_t) -1)
6189 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6191 def
.vd_hash
= bfd_elf_hash (soname
);
6192 defaux
.vda_name
= soname_indx
;
6199 name
= lbasename (output_bfd
->filename
);
6200 def
.vd_hash
= bfd_elf_hash (name
);
6201 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6203 if (indx
== (size_t) -1)
6205 defaux
.vda_name
= indx
;
6207 defaux
.vda_next
= 0;
6209 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6210 (Elf_External_Verdef
*) p
);
6211 p
+= sizeof (Elf_External_Verdef
);
6212 if (info
->create_default_symver
)
6214 /* Add a symbol representing this version. */
6216 if (! (_bfd_generic_link_add_one_symbol
6217 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6219 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6221 h
= (struct elf_link_hash_entry
*) bh
;
6224 h
->type
= STT_OBJECT
;
6225 h
->verinfo
.vertree
= NULL
;
6227 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6230 /* Create a duplicate of the base version with the same
6231 aux block, but different flags. */
6234 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6236 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6237 + sizeof (Elf_External_Verdaux
));
6240 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6241 (Elf_External_Verdef
*) p
);
6242 p
+= sizeof (Elf_External_Verdef
);
6244 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6245 (Elf_External_Verdaux
*) p
);
6246 p
+= sizeof (Elf_External_Verdaux
);
6248 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6251 struct bfd_elf_version_deps
*n
;
6253 /* Don't emit the base version twice. */
6258 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6261 /* Add a symbol representing this version. */
6263 if (! (_bfd_generic_link_add_one_symbol
6264 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6266 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6268 h
= (struct elf_link_hash_entry
*) bh
;
6271 h
->type
= STT_OBJECT
;
6272 h
->verinfo
.vertree
= t
;
6274 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6277 def
.vd_version
= VER_DEF_CURRENT
;
6279 if (t
->globals
.list
== NULL
6280 && t
->locals
.list
== NULL
6282 def
.vd_flags
|= VER_FLG_WEAK
;
6283 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6284 def
.vd_cnt
= cdeps
+ 1;
6285 def
.vd_hash
= bfd_elf_hash (t
->name
);
6286 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6289 /* If a basever node is next, it *must* be the last node in
6290 the chain, otherwise Verdef construction breaks. */
6291 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6292 BFD_ASSERT (t
->next
->next
== NULL
);
6294 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6295 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6296 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6298 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6299 (Elf_External_Verdef
*) p
);
6300 p
+= sizeof (Elf_External_Verdef
);
6302 defaux
.vda_name
= h
->dynstr_index
;
6303 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6305 defaux
.vda_next
= 0;
6306 if (t
->deps
!= NULL
)
6307 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6308 t
->name_indx
= defaux
.vda_name
;
6310 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6311 (Elf_External_Verdaux
*) p
);
6312 p
+= sizeof (Elf_External_Verdaux
);
6314 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6316 if (n
->version_needed
== NULL
)
6318 /* This can happen if there was an error in the
6320 defaux
.vda_name
= 0;
6324 defaux
.vda_name
= n
->version_needed
->name_indx
;
6325 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6328 if (n
->next
== NULL
)
6329 defaux
.vda_next
= 0;
6331 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6333 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6334 (Elf_External_Verdaux
*) p
);
6335 p
+= sizeof (Elf_External_Verdaux
);
6339 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6342 /* Work out the size of the version reference section. */
6344 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6345 BFD_ASSERT (s
!= NULL
);
6347 struct elf_find_verdep_info sinfo
;
6350 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6351 if (sinfo
.vers
== 0)
6353 sinfo
.failed
= FALSE
;
6355 elf_link_hash_traverse (elf_hash_table (info
),
6356 _bfd_elf_link_find_version_dependencies
,
6361 if (elf_tdata (output_bfd
)->verref
== NULL
)
6362 s
->flags
|= SEC_EXCLUDE
;
6365 Elf_Internal_Verneed
*vn
;
6370 /* Build the version dependency section. */
6373 for (vn
= elf_tdata (output_bfd
)->verref
;
6375 vn
= vn
->vn_nextref
)
6377 Elf_Internal_Vernaux
*a
;
6379 size
+= sizeof (Elf_External_Verneed
);
6381 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6382 size
+= sizeof (Elf_External_Vernaux
);
6386 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6387 if (s
->contents
== NULL
)
6391 for (vn
= elf_tdata (output_bfd
)->verref
;
6393 vn
= vn
->vn_nextref
)
6396 Elf_Internal_Vernaux
*a
;
6400 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6403 vn
->vn_version
= VER_NEED_CURRENT
;
6405 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6406 elf_dt_name (vn
->vn_bfd
) != NULL
6407 ? elf_dt_name (vn
->vn_bfd
)
6408 : lbasename (vn
->vn_bfd
->filename
),
6410 if (indx
== (size_t) -1)
6413 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
6414 if (vn
->vn_nextref
== NULL
)
6417 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
6418 + caux
* sizeof (Elf_External_Vernaux
));
6420 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
6421 (Elf_External_Verneed
*) p
);
6422 p
+= sizeof (Elf_External_Verneed
);
6424 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6426 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6427 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6428 a
->vna_nodename
, FALSE
);
6429 if (indx
== (size_t) -1)
6432 if (a
->vna_nextptr
== NULL
)
6435 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6437 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6438 (Elf_External_Vernaux
*) p
);
6439 p
+= sizeof (Elf_External_Vernaux
);
6443 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6448 bed
= get_elf_backend_data (output_bfd
);
6450 if (info
->gc_sections
&& bed
->can_gc_sections
)
6452 struct elf_gc_sweep_symbol_info sweep_info
;
6453 unsigned long section_sym_count
;
6455 /* Remove the symbols that were in the swept sections from the
6456 dynamic symbol table. GCFIXME: Anyone know how to get them
6457 out of the static symbol table as well? */
6458 sweep_info
.info
= info
;
6459 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
6460 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
6463 _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, §ion_sym_count
);
6466 /* Any syms created from now on start with -1 in
6467 got.refcount/offset and plt.refcount/offset. */
6468 elf_hash_table (info
)->init_got_refcount
6469 = elf_hash_table (info
)->init_got_offset
;
6470 elf_hash_table (info
)->init_plt_refcount
6471 = elf_hash_table (info
)->init_plt_offset
;
6473 if (bfd_link_relocatable (info
)
6474 && !_bfd_elf_size_group_sections (info
))
6477 /* The backend may have to create some sections regardless of whether
6478 we're dynamic or not. */
6479 if (bed
->elf_backend_always_size_sections
6480 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
6483 /* Determine any GNU_STACK segment requirements, after the backend
6484 has had a chance to set a default segment size. */
6485 if (info
->execstack
)
6486 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
6487 else if (info
->noexecstack
)
6488 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
6492 asection
*notesec
= NULL
;
6495 for (inputobj
= info
->input_bfds
;
6497 inputobj
= inputobj
->link
.next
)
6502 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
6504 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
6507 if (s
->flags
& SEC_CODE
)
6511 else if (bed
->default_execstack
)
6514 if (notesec
|| info
->stacksize
> 0)
6515 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
6516 if (notesec
&& exec
&& bfd_link_relocatable (info
)
6517 && notesec
->output_section
!= bfd_abs_section_ptr
)
6518 notesec
->output_section
->flags
|= SEC_CODE
;
6521 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6523 struct elf_info_failed eif
;
6524 struct elf_link_hash_entry
*h
;
6528 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
6529 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
6533 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
6535 info
->flags
|= DF_SYMBOLIC
;
6543 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
6545 if (indx
== (size_t) -1)
6548 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
6549 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
6553 if (filter_shlib
!= NULL
)
6557 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6558 filter_shlib
, TRUE
);
6559 if (indx
== (size_t) -1
6560 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
6564 if (auxiliary_filters
!= NULL
)
6566 const char * const *p
;
6568 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
6572 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6574 if (indx
== (size_t) -1
6575 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
6584 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
6586 if (indx
== (size_t) -1
6587 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
6591 if (depaudit
!= NULL
)
6595 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
6597 if (indx
== (size_t) -1
6598 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
6605 /* Find all symbols which were defined in a dynamic object and make
6606 the backend pick a reasonable value for them. */
6607 elf_link_hash_traverse (elf_hash_table (info
),
6608 _bfd_elf_adjust_dynamic_symbol
,
6613 /* Add some entries to the .dynamic section. We fill in some of the
6614 values later, in bfd_elf_final_link, but we must add the entries
6615 now so that we know the final size of the .dynamic section. */
6617 /* If there are initialization and/or finalization functions to
6618 call then add the corresponding DT_INIT/DT_FINI entries. */
6619 h
= (info
->init_function
6620 ? elf_link_hash_lookup (elf_hash_table (info
),
6621 info
->init_function
, FALSE
,
6628 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
6631 h
= (info
->fini_function
6632 ? elf_link_hash_lookup (elf_hash_table (info
),
6633 info
->fini_function
, FALSE
,
6640 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
6644 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
6645 if (s
!= NULL
&& s
->linker_has_input
)
6647 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6648 if (! bfd_link_executable (info
))
6653 for (sub
= info
->input_bfds
; sub
!= NULL
;
6654 sub
= sub
->link
.next
)
6655 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
6656 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
6657 if (elf_section_data (o
)->this_hdr
.sh_type
6658 == SHT_PREINIT_ARRAY
)
6661 (_("%B: .preinit_array section is not allowed in DSO"),
6666 bfd_set_error (bfd_error_nonrepresentable_section
);
6670 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
6671 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
6674 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
6675 if (s
!= NULL
&& s
->linker_has_input
)
6677 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
6678 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
6681 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
6682 if (s
!= NULL
&& s
->linker_has_input
)
6684 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
6685 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
6689 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
6690 /* If .dynstr is excluded from the link, we don't want any of
6691 these tags. Strictly, we should be checking each section
6692 individually; This quick check covers for the case where
6693 someone does a /DISCARD/ : { *(*) }. */
6694 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
6696 bfd_size_type strsize
;
6698 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6699 if ((info
->emit_hash
6700 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
6701 || (info
->emit_gnu_hash
6702 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
6703 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
6704 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
6705 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
6706 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
6707 bed
->s
->sizeof_sym
))
6712 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
6715 /* The backend must work out the sizes of all the other dynamic
6718 && bed
->elf_backend_size_dynamic_sections
!= NULL
6719 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
6722 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6724 unsigned long section_sym_count
;
6726 if (elf_tdata (output_bfd
)->cverdefs
)
6728 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
6730 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
6731 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
6735 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
6737 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
6740 else if (info
->flags
& DF_BIND_NOW
)
6742 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
6748 if (bfd_link_executable (info
))
6749 info
->flags_1
&= ~ (DF_1_INITFIRST
6752 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
6756 if (elf_tdata (output_bfd
)->cverrefs
)
6758 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
6760 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
6761 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
6765 if ((elf_tdata (output_bfd
)->cverrefs
== 0
6766 && elf_tdata (output_bfd
)->cverdefs
== 0)
6767 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6768 §ion_sym_count
) == 0)
6772 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6773 s
->flags
|= SEC_EXCLUDE
;
6779 /* Find the first non-excluded output section. We'll use its
6780 section symbol for some emitted relocs. */
6782 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
6786 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6787 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
6788 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6790 elf_hash_table (info
)->text_index_section
= s
;
6795 /* Find two non-excluded output sections, one for code, one for data.
6796 We'll use their section symbols for some emitted relocs. */
6798 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6802 /* Data first, since setting text_index_section changes
6803 _bfd_elf_link_omit_section_dynsym. */
6804 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6805 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
6806 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6808 elf_hash_table (info
)->data_index_section
= s
;
6812 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6813 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
6814 == (SEC_ALLOC
| SEC_READONLY
))
6815 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6817 elf_hash_table (info
)->text_index_section
= s
;
6821 if (elf_hash_table (info
)->text_index_section
== NULL
)
6822 elf_hash_table (info
)->text_index_section
6823 = elf_hash_table (info
)->data_index_section
;
6827 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6829 const struct elf_backend_data
*bed
;
6831 if (!is_elf_hash_table (info
->hash
))
6834 bed
= get_elf_backend_data (output_bfd
);
6835 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6837 if (elf_hash_table (info
)->dynamic_sections_created
)
6841 bfd_size_type dynsymcount
;
6842 unsigned long section_sym_count
;
6843 unsigned int dtagcount
;
6845 dynobj
= elf_hash_table (info
)->dynobj
;
6847 /* Assign dynsym indicies. In a shared library we generate a
6848 section symbol for each output section, which come first.
6849 Next come all of the back-end allocated local dynamic syms,
6850 followed by the rest of the global symbols. */
6852 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6853 §ion_sym_count
);
6855 /* Work out the size of the symbol version section. */
6856 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6857 BFD_ASSERT (s
!= NULL
);
6858 if ((s
->flags
& SEC_EXCLUDE
) == 0)
6860 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6861 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6862 if (s
->contents
== NULL
)
6865 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
6869 /* Set the size of the .dynsym and .hash sections. We counted
6870 the number of dynamic symbols in elf_link_add_object_symbols.
6871 We will build the contents of .dynsym and .hash when we build
6872 the final symbol table, because until then we do not know the
6873 correct value to give the symbols. We built the .dynstr
6874 section as we went along in elf_link_add_object_symbols. */
6875 s
= elf_hash_table (info
)->dynsym
;
6876 BFD_ASSERT (s
!= NULL
);
6877 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
6879 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6880 if (s
->contents
== NULL
)
6883 /* The first entry in .dynsym is a dummy symbol. Clear all the
6884 section syms, in case we don't output them all. */
6885 ++section_sym_count
;
6886 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
6888 elf_hash_table (info
)->bucketcount
= 0;
6890 /* Compute the size of the hashing table. As a side effect this
6891 computes the hash values for all the names we export. */
6892 if (info
->emit_hash
)
6894 unsigned long int *hashcodes
;
6895 struct hash_codes_info hashinf
;
6897 unsigned long int nsyms
;
6899 size_t hash_entry_size
;
6901 /* Compute the hash values for all exported symbols. At the same
6902 time store the values in an array so that we could use them for
6904 amt
= dynsymcount
* sizeof (unsigned long int);
6905 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
6906 if (hashcodes
== NULL
)
6908 hashinf
.hashcodes
= hashcodes
;
6909 hashinf
.error
= FALSE
;
6911 /* Put all hash values in HASHCODES. */
6912 elf_link_hash_traverse (elf_hash_table (info
),
6913 elf_collect_hash_codes
, &hashinf
);
6920 nsyms
= hashinf
.hashcodes
- hashcodes
;
6922 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6925 if (bucketcount
== 0)
6928 elf_hash_table (info
)->bucketcount
= bucketcount
;
6930 s
= bfd_get_linker_section (dynobj
, ".hash");
6931 BFD_ASSERT (s
!= NULL
);
6932 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6933 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6934 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6935 if (s
->contents
== NULL
)
6938 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6939 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6940 s
->contents
+ hash_entry_size
);
6943 if (info
->emit_gnu_hash
)
6946 unsigned char *contents
;
6947 struct collect_gnu_hash_codes cinfo
;
6951 memset (&cinfo
, 0, sizeof (cinfo
));
6953 /* Compute the hash values for all exported symbols. At the same
6954 time store the values in an array so that we could use them for
6956 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6957 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
6958 if (cinfo
.hashcodes
== NULL
)
6961 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6962 cinfo
.min_dynindx
= -1;
6963 cinfo
.output_bfd
= output_bfd
;
6966 /* Put all hash values in HASHCODES. */
6967 elf_link_hash_traverse (elf_hash_table (info
),
6968 elf_collect_gnu_hash_codes
, &cinfo
);
6971 free (cinfo
.hashcodes
);
6976 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6978 if (bucketcount
== 0)
6980 free (cinfo
.hashcodes
);
6984 s
= bfd_get_linker_section (dynobj
, ".gnu.hash");
6985 BFD_ASSERT (s
!= NULL
);
6987 if (cinfo
.nsyms
== 0)
6989 /* Empty .gnu.hash section is special. */
6990 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6991 free (cinfo
.hashcodes
);
6992 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6993 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6994 if (contents
== NULL
)
6996 s
->contents
= contents
;
6997 /* 1 empty bucket. */
6998 bfd_put_32 (output_bfd
, 1, contents
);
6999 /* SYMIDX above the special symbol 0. */
7000 bfd_put_32 (output_bfd
, 1, contents
+ 4);
7001 /* Just one word for bitmask. */
7002 bfd_put_32 (output_bfd
, 1, contents
+ 8);
7003 /* Only hash fn bloom filter. */
7004 bfd_put_32 (output_bfd
, 0, contents
+ 12);
7005 /* No hashes are valid - empty bitmask. */
7006 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
7007 /* No hashes in the only bucket. */
7008 bfd_put_32 (output_bfd
, 0,
7009 contents
+ 16 + bed
->s
->arch_size
/ 8);
7013 unsigned long int maskwords
, maskbitslog2
, x
;
7014 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
7018 while ((x
>>= 1) != 0)
7020 if (maskbitslog2
< 3)
7022 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
7023 maskbitslog2
= maskbitslog2
+ 3;
7025 maskbitslog2
= maskbitslog2
+ 2;
7026 if (bed
->s
->arch_size
== 64)
7028 if (maskbitslog2
== 5)
7034 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
7035 cinfo
.shift2
= maskbitslog2
;
7036 cinfo
.maskbits
= 1 << maskbitslog2
;
7037 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
7038 amt
= bucketcount
* sizeof (unsigned long int) * 2;
7039 amt
+= maskwords
* sizeof (bfd_vma
);
7040 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
7041 if (cinfo
.bitmask
== NULL
)
7043 free (cinfo
.hashcodes
);
7047 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
7048 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
7049 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
7050 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
7052 /* Determine how often each hash bucket is used. */
7053 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
7054 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
7055 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
7057 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
7058 if (cinfo
.counts
[i
] != 0)
7060 cinfo
.indx
[i
] = cnt
;
7061 cnt
+= cinfo
.counts
[i
];
7063 BFD_ASSERT (cnt
== dynsymcount
);
7064 cinfo
.bucketcount
= bucketcount
;
7065 cinfo
.local_indx
= cinfo
.min_dynindx
;
7067 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
7068 s
->size
+= cinfo
.maskbits
/ 8;
7069 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7070 if (contents
== NULL
)
7072 free (cinfo
.bitmask
);
7073 free (cinfo
.hashcodes
);
7077 s
->contents
= contents
;
7078 bfd_put_32 (output_bfd
, bucketcount
, contents
);
7079 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
7080 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
7081 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
7082 contents
+= 16 + cinfo
.maskbits
/ 8;
7084 for (i
= 0; i
< bucketcount
; ++i
)
7086 if (cinfo
.counts
[i
] == 0)
7087 bfd_put_32 (output_bfd
, 0, contents
);
7089 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
7093 cinfo
.contents
= contents
;
7095 /* Renumber dynamic symbols, populate .gnu.hash section. */
7096 elf_link_hash_traverse (elf_hash_table (info
),
7097 elf_renumber_gnu_hash_syms
, &cinfo
);
7099 contents
= s
->contents
+ 16;
7100 for (i
= 0; i
< maskwords
; ++i
)
7102 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
7104 contents
+= bed
->s
->arch_size
/ 8;
7107 free (cinfo
.bitmask
);
7108 free (cinfo
.hashcodes
);
7112 s
= bfd_get_linker_section (dynobj
, ".dynstr");
7113 BFD_ASSERT (s
!= NULL
);
7115 elf_finalize_dynstr (output_bfd
, info
);
7117 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7119 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
7120 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
7127 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7130 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
7133 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
7134 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
7137 /* Finish SHF_MERGE section merging. */
7140 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
7145 if (!is_elf_hash_table (info
->hash
))
7148 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
7149 if ((ibfd
->flags
& DYNAMIC
) == 0
7150 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
7151 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
7152 == get_elf_backend_data (obfd
)->s
->elfclass
))
7153 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7154 if ((sec
->flags
& SEC_MERGE
) != 0
7155 && !bfd_is_abs_section (sec
->output_section
))
7157 struct bfd_elf_section_data
*secdata
;
7159 secdata
= elf_section_data (sec
);
7160 if (! _bfd_add_merge_section (obfd
,
7161 &elf_hash_table (info
)->merge_info
,
7162 sec
, &secdata
->sec_info
))
7164 else if (secdata
->sec_info
)
7165 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7168 if (elf_hash_table (info
)->merge_info
!= NULL
)
7169 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7170 merge_sections_remove_hook
);
7174 /* Create an entry in an ELF linker hash table. */
7176 struct bfd_hash_entry
*
7177 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7178 struct bfd_hash_table
*table
,
7181 /* Allocate the structure if it has not already been allocated by a
7185 entry
= (struct bfd_hash_entry
*)
7186 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7191 /* Call the allocation method of the superclass. */
7192 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7195 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7196 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7198 /* Set local fields. */
7201 ret
->got
= htab
->init_got_refcount
;
7202 ret
->plt
= htab
->init_plt_refcount
;
7203 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7204 - offsetof (struct elf_link_hash_entry
, size
)));
7205 /* Assume that we have been called by a non-ELF symbol reader.
7206 This flag is then reset by the code which reads an ELF input
7207 file. This ensures that a symbol created by a non-ELF symbol
7208 reader will have the flag set correctly. */
7215 /* Copy data from an indirect symbol to its direct symbol, hiding the
7216 old indirect symbol. Also used for copying flags to a weakdef. */
7219 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7220 struct elf_link_hash_entry
*dir
,
7221 struct elf_link_hash_entry
*ind
)
7223 struct elf_link_hash_table
*htab
;
7225 /* Copy down any references that we may have already seen to the
7226 symbol which just became indirect. */
7228 if (dir
->versioned
!= versioned_hidden
)
7229 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7230 dir
->ref_regular
|= ind
->ref_regular
;
7231 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7232 dir
->non_got_ref
|= ind
->non_got_ref
;
7233 dir
->needs_plt
|= ind
->needs_plt
;
7234 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7236 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7239 /* Copy over the global and procedure linkage table refcount entries.
7240 These may have been already set up by a check_relocs routine. */
7241 htab
= elf_hash_table (info
);
7242 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7244 if (dir
->got
.refcount
< 0)
7245 dir
->got
.refcount
= 0;
7246 dir
->got
.refcount
+= ind
->got
.refcount
;
7247 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7250 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7252 if (dir
->plt
.refcount
< 0)
7253 dir
->plt
.refcount
= 0;
7254 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7255 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7258 if (ind
->dynindx
!= -1)
7260 if (dir
->dynindx
!= -1)
7261 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7262 dir
->dynindx
= ind
->dynindx
;
7263 dir
->dynstr_index
= ind
->dynstr_index
;
7265 ind
->dynstr_index
= 0;
7270 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7271 struct elf_link_hash_entry
*h
,
7272 bfd_boolean force_local
)
7274 /* STT_GNU_IFUNC symbol must go through PLT. */
7275 if (h
->type
!= STT_GNU_IFUNC
)
7277 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7282 h
->forced_local
= 1;
7283 if (h
->dynindx
!= -1)
7286 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7292 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7296 _bfd_elf_link_hash_table_init
7297 (struct elf_link_hash_table
*table
,
7299 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
7300 struct bfd_hash_table
*,
7302 unsigned int entsize
,
7303 enum elf_target_id target_id
)
7306 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
7308 table
->init_got_refcount
.refcount
= can_refcount
- 1;
7309 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
7310 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
7311 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
7312 /* The first dynamic symbol is a dummy. */
7313 table
->dynsymcount
= 1;
7315 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
7317 table
->root
.type
= bfd_link_elf_hash_table
;
7318 table
->hash_table_id
= target_id
;
7323 /* Create an ELF linker hash table. */
7325 struct bfd_link_hash_table
*
7326 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7328 struct elf_link_hash_table
*ret
;
7329 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
7331 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7335 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7336 sizeof (struct elf_link_hash_entry
),
7342 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7347 /* Destroy an ELF linker hash table. */
7350 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7352 struct elf_link_hash_table
*htab
;
7354 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7355 if (htab
->dynstr
!= NULL
)
7356 _bfd_elf_strtab_free (htab
->dynstr
);
7357 _bfd_merge_sections_free (htab
->merge_info
);
7358 _bfd_generic_link_hash_table_free (obfd
);
7361 /* This is a hook for the ELF emulation code in the generic linker to
7362 tell the backend linker what file name to use for the DT_NEEDED
7363 entry for a dynamic object. */
7366 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7368 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7369 && bfd_get_format (abfd
) == bfd_object
)
7370 elf_dt_name (abfd
) = name
;
7374 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
7377 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7378 && bfd_get_format (abfd
) == bfd_object
)
7379 lib_class
= elf_dyn_lib_class (abfd
);
7386 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
7388 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7389 && bfd_get_format (abfd
) == bfd_object
)
7390 elf_dyn_lib_class (abfd
) = lib_class
;
7393 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7394 the linker ELF emulation code. */
7396 struct bfd_link_needed_list
*
7397 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7398 struct bfd_link_info
*info
)
7400 if (! is_elf_hash_table (info
->hash
))
7402 return elf_hash_table (info
)->needed
;
7405 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7406 hook for the linker ELF emulation code. */
7408 struct bfd_link_needed_list
*
7409 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7410 struct bfd_link_info
*info
)
7412 if (! is_elf_hash_table (info
->hash
))
7414 return elf_hash_table (info
)->runpath
;
7417 /* Get the name actually used for a dynamic object for a link. This
7418 is the SONAME entry if there is one. Otherwise, it is the string
7419 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7422 bfd_elf_get_dt_soname (bfd
*abfd
)
7424 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7425 && bfd_get_format (abfd
) == bfd_object
)
7426 return elf_dt_name (abfd
);
7430 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7431 the ELF linker emulation code. */
7434 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
7435 struct bfd_link_needed_list
**pneeded
)
7438 bfd_byte
*dynbuf
= NULL
;
7439 unsigned int elfsec
;
7440 unsigned long shlink
;
7441 bfd_byte
*extdyn
, *extdynend
;
7443 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
7447 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
7448 || bfd_get_format (abfd
) != bfd_object
)
7451 s
= bfd_get_section_by_name (abfd
, ".dynamic");
7452 if (s
== NULL
|| s
->size
== 0)
7455 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
7458 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
7459 if (elfsec
== SHN_BAD
)
7462 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
7464 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
7465 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
7468 extdynend
= extdyn
+ s
->size
;
7469 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
7471 Elf_Internal_Dyn dyn
;
7473 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
7475 if (dyn
.d_tag
== DT_NULL
)
7478 if (dyn
.d_tag
== DT_NEEDED
)
7481 struct bfd_link_needed_list
*l
;
7482 unsigned int tagv
= dyn
.d_un
.d_val
;
7485 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
7490 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
7511 struct elf_symbuf_symbol
7513 unsigned long st_name
; /* Symbol name, index in string tbl */
7514 unsigned char st_info
; /* Type and binding attributes */
7515 unsigned char st_other
; /* Visibilty, and target specific */
7518 struct elf_symbuf_head
7520 struct elf_symbuf_symbol
*ssym
;
7522 unsigned int st_shndx
;
7529 Elf_Internal_Sym
*isym
;
7530 struct elf_symbuf_symbol
*ssym
;
7535 /* Sort references to symbols by ascending section number. */
7538 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
7540 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
7541 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
7543 return s1
->st_shndx
- s2
->st_shndx
;
7547 elf_sym_name_compare (const void *arg1
, const void *arg2
)
7549 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
7550 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
7551 return strcmp (s1
->name
, s2
->name
);
7554 static struct elf_symbuf_head
*
7555 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
7557 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
7558 struct elf_symbuf_symbol
*ssym
;
7559 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
7560 size_t i
, shndx_count
, total_size
;
7562 indbuf
= (Elf_Internal_Sym
**) bfd_malloc2 (symcount
, sizeof (*indbuf
));
7566 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
7567 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
7568 *ind
++ = &isymbuf
[i
];
7571 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
7572 elf_sort_elf_symbol
);
7575 if (indbufend
> indbuf
)
7576 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
7577 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
7580 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
7581 + (indbufend
- indbuf
) * sizeof (*ssym
));
7582 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
7583 if (ssymbuf
== NULL
)
7589 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
7590 ssymbuf
->ssym
= NULL
;
7591 ssymbuf
->count
= shndx_count
;
7592 ssymbuf
->st_shndx
= 0;
7593 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
7595 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
7598 ssymhead
->ssym
= ssym
;
7599 ssymhead
->count
= 0;
7600 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
7602 ssym
->st_name
= (*ind
)->st_name
;
7603 ssym
->st_info
= (*ind
)->st_info
;
7604 ssym
->st_other
= (*ind
)->st_other
;
7607 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
7608 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
7615 /* Check if 2 sections define the same set of local and global
7619 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
7620 struct bfd_link_info
*info
)
7623 const struct elf_backend_data
*bed1
, *bed2
;
7624 Elf_Internal_Shdr
*hdr1
, *hdr2
;
7625 size_t symcount1
, symcount2
;
7626 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
7627 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
7628 Elf_Internal_Sym
*isym
, *isymend
;
7629 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
7630 size_t count1
, count2
, i
;
7631 unsigned int shndx1
, shndx2
;
7637 /* Both sections have to be in ELF. */
7638 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
7639 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
7642 if (elf_section_type (sec1
) != elf_section_type (sec2
))
7645 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
7646 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
7647 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
7650 bed1
= get_elf_backend_data (bfd1
);
7651 bed2
= get_elf_backend_data (bfd2
);
7652 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
7653 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
7654 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
7655 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
7657 if (symcount1
== 0 || symcount2
== 0)
7663 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
7664 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
7666 if (ssymbuf1
== NULL
)
7668 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
7670 if (isymbuf1
== NULL
)
7673 if (!info
->reduce_memory_overheads
)
7674 elf_tdata (bfd1
)->symbuf
= ssymbuf1
7675 = elf_create_symbuf (symcount1
, isymbuf1
);
7678 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
7680 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
7682 if (isymbuf2
== NULL
)
7685 if (ssymbuf1
!= NULL
&& !info
->reduce_memory_overheads
)
7686 elf_tdata (bfd2
)->symbuf
= ssymbuf2
7687 = elf_create_symbuf (symcount2
, isymbuf2
);
7690 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
7692 /* Optimized faster version. */
7694 struct elf_symbol
*symp
;
7695 struct elf_symbuf_symbol
*ssym
, *ssymend
;
7698 hi
= ssymbuf1
->count
;
7703 mid
= (lo
+ hi
) / 2;
7704 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
7706 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
7710 count1
= ssymbuf1
[mid
].count
;
7717 hi
= ssymbuf2
->count
;
7722 mid
= (lo
+ hi
) / 2;
7723 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
7725 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
7729 count2
= ssymbuf2
[mid
].count
;
7735 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7739 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
7741 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
7742 if (symtable1
== NULL
|| symtable2
== NULL
)
7746 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
7747 ssym
< ssymend
; ssym
++, symp
++)
7749 symp
->u
.ssym
= ssym
;
7750 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
7756 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
7757 ssym
< ssymend
; ssym
++, symp
++)
7759 symp
->u
.ssym
= ssym
;
7760 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
7765 /* Sort symbol by name. */
7766 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7767 elf_sym_name_compare
);
7768 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7769 elf_sym_name_compare
);
7771 for (i
= 0; i
< count1
; i
++)
7772 /* Two symbols must have the same binding, type and name. */
7773 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
7774 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
7775 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7782 symtable1
= (struct elf_symbol
*)
7783 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
7784 symtable2
= (struct elf_symbol
*)
7785 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
7786 if (symtable1
== NULL
|| symtable2
== NULL
)
7789 /* Count definitions in the section. */
7791 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
7792 if (isym
->st_shndx
== shndx1
)
7793 symtable1
[count1
++].u
.isym
= isym
;
7796 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
7797 if (isym
->st_shndx
== shndx2
)
7798 symtable2
[count2
++].u
.isym
= isym
;
7800 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7803 for (i
= 0; i
< count1
; i
++)
7805 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
7806 symtable1
[i
].u
.isym
->st_name
);
7808 for (i
= 0; i
< count2
; i
++)
7810 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
7811 symtable2
[i
].u
.isym
->st_name
);
7813 /* Sort symbol by name. */
7814 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7815 elf_sym_name_compare
);
7816 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7817 elf_sym_name_compare
);
7819 for (i
= 0; i
< count1
; i
++)
7820 /* Two symbols must have the same binding, type and name. */
7821 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
7822 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
7823 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7841 /* Return TRUE if 2 section types are compatible. */
7844 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
7845 bfd
*bbfd
, const asection
*bsec
)
7849 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
7850 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7853 return elf_section_type (asec
) == elf_section_type (bsec
);
7856 /* Final phase of ELF linker. */
7858 /* A structure we use to avoid passing large numbers of arguments. */
7860 struct elf_final_link_info
7862 /* General link information. */
7863 struct bfd_link_info
*info
;
7866 /* Symbol string table. */
7867 struct elf_strtab_hash
*symstrtab
;
7868 /* .hash section. */
7870 /* symbol version section (.gnu.version). */
7871 asection
*symver_sec
;
7872 /* Buffer large enough to hold contents of any section. */
7874 /* Buffer large enough to hold external relocs of any section. */
7875 void *external_relocs
;
7876 /* Buffer large enough to hold internal relocs of any section. */
7877 Elf_Internal_Rela
*internal_relocs
;
7878 /* Buffer large enough to hold external local symbols of any input
7880 bfd_byte
*external_syms
;
7881 /* And a buffer for symbol section indices. */
7882 Elf_External_Sym_Shndx
*locsym_shndx
;
7883 /* Buffer large enough to hold internal local symbols of any input
7885 Elf_Internal_Sym
*internal_syms
;
7886 /* Array large enough to hold a symbol index for each local symbol
7887 of any input BFD. */
7889 /* Array large enough to hold a section pointer for each local
7890 symbol of any input BFD. */
7891 asection
**sections
;
7892 /* Buffer for SHT_SYMTAB_SHNDX section. */
7893 Elf_External_Sym_Shndx
*symshndxbuf
;
7894 /* Number of STT_FILE syms seen. */
7895 size_t filesym_count
;
7898 /* This struct is used to pass information to elf_link_output_extsym. */
7900 struct elf_outext_info
7903 bfd_boolean localsyms
;
7904 bfd_boolean file_sym_done
;
7905 struct elf_final_link_info
*flinfo
;
7909 /* Support for evaluating a complex relocation.
7911 Complex relocations are generalized, self-describing relocations. The
7912 implementation of them consists of two parts: complex symbols, and the
7913 relocations themselves.
7915 The relocations are use a reserved elf-wide relocation type code (R_RELC
7916 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7917 information (start bit, end bit, word width, etc) into the addend. This
7918 information is extracted from CGEN-generated operand tables within gas.
7920 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7921 internal) representing prefix-notation expressions, including but not
7922 limited to those sorts of expressions normally encoded as addends in the
7923 addend field. The symbol mangling format is:
7926 | <unary-operator> ':' <node>
7927 | <binary-operator> ':' <node> ':' <node>
7930 <literal> := 's' <digits=N> ':' <N character symbol name>
7931 | 'S' <digits=N> ':' <N character section name>
7935 <binary-operator> := as in C
7936 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7939 set_symbol_value (bfd
*bfd_with_globals
,
7940 Elf_Internal_Sym
*isymbuf
,
7945 struct elf_link_hash_entry
**sym_hashes
;
7946 struct elf_link_hash_entry
*h
;
7947 size_t extsymoff
= locsymcount
;
7949 if (symidx
< locsymcount
)
7951 Elf_Internal_Sym
*sym
;
7953 sym
= isymbuf
+ symidx
;
7954 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
7956 /* It is a local symbol: move it to the
7957 "absolute" section and give it a value. */
7958 sym
->st_shndx
= SHN_ABS
;
7959 sym
->st_value
= val
;
7962 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
7966 /* It is a global symbol: set its link type
7967 to "defined" and give it a value. */
7969 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
7970 h
= sym_hashes
[symidx
- extsymoff
];
7971 while (h
->root
.type
== bfd_link_hash_indirect
7972 || h
->root
.type
== bfd_link_hash_warning
)
7973 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7974 h
->root
.type
= bfd_link_hash_defined
;
7975 h
->root
.u
.def
.value
= val
;
7976 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
7980 resolve_symbol (const char *name
,
7982 struct elf_final_link_info
*flinfo
,
7984 Elf_Internal_Sym
*isymbuf
,
7987 Elf_Internal_Sym
*sym
;
7988 struct bfd_link_hash_entry
*global_entry
;
7989 const char *candidate
= NULL
;
7990 Elf_Internal_Shdr
*symtab_hdr
;
7993 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
7995 for (i
= 0; i
< locsymcount
; ++ i
)
7999 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
8002 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
8003 symtab_hdr
->sh_link
,
8006 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8007 name
, candidate
, (unsigned long) sym
->st_value
);
8009 if (candidate
&& strcmp (candidate
, name
) == 0)
8011 asection
*sec
= flinfo
->sections
[i
];
8013 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
8014 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
8016 printf ("Found symbol with value %8.8lx\n",
8017 (unsigned long) *result
);
8023 /* Hmm, haven't found it yet. perhaps it is a global. */
8024 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
8025 FALSE
, FALSE
, TRUE
);
8029 if (global_entry
->type
== bfd_link_hash_defined
8030 || global_entry
->type
== bfd_link_hash_defweak
)
8032 *result
= (global_entry
->u
.def
.value
8033 + global_entry
->u
.def
.section
->output_section
->vma
8034 + global_entry
->u
.def
.section
->output_offset
);
8036 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8037 global_entry
->root
.string
, (unsigned long) *result
);
8045 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8046 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8047 names like "foo.end" which is the end address of section "foo". */
8050 resolve_section (const char *name
,
8058 for (curr
= sections
; curr
; curr
= curr
->next
)
8059 if (strcmp (curr
->name
, name
) == 0)
8061 *result
= curr
->vma
;
8065 /* Hmm. still haven't found it. try pseudo-section names. */
8066 /* FIXME: This could be coded more efficiently... */
8067 for (curr
= sections
; curr
; curr
= curr
->next
)
8069 len
= strlen (curr
->name
);
8070 if (len
> strlen (name
))
8073 if (strncmp (curr
->name
, name
, len
) == 0)
8075 if (strncmp (".end", name
+ len
, 4) == 0)
8077 *result
= curr
->vma
+ curr
->size
/ bfd_octets_per_byte (abfd
);
8081 /* Insert more pseudo-section names here, if you like. */
8089 undefined_reference (const char *reftype
, const char *name
)
8091 /* xgettext:c-format */
8092 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8097 eval_symbol (bfd_vma
*result
,
8100 struct elf_final_link_info
*flinfo
,
8102 Elf_Internal_Sym
*isymbuf
,
8111 const char *sym
= *symp
;
8113 bfd_boolean symbol_is_section
= FALSE
;
8118 if (len
< 1 || len
> sizeof (symbuf
))
8120 bfd_set_error (bfd_error_invalid_operation
);
8133 *result
= strtoul (sym
, (char **) symp
, 16);
8137 symbol_is_section
= TRUE
;
8141 symlen
= strtol (sym
, (char **) symp
, 10);
8142 sym
= *symp
+ 1; /* Skip the trailing ':'. */
8144 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
8146 bfd_set_error (bfd_error_invalid_operation
);
8150 memcpy (symbuf
, sym
, symlen
);
8151 symbuf
[symlen
] = '\0';
8152 *symp
= sym
+ symlen
;
8154 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8155 the symbol as a section, or vice-versa. so we're pretty liberal in our
8156 interpretation here; section means "try section first", not "must be a
8157 section", and likewise with symbol. */
8159 if (symbol_is_section
)
8161 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
8162 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8163 isymbuf
, locsymcount
))
8165 undefined_reference ("section", symbuf
);
8171 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8172 isymbuf
, locsymcount
)
8173 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8176 undefined_reference ("symbol", symbuf
);
8183 /* All that remains are operators. */
8185 #define UNARY_OP(op) \
8186 if (strncmp (sym, #op, strlen (#op)) == 0) \
8188 sym += strlen (#op); \
8192 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8193 isymbuf, locsymcount, signed_p)) \
8196 *result = op ((bfd_signed_vma) a); \
8202 #define BINARY_OP(op) \
8203 if (strncmp (sym, #op, strlen (#op)) == 0) \
8205 sym += strlen (#op); \
8209 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8210 isymbuf, locsymcount, signed_p)) \
8213 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8214 isymbuf, locsymcount, signed_p)) \
8217 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8247 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
8248 bfd_set_error (bfd_error_invalid_operation
);
8254 put_value (bfd_vma size
,
8255 unsigned long chunksz
,
8260 location
+= (size
- chunksz
);
8262 for (; size
; size
-= chunksz
, location
-= chunksz
)
8267 bfd_put_8 (input_bfd
, x
, location
);
8271 bfd_put_16 (input_bfd
, x
, location
);
8275 bfd_put_32 (input_bfd
, x
, location
);
8276 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8282 bfd_put_64 (input_bfd
, x
, location
);
8283 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8296 get_value (bfd_vma size
,
8297 unsigned long chunksz
,
8304 /* Sanity checks. */
8305 BFD_ASSERT (chunksz
<= sizeof (x
)
8308 && (size
% chunksz
) == 0
8309 && input_bfd
!= NULL
8310 && location
!= NULL
);
8312 if (chunksz
== sizeof (x
))
8314 BFD_ASSERT (size
== chunksz
);
8316 /* Make sure that we do not perform an undefined shift operation.
8317 We know that size == chunksz so there will only be one iteration
8318 of the loop below. */
8322 shift
= 8 * chunksz
;
8324 for (; size
; size
-= chunksz
, location
+= chunksz
)
8329 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
8332 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
8335 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
8339 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
8350 decode_complex_addend (unsigned long *start
, /* in bits */
8351 unsigned long *oplen
, /* in bits */
8352 unsigned long *len
, /* in bits */
8353 unsigned long *wordsz
, /* in bytes */
8354 unsigned long *chunksz
, /* in bytes */
8355 unsigned long *lsb0_p
,
8356 unsigned long *signed_p
,
8357 unsigned long *trunc_p
,
8358 unsigned long encoded
)
8360 * start
= encoded
& 0x3F;
8361 * len
= (encoded
>> 6) & 0x3F;
8362 * oplen
= (encoded
>> 12) & 0x3F;
8363 * wordsz
= (encoded
>> 18) & 0xF;
8364 * chunksz
= (encoded
>> 22) & 0xF;
8365 * lsb0_p
= (encoded
>> 27) & 1;
8366 * signed_p
= (encoded
>> 28) & 1;
8367 * trunc_p
= (encoded
>> 29) & 1;
8370 bfd_reloc_status_type
8371 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
8372 asection
*input_section ATTRIBUTE_UNUSED
,
8374 Elf_Internal_Rela
*rel
,
8377 bfd_vma shift
, x
, mask
;
8378 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
8379 bfd_reloc_status_type r
;
8381 /* Perform this reloc, since it is complex.
8382 (this is not to say that it necessarily refers to a complex
8383 symbol; merely that it is a self-describing CGEN based reloc.
8384 i.e. the addend has the complete reloc information (bit start, end,
8385 word size, etc) encoded within it.). */
8387 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
8388 &chunksz
, &lsb0_p
, &signed_p
,
8389 &trunc_p
, rel
->r_addend
);
8391 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
8394 shift
= (start
+ 1) - len
;
8396 shift
= (8 * wordsz
) - (start
+ len
);
8398 x
= get_value (wordsz
, chunksz
, input_bfd
,
8399 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8402 printf ("Doing complex reloc: "
8403 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8404 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8405 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8406 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
8407 oplen
, (unsigned long) x
, (unsigned long) mask
,
8408 (unsigned long) relocation
);
8413 /* Now do an overflow check. */
8414 r
= bfd_check_overflow ((signed_p
8415 ? complain_overflow_signed
8416 : complain_overflow_unsigned
),
8417 len
, 0, (8 * wordsz
),
8421 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
8424 printf (" relocation: %8.8lx\n"
8425 " shifted mask: %8.8lx\n"
8426 " shifted/masked reloc: %8.8lx\n"
8427 " result: %8.8lx\n",
8428 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
8429 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
8431 put_value (wordsz
, chunksz
, input_bfd
, x
,
8432 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8436 /* Functions to read r_offset from external (target order) reloc
8437 entry. Faster than bfd_getl32 et al, because we let the compiler
8438 know the value is aligned. */
8441 ext32l_r_offset (const void *p
)
8448 const union aligned32
*a
8449 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8451 uint32_t aval
= ( (uint32_t) a
->c
[0]
8452 | (uint32_t) a
->c
[1] << 8
8453 | (uint32_t) a
->c
[2] << 16
8454 | (uint32_t) a
->c
[3] << 24);
8459 ext32b_r_offset (const void *p
)
8466 const union aligned32
*a
8467 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8469 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
8470 | (uint32_t) a
->c
[1] << 16
8471 | (uint32_t) a
->c
[2] << 8
8472 | (uint32_t) a
->c
[3]);
8476 #ifdef BFD_HOST_64_BIT
8478 ext64l_r_offset (const void *p
)
8485 const union aligned64
*a
8486 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8488 uint64_t aval
= ( (uint64_t) a
->c
[0]
8489 | (uint64_t) a
->c
[1] << 8
8490 | (uint64_t) a
->c
[2] << 16
8491 | (uint64_t) a
->c
[3] << 24
8492 | (uint64_t) a
->c
[4] << 32
8493 | (uint64_t) a
->c
[5] << 40
8494 | (uint64_t) a
->c
[6] << 48
8495 | (uint64_t) a
->c
[7] << 56);
8500 ext64b_r_offset (const void *p
)
8507 const union aligned64
*a
8508 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8510 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
8511 | (uint64_t) a
->c
[1] << 48
8512 | (uint64_t) a
->c
[2] << 40
8513 | (uint64_t) a
->c
[3] << 32
8514 | (uint64_t) a
->c
[4] << 24
8515 | (uint64_t) a
->c
[5] << 16
8516 | (uint64_t) a
->c
[6] << 8
8517 | (uint64_t) a
->c
[7]);
8522 /* When performing a relocatable link, the input relocations are
8523 preserved. But, if they reference global symbols, the indices
8524 referenced must be updated. Update all the relocations found in
8528 elf_link_adjust_relocs (bfd
*abfd
,
8530 struct bfd_elf_section_reloc_data
*reldata
,
8534 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8536 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8537 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8538 bfd_vma r_type_mask
;
8540 unsigned int count
= reldata
->count
;
8541 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
8543 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
8545 swap_in
= bed
->s
->swap_reloc_in
;
8546 swap_out
= bed
->s
->swap_reloc_out
;
8548 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
8550 swap_in
= bed
->s
->swap_reloca_in
;
8551 swap_out
= bed
->s
->swap_reloca_out
;
8556 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
8559 if (bed
->s
->arch_size
== 32)
8566 r_type_mask
= 0xffffffff;
8570 erela
= reldata
->hdr
->contents
;
8571 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
8573 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
8576 if (*rel_hash
== NULL
)
8579 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
8581 (*swap_in
) (abfd
, erela
, irela
);
8582 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
8583 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
8584 | (irela
[j
].r_info
& r_type_mask
));
8585 (*swap_out
) (abfd
, irela
, erela
);
8588 if (bed
->elf_backend_update_relocs
)
8589 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
8591 if (sort
&& count
!= 0)
8593 bfd_vma (*ext_r_off
) (const void *);
8596 bfd_byte
*base
, *end
, *p
, *loc
;
8597 bfd_byte
*buf
= NULL
;
8599 if (bed
->s
->arch_size
== 32)
8601 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8602 ext_r_off
= ext32l_r_offset
;
8603 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8604 ext_r_off
= ext32b_r_offset
;
8610 #ifdef BFD_HOST_64_BIT
8611 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8612 ext_r_off
= ext64l_r_offset
;
8613 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8614 ext_r_off
= ext64b_r_offset
;
8620 /* Must use a stable sort here. A modified insertion sort,
8621 since the relocs are mostly sorted already. */
8622 elt_size
= reldata
->hdr
->sh_entsize
;
8623 base
= reldata
->hdr
->contents
;
8624 end
= base
+ count
* elt_size
;
8625 if (elt_size
> sizeof (Elf64_External_Rela
))
8628 /* Ensure the first element is lowest. This acts as a sentinel,
8629 speeding the main loop below. */
8630 r_off
= (*ext_r_off
) (base
);
8631 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
8633 bfd_vma r_off2
= (*ext_r_off
) (p
);
8642 /* Don't just swap *base and *loc as that changes the order
8643 of the original base[0] and base[1] if they happen to
8644 have the same r_offset. */
8645 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
8646 memcpy (onebuf
, loc
, elt_size
);
8647 memmove (base
+ elt_size
, base
, loc
- base
);
8648 memcpy (base
, onebuf
, elt_size
);
8651 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
8653 /* base to p is sorted, *p is next to insert. */
8654 r_off
= (*ext_r_off
) (p
);
8655 /* Search the sorted region for location to insert. */
8657 while (r_off
< (*ext_r_off
) (loc
))
8662 /* Chances are there is a run of relocs to insert here,
8663 from one of more input files. Files are not always
8664 linked in order due to the way elf_link_input_bfd is
8665 called. See pr17666. */
8666 size_t sortlen
= p
- loc
;
8667 bfd_vma r_off2
= (*ext_r_off
) (loc
);
8668 size_t runlen
= elt_size
;
8669 size_t buf_size
= 96 * 1024;
8670 while (p
+ runlen
< end
8671 && (sortlen
<= buf_size
8672 || runlen
+ elt_size
<= buf_size
)
8673 && r_off2
> (*ext_r_off
) (p
+ runlen
))
8677 buf
= bfd_malloc (buf_size
);
8681 if (runlen
< sortlen
)
8683 memcpy (buf
, p
, runlen
);
8684 memmove (loc
+ runlen
, loc
, sortlen
);
8685 memcpy (loc
, buf
, runlen
);
8689 memcpy (buf
, loc
, sortlen
);
8690 memmove (loc
, p
, runlen
);
8691 memcpy (loc
+ runlen
, buf
, sortlen
);
8693 p
+= runlen
- elt_size
;
8696 /* Hashes are no longer valid. */
8697 free (reldata
->hashes
);
8698 reldata
->hashes
= NULL
;
8704 struct elf_link_sort_rela
8710 enum elf_reloc_type_class type
;
8711 /* We use this as an array of size int_rels_per_ext_rel. */
8712 Elf_Internal_Rela rela
[1];
8716 elf_link_sort_cmp1 (const void *A
, const void *B
)
8718 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8719 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8720 int relativea
, relativeb
;
8722 relativea
= a
->type
== reloc_class_relative
;
8723 relativeb
= b
->type
== reloc_class_relative
;
8725 if (relativea
< relativeb
)
8727 if (relativea
> relativeb
)
8729 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
8731 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
8733 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8735 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8741 elf_link_sort_cmp2 (const void *A
, const void *B
)
8743 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8744 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8746 if (a
->type
< b
->type
)
8748 if (a
->type
> b
->type
)
8750 if (a
->u
.offset
< b
->u
.offset
)
8752 if (a
->u
.offset
> b
->u
.offset
)
8754 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8756 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8762 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
8764 asection
*dynamic_relocs
;
8767 bfd_size_type count
, size
;
8768 size_t i
, ret
, sort_elt
, ext_size
;
8769 bfd_byte
*sort
, *s_non_relative
, *p
;
8770 struct elf_link_sort_rela
*sq
;
8771 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8772 int i2e
= bed
->s
->int_rels_per_ext_rel
;
8773 unsigned int opb
= bfd_octets_per_byte (abfd
);
8774 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8775 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8776 struct bfd_link_order
*lo
;
8778 bfd_boolean use_rela
;
8780 /* Find a dynamic reloc section. */
8781 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
8782 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
8783 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
8784 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8786 bfd_boolean use_rela_initialised
= FALSE
;
8788 /* This is just here to stop gcc from complaining.
8789 Its initialization checking code is not perfect. */
8792 /* Both sections are present. Examine the sizes
8793 of the indirect sections to help us choose. */
8794 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8795 if (lo
->type
== bfd_indirect_link_order
)
8797 asection
*o
= lo
->u
.indirect
.section
;
8799 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8801 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8802 /* Section size is divisible by both rel and rela sizes.
8803 It is of no help to us. */
8807 /* Section size is only divisible by rela. */
8808 if (use_rela_initialised
&& (use_rela
== FALSE
))
8810 _bfd_error_handler (_("%B: Unable to sort relocs - "
8811 "they are in more than one size"),
8813 bfd_set_error (bfd_error_invalid_operation
);
8819 use_rela_initialised
= TRUE
;
8823 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8825 /* Section size is only divisible by rel. */
8826 if (use_rela_initialised
&& (use_rela
== TRUE
))
8828 _bfd_error_handler (_("%B: Unable to sort relocs - "
8829 "they are in more than one size"),
8831 bfd_set_error (bfd_error_invalid_operation
);
8837 use_rela_initialised
= TRUE
;
8842 /* The section size is not divisible by either -
8843 something is wrong. */
8844 _bfd_error_handler (_("%B: Unable to sort relocs - "
8845 "they are of an unknown size"), abfd
);
8846 bfd_set_error (bfd_error_invalid_operation
);
8851 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8852 if (lo
->type
== bfd_indirect_link_order
)
8854 asection
*o
= lo
->u
.indirect
.section
;
8856 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8858 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8859 /* Section size is divisible by both rel and rela sizes.
8860 It is of no help to us. */
8864 /* Section size is only divisible by rela. */
8865 if (use_rela_initialised
&& (use_rela
== FALSE
))
8867 _bfd_error_handler (_("%B: Unable to sort relocs - "
8868 "they are in more than one size"),
8870 bfd_set_error (bfd_error_invalid_operation
);
8876 use_rela_initialised
= TRUE
;
8880 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8882 /* Section size is only divisible by rel. */
8883 if (use_rela_initialised
&& (use_rela
== TRUE
))
8885 _bfd_error_handler (_("%B: Unable to sort relocs - "
8886 "they are in more than one size"),
8888 bfd_set_error (bfd_error_invalid_operation
);
8894 use_rela_initialised
= TRUE
;
8899 /* The section size is not divisible by either -
8900 something is wrong. */
8901 _bfd_error_handler (_("%B: Unable to sort relocs - "
8902 "they are of an unknown size"), abfd
);
8903 bfd_set_error (bfd_error_invalid_operation
);
8908 if (! use_rela_initialised
)
8912 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
8914 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8921 dynamic_relocs
= rela_dyn
;
8922 ext_size
= bed
->s
->sizeof_rela
;
8923 swap_in
= bed
->s
->swap_reloca_in
;
8924 swap_out
= bed
->s
->swap_reloca_out
;
8928 dynamic_relocs
= rel_dyn
;
8929 ext_size
= bed
->s
->sizeof_rel
;
8930 swap_in
= bed
->s
->swap_reloc_in
;
8931 swap_out
= bed
->s
->swap_reloc_out
;
8935 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8936 if (lo
->type
== bfd_indirect_link_order
)
8937 size
+= lo
->u
.indirect
.section
->size
;
8939 if (size
!= dynamic_relocs
->size
)
8942 sort_elt
= (sizeof (struct elf_link_sort_rela
)
8943 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
8945 count
= dynamic_relocs
->size
/ ext_size
;
8948 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
8952 (*info
->callbacks
->warning
)
8953 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
8957 if (bed
->s
->arch_size
== 32)
8958 r_sym_mask
= ~(bfd_vma
) 0xff;
8960 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
8962 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8963 if (lo
->type
== bfd_indirect_link_order
)
8965 bfd_byte
*erel
, *erelend
;
8966 asection
*o
= lo
->u
.indirect
.section
;
8968 if (o
->contents
== NULL
&& o
->size
!= 0)
8970 /* This is a reloc section that is being handled as a normal
8971 section. See bfd_section_from_shdr. We can't combine
8972 relocs in this case. */
8977 erelend
= o
->contents
+ o
->size
;
8978 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
8980 while (erel
< erelend
)
8982 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8984 (*swap_in
) (abfd
, erel
, s
->rela
);
8985 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
8986 s
->u
.sym_mask
= r_sym_mask
;
8992 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
8994 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
8996 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8997 if (s
->type
!= reloc_class_relative
)
9003 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
9004 for (; i
< count
; i
++, p
+= sort_elt
)
9006 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
9007 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
9009 sp
->u
.offset
= sq
->rela
->r_offset
;
9012 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
9014 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
9015 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
9017 /* We have plt relocs in .rela.dyn. */
9018 sq
= (struct elf_link_sort_rela
*) sort
;
9019 for (i
= 0; i
< count
; i
++)
9020 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
9022 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
9024 struct bfd_link_order
**plo
;
9025 /* Put srelplt link_order last. This is so the output_offset
9026 set in the next loop is correct for DT_JMPREL. */
9027 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
9028 if ((*plo
)->type
== bfd_indirect_link_order
9029 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
9035 plo
= &(*plo
)->next
;
9038 dynamic_relocs
->map_tail
.link_order
= lo
;
9043 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9044 if (lo
->type
== bfd_indirect_link_order
)
9046 bfd_byte
*erel
, *erelend
;
9047 asection
*o
= lo
->u
.indirect
.section
;
9050 erelend
= o
->contents
+ o
->size
;
9051 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
9052 while (erel
< erelend
)
9054 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9055 (*swap_out
) (abfd
, s
->rela
, erel
);
9062 *psec
= dynamic_relocs
;
9066 /* Add a symbol to the output symbol string table. */
9069 elf_link_output_symstrtab (struct elf_final_link_info
*flinfo
,
9071 Elf_Internal_Sym
*elfsym
,
9072 asection
*input_sec
,
9073 struct elf_link_hash_entry
*h
)
9075 int (*output_symbol_hook
)
9076 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
9077 struct elf_link_hash_entry
*);
9078 struct elf_link_hash_table
*hash_table
;
9079 const struct elf_backend_data
*bed
;
9080 bfd_size_type strtabsize
;
9082 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9084 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9085 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
9086 if (output_symbol_hook
!= NULL
)
9088 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
9095 || (input_sec
->flags
& SEC_EXCLUDE
))
9096 elfsym
->st_name
= (unsigned long) -1;
9099 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9100 to get the final offset for st_name. */
9102 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
9104 if (elfsym
->st_name
== (unsigned long) -1)
9108 hash_table
= elf_hash_table (flinfo
->info
);
9109 strtabsize
= hash_table
->strtabsize
;
9110 if (strtabsize
<= hash_table
->strtabcount
)
9112 strtabsize
+= strtabsize
;
9113 hash_table
->strtabsize
= strtabsize
;
9114 strtabsize
*= sizeof (*hash_table
->strtab
);
9116 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
9118 if (hash_table
->strtab
== NULL
)
9121 hash_table
->strtab
[hash_table
->strtabcount
].sym
= *elfsym
;
9122 hash_table
->strtab
[hash_table
->strtabcount
].dest_index
9123 = hash_table
->strtabcount
;
9124 hash_table
->strtab
[hash_table
->strtabcount
].destshndx_index
9125 = flinfo
->symshndxbuf
? bfd_get_symcount (flinfo
->output_bfd
) : 0;
9127 bfd_get_symcount (flinfo
->output_bfd
) += 1;
9128 hash_table
->strtabcount
+= 1;
9133 /* Swap symbols out to the symbol table and flush the output symbols to
9137 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
9139 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
9142 const struct elf_backend_data
*bed
;
9144 Elf_Internal_Shdr
*hdr
;
9148 if (!hash_table
->strtabcount
)
9151 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9153 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9155 amt
= bed
->s
->sizeof_sym
* hash_table
->strtabcount
;
9156 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
9160 if (flinfo
->symshndxbuf
)
9162 amt
= sizeof (Elf_External_Sym_Shndx
);
9163 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
9164 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
9165 if (flinfo
->symshndxbuf
== NULL
)
9172 for (i
= 0; i
< hash_table
->strtabcount
; i
++)
9174 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
9175 if (elfsym
->sym
.st_name
== (unsigned long) -1)
9176 elfsym
->sym
.st_name
= 0;
9179 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
9180 elfsym
->sym
.st_name
);
9181 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
9182 ((bfd_byte
*) symbuf
9183 + (elfsym
->dest_index
9184 * bed
->s
->sizeof_sym
)),
9185 (flinfo
->symshndxbuf
9186 + elfsym
->destshndx_index
));
9189 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
9190 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
9191 amt
= hash_table
->strtabcount
* bed
->s
->sizeof_sym
;
9192 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
9193 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
9195 hdr
->sh_size
+= amt
;
9203 free (hash_table
->strtab
);
9204 hash_table
->strtab
= NULL
;
9209 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9212 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
9214 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
9215 && sym
->st_shndx
< SHN_LORESERVE
)
9217 /* The gABI doesn't support dynamic symbols in output sections
9220 /* xgettext:c-format */
9221 (_("%B: Too many sections: %d (>= %d)"),
9222 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
9223 bfd_set_error (bfd_error_nonrepresentable_section
);
9229 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9230 allowing an unsatisfied unversioned symbol in the DSO to match a
9231 versioned symbol that would normally require an explicit version.
9232 We also handle the case that a DSO references a hidden symbol
9233 which may be satisfied by a versioned symbol in another DSO. */
9236 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
9237 const struct elf_backend_data
*bed
,
9238 struct elf_link_hash_entry
*h
)
9241 struct elf_link_loaded_list
*loaded
;
9243 if (!is_elf_hash_table (info
->hash
))
9246 /* Check indirect symbol. */
9247 while (h
->root
.type
== bfd_link_hash_indirect
)
9248 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9250 switch (h
->root
.type
)
9256 case bfd_link_hash_undefined
:
9257 case bfd_link_hash_undefweak
:
9258 abfd
= h
->root
.u
.undef
.abfd
;
9260 || (abfd
->flags
& DYNAMIC
) == 0
9261 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
9265 case bfd_link_hash_defined
:
9266 case bfd_link_hash_defweak
:
9267 abfd
= h
->root
.u
.def
.section
->owner
;
9270 case bfd_link_hash_common
:
9271 abfd
= h
->root
.u
.c
.p
->section
->owner
;
9274 BFD_ASSERT (abfd
!= NULL
);
9276 for (loaded
= elf_hash_table (info
)->loaded
;
9278 loaded
= loaded
->next
)
9281 Elf_Internal_Shdr
*hdr
;
9285 Elf_Internal_Shdr
*versymhdr
;
9286 Elf_Internal_Sym
*isym
;
9287 Elf_Internal_Sym
*isymend
;
9288 Elf_Internal_Sym
*isymbuf
;
9289 Elf_External_Versym
*ever
;
9290 Elf_External_Versym
*extversym
;
9292 input
= loaded
->abfd
;
9294 /* We check each DSO for a possible hidden versioned definition. */
9296 || (input
->flags
& DYNAMIC
) == 0
9297 || elf_dynversym (input
) == 0)
9300 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
9302 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9303 if (elf_bad_symtab (input
))
9305 extsymcount
= symcount
;
9310 extsymcount
= symcount
- hdr
->sh_info
;
9311 extsymoff
= hdr
->sh_info
;
9314 if (extsymcount
== 0)
9317 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
9319 if (isymbuf
== NULL
)
9322 /* Read in any version definitions. */
9323 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
9324 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
9325 if (extversym
== NULL
)
9328 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
9329 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
9330 != versymhdr
->sh_size
))
9338 ever
= extversym
+ extsymoff
;
9339 isymend
= isymbuf
+ extsymcount
;
9340 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
9343 Elf_Internal_Versym iver
;
9344 unsigned short version_index
;
9346 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
9347 || isym
->st_shndx
== SHN_UNDEF
)
9350 name
= bfd_elf_string_from_elf_section (input
,
9353 if (strcmp (name
, h
->root
.root
.string
) != 0)
9356 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
9358 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
9360 && h
->forced_local
))
9362 /* If we have a non-hidden versioned sym, then it should
9363 have provided a definition for the undefined sym unless
9364 it is defined in a non-shared object and forced local.
9369 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
9370 if (version_index
== 1 || version_index
== 2)
9372 /* This is the base or first version. We can use it. */
9386 /* Convert ELF common symbol TYPE. */
9389 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
9391 /* Commom symbol can only appear in relocatable link. */
9392 if (!bfd_link_relocatable (info
))
9394 switch (info
->elf_stt_common
)
9398 case elf_stt_common
:
9401 case no_elf_stt_common
:
9408 /* Add an external symbol to the symbol table. This is called from
9409 the hash table traversal routine. When generating a shared object,
9410 we go through the symbol table twice. The first time we output
9411 anything that might have been forced to local scope in a version
9412 script. The second time we output the symbols that are still
9416 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
9418 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
9419 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
9420 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
9422 Elf_Internal_Sym sym
;
9423 asection
*input_sec
;
9424 const struct elf_backend_data
*bed
;
9429 if (h
->root
.type
== bfd_link_hash_warning
)
9431 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9432 if (h
->root
.type
== bfd_link_hash_new
)
9436 /* Decide whether to output this symbol in this pass. */
9437 if (eoinfo
->localsyms
)
9439 if (!h
->forced_local
)
9444 if (h
->forced_local
)
9448 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9450 if (h
->root
.type
== bfd_link_hash_undefined
)
9452 /* If we have an undefined symbol reference here then it must have
9453 come from a shared library that is being linked in. (Undefined
9454 references in regular files have already been handled unless
9455 they are in unreferenced sections which are removed by garbage
9457 bfd_boolean ignore_undef
= FALSE
;
9459 /* Some symbols may be special in that the fact that they're
9460 undefined can be safely ignored - let backend determine that. */
9461 if (bed
->elf_backend_ignore_undef_symbol
)
9462 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
9464 /* If we are reporting errors for this situation then do so now. */
9467 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
9468 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
9469 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
9470 (*flinfo
->info
->callbacks
->undefined_symbol
)
9471 (flinfo
->info
, h
->root
.root
.string
,
9472 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
,
9474 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
);
9476 /* Strip a global symbol defined in a discarded section. */
9481 /* We should also warn if a forced local symbol is referenced from
9482 shared libraries. */
9483 if (bfd_link_executable (flinfo
->info
)
9488 && h
->ref_dynamic_nonweak
9489 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
9493 struct elf_link_hash_entry
*hi
= h
;
9495 /* Check indirect symbol. */
9496 while (hi
->root
.type
== bfd_link_hash_indirect
)
9497 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
9499 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
9500 /* xgettext:c-format */
9501 msg
= _("%B: internal symbol `%s' in %B is referenced by DSO");
9502 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
9503 /* xgettext:c-format */
9504 msg
= _("%B: hidden symbol `%s' in %B is referenced by DSO");
9506 /* xgettext:c-format */
9507 msg
= _("%B: local symbol `%s' in %B is referenced by DSO");
9508 def_bfd
= flinfo
->output_bfd
;
9509 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
9510 def_bfd
= hi
->root
.u
.def
.section
->owner
;
9511 _bfd_error_handler (msg
, flinfo
->output_bfd
,
9512 h
->root
.root
.string
, def_bfd
);
9513 bfd_set_error (bfd_error_bad_value
);
9514 eoinfo
->failed
= TRUE
;
9518 /* We don't want to output symbols that have never been mentioned by
9519 a regular file, or that we have been told to strip. However, if
9520 h->indx is set to -2, the symbol is used by a reloc and we must
9525 else if ((h
->def_dynamic
9527 || h
->root
.type
== bfd_link_hash_new
)
9531 else if (flinfo
->info
->strip
== strip_all
)
9533 else if (flinfo
->info
->strip
== strip_some
9534 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
9535 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
9537 else if ((h
->root
.type
== bfd_link_hash_defined
9538 || h
->root
.type
== bfd_link_hash_defweak
)
9539 && ((flinfo
->info
->strip_discarded
9540 && discarded_section (h
->root
.u
.def
.section
))
9541 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
9542 && h
->root
.u
.def
.section
->owner
!= NULL
9543 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
9545 else if ((h
->root
.type
== bfd_link_hash_undefined
9546 || h
->root
.type
== bfd_link_hash_undefweak
)
9547 && h
->root
.u
.undef
.abfd
!= NULL
9548 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
9553 /* If we're stripping it, and it's not a dynamic symbol, there's
9554 nothing else to do. However, if it is a forced local symbol or
9555 an ifunc symbol we need to give the backend finish_dynamic_symbol
9556 function a chance to make it dynamic. */
9559 && type
!= STT_GNU_IFUNC
9560 && !h
->forced_local
)
9564 sym
.st_size
= h
->size
;
9565 sym
.st_other
= h
->other
;
9566 switch (h
->root
.type
)
9569 case bfd_link_hash_new
:
9570 case bfd_link_hash_warning
:
9574 case bfd_link_hash_undefined
:
9575 case bfd_link_hash_undefweak
:
9576 input_sec
= bfd_und_section_ptr
;
9577 sym
.st_shndx
= SHN_UNDEF
;
9580 case bfd_link_hash_defined
:
9581 case bfd_link_hash_defweak
:
9583 input_sec
= h
->root
.u
.def
.section
;
9584 if (input_sec
->output_section
!= NULL
)
9587 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
9588 input_sec
->output_section
);
9589 if (sym
.st_shndx
== SHN_BAD
)
9592 /* xgettext:c-format */
9593 (_("%B: could not find output section %A for input section %A"),
9594 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
9595 bfd_set_error (bfd_error_nonrepresentable_section
);
9596 eoinfo
->failed
= TRUE
;
9600 /* ELF symbols in relocatable files are section relative,
9601 but in nonrelocatable files they are virtual
9603 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
9604 if (!bfd_link_relocatable (flinfo
->info
))
9606 sym
.st_value
+= input_sec
->output_section
->vma
;
9607 if (h
->type
== STT_TLS
)
9609 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
9610 if (tls_sec
!= NULL
)
9611 sym
.st_value
-= tls_sec
->vma
;
9617 BFD_ASSERT (input_sec
->owner
== NULL
9618 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
9619 sym
.st_shndx
= SHN_UNDEF
;
9620 input_sec
= bfd_und_section_ptr
;
9625 case bfd_link_hash_common
:
9626 input_sec
= h
->root
.u
.c
.p
->section
;
9627 sym
.st_shndx
= bed
->common_section_index (input_sec
);
9628 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
9631 case bfd_link_hash_indirect
:
9632 /* These symbols are created by symbol versioning. They point
9633 to the decorated version of the name. For example, if the
9634 symbol foo@@GNU_1.2 is the default, which should be used when
9635 foo is used with no version, then we add an indirect symbol
9636 foo which points to foo@@GNU_1.2. We ignore these symbols,
9637 since the indirected symbol is already in the hash table. */
9641 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
9642 switch (h
->root
.type
)
9644 case bfd_link_hash_common
:
9645 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9647 case bfd_link_hash_defined
:
9648 case bfd_link_hash_defweak
:
9649 if (bed
->common_definition (&sym
))
9650 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9654 case bfd_link_hash_undefined
:
9655 case bfd_link_hash_undefweak
:
9661 if (h
->forced_local
)
9663 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
9664 /* Turn off visibility on local symbol. */
9665 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
9667 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9668 else if (h
->unique_global
&& h
->def_regular
)
9669 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
9670 else if (h
->root
.type
== bfd_link_hash_undefweak
9671 || h
->root
.type
== bfd_link_hash_defweak
)
9672 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
9674 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
9675 sym
.st_target_internal
= h
->target_internal
;
9677 /* Give the processor backend a chance to tweak the symbol value,
9678 and also to finish up anything that needs to be done for this
9679 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9680 forced local syms when non-shared is due to a historical quirk.
9681 STT_GNU_IFUNC symbol must go through PLT. */
9682 if ((h
->type
== STT_GNU_IFUNC
9684 && !bfd_link_relocatable (flinfo
->info
))
9685 || ((h
->dynindx
!= -1
9687 && ((bfd_link_pic (flinfo
->info
)
9688 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
9689 || h
->root
.type
!= bfd_link_hash_undefweak
))
9690 || !h
->forced_local
)
9691 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
9693 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
9694 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
9696 eoinfo
->failed
= TRUE
;
9701 /* If we are marking the symbol as undefined, and there are no
9702 non-weak references to this symbol from a regular object, then
9703 mark the symbol as weak undefined; if there are non-weak
9704 references, mark the symbol as strong. We can't do this earlier,
9705 because it might not be marked as undefined until the
9706 finish_dynamic_symbol routine gets through with it. */
9707 if (sym
.st_shndx
== SHN_UNDEF
9709 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
9710 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
9713 type
= ELF_ST_TYPE (sym
.st_info
);
9715 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9716 if (type
== STT_GNU_IFUNC
)
9719 if (h
->ref_regular_nonweak
)
9720 bindtype
= STB_GLOBAL
;
9722 bindtype
= STB_WEAK
;
9723 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
9726 /* If this is a symbol defined in a dynamic library, don't use the
9727 symbol size from the dynamic library. Relinking an executable
9728 against a new library may introduce gratuitous changes in the
9729 executable's symbols if we keep the size. */
9730 if (sym
.st_shndx
== SHN_UNDEF
9735 /* If a non-weak symbol with non-default visibility is not defined
9736 locally, it is a fatal error. */
9737 if (!bfd_link_relocatable (flinfo
->info
)
9738 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
9739 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
9740 && h
->root
.type
== bfd_link_hash_undefined
9745 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
9746 /* xgettext:c-format */
9747 msg
= _("%B: protected symbol `%s' isn't defined");
9748 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
9749 /* xgettext:c-format */
9750 msg
= _("%B: internal symbol `%s' isn't defined");
9752 /* xgettext:c-format */
9753 msg
= _("%B: hidden symbol `%s' isn't defined");
9754 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
9755 bfd_set_error (bfd_error_bad_value
);
9756 eoinfo
->failed
= TRUE
;
9760 /* If this symbol should be put in the .dynsym section, then put it
9761 there now. We already know the symbol index. We also fill in
9762 the entry in the .hash section. */
9763 if (elf_hash_table (flinfo
->info
)->dynsym
!= NULL
9765 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
)
9769 /* Since there is no version information in the dynamic string,
9770 if there is no version info in symbol version section, we will
9771 have a run-time problem if not linking executable, referenced
9772 by shared library, or not bound locally. */
9773 if (h
->verinfo
.verdef
== NULL
9774 && (!bfd_link_executable (flinfo
->info
)
9776 || !h
->def_regular
))
9778 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
9780 if (p
&& p
[1] != '\0')
9783 /* xgettext:c-format */
9784 (_("%B: No symbol version section for versioned symbol `%s'"),
9785 flinfo
->output_bfd
, h
->root
.root
.string
);
9786 eoinfo
->failed
= TRUE
;
9791 sym
.st_name
= h
->dynstr_index
;
9792 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
9793 + h
->dynindx
* bed
->s
->sizeof_sym
);
9794 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
9796 eoinfo
->failed
= TRUE
;
9799 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
9801 if (flinfo
->hash_sec
!= NULL
)
9803 size_t hash_entry_size
;
9804 bfd_byte
*bucketpos
;
9809 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
9810 bucket
= h
->u
.elf_hash_value
% bucketcount
;
9813 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
9814 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
9815 + (bucket
+ 2) * hash_entry_size
);
9816 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
9817 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
9819 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
9820 ((bfd_byte
*) flinfo
->hash_sec
->contents
9821 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
9824 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
9826 Elf_Internal_Versym iversym
;
9827 Elf_External_Versym
*eversym
;
9829 if (!h
->def_regular
)
9831 if (h
->verinfo
.verdef
== NULL
9832 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
9833 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
9834 iversym
.vs_vers
= 0;
9836 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
9840 if (h
->verinfo
.vertree
== NULL
)
9841 iversym
.vs_vers
= 1;
9843 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
9844 if (flinfo
->info
->create_default_symver
)
9848 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9850 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
9851 iversym
.vs_vers
|= VERSYM_HIDDEN
;
9853 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
9854 eversym
+= h
->dynindx
;
9855 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
9859 /* If the symbol is undefined, and we didn't output it to .dynsym,
9860 strip it from .symtab too. Obviously we can't do this for
9861 relocatable output or when needed for --emit-relocs. */
9862 else if (input_sec
== bfd_und_section_ptr
9864 && !bfd_link_relocatable (flinfo
->info
))
9866 /* Also strip others that we couldn't earlier due to dynamic symbol
9870 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
9873 /* Output a FILE symbol so that following locals are not associated
9874 with the wrong input file. We need one for forced local symbols
9875 if we've seen more than one FILE symbol or when we have exactly
9876 one FILE symbol but global symbols are present in a file other
9877 than the one with the FILE symbol. We also need one if linker
9878 defined symbols are present. In practice these conditions are
9879 always met, so just emit the FILE symbol unconditionally. */
9880 if (eoinfo
->localsyms
9881 && !eoinfo
->file_sym_done
9882 && eoinfo
->flinfo
->filesym_count
!= 0)
9884 Elf_Internal_Sym fsym
;
9886 memset (&fsym
, 0, sizeof (fsym
));
9887 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
9888 fsym
.st_shndx
= SHN_ABS
;
9889 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
9890 bfd_und_section_ptr
, NULL
))
9893 eoinfo
->file_sym_done
= TRUE
;
9896 indx
= bfd_get_symcount (flinfo
->output_bfd
);
9897 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
9901 eoinfo
->failed
= TRUE
;
9906 else if (h
->indx
== -2)
9912 /* Return TRUE if special handling is done for relocs in SEC against
9913 symbols defined in discarded sections. */
9916 elf_section_ignore_discarded_relocs (asection
*sec
)
9918 const struct elf_backend_data
*bed
;
9920 switch (sec
->sec_info_type
)
9922 case SEC_INFO_TYPE_STABS
:
9923 case SEC_INFO_TYPE_EH_FRAME
:
9924 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
9930 bed
= get_elf_backend_data (sec
->owner
);
9931 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
9932 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
9938 /* Return a mask saying how ld should treat relocations in SEC against
9939 symbols defined in discarded sections. If this function returns
9940 COMPLAIN set, ld will issue a warning message. If this function
9941 returns PRETEND set, and the discarded section was link-once and the
9942 same size as the kept link-once section, ld will pretend that the
9943 symbol was actually defined in the kept section. Otherwise ld will
9944 zero the reloc (at least that is the intent, but some cooperation by
9945 the target dependent code is needed, particularly for REL targets). */
9948 _bfd_elf_default_action_discarded (asection
*sec
)
9950 if (sec
->flags
& SEC_DEBUGGING
)
9953 if (strcmp (".eh_frame", sec
->name
) == 0)
9956 if (strcmp (".gcc_except_table", sec
->name
) == 0)
9959 return COMPLAIN
| PRETEND
;
9962 /* Find a match between a section and a member of a section group. */
9965 match_group_member (asection
*sec
, asection
*group
,
9966 struct bfd_link_info
*info
)
9968 asection
*first
= elf_next_in_group (group
);
9969 asection
*s
= first
;
9973 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
9976 s
= elf_next_in_group (s
);
9984 /* Check if the kept section of a discarded section SEC can be used
9985 to replace it. Return the replacement if it is OK. Otherwise return
9989 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
9993 kept
= sec
->kept_section
;
9996 if ((kept
->flags
& SEC_GROUP
) != 0)
9997 kept
= match_group_member (sec
, kept
, info
);
9999 && ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
10000 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
)))
10002 sec
->kept_section
= kept
;
10007 /* Link an input file into the linker output file. This function
10008 handles all the sections and relocations of the input file at once.
10009 This is so that we only have to read the local symbols once, and
10010 don't have to keep them in memory. */
10013 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
10015 int (*relocate_section
)
10016 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
10017 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
10019 Elf_Internal_Shdr
*symtab_hdr
;
10020 size_t locsymcount
;
10022 Elf_Internal_Sym
*isymbuf
;
10023 Elf_Internal_Sym
*isym
;
10024 Elf_Internal_Sym
*isymend
;
10026 asection
**ppsection
;
10028 const struct elf_backend_data
*bed
;
10029 struct elf_link_hash_entry
**sym_hashes
;
10030 bfd_size_type address_size
;
10031 bfd_vma r_type_mask
;
10033 bfd_boolean have_file_sym
= FALSE
;
10035 output_bfd
= flinfo
->output_bfd
;
10036 bed
= get_elf_backend_data (output_bfd
);
10037 relocate_section
= bed
->elf_backend_relocate_section
;
10039 /* If this is a dynamic object, we don't want to do anything here:
10040 we don't want the local symbols, and we don't want the section
10042 if ((input_bfd
->flags
& DYNAMIC
) != 0)
10045 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
10046 if (elf_bad_symtab (input_bfd
))
10048 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10053 locsymcount
= symtab_hdr
->sh_info
;
10054 extsymoff
= symtab_hdr
->sh_info
;
10057 /* Read the local symbols. */
10058 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10059 if (isymbuf
== NULL
&& locsymcount
!= 0)
10061 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
10062 flinfo
->internal_syms
,
10063 flinfo
->external_syms
,
10064 flinfo
->locsym_shndx
);
10065 if (isymbuf
== NULL
)
10069 /* Find local symbol sections and adjust values of symbols in
10070 SEC_MERGE sections. Write out those local symbols we know are
10071 going into the output file. */
10072 isymend
= isymbuf
+ locsymcount
;
10073 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
10075 isym
++, pindex
++, ppsection
++)
10079 Elf_Internal_Sym osym
;
10085 if (elf_bad_symtab (input_bfd
))
10087 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
10094 if (isym
->st_shndx
== SHN_UNDEF
)
10095 isec
= bfd_und_section_ptr
;
10096 else if (isym
->st_shndx
== SHN_ABS
)
10097 isec
= bfd_abs_section_ptr
;
10098 else if (isym
->st_shndx
== SHN_COMMON
)
10099 isec
= bfd_com_section_ptr
;
10102 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
10105 /* Don't attempt to output symbols with st_shnx in the
10106 reserved range other than SHN_ABS and SHN_COMMON. */
10110 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
10111 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
10113 _bfd_merged_section_offset (output_bfd
, &isec
,
10114 elf_section_data (isec
)->sec_info
,
10120 /* Don't output the first, undefined, symbol. In fact, don't
10121 output any undefined local symbol. */
10122 if (isec
== bfd_und_section_ptr
)
10125 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
10127 /* We never output section symbols. Instead, we use the
10128 section symbol of the corresponding section in the output
10133 /* If we are stripping all symbols, we don't want to output this
10135 if (flinfo
->info
->strip
== strip_all
)
10138 /* If we are discarding all local symbols, we don't want to
10139 output this one. If we are generating a relocatable output
10140 file, then some of the local symbols may be required by
10141 relocs; we output them below as we discover that they are
10143 if (flinfo
->info
->discard
== discard_all
)
10146 /* If this symbol is defined in a section which we are
10147 discarding, we don't need to keep it. */
10148 if (isym
->st_shndx
!= SHN_UNDEF
10149 && isym
->st_shndx
< SHN_LORESERVE
10150 && bfd_section_removed_from_list (output_bfd
,
10151 isec
->output_section
))
10154 /* Get the name of the symbol. */
10155 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
10160 /* See if we are discarding symbols with this name. */
10161 if ((flinfo
->info
->strip
== strip_some
10162 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
10164 || (((flinfo
->info
->discard
== discard_sec_merge
10165 && (isec
->flags
& SEC_MERGE
)
10166 && !bfd_link_relocatable (flinfo
->info
))
10167 || flinfo
->info
->discard
== discard_l
)
10168 && bfd_is_local_label_name (input_bfd
, name
)))
10171 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
10173 if (input_bfd
->lto_output
)
10174 /* -flto puts a temp file name here. This means builds
10175 are not reproducible. Discard the symbol. */
10177 have_file_sym
= TRUE
;
10178 flinfo
->filesym_count
+= 1;
10180 if (!have_file_sym
)
10182 /* In the absence of debug info, bfd_find_nearest_line uses
10183 FILE symbols to determine the source file for local
10184 function symbols. Provide a FILE symbol here if input
10185 files lack such, so that their symbols won't be
10186 associated with a previous input file. It's not the
10187 source file, but the best we can do. */
10188 have_file_sym
= TRUE
;
10189 flinfo
->filesym_count
+= 1;
10190 memset (&osym
, 0, sizeof (osym
));
10191 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10192 osym
.st_shndx
= SHN_ABS
;
10193 if (!elf_link_output_symstrtab (flinfo
,
10194 (input_bfd
->lto_output
? NULL
10195 : input_bfd
->filename
),
10196 &osym
, bfd_abs_section_ptr
,
10203 /* Adjust the section index for the output file. */
10204 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10205 isec
->output_section
);
10206 if (osym
.st_shndx
== SHN_BAD
)
10209 /* ELF symbols in relocatable files are section relative, but
10210 in executable files they are virtual addresses. Note that
10211 this code assumes that all ELF sections have an associated
10212 BFD section with a reasonable value for output_offset; below
10213 we assume that they also have a reasonable value for
10214 output_section. Any special sections must be set up to meet
10215 these requirements. */
10216 osym
.st_value
+= isec
->output_offset
;
10217 if (!bfd_link_relocatable (flinfo
->info
))
10219 osym
.st_value
+= isec
->output_section
->vma
;
10220 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
10222 /* STT_TLS symbols are relative to PT_TLS segment base. */
10223 BFD_ASSERT (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
);
10224 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
10228 indx
= bfd_get_symcount (output_bfd
);
10229 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
10236 if (bed
->s
->arch_size
== 32)
10238 r_type_mask
= 0xff;
10244 r_type_mask
= 0xffffffff;
10249 /* Relocate the contents of each section. */
10250 sym_hashes
= elf_sym_hashes (input_bfd
);
10251 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
10253 bfd_byte
*contents
;
10255 if (! o
->linker_mark
)
10257 /* This section was omitted from the link. */
10261 if (bfd_link_relocatable (flinfo
->info
)
10262 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
10264 /* Deal with the group signature symbol. */
10265 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
10266 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
10267 asection
*osec
= o
->output_section
;
10269 if (symndx
>= locsymcount
10270 || (elf_bad_symtab (input_bfd
)
10271 && flinfo
->sections
[symndx
] == NULL
))
10273 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
10274 while (h
->root
.type
== bfd_link_hash_indirect
10275 || h
->root
.type
== bfd_link_hash_warning
)
10276 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10277 /* Arrange for symbol to be output. */
10279 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
10281 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
10283 /* We'll use the output section target_index. */
10284 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10285 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
10289 if (flinfo
->indices
[symndx
] == -1)
10291 /* Otherwise output the local symbol now. */
10292 Elf_Internal_Sym sym
= isymbuf
[symndx
];
10293 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10298 name
= bfd_elf_string_from_elf_section (input_bfd
,
10299 symtab_hdr
->sh_link
,
10304 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10306 if (sym
.st_shndx
== SHN_BAD
)
10309 sym
.st_value
+= o
->output_offset
;
10311 indx
= bfd_get_symcount (output_bfd
);
10312 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
10317 flinfo
->indices
[symndx
] = indx
;
10321 elf_section_data (osec
)->this_hdr
.sh_info
10322 = flinfo
->indices
[symndx
];
10326 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
10327 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
10330 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
10332 /* Section was created by _bfd_elf_link_create_dynamic_sections
10337 /* Get the contents of the section. They have been cached by a
10338 relaxation routine. Note that o is a section in an input
10339 file, so the contents field will not have been set by any of
10340 the routines which work on output files. */
10341 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
10343 contents
= elf_section_data (o
)->this_hdr
.contents
;
10344 if (bed
->caches_rawsize
10346 && o
->rawsize
< o
->size
)
10348 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
10349 contents
= flinfo
->contents
;
10354 contents
= flinfo
->contents
;
10355 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
10359 if ((o
->flags
& SEC_RELOC
) != 0)
10361 Elf_Internal_Rela
*internal_relocs
;
10362 Elf_Internal_Rela
*rel
, *relend
;
10363 int action_discarded
;
10366 /* Get the swapped relocs. */
10368 = _bfd_elf_link_read_relocs (input_bfd
, o
, flinfo
->external_relocs
,
10369 flinfo
->internal_relocs
, FALSE
);
10370 if (internal_relocs
== NULL
10371 && o
->reloc_count
> 0)
10374 /* We need to reverse-copy input .ctors/.dtors sections if
10375 they are placed in .init_array/.finit_array for output. */
10376 if (o
->size
> address_size
10377 && ((strncmp (o
->name
, ".ctors", 6) == 0
10378 && strcmp (o
->output_section
->name
,
10379 ".init_array") == 0)
10380 || (strncmp (o
->name
, ".dtors", 6) == 0
10381 && strcmp (o
->output_section
->name
,
10382 ".fini_array") == 0))
10383 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
10385 if (o
->size
!= o
->reloc_count
* address_size
)
10388 /* xgettext:c-format */
10389 (_("error: %B: size of section %A is not "
10390 "multiple of address size"),
10392 bfd_set_error (bfd_error_on_input
);
10395 o
->flags
|= SEC_ELF_REVERSE_COPY
;
10398 action_discarded
= -1;
10399 if (!elf_section_ignore_discarded_relocs (o
))
10400 action_discarded
= (*bed
->action_discarded
) (o
);
10402 /* Run through the relocs evaluating complex reloc symbols and
10403 looking for relocs against symbols from discarded sections
10404 or section symbols from removed link-once sections.
10405 Complain about relocs against discarded sections. Zero
10406 relocs against removed link-once sections. */
10408 rel
= internal_relocs
;
10409 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10410 for ( ; rel
< relend
; rel
++)
10412 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
10413 unsigned int s_type
;
10414 asection
**ps
, *sec
;
10415 struct elf_link_hash_entry
*h
= NULL
;
10416 const char *sym_name
;
10418 if (r_symndx
== STN_UNDEF
)
10421 if (r_symndx
>= locsymcount
10422 || (elf_bad_symtab (input_bfd
)
10423 && flinfo
->sections
[r_symndx
] == NULL
))
10425 h
= sym_hashes
[r_symndx
- extsymoff
];
10427 /* Badly formatted input files can contain relocs that
10428 reference non-existant symbols. Check here so that
10429 we do not seg fault. */
10434 sprintf_vma (buffer
, rel
->r_info
);
10436 /* xgettext:c-format */
10437 (_("error: %B contains a reloc (0x%s) for section %A "
10438 "that references a non-existent global symbol"),
10439 input_bfd
, buffer
, o
);
10440 bfd_set_error (bfd_error_bad_value
);
10444 while (h
->root
.type
== bfd_link_hash_indirect
10445 || h
->root
.type
== bfd_link_hash_warning
)
10446 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10450 /* If a plugin symbol is referenced from a non-IR file,
10451 mark the symbol as undefined. Note that the
10452 linker may attach linker created dynamic sections
10453 to the plugin bfd. Symbols defined in linker
10454 created sections are not plugin symbols. */
10455 if (h
->root
.non_ir_ref
10456 && (h
->root
.type
== bfd_link_hash_defined
10457 || h
->root
.type
== bfd_link_hash_defweak
)
10458 && (h
->root
.u
.def
.section
->flags
10459 & SEC_LINKER_CREATED
) == 0
10460 && h
->root
.u
.def
.section
->owner
!= NULL
10461 && (h
->root
.u
.def
.section
->owner
->flags
10462 & BFD_PLUGIN
) != 0)
10464 h
->root
.type
= bfd_link_hash_undefined
;
10465 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
10469 if (h
->root
.type
== bfd_link_hash_defined
10470 || h
->root
.type
== bfd_link_hash_defweak
)
10471 ps
= &h
->root
.u
.def
.section
;
10473 sym_name
= h
->root
.root
.string
;
10477 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
10479 s_type
= ELF_ST_TYPE (sym
->st_info
);
10480 ps
= &flinfo
->sections
[r_symndx
];
10481 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
10485 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
10486 && !bfd_link_relocatable (flinfo
->info
))
10489 bfd_vma dot
= (rel
->r_offset
10490 + o
->output_offset
+ o
->output_section
->vma
);
10492 printf ("Encountered a complex symbol!");
10493 printf (" (input_bfd %s, section %s, reloc %ld\n",
10494 input_bfd
->filename
, o
->name
,
10495 (long) (rel
- internal_relocs
));
10496 printf (" symbol: idx %8.8lx, name %s\n",
10497 r_symndx
, sym_name
);
10498 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10499 (unsigned long) rel
->r_info
,
10500 (unsigned long) rel
->r_offset
);
10502 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
10503 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
10506 /* Symbol evaluated OK. Update to absolute value. */
10507 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
10512 if (action_discarded
!= -1 && ps
!= NULL
)
10514 /* Complain if the definition comes from a
10515 discarded section. */
10516 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
10518 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
10519 if (action_discarded
& COMPLAIN
)
10520 (*flinfo
->info
->callbacks
->einfo
)
10521 /* xgettext:c-format */
10522 (_("%X`%s' referenced in section `%A' of %B: "
10523 "defined in discarded section `%A' of %B\n"),
10524 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
10526 /* Try to do the best we can to support buggy old
10527 versions of gcc. Pretend that the symbol is
10528 really defined in the kept linkonce section.
10529 FIXME: This is quite broken. Modifying the
10530 symbol here means we will be changing all later
10531 uses of the symbol, not just in this section. */
10532 if (action_discarded
& PRETEND
)
10536 kept
= _bfd_elf_check_kept_section (sec
,
10548 /* Relocate the section by invoking a back end routine.
10550 The back end routine is responsible for adjusting the
10551 section contents as necessary, and (if using Rela relocs
10552 and generating a relocatable output file) adjusting the
10553 reloc addend as necessary.
10555 The back end routine does not have to worry about setting
10556 the reloc address or the reloc symbol index.
10558 The back end routine is given a pointer to the swapped in
10559 internal symbols, and can access the hash table entries
10560 for the external symbols via elf_sym_hashes (input_bfd).
10562 When generating relocatable output, the back end routine
10563 must handle STB_LOCAL/STT_SECTION symbols specially. The
10564 output symbol is going to be a section symbol
10565 corresponding to the output section, which will require
10566 the addend to be adjusted. */
10568 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
10569 input_bfd
, o
, contents
,
10577 || bfd_link_relocatable (flinfo
->info
)
10578 || flinfo
->info
->emitrelocations
)
10580 Elf_Internal_Rela
*irela
;
10581 Elf_Internal_Rela
*irelaend
, *irelamid
;
10582 bfd_vma last_offset
;
10583 struct elf_link_hash_entry
**rel_hash
;
10584 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
10585 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
10586 unsigned int next_erel
;
10587 bfd_boolean rela_normal
;
10588 struct bfd_elf_section_data
*esdi
, *esdo
;
10590 esdi
= elf_section_data (o
);
10591 esdo
= elf_section_data (o
->output_section
);
10592 rela_normal
= FALSE
;
10594 /* Adjust the reloc addresses and symbol indices. */
10596 irela
= internal_relocs
;
10597 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10598 rel_hash
= esdo
->rel
.hashes
+ esdo
->rel
.count
;
10599 /* We start processing the REL relocs, if any. When we reach
10600 IRELAMID in the loop, we switch to the RELA relocs. */
10602 if (esdi
->rel
.hdr
!= NULL
)
10603 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
10604 * bed
->s
->int_rels_per_ext_rel
);
10605 rel_hash_list
= rel_hash
;
10606 rela_hash_list
= NULL
;
10607 last_offset
= o
->output_offset
;
10608 if (!bfd_link_relocatable (flinfo
->info
))
10609 last_offset
+= o
->output_section
->vma
;
10610 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
10612 unsigned long r_symndx
;
10614 Elf_Internal_Sym sym
;
10616 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
10622 if (irela
== irelamid
)
10624 rel_hash
= esdo
->rela
.hashes
+ esdo
->rela
.count
;
10625 rela_hash_list
= rel_hash
;
10626 rela_normal
= bed
->rela_normal
;
10629 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
10632 if (irela
->r_offset
>= (bfd_vma
) -2)
10634 /* This is a reloc for a deleted entry or somesuch.
10635 Turn it into an R_*_NONE reloc, at the same
10636 offset as the last reloc. elf_eh_frame.c and
10637 bfd_elf_discard_info rely on reloc offsets
10639 irela
->r_offset
= last_offset
;
10641 irela
->r_addend
= 0;
10645 irela
->r_offset
+= o
->output_offset
;
10647 /* Relocs in an executable have to be virtual addresses. */
10648 if (!bfd_link_relocatable (flinfo
->info
))
10649 irela
->r_offset
+= o
->output_section
->vma
;
10651 last_offset
= irela
->r_offset
;
10653 r_symndx
= irela
->r_info
>> r_sym_shift
;
10654 if (r_symndx
== STN_UNDEF
)
10657 if (r_symndx
>= locsymcount
10658 || (elf_bad_symtab (input_bfd
)
10659 && flinfo
->sections
[r_symndx
] == NULL
))
10661 struct elf_link_hash_entry
*rh
;
10662 unsigned long indx
;
10664 /* This is a reloc against a global symbol. We
10665 have not yet output all the local symbols, so
10666 we do not know the symbol index of any global
10667 symbol. We set the rel_hash entry for this
10668 reloc to point to the global hash table entry
10669 for this symbol. The symbol index is then
10670 set at the end of bfd_elf_final_link. */
10671 indx
= r_symndx
- extsymoff
;
10672 rh
= elf_sym_hashes (input_bfd
)[indx
];
10673 while (rh
->root
.type
== bfd_link_hash_indirect
10674 || rh
->root
.type
== bfd_link_hash_warning
)
10675 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
10677 /* Setting the index to -2 tells
10678 elf_link_output_extsym that this symbol is
10679 used by a reloc. */
10680 BFD_ASSERT (rh
->indx
< 0);
10688 /* This is a reloc against a local symbol. */
10691 sym
= isymbuf
[r_symndx
];
10692 sec
= flinfo
->sections
[r_symndx
];
10693 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
10695 /* I suppose the backend ought to fill in the
10696 section of any STT_SECTION symbol against a
10697 processor specific section. */
10698 r_symndx
= STN_UNDEF
;
10699 if (bfd_is_abs_section (sec
))
10701 else if (sec
== NULL
|| sec
->owner
== NULL
)
10703 bfd_set_error (bfd_error_bad_value
);
10708 asection
*osec
= sec
->output_section
;
10710 /* If we have discarded a section, the output
10711 section will be the absolute section. In
10712 case of discarded SEC_MERGE sections, use
10713 the kept section. relocate_section should
10714 have already handled discarded linkonce
10716 if (bfd_is_abs_section (osec
)
10717 && sec
->kept_section
!= NULL
10718 && sec
->kept_section
->output_section
!= NULL
)
10720 osec
= sec
->kept_section
->output_section
;
10721 irela
->r_addend
-= osec
->vma
;
10724 if (!bfd_is_abs_section (osec
))
10726 r_symndx
= osec
->target_index
;
10727 if (r_symndx
== STN_UNDEF
)
10729 irela
->r_addend
+= osec
->vma
;
10730 osec
= _bfd_nearby_section (output_bfd
, osec
,
10732 irela
->r_addend
-= osec
->vma
;
10733 r_symndx
= osec
->target_index
;
10738 /* Adjust the addend according to where the
10739 section winds up in the output section. */
10741 irela
->r_addend
+= sec
->output_offset
;
10745 if (flinfo
->indices
[r_symndx
] == -1)
10747 unsigned long shlink
;
10752 if (flinfo
->info
->strip
== strip_all
)
10754 /* You can't do ld -r -s. */
10755 bfd_set_error (bfd_error_invalid_operation
);
10759 /* This symbol was skipped earlier, but
10760 since it is needed by a reloc, we
10761 must output it now. */
10762 shlink
= symtab_hdr
->sh_link
;
10763 name
= (bfd_elf_string_from_elf_section
10764 (input_bfd
, shlink
, sym
.st_name
));
10768 osec
= sec
->output_section
;
10770 _bfd_elf_section_from_bfd_section (output_bfd
,
10772 if (sym
.st_shndx
== SHN_BAD
)
10775 sym
.st_value
+= sec
->output_offset
;
10776 if (!bfd_link_relocatable (flinfo
->info
))
10778 sym
.st_value
+= osec
->vma
;
10779 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
10781 /* STT_TLS symbols are relative to PT_TLS
10783 BFD_ASSERT (elf_hash_table (flinfo
->info
)
10784 ->tls_sec
!= NULL
);
10785 sym
.st_value
-= (elf_hash_table (flinfo
->info
)
10790 indx
= bfd_get_symcount (output_bfd
);
10791 ret
= elf_link_output_symstrtab (flinfo
, name
,
10797 flinfo
->indices
[r_symndx
] = indx
;
10802 r_symndx
= flinfo
->indices
[r_symndx
];
10805 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
10806 | (irela
->r_info
& r_type_mask
));
10809 /* Swap out the relocs. */
10810 input_rel_hdr
= esdi
->rel
.hdr
;
10811 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
10813 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10818 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
10819 * bed
->s
->int_rels_per_ext_rel
);
10820 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
10823 input_rela_hdr
= esdi
->rela
.hdr
;
10824 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
10826 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10835 /* Write out the modified section contents. */
10836 if (bed
->elf_backend_write_section
10837 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
10840 /* Section written out. */
10842 else switch (o
->sec_info_type
)
10844 case SEC_INFO_TYPE_STABS
:
10845 if (! (_bfd_write_section_stabs
10847 &elf_hash_table (flinfo
->info
)->stab_info
,
10848 o
, &elf_section_data (o
)->sec_info
, contents
)))
10851 case SEC_INFO_TYPE_MERGE
:
10852 if (! _bfd_write_merged_section (output_bfd
, o
,
10853 elf_section_data (o
)->sec_info
))
10856 case SEC_INFO_TYPE_EH_FRAME
:
10858 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
10863 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10865 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
10873 if (! (o
->flags
& SEC_EXCLUDE
))
10875 file_ptr offset
= (file_ptr
) o
->output_offset
;
10876 bfd_size_type todo
= o
->size
;
10878 offset
*= bfd_octets_per_byte (output_bfd
);
10880 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
10882 /* Reverse-copy input section to output. */
10885 todo
-= address_size
;
10886 if (! bfd_set_section_contents (output_bfd
,
10894 offset
+= address_size
;
10898 else if (! bfd_set_section_contents (output_bfd
,
10912 /* Generate a reloc when linking an ELF file. This is a reloc
10913 requested by the linker, and does not come from any input file. This
10914 is used to build constructor and destructor tables when linking
10918 elf_reloc_link_order (bfd
*output_bfd
,
10919 struct bfd_link_info
*info
,
10920 asection
*output_section
,
10921 struct bfd_link_order
*link_order
)
10923 reloc_howto_type
*howto
;
10927 struct bfd_elf_section_reloc_data
*reldata
;
10928 struct elf_link_hash_entry
**rel_hash_ptr
;
10929 Elf_Internal_Shdr
*rel_hdr
;
10930 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
10931 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
10934 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
10936 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
10939 bfd_set_error (bfd_error_bad_value
);
10943 addend
= link_order
->u
.reloc
.p
->addend
;
10946 reldata
= &esdo
->rel
;
10947 else if (esdo
->rela
.hdr
)
10948 reldata
= &esdo
->rela
;
10955 /* Figure out the symbol index. */
10956 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
10957 if (link_order
->type
== bfd_section_reloc_link_order
)
10959 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
10960 BFD_ASSERT (indx
!= 0);
10961 *rel_hash_ptr
= NULL
;
10965 struct elf_link_hash_entry
*h
;
10967 /* Treat a reloc against a defined symbol as though it were
10968 actually against the section. */
10969 h
= ((struct elf_link_hash_entry
*)
10970 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
10971 link_order
->u
.reloc
.p
->u
.name
,
10972 FALSE
, FALSE
, TRUE
));
10974 && (h
->root
.type
== bfd_link_hash_defined
10975 || h
->root
.type
== bfd_link_hash_defweak
))
10979 section
= h
->root
.u
.def
.section
;
10980 indx
= section
->output_section
->target_index
;
10981 *rel_hash_ptr
= NULL
;
10982 /* It seems that we ought to add the symbol value to the
10983 addend here, but in practice it has already been added
10984 because it was passed to constructor_callback. */
10985 addend
+= section
->output_section
->vma
+ section
->output_offset
;
10987 else if (h
!= NULL
)
10989 /* Setting the index to -2 tells elf_link_output_extsym that
10990 this symbol is used by a reloc. */
10997 (*info
->callbacks
->unattached_reloc
)
10998 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
11003 /* If this is an inplace reloc, we must write the addend into the
11005 if (howto
->partial_inplace
&& addend
!= 0)
11007 bfd_size_type size
;
11008 bfd_reloc_status_type rstat
;
11011 const char *sym_name
;
11013 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
11014 buf
= (bfd_byte
*) bfd_zmalloc (size
);
11015 if (buf
== NULL
&& size
!= 0)
11017 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
11024 case bfd_reloc_outofrange
:
11027 case bfd_reloc_overflow
:
11028 if (link_order
->type
== bfd_section_reloc_link_order
)
11029 sym_name
= bfd_section_name (output_bfd
,
11030 link_order
->u
.reloc
.p
->u
.section
);
11032 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
11033 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
11034 howto
->name
, addend
, NULL
, NULL
,
11039 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
11041 * bfd_octets_per_byte (output_bfd
),
11048 /* The address of a reloc is relative to the section in a
11049 relocatable file, and is a virtual address in an executable
11051 offset
= link_order
->offset
;
11052 if (! bfd_link_relocatable (info
))
11053 offset
+= output_section
->vma
;
11055 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
11057 irel
[i
].r_offset
= offset
;
11058 irel
[i
].r_info
= 0;
11059 irel
[i
].r_addend
= 0;
11061 if (bed
->s
->arch_size
== 32)
11062 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
11064 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
11066 rel_hdr
= reldata
->hdr
;
11067 erel
= rel_hdr
->contents
;
11068 if (rel_hdr
->sh_type
== SHT_REL
)
11070 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
11071 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
11075 irel
[0].r_addend
= addend
;
11076 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
11077 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
11086 /* Get the output vma of the section pointed to by the sh_link field. */
11089 elf_get_linked_section_vma (struct bfd_link_order
*p
)
11091 Elf_Internal_Shdr
**elf_shdrp
;
11095 s
= p
->u
.indirect
.section
;
11096 elf_shdrp
= elf_elfsections (s
->owner
);
11097 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
11098 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
11100 The Intel C compiler generates SHT_IA_64_UNWIND with
11101 SHF_LINK_ORDER. But it doesn't set the sh_link or
11102 sh_info fields. Hence we could get the situation
11103 where elfsec is 0. */
11106 const struct elf_backend_data
*bed
11107 = get_elf_backend_data (s
->owner
);
11108 if (bed
->link_order_error_handler
)
11109 bed
->link_order_error_handler
11110 /* xgettext:c-format */
11111 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
11116 s
= elf_shdrp
[elfsec
]->bfd_section
;
11117 return s
->output_section
->vma
+ s
->output_offset
;
11122 /* Compare two sections based on the locations of the sections they are
11123 linked to. Used by elf_fixup_link_order. */
11126 compare_link_order (const void * a
, const void * b
)
11131 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
11132 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
11135 return apos
> bpos
;
11139 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11140 order as their linked sections. Returns false if this could not be done
11141 because an output section includes both ordered and unordered
11142 sections. Ideally we'd do this in the linker proper. */
11145 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
11147 int seen_linkorder
;
11150 struct bfd_link_order
*p
;
11152 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11154 struct bfd_link_order
**sections
;
11155 asection
*s
, *other_sec
, *linkorder_sec
;
11159 linkorder_sec
= NULL
;
11161 seen_linkorder
= 0;
11162 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11164 if (p
->type
== bfd_indirect_link_order
)
11166 s
= p
->u
.indirect
.section
;
11168 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
11169 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
11170 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
11171 && elfsec
< elf_numsections (sub
)
11172 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
11173 && elf_elfsections (sub
)[elfsec
]->sh_link
< elf_numsections (sub
))
11187 if (seen_other
&& seen_linkorder
)
11189 if (other_sec
&& linkorder_sec
)
11191 /* xgettext:c-format */
11192 (_("%A has both ordered [`%A' in %B] "
11193 "and unordered [`%A' in %B] sections"),
11194 o
, linkorder_sec
, linkorder_sec
->owner
,
11195 other_sec
, other_sec
->owner
);
11198 (_("%A has both ordered and unordered sections"), o
);
11199 bfd_set_error (bfd_error_bad_value
);
11204 if (!seen_linkorder
)
11207 sections
= (struct bfd_link_order
**)
11208 bfd_malloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
11209 if (sections
== NULL
)
11211 seen_linkorder
= 0;
11213 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11215 sections
[seen_linkorder
++] = p
;
11217 /* Sort the input sections in the order of their linked section. */
11218 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
11219 compare_link_order
);
11221 /* Change the offsets of the sections. */
11223 for (n
= 0; n
< seen_linkorder
; n
++)
11225 s
= sections
[n
]->u
.indirect
.section
;
11226 offset
&= ~(bfd_vma
) 0 << s
->alignment_power
;
11227 s
->output_offset
= offset
/ bfd_octets_per_byte (abfd
);
11228 sections
[n
]->offset
= offset
;
11229 offset
+= sections
[n
]->size
;
11236 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11237 Returns TRUE upon success, FALSE otherwise. */
11240 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
11242 bfd_boolean ret
= FALSE
;
11244 const struct elf_backend_data
*bed
;
11246 enum bfd_architecture arch
;
11248 asymbol
**sympp
= NULL
;
11252 elf_symbol_type
*osymbuf
;
11254 implib_bfd
= info
->out_implib_bfd
;
11255 bed
= get_elf_backend_data (abfd
);
11257 if (!bfd_set_format (implib_bfd
, bfd_object
))
11260 flags
= bfd_get_file_flags (abfd
);
11261 flags
&= ~HAS_RELOC
;
11262 if (!bfd_set_start_address (implib_bfd
, 0)
11263 || !bfd_set_file_flags (implib_bfd
, flags
))
11266 /* Copy architecture of output file to import library file. */
11267 arch
= bfd_get_arch (abfd
);
11268 mach
= bfd_get_mach (abfd
);
11269 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
11270 && (abfd
->target_defaulted
11271 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
11274 /* Get symbol table size. */
11275 symsize
= bfd_get_symtab_upper_bound (abfd
);
11279 /* Read in the symbol table. */
11280 sympp
= (asymbol
**) xmalloc (symsize
);
11281 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
11285 /* Allow the BFD backend to copy any private header data it
11286 understands from the output BFD to the import library BFD. */
11287 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
11290 /* Filter symbols to appear in the import library. */
11291 if (bed
->elf_backend_filter_implib_symbols
)
11292 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
11295 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
11298 bfd_set_error (bfd_error_no_symbols
);
11299 _bfd_error_handler (_("%B: no symbol found for import library"),
11305 /* Make symbols absolute. */
11306 osymbuf
= (elf_symbol_type
*) bfd_alloc2 (implib_bfd
, symcount
,
11307 sizeof (*osymbuf
));
11308 for (src_count
= 0; src_count
< symcount
; src_count
++)
11310 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
11311 sizeof (*osymbuf
));
11312 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
11313 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
11314 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
11315 osymbuf
[src_count
].internal_elf_sym
.st_value
=
11316 osymbuf
[src_count
].symbol
.value
;
11317 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
11320 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
11322 /* Allow the BFD backend to copy any private data it understands
11323 from the output BFD to the import library BFD. This is done last
11324 to permit the routine to look at the filtered symbol table. */
11325 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
11328 if (!bfd_close (implib_bfd
))
11339 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
11343 if (flinfo
->symstrtab
!= NULL
)
11344 _bfd_elf_strtab_free (flinfo
->symstrtab
);
11345 if (flinfo
->contents
!= NULL
)
11346 free (flinfo
->contents
);
11347 if (flinfo
->external_relocs
!= NULL
)
11348 free (flinfo
->external_relocs
);
11349 if (flinfo
->internal_relocs
!= NULL
)
11350 free (flinfo
->internal_relocs
);
11351 if (flinfo
->external_syms
!= NULL
)
11352 free (flinfo
->external_syms
);
11353 if (flinfo
->locsym_shndx
!= NULL
)
11354 free (flinfo
->locsym_shndx
);
11355 if (flinfo
->internal_syms
!= NULL
)
11356 free (flinfo
->internal_syms
);
11357 if (flinfo
->indices
!= NULL
)
11358 free (flinfo
->indices
);
11359 if (flinfo
->sections
!= NULL
)
11360 free (flinfo
->sections
);
11361 if (flinfo
->symshndxbuf
!= NULL
)
11362 free (flinfo
->symshndxbuf
);
11363 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
11365 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11366 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rel
.hashes
!= NULL
)
11367 free (esdo
->rel
.hashes
);
11368 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rela
.hashes
!= NULL
)
11369 free (esdo
->rela
.hashes
);
11373 /* Do the final step of an ELF link. */
11376 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
11378 bfd_boolean dynamic
;
11379 bfd_boolean emit_relocs
;
11381 struct elf_final_link_info flinfo
;
11383 struct bfd_link_order
*p
;
11385 bfd_size_type max_contents_size
;
11386 bfd_size_type max_external_reloc_size
;
11387 bfd_size_type max_internal_reloc_count
;
11388 bfd_size_type max_sym_count
;
11389 bfd_size_type max_sym_shndx_count
;
11390 Elf_Internal_Sym elfsym
;
11392 Elf_Internal_Shdr
*symtab_hdr
;
11393 Elf_Internal_Shdr
*symtab_shndx_hdr
;
11394 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11395 struct elf_outext_info eoinfo
;
11396 bfd_boolean merged
;
11397 size_t relativecount
= 0;
11398 asection
*reldyn
= 0;
11400 asection
*attr_section
= NULL
;
11401 bfd_vma attr_size
= 0;
11402 const char *std_attrs_section
;
11403 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
11405 if (!is_elf_hash_table (htab
))
11408 if (bfd_link_pic (info
))
11409 abfd
->flags
|= DYNAMIC
;
11411 dynamic
= htab
->dynamic_sections_created
;
11412 dynobj
= htab
->dynobj
;
11414 emit_relocs
= (bfd_link_relocatable (info
)
11415 || info
->emitrelocations
);
11417 flinfo
.info
= info
;
11418 flinfo
.output_bfd
= abfd
;
11419 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
11420 if (flinfo
.symstrtab
== NULL
)
11425 flinfo
.hash_sec
= NULL
;
11426 flinfo
.symver_sec
= NULL
;
11430 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
11431 /* Note that dynsym_sec can be NULL (on VMS). */
11432 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
11433 /* Note that it is OK if symver_sec is NULL. */
11436 flinfo
.contents
= NULL
;
11437 flinfo
.external_relocs
= NULL
;
11438 flinfo
.internal_relocs
= NULL
;
11439 flinfo
.external_syms
= NULL
;
11440 flinfo
.locsym_shndx
= NULL
;
11441 flinfo
.internal_syms
= NULL
;
11442 flinfo
.indices
= NULL
;
11443 flinfo
.sections
= NULL
;
11444 flinfo
.symshndxbuf
= NULL
;
11445 flinfo
.filesym_count
= 0;
11447 /* The object attributes have been merged. Remove the input
11448 sections from the link, and set the contents of the output
11450 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
11451 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11453 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
11454 || strcmp (o
->name
, ".gnu.attributes") == 0)
11456 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11458 asection
*input_section
;
11460 if (p
->type
!= bfd_indirect_link_order
)
11462 input_section
= p
->u
.indirect
.section
;
11463 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11464 elf_link_input_bfd ignores this section. */
11465 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
11468 attr_size
= bfd_elf_obj_attr_size (abfd
);
11471 bfd_set_section_size (abfd
, o
, attr_size
);
11473 /* Skip this section later on. */
11474 o
->map_head
.link_order
= NULL
;
11477 o
->flags
|= SEC_EXCLUDE
;
11481 /* Count up the number of relocations we will output for each output
11482 section, so that we know the sizes of the reloc sections. We
11483 also figure out some maximum sizes. */
11484 max_contents_size
= 0;
11485 max_external_reloc_size
= 0;
11486 max_internal_reloc_count
= 0;
11488 max_sym_shndx_count
= 0;
11490 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11492 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11493 o
->reloc_count
= 0;
11495 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11497 unsigned int reloc_count
= 0;
11498 unsigned int additional_reloc_count
= 0;
11499 struct bfd_elf_section_data
*esdi
= NULL
;
11501 if (p
->type
== bfd_section_reloc_link_order
11502 || p
->type
== bfd_symbol_reloc_link_order
)
11504 else if (p
->type
== bfd_indirect_link_order
)
11508 sec
= p
->u
.indirect
.section
;
11510 /* Mark all sections which are to be included in the
11511 link. This will normally be every section. We need
11512 to do this so that we can identify any sections which
11513 the linker has decided to not include. */
11514 sec
->linker_mark
= TRUE
;
11516 if (sec
->flags
& SEC_MERGE
)
11519 if (sec
->rawsize
> max_contents_size
)
11520 max_contents_size
= sec
->rawsize
;
11521 if (sec
->size
> max_contents_size
)
11522 max_contents_size
= sec
->size
;
11524 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
11525 && (sec
->owner
->flags
& DYNAMIC
) == 0)
11529 /* We are interested in just local symbols, not all
11531 if (elf_bad_symtab (sec
->owner
))
11532 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
11533 / bed
->s
->sizeof_sym
);
11535 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
11537 if (sym_count
> max_sym_count
)
11538 max_sym_count
= sym_count
;
11540 if (sym_count
> max_sym_shndx_count
11541 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
11542 max_sym_shndx_count
= sym_count
;
11544 if (esdo
->this_hdr
.sh_type
== SHT_REL
11545 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
11546 /* Some backends use reloc_count in relocation sections
11547 to count particular types of relocs. Of course,
11548 reloc sections themselves can't have relocations. */
11550 else if (emit_relocs
)
11552 reloc_count
= sec
->reloc_count
;
11553 if (bed
->elf_backend_count_additional_relocs
)
11556 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
11557 additional_reloc_count
+= c
;
11560 else if (bed
->elf_backend_count_relocs
)
11561 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
11563 esdi
= elf_section_data (sec
);
11565 if ((sec
->flags
& SEC_RELOC
) != 0)
11567 size_t ext_size
= 0;
11569 if (esdi
->rel
.hdr
!= NULL
)
11570 ext_size
= esdi
->rel
.hdr
->sh_size
;
11571 if (esdi
->rela
.hdr
!= NULL
)
11572 ext_size
+= esdi
->rela
.hdr
->sh_size
;
11574 if (ext_size
> max_external_reloc_size
)
11575 max_external_reloc_size
= ext_size
;
11576 if (sec
->reloc_count
> max_internal_reloc_count
)
11577 max_internal_reloc_count
= sec
->reloc_count
;
11582 if (reloc_count
== 0)
11585 reloc_count
+= additional_reloc_count
;
11586 o
->reloc_count
+= reloc_count
;
11588 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
11592 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
11593 esdo
->rel
.count
+= additional_reloc_count
;
11595 if (esdi
->rela
.hdr
)
11597 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
11598 esdo
->rela
.count
+= additional_reloc_count
;
11604 esdo
->rela
.count
+= reloc_count
;
11606 esdo
->rel
.count
+= reloc_count
;
11610 if (o
->reloc_count
> 0)
11611 o
->flags
|= SEC_RELOC
;
11614 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11615 set it (this is probably a bug) and if it is set
11616 assign_section_numbers will create a reloc section. */
11617 o
->flags
&=~ SEC_RELOC
;
11620 /* If the SEC_ALLOC flag is not set, force the section VMA to
11621 zero. This is done in elf_fake_sections as well, but forcing
11622 the VMA to 0 here will ensure that relocs against these
11623 sections are handled correctly. */
11624 if ((o
->flags
& SEC_ALLOC
) == 0
11625 && ! o
->user_set_vma
)
11629 if (! bfd_link_relocatable (info
) && merged
)
11630 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
11632 /* Figure out the file positions for everything but the symbol table
11633 and the relocs. We set symcount to force assign_section_numbers
11634 to create a symbol table. */
11635 bfd_get_symcount (abfd
) = info
->strip
!= strip_all
|| emit_relocs
;
11636 BFD_ASSERT (! abfd
->output_has_begun
);
11637 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
11640 /* Set sizes, and assign file positions for reloc sections. */
11641 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11643 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11644 if ((o
->flags
& SEC_RELOC
) != 0)
11647 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
11651 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
11655 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11656 to count upwards while actually outputting the relocations. */
11657 esdo
->rel
.count
= 0;
11658 esdo
->rela
.count
= 0;
11660 if (esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
11662 /* Cache the section contents so that they can be compressed
11663 later. Use bfd_malloc since it will be freed by
11664 bfd_compress_section_contents. */
11665 unsigned char *contents
= esdo
->this_hdr
.contents
;
11666 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
11669 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
11670 if (contents
== NULL
)
11672 esdo
->this_hdr
.contents
= contents
;
11676 /* We have now assigned file positions for all the sections except
11677 .symtab, .strtab, and non-loaded reloc sections. We start the
11678 .symtab section at the current file position, and write directly
11679 to it. We build the .strtab section in memory. */
11680 bfd_get_symcount (abfd
) = 0;
11681 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11682 /* sh_name is set in prep_headers. */
11683 symtab_hdr
->sh_type
= SHT_SYMTAB
;
11684 /* sh_flags, sh_addr and sh_size all start off zero. */
11685 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
11686 /* sh_link is set in assign_section_numbers. */
11687 /* sh_info is set below. */
11688 /* sh_offset is set just below. */
11689 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
11691 if (max_sym_count
< 20)
11692 max_sym_count
= 20;
11693 htab
->strtabsize
= max_sym_count
;
11694 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
11695 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
11696 if (htab
->strtab
== NULL
)
11698 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11700 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
11701 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
11703 if (info
->strip
!= strip_all
|| emit_relocs
)
11705 file_ptr off
= elf_next_file_pos (abfd
);
11707 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
11709 /* Note that at this point elf_next_file_pos (abfd) is
11710 incorrect. We do not yet know the size of the .symtab section.
11711 We correct next_file_pos below, after we do know the size. */
11713 /* Start writing out the symbol table. The first symbol is always a
11715 elfsym
.st_value
= 0;
11716 elfsym
.st_size
= 0;
11717 elfsym
.st_info
= 0;
11718 elfsym
.st_other
= 0;
11719 elfsym
.st_shndx
= SHN_UNDEF
;
11720 elfsym
.st_target_internal
= 0;
11721 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
11722 bfd_und_section_ptr
, NULL
) != 1)
11725 /* Output a symbol for each section. We output these even if we are
11726 discarding local symbols, since they are used for relocs. These
11727 symbols have no names. We store the index of each one in the
11728 index field of the section, so that we can find it again when
11729 outputting relocs. */
11731 elfsym
.st_size
= 0;
11732 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
11733 elfsym
.st_other
= 0;
11734 elfsym
.st_value
= 0;
11735 elfsym
.st_target_internal
= 0;
11736 for (i
= 1; i
< elf_numsections (abfd
); i
++)
11738 o
= bfd_section_from_elf_index (abfd
, i
);
11741 o
->target_index
= bfd_get_symcount (abfd
);
11742 elfsym
.st_shndx
= i
;
11743 if (!bfd_link_relocatable (info
))
11744 elfsym
.st_value
= o
->vma
;
11745 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
, o
,
11752 /* Allocate some memory to hold information read in from the input
11754 if (max_contents_size
!= 0)
11756 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
11757 if (flinfo
.contents
== NULL
)
11761 if (max_external_reloc_size
!= 0)
11763 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
11764 if (flinfo
.external_relocs
== NULL
)
11768 if (max_internal_reloc_count
!= 0)
11770 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
11771 amt
*= sizeof (Elf_Internal_Rela
);
11772 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
11773 if (flinfo
.internal_relocs
== NULL
)
11777 if (max_sym_count
!= 0)
11779 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
11780 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
11781 if (flinfo
.external_syms
== NULL
)
11784 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
11785 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
11786 if (flinfo
.internal_syms
== NULL
)
11789 amt
= max_sym_count
* sizeof (long);
11790 flinfo
.indices
= (long int *) bfd_malloc (amt
);
11791 if (flinfo
.indices
== NULL
)
11794 amt
= max_sym_count
* sizeof (asection
*);
11795 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
11796 if (flinfo
.sections
== NULL
)
11800 if (max_sym_shndx_count
!= 0)
11802 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
11803 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
11804 if (flinfo
.locsym_shndx
== NULL
)
11810 bfd_vma base
, end
= 0;
11813 for (sec
= htab
->tls_sec
;
11814 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
11817 bfd_size_type size
= sec
->size
;
11820 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
11822 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
11825 size
= ord
->offset
+ ord
->size
;
11827 end
= sec
->vma
+ size
;
11829 base
= htab
->tls_sec
->vma
;
11830 /* Only align end of TLS section if static TLS doesn't have special
11831 alignment requirements. */
11832 if (bed
->static_tls_alignment
== 1)
11833 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
11834 htab
->tls_size
= end
- base
;
11837 /* Reorder SHF_LINK_ORDER sections. */
11838 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11840 if (!elf_fixup_link_order (abfd
, o
))
11844 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
11847 /* Since ELF permits relocations to be against local symbols, we
11848 must have the local symbols available when we do the relocations.
11849 Since we would rather only read the local symbols once, and we
11850 would rather not keep them in memory, we handle all the
11851 relocations for a single input file at the same time.
11853 Unfortunately, there is no way to know the total number of local
11854 symbols until we have seen all of them, and the local symbol
11855 indices precede the global symbol indices. This means that when
11856 we are generating relocatable output, and we see a reloc against
11857 a global symbol, we can not know the symbol index until we have
11858 finished examining all the local symbols to see which ones we are
11859 going to output. To deal with this, we keep the relocations in
11860 memory, and don't output them until the end of the link. This is
11861 an unfortunate waste of memory, but I don't see a good way around
11862 it. Fortunately, it only happens when performing a relocatable
11863 link, which is not the common case. FIXME: If keep_memory is set
11864 we could write the relocs out and then read them again; I don't
11865 know how bad the memory loss will be. */
11867 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11868 sub
->output_has_begun
= FALSE
;
11869 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11871 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11873 if (p
->type
== bfd_indirect_link_order
11874 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
11875 == bfd_target_elf_flavour
)
11876 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
11878 if (! sub
->output_has_begun
)
11880 if (! elf_link_input_bfd (&flinfo
, sub
))
11882 sub
->output_has_begun
= TRUE
;
11885 else if (p
->type
== bfd_section_reloc_link_order
11886 || p
->type
== bfd_symbol_reloc_link_order
)
11888 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
11893 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
11895 if (p
->type
== bfd_indirect_link_order
11896 && (bfd_get_flavour (sub
)
11897 == bfd_target_elf_flavour
)
11898 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
11899 != bed
->s
->elfclass
))
11901 const char *iclass
, *oclass
;
11903 switch (bed
->s
->elfclass
)
11905 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
11906 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
11907 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
11911 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
11913 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
11914 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
11915 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
11919 bfd_set_error (bfd_error_wrong_format
);
11921 /* xgettext:c-format */
11922 (_("%B: file class %s incompatible with %s"),
11923 sub
, iclass
, oclass
);
11932 /* Free symbol buffer if needed. */
11933 if (!info
->reduce_memory_overheads
)
11935 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11936 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
11937 && elf_tdata (sub
)->symbuf
)
11939 free (elf_tdata (sub
)->symbuf
);
11940 elf_tdata (sub
)->symbuf
= NULL
;
11944 /* Output any global symbols that got converted to local in a
11945 version script or due to symbol visibility. We do this in a
11946 separate step since ELF requires all local symbols to appear
11947 prior to any global symbols. FIXME: We should only do this if
11948 some global symbols were, in fact, converted to become local.
11949 FIXME: Will this work correctly with the Irix 5 linker? */
11950 eoinfo
.failed
= FALSE
;
11951 eoinfo
.flinfo
= &flinfo
;
11952 eoinfo
.localsyms
= TRUE
;
11953 eoinfo
.file_sym_done
= FALSE
;
11954 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
11958 /* If backend needs to output some local symbols not present in the hash
11959 table, do it now. */
11960 if (bed
->elf_backend_output_arch_local_syms
11961 && (info
->strip
!= strip_all
|| emit_relocs
))
11963 typedef int (*out_sym_func
)
11964 (void *, const char *, Elf_Internal_Sym
*, asection
*,
11965 struct elf_link_hash_entry
*);
11967 if (! ((*bed
->elf_backend_output_arch_local_syms
)
11968 (abfd
, info
, &flinfo
,
11969 (out_sym_func
) elf_link_output_symstrtab
)))
11973 /* That wrote out all the local symbols. Finish up the symbol table
11974 with the global symbols. Even if we want to strip everything we
11975 can, we still need to deal with those global symbols that got
11976 converted to local in a version script. */
11978 /* The sh_info field records the index of the first non local symbol. */
11979 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
11982 && htab
->dynsym
!= NULL
11983 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
11985 Elf_Internal_Sym sym
;
11986 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
11988 o
= htab
->dynsym
->output_section
;
11989 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
11991 /* Write out the section symbols for the output sections. */
11992 if (bfd_link_pic (info
)
11993 || htab
->is_relocatable_executable
)
11999 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12001 sym
.st_target_internal
= 0;
12003 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
12009 dynindx
= elf_section_data (s
)->dynindx
;
12012 indx
= elf_section_data (s
)->this_idx
;
12013 BFD_ASSERT (indx
> 0);
12014 sym
.st_shndx
= indx
;
12015 if (! check_dynsym (abfd
, &sym
))
12017 sym
.st_value
= s
->vma
;
12018 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
12019 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12023 /* Write out the local dynsyms. */
12024 if (htab
->dynlocal
)
12026 struct elf_link_local_dynamic_entry
*e
;
12027 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
12032 /* Copy the internal symbol and turn off visibility.
12033 Note that we saved a word of storage and overwrote
12034 the original st_name with the dynstr_index. */
12036 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
12038 s
= bfd_section_from_elf_index (e
->input_bfd
,
12043 elf_section_data (s
->output_section
)->this_idx
;
12044 if (! check_dynsym (abfd
, &sym
))
12046 sym
.st_value
= (s
->output_section
->vma
12048 + e
->isym
.st_value
);
12051 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
12052 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12057 /* We get the global symbols from the hash table. */
12058 eoinfo
.failed
= FALSE
;
12059 eoinfo
.localsyms
= FALSE
;
12060 eoinfo
.flinfo
= &flinfo
;
12061 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12065 /* If backend needs to output some symbols not present in the hash
12066 table, do it now. */
12067 if (bed
->elf_backend_output_arch_syms
12068 && (info
->strip
!= strip_all
|| emit_relocs
))
12070 typedef int (*out_sym_func
)
12071 (void *, const char *, Elf_Internal_Sym
*, asection
*,
12072 struct elf_link_hash_entry
*);
12074 if (! ((*bed
->elf_backend_output_arch_syms
)
12075 (abfd
, info
, &flinfo
,
12076 (out_sym_func
) elf_link_output_symstrtab
)))
12080 /* Finalize the .strtab section. */
12081 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
12083 /* Swap out the .strtab section. */
12084 if (!elf_link_swap_symbols_out (&flinfo
))
12087 /* Now we know the size of the symtab section. */
12088 if (bfd_get_symcount (abfd
) > 0)
12090 /* Finish up and write out the symbol string table (.strtab)
12092 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
12093 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
12095 if (elf_symtab_shndx_list (abfd
))
12097 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
12099 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
12101 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
12102 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
12103 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
12104 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
12105 symtab_shndx_hdr
->sh_size
= amt
;
12107 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
12110 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
12111 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
12116 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
12117 /* sh_name was set in prep_headers. */
12118 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
12119 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
12120 symstrtab_hdr
->sh_addr
= 0;
12121 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
12122 symstrtab_hdr
->sh_entsize
= 0;
12123 symstrtab_hdr
->sh_link
= 0;
12124 symstrtab_hdr
->sh_info
= 0;
12125 /* sh_offset is set just below. */
12126 symstrtab_hdr
->sh_addralign
= 1;
12128 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
12130 elf_next_file_pos (abfd
) = off
;
12132 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
12133 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
12137 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
12139 _bfd_error_handler (_("%B: failed to generate import library"),
12140 info
->out_implib_bfd
);
12144 /* Adjust the relocs to have the correct symbol indices. */
12145 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12147 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12149 if ((o
->flags
& SEC_RELOC
) == 0)
12152 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
12153 if (esdo
->rel
.hdr
!= NULL
12154 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
))
12156 if (esdo
->rela
.hdr
!= NULL
12157 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
))
12160 /* Set the reloc_count field to 0 to prevent write_relocs from
12161 trying to swap the relocs out itself. */
12162 o
->reloc_count
= 0;
12165 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
12166 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
12168 /* If we are linking against a dynamic object, or generating a
12169 shared library, finish up the dynamic linking information. */
12172 bfd_byte
*dyncon
, *dynconend
;
12174 /* Fix up .dynamic entries. */
12175 o
= bfd_get_linker_section (dynobj
, ".dynamic");
12176 BFD_ASSERT (o
!= NULL
);
12178 dyncon
= o
->contents
;
12179 dynconend
= o
->contents
+ o
->size
;
12180 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12182 Elf_Internal_Dyn dyn
;
12185 bfd_size_type sh_size
;
12188 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12195 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
12197 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
12199 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
12200 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
12203 dyn
.d_un
.d_val
= relativecount
;
12210 name
= info
->init_function
;
12213 name
= info
->fini_function
;
12216 struct elf_link_hash_entry
*h
;
12218 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
12220 && (h
->root
.type
== bfd_link_hash_defined
12221 || h
->root
.type
== bfd_link_hash_defweak
))
12223 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
12224 o
= h
->root
.u
.def
.section
;
12225 if (o
->output_section
!= NULL
)
12226 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
12227 + o
->output_offset
);
12230 /* The symbol is imported from another shared
12231 library and does not apply to this one. */
12232 dyn
.d_un
.d_ptr
= 0;
12239 case DT_PREINIT_ARRAYSZ
:
12240 name
= ".preinit_array";
12242 case DT_INIT_ARRAYSZ
:
12243 name
= ".init_array";
12245 case DT_FINI_ARRAYSZ
:
12246 name
= ".fini_array";
12248 o
= bfd_get_section_by_name (abfd
, name
);
12252 (_("could not find section %s"), name
);
12257 (_("warning: %s section has zero size"), name
);
12258 dyn
.d_un
.d_val
= o
->size
;
12261 case DT_PREINIT_ARRAY
:
12262 name
= ".preinit_array";
12264 case DT_INIT_ARRAY
:
12265 name
= ".init_array";
12267 case DT_FINI_ARRAY
:
12268 name
= ".fini_array";
12270 o
= bfd_get_section_by_name (abfd
, name
);
12277 name
= ".gnu.hash";
12286 name
= ".gnu.version_d";
12289 name
= ".gnu.version_r";
12292 name
= ".gnu.version";
12294 o
= bfd_get_linker_section (dynobj
, name
);
12299 (_("could not find section %s"), name
);
12302 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
12305 (_("warning: section '%s' is being made into a note"), name
);
12306 bfd_set_error (bfd_error_nonrepresentable_section
);
12309 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
12316 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
12322 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12324 Elf_Internal_Shdr
*hdr
;
12326 hdr
= elf_elfsections (abfd
)[i
];
12327 if (hdr
->sh_type
== type
12328 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
12330 sh_size
+= hdr
->sh_size
;
12332 || sh_addr
> hdr
->sh_addr
)
12333 sh_addr
= hdr
->sh_addr
;
12337 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
12339 /* Don't count procedure linkage table relocs in the
12340 overall reloc count. */
12341 sh_size
-= htab
->srelplt
->size
;
12343 /* If the size is zero, make the address zero too.
12344 This is to avoid a glibc bug. If the backend
12345 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12346 zero, then we'll put DT_RELA at the end of
12347 DT_JMPREL. glibc will interpret the end of
12348 DT_RELA matching the end of DT_JMPREL as the
12349 case where DT_RELA includes DT_JMPREL, and for
12350 LD_BIND_NOW will decide that processing DT_RELA
12351 will process the PLT relocs too. Net result:
12352 No PLT relocs applied. */
12355 /* If .rela.plt is the first .rela section, exclude
12356 it from DT_RELA. */
12357 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
12358 + htab
->srelplt
->output_offset
))
12359 sh_addr
+= htab
->srelplt
->size
;
12362 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
12363 dyn
.d_un
.d_val
= sh_size
;
12365 dyn
.d_un
.d_ptr
= sh_addr
;
12368 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
12372 /* If we have created any dynamic sections, then output them. */
12373 if (dynobj
!= NULL
)
12375 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
12378 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12379 if (((info
->warn_shared_textrel
&& bfd_link_pic (info
))
12380 || info
->error_textrel
)
12381 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
)
12383 bfd_byte
*dyncon
, *dynconend
;
12385 dyncon
= o
->contents
;
12386 dynconend
= o
->contents
+ o
->size
;
12387 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12389 Elf_Internal_Dyn dyn
;
12391 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12393 if (dyn
.d_tag
== DT_TEXTREL
)
12395 if (info
->error_textrel
)
12396 info
->callbacks
->einfo
12397 (_("%P%X: read-only segment has dynamic relocations.\n"));
12399 info
->callbacks
->einfo
12400 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12406 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
12408 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
12410 || o
->output_section
== bfd_abs_section_ptr
)
12412 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
12414 /* At this point, we are only interested in sections
12415 created by _bfd_elf_link_create_dynamic_sections. */
12418 if (htab
->stab_info
.stabstr
== o
)
12420 if (htab
->eh_info
.hdr_sec
== o
)
12422 if (strcmp (o
->name
, ".dynstr") != 0)
12424 if (! bfd_set_section_contents (abfd
, o
->output_section
,
12426 (file_ptr
) o
->output_offset
12427 * bfd_octets_per_byte (abfd
),
12433 /* The contents of the .dynstr section are actually in a
12437 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
12438 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
12439 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
12445 if (bfd_link_relocatable (info
))
12447 bfd_boolean failed
= FALSE
;
12449 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
12454 /* If we have optimized stabs strings, output them. */
12455 if (htab
->stab_info
.stabstr
!= NULL
)
12457 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
12461 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
12464 elf_final_link_free (abfd
, &flinfo
);
12466 elf_linker (abfd
) = TRUE
;
12470 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
12471 if (contents
== NULL
)
12472 return FALSE
; /* Bail out and fail. */
12473 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
12474 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
12481 elf_final_link_free (abfd
, &flinfo
);
12485 /* Initialize COOKIE for input bfd ABFD. */
12488 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
12489 struct bfd_link_info
*info
, bfd
*abfd
)
12491 Elf_Internal_Shdr
*symtab_hdr
;
12492 const struct elf_backend_data
*bed
;
12494 bed
= get_elf_backend_data (abfd
);
12495 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12497 cookie
->abfd
= abfd
;
12498 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
12499 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
12500 if (cookie
->bad_symtab
)
12502 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
12503 cookie
->extsymoff
= 0;
12507 cookie
->locsymcount
= symtab_hdr
->sh_info
;
12508 cookie
->extsymoff
= symtab_hdr
->sh_info
;
12511 if (bed
->s
->arch_size
== 32)
12512 cookie
->r_sym_shift
= 8;
12514 cookie
->r_sym_shift
= 32;
12516 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
12517 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
12519 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
12520 cookie
->locsymcount
, 0,
12522 if (cookie
->locsyms
== NULL
)
12524 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
12527 if (info
->keep_memory
)
12528 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
12533 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12536 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
12538 Elf_Internal_Shdr
*symtab_hdr
;
12540 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12541 if (cookie
->locsyms
!= NULL
12542 && symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
12543 free (cookie
->locsyms
);
12546 /* Initialize the relocation information in COOKIE for input section SEC
12547 of input bfd ABFD. */
12550 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12551 struct bfd_link_info
*info
, bfd
*abfd
,
12554 const struct elf_backend_data
*bed
;
12556 if (sec
->reloc_count
== 0)
12558 cookie
->rels
= NULL
;
12559 cookie
->relend
= NULL
;
12563 bed
= get_elf_backend_data (abfd
);
12565 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
12566 info
->keep_memory
);
12567 if (cookie
->rels
== NULL
)
12569 cookie
->rel
= cookie
->rels
;
12570 cookie
->relend
= (cookie
->rels
12571 + sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
);
12573 cookie
->rel
= cookie
->rels
;
12577 /* Free the memory allocated by init_reloc_cookie_rels,
12581 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12584 if (cookie
->rels
&& elf_section_data (sec
)->relocs
!= cookie
->rels
)
12585 free (cookie
->rels
);
12588 /* Initialize the whole of COOKIE for input section SEC. */
12591 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12592 struct bfd_link_info
*info
,
12595 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
12597 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
12602 fini_reloc_cookie (cookie
, sec
->owner
);
12607 /* Free the memory allocated by init_reloc_cookie_for_section,
12611 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12614 fini_reloc_cookie_rels (cookie
, sec
);
12615 fini_reloc_cookie (cookie
, sec
->owner
);
12618 /* Garbage collect unused sections. */
12620 /* Default gc_mark_hook. */
12623 _bfd_elf_gc_mark_hook (asection
*sec
,
12624 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12625 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
12626 struct elf_link_hash_entry
*h
,
12627 Elf_Internal_Sym
*sym
)
12631 switch (h
->root
.type
)
12633 case bfd_link_hash_defined
:
12634 case bfd_link_hash_defweak
:
12635 return h
->root
.u
.def
.section
;
12637 case bfd_link_hash_common
:
12638 return h
->root
.u
.c
.p
->section
;
12645 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
12650 /* For undefined __start_<name> and __stop_<name> symbols, return the
12651 first input section matching <name>. Return NULL otherwise. */
12654 _bfd_elf_is_start_stop (const struct bfd_link_info
*info
,
12655 struct elf_link_hash_entry
*h
)
12658 const char *sec_name
;
12660 if (h
->root
.type
!= bfd_link_hash_undefined
12661 && h
->root
.type
!= bfd_link_hash_undefweak
)
12664 s
= h
->root
.u
.undef
.section
;
12667 if (s
== (asection
*) 0 - 1)
12673 if (strncmp (h
->root
.root
.string
, "__start_", 8) == 0)
12674 sec_name
= h
->root
.root
.string
+ 8;
12675 else if (strncmp (h
->root
.root
.string
, "__stop_", 7) == 0)
12676 sec_name
= h
->root
.root
.string
+ 7;
12678 if (sec_name
!= NULL
&& *sec_name
!= '\0')
12682 for (i
= info
->input_bfds
; i
!= NULL
; i
= i
->link
.next
)
12684 s
= bfd_get_section_by_name (i
, sec_name
);
12687 h
->root
.u
.undef
.section
= s
;
12694 h
->root
.u
.undef
.section
= (asection
*) 0 - 1;
12699 /* COOKIE->rel describes a relocation against section SEC, which is
12700 a section we've decided to keep. Return the section that contains
12701 the relocation symbol, or NULL if no section contains it. */
12704 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
12705 elf_gc_mark_hook_fn gc_mark_hook
,
12706 struct elf_reloc_cookie
*cookie
,
12707 bfd_boolean
*start_stop
)
12709 unsigned long r_symndx
;
12710 struct elf_link_hash_entry
*h
;
12712 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
12713 if (r_symndx
== STN_UNDEF
)
12716 if (r_symndx
>= cookie
->locsymcount
12717 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
12719 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
12722 info
->callbacks
->einfo (_("%F%P: corrupt input: %B\n"),
12726 while (h
->root
.type
== bfd_link_hash_indirect
12727 || h
->root
.type
== bfd_link_hash_warning
)
12728 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
12730 /* If this symbol is weak and there is a non-weak definition, we
12731 keep the non-weak definition because many backends put
12732 dynamic reloc info on the non-weak definition for code
12733 handling copy relocs. */
12734 if (h
->u
.weakdef
!= NULL
)
12735 h
->u
.weakdef
->mark
= 1;
12737 if (start_stop
!= NULL
)
12739 /* To work around a glibc bug, mark all XXX input sections
12740 when there is an as yet undefined reference to __start_XXX
12741 or __stop_XXX symbols. The linker will later define such
12742 symbols for orphan input sections that have a name
12743 representable as a C identifier. */
12744 asection
*s
= _bfd_elf_is_start_stop (info
, h
);
12748 *start_stop
= !s
->gc_mark
;
12753 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
12756 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
12757 &cookie
->locsyms
[r_symndx
]);
12760 /* COOKIE->rel describes a relocation against section SEC, which is
12761 a section we've decided to keep. Mark the section that contains
12762 the relocation symbol. */
12765 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
12767 elf_gc_mark_hook_fn gc_mark_hook
,
12768 struct elf_reloc_cookie
*cookie
)
12771 bfd_boolean start_stop
= FALSE
;
12773 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
12774 while (rsec
!= NULL
)
12776 if (!rsec
->gc_mark
)
12778 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
12779 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
12781 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
12786 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
12791 /* The mark phase of garbage collection. For a given section, mark
12792 it and any sections in this section's group, and all the sections
12793 which define symbols to which it refers. */
12796 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
12798 elf_gc_mark_hook_fn gc_mark_hook
)
12801 asection
*group_sec
, *eh_frame
;
12805 /* Mark all the sections in the group. */
12806 group_sec
= elf_section_data (sec
)->next_in_group
;
12807 if (group_sec
&& !group_sec
->gc_mark
)
12808 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
12811 /* Look through the section relocs. */
12813 eh_frame
= elf_eh_frame_section (sec
->owner
);
12814 if ((sec
->flags
& SEC_RELOC
) != 0
12815 && sec
->reloc_count
> 0
12816 && sec
!= eh_frame
)
12818 struct elf_reloc_cookie cookie
;
12820 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
12824 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
12825 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
12830 fini_reloc_cookie_for_section (&cookie
, sec
);
12834 if (ret
&& eh_frame
&& elf_fde_list (sec
))
12836 struct elf_reloc_cookie cookie
;
12838 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
12842 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
12843 gc_mark_hook
, &cookie
))
12845 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
12849 eh_frame
= elf_section_eh_frame_entry (sec
);
12850 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
12851 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
12857 /* Scan and mark sections in a special or debug section group. */
12860 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
12862 /* Point to first section of section group. */
12864 /* Used to iterate the section group. */
12867 bfd_boolean is_special_grp
= TRUE
;
12868 bfd_boolean is_debug_grp
= TRUE
;
12870 /* First scan to see if group contains any section other than debug
12871 and special section. */
12872 ssec
= msec
= elf_next_in_group (grp
);
12875 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
12876 is_debug_grp
= FALSE
;
12878 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
12879 is_special_grp
= FALSE
;
12881 msec
= elf_next_in_group (msec
);
12883 while (msec
!= ssec
);
12885 /* If this is a pure debug section group or pure special section group,
12886 keep all sections in this group. */
12887 if (is_debug_grp
|| is_special_grp
)
12892 msec
= elf_next_in_group (msec
);
12894 while (msec
!= ssec
);
12898 /* Keep debug and special sections. */
12901 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
12902 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED
)
12906 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
12909 bfd_boolean some_kept
;
12910 bfd_boolean debug_frag_seen
;
12912 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
12915 /* Ensure all linker created sections are kept,
12916 see if any other section is already marked,
12917 and note if we have any fragmented debug sections. */
12918 debug_frag_seen
= some_kept
= FALSE
;
12919 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12921 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
12923 else if (isec
->gc_mark
)
12926 if (debug_frag_seen
== FALSE
12927 && (isec
->flags
& SEC_DEBUGGING
)
12928 && CONST_STRNEQ (isec
->name
, ".debug_line."))
12929 debug_frag_seen
= TRUE
;
12932 /* If no section in this file will be kept, then we can
12933 toss out the debug and special sections. */
12937 /* Keep debug and special sections like .comment when they are
12938 not part of a group. Also keep section groups that contain
12939 just debug sections or special sections. */
12940 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12942 if ((isec
->flags
& SEC_GROUP
) != 0)
12943 _bfd_elf_gc_mark_debug_special_section_group (isec
);
12944 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
12945 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
12946 && elf_next_in_group (isec
) == NULL
)
12950 if (! debug_frag_seen
)
12953 /* Look for CODE sections which are going to be discarded,
12954 and find and discard any fragmented debug sections which
12955 are associated with that code section. */
12956 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12957 if ((isec
->flags
& SEC_CODE
) != 0
12958 && isec
->gc_mark
== 0)
12963 ilen
= strlen (isec
->name
);
12965 /* Association is determined by the name of the debug section
12966 containing the name of the code section as a suffix. For
12967 example .debug_line.text.foo is a debug section associated
12969 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
12973 if (dsec
->gc_mark
== 0
12974 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
12977 dlen
= strlen (dsec
->name
);
12980 && strncmp (dsec
->name
+ (dlen
- ilen
),
12981 isec
->name
, ilen
) == 0)
12991 /* The sweep phase of garbage collection. Remove all garbage sections. */
12993 typedef bfd_boolean (*gc_sweep_hook_fn
)
12994 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
12997 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
13000 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13001 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
13003 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13007 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13008 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13011 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13013 /* When any section in a section group is kept, we keep all
13014 sections in the section group. If the first member of
13015 the section group is excluded, we will also exclude the
13017 if (o
->flags
& SEC_GROUP
)
13019 asection
*first
= elf_next_in_group (o
);
13020 o
->gc_mark
= first
->gc_mark
;
13026 /* Skip sweeping sections already excluded. */
13027 if (o
->flags
& SEC_EXCLUDE
)
13030 /* Since this is early in the link process, it is simple
13031 to remove a section from the output. */
13032 o
->flags
|= SEC_EXCLUDE
;
13034 if (info
->print_gc_sections
&& o
->size
!= 0)
13035 /* xgettext:c-format */
13036 _bfd_error_handler (_("Removing unused section '%A' in file '%B'"),
13039 /* But we also have to update some of the relocation
13040 info we collected before. */
13042 && (o
->flags
& SEC_RELOC
) != 0
13043 && o
->reloc_count
!= 0
13044 && !((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
13045 && (o
->flags
& SEC_DEBUGGING
) != 0)
13046 && !bfd_is_abs_section (o
->output_section
))
13048 Elf_Internal_Rela
*internal_relocs
;
13052 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
13053 info
->keep_memory
);
13054 if (internal_relocs
== NULL
)
13057 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
13059 if (elf_section_data (o
)->relocs
!= internal_relocs
)
13060 free (internal_relocs
);
13071 /* Propagate collected vtable information. This is called through
13072 elf_link_hash_traverse. */
13075 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
13077 /* Those that are not vtables. */
13078 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
13081 /* Those vtables that do not have parents, we cannot merge. */
13082 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
13085 /* If we've already been done, exit. */
13086 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
13089 /* Make sure the parent's table is up to date. */
13090 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
13092 if (h
->vtable
->used
== NULL
)
13094 /* None of this table's entries were referenced. Re-use the
13096 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
13097 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
13102 bfd_boolean
*cu
, *pu
;
13104 /* Or the parent's entries into ours. */
13105 cu
= h
->vtable
->used
;
13107 pu
= h
->vtable
->parent
->vtable
->used
;
13110 const struct elf_backend_data
*bed
;
13111 unsigned int log_file_align
;
13113 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
13114 log_file_align
= bed
->s
->log_file_align
;
13115 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
13130 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
13133 bfd_vma hstart
, hend
;
13134 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
13135 const struct elf_backend_data
*bed
;
13136 unsigned int log_file_align
;
13138 /* Take care of both those symbols that do not describe vtables as
13139 well as those that are not loaded. */
13140 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
13143 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
13144 || h
->root
.type
== bfd_link_hash_defweak
);
13146 sec
= h
->root
.u
.def
.section
;
13147 hstart
= h
->root
.u
.def
.value
;
13148 hend
= hstart
+ h
->size
;
13150 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
13152 return *(bfd_boolean
*) okp
= FALSE
;
13153 bed
= get_elf_backend_data (sec
->owner
);
13154 log_file_align
= bed
->s
->log_file_align
;
13156 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
13158 for (rel
= relstart
; rel
< relend
; ++rel
)
13159 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
13161 /* If the entry is in use, do nothing. */
13162 if (h
->vtable
->used
13163 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
13165 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
13166 if (h
->vtable
->used
[entry
])
13169 /* Otherwise, kill it. */
13170 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
13176 /* Mark sections containing dynamically referenced symbols. When
13177 building shared libraries, we must assume that any visible symbol is
13181 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
13183 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
13184 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
13186 if ((h
->root
.type
== bfd_link_hash_defined
13187 || h
->root
.type
== bfd_link_hash_defweak
)
13189 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
13190 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
13191 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
13192 && (!bfd_link_executable (info
)
13193 || info
->gc_keep_exported
13194 || info
->export_dynamic
13197 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
13198 && (h
->versioned
>= versioned
13199 || !bfd_hide_sym_by_version (info
->version_info
,
13200 h
->root
.root
.string
)))))
13201 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13206 /* Keep all sections containing symbols undefined on the command-line,
13207 and the section containing the entry symbol. */
13210 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
13212 struct bfd_sym_chain
*sym
;
13214 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
13216 struct elf_link_hash_entry
*h
;
13218 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
13219 FALSE
, FALSE
, FALSE
);
13222 && (h
->root
.type
== bfd_link_hash_defined
13223 || h
->root
.type
== bfd_link_hash_defweak
)
13224 && !bfd_is_abs_section (h
->root
.u
.def
.section
)
13225 && !bfd_is_und_section (h
->root
.u
.def
.section
))
13226 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13231 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
13232 struct bfd_link_info
*info
)
13234 bfd
*ibfd
= info
->input_bfds
;
13236 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13239 struct elf_reloc_cookie cookie
;
13241 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13244 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
13247 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
13249 if (CONST_STRNEQ (bfd_section_name (ibfd
, sec
), ".eh_frame_entry")
13250 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
13252 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
13253 fini_reloc_cookie_rels (&cookie
, sec
);
13260 /* Do mark and sweep of unused sections. */
13263 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
13265 bfd_boolean ok
= TRUE
;
13267 elf_gc_mark_hook_fn gc_mark_hook
;
13268 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13269 struct elf_link_hash_table
*htab
;
13271 if (!bed
->can_gc_sections
13272 || !is_elf_hash_table (info
->hash
))
13274 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13278 bed
->gc_keep (info
);
13279 htab
= elf_hash_table (info
);
13281 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13282 at the .eh_frame section if we can mark the FDEs individually. */
13283 for (sub
= info
->input_bfds
;
13284 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
13285 sub
= sub
->link
.next
)
13288 struct elf_reloc_cookie cookie
;
13290 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
13291 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
13293 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
13294 if (elf_section_data (sec
)->sec_info
13295 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
13296 elf_eh_frame_section (sub
) = sec
;
13297 fini_reloc_cookie_for_section (&cookie
, sec
);
13298 sec
= bfd_get_next_section_by_name (NULL
, sec
);
13302 /* Apply transitive closure to the vtable entry usage info. */
13303 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
13307 /* Kill the vtable relocations that were not used. */
13308 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &ok
);
13312 /* Mark dynamically referenced symbols. */
13313 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
13314 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
13316 /* Grovel through relocs to find out who stays ... */
13317 gc_mark_hook
= bed
->gc_mark_hook
;
13318 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13322 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13323 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13326 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13327 Also treat note sections as a root, if the section is not part
13329 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13331 && (o
->flags
& SEC_EXCLUDE
) == 0
13332 && ((o
->flags
& SEC_KEEP
) != 0
13333 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
13334 && elf_next_in_group (o
) == NULL
)))
13336 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
13341 /* Allow the backend to mark additional target specific sections. */
13342 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
13344 /* ... and mark SEC_EXCLUDE for those that go. */
13345 return elf_gc_sweep (abfd
, info
);
13348 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13351 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
13353 struct elf_link_hash_entry
*h
,
13356 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
13357 struct elf_link_hash_entry
**search
, *child
;
13358 size_t extsymcount
;
13359 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13361 /* The sh_info field of the symtab header tells us where the
13362 external symbols start. We don't care about the local symbols at
13364 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
13365 if (!elf_bad_symtab (abfd
))
13366 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
13368 sym_hashes
= elf_sym_hashes (abfd
);
13369 sym_hashes_end
= sym_hashes
+ extsymcount
;
13371 /* Hunt down the child symbol, which is in this section at the same
13372 offset as the relocation. */
13373 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
13375 if ((child
= *search
) != NULL
13376 && (child
->root
.type
== bfd_link_hash_defined
13377 || child
->root
.type
== bfd_link_hash_defweak
)
13378 && child
->root
.u
.def
.section
== sec
13379 && child
->root
.u
.def
.value
== offset
)
13383 /* xgettext:c-format */
13384 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"),
13385 abfd
, sec
, (unsigned long) offset
);
13386 bfd_set_error (bfd_error_invalid_operation
);
13390 if (!child
->vtable
)
13392 child
->vtable
= ((struct elf_link_virtual_table_entry
*)
13393 bfd_zalloc (abfd
, sizeof (*child
->vtable
)));
13394 if (!child
->vtable
)
13399 /* This *should* only be the absolute section. It could potentially
13400 be that someone has defined a non-global vtable though, which
13401 would be bad. It isn't worth paging in the local symbols to be
13402 sure though; that case should simply be handled by the assembler. */
13404 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
13407 child
->vtable
->parent
= h
;
13412 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13415 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
13416 asection
*sec ATTRIBUTE_UNUSED
,
13417 struct elf_link_hash_entry
*h
,
13420 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13421 unsigned int log_file_align
= bed
->s
->log_file_align
;
13425 h
->vtable
= ((struct elf_link_virtual_table_entry
*)
13426 bfd_zalloc (abfd
, sizeof (*h
->vtable
)));
13431 if (addend
>= h
->vtable
->size
)
13433 size_t size
, bytes
, file_align
;
13434 bfd_boolean
*ptr
= h
->vtable
->used
;
13436 /* While the symbol is undefined, we have to be prepared to handle
13438 file_align
= 1 << log_file_align
;
13439 if (h
->root
.type
== bfd_link_hash_undefined
)
13440 size
= addend
+ file_align
;
13444 if (addend
>= size
)
13446 /* Oops! We've got a reference past the defined end of
13447 the table. This is probably a bug -- shall we warn? */
13448 size
= addend
+ file_align
;
13451 size
= (size
+ file_align
- 1) & -file_align
;
13453 /* Allocate one extra entry for use as a "done" flag for the
13454 consolidation pass. */
13455 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
13459 ptr
= (bfd_boolean
*) bfd_realloc (ptr
- 1, bytes
);
13465 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
13466 * sizeof (bfd_boolean
));
13467 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
13471 ptr
= (bfd_boolean
*) bfd_zmalloc (bytes
);
13476 /* And arrange for that done flag to be at index -1. */
13477 h
->vtable
->used
= ptr
+ 1;
13478 h
->vtable
->size
= size
;
13481 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
13486 /* Map an ELF section header flag to its corresponding string. */
13490 flagword flag_value
;
13491 } elf_flags_to_name_table
;
13493 static elf_flags_to_name_table elf_flags_to_names
[] =
13495 { "SHF_WRITE", SHF_WRITE
},
13496 { "SHF_ALLOC", SHF_ALLOC
},
13497 { "SHF_EXECINSTR", SHF_EXECINSTR
},
13498 { "SHF_MERGE", SHF_MERGE
},
13499 { "SHF_STRINGS", SHF_STRINGS
},
13500 { "SHF_INFO_LINK", SHF_INFO_LINK
},
13501 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
13502 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
13503 { "SHF_GROUP", SHF_GROUP
},
13504 { "SHF_TLS", SHF_TLS
},
13505 { "SHF_MASKOS", SHF_MASKOS
},
13506 { "SHF_EXCLUDE", SHF_EXCLUDE
},
13509 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13511 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
13512 struct flag_info
*flaginfo
,
13515 const bfd_vma sh_flags
= elf_section_flags (section
);
13517 if (!flaginfo
->flags_initialized
)
13519 bfd
*obfd
= info
->output_bfd
;
13520 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13521 struct flag_info_list
*tf
= flaginfo
->flag_list
;
13523 int without_hex
= 0;
13525 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
13528 flagword (*lookup
) (char *);
13530 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
13531 if (lookup
!= NULL
)
13533 flagword hexval
= (*lookup
) ((char *) tf
->name
);
13537 if (tf
->with
== with_flags
)
13538 with_hex
|= hexval
;
13539 else if (tf
->with
== without_flags
)
13540 without_hex
|= hexval
;
13545 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
13547 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
13549 if (tf
->with
== with_flags
)
13550 with_hex
|= elf_flags_to_names
[i
].flag_value
;
13551 else if (tf
->with
== without_flags
)
13552 without_hex
|= elf_flags_to_names
[i
].flag_value
;
13559 info
->callbacks
->einfo
13560 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
13564 flaginfo
->flags_initialized
= TRUE
;
13565 flaginfo
->only_with_flags
|= with_hex
;
13566 flaginfo
->not_with_flags
|= without_hex
;
13569 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
13572 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
13578 struct alloc_got_off_arg
{
13580 struct bfd_link_info
*info
;
13583 /* We need a special top-level link routine to convert got reference counts
13584 to real got offsets. */
13587 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
13589 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
13590 bfd
*obfd
= gofarg
->info
->output_bfd
;
13591 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13593 if (h
->got
.refcount
> 0)
13595 h
->got
.offset
= gofarg
->gotoff
;
13596 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
13599 h
->got
.offset
= (bfd_vma
) -1;
13604 /* And an accompanying bit to work out final got entry offsets once
13605 we're done. Should be called from final_link. */
13608 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
13609 struct bfd_link_info
*info
)
13612 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13614 struct alloc_got_off_arg gofarg
;
13616 BFD_ASSERT (abfd
== info
->output_bfd
);
13618 if (! is_elf_hash_table (info
->hash
))
13621 /* The GOT offset is relative to the .got section, but the GOT header is
13622 put into the .got.plt section, if the backend uses it. */
13623 if (bed
->want_got_plt
)
13626 gotoff
= bed
->got_header_size
;
13628 /* Do the local .got entries first. */
13629 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
13631 bfd_signed_vma
*local_got
;
13632 size_t j
, locsymcount
;
13633 Elf_Internal_Shdr
*symtab_hdr
;
13635 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
13638 local_got
= elf_local_got_refcounts (i
);
13642 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
13643 if (elf_bad_symtab (i
))
13644 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13646 locsymcount
= symtab_hdr
->sh_info
;
13648 for (j
= 0; j
< locsymcount
; ++j
)
13650 if (local_got
[j
] > 0)
13652 local_got
[j
] = gotoff
;
13653 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
13656 local_got
[j
] = (bfd_vma
) -1;
13660 /* Then the global .got entries. .plt refcounts are handled by
13661 adjust_dynamic_symbol */
13662 gofarg
.gotoff
= gotoff
;
13663 gofarg
.info
= info
;
13664 elf_link_hash_traverse (elf_hash_table (info
),
13665 elf_gc_allocate_got_offsets
,
13670 /* Many folk need no more in the way of final link than this, once
13671 got entry reference counting is enabled. */
13674 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
13676 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
13679 /* Invoke the regular ELF backend linker to do all the work. */
13680 return bfd_elf_final_link (abfd
, info
);
13684 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
13686 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
13688 if (rcookie
->bad_symtab
)
13689 rcookie
->rel
= rcookie
->rels
;
13691 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
13693 unsigned long r_symndx
;
13695 if (! rcookie
->bad_symtab
)
13696 if (rcookie
->rel
->r_offset
> offset
)
13698 if (rcookie
->rel
->r_offset
!= offset
)
13701 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
13702 if (r_symndx
== STN_UNDEF
)
13705 if (r_symndx
>= rcookie
->locsymcount
13706 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13708 struct elf_link_hash_entry
*h
;
13710 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
13712 while (h
->root
.type
== bfd_link_hash_indirect
13713 || h
->root
.type
== bfd_link_hash_warning
)
13714 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13716 if ((h
->root
.type
== bfd_link_hash_defined
13717 || h
->root
.type
== bfd_link_hash_defweak
)
13718 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
13719 || h
->root
.u
.def
.section
->kept_section
!= NULL
13720 || discarded_section (h
->root
.u
.def
.section
)))
13725 /* It's not a relocation against a global symbol,
13726 but it could be a relocation against a local
13727 symbol for a discarded section. */
13729 Elf_Internal_Sym
*isym
;
13731 /* Need to: get the symbol; get the section. */
13732 isym
= &rcookie
->locsyms
[r_symndx
];
13733 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
13735 && (isec
->kept_section
!= NULL
13736 || discarded_section (isec
)))
13744 /* Discard unneeded references to discarded sections.
13745 Returns -1 on error, 1 if any section's size was changed, 0 if
13746 nothing changed. This function assumes that the relocations are in
13747 sorted order, which is true for all known assemblers. */
13750 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
13752 struct elf_reloc_cookie cookie
;
13757 if (info
->traditional_format
13758 || !is_elf_hash_table (info
->hash
))
13761 o
= bfd_get_section_by_name (output_bfd
, ".stab");
13766 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13769 || i
->reloc_count
== 0
13770 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
13774 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13777 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13780 if (_bfd_discard_section_stabs (abfd
, i
,
13781 elf_section_data (i
)->sec_info
,
13782 bfd_elf_reloc_symbol_deleted_p
,
13786 fini_reloc_cookie_for_section (&cookie
, i
);
13791 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
13792 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
13797 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13803 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13806 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13809 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
13810 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
13811 bfd_elf_reloc_symbol_deleted_p
,
13815 fini_reloc_cookie_for_section (&cookie
, i
);
13819 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
13821 const struct elf_backend_data
*bed
;
13823 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13826 bed
= get_elf_backend_data (abfd
);
13828 if (bed
->elf_backend_discard_info
!= NULL
)
13830 if (!init_reloc_cookie (&cookie
, info
, abfd
))
13833 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
13836 fini_reloc_cookie (&cookie
, abfd
);
13840 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
13841 _bfd_elf_end_eh_frame_parsing (info
);
13843 if (info
->eh_frame_hdr_type
13844 && !bfd_link_relocatable (info
)
13845 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
13852 _bfd_elf_section_already_linked (bfd
*abfd
,
13854 struct bfd_link_info
*info
)
13857 const char *name
, *key
;
13858 struct bfd_section_already_linked
*l
;
13859 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
13861 if (sec
->output_section
== bfd_abs_section_ptr
)
13864 flags
= sec
->flags
;
13866 /* Return if it isn't a linkonce section. A comdat group section
13867 also has SEC_LINK_ONCE set. */
13868 if ((flags
& SEC_LINK_ONCE
) == 0)
13871 /* Don't put group member sections on our list of already linked
13872 sections. They are handled as a group via their group section. */
13873 if (elf_sec_group (sec
) != NULL
)
13876 /* For a SHT_GROUP section, use the group signature as the key. */
13878 if ((flags
& SEC_GROUP
) != 0
13879 && elf_next_in_group (sec
) != NULL
13880 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
13881 key
= elf_group_name (elf_next_in_group (sec
));
13884 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13885 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
13886 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
13889 /* Must be a user linkonce section that doesn't follow gcc's
13890 naming convention. In this case we won't be matching
13891 single member groups. */
13895 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
13897 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13899 /* We may have 2 different types of sections on the list: group
13900 sections with a signature of <key> (<key> is some string),
13901 and linkonce sections named .gnu.linkonce.<type>.<key>.
13902 Match like sections. LTO plugin sections are an exception.
13903 They are always named .gnu.linkonce.t.<key> and match either
13904 type of section. */
13905 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
13906 && ((flags
& SEC_GROUP
) != 0
13907 || strcmp (name
, l
->sec
->name
) == 0))
13908 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0)
13910 /* The section has already been linked. See if we should
13911 issue a warning. */
13912 if (!_bfd_handle_already_linked (sec
, l
, info
))
13915 if (flags
& SEC_GROUP
)
13917 asection
*first
= elf_next_in_group (sec
);
13918 asection
*s
= first
;
13922 s
->output_section
= bfd_abs_section_ptr
;
13923 /* Record which group discards it. */
13924 s
->kept_section
= l
->sec
;
13925 s
= elf_next_in_group (s
);
13926 /* These lists are circular. */
13936 /* A single member comdat group section may be discarded by a
13937 linkonce section and vice versa. */
13938 if ((flags
& SEC_GROUP
) != 0)
13940 asection
*first
= elf_next_in_group (sec
);
13942 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
13943 /* Check this single member group against linkonce sections. */
13944 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13945 if ((l
->sec
->flags
& SEC_GROUP
) == 0
13946 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
13948 first
->output_section
= bfd_abs_section_ptr
;
13949 first
->kept_section
= l
->sec
;
13950 sec
->output_section
= bfd_abs_section_ptr
;
13955 /* Check this linkonce section against single member groups. */
13956 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13957 if (l
->sec
->flags
& SEC_GROUP
)
13959 asection
*first
= elf_next_in_group (l
->sec
);
13962 && elf_next_in_group (first
) == first
13963 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
13965 sec
->output_section
= bfd_abs_section_ptr
;
13966 sec
->kept_section
= first
;
13971 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13972 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13973 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13974 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13975 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13976 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13977 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13978 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13979 The reverse order cannot happen as there is never a bfd with only the
13980 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13981 matter as here were are looking only for cross-bfd sections. */
13983 if ((flags
& SEC_GROUP
) == 0 && CONST_STRNEQ (name
, ".gnu.linkonce.r."))
13984 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13985 if ((l
->sec
->flags
& SEC_GROUP
) == 0
13986 && CONST_STRNEQ (l
->sec
->name
, ".gnu.linkonce.t."))
13988 if (abfd
!= l
->sec
->owner
)
13989 sec
->output_section
= bfd_abs_section_ptr
;
13993 /* This is the first section with this name. Record it. */
13994 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
13995 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
13996 return sec
->output_section
== bfd_abs_section_ptr
;
14000 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
14002 return sym
->st_shndx
== SHN_COMMON
;
14006 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
14012 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
14014 return bfd_com_section_ptr
;
14018 _bfd_elf_default_got_elt_size (bfd
*abfd
,
14019 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
14020 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
14021 bfd
*ibfd ATTRIBUTE_UNUSED
,
14022 unsigned long symndx ATTRIBUTE_UNUSED
)
14024 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14025 return bed
->s
->arch_size
/ 8;
14028 /* Routines to support the creation of dynamic relocs. */
14030 /* Returns the name of the dynamic reloc section associated with SEC. */
14032 static const char *
14033 get_dynamic_reloc_section_name (bfd
* abfd
,
14035 bfd_boolean is_rela
)
14038 const char *old_name
= bfd_get_section_name (NULL
, sec
);
14039 const char *prefix
= is_rela
? ".rela" : ".rel";
14041 if (old_name
== NULL
)
14044 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
14045 sprintf (name
, "%s%s", prefix
, old_name
);
14050 /* Returns the dynamic reloc section associated with SEC.
14051 If necessary compute the name of the dynamic reloc section based
14052 on SEC's name (looked up in ABFD's string table) and the setting
14056 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
14058 bfd_boolean is_rela
)
14060 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14062 if (reloc_sec
== NULL
)
14064 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14068 reloc_sec
= bfd_get_linker_section (abfd
, name
);
14070 if (reloc_sec
!= NULL
)
14071 elf_section_data (sec
)->sreloc
= reloc_sec
;
14078 /* Returns the dynamic reloc section associated with SEC. If the
14079 section does not exist it is created and attached to the DYNOBJ
14080 bfd and stored in the SRELOC field of SEC's elf_section_data
14083 ALIGNMENT is the alignment for the newly created section and
14084 IS_RELA defines whether the name should be .rela.<SEC's name>
14085 or .rel.<SEC's name>. The section name is looked up in the
14086 string table associated with ABFD. */
14089 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
14091 unsigned int alignment
,
14093 bfd_boolean is_rela
)
14095 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14097 if (reloc_sec
== NULL
)
14099 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14104 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
14106 if (reloc_sec
== NULL
)
14108 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
14109 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
14110 if ((sec
->flags
& SEC_ALLOC
) != 0)
14111 flags
|= SEC_ALLOC
| SEC_LOAD
;
14113 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
14114 if (reloc_sec
!= NULL
)
14116 /* _bfd_elf_get_sec_type_attr chooses a section type by
14117 name. Override as it may be wrong, eg. for a user
14118 section named "auto" we'll get ".relauto" which is
14119 seen to be a .rela section. */
14120 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
14121 if (! bfd_set_section_alignment (dynobj
, reloc_sec
, alignment
))
14126 elf_section_data (sec
)->sreloc
= reloc_sec
;
14132 /* Copy the ELF symbol type and other attributes for a linker script
14133 assignment from HSRC to HDEST. Generally this should be treated as
14134 if we found a strong non-dynamic definition for HDEST (except that
14135 ld ignores multiple definition errors). */
14137 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
14138 struct bfd_link_hash_entry
*hdest
,
14139 struct bfd_link_hash_entry
*hsrc
)
14141 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
14142 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
14143 Elf_Internal_Sym isym
;
14145 ehdest
->type
= ehsrc
->type
;
14146 ehdest
->target_internal
= ehsrc
->target_internal
;
14148 isym
.st_other
= ehsrc
->other
;
14149 elf_merge_st_other (abfd
, ehdest
, &isym
, NULL
, TRUE
, FALSE
);
14152 /* Append a RELA relocation REL to section S in BFD. */
14155 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14157 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14158 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
14159 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
14160 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
14163 /* Append a REL relocation REL to section S in BFD. */
14166 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14168 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14169 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
14170 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
14171 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);