1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
29 #include "safe-ctype.h"
30 #include "libiberty.h"
33 /* Define a symbol in a dynamic linkage section. */
35 struct elf_link_hash_entry
*
36 _bfd_elf_define_linkage_sym (bfd
*abfd
,
37 struct bfd_link_info
*info
,
41 struct elf_link_hash_entry
*h
;
42 struct bfd_link_hash_entry
*bh
;
43 const struct elf_backend_data
*bed
;
45 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
48 /* Zap symbol defined in an as-needed lib that wasn't linked.
49 This is a symptom of a larger problem: Absolute symbols
50 defined in shared libraries can't be overridden, because we
51 lose the link to the bfd which is via the symbol section. */
52 h
->root
.type
= bfd_link_hash_new
;
56 if (!_bfd_generic_link_add_one_symbol (info
, abfd
, name
, BSF_GLOBAL
,
58 get_elf_backend_data (abfd
)->collect
,
61 h
= (struct elf_link_hash_entry
*) bh
;
64 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
66 bed
= get_elf_backend_data (abfd
);
67 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
72 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
76 struct elf_link_hash_entry
*h
;
77 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
80 /* This function may be called more than once. */
81 s
= bfd_get_section_by_name (abfd
, ".got");
82 if (s
!= NULL
&& (s
->flags
& SEC_LINKER_CREATED
) != 0)
85 switch (bed
->s
->arch_size
)
96 bfd_set_error (bfd_error_bad_value
);
100 flags
= bed
->dynamic_sec_flags
;
102 s
= bfd_make_section_with_flags (abfd
, ".got", flags
);
104 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
107 if (bed
->want_got_plt
)
109 s
= bfd_make_section_with_flags (abfd
, ".got.plt", flags
);
111 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
115 if (bed
->want_got_sym
)
117 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
118 (or .got.plt) section. We don't do this in the linker script
119 because we don't want to define the symbol if we are not creating
120 a global offset table. */
121 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_GLOBAL_OFFSET_TABLE_");
122 elf_hash_table (info
)->hgot
= h
;
127 /* The first bit of the global offset table is the header. */
128 s
->size
+= bed
->got_header_size
;
133 /* Create a strtab to hold the dynamic symbol names. */
135 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
137 struct elf_link_hash_table
*hash_table
;
139 hash_table
= elf_hash_table (info
);
140 if (hash_table
->dynobj
== NULL
)
141 hash_table
->dynobj
= abfd
;
143 if (hash_table
->dynstr
== NULL
)
145 hash_table
->dynstr
= _bfd_elf_strtab_init ();
146 if (hash_table
->dynstr
== NULL
)
152 /* Create some sections which will be filled in with dynamic linking
153 information. ABFD is an input file which requires dynamic sections
154 to be created. The dynamic sections take up virtual memory space
155 when the final executable is run, so we need to create them before
156 addresses are assigned to the output sections. We work out the
157 actual contents and size of these sections later. */
160 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
163 register asection
*s
;
164 const struct elf_backend_data
*bed
;
166 if (! is_elf_hash_table (info
->hash
))
169 if (elf_hash_table (info
)->dynamic_sections_created
)
172 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
175 abfd
= elf_hash_table (info
)->dynobj
;
176 bed
= get_elf_backend_data (abfd
);
178 flags
= bed
->dynamic_sec_flags
;
180 /* A dynamically linked executable has a .interp section, but a
181 shared library does not. */
182 if (info
->executable
)
184 s
= bfd_make_section_with_flags (abfd
, ".interp",
185 flags
| SEC_READONLY
);
190 /* Create sections to hold version informations. These are removed
191 if they are not needed. */
192 s
= bfd_make_section_with_flags (abfd
, ".gnu.version_d",
193 flags
| SEC_READONLY
);
195 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
198 s
= bfd_make_section_with_flags (abfd
, ".gnu.version",
199 flags
| SEC_READONLY
);
201 || ! bfd_set_section_alignment (abfd
, s
, 1))
204 s
= bfd_make_section_with_flags (abfd
, ".gnu.version_r",
205 flags
| SEC_READONLY
);
207 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
210 s
= bfd_make_section_with_flags (abfd
, ".dynsym",
211 flags
| SEC_READONLY
);
213 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
216 s
= bfd_make_section_with_flags (abfd
, ".dynstr",
217 flags
| SEC_READONLY
);
221 s
= bfd_make_section_with_flags (abfd
, ".dynamic", flags
);
223 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
226 /* The special symbol _DYNAMIC is always set to the start of the
227 .dynamic section. We could set _DYNAMIC in a linker script, but we
228 only want to define it if we are, in fact, creating a .dynamic
229 section. We don't want to define it if there is no .dynamic
230 section, since on some ELF platforms the start up code examines it
231 to decide how to initialize the process. */
232 if (!_bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC"))
237 s
= bfd_make_section_with_flags (abfd
, ".hash", flags
| SEC_READONLY
);
239 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
241 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
244 if (info
->emit_gnu_hash
)
246 s
= bfd_make_section_with_flags (abfd
, ".gnu.hash",
247 flags
| SEC_READONLY
);
249 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
251 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
252 4 32-bit words followed by variable count of 64-bit words, then
253 variable count of 32-bit words. */
254 if (bed
->s
->arch_size
== 64)
255 elf_section_data (s
)->this_hdr
.sh_entsize
= 0;
257 elf_section_data (s
)->this_hdr
.sh_entsize
= 4;
260 /* Let the backend create the rest of the sections. This lets the
261 backend set the right flags. The backend will normally create
262 the .got and .plt sections. */
263 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
266 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
271 /* Create dynamic sections when linking against a dynamic object. */
274 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
276 flagword flags
, pltflags
;
277 struct elf_link_hash_entry
*h
;
279 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
281 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
282 .rel[a].bss sections. */
283 flags
= bed
->dynamic_sec_flags
;
286 if (bed
->plt_not_loaded
)
287 /* We do not clear SEC_ALLOC here because we still want the OS to
288 allocate space for the section; it's just that there's nothing
289 to read in from the object file. */
290 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
292 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
293 if (bed
->plt_readonly
)
294 pltflags
|= SEC_READONLY
;
296 s
= bfd_make_section_with_flags (abfd
, ".plt", pltflags
);
298 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
301 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
303 if (bed
->want_plt_sym
)
305 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
306 "_PROCEDURE_LINKAGE_TABLE_");
307 elf_hash_table (info
)->hplt
= h
;
312 s
= bfd_make_section_with_flags (abfd
,
313 (bed
->rela_plts_and_copies_p
314 ? ".rela.plt" : ".rel.plt"),
315 flags
| SEC_READONLY
);
317 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
320 if (! _bfd_elf_create_got_section (abfd
, info
))
323 if (bed
->want_dynbss
)
325 /* The .dynbss section is a place to put symbols which are defined
326 by dynamic objects, are referenced by regular objects, and are
327 not functions. We must allocate space for them in the process
328 image and use a R_*_COPY reloc to tell the dynamic linker to
329 initialize them at run time. The linker script puts the .dynbss
330 section into the .bss section of the final image. */
331 s
= bfd_make_section_with_flags (abfd
, ".dynbss",
333 | SEC_LINKER_CREATED
));
337 /* The .rel[a].bss section holds copy relocs. This section is not
338 normally needed. We need to create it here, though, so that the
339 linker will map it to an output section. We can't just create it
340 only if we need it, because we will not know whether we need it
341 until we have seen all the input files, and the first time the
342 main linker code calls BFD after examining all the input files
343 (size_dynamic_sections) the input sections have already been
344 mapped to the output sections. If the section turns out not to
345 be needed, we can discard it later. We will never need this
346 section when generating a shared object, since they do not use
350 s
= bfd_make_section_with_flags (abfd
,
351 (bed
->rela_plts_and_copies_p
352 ? ".rela.bss" : ".rel.bss"),
353 flags
| SEC_READONLY
);
355 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
363 /* Record a new dynamic symbol. We record the dynamic symbols as we
364 read the input files, since we need to have a list of all of them
365 before we can determine the final sizes of the output sections.
366 Note that we may actually call this function even though we are not
367 going to output any dynamic symbols; in some cases we know that a
368 symbol should be in the dynamic symbol table, but only if there is
372 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
373 struct elf_link_hash_entry
*h
)
375 if (h
->dynindx
== -1)
377 struct elf_strtab_hash
*dynstr
;
382 /* XXX: The ABI draft says the linker must turn hidden and
383 internal symbols into STB_LOCAL symbols when producing the
384 DSO. However, if ld.so honors st_other in the dynamic table,
385 this would not be necessary. */
386 switch (ELF_ST_VISIBILITY (h
->other
))
390 if (h
->root
.type
!= bfd_link_hash_undefined
391 && h
->root
.type
!= bfd_link_hash_undefweak
)
394 if (!elf_hash_table (info
)->is_relocatable_executable
)
402 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
403 ++elf_hash_table (info
)->dynsymcount
;
405 dynstr
= elf_hash_table (info
)->dynstr
;
408 /* Create a strtab to hold the dynamic symbol names. */
409 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
414 /* We don't put any version information in the dynamic string
416 name
= h
->root
.root
.string
;
417 p
= strchr (name
, ELF_VER_CHR
);
419 /* We know that the p points into writable memory. In fact,
420 there are only a few symbols that have read-only names, being
421 those like _GLOBAL_OFFSET_TABLE_ that are created specially
422 by the backends. Most symbols will have names pointing into
423 an ELF string table read from a file, or to objalloc memory. */
426 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
431 if (indx
== (bfd_size_type
) -1)
433 h
->dynstr_index
= indx
;
439 /* Mark a symbol dynamic. */
442 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
443 struct elf_link_hash_entry
*h
,
444 Elf_Internal_Sym
*sym
)
446 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
448 /* It may be called more than once on the same H. */
449 if(h
->dynamic
|| info
->relocatable
)
452 if ((info
->dynamic_data
453 && (h
->type
== STT_OBJECT
455 && ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
)))
457 && h
->root
.type
== bfd_link_hash_new
458 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
462 /* Record an assignment to a symbol made by a linker script. We need
463 this in case some dynamic object refers to this symbol. */
466 bfd_elf_record_link_assignment (bfd
*output_bfd
,
467 struct bfd_link_info
*info
,
472 struct elf_link_hash_entry
*h
, *hv
;
473 struct elf_link_hash_table
*htab
;
474 const struct elf_backend_data
*bed
;
476 if (!is_elf_hash_table (info
->hash
))
479 htab
= elf_hash_table (info
);
480 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
484 switch (h
->root
.type
)
486 case bfd_link_hash_defined
:
487 case bfd_link_hash_defweak
:
488 case bfd_link_hash_common
:
490 case bfd_link_hash_undefweak
:
491 case bfd_link_hash_undefined
:
492 /* Since we're defining the symbol, don't let it seem to have not
493 been defined. record_dynamic_symbol and size_dynamic_sections
494 may depend on this. */
495 h
->root
.type
= bfd_link_hash_new
;
496 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
497 bfd_link_repair_undef_list (&htab
->root
);
499 case bfd_link_hash_new
:
500 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
503 case bfd_link_hash_indirect
:
504 /* We had a versioned symbol in a dynamic library. We make the
505 the versioned symbol point to this one. */
506 bed
= get_elf_backend_data (output_bfd
);
508 while (hv
->root
.type
== bfd_link_hash_indirect
509 || hv
->root
.type
== bfd_link_hash_warning
)
510 hv
= (struct elf_link_hash_entry
*) hv
->root
.u
.i
.link
;
511 /* We don't need to update h->root.u since linker will set them
513 h
->root
.type
= bfd_link_hash_undefined
;
514 hv
->root
.type
= bfd_link_hash_indirect
;
515 hv
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
516 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hv
);
518 case bfd_link_hash_warning
:
523 /* If this symbol is being provided by the linker script, and it is
524 currently defined by a dynamic object, but not by a regular
525 object, then mark it as undefined so that the generic linker will
526 force the correct value. */
530 h
->root
.type
= bfd_link_hash_undefined
;
532 /* If this symbol is not being provided by the linker script, and it is
533 currently defined by a dynamic object, but not by a regular object,
534 then clear out any version information because the symbol will not be
535 associated with the dynamic object any more. */
539 h
->verinfo
.verdef
= NULL
;
543 if (provide
&& hidden
)
545 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
547 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
548 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
551 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
553 if (!info
->relocatable
555 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
556 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
562 || (info
->executable
&& elf_hash_table (info
)->is_relocatable_executable
))
565 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
568 /* If this is a weak defined symbol, and we know a corresponding
569 real symbol from the same dynamic object, make sure the real
570 symbol is also made into a dynamic symbol. */
571 if (h
->u
.weakdef
!= NULL
572 && h
->u
.weakdef
->dynindx
== -1)
574 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
582 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
583 success, and 2 on a failure caused by attempting to record a symbol
584 in a discarded section, eg. a discarded link-once section symbol. */
587 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
592 struct elf_link_local_dynamic_entry
*entry
;
593 struct elf_link_hash_table
*eht
;
594 struct elf_strtab_hash
*dynstr
;
595 unsigned long dynstr_index
;
597 Elf_External_Sym_Shndx eshndx
;
598 char esym
[sizeof (Elf64_External_Sym
)];
600 if (! is_elf_hash_table (info
->hash
))
603 /* See if the entry exists already. */
604 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
605 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
608 amt
= sizeof (*entry
);
609 entry
= bfd_alloc (input_bfd
, amt
);
613 /* Go find the symbol, so that we can find it's name. */
614 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
615 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
617 bfd_release (input_bfd
, entry
);
621 if (entry
->isym
.st_shndx
!= SHN_UNDEF
622 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
626 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
627 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
629 /* We can still bfd_release here as nothing has done another
630 bfd_alloc. We can't do this later in this function. */
631 bfd_release (input_bfd
, entry
);
636 name
= (bfd_elf_string_from_elf_section
637 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
638 entry
->isym
.st_name
));
640 dynstr
= elf_hash_table (info
)->dynstr
;
643 /* Create a strtab to hold the dynamic symbol names. */
644 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
649 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
650 if (dynstr_index
== (unsigned long) -1)
652 entry
->isym
.st_name
= dynstr_index
;
654 eht
= elf_hash_table (info
);
656 entry
->next
= eht
->dynlocal
;
657 eht
->dynlocal
= entry
;
658 entry
->input_bfd
= input_bfd
;
659 entry
->input_indx
= input_indx
;
662 /* Whatever binding the symbol had before, it's now local. */
664 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
666 /* The dynindx will be set at the end of size_dynamic_sections. */
671 /* Return the dynindex of a local dynamic symbol. */
674 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
678 struct elf_link_local_dynamic_entry
*e
;
680 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
681 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
686 /* This function is used to renumber the dynamic symbols, if some of
687 them are removed because they are marked as local. This is called
688 via elf_link_hash_traverse. */
691 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
694 size_t *count
= data
;
696 if (h
->root
.type
== bfd_link_hash_warning
)
697 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
702 if (h
->dynindx
!= -1)
703 h
->dynindx
= ++(*count
);
709 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
710 STB_LOCAL binding. */
713 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
716 size_t *count
= data
;
718 if (h
->root
.type
== bfd_link_hash_warning
)
719 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
721 if (!h
->forced_local
)
724 if (h
->dynindx
!= -1)
725 h
->dynindx
= ++(*count
);
730 /* Return true if the dynamic symbol for a given section should be
731 omitted when creating a shared library. */
733 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
734 struct bfd_link_info
*info
,
737 struct elf_link_hash_table
*htab
;
739 switch (elf_section_data (p
)->this_hdr
.sh_type
)
743 /* If sh_type is yet undecided, assume it could be
744 SHT_PROGBITS/SHT_NOBITS. */
746 htab
= elf_hash_table (info
);
747 if (p
== htab
->tls_sec
)
750 if (htab
->text_index_section
!= NULL
)
751 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
753 if (strcmp (p
->name
, ".got") == 0
754 || strcmp (p
->name
, ".got.plt") == 0
755 || strcmp (p
->name
, ".plt") == 0)
759 if (htab
->dynobj
!= NULL
760 && (ip
= bfd_get_section_by_name (htab
->dynobj
, p
->name
)) != NULL
761 && (ip
->flags
& SEC_LINKER_CREATED
)
762 && ip
->output_section
== p
)
767 /* There shouldn't be section relative relocations
768 against any other section. */
774 /* Assign dynsym indices. In a shared library we generate a section
775 symbol for each output section, which come first. Next come symbols
776 which have been forced to local binding. Then all of the back-end
777 allocated local dynamic syms, followed by the rest of the global
781 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
782 struct bfd_link_info
*info
,
783 unsigned long *section_sym_count
)
785 unsigned long dynsymcount
= 0;
787 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
789 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
791 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
792 if ((p
->flags
& SEC_EXCLUDE
) == 0
793 && (p
->flags
& SEC_ALLOC
) != 0
794 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
795 elf_section_data (p
)->dynindx
= ++dynsymcount
;
797 elf_section_data (p
)->dynindx
= 0;
799 *section_sym_count
= dynsymcount
;
801 elf_link_hash_traverse (elf_hash_table (info
),
802 elf_link_renumber_local_hash_table_dynsyms
,
805 if (elf_hash_table (info
)->dynlocal
)
807 struct elf_link_local_dynamic_entry
*p
;
808 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
809 p
->dynindx
= ++dynsymcount
;
812 elf_link_hash_traverse (elf_hash_table (info
),
813 elf_link_renumber_hash_table_dynsyms
,
816 /* There is an unused NULL entry at the head of the table which
817 we must account for in our count. Unless there weren't any
818 symbols, which means we'll have no table at all. */
819 if (dynsymcount
!= 0)
822 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
826 /* Merge st_other field. */
829 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
830 Elf_Internal_Sym
*isym
, bfd_boolean definition
,
833 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
835 /* If st_other has a processor-specific meaning, specific
836 code might be needed here. We never merge the visibility
837 attribute with the one from a dynamic object. */
838 if (bed
->elf_backend_merge_symbol_attribute
)
839 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
842 /* If this symbol has default visibility and the user has requested
843 we not re-export it, then mark it as hidden. */
847 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
848 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
849 isym
->st_other
= (STV_HIDDEN
850 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
852 if (!dynamic
&& ELF_ST_VISIBILITY (isym
->st_other
) != 0)
854 unsigned char hvis
, symvis
, other
, nvis
;
856 /* Only merge the visibility. Leave the remainder of the
857 st_other field to elf_backend_merge_symbol_attribute. */
858 other
= h
->other
& ~ELF_ST_VISIBILITY (-1);
860 /* Combine visibilities, using the most constraining one. */
861 hvis
= ELF_ST_VISIBILITY (h
->other
);
862 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
868 nvis
= hvis
< symvis
? hvis
: symvis
;
870 h
->other
= other
| nvis
;
874 /* This function is called when we want to define a new symbol. It
875 handles the various cases which arise when we find a definition in
876 a dynamic object, or when there is already a definition in a
877 dynamic object. The new symbol is described by NAME, SYM, PSEC,
878 and PVALUE. We set SYM_HASH to the hash table entry. We set
879 OVERRIDE if the old symbol is overriding a new definition. We set
880 TYPE_CHANGE_OK if it is OK for the type to change. We set
881 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
882 change, we mean that we shouldn't warn if the type or size does
883 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
884 object is overridden by a regular object. */
887 _bfd_elf_merge_symbol (bfd
*abfd
,
888 struct bfd_link_info
*info
,
890 Elf_Internal_Sym
*sym
,
893 unsigned int *pold_alignment
,
894 struct elf_link_hash_entry
**sym_hash
,
896 bfd_boolean
*override
,
897 bfd_boolean
*type_change_ok
,
898 bfd_boolean
*size_change_ok
)
900 asection
*sec
, *oldsec
;
901 struct elf_link_hash_entry
*h
;
902 struct elf_link_hash_entry
*flip
;
905 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
906 bfd_boolean newweak
, oldweak
, newfunc
, oldfunc
;
907 const struct elf_backend_data
*bed
;
913 bind
= ELF_ST_BIND (sym
->st_info
);
915 /* Silently discard TLS symbols from --just-syms. There's no way to
916 combine a static TLS block with a new TLS block for this executable. */
917 if (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
918 && sec
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
924 if (! bfd_is_und_section (sec
))
925 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
927 h
= ((struct elf_link_hash_entry
*)
928 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
933 bed
= get_elf_backend_data (abfd
);
935 /* This code is for coping with dynamic objects, and is only useful
936 if we are doing an ELF link. */
937 if (!(*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
940 /* For merging, we only care about real symbols. */
942 while (h
->root
.type
== bfd_link_hash_indirect
943 || h
->root
.type
== bfd_link_hash_warning
)
944 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
946 /* We have to check it for every instance since the first few may be
947 refereences and not all compilers emit symbol type for undefined
949 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
951 /* If we just created the symbol, mark it as being an ELF symbol.
952 Other than that, there is nothing to do--there is no merge issue
953 with a newly defined symbol--so we just return. */
955 if (h
->root
.type
== bfd_link_hash_new
)
961 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
964 switch (h
->root
.type
)
971 case bfd_link_hash_undefined
:
972 case bfd_link_hash_undefweak
:
973 oldbfd
= h
->root
.u
.undef
.abfd
;
977 case bfd_link_hash_defined
:
978 case bfd_link_hash_defweak
:
979 oldbfd
= h
->root
.u
.def
.section
->owner
;
980 oldsec
= h
->root
.u
.def
.section
;
983 case bfd_link_hash_common
:
984 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
985 oldsec
= h
->root
.u
.c
.p
->section
;
989 /* In cases involving weak versioned symbols, we may wind up trying
990 to merge a symbol with itself. Catch that here, to avoid the
991 confusion that results if we try to override a symbol with
992 itself. The additional tests catch cases like
993 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
994 dynamic object, which we do want to handle here. */
996 && ((abfd
->flags
& DYNAMIC
) == 0
1000 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1001 respectively, is from a dynamic object. */
1003 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1007 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1008 else if (oldsec
!= NULL
)
1010 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1011 indices used by MIPS ELF. */
1012 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1015 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1016 respectively, appear to be a definition rather than reference. */
1018 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1020 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1021 && h
->root
.type
!= bfd_link_hash_undefweak
1022 && h
->root
.type
!= bfd_link_hash_common
);
1024 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1025 respectively, appear to be a function. */
1027 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1028 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1030 oldfunc
= (h
->type
!= STT_NOTYPE
1031 && bed
->is_function_type (h
->type
));
1033 /* When we try to create a default indirect symbol from the dynamic
1034 definition with the default version, we skip it if its type and
1035 the type of existing regular definition mismatch. We only do it
1036 if the existing regular definition won't be dynamic. */
1037 if (pold_alignment
== NULL
1039 && !info
->export_dynamic
1044 && (olddef
|| h
->root
.type
== bfd_link_hash_common
)
1045 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1046 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1047 && h
->type
!= STT_NOTYPE
1048 && !(newfunc
&& oldfunc
))
1054 /* Check TLS symbol. We don't check undefined symbol introduced by
1056 if ((ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
)
1057 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1061 bfd_boolean ntdef
, tdef
;
1062 asection
*ntsec
, *tsec
;
1064 if (h
->type
== STT_TLS
)
1084 (*_bfd_error_handler
)
1085 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1086 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
1087 else if (!tdef
&& !ntdef
)
1088 (*_bfd_error_handler
)
1089 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1090 tbfd
, ntbfd
, h
->root
.root
.string
);
1092 (*_bfd_error_handler
)
1093 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1094 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
1096 (*_bfd_error_handler
)
1097 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1098 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
1100 bfd_set_error (bfd_error_bad_value
);
1104 /* We need to remember if a symbol has a definition in a dynamic
1105 object or is weak in all dynamic objects. Internal and hidden
1106 visibility will make it unavailable to dynamic objects. */
1107 if (newdyn
&& !h
->dynamic_def
)
1109 if (!bfd_is_und_section (sec
))
1113 /* Check if this symbol is weak in all dynamic objects. If it
1114 is the first time we see it in a dynamic object, we mark
1115 if it is weak. Otherwise, we clear it. */
1116 if (!h
->ref_dynamic
)
1118 if (bind
== STB_WEAK
)
1119 h
->dynamic_weak
= 1;
1121 else if (bind
!= STB_WEAK
)
1122 h
->dynamic_weak
= 0;
1126 /* If the old symbol has non-default visibility, we ignore the new
1127 definition from a dynamic object. */
1129 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1130 && !bfd_is_und_section (sec
))
1133 /* Make sure this symbol is dynamic. */
1135 /* A protected symbol has external availability. Make sure it is
1136 recorded as dynamic.
1138 FIXME: Should we check type and size for protected symbol? */
1139 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1140 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1145 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1148 /* If the new symbol with non-default visibility comes from a
1149 relocatable file and the old definition comes from a dynamic
1150 object, we remove the old definition. */
1151 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1153 /* Handle the case where the old dynamic definition is
1154 default versioned. We need to copy the symbol info from
1155 the symbol with default version to the normal one if it
1156 was referenced before. */
1159 const struct elf_backend_data
*bed
1160 = get_elf_backend_data (abfd
);
1161 struct elf_link_hash_entry
*vh
= *sym_hash
;
1162 vh
->root
.type
= h
->root
.type
;
1163 h
->root
.type
= bfd_link_hash_indirect
;
1164 (*bed
->elf_backend_copy_indirect_symbol
) (info
, vh
, h
);
1165 /* Protected symbols will override the dynamic definition
1166 with default version. */
1167 if (ELF_ST_VISIBILITY (sym
->st_other
) == STV_PROTECTED
)
1169 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) vh
;
1170 vh
->dynamic_def
= 1;
1171 vh
->ref_dynamic
= 1;
1175 h
->root
.type
= vh
->root
.type
;
1176 vh
->ref_dynamic
= 0;
1177 /* We have to hide it here since it was made dynamic
1178 global with extra bits when the symbol info was
1179 copied from the old dynamic definition. */
1180 (*bed
->elf_backend_hide_symbol
) (info
, vh
, TRUE
);
1188 if ((h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1189 && bfd_is_und_section (sec
))
1191 /* If the new symbol is undefined and the old symbol was
1192 also undefined before, we need to make sure
1193 _bfd_generic_link_add_one_symbol doesn't mess
1194 up the linker hash table undefs list. Since the old
1195 definition came from a dynamic object, it is still on the
1197 h
->root
.type
= bfd_link_hash_undefined
;
1198 h
->root
.u
.undef
.abfd
= abfd
;
1202 h
->root
.type
= bfd_link_hash_new
;
1203 h
->root
.u
.undef
.abfd
= NULL
;
1212 /* FIXME: Should we check type and size for protected symbol? */
1218 /* Differentiate strong and weak symbols. */
1219 newweak
= bind
== STB_WEAK
;
1220 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1221 || h
->root
.type
== bfd_link_hash_undefweak
);
1223 /* If a new weak symbol definition comes from a regular file and the
1224 old symbol comes from a dynamic library, we treat the new one as
1225 strong. Similarly, an old weak symbol definition from a regular
1226 file is treated as strong when the new symbol comes from a dynamic
1227 library. Further, an old weak symbol from a dynamic library is
1228 treated as strong if the new symbol is from a dynamic library.
1229 This reflects the way glibc's ld.so works.
1231 Do this before setting *type_change_ok or *size_change_ok so that
1232 we warn properly when dynamic library symbols are overridden. */
1234 if (newdef
&& !newdyn
&& olddyn
)
1236 if (olddef
&& newdyn
)
1239 /* Allow changes between different types of funciton symbol. */
1240 if (newfunc
&& oldfunc
)
1241 *type_change_ok
= TRUE
;
1243 /* It's OK to change the type if either the existing symbol or the
1244 new symbol is weak. A type change is also OK if the old symbol
1245 is undefined and the new symbol is defined. */
1250 && h
->root
.type
== bfd_link_hash_undefined
))
1251 *type_change_ok
= TRUE
;
1253 /* It's OK to change the size if either the existing symbol or the
1254 new symbol is weak, or if the old symbol is undefined. */
1257 || h
->root
.type
== bfd_link_hash_undefined
)
1258 *size_change_ok
= TRUE
;
1260 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1261 symbol, respectively, appears to be a common symbol in a dynamic
1262 object. If a symbol appears in an uninitialized section, and is
1263 not weak, and is not a function, then it may be a common symbol
1264 which was resolved when the dynamic object was created. We want
1265 to treat such symbols specially, because they raise special
1266 considerations when setting the symbol size: if the symbol
1267 appears as a common symbol in a regular object, and the size in
1268 the regular object is larger, we must make sure that we use the
1269 larger size. This problematic case can always be avoided in C,
1270 but it must be handled correctly when using Fortran shared
1273 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1274 likewise for OLDDYNCOMMON and OLDDEF.
1276 Note that this test is just a heuristic, and that it is quite
1277 possible to have an uninitialized symbol in a shared object which
1278 is really a definition, rather than a common symbol. This could
1279 lead to some minor confusion when the symbol really is a common
1280 symbol in some regular object. However, I think it will be
1286 && (sec
->flags
& SEC_ALLOC
) != 0
1287 && (sec
->flags
& SEC_LOAD
) == 0
1290 newdyncommon
= TRUE
;
1292 newdyncommon
= FALSE
;
1296 && h
->root
.type
== bfd_link_hash_defined
1298 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1299 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1302 olddyncommon
= TRUE
;
1304 olddyncommon
= FALSE
;
1306 /* We now know everything about the old and new symbols. We ask the
1307 backend to check if we can merge them. */
1308 if (bed
->merge_symbol
1309 && !bed
->merge_symbol (info
, sym_hash
, h
, sym
, psec
, pvalue
,
1310 pold_alignment
, skip
, override
,
1311 type_change_ok
, size_change_ok
,
1312 &newdyn
, &newdef
, &newdyncommon
, &newweak
,
1314 &olddyn
, &olddef
, &olddyncommon
, &oldweak
,
1318 /* If both the old and the new symbols look like common symbols in a
1319 dynamic object, set the size of the symbol to the larger of the
1324 && sym
->st_size
!= h
->size
)
1326 /* Since we think we have two common symbols, issue a multiple
1327 common warning if desired. Note that we only warn if the
1328 size is different. If the size is the same, we simply let
1329 the old symbol override the new one as normally happens with
1330 symbols defined in dynamic objects. */
1332 if (! ((*info
->callbacks
->multiple_common
)
1333 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1334 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1337 if (sym
->st_size
> h
->size
)
1338 h
->size
= sym
->st_size
;
1340 *size_change_ok
= TRUE
;
1343 /* If we are looking at a dynamic object, and we have found a
1344 definition, we need to see if the symbol was already defined by
1345 some other object. If so, we want to use the existing
1346 definition, and we do not want to report a multiple symbol
1347 definition error; we do this by clobbering *PSEC to be
1348 bfd_und_section_ptr.
1350 We treat a common symbol as a definition if the symbol in the
1351 shared library is a function, since common symbols always
1352 represent variables; this can cause confusion in principle, but
1353 any such confusion would seem to indicate an erroneous program or
1354 shared library. We also permit a common symbol in a regular
1355 object to override a weak symbol in a shared object. */
1360 || (h
->root
.type
== bfd_link_hash_common
1361 && (newweak
|| newfunc
))))
1365 newdyncommon
= FALSE
;
1367 *psec
= sec
= bfd_und_section_ptr
;
1368 *size_change_ok
= TRUE
;
1370 /* If we get here when the old symbol is a common symbol, then
1371 we are explicitly letting it override a weak symbol or
1372 function in a dynamic object, and we don't want to warn about
1373 a type change. If the old symbol is a defined symbol, a type
1374 change warning may still be appropriate. */
1376 if (h
->root
.type
== bfd_link_hash_common
)
1377 *type_change_ok
= TRUE
;
1380 /* Handle the special case of an old common symbol merging with a
1381 new symbol which looks like a common symbol in a shared object.
1382 We change *PSEC and *PVALUE to make the new symbol look like a
1383 common symbol, and let _bfd_generic_link_add_one_symbol do the
1387 && h
->root
.type
== bfd_link_hash_common
)
1391 newdyncommon
= FALSE
;
1392 *pvalue
= sym
->st_size
;
1393 *psec
= sec
= bed
->common_section (oldsec
);
1394 *size_change_ok
= TRUE
;
1397 /* Skip weak definitions of symbols that are already defined. */
1398 if (newdef
&& olddef
&& newweak
)
1402 /* Merge st_other. If the symbol already has a dynamic index,
1403 but visibility says it should not be visible, turn it into a
1405 elf_merge_st_other (abfd
, h
, sym
, newdef
, newdyn
);
1406 if (h
->dynindx
!= -1)
1407 switch (ELF_ST_VISIBILITY (h
->other
))
1411 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1416 /* If the old symbol is from a dynamic object, and the new symbol is
1417 a definition which is not from a dynamic object, then the new
1418 symbol overrides the old symbol. Symbols from regular files
1419 always take precedence over symbols from dynamic objects, even if
1420 they are defined after the dynamic object in the link.
1422 As above, we again permit a common symbol in a regular object to
1423 override a definition in a shared object if the shared object
1424 symbol is a function or is weak. */
1429 || (bfd_is_com_section (sec
)
1430 && (oldweak
|| oldfunc
)))
1435 /* Change the hash table entry to undefined, and let
1436 _bfd_generic_link_add_one_symbol do the right thing with the
1439 h
->root
.type
= bfd_link_hash_undefined
;
1440 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1441 *size_change_ok
= TRUE
;
1444 olddyncommon
= FALSE
;
1446 /* We again permit a type change when a common symbol may be
1447 overriding a function. */
1449 if (bfd_is_com_section (sec
))
1453 /* If a common symbol overrides a function, make sure
1454 that it isn't defined dynamically nor has type
1457 h
->type
= STT_NOTYPE
;
1459 *type_change_ok
= TRUE
;
1462 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1465 /* This union may have been set to be non-NULL when this symbol
1466 was seen in a dynamic object. We must force the union to be
1467 NULL, so that it is correct for a regular symbol. */
1468 h
->verinfo
.vertree
= NULL
;
1471 /* Handle the special case of a new common symbol merging with an
1472 old symbol that looks like it might be a common symbol defined in
1473 a shared object. Note that we have already handled the case in
1474 which a new common symbol should simply override the definition
1475 in the shared library. */
1478 && bfd_is_com_section (sec
)
1481 /* It would be best if we could set the hash table entry to a
1482 common symbol, but we don't know what to use for the section
1483 or the alignment. */
1484 if (! ((*info
->callbacks
->multiple_common
)
1485 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1486 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1489 /* If the presumed common symbol in the dynamic object is
1490 larger, pretend that the new symbol has its size. */
1492 if (h
->size
> *pvalue
)
1495 /* We need to remember the alignment required by the symbol
1496 in the dynamic object. */
1497 BFD_ASSERT (pold_alignment
);
1498 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1501 olddyncommon
= FALSE
;
1503 h
->root
.type
= bfd_link_hash_undefined
;
1504 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1506 *size_change_ok
= TRUE
;
1507 *type_change_ok
= TRUE
;
1509 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1512 h
->verinfo
.vertree
= NULL
;
1517 /* Handle the case where we had a versioned symbol in a dynamic
1518 library and now find a definition in a normal object. In this
1519 case, we make the versioned symbol point to the normal one. */
1520 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1521 flip
->root
.type
= h
->root
.type
;
1522 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1523 h
->root
.type
= bfd_link_hash_indirect
;
1524 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1525 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1529 flip
->ref_dynamic
= 1;
1536 /* This function is called to create an indirect symbol from the
1537 default for the symbol with the default version if needed. The
1538 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1539 set DYNSYM if the new indirect symbol is dynamic. */
1542 _bfd_elf_add_default_symbol (bfd
*abfd
,
1543 struct bfd_link_info
*info
,
1544 struct elf_link_hash_entry
*h
,
1546 Elf_Internal_Sym
*sym
,
1549 bfd_boolean
*dynsym
,
1550 bfd_boolean override
)
1552 bfd_boolean type_change_ok
;
1553 bfd_boolean size_change_ok
;
1556 struct elf_link_hash_entry
*hi
;
1557 struct bfd_link_hash_entry
*bh
;
1558 const struct elf_backend_data
*bed
;
1559 bfd_boolean collect
;
1560 bfd_boolean dynamic
;
1562 size_t len
, shortlen
;
1565 /* If this symbol has a version, and it is the default version, we
1566 create an indirect symbol from the default name to the fully
1567 decorated name. This will cause external references which do not
1568 specify a version to be bound to this version of the symbol. */
1569 p
= strchr (name
, ELF_VER_CHR
);
1570 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1575 /* We are overridden by an old definition. We need to check if we
1576 need to create the indirect symbol from the default name. */
1577 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1579 BFD_ASSERT (hi
!= NULL
);
1582 while (hi
->root
.type
== bfd_link_hash_indirect
1583 || hi
->root
.type
== bfd_link_hash_warning
)
1585 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1591 bed
= get_elf_backend_data (abfd
);
1592 collect
= bed
->collect
;
1593 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1595 shortlen
= p
- name
;
1596 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1597 if (shortname
== NULL
)
1599 memcpy (shortname
, name
, shortlen
);
1600 shortname
[shortlen
] = '\0';
1602 /* We are going to create a new symbol. Merge it with any existing
1603 symbol with this name. For the purposes of the merge, act as
1604 though we were defining the symbol we just defined, although we
1605 actually going to define an indirect symbol. */
1606 type_change_ok
= FALSE
;
1607 size_change_ok
= FALSE
;
1609 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1610 NULL
, &hi
, &skip
, &override
,
1611 &type_change_ok
, &size_change_ok
))
1620 if (! (_bfd_generic_link_add_one_symbol
1621 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1622 0, name
, FALSE
, collect
, &bh
)))
1624 hi
= (struct elf_link_hash_entry
*) bh
;
1628 /* In this case the symbol named SHORTNAME is overriding the
1629 indirect symbol we want to add. We were planning on making
1630 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1631 is the name without a version. NAME is the fully versioned
1632 name, and it is the default version.
1634 Overriding means that we already saw a definition for the
1635 symbol SHORTNAME in a regular object, and it is overriding
1636 the symbol defined in the dynamic object.
1638 When this happens, we actually want to change NAME, the
1639 symbol we just added, to refer to SHORTNAME. This will cause
1640 references to NAME in the shared object to become references
1641 to SHORTNAME in the regular object. This is what we expect
1642 when we override a function in a shared object: that the
1643 references in the shared object will be mapped to the
1644 definition in the regular object. */
1646 while (hi
->root
.type
== bfd_link_hash_indirect
1647 || hi
->root
.type
== bfd_link_hash_warning
)
1648 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1650 h
->root
.type
= bfd_link_hash_indirect
;
1651 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1655 hi
->ref_dynamic
= 1;
1659 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1664 /* Now set HI to H, so that the following code will set the
1665 other fields correctly. */
1669 /* Check if HI is a warning symbol. */
1670 if (hi
->root
.type
== bfd_link_hash_warning
)
1671 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1673 /* If there is a duplicate definition somewhere, then HI may not
1674 point to an indirect symbol. We will have reported an error to
1675 the user in that case. */
1677 if (hi
->root
.type
== bfd_link_hash_indirect
)
1679 struct elf_link_hash_entry
*ht
;
1681 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1682 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
1684 /* See if the new flags lead us to realize that the symbol must
1696 if (hi
->ref_regular
)
1702 /* We also need to define an indirection from the nondefault version
1706 len
= strlen (name
);
1707 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1708 if (shortname
== NULL
)
1710 memcpy (shortname
, name
, shortlen
);
1711 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1713 /* Once again, merge with any existing symbol. */
1714 type_change_ok
= FALSE
;
1715 size_change_ok
= FALSE
;
1717 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1718 NULL
, &hi
, &skip
, &override
,
1719 &type_change_ok
, &size_change_ok
))
1727 /* Here SHORTNAME is a versioned name, so we don't expect to see
1728 the type of override we do in the case above unless it is
1729 overridden by a versioned definition. */
1730 if (hi
->root
.type
!= bfd_link_hash_defined
1731 && hi
->root
.type
!= bfd_link_hash_defweak
)
1732 (*_bfd_error_handler
)
1733 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1739 if (! (_bfd_generic_link_add_one_symbol
1740 (info
, abfd
, shortname
, BSF_INDIRECT
,
1741 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1743 hi
= (struct elf_link_hash_entry
*) bh
;
1745 /* If there is a duplicate definition somewhere, then HI may not
1746 point to an indirect symbol. We will have reported an error
1747 to the user in that case. */
1749 if (hi
->root
.type
== bfd_link_hash_indirect
)
1751 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
1753 /* See if the new flags lead us to realize that the symbol
1765 if (hi
->ref_regular
)
1775 /* This routine is used to export all defined symbols into the dynamic
1776 symbol table. It is called via elf_link_hash_traverse. */
1779 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1781 struct elf_info_failed
*eif
= data
;
1783 /* Ignore this if we won't export it. */
1784 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
1787 /* Ignore indirect symbols. These are added by the versioning code. */
1788 if (h
->root
.type
== bfd_link_hash_indirect
)
1791 if (h
->root
.type
== bfd_link_hash_warning
)
1792 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1794 if (h
->dynindx
== -1
1798 struct bfd_elf_version_tree
*t
;
1799 struct bfd_elf_version_expr
*d
;
1801 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1803 if (t
->globals
.list
!= NULL
)
1805 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1810 if (t
->locals
.list
!= NULL
)
1812 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1821 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1832 /* Look through the symbols which are defined in other shared
1833 libraries and referenced here. Update the list of version
1834 dependencies. This will be put into the .gnu.version_r section.
1835 This function is called via elf_link_hash_traverse. */
1838 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1841 struct elf_find_verdep_info
*rinfo
= data
;
1842 Elf_Internal_Verneed
*t
;
1843 Elf_Internal_Vernaux
*a
;
1846 if (h
->root
.type
== bfd_link_hash_warning
)
1847 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1849 /* We only care about symbols defined in shared objects with version
1854 || h
->verinfo
.verdef
== NULL
)
1857 /* See if we already know about this version. */
1858 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1860 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1863 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1864 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1870 /* This is a new version. Add it to tree we are building. */
1875 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1878 rinfo
->failed
= TRUE
;
1882 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1883 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1884 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1888 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1891 rinfo
->failed
= TRUE
;
1895 /* Note that we are copying a string pointer here, and testing it
1896 above. If bfd_elf_string_from_elf_section is ever changed to
1897 discard the string data when low in memory, this will have to be
1899 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1901 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1902 a
->vna_nextptr
= t
->vn_auxptr
;
1904 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1907 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1914 /* Figure out appropriate versions for all the symbols. We may not
1915 have the version number script until we have read all of the input
1916 files, so until that point we don't know which symbols should be
1917 local. This function is called via elf_link_hash_traverse. */
1920 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1922 struct elf_assign_sym_version_info
*sinfo
;
1923 struct bfd_link_info
*info
;
1924 const struct elf_backend_data
*bed
;
1925 struct elf_info_failed eif
;
1932 if (h
->root
.type
== bfd_link_hash_warning
)
1933 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1935 /* Fix the symbol flags. */
1938 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1941 sinfo
->failed
= TRUE
;
1945 /* We only need version numbers for symbols defined in regular
1947 if (!h
->def_regular
)
1950 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1951 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1952 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1954 struct bfd_elf_version_tree
*t
;
1959 /* There are two consecutive ELF_VER_CHR characters if this is
1960 not a hidden symbol. */
1962 if (*p
== ELF_VER_CHR
)
1968 /* If there is no version string, we can just return out. */
1976 /* Look for the version. If we find it, it is no longer weak. */
1977 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1979 if (strcmp (t
->name
, p
) == 0)
1983 struct bfd_elf_version_expr
*d
;
1985 len
= p
- h
->root
.root
.string
;
1986 alc
= bfd_malloc (len
);
1989 sinfo
->failed
= TRUE
;
1992 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1993 alc
[len
- 1] = '\0';
1994 if (alc
[len
- 2] == ELF_VER_CHR
)
1995 alc
[len
- 2] = '\0';
1997 h
->verinfo
.vertree
= t
;
2001 if (t
->globals
.list
!= NULL
)
2002 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2004 /* See if there is anything to force this symbol to
2006 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2008 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2011 && ! info
->export_dynamic
)
2012 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2020 /* If we are building an application, we need to create a
2021 version node for this version. */
2022 if (t
== NULL
&& info
->executable
)
2024 struct bfd_elf_version_tree
**pp
;
2027 /* If we aren't going to export this symbol, we don't need
2028 to worry about it. */
2029 if (h
->dynindx
== -1)
2033 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
2036 sinfo
->failed
= TRUE
;
2041 t
->name_indx
= (unsigned int) -1;
2045 /* Don't count anonymous version tag. */
2046 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
2048 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
2050 t
->vernum
= version_index
;
2054 h
->verinfo
.vertree
= t
;
2058 /* We could not find the version for a symbol when
2059 generating a shared archive. Return an error. */
2060 (*_bfd_error_handler
)
2061 (_("%B: version node not found for symbol %s"),
2062 sinfo
->output_bfd
, h
->root
.root
.string
);
2063 bfd_set_error (bfd_error_bad_value
);
2064 sinfo
->failed
= TRUE
;
2072 /* If we don't have a version for this symbol, see if we can find
2074 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
2076 struct bfd_elf_version_tree
*t
;
2077 struct bfd_elf_version_tree
*local_ver
, *global_ver
, *exist_ver
;
2078 struct bfd_elf_version_expr
*d
;
2080 /* See if can find what version this symbol is in. If the
2081 symbol is supposed to be local, then don't actually register
2086 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
2088 if (t
->globals
.list
!= NULL
)
2091 while ((d
= (*t
->match
) (&t
->globals
, d
,
2092 h
->root
.root
.string
)) != NULL
)
2099 /* If the match is a wildcard pattern, keep looking for
2100 a more explicit, perhaps even local, match. */
2109 if (t
->locals
.list
!= NULL
)
2112 while ((d
= (*t
->match
) (&t
->locals
, d
,
2113 h
->root
.root
.string
)) != NULL
)
2116 /* If the match is a wildcard pattern, keep looking for
2117 a more explicit, perhaps even global, match. */
2120 /* An exact match overrides a global wildcard. */
2131 if (global_ver
!= NULL
)
2133 h
->verinfo
.vertree
= global_ver
;
2134 /* If we already have a versioned symbol that matches the
2135 node for this symbol, then we don't want to create a
2136 duplicate from the unversioned symbol. Instead hide the
2137 unversioned symbol. */
2138 if (exist_ver
== global_ver
)
2139 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2141 else if (local_ver
!= NULL
)
2143 h
->verinfo
.vertree
= local_ver
;
2144 if (!info
->export_dynamic
2145 || exist_ver
== local_ver
)
2146 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2153 /* Read and swap the relocs from the section indicated by SHDR. This
2154 may be either a REL or a RELA section. The relocations are
2155 translated into RELA relocations and stored in INTERNAL_RELOCS,
2156 which should have already been allocated to contain enough space.
2157 The EXTERNAL_RELOCS are a buffer where the external form of the
2158 relocations should be stored.
2160 Returns FALSE if something goes wrong. */
2163 elf_link_read_relocs_from_section (bfd
*abfd
,
2165 Elf_Internal_Shdr
*shdr
,
2166 void *external_relocs
,
2167 Elf_Internal_Rela
*internal_relocs
)
2169 const struct elf_backend_data
*bed
;
2170 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2171 const bfd_byte
*erela
;
2172 const bfd_byte
*erelaend
;
2173 Elf_Internal_Rela
*irela
;
2174 Elf_Internal_Shdr
*symtab_hdr
;
2177 /* Position ourselves at the start of the section. */
2178 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2181 /* Read the relocations. */
2182 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2185 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2186 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
2188 bed
= get_elf_backend_data (abfd
);
2190 /* Convert the external relocations to the internal format. */
2191 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2192 swap_in
= bed
->s
->swap_reloc_in
;
2193 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2194 swap_in
= bed
->s
->swap_reloca_in
;
2197 bfd_set_error (bfd_error_wrong_format
);
2201 erela
= external_relocs
;
2202 erelaend
= erela
+ shdr
->sh_size
;
2203 irela
= internal_relocs
;
2204 while (erela
< erelaend
)
2208 (*swap_in
) (abfd
, erela
, irela
);
2209 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2210 if (bed
->s
->arch_size
== 64)
2212 if ((size_t) r_symndx
>= nsyms
)
2214 (*_bfd_error_handler
)
2215 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2216 " for offset 0x%lx in section `%A'"),
2218 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2219 bfd_set_error (bfd_error_bad_value
);
2222 irela
+= bed
->s
->int_rels_per_ext_rel
;
2223 erela
+= shdr
->sh_entsize
;
2229 /* Read and swap the relocs for a section O. They may have been
2230 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2231 not NULL, they are used as buffers to read into. They are known to
2232 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2233 the return value is allocated using either malloc or bfd_alloc,
2234 according to the KEEP_MEMORY argument. If O has two relocation
2235 sections (both REL and RELA relocations), then the REL_HDR
2236 relocations will appear first in INTERNAL_RELOCS, followed by the
2237 REL_HDR2 relocations. */
2240 _bfd_elf_link_read_relocs (bfd
*abfd
,
2242 void *external_relocs
,
2243 Elf_Internal_Rela
*internal_relocs
,
2244 bfd_boolean keep_memory
)
2246 Elf_Internal_Shdr
*rel_hdr
;
2247 void *alloc1
= NULL
;
2248 Elf_Internal_Rela
*alloc2
= NULL
;
2249 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2251 if (elf_section_data (o
)->relocs
!= NULL
)
2252 return elf_section_data (o
)->relocs
;
2254 if (o
->reloc_count
== 0)
2257 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2259 if (internal_relocs
== NULL
)
2263 size
= o
->reloc_count
;
2264 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2266 internal_relocs
= alloc2
= bfd_alloc (abfd
, size
);
2268 internal_relocs
= alloc2
= bfd_malloc (size
);
2269 if (internal_relocs
== NULL
)
2273 if (external_relocs
== NULL
)
2275 bfd_size_type size
= rel_hdr
->sh_size
;
2277 if (elf_section_data (o
)->rel_hdr2
)
2278 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2279 alloc1
= bfd_malloc (size
);
2282 external_relocs
= alloc1
;
2285 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
2289 if (elf_section_data (o
)->rel_hdr2
2290 && (!elf_link_read_relocs_from_section
2292 elf_section_data (o
)->rel_hdr2
,
2293 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2294 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2295 * bed
->s
->int_rels_per_ext_rel
))))
2298 /* Cache the results for next time, if we can. */
2300 elf_section_data (o
)->relocs
= internal_relocs
;
2305 /* Don't free alloc2, since if it was allocated we are passing it
2306 back (under the name of internal_relocs). */
2308 return internal_relocs
;
2316 bfd_release (abfd
, alloc2
);
2323 /* Compute the size of, and allocate space for, REL_HDR which is the
2324 section header for a section containing relocations for O. */
2327 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2328 Elf_Internal_Shdr
*rel_hdr
,
2331 bfd_size_type reloc_count
;
2332 bfd_size_type num_rel_hashes
;
2334 /* Figure out how many relocations there will be. */
2335 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2336 reloc_count
= elf_section_data (o
)->rel_count
;
2338 reloc_count
= elf_section_data (o
)->rel_count2
;
2340 num_rel_hashes
= o
->reloc_count
;
2341 if (num_rel_hashes
< reloc_count
)
2342 num_rel_hashes
= reloc_count
;
2344 /* That allows us to calculate the size of the section. */
2345 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2347 /* The contents field must last into write_object_contents, so we
2348 allocate it with bfd_alloc rather than malloc. Also since we
2349 cannot be sure that the contents will actually be filled in,
2350 we zero the allocated space. */
2351 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2352 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2355 /* We only allocate one set of hash entries, so we only do it the
2356 first time we are called. */
2357 if (elf_section_data (o
)->rel_hashes
== NULL
2360 struct elf_link_hash_entry
**p
;
2362 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2366 elf_section_data (o
)->rel_hashes
= p
;
2372 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2373 originated from the section given by INPUT_REL_HDR) to the
2377 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2378 asection
*input_section
,
2379 Elf_Internal_Shdr
*input_rel_hdr
,
2380 Elf_Internal_Rela
*internal_relocs
,
2381 struct elf_link_hash_entry
**rel_hash
2384 Elf_Internal_Rela
*irela
;
2385 Elf_Internal_Rela
*irelaend
;
2387 Elf_Internal_Shdr
*output_rel_hdr
;
2388 asection
*output_section
;
2389 unsigned int *rel_countp
= NULL
;
2390 const struct elf_backend_data
*bed
;
2391 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2393 output_section
= input_section
->output_section
;
2394 output_rel_hdr
= NULL
;
2396 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2397 == input_rel_hdr
->sh_entsize
)
2399 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2400 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2402 else if (elf_section_data (output_section
)->rel_hdr2
2403 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2404 == input_rel_hdr
->sh_entsize
))
2406 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2407 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2411 (*_bfd_error_handler
)
2412 (_("%B: relocation size mismatch in %B section %A"),
2413 output_bfd
, input_section
->owner
, input_section
);
2414 bfd_set_error (bfd_error_wrong_format
);
2418 bed
= get_elf_backend_data (output_bfd
);
2419 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2420 swap_out
= bed
->s
->swap_reloc_out
;
2421 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2422 swap_out
= bed
->s
->swap_reloca_out
;
2426 erel
= output_rel_hdr
->contents
;
2427 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2428 irela
= internal_relocs
;
2429 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2430 * bed
->s
->int_rels_per_ext_rel
);
2431 while (irela
< irelaend
)
2433 (*swap_out
) (output_bfd
, irela
, erel
);
2434 irela
+= bed
->s
->int_rels_per_ext_rel
;
2435 erel
+= input_rel_hdr
->sh_entsize
;
2438 /* Bump the counter, so that we know where to add the next set of
2440 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2445 /* Make weak undefined symbols in PIE dynamic. */
2448 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2449 struct elf_link_hash_entry
*h
)
2453 && h
->root
.type
== bfd_link_hash_undefweak
)
2454 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2459 /* Fix up the flags for a symbol. This handles various cases which
2460 can only be fixed after all the input files are seen. This is
2461 currently called by both adjust_dynamic_symbol and
2462 assign_sym_version, which is unnecessary but perhaps more robust in
2463 the face of future changes. */
2466 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2467 struct elf_info_failed
*eif
)
2469 const struct elf_backend_data
*bed
;
2471 /* If this symbol was mentioned in a non-ELF file, try to set
2472 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2473 permit a non-ELF file to correctly refer to a symbol defined in
2474 an ELF dynamic object. */
2477 while (h
->root
.type
== bfd_link_hash_indirect
)
2478 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2480 if (h
->root
.type
!= bfd_link_hash_defined
2481 && h
->root
.type
!= bfd_link_hash_defweak
)
2484 h
->ref_regular_nonweak
= 1;
2488 if (h
->root
.u
.def
.section
->owner
!= NULL
2489 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2490 == bfd_target_elf_flavour
))
2493 h
->ref_regular_nonweak
= 1;
2499 if (h
->dynindx
== -1
2503 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2512 /* Unfortunately, NON_ELF is only correct if the symbol
2513 was first seen in a non-ELF file. Fortunately, if the symbol
2514 was first seen in an ELF file, we're probably OK unless the
2515 symbol was defined in a non-ELF file. Catch that case here.
2516 FIXME: We're still in trouble if the symbol was first seen in
2517 a dynamic object, and then later in a non-ELF regular object. */
2518 if ((h
->root
.type
== bfd_link_hash_defined
2519 || h
->root
.type
== bfd_link_hash_defweak
)
2521 && (h
->root
.u
.def
.section
->owner
!= NULL
2522 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2523 != bfd_target_elf_flavour
)
2524 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2525 && !h
->def_dynamic
)))
2529 /* Backend specific symbol fixup. */
2530 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2531 if (bed
->elf_backend_fixup_symbol
2532 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2535 /* If this is a final link, and the symbol was defined as a common
2536 symbol in a regular object file, and there was no definition in
2537 any dynamic object, then the linker will have allocated space for
2538 the symbol in a common section but the DEF_REGULAR
2539 flag will not have been set. */
2540 if (h
->root
.type
== bfd_link_hash_defined
2544 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2547 /* If -Bsymbolic was used (which means to bind references to global
2548 symbols to the definition within the shared object), and this
2549 symbol was defined in a regular object, then it actually doesn't
2550 need a PLT entry. Likewise, if the symbol has non-default
2551 visibility. If the symbol has hidden or internal visibility, we
2552 will force it local. */
2554 && eif
->info
->shared
2555 && is_elf_hash_table (eif
->info
->hash
)
2556 && (SYMBOLIC_BIND (eif
->info
, h
)
2557 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2560 bfd_boolean force_local
;
2562 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2563 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2564 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2567 /* If a weak undefined symbol has non-default visibility, we also
2568 hide it from the dynamic linker. */
2569 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2570 && h
->root
.type
== bfd_link_hash_undefweak
)
2571 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2573 /* If this is a weak defined symbol in a dynamic object, and we know
2574 the real definition in the dynamic object, copy interesting flags
2575 over to the real definition. */
2576 if (h
->u
.weakdef
!= NULL
)
2578 struct elf_link_hash_entry
*weakdef
;
2580 weakdef
= h
->u
.weakdef
;
2581 if (h
->root
.type
== bfd_link_hash_indirect
)
2582 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2584 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2585 || h
->root
.type
== bfd_link_hash_defweak
);
2586 BFD_ASSERT (weakdef
->def_dynamic
);
2588 /* If the real definition is defined by a regular object file,
2589 don't do anything special. See the longer description in
2590 _bfd_elf_adjust_dynamic_symbol, below. */
2591 if (weakdef
->def_regular
)
2592 h
->u
.weakdef
= NULL
;
2595 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2596 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2597 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
, h
);
2604 /* Make the backend pick a good value for a dynamic symbol. This is
2605 called via elf_link_hash_traverse, and also calls itself
2609 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2611 struct elf_info_failed
*eif
= data
;
2613 const struct elf_backend_data
*bed
;
2615 if (! is_elf_hash_table (eif
->info
->hash
))
2618 if (h
->root
.type
== bfd_link_hash_warning
)
2620 h
->got
= elf_hash_table (eif
->info
)->init_got_offset
;
2621 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2623 /* When warning symbols are created, they **replace** the "real"
2624 entry in the hash table, thus we never get to see the real
2625 symbol in a hash traversal. So look at it now. */
2626 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2629 /* Ignore indirect symbols. These are added by the versioning code. */
2630 if (h
->root
.type
== bfd_link_hash_indirect
)
2633 /* Fix the symbol flags. */
2634 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2637 /* If this symbol does not require a PLT entry, and it is not
2638 defined by a dynamic object, or is not referenced by a regular
2639 object, ignore it. We do have to handle a weak defined symbol,
2640 even if no regular object refers to it, if we decided to add it
2641 to the dynamic symbol table. FIXME: Do we normally need to worry
2642 about symbols which are defined by one dynamic object and
2643 referenced by another one? */
2648 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2650 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2654 /* If we've already adjusted this symbol, don't do it again. This
2655 can happen via a recursive call. */
2656 if (h
->dynamic_adjusted
)
2659 /* Don't look at this symbol again. Note that we must set this
2660 after checking the above conditions, because we may look at a
2661 symbol once, decide not to do anything, and then get called
2662 recursively later after REF_REGULAR is set below. */
2663 h
->dynamic_adjusted
= 1;
2665 /* If this is a weak definition, and we know a real definition, and
2666 the real symbol is not itself defined by a regular object file,
2667 then get a good value for the real definition. We handle the
2668 real symbol first, for the convenience of the backend routine.
2670 Note that there is a confusing case here. If the real definition
2671 is defined by a regular object file, we don't get the real symbol
2672 from the dynamic object, but we do get the weak symbol. If the
2673 processor backend uses a COPY reloc, then if some routine in the
2674 dynamic object changes the real symbol, we will not see that
2675 change in the corresponding weak symbol. This is the way other
2676 ELF linkers work as well, and seems to be a result of the shared
2679 I will clarify this issue. Most SVR4 shared libraries define the
2680 variable _timezone and define timezone as a weak synonym. The
2681 tzset call changes _timezone. If you write
2682 extern int timezone;
2684 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2685 you might expect that, since timezone is a synonym for _timezone,
2686 the same number will print both times. However, if the processor
2687 backend uses a COPY reloc, then actually timezone will be copied
2688 into your process image, and, since you define _timezone
2689 yourself, _timezone will not. Thus timezone and _timezone will
2690 wind up at different memory locations. The tzset call will set
2691 _timezone, leaving timezone unchanged. */
2693 if (h
->u
.weakdef
!= NULL
)
2695 /* If we get to this point, we know there is an implicit
2696 reference by a regular object file via the weak symbol H.
2697 FIXME: Is this really true? What if the traversal finds
2698 H->U.WEAKDEF before it finds H? */
2699 h
->u
.weakdef
->ref_regular
= 1;
2701 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2705 /* If a symbol has no type and no size and does not require a PLT
2706 entry, then we are probably about to do the wrong thing here: we
2707 are probably going to create a COPY reloc for an empty object.
2708 This case can arise when a shared object is built with assembly
2709 code, and the assembly code fails to set the symbol type. */
2711 && h
->type
== STT_NOTYPE
2713 (*_bfd_error_handler
)
2714 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2715 h
->root
.root
.string
);
2717 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2718 bed
= get_elf_backend_data (dynobj
);
2720 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2729 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2733 _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry
*h
,
2736 unsigned int power_of_two
;
2738 asection
*sec
= h
->root
.u
.def
.section
;
2740 /* The section aligment of definition is the maximum alignment
2741 requirement of symbols defined in the section. Since we don't
2742 know the symbol alignment requirement, we start with the
2743 maximum alignment and check low bits of the symbol address
2744 for the minimum alignment. */
2745 power_of_two
= bfd_get_section_alignment (sec
->owner
, sec
);
2746 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
2747 while ((h
->root
.u
.def
.value
& mask
) != 0)
2753 if (power_of_two
> bfd_get_section_alignment (dynbss
->owner
,
2756 /* Adjust the section alignment if needed. */
2757 if (! bfd_set_section_alignment (dynbss
->owner
, dynbss
,
2762 /* We make sure that the symbol will be aligned properly. */
2763 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
2765 /* Define the symbol as being at this point in DYNBSS. */
2766 h
->root
.u
.def
.section
= dynbss
;
2767 h
->root
.u
.def
.value
= dynbss
->size
;
2769 /* Increment the size of DYNBSS to make room for the symbol. */
2770 dynbss
->size
+= h
->size
;
2775 /* Adjust all external symbols pointing into SEC_MERGE sections
2776 to reflect the object merging within the sections. */
2779 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2783 if (h
->root
.type
== bfd_link_hash_warning
)
2784 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2786 if ((h
->root
.type
== bfd_link_hash_defined
2787 || h
->root
.type
== bfd_link_hash_defweak
)
2788 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2789 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2791 bfd
*output_bfd
= data
;
2793 h
->root
.u
.def
.value
=
2794 _bfd_merged_section_offset (output_bfd
,
2795 &h
->root
.u
.def
.section
,
2796 elf_section_data (sec
)->sec_info
,
2797 h
->root
.u
.def
.value
);
2803 /* Returns false if the symbol referred to by H should be considered
2804 to resolve local to the current module, and true if it should be
2805 considered to bind dynamically. */
2808 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2809 struct bfd_link_info
*info
,
2810 bfd_boolean ignore_protected
)
2812 bfd_boolean binding_stays_local_p
;
2813 const struct elf_backend_data
*bed
;
2814 struct elf_link_hash_table
*hash_table
;
2819 while (h
->root
.type
== bfd_link_hash_indirect
2820 || h
->root
.type
== bfd_link_hash_warning
)
2821 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2823 /* If it was forced local, then clearly it's not dynamic. */
2824 if (h
->dynindx
== -1)
2826 if (h
->forced_local
)
2829 /* Identify the cases where name binding rules say that a
2830 visible symbol resolves locally. */
2831 binding_stays_local_p
= info
->executable
|| SYMBOLIC_BIND (info
, h
);
2833 switch (ELF_ST_VISIBILITY (h
->other
))
2840 hash_table
= elf_hash_table (info
);
2841 if (!is_elf_hash_table (hash_table
))
2844 bed
= get_elf_backend_data (hash_table
->dynobj
);
2846 /* Proper resolution for function pointer equality may require
2847 that these symbols perhaps be resolved dynamically, even though
2848 we should be resolving them to the current module. */
2849 if (!ignore_protected
|| !bed
->is_function_type (h
->type
))
2850 binding_stays_local_p
= TRUE
;
2857 /* If it isn't defined locally, then clearly it's dynamic. */
2858 if (!h
->def_regular
)
2861 /* Otherwise, the symbol is dynamic if binding rules don't tell
2862 us that it remains local. */
2863 return !binding_stays_local_p
;
2866 /* Return true if the symbol referred to by H should be considered
2867 to resolve local to the current module, and false otherwise. Differs
2868 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2869 undefined symbols and weak symbols. */
2872 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2873 struct bfd_link_info
*info
,
2874 bfd_boolean local_protected
)
2876 const struct elf_backend_data
*bed
;
2877 struct elf_link_hash_table
*hash_table
;
2879 /* If it's a local sym, of course we resolve locally. */
2883 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2884 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
2885 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
2888 /* Common symbols that become definitions don't get the DEF_REGULAR
2889 flag set, so test it first, and don't bail out. */
2890 if (ELF_COMMON_DEF_P (h
))
2892 /* If we don't have a definition in a regular file, then we can't
2893 resolve locally. The sym is either undefined or dynamic. */
2894 else if (!h
->def_regular
)
2897 /* Forced local symbols resolve locally. */
2898 if (h
->forced_local
)
2901 /* As do non-dynamic symbols. */
2902 if (h
->dynindx
== -1)
2905 /* At this point, we know the symbol is defined and dynamic. In an
2906 executable it must resolve locally, likewise when building symbolic
2907 shared libraries. */
2908 if (info
->executable
|| SYMBOLIC_BIND (info
, h
))
2911 /* Now deal with defined dynamic symbols in shared libraries. Ones
2912 with default visibility might not resolve locally. */
2913 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2916 hash_table
= elf_hash_table (info
);
2917 if (!is_elf_hash_table (hash_table
))
2920 bed
= get_elf_backend_data (hash_table
->dynobj
);
2922 /* STV_PROTECTED non-function symbols are local. */
2923 if (!bed
->is_function_type (h
->type
))
2926 /* Function pointer equality tests may require that STV_PROTECTED
2927 symbols be treated as dynamic symbols, even when we know that the
2928 dynamic linker will resolve them locally. */
2929 return local_protected
;
2932 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2933 aligned. Returns the first TLS output section. */
2935 struct bfd_section
*
2936 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2938 struct bfd_section
*sec
, *tls
;
2939 unsigned int align
= 0;
2941 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2942 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2946 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2947 if (sec
->alignment_power
> align
)
2948 align
= sec
->alignment_power
;
2950 elf_hash_table (info
)->tls_sec
= tls
;
2952 /* Ensure the alignment of the first section is the largest alignment,
2953 so that the tls segment starts aligned. */
2955 tls
->alignment_power
= align
;
2960 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2962 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2963 Elf_Internal_Sym
*sym
)
2965 const struct elf_backend_data
*bed
;
2967 /* Local symbols do not count, but target specific ones might. */
2968 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2969 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2972 bed
= get_elf_backend_data (abfd
);
2973 /* Function symbols do not count. */
2974 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
2977 /* If the section is undefined, then so is the symbol. */
2978 if (sym
->st_shndx
== SHN_UNDEF
)
2981 /* If the symbol is defined in the common section, then
2982 it is a common definition and so does not count. */
2983 if (bed
->common_definition (sym
))
2986 /* If the symbol is in a target specific section then we
2987 must rely upon the backend to tell us what it is. */
2988 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2989 /* FIXME - this function is not coded yet:
2991 return _bfd_is_global_symbol_definition (abfd, sym);
2993 Instead for now assume that the definition is not global,
2994 Even if this is wrong, at least the linker will behave
2995 in the same way that it used to do. */
3001 /* Search the symbol table of the archive element of the archive ABFD
3002 whose archive map contains a mention of SYMDEF, and determine if
3003 the symbol is defined in this element. */
3005 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3007 Elf_Internal_Shdr
* hdr
;
3008 bfd_size_type symcount
;
3009 bfd_size_type extsymcount
;
3010 bfd_size_type extsymoff
;
3011 Elf_Internal_Sym
*isymbuf
;
3012 Elf_Internal_Sym
*isym
;
3013 Elf_Internal_Sym
*isymend
;
3016 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
3020 if (! bfd_check_format (abfd
, bfd_object
))
3023 /* If we have already included the element containing this symbol in the
3024 link then we do not need to include it again. Just claim that any symbol
3025 it contains is not a definition, so that our caller will not decide to
3026 (re)include this element. */
3027 if (abfd
->archive_pass
)
3030 /* Select the appropriate symbol table. */
3031 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3032 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3034 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3036 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3038 /* The sh_info field of the symtab header tells us where the
3039 external symbols start. We don't care about the local symbols. */
3040 if (elf_bad_symtab (abfd
))
3042 extsymcount
= symcount
;
3047 extsymcount
= symcount
- hdr
->sh_info
;
3048 extsymoff
= hdr
->sh_info
;
3051 if (extsymcount
== 0)
3054 /* Read in the symbol table. */
3055 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3057 if (isymbuf
== NULL
)
3060 /* Scan the symbol table looking for SYMDEF. */
3062 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3066 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3071 if (strcmp (name
, symdef
->name
) == 0)
3073 result
= is_global_data_symbol_definition (abfd
, isym
);
3083 /* Add an entry to the .dynamic table. */
3086 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3090 struct elf_link_hash_table
*hash_table
;
3091 const struct elf_backend_data
*bed
;
3093 bfd_size_type newsize
;
3094 bfd_byte
*newcontents
;
3095 Elf_Internal_Dyn dyn
;
3097 hash_table
= elf_hash_table (info
);
3098 if (! is_elf_hash_table (hash_table
))
3101 bed
= get_elf_backend_data (hash_table
->dynobj
);
3102 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
3103 BFD_ASSERT (s
!= NULL
);
3105 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3106 newcontents
= bfd_realloc (s
->contents
, newsize
);
3107 if (newcontents
== NULL
)
3111 dyn
.d_un
.d_val
= val
;
3112 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3115 s
->contents
= newcontents
;
3120 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3121 otherwise just check whether one already exists. Returns -1 on error,
3122 1 if a DT_NEEDED tag already exists, and 0 on success. */
3125 elf_add_dt_needed_tag (bfd
*abfd
,
3126 struct bfd_link_info
*info
,
3130 struct elf_link_hash_table
*hash_table
;
3131 bfd_size_type oldsize
;
3132 bfd_size_type strindex
;
3134 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3137 hash_table
= elf_hash_table (info
);
3138 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
3139 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3140 if (strindex
== (bfd_size_type
) -1)
3143 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
3146 const struct elf_backend_data
*bed
;
3149 bed
= get_elf_backend_data (hash_table
->dynobj
);
3150 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
3152 for (extdyn
= sdyn
->contents
;
3153 extdyn
< sdyn
->contents
+ sdyn
->size
;
3154 extdyn
+= bed
->s
->sizeof_dyn
)
3156 Elf_Internal_Dyn dyn
;
3158 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3159 if (dyn
.d_tag
== DT_NEEDED
3160 && dyn
.d_un
.d_val
== strindex
)
3162 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3170 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3173 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3177 /* We were just checking for existence of the tag. */
3178 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3183 /* Sort symbol by value and section. */
3185 elf_sort_symbol (const void *arg1
, const void *arg2
)
3187 const struct elf_link_hash_entry
*h1
;
3188 const struct elf_link_hash_entry
*h2
;
3189 bfd_signed_vma vdiff
;
3191 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3192 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3193 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3195 return vdiff
> 0 ? 1 : -1;
3198 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3200 return sdiff
> 0 ? 1 : -1;
3205 /* This function is used to adjust offsets into .dynstr for
3206 dynamic symbols. This is called via elf_link_hash_traverse. */
3209 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3211 struct elf_strtab_hash
*dynstr
= data
;
3213 if (h
->root
.type
== bfd_link_hash_warning
)
3214 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3216 if (h
->dynindx
!= -1)
3217 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3221 /* Assign string offsets in .dynstr, update all structures referencing
3225 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3227 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3228 struct elf_link_local_dynamic_entry
*entry
;
3229 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3230 bfd
*dynobj
= hash_table
->dynobj
;
3233 const struct elf_backend_data
*bed
;
3236 _bfd_elf_strtab_finalize (dynstr
);
3237 size
= _bfd_elf_strtab_size (dynstr
);
3239 bed
= get_elf_backend_data (dynobj
);
3240 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3241 BFD_ASSERT (sdyn
!= NULL
);
3243 /* Update all .dynamic entries referencing .dynstr strings. */
3244 for (extdyn
= sdyn
->contents
;
3245 extdyn
< sdyn
->contents
+ sdyn
->size
;
3246 extdyn
+= bed
->s
->sizeof_dyn
)
3248 Elf_Internal_Dyn dyn
;
3250 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3254 dyn
.d_un
.d_val
= size
;
3262 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3267 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3270 /* Now update local dynamic symbols. */
3271 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3272 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3273 entry
->isym
.st_name
);
3275 /* And the rest of dynamic symbols. */
3276 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3278 /* Adjust version definitions. */
3279 if (elf_tdata (output_bfd
)->cverdefs
)
3284 Elf_Internal_Verdef def
;
3285 Elf_Internal_Verdaux defaux
;
3287 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3291 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3293 p
+= sizeof (Elf_External_Verdef
);
3294 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3296 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3298 _bfd_elf_swap_verdaux_in (output_bfd
,
3299 (Elf_External_Verdaux
*) p
, &defaux
);
3300 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3302 _bfd_elf_swap_verdaux_out (output_bfd
,
3303 &defaux
, (Elf_External_Verdaux
*) p
);
3304 p
+= sizeof (Elf_External_Verdaux
);
3307 while (def
.vd_next
);
3310 /* Adjust version references. */
3311 if (elf_tdata (output_bfd
)->verref
)
3316 Elf_Internal_Verneed need
;
3317 Elf_Internal_Vernaux needaux
;
3319 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3323 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3325 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3326 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3327 (Elf_External_Verneed
*) p
);
3328 p
+= sizeof (Elf_External_Verneed
);
3329 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3331 _bfd_elf_swap_vernaux_in (output_bfd
,
3332 (Elf_External_Vernaux
*) p
, &needaux
);
3333 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3335 _bfd_elf_swap_vernaux_out (output_bfd
,
3337 (Elf_External_Vernaux
*) p
);
3338 p
+= sizeof (Elf_External_Vernaux
);
3341 while (need
.vn_next
);
3347 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3348 The default is to only match when the INPUT and OUTPUT are exactly
3352 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3353 const bfd_target
*output
)
3355 return input
== output
;
3358 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3359 This version is used when different targets for the same architecture
3360 are virtually identical. */
3363 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3364 const bfd_target
*output
)
3366 const struct elf_backend_data
*obed
, *ibed
;
3368 if (input
== output
)
3371 ibed
= xvec_get_elf_backend_data (input
);
3372 obed
= xvec_get_elf_backend_data (output
);
3374 if (ibed
->arch
!= obed
->arch
)
3377 /* If both backends are using this function, deem them compatible. */
3378 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3381 /* Add symbols from an ELF object file to the linker hash table. */
3384 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3386 Elf_Internal_Shdr
*hdr
;
3387 bfd_size_type symcount
;
3388 bfd_size_type extsymcount
;
3389 bfd_size_type extsymoff
;
3390 struct elf_link_hash_entry
**sym_hash
;
3391 bfd_boolean dynamic
;
3392 Elf_External_Versym
*extversym
= NULL
;
3393 Elf_External_Versym
*ever
;
3394 struct elf_link_hash_entry
*weaks
;
3395 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3396 bfd_size_type nondeflt_vers_cnt
= 0;
3397 Elf_Internal_Sym
*isymbuf
= NULL
;
3398 Elf_Internal_Sym
*isym
;
3399 Elf_Internal_Sym
*isymend
;
3400 const struct elf_backend_data
*bed
;
3401 bfd_boolean add_needed
;
3402 struct elf_link_hash_table
*htab
;
3404 void *alloc_mark
= NULL
;
3405 struct bfd_hash_entry
**old_table
= NULL
;
3406 unsigned int old_size
= 0;
3407 unsigned int old_count
= 0;
3408 void *old_tab
= NULL
;
3411 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3412 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3413 long old_dynsymcount
= 0;
3415 size_t hashsize
= 0;
3417 htab
= elf_hash_table (info
);
3418 bed
= get_elf_backend_data (abfd
);
3420 if ((abfd
->flags
& DYNAMIC
) == 0)
3426 /* You can't use -r against a dynamic object. Also, there's no
3427 hope of using a dynamic object which does not exactly match
3428 the format of the output file. */
3429 if (info
->relocatable
3430 || !is_elf_hash_table (htab
)
3431 || info
->output_bfd
->xvec
!= abfd
->xvec
)
3433 if (info
->relocatable
)
3434 bfd_set_error (bfd_error_invalid_operation
);
3436 bfd_set_error (bfd_error_wrong_format
);
3441 /* As a GNU extension, any input sections which are named
3442 .gnu.warning.SYMBOL are treated as warning symbols for the given
3443 symbol. This differs from .gnu.warning sections, which generate
3444 warnings when they are included in an output file. */
3445 if (info
->executable
)
3449 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3453 name
= bfd_get_section_name (abfd
, s
);
3454 if (CONST_STRNEQ (name
, ".gnu.warning."))
3459 name
+= sizeof ".gnu.warning." - 1;
3461 /* If this is a shared object, then look up the symbol
3462 in the hash table. If it is there, and it is already
3463 been defined, then we will not be using the entry
3464 from this shared object, so we don't need to warn.
3465 FIXME: If we see the definition in a regular object
3466 later on, we will warn, but we shouldn't. The only
3467 fix is to keep track of what warnings we are supposed
3468 to emit, and then handle them all at the end of the
3472 struct elf_link_hash_entry
*h
;
3474 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3476 /* FIXME: What about bfd_link_hash_common? */
3478 && (h
->root
.type
== bfd_link_hash_defined
3479 || h
->root
.type
== bfd_link_hash_defweak
))
3481 /* We don't want to issue this warning. Clobber
3482 the section size so that the warning does not
3483 get copied into the output file. */
3490 msg
= bfd_alloc (abfd
, sz
+ 1);
3494 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3499 if (! (_bfd_generic_link_add_one_symbol
3500 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3501 FALSE
, bed
->collect
, NULL
)))
3504 if (! info
->relocatable
)
3506 /* Clobber the section size so that the warning does
3507 not get copied into the output file. */
3510 /* Also set SEC_EXCLUDE, so that symbols defined in
3511 the warning section don't get copied to the output. */
3512 s
->flags
|= SEC_EXCLUDE
;
3521 /* If we are creating a shared library, create all the dynamic
3522 sections immediately. We need to attach them to something,
3523 so we attach them to this BFD, provided it is the right
3524 format. FIXME: If there are no input BFD's of the same
3525 format as the output, we can't make a shared library. */
3527 && is_elf_hash_table (htab
)
3528 && info
->output_bfd
->xvec
== abfd
->xvec
3529 && !htab
->dynamic_sections_created
)
3531 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3535 else if (!is_elf_hash_table (htab
))
3540 const char *soname
= NULL
;
3541 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3544 /* ld --just-symbols and dynamic objects don't mix very well.
3545 ld shouldn't allow it. */
3546 if ((s
= abfd
->sections
) != NULL
3547 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3550 /* If this dynamic lib was specified on the command line with
3551 --as-needed in effect, then we don't want to add a DT_NEEDED
3552 tag unless the lib is actually used. Similary for libs brought
3553 in by another lib's DT_NEEDED. When --no-add-needed is used
3554 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3555 any dynamic library in DT_NEEDED tags in the dynamic lib at
3557 add_needed
= (elf_dyn_lib_class (abfd
)
3558 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3559 | DYN_NO_NEEDED
)) == 0;
3561 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3566 unsigned int elfsec
;
3567 unsigned long shlink
;
3569 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3570 goto error_free_dyn
;
3572 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3573 if (elfsec
== SHN_BAD
)
3574 goto error_free_dyn
;
3575 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3577 for (extdyn
= dynbuf
;
3578 extdyn
< dynbuf
+ s
->size
;
3579 extdyn
+= bed
->s
->sizeof_dyn
)
3581 Elf_Internal_Dyn dyn
;
3583 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3584 if (dyn
.d_tag
== DT_SONAME
)
3586 unsigned int tagv
= dyn
.d_un
.d_val
;
3587 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3589 goto error_free_dyn
;
3591 if (dyn
.d_tag
== DT_NEEDED
)
3593 struct bfd_link_needed_list
*n
, **pn
;
3595 unsigned int tagv
= dyn
.d_un
.d_val
;
3597 amt
= sizeof (struct bfd_link_needed_list
);
3598 n
= bfd_alloc (abfd
, amt
);
3599 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3600 if (n
== NULL
|| fnm
== NULL
)
3601 goto error_free_dyn
;
3602 amt
= strlen (fnm
) + 1;
3603 anm
= bfd_alloc (abfd
, amt
);
3605 goto error_free_dyn
;
3606 memcpy (anm
, fnm
, amt
);
3610 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3614 if (dyn
.d_tag
== DT_RUNPATH
)
3616 struct bfd_link_needed_list
*n
, **pn
;
3618 unsigned int tagv
= dyn
.d_un
.d_val
;
3620 amt
= sizeof (struct bfd_link_needed_list
);
3621 n
= bfd_alloc (abfd
, amt
);
3622 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3623 if (n
== NULL
|| fnm
== NULL
)
3624 goto error_free_dyn
;
3625 amt
= strlen (fnm
) + 1;
3626 anm
= bfd_alloc (abfd
, amt
);
3628 goto error_free_dyn
;
3629 memcpy (anm
, fnm
, amt
);
3633 for (pn
= & runpath
;
3639 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3640 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3642 struct bfd_link_needed_list
*n
, **pn
;
3644 unsigned int tagv
= dyn
.d_un
.d_val
;
3646 amt
= sizeof (struct bfd_link_needed_list
);
3647 n
= bfd_alloc (abfd
, amt
);
3648 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3649 if (n
== NULL
|| fnm
== NULL
)
3650 goto error_free_dyn
;
3651 amt
= strlen (fnm
) + 1;
3652 anm
= bfd_alloc (abfd
, amt
);
3659 memcpy (anm
, fnm
, amt
);
3674 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3675 frees all more recently bfd_alloc'd blocks as well. */
3681 struct bfd_link_needed_list
**pn
;
3682 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3687 /* We do not want to include any of the sections in a dynamic
3688 object in the output file. We hack by simply clobbering the
3689 list of sections in the BFD. This could be handled more
3690 cleanly by, say, a new section flag; the existing
3691 SEC_NEVER_LOAD flag is not the one we want, because that one
3692 still implies that the section takes up space in the output
3694 bfd_section_list_clear (abfd
);
3696 /* Find the name to use in a DT_NEEDED entry that refers to this
3697 object. If the object has a DT_SONAME entry, we use it.
3698 Otherwise, if the generic linker stuck something in
3699 elf_dt_name, we use that. Otherwise, we just use the file
3701 if (soname
== NULL
|| *soname
== '\0')
3703 soname
= elf_dt_name (abfd
);
3704 if (soname
== NULL
|| *soname
== '\0')
3705 soname
= bfd_get_filename (abfd
);
3708 /* Save the SONAME because sometimes the linker emulation code
3709 will need to know it. */
3710 elf_dt_name (abfd
) = soname
;
3712 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3716 /* If we have already included this dynamic object in the
3717 link, just ignore it. There is no reason to include a
3718 particular dynamic object more than once. */
3723 /* If this is a dynamic object, we always link against the .dynsym
3724 symbol table, not the .symtab symbol table. The dynamic linker
3725 will only see the .dynsym symbol table, so there is no reason to
3726 look at .symtab for a dynamic object. */
3728 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3729 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3731 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3733 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3735 /* The sh_info field of the symtab header tells us where the
3736 external symbols start. We don't care about the local symbols at
3738 if (elf_bad_symtab (abfd
))
3740 extsymcount
= symcount
;
3745 extsymcount
= symcount
- hdr
->sh_info
;
3746 extsymoff
= hdr
->sh_info
;
3750 if (extsymcount
!= 0)
3752 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3754 if (isymbuf
== NULL
)
3757 /* We store a pointer to the hash table entry for each external
3759 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3760 sym_hash
= bfd_alloc (abfd
, amt
);
3761 if (sym_hash
== NULL
)
3762 goto error_free_sym
;
3763 elf_sym_hashes (abfd
) = sym_hash
;
3768 /* Read in any version definitions. */
3769 if (!_bfd_elf_slurp_version_tables (abfd
,
3770 info
->default_imported_symver
))
3771 goto error_free_sym
;
3773 /* Read in the symbol versions, but don't bother to convert them
3774 to internal format. */
3775 if (elf_dynversym (abfd
) != 0)
3777 Elf_Internal_Shdr
*versymhdr
;
3779 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3780 extversym
= bfd_malloc (versymhdr
->sh_size
);
3781 if (extversym
== NULL
)
3782 goto error_free_sym
;
3783 amt
= versymhdr
->sh_size
;
3784 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3785 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3786 goto error_free_vers
;
3790 /* If we are loading an as-needed shared lib, save the symbol table
3791 state before we start adding symbols. If the lib turns out
3792 to be unneeded, restore the state. */
3793 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
3798 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
3800 struct bfd_hash_entry
*p
;
3801 struct elf_link_hash_entry
*h
;
3803 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3805 h
= (struct elf_link_hash_entry
*) p
;
3806 entsize
+= htab
->root
.table
.entsize
;
3807 if (h
->root
.type
== bfd_link_hash_warning
)
3808 entsize
+= htab
->root
.table
.entsize
;
3812 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
3813 hashsize
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3814 old_tab
= bfd_malloc (tabsize
+ entsize
+ hashsize
);
3815 if (old_tab
== NULL
)
3816 goto error_free_vers
;
3818 /* Remember the current objalloc pointer, so that all mem for
3819 symbols added can later be reclaimed. */
3820 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
3821 if (alloc_mark
== NULL
)
3822 goto error_free_vers
;
3824 /* Make a special call to the linker "notice" function to
3825 tell it that we are about to handle an as-needed lib. */
3826 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
3828 goto error_free_vers
;
3830 /* Clone the symbol table and sym hashes. Remember some
3831 pointers into the symbol table, and dynamic symbol count. */
3832 old_hash
= (char *) old_tab
+ tabsize
;
3833 old_ent
= (char *) old_hash
+ hashsize
;
3834 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
3835 memcpy (old_hash
, sym_hash
, hashsize
);
3836 old_undefs
= htab
->root
.undefs
;
3837 old_undefs_tail
= htab
->root
.undefs_tail
;
3838 old_table
= htab
->root
.table
.table
;
3839 old_size
= htab
->root
.table
.size
;
3840 old_count
= htab
->root
.table
.count
;
3841 old_dynsymcount
= htab
->dynsymcount
;
3843 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
3845 struct bfd_hash_entry
*p
;
3846 struct elf_link_hash_entry
*h
;
3848 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3850 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
3851 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3852 h
= (struct elf_link_hash_entry
*) p
;
3853 if (h
->root
.type
== bfd_link_hash_warning
)
3855 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
3856 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3863 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3864 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3866 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3870 asection
*sec
, *new_sec
;
3873 struct elf_link_hash_entry
*h
;
3874 bfd_boolean definition
;
3875 bfd_boolean size_change_ok
;
3876 bfd_boolean type_change_ok
;
3877 bfd_boolean new_weakdef
;
3878 bfd_boolean override
;
3880 unsigned int old_alignment
;
3885 flags
= BSF_NO_FLAGS
;
3887 value
= isym
->st_value
;
3889 common
= bed
->common_definition (isym
);
3891 bind
= ELF_ST_BIND (isym
->st_info
);
3892 if (bind
== STB_LOCAL
)
3894 /* This should be impossible, since ELF requires that all
3895 global symbols follow all local symbols, and that sh_info
3896 point to the first global symbol. Unfortunately, Irix 5
3900 else if (bind
== STB_GLOBAL
)
3902 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
3905 else if (bind
== STB_WEAK
)
3909 /* Leave it up to the processor backend. */
3912 if (isym
->st_shndx
== SHN_UNDEF
)
3913 sec
= bfd_und_section_ptr
;
3914 else if (isym
->st_shndx
== SHN_ABS
)
3915 sec
= bfd_abs_section_ptr
;
3916 else if (isym
->st_shndx
== SHN_COMMON
)
3918 sec
= bfd_com_section_ptr
;
3919 /* What ELF calls the size we call the value. What ELF
3920 calls the value we call the alignment. */
3921 value
= isym
->st_size
;
3925 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3927 sec
= bfd_abs_section_ptr
;
3928 else if (sec
->kept_section
)
3930 /* Symbols from discarded section are undefined. We keep
3932 sec
= bfd_und_section_ptr
;
3933 isym
->st_shndx
= SHN_UNDEF
;
3935 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3939 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3942 goto error_free_vers
;
3944 if (isym
->st_shndx
== SHN_COMMON
3945 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
3946 && !info
->relocatable
)
3948 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3952 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon",
3955 | SEC_LINKER_CREATED
3956 | SEC_THREAD_LOCAL
));
3958 goto error_free_vers
;
3962 else if (bed
->elf_add_symbol_hook
)
3964 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
3966 goto error_free_vers
;
3968 /* The hook function sets the name to NULL if this symbol
3969 should be skipped for some reason. */
3974 /* Sanity check that all possibilities were handled. */
3977 bfd_set_error (bfd_error_bad_value
);
3978 goto error_free_vers
;
3981 if (bfd_is_und_section (sec
)
3982 || bfd_is_com_section (sec
))
3987 size_change_ok
= FALSE
;
3988 type_change_ok
= bed
->type_change_ok
;
3993 if (is_elf_hash_table (htab
))
3995 Elf_Internal_Versym iver
;
3996 unsigned int vernum
= 0;
4001 if (info
->default_imported_symver
)
4002 /* Use the default symbol version created earlier. */
4003 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4008 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4010 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4012 /* If this is a hidden symbol, or if it is not version
4013 1, we append the version name to the symbol name.
4014 However, we do not modify a non-hidden absolute symbol
4015 if it is not a function, because it might be the version
4016 symbol itself. FIXME: What if it isn't? */
4017 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4019 && (!bfd_is_abs_section (sec
)
4020 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4023 size_t namelen
, verlen
, newlen
;
4026 if (isym
->st_shndx
!= SHN_UNDEF
)
4028 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4030 else if (vernum
> 1)
4032 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4038 (*_bfd_error_handler
)
4039 (_("%B: %s: invalid version %u (max %d)"),
4041 elf_tdata (abfd
)->cverdefs
);
4042 bfd_set_error (bfd_error_bad_value
);
4043 goto error_free_vers
;
4048 /* We cannot simply test for the number of
4049 entries in the VERNEED section since the
4050 numbers for the needed versions do not start
4052 Elf_Internal_Verneed
*t
;
4055 for (t
= elf_tdata (abfd
)->verref
;
4059 Elf_Internal_Vernaux
*a
;
4061 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4063 if (a
->vna_other
== vernum
)
4065 verstr
= a
->vna_nodename
;
4074 (*_bfd_error_handler
)
4075 (_("%B: %s: invalid needed version %d"),
4076 abfd
, name
, vernum
);
4077 bfd_set_error (bfd_error_bad_value
);
4078 goto error_free_vers
;
4082 namelen
= strlen (name
);
4083 verlen
= strlen (verstr
);
4084 newlen
= namelen
+ verlen
+ 2;
4085 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4086 && isym
->st_shndx
!= SHN_UNDEF
)
4089 newname
= bfd_hash_allocate (&htab
->root
.table
, newlen
);
4090 if (newname
== NULL
)
4091 goto error_free_vers
;
4092 memcpy (newname
, name
, namelen
);
4093 p
= newname
+ namelen
;
4095 /* If this is a defined non-hidden version symbol,
4096 we add another @ to the name. This indicates the
4097 default version of the symbol. */
4098 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4099 && isym
->st_shndx
!= SHN_UNDEF
)
4101 memcpy (p
, verstr
, verlen
+ 1);
4106 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
,
4107 &value
, &old_alignment
,
4108 sym_hash
, &skip
, &override
,
4109 &type_change_ok
, &size_change_ok
))
4110 goto error_free_vers
;
4119 while (h
->root
.type
== bfd_link_hash_indirect
4120 || h
->root
.type
== bfd_link_hash_warning
)
4121 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4123 /* Remember the old alignment if this is a common symbol, so
4124 that we don't reduce the alignment later on. We can't
4125 check later, because _bfd_generic_link_add_one_symbol
4126 will set a default for the alignment which we want to
4127 override. We also remember the old bfd where the existing
4128 definition comes from. */
4129 switch (h
->root
.type
)
4134 case bfd_link_hash_defined
:
4135 case bfd_link_hash_defweak
:
4136 old_bfd
= h
->root
.u
.def
.section
->owner
;
4139 case bfd_link_hash_common
:
4140 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
4141 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
4145 if (elf_tdata (abfd
)->verdef
!= NULL
4149 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4152 if (! (_bfd_generic_link_add_one_symbol
4153 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4154 (struct bfd_link_hash_entry
**) sym_hash
)))
4155 goto error_free_vers
;
4158 while (h
->root
.type
== bfd_link_hash_indirect
4159 || h
->root
.type
== bfd_link_hash_warning
)
4160 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4163 new_weakdef
= FALSE
;
4166 && (flags
& BSF_WEAK
) != 0
4167 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4168 && is_elf_hash_table (htab
)
4169 && h
->u
.weakdef
== NULL
)
4171 /* Keep a list of all weak defined non function symbols from
4172 a dynamic object, using the weakdef field. Later in this
4173 function we will set the weakdef field to the correct
4174 value. We only put non-function symbols from dynamic
4175 objects on this list, because that happens to be the only
4176 time we need to know the normal symbol corresponding to a
4177 weak symbol, and the information is time consuming to
4178 figure out. If the weakdef field is not already NULL,
4179 then this symbol was already defined by some previous
4180 dynamic object, and we will be using that previous
4181 definition anyhow. */
4183 h
->u
.weakdef
= weaks
;
4188 /* Set the alignment of a common symbol. */
4189 if ((common
|| bfd_is_com_section (sec
))
4190 && h
->root
.type
== bfd_link_hash_common
)
4195 align
= bfd_log2 (isym
->st_value
);
4198 /* The new symbol is a common symbol in a shared object.
4199 We need to get the alignment from the section. */
4200 align
= new_sec
->alignment_power
;
4202 if (align
> old_alignment
4203 /* Permit an alignment power of zero if an alignment of one
4204 is specified and no other alignments have been specified. */
4205 || (isym
->st_value
== 1 && old_alignment
== 0))
4206 h
->root
.u
.c
.p
->alignment_power
= align
;
4208 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4211 if (is_elf_hash_table (htab
))
4215 /* Check the alignment when a common symbol is involved. This
4216 can change when a common symbol is overridden by a normal
4217 definition or a common symbol is ignored due to the old
4218 normal definition. We need to make sure the maximum
4219 alignment is maintained. */
4220 if ((old_alignment
|| common
)
4221 && h
->root
.type
!= bfd_link_hash_common
)
4223 unsigned int common_align
;
4224 unsigned int normal_align
;
4225 unsigned int symbol_align
;
4229 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4230 if (h
->root
.u
.def
.section
->owner
!= NULL
4231 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
4233 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4234 if (normal_align
> symbol_align
)
4235 normal_align
= symbol_align
;
4238 normal_align
= symbol_align
;
4242 common_align
= old_alignment
;
4243 common_bfd
= old_bfd
;
4248 common_align
= bfd_log2 (isym
->st_value
);
4250 normal_bfd
= old_bfd
;
4253 if (normal_align
< common_align
)
4255 /* PR binutils/2735 */
4256 if (normal_bfd
== NULL
)
4257 (*_bfd_error_handler
)
4258 (_("Warning: alignment %u of common symbol `%s' in %B"
4259 " is greater than the alignment (%u) of its section %A"),
4260 common_bfd
, h
->root
.u
.def
.section
,
4261 1 << common_align
, name
, 1 << normal_align
);
4263 (*_bfd_error_handler
)
4264 (_("Warning: alignment %u of symbol `%s' in %B"
4265 " is smaller than %u in %B"),
4266 normal_bfd
, common_bfd
,
4267 1 << normal_align
, name
, 1 << common_align
);
4271 /* Remember the symbol size if it isn't undefined. */
4272 if ((isym
->st_size
!= 0 && isym
->st_shndx
!= SHN_UNDEF
)
4273 && (definition
|| h
->size
== 0))
4276 && h
->size
!= isym
->st_size
4277 && ! size_change_ok
)
4278 (*_bfd_error_handler
)
4279 (_("Warning: size of symbol `%s' changed"
4280 " from %lu in %B to %lu in %B"),
4282 name
, (unsigned long) h
->size
,
4283 (unsigned long) isym
->st_size
);
4285 h
->size
= isym
->st_size
;
4288 /* If this is a common symbol, then we always want H->SIZE
4289 to be the size of the common symbol. The code just above
4290 won't fix the size if a common symbol becomes larger. We
4291 don't warn about a size change here, because that is
4292 covered by --warn-common. Allow changed between different
4294 if (h
->root
.type
== bfd_link_hash_common
)
4295 h
->size
= h
->root
.u
.c
.size
;
4297 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4298 && (definition
|| h
->type
== STT_NOTYPE
))
4300 if (h
->type
!= STT_NOTYPE
4301 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
4302 && ! type_change_ok
)
4303 (*_bfd_error_handler
)
4304 (_("Warning: type of symbol `%s' changed"
4305 " from %d to %d in %B"),
4306 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
4308 h
->type
= ELF_ST_TYPE (isym
->st_info
);
4311 /* Merge st_other field. */
4312 elf_merge_st_other (abfd
, h
, isym
, definition
, dynamic
);
4314 /* Set a flag in the hash table entry indicating the type of
4315 reference or definition we just found. Keep a count of
4316 the number of dynamic symbols we find. A dynamic symbol
4317 is one which is referenced or defined by both a regular
4318 object and a shared object. */
4325 if (bind
!= STB_WEAK
)
4326 h
->ref_regular_nonweak
= 1;
4338 if (! info
->executable
4351 || (h
->u
.weakdef
!= NULL
4353 && h
->u
.weakdef
->dynindx
!= -1))
4357 if (definition
&& (sec
->flags
& SEC_DEBUGGING
) && !info
->relocatable
)
4359 /* We don't want to make debug symbol dynamic. */
4360 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4364 /* Check to see if we need to add an indirect symbol for
4365 the default name. */
4366 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
4367 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4368 &sec
, &value
, &dynsym
,
4370 goto error_free_vers
;
4372 if (definition
&& !dynamic
)
4374 char *p
= strchr (name
, ELF_VER_CHR
);
4375 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4377 /* Queue non-default versions so that .symver x, x@FOO
4378 aliases can be checked. */
4381 amt
= ((isymend
- isym
+ 1)
4382 * sizeof (struct elf_link_hash_entry
*));
4383 nondeflt_vers
= bfd_malloc (amt
);
4385 goto error_free_vers
;
4387 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4391 if (dynsym
&& h
->dynindx
== -1)
4393 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4394 goto error_free_vers
;
4395 if (h
->u
.weakdef
!= NULL
4397 && h
->u
.weakdef
->dynindx
== -1)
4399 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4400 goto error_free_vers
;
4403 else if (dynsym
&& h
->dynindx
!= -1)
4404 /* If the symbol already has a dynamic index, but
4405 visibility says it should not be visible, turn it into
4407 switch (ELF_ST_VISIBILITY (h
->other
))
4411 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4422 const char *soname
= elf_dt_name (abfd
);
4424 /* A symbol from a library loaded via DT_NEEDED of some
4425 other library is referenced by a regular object.
4426 Add a DT_NEEDED entry for it. Issue an error if
4427 --no-add-needed is used. */
4428 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4430 (*_bfd_error_handler
)
4431 (_("%s: invalid DSO for symbol `%s' definition"),
4433 bfd_set_error (bfd_error_bad_value
);
4434 goto error_free_vers
;
4437 elf_dyn_lib_class (abfd
) &= ~DYN_AS_NEEDED
;
4440 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4442 goto error_free_vers
;
4444 BFD_ASSERT (ret
== 0);
4449 if (extversym
!= NULL
)
4455 if (isymbuf
!= NULL
)
4461 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4465 /* Restore the symbol table. */
4466 if (bed
->as_needed_cleanup
)
4467 (*bed
->as_needed_cleanup
) (abfd
, info
);
4468 old_hash
= (char *) old_tab
+ tabsize
;
4469 old_ent
= (char *) old_hash
+ hashsize
;
4470 sym_hash
= elf_sym_hashes (abfd
);
4471 htab
->root
.table
.table
= old_table
;
4472 htab
->root
.table
.size
= old_size
;
4473 htab
->root
.table
.count
= old_count
;
4474 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4475 memcpy (sym_hash
, old_hash
, hashsize
);
4476 htab
->root
.undefs
= old_undefs
;
4477 htab
->root
.undefs_tail
= old_undefs_tail
;
4478 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4480 struct bfd_hash_entry
*p
;
4481 struct elf_link_hash_entry
*h
;
4483 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4485 h
= (struct elf_link_hash_entry
*) p
;
4486 if (h
->root
.type
== bfd_link_hash_warning
)
4487 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4488 if (h
->dynindx
>= old_dynsymcount
)
4489 _bfd_elf_strtab_delref (htab
->dynstr
, h
->dynstr_index
);
4491 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4492 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4493 h
= (struct elf_link_hash_entry
*) p
;
4494 if (h
->root
.type
== bfd_link_hash_warning
)
4496 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4497 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4502 /* Make a special call to the linker "notice" function to
4503 tell it that symbols added for crefs may need to be removed. */
4504 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
4506 goto error_free_vers
;
4509 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4511 if (nondeflt_vers
!= NULL
)
4512 free (nondeflt_vers
);
4516 if (old_tab
!= NULL
)
4518 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
4520 goto error_free_vers
;
4525 /* Now that all the symbols from this input file are created, handle
4526 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4527 if (nondeflt_vers
!= NULL
)
4529 bfd_size_type cnt
, symidx
;
4531 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4533 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4534 char *shortname
, *p
;
4536 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4538 || (h
->root
.type
!= bfd_link_hash_defined
4539 && h
->root
.type
!= bfd_link_hash_defweak
))
4542 amt
= p
- h
->root
.root
.string
;
4543 shortname
= bfd_malloc (amt
+ 1);
4545 goto error_free_vers
;
4546 memcpy (shortname
, h
->root
.root
.string
, amt
);
4547 shortname
[amt
] = '\0';
4549 hi
= (struct elf_link_hash_entry
*)
4550 bfd_link_hash_lookup (&htab
->root
, shortname
,
4551 FALSE
, FALSE
, FALSE
);
4553 && hi
->root
.type
== h
->root
.type
4554 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4555 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4557 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4558 hi
->root
.type
= bfd_link_hash_indirect
;
4559 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4560 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4561 sym_hash
= elf_sym_hashes (abfd
);
4563 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4564 if (sym_hash
[symidx
] == hi
)
4566 sym_hash
[symidx
] = h
;
4572 free (nondeflt_vers
);
4573 nondeflt_vers
= NULL
;
4576 /* Now set the weakdefs field correctly for all the weak defined
4577 symbols we found. The only way to do this is to search all the
4578 symbols. Since we only need the information for non functions in
4579 dynamic objects, that's the only time we actually put anything on
4580 the list WEAKS. We need this information so that if a regular
4581 object refers to a symbol defined weakly in a dynamic object, the
4582 real symbol in the dynamic object is also put in the dynamic
4583 symbols; we also must arrange for both symbols to point to the
4584 same memory location. We could handle the general case of symbol
4585 aliasing, but a general symbol alias can only be generated in
4586 assembler code, handling it correctly would be very time
4587 consuming, and other ELF linkers don't handle general aliasing
4591 struct elf_link_hash_entry
**hpp
;
4592 struct elf_link_hash_entry
**hppend
;
4593 struct elf_link_hash_entry
**sorted_sym_hash
;
4594 struct elf_link_hash_entry
*h
;
4597 /* Since we have to search the whole symbol list for each weak
4598 defined symbol, search time for N weak defined symbols will be
4599 O(N^2). Binary search will cut it down to O(NlogN). */
4600 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4601 sorted_sym_hash
= bfd_malloc (amt
);
4602 if (sorted_sym_hash
== NULL
)
4604 sym_hash
= sorted_sym_hash
;
4605 hpp
= elf_sym_hashes (abfd
);
4606 hppend
= hpp
+ extsymcount
;
4608 for (; hpp
< hppend
; hpp
++)
4612 && h
->root
.type
== bfd_link_hash_defined
4613 && !bed
->is_function_type (h
->type
))
4621 qsort (sorted_sym_hash
, sym_count
,
4622 sizeof (struct elf_link_hash_entry
*),
4625 while (weaks
!= NULL
)
4627 struct elf_link_hash_entry
*hlook
;
4634 weaks
= hlook
->u
.weakdef
;
4635 hlook
->u
.weakdef
= NULL
;
4637 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4638 || hlook
->root
.type
== bfd_link_hash_defweak
4639 || hlook
->root
.type
== bfd_link_hash_common
4640 || hlook
->root
.type
== bfd_link_hash_indirect
);
4641 slook
= hlook
->root
.u
.def
.section
;
4642 vlook
= hlook
->root
.u
.def
.value
;
4649 bfd_signed_vma vdiff
;
4651 h
= sorted_sym_hash
[idx
];
4652 vdiff
= vlook
- h
->root
.u
.def
.value
;
4659 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4672 /* We didn't find a value/section match. */
4676 for (i
= ilook
; i
< sym_count
; i
++)
4678 h
= sorted_sym_hash
[i
];
4680 /* Stop if value or section doesn't match. */
4681 if (h
->root
.u
.def
.value
!= vlook
4682 || h
->root
.u
.def
.section
!= slook
)
4684 else if (h
!= hlook
)
4686 hlook
->u
.weakdef
= h
;
4688 /* If the weak definition is in the list of dynamic
4689 symbols, make sure the real definition is put
4691 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4693 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4696 free (sorted_sym_hash
);
4701 /* If the real definition is in the list of dynamic
4702 symbols, make sure the weak definition is put
4703 there as well. If we don't do this, then the
4704 dynamic loader might not merge the entries for the
4705 real definition and the weak definition. */
4706 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4708 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4709 goto err_free_sym_hash
;
4716 free (sorted_sym_hash
);
4719 if (bed
->check_directives
4720 && !(*bed
->check_directives
) (abfd
, info
))
4723 /* If this object is the same format as the output object, and it is
4724 not a shared library, then let the backend look through the
4727 This is required to build global offset table entries and to
4728 arrange for dynamic relocs. It is not required for the
4729 particular common case of linking non PIC code, even when linking
4730 against shared libraries, but unfortunately there is no way of
4731 knowing whether an object file has been compiled PIC or not.
4732 Looking through the relocs is not particularly time consuming.
4733 The problem is that we must either (1) keep the relocs in memory,
4734 which causes the linker to require additional runtime memory or
4735 (2) read the relocs twice from the input file, which wastes time.
4736 This would be a good case for using mmap.
4738 I have no idea how to handle linking PIC code into a file of a
4739 different format. It probably can't be done. */
4741 && is_elf_hash_table (htab
)
4742 && bed
->check_relocs
!= NULL
4743 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
4747 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4749 Elf_Internal_Rela
*internal_relocs
;
4752 if ((o
->flags
& SEC_RELOC
) == 0
4753 || o
->reloc_count
== 0
4754 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4755 && (o
->flags
& SEC_DEBUGGING
) != 0)
4756 || bfd_is_abs_section (o
->output_section
))
4759 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4761 if (internal_relocs
== NULL
)
4764 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
4766 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4767 free (internal_relocs
);
4774 /* If this is a non-traditional link, try to optimize the handling
4775 of the .stab/.stabstr sections. */
4777 && ! info
->traditional_format
4778 && is_elf_hash_table (htab
)
4779 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4783 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4784 if (stabstr
!= NULL
)
4786 bfd_size_type string_offset
= 0;
4789 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4790 if (CONST_STRNEQ (stab
->name
, ".stab")
4791 && (!stab
->name
[5] ||
4792 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4793 && (stab
->flags
& SEC_MERGE
) == 0
4794 && !bfd_is_abs_section (stab
->output_section
))
4796 struct bfd_elf_section_data
*secdata
;
4798 secdata
= elf_section_data (stab
);
4799 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
4800 stabstr
, &secdata
->sec_info
,
4803 if (secdata
->sec_info
)
4804 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4809 if (is_elf_hash_table (htab
) && add_needed
)
4811 /* Add this bfd to the loaded list. */
4812 struct elf_link_loaded_list
*n
;
4814 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4818 n
->next
= htab
->loaded
;
4825 if (old_tab
!= NULL
)
4827 if (nondeflt_vers
!= NULL
)
4828 free (nondeflt_vers
);
4829 if (extversym
!= NULL
)
4832 if (isymbuf
!= NULL
)
4838 /* Return the linker hash table entry of a symbol that might be
4839 satisfied by an archive symbol. Return -1 on error. */
4841 struct elf_link_hash_entry
*
4842 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4843 struct bfd_link_info
*info
,
4846 struct elf_link_hash_entry
*h
;
4850 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4854 /* If this is a default version (the name contains @@), look up the
4855 symbol again with only one `@' as well as without the version.
4856 The effect is that references to the symbol with and without the
4857 version will be matched by the default symbol in the archive. */
4859 p
= strchr (name
, ELF_VER_CHR
);
4860 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4863 /* First check with only one `@'. */
4864 len
= strlen (name
);
4865 copy
= bfd_alloc (abfd
, len
);
4867 return (struct elf_link_hash_entry
*) 0 - 1;
4869 first
= p
- name
+ 1;
4870 memcpy (copy
, name
, first
);
4871 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4873 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4876 /* We also need to check references to the symbol without the
4878 copy
[first
- 1] = '\0';
4879 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4880 FALSE
, FALSE
, FALSE
);
4883 bfd_release (abfd
, copy
);
4887 /* Add symbols from an ELF archive file to the linker hash table. We
4888 don't use _bfd_generic_link_add_archive_symbols because of a
4889 problem which arises on UnixWare. The UnixWare libc.so is an
4890 archive which includes an entry libc.so.1 which defines a bunch of
4891 symbols. The libc.so archive also includes a number of other
4892 object files, which also define symbols, some of which are the same
4893 as those defined in libc.so.1. Correct linking requires that we
4894 consider each object file in turn, and include it if it defines any
4895 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4896 this; it looks through the list of undefined symbols, and includes
4897 any object file which defines them. When this algorithm is used on
4898 UnixWare, it winds up pulling in libc.so.1 early and defining a
4899 bunch of symbols. This means that some of the other objects in the
4900 archive are not included in the link, which is incorrect since they
4901 precede libc.so.1 in the archive.
4903 Fortunately, ELF archive handling is simpler than that done by
4904 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4905 oddities. In ELF, if we find a symbol in the archive map, and the
4906 symbol is currently undefined, we know that we must pull in that
4909 Unfortunately, we do have to make multiple passes over the symbol
4910 table until nothing further is resolved. */
4913 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4916 bfd_boolean
*defined
= NULL
;
4917 bfd_boolean
*included
= NULL
;
4921 const struct elf_backend_data
*bed
;
4922 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4923 (bfd
*, struct bfd_link_info
*, const char *);
4925 if (! bfd_has_map (abfd
))
4927 /* An empty archive is a special case. */
4928 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4930 bfd_set_error (bfd_error_no_armap
);
4934 /* Keep track of all symbols we know to be already defined, and all
4935 files we know to be already included. This is to speed up the
4936 second and subsequent passes. */
4937 c
= bfd_ardata (abfd
)->symdef_count
;
4941 amt
*= sizeof (bfd_boolean
);
4942 defined
= bfd_zmalloc (amt
);
4943 included
= bfd_zmalloc (amt
);
4944 if (defined
== NULL
|| included
== NULL
)
4947 symdefs
= bfd_ardata (abfd
)->symdefs
;
4948 bed
= get_elf_backend_data (abfd
);
4949 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4962 symdefend
= symdef
+ c
;
4963 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4965 struct elf_link_hash_entry
*h
;
4967 struct bfd_link_hash_entry
*undefs_tail
;
4970 if (defined
[i
] || included
[i
])
4972 if (symdef
->file_offset
== last
)
4978 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4979 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4985 if (h
->root
.type
== bfd_link_hash_common
)
4987 /* We currently have a common symbol. The archive map contains
4988 a reference to this symbol, so we may want to include it. We
4989 only want to include it however, if this archive element
4990 contains a definition of the symbol, not just another common
4993 Unfortunately some archivers (including GNU ar) will put
4994 declarations of common symbols into their archive maps, as
4995 well as real definitions, so we cannot just go by the archive
4996 map alone. Instead we must read in the element's symbol
4997 table and check that to see what kind of symbol definition
4999 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5002 else if (h
->root
.type
!= bfd_link_hash_undefined
)
5004 if (h
->root
.type
!= bfd_link_hash_undefweak
)
5009 /* We need to include this archive member. */
5010 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5011 if (element
== NULL
)
5014 if (! bfd_check_format (element
, bfd_object
))
5017 /* Doublecheck that we have not included this object
5018 already--it should be impossible, but there may be
5019 something wrong with the archive. */
5020 if (element
->archive_pass
!= 0)
5022 bfd_set_error (bfd_error_bad_value
);
5025 element
->archive_pass
= 1;
5027 undefs_tail
= info
->hash
->undefs_tail
;
5029 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
5032 if (! bfd_link_add_symbols (element
, info
))
5035 /* If there are any new undefined symbols, we need to make
5036 another pass through the archive in order to see whether
5037 they can be defined. FIXME: This isn't perfect, because
5038 common symbols wind up on undefs_tail and because an
5039 undefined symbol which is defined later on in this pass
5040 does not require another pass. This isn't a bug, but it
5041 does make the code less efficient than it could be. */
5042 if (undefs_tail
!= info
->hash
->undefs_tail
)
5045 /* Look backward to mark all symbols from this object file
5046 which we have already seen in this pass. */
5050 included
[mark
] = TRUE
;
5055 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5057 /* We mark subsequent symbols from this object file as we go
5058 on through the loop. */
5059 last
= symdef
->file_offset
;
5070 if (defined
!= NULL
)
5072 if (included
!= NULL
)
5077 /* Given an ELF BFD, add symbols to the global hash table as
5081 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5083 switch (bfd_get_format (abfd
))
5086 return elf_link_add_object_symbols (abfd
, info
);
5088 return elf_link_add_archive_symbols (abfd
, info
);
5090 bfd_set_error (bfd_error_wrong_format
);
5095 struct hash_codes_info
5097 unsigned long *hashcodes
;
5101 /* This function will be called though elf_link_hash_traverse to store
5102 all hash value of the exported symbols in an array. */
5105 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5107 struct hash_codes_info
*inf
= data
;
5113 if (h
->root
.type
== bfd_link_hash_warning
)
5114 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5116 /* Ignore indirect symbols. These are added by the versioning code. */
5117 if (h
->dynindx
== -1)
5120 name
= h
->root
.root
.string
;
5121 p
= strchr (name
, ELF_VER_CHR
);
5124 alc
= bfd_malloc (p
- name
+ 1);
5130 memcpy (alc
, name
, p
- name
);
5131 alc
[p
- name
] = '\0';
5135 /* Compute the hash value. */
5136 ha
= bfd_elf_hash (name
);
5138 /* Store the found hash value in the array given as the argument. */
5139 *(inf
->hashcodes
)++ = ha
;
5141 /* And store it in the struct so that we can put it in the hash table
5143 h
->u
.elf_hash_value
= ha
;
5151 struct collect_gnu_hash_codes
5154 const struct elf_backend_data
*bed
;
5155 unsigned long int nsyms
;
5156 unsigned long int maskbits
;
5157 unsigned long int *hashcodes
;
5158 unsigned long int *hashval
;
5159 unsigned long int *indx
;
5160 unsigned long int *counts
;
5163 long int min_dynindx
;
5164 unsigned long int bucketcount
;
5165 unsigned long int symindx
;
5166 long int local_indx
;
5167 long int shift1
, shift2
;
5168 unsigned long int mask
;
5172 /* This function will be called though elf_link_hash_traverse to store
5173 all hash value of the exported symbols in an array. */
5176 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5178 struct collect_gnu_hash_codes
*s
= data
;
5184 if (h
->root
.type
== bfd_link_hash_warning
)
5185 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5187 /* Ignore indirect symbols. These are added by the versioning code. */
5188 if (h
->dynindx
== -1)
5191 /* Ignore also local symbols and undefined symbols. */
5192 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5195 name
= h
->root
.root
.string
;
5196 p
= strchr (name
, ELF_VER_CHR
);
5199 alc
= bfd_malloc (p
- name
+ 1);
5205 memcpy (alc
, name
, p
- name
);
5206 alc
[p
- name
] = '\0';
5210 /* Compute the hash value. */
5211 ha
= bfd_elf_gnu_hash (name
);
5213 /* Store the found hash value in the array for compute_bucket_count,
5214 and also for .dynsym reordering purposes. */
5215 s
->hashcodes
[s
->nsyms
] = ha
;
5216 s
->hashval
[h
->dynindx
] = ha
;
5218 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5219 s
->min_dynindx
= h
->dynindx
;
5227 /* This function will be called though elf_link_hash_traverse to do
5228 final dynaminc symbol renumbering. */
5231 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5233 struct collect_gnu_hash_codes
*s
= data
;
5234 unsigned long int bucket
;
5235 unsigned long int val
;
5237 if (h
->root
.type
== bfd_link_hash_warning
)
5238 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5240 /* Ignore indirect symbols. */
5241 if (h
->dynindx
== -1)
5244 /* Ignore also local symbols and undefined symbols. */
5245 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5247 if (h
->dynindx
>= s
->min_dynindx
)
5248 h
->dynindx
= s
->local_indx
++;
5252 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5253 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5254 & ((s
->maskbits
>> s
->shift1
) - 1);
5255 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5257 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5258 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5259 if (s
->counts
[bucket
] == 1)
5260 /* Last element terminates the chain. */
5262 bfd_put_32 (s
->output_bfd
, val
,
5263 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5264 --s
->counts
[bucket
];
5265 h
->dynindx
= s
->indx
[bucket
]++;
5269 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5272 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5274 return !(h
->forced_local
5275 || h
->root
.type
== bfd_link_hash_undefined
5276 || h
->root
.type
== bfd_link_hash_undefweak
5277 || ((h
->root
.type
== bfd_link_hash_defined
5278 || h
->root
.type
== bfd_link_hash_defweak
)
5279 && h
->root
.u
.def
.section
->output_section
== NULL
));
5282 /* Array used to determine the number of hash table buckets to use
5283 based on the number of symbols there are. If there are fewer than
5284 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5285 fewer than 37 we use 17 buckets, and so forth. We never use more
5286 than 32771 buckets. */
5288 static const size_t elf_buckets
[] =
5290 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5294 /* Compute bucket count for hashing table. We do not use a static set
5295 of possible tables sizes anymore. Instead we determine for all
5296 possible reasonable sizes of the table the outcome (i.e., the
5297 number of collisions etc) and choose the best solution. The
5298 weighting functions are not too simple to allow the table to grow
5299 without bounds. Instead one of the weighting factors is the size.
5300 Therefore the result is always a good payoff between few collisions
5301 (= short chain lengths) and table size. */
5303 compute_bucket_count (struct bfd_link_info
*info
,
5304 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
5305 unsigned long int nsyms
,
5308 size_t best_size
= 0;
5309 unsigned long int i
;
5311 /* We have a problem here. The following code to optimize the table
5312 size requires an integer type with more the 32 bits. If
5313 BFD_HOST_U_64_BIT is set we know about such a type. */
5314 #ifdef BFD_HOST_U_64_BIT
5319 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5320 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5321 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5322 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5323 unsigned long int *counts
;
5326 /* Possible optimization parameters: if we have NSYMS symbols we say
5327 that the hashing table must at least have NSYMS/4 and at most
5329 minsize
= nsyms
/ 4;
5332 best_size
= maxsize
= nsyms
* 2;
5337 if ((best_size
& 31) == 0)
5341 /* Create array where we count the collisions in. We must use bfd_malloc
5342 since the size could be large. */
5344 amt
*= sizeof (unsigned long int);
5345 counts
= bfd_malloc (amt
);
5349 /* Compute the "optimal" size for the hash table. The criteria is a
5350 minimal chain length. The minor criteria is (of course) the size
5352 for (i
= minsize
; i
< maxsize
; ++i
)
5354 /* Walk through the array of hashcodes and count the collisions. */
5355 BFD_HOST_U_64_BIT max
;
5356 unsigned long int j
;
5357 unsigned long int fact
;
5359 if (gnu_hash
&& (i
& 31) == 0)
5362 memset (counts
, '\0', i
* sizeof (unsigned long int));
5364 /* Determine how often each hash bucket is used. */
5365 for (j
= 0; j
< nsyms
; ++j
)
5366 ++counts
[hashcodes
[j
] % i
];
5368 /* For the weight function we need some information about the
5369 pagesize on the target. This is information need not be 100%
5370 accurate. Since this information is not available (so far) we
5371 define it here to a reasonable default value. If it is crucial
5372 to have a better value some day simply define this value. */
5373 # ifndef BFD_TARGET_PAGESIZE
5374 # define BFD_TARGET_PAGESIZE (4096)
5377 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5379 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5382 /* Variant 1: optimize for short chains. We add the squares
5383 of all the chain lengths (which favors many small chain
5384 over a few long chains). */
5385 for (j
= 0; j
< i
; ++j
)
5386 max
+= counts
[j
] * counts
[j
];
5388 /* This adds penalties for the overall size of the table. */
5389 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5392 /* Variant 2: Optimize a lot more for small table. Here we
5393 also add squares of the size but we also add penalties for
5394 empty slots (the +1 term). */
5395 for (j
= 0; j
< i
; ++j
)
5396 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5398 /* The overall size of the table is considered, but not as
5399 strong as in variant 1, where it is squared. */
5400 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5404 /* Compare with current best results. */
5405 if (max
< best_chlen
)
5415 #endif /* defined (BFD_HOST_U_64_BIT) */
5417 /* This is the fallback solution if no 64bit type is available or if we
5418 are not supposed to spend much time on optimizations. We select the
5419 bucket count using a fixed set of numbers. */
5420 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5422 best_size
= elf_buckets
[i
];
5423 if (nsyms
< elf_buckets
[i
+ 1])
5426 if (gnu_hash
&& best_size
< 2)
5433 /* Set up the sizes and contents of the ELF dynamic sections. This is
5434 called by the ELF linker emulation before_allocation routine. We
5435 must set the sizes of the sections before the linker sets the
5436 addresses of the various sections. */
5439 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5442 const char *filter_shlib
,
5443 const char * const *auxiliary_filters
,
5444 struct bfd_link_info
*info
,
5445 asection
**sinterpptr
,
5446 struct bfd_elf_version_tree
*verdefs
)
5448 bfd_size_type soname_indx
;
5450 const struct elf_backend_data
*bed
;
5451 struct elf_assign_sym_version_info asvinfo
;
5455 soname_indx
= (bfd_size_type
) -1;
5457 if (!is_elf_hash_table (info
->hash
))
5460 bed
= get_elf_backend_data (output_bfd
);
5461 if (info
->execstack
)
5462 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
5463 else if (info
->noexecstack
)
5464 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
5468 asection
*notesec
= NULL
;
5471 for (inputobj
= info
->input_bfds
;
5473 inputobj
= inputobj
->link_next
)
5477 if (inputobj
->flags
& (DYNAMIC
| EXEC_P
| BFD_LINKER_CREATED
))
5479 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
5482 if (s
->flags
& SEC_CODE
)
5486 else if (bed
->default_execstack
)
5491 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
5492 if (exec
&& info
->relocatable
5493 && notesec
->output_section
!= bfd_abs_section_ptr
)
5494 notesec
->output_section
->flags
|= SEC_CODE
;
5498 /* Any syms created from now on start with -1 in
5499 got.refcount/offset and plt.refcount/offset. */
5500 elf_hash_table (info
)->init_got_refcount
5501 = elf_hash_table (info
)->init_got_offset
;
5502 elf_hash_table (info
)->init_plt_refcount
5503 = elf_hash_table (info
)->init_plt_offset
;
5505 /* The backend may have to create some sections regardless of whether
5506 we're dynamic or not. */
5507 if (bed
->elf_backend_always_size_sections
5508 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5511 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
5514 dynobj
= elf_hash_table (info
)->dynobj
;
5516 /* If there were no dynamic objects in the link, there is nothing to
5521 if (elf_hash_table (info
)->dynamic_sections_created
)
5523 struct elf_info_failed eif
;
5524 struct elf_link_hash_entry
*h
;
5526 struct bfd_elf_version_tree
*t
;
5527 struct bfd_elf_version_expr
*d
;
5529 bfd_boolean all_defined
;
5531 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
5532 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
5536 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5538 if (soname_indx
== (bfd_size_type
) -1
5539 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5545 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5547 info
->flags
|= DF_SYMBOLIC
;
5554 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5556 if (indx
== (bfd_size_type
) -1
5557 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
5560 if (info
->new_dtags
)
5562 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
5563 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
5568 if (filter_shlib
!= NULL
)
5572 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5573 filter_shlib
, TRUE
);
5574 if (indx
== (bfd_size_type
) -1
5575 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5579 if (auxiliary_filters
!= NULL
)
5581 const char * const *p
;
5583 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5587 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5589 if (indx
== (bfd_size_type
) -1
5590 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5596 eif
.verdefs
= verdefs
;
5599 /* If we are supposed to export all symbols into the dynamic symbol
5600 table (this is not the normal case), then do so. */
5601 if (info
->export_dynamic
5602 || (info
->executable
&& info
->dynamic
))
5604 elf_link_hash_traverse (elf_hash_table (info
),
5605 _bfd_elf_export_symbol
,
5611 /* Make all global versions with definition. */
5612 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5613 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5614 if (!d
->symver
&& d
->literal
)
5616 const char *verstr
, *name
;
5617 size_t namelen
, verlen
, newlen
;
5619 struct elf_link_hash_entry
*newh
;
5622 namelen
= strlen (name
);
5624 verlen
= strlen (verstr
);
5625 newlen
= namelen
+ verlen
+ 3;
5627 newname
= bfd_malloc (newlen
);
5628 if (newname
== NULL
)
5630 memcpy (newname
, name
, namelen
);
5632 /* Check the hidden versioned definition. */
5633 p
= newname
+ namelen
;
5635 memcpy (p
, verstr
, verlen
+ 1);
5636 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5637 newname
, FALSE
, FALSE
,
5640 || (newh
->root
.type
!= bfd_link_hash_defined
5641 && newh
->root
.type
!= bfd_link_hash_defweak
))
5643 /* Check the default versioned definition. */
5645 memcpy (p
, verstr
, verlen
+ 1);
5646 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5647 newname
, FALSE
, FALSE
,
5652 /* Mark this version if there is a definition and it is
5653 not defined in a shared object. */
5655 && !newh
->def_dynamic
5656 && (newh
->root
.type
== bfd_link_hash_defined
5657 || newh
->root
.type
== bfd_link_hash_defweak
))
5661 /* Attach all the symbols to their version information. */
5662 asvinfo
.output_bfd
= output_bfd
;
5663 asvinfo
.info
= info
;
5664 asvinfo
.verdefs
= verdefs
;
5665 asvinfo
.failed
= FALSE
;
5667 elf_link_hash_traverse (elf_hash_table (info
),
5668 _bfd_elf_link_assign_sym_version
,
5673 if (!info
->allow_undefined_version
)
5675 /* Check if all global versions have a definition. */
5677 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5678 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5679 if (d
->literal
&& !d
->symver
&& !d
->script
)
5681 (*_bfd_error_handler
)
5682 (_("%s: undefined version: %s"),
5683 d
->pattern
, t
->name
);
5684 all_defined
= FALSE
;
5689 bfd_set_error (bfd_error_bad_value
);
5694 /* Find all symbols which were defined in a dynamic object and make
5695 the backend pick a reasonable value for them. */
5696 elf_link_hash_traverse (elf_hash_table (info
),
5697 _bfd_elf_adjust_dynamic_symbol
,
5702 /* Add some entries to the .dynamic section. We fill in some of the
5703 values later, in bfd_elf_final_link, but we must add the entries
5704 now so that we know the final size of the .dynamic section. */
5706 /* If there are initialization and/or finalization functions to
5707 call then add the corresponding DT_INIT/DT_FINI entries. */
5708 h
= (info
->init_function
5709 ? elf_link_hash_lookup (elf_hash_table (info
),
5710 info
->init_function
, FALSE
,
5717 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5720 h
= (info
->fini_function
5721 ? elf_link_hash_lookup (elf_hash_table (info
),
5722 info
->fini_function
, FALSE
,
5729 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5733 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
5734 if (s
!= NULL
&& s
->linker_has_input
)
5736 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5737 if (! info
->executable
)
5742 for (sub
= info
->input_bfds
; sub
!= NULL
;
5743 sub
= sub
->link_next
)
5744 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
5745 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5746 if (elf_section_data (o
)->this_hdr
.sh_type
5747 == SHT_PREINIT_ARRAY
)
5749 (*_bfd_error_handler
)
5750 (_("%B: .preinit_array section is not allowed in DSO"),
5755 bfd_set_error (bfd_error_nonrepresentable_section
);
5759 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5760 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5763 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
5764 if (s
!= NULL
&& s
->linker_has_input
)
5766 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5767 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5770 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
5771 if (s
!= NULL
&& s
->linker_has_input
)
5773 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5774 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5778 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5779 /* If .dynstr is excluded from the link, we don't want any of
5780 these tags. Strictly, we should be checking each section
5781 individually; This quick check covers for the case where
5782 someone does a /DISCARD/ : { *(*) }. */
5783 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5785 bfd_size_type strsize
;
5787 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5788 if ((info
->emit_hash
5789 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
5790 || (info
->emit_gnu_hash
5791 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
5792 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5793 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5794 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5795 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5796 bed
->s
->sizeof_sym
))
5801 /* The backend must work out the sizes of all the other dynamic
5803 if (bed
->elf_backend_size_dynamic_sections
5804 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5807 if (elf_hash_table (info
)->dynamic_sections_created
)
5809 unsigned long section_sym_count
;
5812 /* Set up the version definition section. */
5813 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5814 BFD_ASSERT (s
!= NULL
);
5816 /* We may have created additional version definitions if we are
5817 just linking a regular application. */
5818 verdefs
= asvinfo
.verdefs
;
5820 /* Skip anonymous version tag. */
5821 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5822 verdefs
= verdefs
->next
;
5824 if (verdefs
== NULL
&& !info
->create_default_symver
)
5825 s
->flags
|= SEC_EXCLUDE
;
5830 struct bfd_elf_version_tree
*t
;
5832 Elf_Internal_Verdef def
;
5833 Elf_Internal_Verdaux defaux
;
5834 struct bfd_link_hash_entry
*bh
;
5835 struct elf_link_hash_entry
*h
;
5841 /* Make space for the base version. */
5842 size
+= sizeof (Elf_External_Verdef
);
5843 size
+= sizeof (Elf_External_Verdaux
);
5846 /* Make space for the default version. */
5847 if (info
->create_default_symver
)
5849 size
+= sizeof (Elf_External_Verdef
);
5853 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5855 struct bfd_elf_version_deps
*n
;
5857 size
+= sizeof (Elf_External_Verdef
);
5858 size
+= sizeof (Elf_External_Verdaux
);
5861 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5862 size
+= sizeof (Elf_External_Verdaux
);
5866 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5867 if (s
->contents
== NULL
&& s
->size
!= 0)
5870 /* Fill in the version definition section. */
5874 def
.vd_version
= VER_DEF_CURRENT
;
5875 def
.vd_flags
= VER_FLG_BASE
;
5878 if (info
->create_default_symver
)
5880 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5881 def
.vd_next
= sizeof (Elf_External_Verdef
);
5885 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5886 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5887 + sizeof (Elf_External_Verdaux
));
5890 if (soname_indx
!= (bfd_size_type
) -1)
5892 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5894 def
.vd_hash
= bfd_elf_hash (soname
);
5895 defaux
.vda_name
= soname_indx
;
5902 name
= lbasename (output_bfd
->filename
);
5903 def
.vd_hash
= bfd_elf_hash (name
);
5904 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5906 if (indx
== (bfd_size_type
) -1)
5908 defaux
.vda_name
= indx
;
5910 defaux
.vda_next
= 0;
5912 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5913 (Elf_External_Verdef
*) p
);
5914 p
+= sizeof (Elf_External_Verdef
);
5915 if (info
->create_default_symver
)
5917 /* Add a symbol representing this version. */
5919 if (! (_bfd_generic_link_add_one_symbol
5920 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5922 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5924 h
= (struct elf_link_hash_entry
*) bh
;
5927 h
->type
= STT_OBJECT
;
5928 h
->verinfo
.vertree
= NULL
;
5930 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5933 /* Create a duplicate of the base version with the same
5934 aux block, but different flags. */
5937 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5939 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5940 + sizeof (Elf_External_Verdaux
));
5943 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5944 (Elf_External_Verdef
*) p
);
5945 p
+= sizeof (Elf_External_Verdef
);
5947 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5948 (Elf_External_Verdaux
*) p
);
5949 p
+= sizeof (Elf_External_Verdaux
);
5951 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5954 struct bfd_elf_version_deps
*n
;
5957 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5960 /* Add a symbol representing this version. */
5962 if (! (_bfd_generic_link_add_one_symbol
5963 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5965 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5967 h
= (struct elf_link_hash_entry
*) bh
;
5970 h
->type
= STT_OBJECT
;
5971 h
->verinfo
.vertree
= t
;
5973 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5976 def
.vd_version
= VER_DEF_CURRENT
;
5978 if (t
->globals
.list
== NULL
5979 && t
->locals
.list
== NULL
5981 def
.vd_flags
|= VER_FLG_WEAK
;
5982 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5983 def
.vd_cnt
= cdeps
+ 1;
5984 def
.vd_hash
= bfd_elf_hash (t
->name
);
5985 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5987 if (t
->next
!= NULL
)
5988 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5989 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5991 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5992 (Elf_External_Verdef
*) p
);
5993 p
+= sizeof (Elf_External_Verdef
);
5995 defaux
.vda_name
= h
->dynstr_index
;
5996 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5998 defaux
.vda_next
= 0;
5999 if (t
->deps
!= NULL
)
6000 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6001 t
->name_indx
= defaux
.vda_name
;
6003 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6004 (Elf_External_Verdaux
*) p
);
6005 p
+= sizeof (Elf_External_Verdaux
);
6007 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6009 if (n
->version_needed
== NULL
)
6011 /* This can happen if there was an error in the
6013 defaux
.vda_name
= 0;
6017 defaux
.vda_name
= n
->version_needed
->name_indx
;
6018 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6021 if (n
->next
== NULL
)
6022 defaux
.vda_next
= 0;
6024 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6026 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6027 (Elf_External_Verdaux
*) p
);
6028 p
+= sizeof (Elf_External_Verdaux
);
6032 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
6033 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
6036 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6039 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
6041 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
6044 else if (info
->flags
& DF_BIND_NOW
)
6046 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
6052 if (info
->executable
)
6053 info
->flags_1
&= ~ (DF_1_INITFIRST
6056 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
6060 /* Work out the size of the version reference section. */
6062 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
6063 BFD_ASSERT (s
!= NULL
);
6065 struct elf_find_verdep_info sinfo
;
6067 sinfo
.output_bfd
= output_bfd
;
6069 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6070 if (sinfo
.vers
== 0)
6072 sinfo
.failed
= FALSE
;
6074 elf_link_hash_traverse (elf_hash_table (info
),
6075 _bfd_elf_link_find_version_dependencies
,
6080 if (elf_tdata (output_bfd
)->verref
== NULL
)
6081 s
->flags
|= SEC_EXCLUDE
;
6084 Elf_Internal_Verneed
*t
;
6089 /* Build the version definition section. */
6092 for (t
= elf_tdata (output_bfd
)->verref
;
6096 Elf_Internal_Vernaux
*a
;
6098 size
+= sizeof (Elf_External_Verneed
);
6100 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6101 size
+= sizeof (Elf_External_Vernaux
);
6105 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
6106 if (s
->contents
== NULL
)
6110 for (t
= elf_tdata (output_bfd
)->verref
;
6115 Elf_Internal_Vernaux
*a
;
6119 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6122 t
->vn_version
= VER_NEED_CURRENT
;
6124 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6125 elf_dt_name (t
->vn_bfd
) != NULL
6126 ? elf_dt_name (t
->vn_bfd
)
6127 : lbasename (t
->vn_bfd
->filename
),
6129 if (indx
== (bfd_size_type
) -1)
6132 t
->vn_aux
= sizeof (Elf_External_Verneed
);
6133 if (t
->vn_nextref
== NULL
)
6136 t
->vn_next
= (sizeof (Elf_External_Verneed
)
6137 + caux
* sizeof (Elf_External_Vernaux
));
6139 _bfd_elf_swap_verneed_out (output_bfd
, t
,
6140 (Elf_External_Verneed
*) p
);
6141 p
+= sizeof (Elf_External_Verneed
);
6143 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6145 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6146 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6147 a
->vna_nodename
, FALSE
);
6148 if (indx
== (bfd_size_type
) -1)
6151 if (a
->vna_nextptr
== NULL
)
6154 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6156 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6157 (Elf_External_Vernaux
*) p
);
6158 p
+= sizeof (Elf_External_Vernaux
);
6162 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
6163 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
6166 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6170 if ((elf_tdata (output_bfd
)->cverrefs
== 0
6171 && elf_tdata (output_bfd
)->cverdefs
== 0)
6172 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6173 §ion_sym_count
) == 0)
6175 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
6176 s
->flags
|= SEC_EXCLUDE
;
6182 /* Find the first non-excluded output section. We'll use its
6183 section symbol for some emitted relocs. */
6185 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
6189 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6190 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
6191 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6193 elf_hash_table (info
)->text_index_section
= s
;
6198 /* Find two non-excluded output sections, one for code, one for data.
6199 We'll use their section symbols for some emitted relocs. */
6201 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6205 /* Data first, since setting text_index_section changes
6206 _bfd_elf_link_omit_section_dynsym. */
6207 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6208 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
6209 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6211 elf_hash_table (info
)->data_index_section
= s
;
6215 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6216 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
6217 == (SEC_ALLOC
| SEC_READONLY
))
6218 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6220 elf_hash_table (info
)->text_index_section
= s
;
6224 if (elf_hash_table (info
)->text_index_section
== NULL
)
6225 elf_hash_table (info
)->text_index_section
6226 = elf_hash_table (info
)->data_index_section
;
6230 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6232 const struct elf_backend_data
*bed
;
6234 if (!is_elf_hash_table (info
->hash
))
6237 bed
= get_elf_backend_data (output_bfd
);
6238 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6240 if (elf_hash_table (info
)->dynamic_sections_created
)
6244 bfd_size_type dynsymcount
;
6245 unsigned long section_sym_count
;
6246 unsigned int dtagcount
;
6248 dynobj
= elf_hash_table (info
)->dynobj
;
6250 /* Assign dynsym indicies. In a shared library we generate a
6251 section symbol for each output section, which come first.
6252 Next come all of the back-end allocated local dynamic syms,
6253 followed by the rest of the global symbols. */
6255 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6256 §ion_sym_count
);
6258 /* Work out the size of the symbol version section. */
6259 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
6260 BFD_ASSERT (s
!= NULL
);
6261 if (dynsymcount
!= 0
6262 && (s
->flags
& SEC_EXCLUDE
) == 0)
6264 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6265 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
6266 if (s
->contents
== NULL
)
6269 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
6273 /* Set the size of the .dynsym and .hash sections. We counted
6274 the number of dynamic symbols in elf_link_add_object_symbols.
6275 We will build the contents of .dynsym and .hash when we build
6276 the final symbol table, because until then we do not know the
6277 correct value to give the symbols. We built the .dynstr
6278 section as we went along in elf_link_add_object_symbols. */
6279 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
6280 BFD_ASSERT (s
!= NULL
);
6281 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
6283 if (dynsymcount
!= 0)
6285 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
6286 if (s
->contents
== NULL
)
6289 /* The first entry in .dynsym is a dummy symbol.
6290 Clear all the section syms, in case we don't output them all. */
6291 ++section_sym_count
;
6292 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
6295 elf_hash_table (info
)->bucketcount
= 0;
6297 /* Compute the size of the hashing table. As a side effect this
6298 computes the hash values for all the names we export. */
6299 if (info
->emit_hash
)
6301 unsigned long int *hashcodes
;
6302 struct hash_codes_info hashinf
;
6304 unsigned long int nsyms
;
6306 size_t hash_entry_size
;
6308 /* Compute the hash values for all exported symbols. At the same
6309 time store the values in an array so that we could use them for
6311 amt
= dynsymcount
* sizeof (unsigned long int);
6312 hashcodes
= bfd_malloc (amt
);
6313 if (hashcodes
== NULL
)
6315 hashinf
.hashcodes
= hashcodes
;
6316 hashinf
.error
= FALSE
;
6318 /* Put all hash values in HASHCODES. */
6319 elf_link_hash_traverse (elf_hash_table (info
),
6320 elf_collect_hash_codes
, &hashinf
);
6327 nsyms
= hashinf
.hashcodes
- hashcodes
;
6329 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6332 if (bucketcount
== 0)
6335 elf_hash_table (info
)->bucketcount
= bucketcount
;
6337 s
= bfd_get_section_by_name (dynobj
, ".hash");
6338 BFD_ASSERT (s
!= NULL
);
6339 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6340 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6341 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
6342 if (s
->contents
== NULL
)
6345 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6346 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6347 s
->contents
+ hash_entry_size
);
6350 if (info
->emit_gnu_hash
)
6353 unsigned char *contents
;
6354 struct collect_gnu_hash_codes cinfo
;
6358 memset (&cinfo
, 0, sizeof (cinfo
));
6360 /* Compute the hash values for all exported symbols. At the same
6361 time store the values in an array so that we could use them for
6363 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6364 cinfo
.hashcodes
= bfd_malloc (amt
);
6365 if (cinfo
.hashcodes
== NULL
)
6368 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6369 cinfo
.min_dynindx
= -1;
6370 cinfo
.output_bfd
= output_bfd
;
6373 /* Put all hash values in HASHCODES. */
6374 elf_link_hash_traverse (elf_hash_table (info
),
6375 elf_collect_gnu_hash_codes
, &cinfo
);
6378 free (cinfo
.hashcodes
);
6383 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6385 if (bucketcount
== 0)
6387 free (cinfo
.hashcodes
);
6391 s
= bfd_get_section_by_name (dynobj
, ".gnu.hash");
6392 BFD_ASSERT (s
!= NULL
);
6394 if (cinfo
.nsyms
== 0)
6396 /* Empty .gnu.hash section is special. */
6397 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6398 free (cinfo
.hashcodes
);
6399 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6400 contents
= bfd_zalloc (output_bfd
, s
->size
);
6401 if (contents
== NULL
)
6403 s
->contents
= contents
;
6404 /* 1 empty bucket. */
6405 bfd_put_32 (output_bfd
, 1, contents
);
6406 /* SYMIDX above the special symbol 0. */
6407 bfd_put_32 (output_bfd
, 1, contents
+ 4);
6408 /* Just one word for bitmask. */
6409 bfd_put_32 (output_bfd
, 1, contents
+ 8);
6410 /* Only hash fn bloom filter. */
6411 bfd_put_32 (output_bfd
, 0, contents
+ 12);
6412 /* No hashes are valid - empty bitmask. */
6413 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
6414 /* No hashes in the only bucket. */
6415 bfd_put_32 (output_bfd
, 0,
6416 contents
+ 16 + bed
->s
->arch_size
/ 8);
6420 unsigned long int maskwords
, maskbitslog2
;
6421 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
6423 maskbitslog2
= bfd_log2 (cinfo
.nsyms
) + 1;
6424 if (maskbitslog2
< 3)
6426 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
6427 maskbitslog2
= maskbitslog2
+ 3;
6429 maskbitslog2
= maskbitslog2
+ 2;
6430 if (bed
->s
->arch_size
== 64)
6432 if (maskbitslog2
== 5)
6438 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
6439 cinfo
.shift2
= maskbitslog2
;
6440 cinfo
.maskbits
= 1 << maskbitslog2
;
6441 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
6442 amt
= bucketcount
* sizeof (unsigned long int) * 2;
6443 amt
+= maskwords
* sizeof (bfd_vma
);
6444 cinfo
.bitmask
= bfd_malloc (amt
);
6445 if (cinfo
.bitmask
== NULL
)
6447 free (cinfo
.hashcodes
);
6451 cinfo
.counts
= (void *) (cinfo
.bitmask
+ maskwords
);
6452 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
6453 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
6454 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
6456 /* Determine how often each hash bucket is used. */
6457 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
6458 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
6459 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
6461 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
6462 if (cinfo
.counts
[i
] != 0)
6464 cinfo
.indx
[i
] = cnt
;
6465 cnt
+= cinfo
.counts
[i
];
6467 BFD_ASSERT (cnt
== dynsymcount
);
6468 cinfo
.bucketcount
= bucketcount
;
6469 cinfo
.local_indx
= cinfo
.min_dynindx
;
6471 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
6472 s
->size
+= cinfo
.maskbits
/ 8;
6473 contents
= bfd_zalloc (output_bfd
, s
->size
);
6474 if (contents
== NULL
)
6476 free (cinfo
.bitmask
);
6477 free (cinfo
.hashcodes
);
6481 s
->contents
= contents
;
6482 bfd_put_32 (output_bfd
, bucketcount
, contents
);
6483 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
6484 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
6485 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
6486 contents
+= 16 + cinfo
.maskbits
/ 8;
6488 for (i
= 0; i
< bucketcount
; ++i
)
6490 if (cinfo
.counts
[i
] == 0)
6491 bfd_put_32 (output_bfd
, 0, contents
);
6493 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
6497 cinfo
.contents
= contents
;
6499 /* Renumber dynamic symbols, populate .gnu.hash section. */
6500 elf_link_hash_traverse (elf_hash_table (info
),
6501 elf_renumber_gnu_hash_syms
, &cinfo
);
6503 contents
= s
->contents
+ 16;
6504 for (i
= 0; i
< maskwords
; ++i
)
6506 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
6508 contents
+= bed
->s
->arch_size
/ 8;
6511 free (cinfo
.bitmask
);
6512 free (cinfo
.hashcodes
);
6516 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
6517 BFD_ASSERT (s
!= NULL
);
6519 elf_finalize_dynstr (output_bfd
, info
);
6521 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6523 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
6524 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
6531 /* Indicate that we are only retrieving symbol values from this
6535 _bfd_elf_link_just_syms (asection
*sec
, struct bfd_link_info
*info
)
6537 if (is_elf_hash_table (info
->hash
))
6538 sec
->sec_info_type
= ELF_INFO_TYPE_JUST_SYMS
;
6539 _bfd_generic_link_just_syms (sec
, info
);
6542 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6545 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
6548 BFD_ASSERT (sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
);
6549 sec
->sec_info_type
= ELF_INFO_TYPE_NONE
;
6552 /* Finish SHF_MERGE section merging. */
6555 _bfd_elf_merge_sections (bfd
*abfd
, struct bfd_link_info
*info
)
6560 if (!is_elf_hash_table (info
->hash
))
6563 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
6564 if ((ibfd
->flags
& DYNAMIC
) == 0)
6565 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6566 if ((sec
->flags
& SEC_MERGE
) != 0
6567 && !bfd_is_abs_section (sec
->output_section
))
6569 struct bfd_elf_section_data
*secdata
;
6571 secdata
= elf_section_data (sec
);
6572 if (! _bfd_add_merge_section (abfd
,
6573 &elf_hash_table (info
)->merge_info
,
6574 sec
, &secdata
->sec_info
))
6576 else if (secdata
->sec_info
)
6577 sec
->sec_info_type
= ELF_INFO_TYPE_MERGE
;
6580 if (elf_hash_table (info
)->merge_info
!= NULL
)
6581 _bfd_merge_sections (abfd
, info
, elf_hash_table (info
)->merge_info
,
6582 merge_sections_remove_hook
);
6586 /* Create an entry in an ELF linker hash table. */
6588 struct bfd_hash_entry
*
6589 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
6590 struct bfd_hash_table
*table
,
6593 /* Allocate the structure if it has not already been allocated by a
6597 entry
= bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
6602 /* Call the allocation method of the superclass. */
6603 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
6606 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
6607 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
6609 /* Set local fields. */
6612 ret
->got
= htab
->init_got_refcount
;
6613 ret
->plt
= htab
->init_plt_refcount
;
6614 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
6615 - offsetof (struct elf_link_hash_entry
, size
)));
6616 /* Assume that we have been called by a non-ELF symbol reader.
6617 This flag is then reset by the code which reads an ELF input
6618 file. This ensures that a symbol created by a non-ELF symbol
6619 reader will have the flag set correctly. */
6626 /* Copy data from an indirect symbol to its direct symbol, hiding the
6627 old indirect symbol. Also used for copying flags to a weakdef. */
6630 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
6631 struct elf_link_hash_entry
*dir
,
6632 struct elf_link_hash_entry
*ind
)
6634 struct elf_link_hash_table
*htab
;
6636 /* Copy down any references that we may have already seen to the
6637 symbol which just became indirect. */
6639 dir
->ref_dynamic
|= ind
->ref_dynamic
;
6640 dir
->ref_regular
|= ind
->ref_regular
;
6641 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
6642 dir
->non_got_ref
|= ind
->non_got_ref
;
6643 dir
->needs_plt
|= ind
->needs_plt
;
6644 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
6646 if (ind
->root
.type
!= bfd_link_hash_indirect
)
6649 /* Copy over the global and procedure linkage table refcount entries.
6650 These may have been already set up by a check_relocs routine. */
6651 htab
= elf_hash_table (info
);
6652 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
6654 if (dir
->got
.refcount
< 0)
6655 dir
->got
.refcount
= 0;
6656 dir
->got
.refcount
+= ind
->got
.refcount
;
6657 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
6660 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
6662 if (dir
->plt
.refcount
< 0)
6663 dir
->plt
.refcount
= 0;
6664 dir
->plt
.refcount
+= ind
->plt
.refcount
;
6665 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
6668 if (ind
->dynindx
!= -1)
6670 if (dir
->dynindx
!= -1)
6671 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
6672 dir
->dynindx
= ind
->dynindx
;
6673 dir
->dynstr_index
= ind
->dynstr_index
;
6675 ind
->dynstr_index
= 0;
6680 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
6681 struct elf_link_hash_entry
*h
,
6682 bfd_boolean force_local
)
6684 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
6688 h
->forced_local
= 1;
6689 if (h
->dynindx
!= -1)
6692 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
6698 /* Initialize an ELF linker hash table. */
6701 _bfd_elf_link_hash_table_init
6702 (struct elf_link_hash_table
*table
,
6704 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
6705 struct bfd_hash_table
*,
6707 unsigned int entsize
)
6710 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
6712 memset (table
, 0, sizeof * table
);
6713 table
->init_got_refcount
.refcount
= can_refcount
- 1;
6714 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
6715 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
6716 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
6717 /* The first dynamic symbol is a dummy. */
6718 table
->dynsymcount
= 1;
6720 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
6721 table
->root
.type
= bfd_link_elf_hash_table
;
6726 /* Create an ELF linker hash table. */
6728 struct bfd_link_hash_table
*
6729 _bfd_elf_link_hash_table_create (bfd
*abfd
)
6731 struct elf_link_hash_table
*ret
;
6732 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
6734 ret
= bfd_malloc (amt
);
6738 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
6739 sizeof (struct elf_link_hash_entry
)))
6748 /* This is a hook for the ELF emulation code in the generic linker to
6749 tell the backend linker what file name to use for the DT_NEEDED
6750 entry for a dynamic object. */
6753 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
6755 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
6756 && bfd_get_format (abfd
) == bfd_object
)
6757 elf_dt_name (abfd
) = name
;
6761 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
6764 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
6765 && bfd_get_format (abfd
) == bfd_object
)
6766 lib_class
= elf_dyn_lib_class (abfd
);
6773 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
6775 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
6776 && bfd_get_format (abfd
) == bfd_object
)
6777 elf_dyn_lib_class (abfd
) = lib_class
;
6780 /* Get the list of DT_NEEDED entries for a link. This is a hook for
6781 the linker ELF emulation code. */
6783 struct bfd_link_needed_list
*
6784 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
6785 struct bfd_link_info
*info
)
6787 if (! is_elf_hash_table (info
->hash
))
6789 return elf_hash_table (info
)->needed
;
6792 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6793 hook for the linker ELF emulation code. */
6795 struct bfd_link_needed_list
*
6796 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
6797 struct bfd_link_info
*info
)
6799 if (! is_elf_hash_table (info
->hash
))
6801 return elf_hash_table (info
)->runpath
;
6804 /* Get the name actually used for a dynamic object for a link. This
6805 is the SONAME entry if there is one. Otherwise, it is the string
6806 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
6809 bfd_elf_get_dt_soname (bfd
*abfd
)
6811 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
6812 && bfd_get_format (abfd
) == bfd_object
)
6813 return elf_dt_name (abfd
);
6817 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
6818 the ELF linker emulation code. */
6821 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
6822 struct bfd_link_needed_list
**pneeded
)
6825 bfd_byte
*dynbuf
= NULL
;
6826 unsigned int elfsec
;
6827 unsigned long shlink
;
6828 bfd_byte
*extdyn
, *extdynend
;
6830 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
6834 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
6835 || bfd_get_format (abfd
) != bfd_object
)
6838 s
= bfd_get_section_by_name (abfd
, ".dynamic");
6839 if (s
== NULL
|| s
->size
== 0)
6842 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
6845 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
6846 if (elfsec
== SHN_BAD
)
6849 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
6851 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
6852 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
6855 extdynend
= extdyn
+ s
->size
;
6856 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
6858 Elf_Internal_Dyn dyn
;
6860 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
6862 if (dyn
.d_tag
== DT_NULL
)
6865 if (dyn
.d_tag
== DT_NEEDED
)
6868 struct bfd_link_needed_list
*l
;
6869 unsigned int tagv
= dyn
.d_un
.d_val
;
6872 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
6877 l
= bfd_alloc (abfd
, amt
);
6898 struct elf_symbuf_symbol
6900 unsigned long st_name
; /* Symbol name, index in string tbl */
6901 unsigned char st_info
; /* Type and binding attributes */
6902 unsigned char st_other
; /* Visibilty, and target specific */
6905 struct elf_symbuf_head
6907 struct elf_symbuf_symbol
*ssym
;
6908 bfd_size_type count
;
6909 unsigned int st_shndx
;
6916 Elf_Internal_Sym
*isym
;
6917 struct elf_symbuf_symbol
*ssym
;
6922 /* Sort references to symbols by ascending section number. */
6925 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
6927 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
6928 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
6930 return s1
->st_shndx
- s2
->st_shndx
;
6934 elf_sym_name_compare (const void *arg1
, const void *arg2
)
6936 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
6937 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
6938 return strcmp (s1
->name
, s2
->name
);
6941 static struct elf_symbuf_head
*
6942 elf_create_symbuf (bfd_size_type symcount
, Elf_Internal_Sym
*isymbuf
)
6944 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
6945 struct elf_symbuf_symbol
*ssym
;
6946 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
6947 bfd_size_type i
, shndx_count
, total_size
;
6949 indbuf
= bfd_malloc2 (symcount
, sizeof (*indbuf
));
6953 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
6954 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
6955 *ind
++ = &isymbuf
[i
];
6958 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
6959 elf_sort_elf_symbol
);
6962 if (indbufend
> indbuf
)
6963 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
6964 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
6967 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
6968 + (indbufend
- indbuf
) * sizeof (*ssym
));
6969 ssymbuf
= bfd_malloc (total_size
);
6970 if (ssymbuf
== NULL
)
6976 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
6977 ssymbuf
->ssym
= NULL
;
6978 ssymbuf
->count
= shndx_count
;
6979 ssymbuf
->st_shndx
= 0;
6980 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
6982 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
6985 ssymhead
->ssym
= ssym
;
6986 ssymhead
->count
= 0;
6987 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
6989 ssym
->st_name
= (*ind
)->st_name
;
6990 ssym
->st_info
= (*ind
)->st_info
;
6991 ssym
->st_other
= (*ind
)->st_other
;
6994 BFD_ASSERT ((bfd_size_type
) (ssymhead
- ssymbuf
) == shndx_count
6995 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
7002 /* Check if 2 sections define the same set of local and global
7006 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
7007 struct bfd_link_info
*info
)
7010 const struct elf_backend_data
*bed1
, *bed2
;
7011 Elf_Internal_Shdr
*hdr1
, *hdr2
;
7012 bfd_size_type symcount1
, symcount2
;
7013 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
7014 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
7015 Elf_Internal_Sym
*isym
, *isymend
;
7016 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
7017 bfd_size_type count1
, count2
, i
;
7018 unsigned int shndx1
, shndx2
;
7024 /* Both sections have to be in ELF. */
7025 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
7026 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
7029 if (elf_section_type (sec1
) != elf_section_type (sec2
))
7032 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
7033 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
7034 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
7037 bed1
= get_elf_backend_data (bfd1
);
7038 bed2
= get_elf_backend_data (bfd2
);
7039 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
7040 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
7041 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
7042 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
7044 if (symcount1
== 0 || symcount2
== 0)
7050 ssymbuf1
= elf_tdata (bfd1
)->symbuf
;
7051 ssymbuf2
= elf_tdata (bfd2
)->symbuf
;
7053 if (ssymbuf1
== NULL
)
7055 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
7057 if (isymbuf1
== NULL
)
7060 if (!info
->reduce_memory_overheads
)
7061 elf_tdata (bfd1
)->symbuf
= ssymbuf1
7062 = elf_create_symbuf (symcount1
, isymbuf1
);
7065 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
7067 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
7069 if (isymbuf2
== NULL
)
7072 if (ssymbuf1
!= NULL
&& !info
->reduce_memory_overheads
)
7073 elf_tdata (bfd2
)->symbuf
= ssymbuf2
7074 = elf_create_symbuf (symcount2
, isymbuf2
);
7077 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
7079 /* Optimized faster version. */
7080 bfd_size_type lo
, hi
, mid
;
7081 struct elf_symbol
*symp
;
7082 struct elf_symbuf_symbol
*ssym
, *ssymend
;
7085 hi
= ssymbuf1
->count
;
7090 mid
= (lo
+ hi
) / 2;
7091 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
7093 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
7097 count1
= ssymbuf1
[mid
].count
;
7104 hi
= ssymbuf2
->count
;
7109 mid
= (lo
+ hi
) / 2;
7110 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
7112 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
7116 count2
= ssymbuf2
[mid
].count
;
7122 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7125 symtable1
= bfd_malloc (count1
* sizeof (struct elf_symbol
));
7126 symtable2
= bfd_malloc (count2
* sizeof (struct elf_symbol
));
7127 if (symtable1
== NULL
|| symtable2
== NULL
)
7131 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
7132 ssym
< ssymend
; ssym
++, symp
++)
7134 symp
->u
.ssym
= ssym
;
7135 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
7141 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
7142 ssym
< ssymend
; ssym
++, symp
++)
7144 symp
->u
.ssym
= ssym
;
7145 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
7150 /* Sort symbol by name. */
7151 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7152 elf_sym_name_compare
);
7153 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7154 elf_sym_name_compare
);
7156 for (i
= 0; i
< count1
; i
++)
7157 /* Two symbols must have the same binding, type and name. */
7158 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
7159 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
7160 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7167 symtable1
= bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
7168 symtable2
= bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
7169 if (symtable1
== NULL
|| symtable2
== NULL
)
7172 /* Count definitions in the section. */
7174 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
7175 if (isym
->st_shndx
== shndx1
)
7176 symtable1
[count1
++].u
.isym
= isym
;
7179 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
7180 if (isym
->st_shndx
== shndx2
)
7181 symtable2
[count2
++].u
.isym
= isym
;
7183 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7186 for (i
= 0; i
< count1
; i
++)
7188 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
7189 symtable1
[i
].u
.isym
->st_name
);
7191 for (i
= 0; i
< count2
; i
++)
7193 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
7194 symtable2
[i
].u
.isym
->st_name
);
7196 /* Sort symbol by name. */
7197 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7198 elf_sym_name_compare
);
7199 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7200 elf_sym_name_compare
);
7202 for (i
= 0; i
< count1
; i
++)
7203 /* Two symbols must have the same binding, type and name. */
7204 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
7205 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
7206 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7224 /* Return TRUE if 2 section types are compatible. */
7227 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
7228 bfd
*bbfd
, const asection
*bsec
)
7232 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
7233 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7236 return elf_section_type (asec
) == elf_section_type (bsec
);
7239 /* Final phase of ELF linker. */
7241 /* A structure we use to avoid passing large numbers of arguments. */
7243 struct elf_final_link_info
7245 /* General link information. */
7246 struct bfd_link_info
*info
;
7249 /* Symbol string table. */
7250 struct bfd_strtab_hash
*symstrtab
;
7251 /* .dynsym section. */
7252 asection
*dynsym_sec
;
7253 /* .hash section. */
7255 /* symbol version section (.gnu.version). */
7256 asection
*symver_sec
;
7257 /* Buffer large enough to hold contents of any section. */
7259 /* Buffer large enough to hold external relocs of any section. */
7260 void *external_relocs
;
7261 /* Buffer large enough to hold internal relocs of any section. */
7262 Elf_Internal_Rela
*internal_relocs
;
7263 /* Buffer large enough to hold external local symbols of any input
7265 bfd_byte
*external_syms
;
7266 /* And a buffer for symbol section indices. */
7267 Elf_External_Sym_Shndx
*locsym_shndx
;
7268 /* Buffer large enough to hold internal local symbols of any input
7270 Elf_Internal_Sym
*internal_syms
;
7271 /* Array large enough to hold a symbol index for each local symbol
7272 of any input BFD. */
7274 /* Array large enough to hold a section pointer for each local
7275 symbol of any input BFD. */
7276 asection
**sections
;
7277 /* Buffer to hold swapped out symbols. */
7279 /* And one for symbol section indices. */
7280 Elf_External_Sym_Shndx
*symshndxbuf
;
7281 /* Number of swapped out symbols in buffer. */
7282 size_t symbuf_count
;
7283 /* Number of symbols which fit in symbuf. */
7285 /* And same for symshndxbuf. */
7286 size_t shndxbuf_size
;
7289 /* This struct is used to pass information to elf_link_output_extsym. */
7291 struct elf_outext_info
7294 bfd_boolean localsyms
;
7295 struct elf_final_link_info
*finfo
;
7299 /* Support for evaluating a complex relocation.
7301 Complex relocations are generalized, self-describing relocations. The
7302 implementation of them consists of two parts: complex symbols, and the
7303 relocations themselves.
7305 The relocations are use a reserved elf-wide relocation type code (R_RELC
7306 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7307 information (start bit, end bit, word width, etc) into the addend. This
7308 information is extracted from CGEN-generated operand tables within gas.
7310 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7311 internal) representing prefix-notation expressions, including but not
7312 limited to those sorts of expressions normally encoded as addends in the
7313 addend field. The symbol mangling format is:
7316 | <unary-operator> ':' <node>
7317 | <binary-operator> ':' <node> ':' <node>
7320 <literal> := 's' <digits=N> ':' <N character symbol name>
7321 | 'S' <digits=N> ':' <N character section name>
7325 <binary-operator> := as in C
7326 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7329 set_symbol_value (bfd
*bfd_with_globals
,
7330 Elf_Internal_Sym
*isymbuf
,
7335 struct elf_link_hash_entry
**sym_hashes
;
7336 struct elf_link_hash_entry
*h
;
7337 size_t extsymoff
= locsymcount
;
7339 if (symidx
< locsymcount
)
7341 Elf_Internal_Sym
*sym
;
7343 sym
= isymbuf
+ symidx
;
7344 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
7346 /* It is a local symbol: move it to the
7347 "absolute" section and give it a value. */
7348 sym
->st_shndx
= SHN_ABS
;
7349 sym
->st_value
= val
;
7352 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
7356 /* It is a global symbol: set its link type
7357 to "defined" and give it a value. */
7359 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
7360 h
= sym_hashes
[symidx
- extsymoff
];
7361 while (h
->root
.type
== bfd_link_hash_indirect
7362 || h
->root
.type
== bfd_link_hash_warning
)
7363 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7364 h
->root
.type
= bfd_link_hash_defined
;
7365 h
->root
.u
.def
.value
= val
;
7366 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
7370 resolve_symbol (const char *name
,
7372 struct elf_final_link_info
*finfo
,
7374 Elf_Internal_Sym
*isymbuf
,
7377 Elf_Internal_Sym
*sym
;
7378 struct bfd_link_hash_entry
*global_entry
;
7379 const char *candidate
= NULL
;
7380 Elf_Internal_Shdr
*symtab_hdr
;
7383 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
7385 for (i
= 0; i
< locsymcount
; ++ i
)
7389 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
7392 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
7393 symtab_hdr
->sh_link
,
7396 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7397 name
, candidate
, (unsigned long) sym
->st_value
);
7399 if (candidate
&& strcmp (candidate
, name
) == 0)
7401 asection
*sec
= finfo
->sections
[i
];
7403 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
7404 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
7406 printf ("Found symbol with value %8.8lx\n",
7407 (unsigned long) *result
);
7413 /* Hmm, haven't found it yet. perhaps it is a global. */
7414 global_entry
= bfd_link_hash_lookup (finfo
->info
->hash
, name
,
7415 FALSE
, FALSE
, TRUE
);
7419 if (global_entry
->type
== bfd_link_hash_defined
7420 || global_entry
->type
== bfd_link_hash_defweak
)
7422 *result
= (global_entry
->u
.def
.value
7423 + global_entry
->u
.def
.section
->output_section
->vma
7424 + global_entry
->u
.def
.section
->output_offset
);
7426 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7427 global_entry
->root
.string
, (unsigned long) *result
);
7436 resolve_section (const char *name
,
7443 for (curr
= sections
; curr
; curr
= curr
->next
)
7444 if (strcmp (curr
->name
, name
) == 0)
7446 *result
= curr
->vma
;
7450 /* Hmm. still haven't found it. try pseudo-section names. */
7451 for (curr
= sections
; curr
; curr
= curr
->next
)
7453 len
= strlen (curr
->name
);
7454 if (len
> strlen (name
))
7457 if (strncmp (curr
->name
, name
, len
) == 0)
7459 if (strncmp (".end", name
+ len
, 4) == 0)
7461 *result
= curr
->vma
+ curr
->size
;
7465 /* Insert more pseudo-section names here, if you like. */
7473 undefined_reference (const char *reftype
, const char *name
)
7475 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7480 eval_symbol (bfd_vma
*result
,
7483 struct elf_final_link_info
*finfo
,
7485 Elf_Internal_Sym
*isymbuf
,
7494 const char *sym
= *symp
;
7496 bfd_boolean symbol_is_section
= FALSE
;
7501 if (len
< 1 || len
> sizeof (symbuf
))
7503 bfd_set_error (bfd_error_invalid_operation
);
7516 *result
= strtoul (sym
, (char **) symp
, 16);
7520 symbol_is_section
= TRUE
;
7523 symlen
= strtol (sym
, (char **) symp
, 10);
7524 sym
= *symp
+ 1; /* Skip the trailing ':'. */
7526 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
7528 bfd_set_error (bfd_error_invalid_operation
);
7532 memcpy (symbuf
, sym
, symlen
);
7533 symbuf
[symlen
] = '\0';
7534 *symp
= sym
+ symlen
;
7536 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7537 the symbol as a section, or vice-versa. so we're pretty liberal in our
7538 interpretation here; section means "try section first", not "must be a
7539 section", and likewise with symbol. */
7541 if (symbol_is_section
)
7543 if (!resolve_section (symbuf
, finfo
->output_bfd
->sections
, result
)
7544 && !resolve_symbol (symbuf
, input_bfd
, finfo
, result
,
7545 isymbuf
, locsymcount
))
7547 undefined_reference ("section", symbuf
);
7553 if (!resolve_symbol (symbuf
, input_bfd
, finfo
, result
,
7554 isymbuf
, locsymcount
)
7555 && !resolve_section (symbuf
, finfo
->output_bfd
->sections
,
7558 undefined_reference ("symbol", symbuf
);
7565 /* All that remains are operators. */
7567 #define UNARY_OP(op) \
7568 if (strncmp (sym, #op, strlen (#op)) == 0) \
7570 sym += strlen (#op); \
7574 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7575 isymbuf, locsymcount, signed_p)) \
7578 *result = op ((bfd_signed_vma) a); \
7584 #define BINARY_OP(op) \
7585 if (strncmp (sym, #op, strlen (#op)) == 0) \
7587 sym += strlen (#op); \
7591 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7592 isymbuf, locsymcount, signed_p)) \
7595 if (!eval_symbol (&b, symp, input_bfd, finfo, dot, \
7596 isymbuf, locsymcount, signed_p)) \
7599 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7629 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
7630 bfd_set_error (bfd_error_invalid_operation
);
7636 put_value (bfd_vma size
,
7637 unsigned long chunksz
,
7642 location
+= (size
- chunksz
);
7644 for (; size
; size
-= chunksz
, location
-= chunksz
, x
>>= (chunksz
* 8))
7652 bfd_put_8 (input_bfd
, x
, location
);
7655 bfd_put_16 (input_bfd
, x
, location
);
7658 bfd_put_32 (input_bfd
, x
, location
);
7662 bfd_put_64 (input_bfd
, x
, location
);
7672 get_value (bfd_vma size
,
7673 unsigned long chunksz
,
7679 for (; size
; size
-= chunksz
, location
+= chunksz
)
7687 x
= (x
<< (8 * chunksz
)) | bfd_get_8 (input_bfd
, location
);
7690 x
= (x
<< (8 * chunksz
)) | bfd_get_16 (input_bfd
, location
);
7693 x
= (x
<< (8 * chunksz
)) | bfd_get_32 (input_bfd
, location
);
7697 x
= (x
<< (8 * chunksz
)) | bfd_get_64 (input_bfd
, location
);
7708 decode_complex_addend (unsigned long *start
, /* in bits */
7709 unsigned long *oplen
, /* in bits */
7710 unsigned long *len
, /* in bits */
7711 unsigned long *wordsz
, /* in bytes */
7712 unsigned long *chunksz
, /* in bytes */
7713 unsigned long *lsb0_p
,
7714 unsigned long *signed_p
,
7715 unsigned long *trunc_p
,
7716 unsigned long encoded
)
7718 * start
= encoded
& 0x3F;
7719 * len
= (encoded
>> 6) & 0x3F;
7720 * oplen
= (encoded
>> 12) & 0x3F;
7721 * wordsz
= (encoded
>> 18) & 0xF;
7722 * chunksz
= (encoded
>> 22) & 0xF;
7723 * lsb0_p
= (encoded
>> 27) & 1;
7724 * signed_p
= (encoded
>> 28) & 1;
7725 * trunc_p
= (encoded
>> 29) & 1;
7728 bfd_reloc_status_type
7729 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
7730 asection
*input_section ATTRIBUTE_UNUSED
,
7732 Elf_Internal_Rela
*rel
,
7735 bfd_vma shift
, x
, mask
;
7736 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
7737 bfd_reloc_status_type r
;
7739 /* Perform this reloc, since it is complex.
7740 (this is not to say that it necessarily refers to a complex
7741 symbol; merely that it is a self-describing CGEN based reloc.
7742 i.e. the addend has the complete reloc information (bit start, end,
7743 word size, etc) encoded within it.). */
7745 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
7746 &chunksz
, &lsb0_p
, &signed_p
,
7747 &trunc_p
, rel
->r_addend
);
7749 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
7752 shift
= (start
+ 1) - len
;
7754 shift
= (8 * wordsz
) - (start
+ len
);
7756 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ rel
->r_offset
);
7759 printf ("Doing complex reloc: "
7760 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7761 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7762 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7763 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
7764 oplen
, x
, mask
, relocation
);
7769 /* Now do an overflow check. */
7770 r
= bfd_check_overflow ((signed_p
7771 ? complain_overflow_signed
7772 : complain_overflow_unsigned
),
7773 len
, 0, (8 * wordsz
),
7777 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
7780 printf (" relocation: %8.8lx\n"
7781 " shifted mask: %8.8lx\n"
7782 " shifted/masked reloc: %8.8lx\n"
7783 " result: %8.8lx\n",
7784 relocation
, (mask
<< shift
),
7785 ((relocation
& mask
) << shift
), x
);
7787 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ rel
->r_offset
);
7791 /* When performing a relocatable link, the input relocations are
7792 preserved. But, if they reference global symbols, the indices
7793 referenced must be updated. Update all the relocations in
7794 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
7797 elf_link_adjust_relocs (bfd
*abfd
,
7798 Elf_Internal_Shdr
*rel_hdr
,
7800 struct elf_link_hash_entry
**rel_hash
)
7803 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7805 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
7806 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
7807 bfd_vma r_type_mask
;
7810 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
7812 swap_in
= bed
->s
->swap_reloc_in
;
7813 swap_out
= bed
->s
->swap_reloc_out
;
7815 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
7817 swap_in
= bed
->s
->swap_reloca_in
;
7818 swap_out
= bed
->s
->swap_reloca_out
;
7823 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
7826 if (bed
->s
->arch_size
== 32)
7833 r_type_mask
= 0xffffffff;
7837 erela
= rel_hdr
->contents
;
7838 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
7840 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
7843 if (*rel_hash
== NULL
)
7846 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
7848 (*swap_in
) (abfd
, erela
, irela
);
7849 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
7850 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
7851 | (irela
[j
].r_info
& r_type_mask
));
7852 (*swap_out
) (abfd
, irela
, erela
);
7856 struct elf_link_sort_rela
7862 enum elf_reloc_type_class type
;
7863 /* We use this as an array of size int_rels_per_ext_rel. */
7864 Elf_Internal_Rela rela
[1];
7868 elf_link_sort_cmp1 (const void *A
, const void *B
)
7870 const struct elf_link_sort_rela
*a
= A
;
7871 const struct elf_link_sort_rela
*b
= B
;
7872 int relativea
, relativeb
;
7874 relativea
= a
->type
== reloc_class_relative
;
7875 relativeb
= b
->type
== reloc_class_relative
;
7877 if (relativea
< relativeb
)
7879 if (relativea
> relativeb
)
7881 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
7883 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
7885 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
7887 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
7893 elf_link_sort_cmp2 (const void *A
, const void *B
)
7895 const struct elf_link_sort_rela
*a
= A
;
7896 const struct elf_link_sort_rela
*b
= B
;
7899 if (a
->u
.offset
< b
->u
.offset
)
7901 if (a
->u
.offset
> b
->u
.offset
)
7903 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
7904 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
7909 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
7911 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
7917 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
7919 asection
*dynamic_relocs
;
7922 bfd_size_type count
, size
;
7923 size_t i
, ret
, sort_elt
, ext_size
;
7924 bfd_byte
*sort
, *s_non_relative
, *p
;
7925 struct elf_link_sort_rela
*sq
;
7926 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7927 int i2e
= bed
->s
->int_rels_per_ext_rel
;
7928 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
7929 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
7930 struct bfd_link_order
*lo
;
7932 bfd_boolean use_rela
;
7934 /* Find a dynamic reloc section. */
7935 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
7936 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
7937 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
7938 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
7940 bfd_boolean use_rela_initialised
= FALSE
;
7942 /* This is just here to stop gcc from complaining.
7943 It's initialization checking code is not perfect. */
7946 /* Both sections are present. Examine the sizes
7947 of the indirect sections to help us choose. */
7948 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
7949 if (lo
->type
== bfd_indirect_link_order
)
7951 asection
*o
= lo
->u
.indirect
.section
;
7953 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
7955 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
7956 /* Section size is divisible by both rel and rela sizes.
7957 It is of no help to us. */
7961 /* Section size is only divisible by rela. */
7962 if (use_rela_initialised
&& (use_rela
== FALSE
))
7965 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
7966 bfd_set_error (bfd_error_invalid_operation
);
7972 use_rela_initialised
= TRUE
;
7976 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
7978 /* Section size is only divisible by rel. */
7979 if (use_rela_initialised
&& (use_rela
== TRUE
))
7982 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
7983 bfd_set_error (bfd_error_invalid_operation
);
7989 use_rela_initialised
= TRUE
;
7994 /* The section size is not divisible by either - something is wrong. */
7996 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd
);
7997 bfd_set_error (bfd_error_invalid_operation
);
8002 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8003 if (lo
->type
== bfd_indirect_link_order
)
8005 asection
*o
= lo
->u
.indirect
.section
;
8007 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8009 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8010 /* Section size is divisible by both rel and rela sizes.
8011 It is of no help to us. */
8015 /* Section size is only divisible by rela. */
8016 if (use_rela_initialised
&& (use_rela
== FALSE
))
8019 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
8020 bfd_set_error (bfd_error_invalid_operation
);
8026 use_rela_initialised
= TRUE
;
8030 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8032 /* Section size is only divisible by rel. */
8033 if (use_rela_initialised
&& (use_rela
== TRUE
))
8036 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
8037 bfd_set_error (bfd_error_invalid_operation
);
8043 use_rela_initialised
= TRUE
;
8048 /* The section size is not divisible by either - something is wrong. */
8050 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd
);
8051 bfd_set_error (bfd_error_invalid_operation
);
8056 if (! use_rela_initialised
)
8060 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
8062 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8069 dynamic_relocs
= rela_dyn
;
8070 ext_size
= bed
->s
->sizeof_rela
;
8071 swap_in
= bed
->s
->swap_reloca_in
;
8072 swap_out
= bed
->s
->swap_reloca_out
;
8076 dynamic_relocs
= rel_dyn
;
8077 ext_size
= bed
->s
->sizeof_rel
;
8078 swap_in
= bed
->s
->swap_reloc_in
;
8079 swap_out
= bed
->s
->swap_reloc_out
;
8083 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8084 if (lo
->type
== bfd_indirect_link_order
)
8085 size
+= lo
->u
.indirect
.section
->size
;
8087 if (size
!= dynamic_relocs
->size
)
8090 sort_elt
= (sizeof (struct elf_link_sort_rela
)
8091 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
8093 count
= dynamic_relocs
->size
/ ext_size
;
8094 sort
= bfd_zmalloc (sort_elt
* count
);
8098 (*info
->callbacks
->warning
)
8099 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
8103 if (bed
->s
->arch_size
== 32)
8104 r_sym_mask
= ~(bfd_vma
) 0xff;
8106 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
8108 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8109 if (lo
->type
== bfd_indirect_link_order
)
8111 bfd_byte
*erel
, *erelend
;
8112 asection
*o
= lo
->u
.indirect
.section
;
8114 if (o
->contents
== NULL
&& o
->size
!= 0)
8116 /* This is a reloc section that is being handled as a normal
8117 section. See bfd_section_from_shdr. We can't combine
8118 relocs in this case. */
8123 erelend
= o
->contents
+ o
->size
;
8124 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
8126 while (erel
< erelend
)
8128 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8130 (*swap_in
) (abfd
, erel
, s
->rela
);
8131 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
8132 s
->u
.sym_mask
= r_sym_mask
;
8138 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
8140 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
8142 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8143 if (s
->type
!= reloc_class_relative
)
8149 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
8150 for (; i
< count
; i
++, p
+= sort_elt
)
8152 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
8153 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
8155 sp
->u
.offset
= sq
->rela
->r_offset
;
8158 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
8160 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8161 if (lo
->type
== bfd_indirect_link_order
)
8163 bfd_byte
*erel
, *erelend
;
8164 asection
*o
= lo
->u
.indirect
.section
;
8167 erelend
= o
->contents
+ o
->size
;
8168 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
8169 while (erel
< erelend
)
8171 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8172 (*swap_out
) (abfd
, s
->rela
, erel
);
8179 *psec
= dynamic_relocs
;
8183 /* Flush the output symbols to the file. */
8186 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
8187 const struct elf_backend_data
*bed
)
8189 if (finfo
->symbuf_count
> 0)
8191 Elf_Internal_Shdr
*hdr
;
8195 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
8196 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
8197 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
8198 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
8199 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
8202 hdr
->sh_size
+= amt
;
8203 finfo
->symbuf_count
= 0;
8209 /* Add a symbol to the output symbol table. */
8212 elf_link_output_sym (struct elf_final_link_info
*finfo
,
8214 Elf_Internal_Sym
*elfsym
,
8215 asection
*input_sec
,
8216 struct elf_link_hash_entry
*h
)
8219 Elf_External_Sym_Shndx
*destshndx
;
8220 bfd_boolean (*output_symbol_hook
)
8221 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
8222 struct elf_link_hash_entry
*);
8223 const struct elf_backend_data
*bed
;
8225 bed
= get_elf_backend_data (finfo
->output_bfd
);
8226 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
8227 if (output_symbol_hook
!= NULL
)
8229 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
8233 if (name
== NULL
|| *name
== '\0')
8234 elfsym
->st_name
= 0;
8235 else if (input_sec
->flags
& SEC_EXCLUDE
)
8236 elfsym
->st_name
= 0;
8239 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
8241 if (elfsym
->st_name
== (unsigned long) -1)
8245 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
8247 if (! elf_link_flush_output_syms (finfo
, bed
))
8251 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
8252 destshndx
= finfo
->symshndxbuf
;
8253 if (destshndx
!= NULL
)
8255 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
8259 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
8260 destshndx
= bfd_realloc (destshndx
, amt
* 2);
8261 if (destshndx
== NULL
)
8263 finfo
->symshndxbuf
= destshndx
;
8264 memset ((char *) destshndx
+ amt
, 0, amt
);
8265 finfo
->shndxbuf_size
*= 2;
8267 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
8270 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
8271 finfo
->symbuf_count
+= 1;
8272 bfd_get_symcount (finfo
->output_bfd
) += 1;
8277 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8280 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
8282 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
8283 && sym
->st_shndx
< SHN_LORESERVE
)
8285 /* The gABI doesn't support dynamic symbols in output sections
8287 (*_bfd_error_handler
)
8288 (_("%B: Too many sections: %d (>= %d)"),
8289 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
8290 bfd_set_error (bfd_error_nonrepresentable_section
);
8296 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8297 allowing an unsatisfied unversioned symbol in the DSO to match a
8298 versioned symbol that would normally require an explicit version.
8299 We also handle the case that a DSO references a hidden symbol
8300 which may be satisfied by a versioned symbol in another DSO. */
8303 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
8304 const struct elf_backend_data
*bed
,
8305 struct elf_link_hash_entry
*h
)
8308 struct elf_link_loaded_list
*loaded
;
8310 if (!is_elf_hash_table (info
->hash
))
8313 switch (h
->root
.type
)
8319 case bfd_link_hash_undefined
:
8320 case bfd_link_hash_undefweak
:
8321 abfd
= h
->root
.u
.undef
.abfd
;
8322 if ((abfd
->flags
& DYNAMIC
) == 0
8323 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
8327 case bfd_link_hash_defined
:
8328 case bfd_link_hash_defweak
:
8329 abfd
= h
->root
.u
.def
.section
->owner
;
8332 case bfd_link_hash_common
:
8333 abfd
= h
->root
.u
.c
.p
->section
->owner
;
8336 BFD_ASSERT (abfd
!= NULL
);
8338 for (loaded
= elf_hash_table (info
)->loaded
;
8340 loaded
= loaded
->next
)
8343 Elf_Internal_Shdr
*hdr
;
8344 bfd_size_type symcount
;
8345 bfd_size_type extsymcount
;
8346 bfd_size_type extsymoff
;
8347 Elf_Internal_Shdr
*versymhdr
;
8348 Elf_Internal_Sym
*isym
;
8349 Elf_Internal_Sym
*isymend
;
8350 Elf_Internal_Sym
*isymbuf
;
8351 Elf_External_Versym
*ever
;
8352 Elf_External_Versym
*extversym
;
8354 input
= loaded
->abfd
;
8356 /* We check each DSO for a possible hidden versioned definition. */
8358 || (input
->flags
& DYNAMIC
) == 0
8359 || elf_dynversym (input
) == 0)
8362 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
8364 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8365 if (elf_bad_symtab (input
))
8367 extsymcount
= symcount
;
8372 extsymcount
= symcount
- hdr
->sh_info
;
8373 extsymoff
= hdr
->sh_info
;
8376 if (extsymcount
== 0)
8379 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
8381 if (isymbuf
== NULL
)
8384 /* Read in any version definitions. */
8385 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
8386 extversym
= bfd_malloc (versymhdr
->sh_size
);
8387 if (extversym
== NULL
)
8390 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
8391 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
8392 != versymhdr
->sh_size
))
8400 ever
= extversym
+ extsymoff
;
8401 isymend
= isymbuf
+ extsymcount
;
8402 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
8405 Elf_Internal_Versym iver
;
8406 unsigned short version_index
;
8408 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
8409 || isym
->st_shndx
== SHN_UNDEF
)
8412 name
= bfd_elf_string_from_elf_section (input
,
8415 if (strcmp (name
, h
->root
.root
.string
) != 0)
8418 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
8420 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
8422 /* If we have a non-hidden versioned sym, then it should
8423 have provided a definition for the undefined sym. */
8427 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
8428 if (version_index
== 1 || version_index
== 2)
8430 /* This is the base or first version. We can use it. */
8444 /* Add an external symbol to the symbol table. This is called from
8445 the hash table traversal routine. When generating a shared object,
8446 we go through the symbol table twice. The first time we output
8447 anything that might have been forced to local scope in a version
8448 script. The second time we output the symbols that are still
8452 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
8454 struct elf_outext_info
*eoinfo
= data
;
8455 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
8457 Elf_Internal_Sym sym
;
8458 asection
*input_sec
;
8459 const struct elf_backend_data
*bed
;
8461 if (h
->root
.type
== bfd_link_hash_warning
)
8463 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8464 if (h
->root
.type
== bfd_link_hash_new
)
8468 /* Decide whether to output this symbol in this pass. */
8469 if (eoinfo
->localsyms
)
8471 if (!h
->forced_local
)
8476 if (h
->forced_local
)
8480 bed
= get_elf_backend_data (finfo
->output_bfd
);
8482 if (h
->root
.type
== bfd_link_hash_undefined
)
8484 /* If we have an undefined symbol reference here then it must have
8485 come from a shared library that is being linked in. (Undefined
8486 references in regular files have already been handled). */
8487 bfd_boolean ignore_undef
= FALSE
;
8489 /* Some symbols may be special in that the fact that they're
8490 undefined can be safely ignored - let backend determine that. */
8491 if (bed
->elf_backend_ignore_undef_symbol
)
8492 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
8494 /* If we are reporting errors for this situation then do so now. */
8495 if (ignore_undef
== FALSE
8498 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
8499 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
8501 if (! (finfo
->info
->callbacks
->undefined_symbol
8502 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
8503 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
8505 eoinfo
->failed
= TRUE
;
8511 /* We should also warn if a forced local symbol is referenced from
8512 shared libraries. */
8513 if (! finfo
->info
->relocatable
8514 && (! finfo
->info
->shared
)
8519 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
8521 (*_bfd_error_handler
)
8522 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
8524 h
->root
.u
.def
.section
== bfd_abs_section_ptr
8525 ? finfo
->output_bfd
: h
->root
.u
.def
.section
->owner
,
8526 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
8528 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
8529 ? "hidden" : "local",
8530 h
->root
.root
.string
);
8531 eoinfo
->failed
= TRUE
;
8535 /* We don't want to output symbols that have never been mentioned by
8536 a regular file, or that we have been told to strip. However, if
8537 h->indx is set to -2, the symbol is used by a reloc and we must
8541 else if ((h
->def_dynamic
8543 || h
->root
.type
== bfd_link_hash_new
)
8547 else if (finfo
->info
->strip
== strip_all
)
8549 else if (finfo
->info
->strip
== strip_some
8550 && bfd_hash_lookup (finfo
->info
->keep_hash
,
8551 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
8553 else if (finfo
->info
->strip_discarded
8554 && (h
->root
.type
== bfd_link_hash_defined
8555 || h
->root
.type
== bfd_link_hash_defweak
)
8556 && elf_discarded_section (h
->root
.u
.def
.section
))
8561 /* If we're stripping it, and it's not a dynamic symbol, there's
8562 nothing else to do unless it is a forced local symbol. */
8565 && !h
->forced_local
)
8569 sym
.st_size
= h
->size
;
8570 sym
.st_other
= h
->other
;
8571 if (h
->forced_local
)
8572 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
8573 else if (h
->root
.type
== bfd_link_hash_undefweak
8574 || h
->root
.type
== bfd_link_hash_defweak
)
8575 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
8577 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
8579 switch (h
->root
.type
)
8582 case bfd_link_hash_new
:
8583 case bfd_link_hash_warning
:
8587 case bfd_link_hash_undefined
:
8588 case bfd_link_hash_undefweak
:
8589 input_sec
= bfd_und_section_ptr
;
8590 sym
.st_shndx
= SHN_UNDEF
;
8593 case bfd_link_hash_defined
:
8594 case bfd_link_hash_defweak
:
8596 input_sec
= h
->root
.u
.def
.section
;
8597 if (input_sec
->output_section
!= NULL
)
8600 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
8601 input_sec
->output_section
);
8602 if (sym
.st_shndx
== SHN_BAD
)
8604 (*_bfd_error_handler
)
8605 (_("%B: could not find output section %A for input section %A"),
8606 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
8607 eoinfo
->failed
= TRUE
;
8611 /* ELF symbols in relocatable files are section relative,
8612 but in nonrelocatable files they are virtual
8614 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
8615 if (! finfo
->info
->relocatable
)
8617 sym
.st_value
+= input_sec
->output_section
->vma
;
8618 if (h
->type
== STT_TLS
)
8620 asection
*tls_sec
= elf_hash_table (finfo
->info
)->tls_sec
;
8621 if (tls_sec
!= NULL
)
8622 sym
.st_value
-= tls_sec
->vma
;
8625 /* The TLS section may have been garbage collected. */
8626 BFD_ASSERT (finfo
->info
->gc_sections
8627 && !input_sec
->gc_mark
);
8634 BFD_ASSERT (input_sec
->owner
== NULL
8635 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
8636 sym
.st_shndx
= SHN_UNDEF
;
8637 input_sec
= bfd_und_section_ptr
;
8642 case bfd_link_hash_common
:
8643 input_sec
= h
->root
.u
.c
.p
->section
;
8644 sym
.st_shndx
= bed
->common_section_index (input_sec
);
8645 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
8648 case bfd_link_hash_indirect
:
8649 /* These symbols are created by symbol versioning. They point
8650 to the decorated version of the name. For example, if the
8651 symbol foo@@GNU_1.2 is the default, which should be used when
8652 foo is used with no version, then we add an indirect symbol
8653 foo which points to foo@@GNU_1.2. We ignore these symbols,
8654 since the indirected symbol is already in the hash table. */
8658 /* Give the processor backend a chance to tweak the symbol value,
8659 and also to finish up anything that needs to be done for this
8660 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
8661 forced local syms when non-shared is due to a historical quirk. */
8662 if ((h
->dynindx
!= -1
8664 && ((finfo
->info
->shared
8665 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
8666 || h
->root
.type
!= bfd_link_hash_undefweak
))
8667 || !h
->forced_local
)
8668 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
8670 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
8671 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
8673 eoinfo
->failed
= TRUE
;
8678 /* If we are marking the symbol as undefined, and there are no
8679 non-weak references to this symbol from a regular object, then
8680 mark the symbol as weak undefined; if there are non-weak
8681 references, mark the symbol as strong. We can't do this earlier,
8682 because it might not be marked as undefined until the
8683 finish_dynamic_symbol routine gets through with it. */
8684 if (sym
.st_shndx
== SHN_UNDEF
8686 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
8687 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
8691 if (h
->ref_regular_nonweak
)
8692 bindtype
= STB_GLOBAL
;
8694 bindtype
= STB_WEAK
;
8695 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
8698 /* If this is a symbol defined in a dynamic library, don't use the
8699 symbol size from the dynamic library. Relinking an executable
8700 against a new library may introduce gratuitous changes in the
8701 executable's symbols if we keep the size. */
8702 if (sym
.st_shndx
== SHN_UNDEF
8707 /* If a non-weak symbol with non-default visibility is not defined
8708 locally, it is a fatal error. */
8709 if (! finfo
->info
->relocatable
8710 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
8711 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
8712 && h
->root
.type
== bfd_link_hash_undefined
8715 (*_bfd_error_handler
)
8716 (_("%B: %s symbol `%s' isn't defined"),
8718 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
8720 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
8721 ? "internal" : "hidden",
8722 h
->root
.root
.string
);
8723 eoinfo
->failed
= TRUE
;
8727 /* If this symbol should be put in the .dynsym section, then put it
8728 there now. We already know the symbol index. We also fill in
8729 the entry in the .hash section. */
8730 if (h
->dynindx
!= -1
8731 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
8735 sym
.st_name
= h
->dynstr_index
;
8736 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
8737 if (! check_dynsym (finfo
->output_bfd
, &sym
))
8739 eoinfo
->failed
= TRUE
;
8742 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
8744 if (finfo
->hash_sec
!= NULL
)
8746 size_t hash_entry_size
;
8747 bfd_byte
*bucketpos
;
8752 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
8753 bucket
= h
->u
.elf_hash_value
% bucketcount
;
8756 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
8757 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
8758 + (bucket
+ 2) * hash_entry_size
);
8759 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
8760 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
8761 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
8762 ((bfd_byte
*) finfo
->hash_sec
->contents
8763 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
8766 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
8768 Elf_Internal_Versym iversym
;
8769 Elf_External_Versym
*eversym
;
8771 if (!h
->def_regular
)
8773 if (h
->verinfo
.verdef
== NULL
)
8774 iversym
.vs_vers
= 0;
8776 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
8780 if (h
->verinfo
.vertree
== NULL
)
8781 iversym
.vs_vers
= 1;
8783 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
8784 if (finfo
->info
->create_default_symver
)
8789 iversym
.vs_vers
|= VERSYM_HIDDEN
;
8791 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
8792 eversym
+= h
->dynindx
;
8793 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
8797 /* If we're stripping it, then it was just a dynamic symbol, and
8798 there's nothing else to do. */
8799 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
8802 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
8804 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
8806 eoinfo
->failed
= TRUE
;
8813 /* Return TRUE if special handling is done for relocs in SEC against
8814 symbols defined in discarded sections. */
8817 elf_section_ignore_discarded_relocs (asection
*sec
)
8819 const struct elf_backend_data
*bed
;
8821 switch (sec
->sec_info_type
)
8823 case ELF_INFO_TYPE_STABS
:
8824 case ELF_INFO_TYPE_EH_FRAME
:
8830 bed
= get_elf_backend_data (sec
->owner
);
8831 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
8832 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
8838 /* Return a mask saying how ld should treat relocations in SEC against
8839 symbols defined in discarded sections. If this function returns
8840 COMPLAIN set, ld will issue a warning message. If this function
8841 returns PRETEND set, and the discarded section was link-once and the
8842 same size as the kept link-once section, ld will pretend that the
8843 symbol was actually defined in the kept section. Otherwise ld will
8844 zero the reloc (at least that is the intent, but some cooperation by
8845 the target dependent code is needed, particularly for REL targets). */
8848 _bfd_elf_default_action_discarded (asection
*sec
)
8850 if (sec
->flags
& SEC_DEBUGGING
)
8853 if (strcmp (".eh_frame", sec
->name
) == 0)
8856 if (strcmp (".gcc_except_table", sec
->name
) == 0)
8859 return COMPLAIN
| PRETEND
;
8862 /* Find a match between a section and a member of a section group. */
8865 match_group_member (asection
*sec
, asection
*group
,
8866 struct bfd_link_info
*info
)
8868 asection
*first
= elf_next_in_group (group
);
8869 asection
*s
= first
;
8873 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
8876 s
= elf_next_in_group (s
);
8884 /* Check if the kept section of a discarded section SEC can be used
8885 to replace it. Return the replacement if it is OK. Otherwise return
8889 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
8893 kept
= sec
->kept_section
;
8896 if ((kept
->flags
& SEC_GROUP
) != 0)
8897 kept
= match_group_member (sec
, kept
, info
);
8899 && ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
8900 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
)))
8902 sec
->kept_section
= kept
;
8907 /* Link an input file into the linker output file. This function
8908 handles all the sections and relocations of the input file at once.
8909 This is so that we only have to read the local symbols once, and
8910 don't have to keep them in memory. */
8913 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
8915 int (*relocate_section
)
8916 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
8917 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
8919 Elf_Internal_Shdr
*symtab_hdr
;
8922 Elf_Internal_Sym
*isymbuf
;
8923 Elf_Internal_Sym
*isym
;
8924 Elf_Internal_Sym
*isymend
;
8926 asection
**ppsection
;
8928 const struct elf_backend_data
*bed
;
8929 struct elf_link_hash_entry
**sym_hashes
;
8931 output_bfd
= finfo
->output_bfd
;
8932 bed
= get_elf_backend_data (output_bfd
);
8933 relocate_section
= bed
->elf_backend_relocate_section
;
8935 /* If this is a dynamic object, we don't want to do anything here:
8936 we don't want the local symbols, and we don't want the section
8938 if ((input_bfd
->flags
& DYNAMIC
) != 0)
8941 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8942 if (elf_bad_symtab (input_bfd
))
8944 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8949 locsymcount
= symtab_hdr
->sh_info
;
8950 extsymoff
= symtab_hdr
->sh_info
;
8953 /* Read the local symbols. */
8954 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8955 if (isymbuf
== NULL
&& locsymcount
!= 0)
8957 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
8958 finfo
->internal_syms
,
8959 finfo
->external_syms
,
8960 finfo
->locsym_shndx
);
8961 if (isymbuf
== NULL
)
8965 /* Find local symbol sections and adjust values of symbols in
8966 SEC_MERGE sections. Write out those local symbols we know are
8967 going into the output file. */
8968 isymend
= isymbuf
+ locsymcount
;
8969 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
8971 isym
++, pindex
++, ppsection
++)
8975 Elf_Internal_Sym osym
;
8979 if (elf_bad_symtab (input_bfd
))
8981 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
8988 if (isym
->st_shndx
== SHN_UNDEF
)
8989 isec
= bfd_und_section_ptr
;
8990 else if (isym
->st_shndx
== SHN_ABS
)
8991 isec
= bfd_abs_section_ptr
;
8992 else if (isym
->st_shndx
== SHN_COMMON
)
8993 isec
= bfd_com_section_ptr
;
8996 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
8999 /* Don't attempt to output symbols with st_shnx in the
9000 reserved range other than SHN_ABS and SHN_COMMON. */
9004 else if (isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
9005 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
9007 _bfd_merged_section_offset (output_bfd
, &isec
,
9008 elf_section_data (isec
)->sec_info
,
9014 /* Don't output the first, undefined, symbol. */
9015 if (ppsection
== finfo
->sections
)
9018 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
9020 /* We never output section symbols. Instead, we use the
9021 section symbol of the corresponding section in the output
9026 /* If we are stripping all symbols, we don't want to output this
9028 if (finfo
->info
->strip
== strip_all
)
9031 /* If we are discarding all local symbols, we don't want to
9032 output this one. If we are generating a relocatable output
9033 file, then some of the local symbols may be required by
9034 relocs; we output them below as we discover that they are
9036 if (finfo
->info
->discard
== discard_all
)
9039 /* If this symbol is defined in a section which we are
9040 discarding, we don't need to keep it. */
9041 if (isym
->st_shndx
!= SHN_UNDEF
9042 && isym
->st_shndx
< SHN_LORESERVE
9043 && bfd_section_removed_from_list (output_bfd
,
9044 isec
->output_section
))
9047 /* Get the name of the symbol. */
9048 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
9053 /* See if we are discarding symbols with this name. */
9054 if ((finfo
->info
->strip
== strip_some
9055 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
9057 || (((finfo
->info
->discard
== discard_sec_merge
9058 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
9059 || finfo
->info
->discard
== discard_l
)
9060 && bfd_is_local_label_name (input_bfd
, name
)))
9063 /* If we get here, we are going to output this symbol. */
9067 /* Adjust the section index for the output file. */
9068 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
9069 isec
->output_section
);
9070 if (osym
.st_shndx
== SHN_BAD
)
9073 *pindex
= bfd_get_symcount (output_bfd
);
9075 /* ELF symbols in relocatable files are section relative, but
9076 in executable files they are virtual addresses. Note that
9077 this code assumes that all ELF sections have an associated
9078 BFD section with a reasonable value for output_offset; below
9079 we assume that they also have a reasonable value for
9080 output_section. Any special sections must be set up to meet
9081 these requirements. */
9082 osym
.st_value
+= isec
->output_offset
;
9083 if (! finfo
->info
->relocatable
)
9085 osym
.st_value
+= isec
->output_section
->vma
;
9086 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
9088 /* STT_TLS symbols are relative to PT_TLS segment base. */
9089 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
9090 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
9094 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
9098 /* Relocate the contents of each section. */
9099 sym_hashes
= elf_sym_hashes (input_bfd
);
9100 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
9104 if (! o
->linker_mark
)
9106 /* This section was omitted from the link. */
9110 if (finfo
->info
->relocatable
9111 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
9113 /* Deal with the group signature symbol. */
9114 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
9115 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
9116 asection
*osec
= o
->output_section
;
9118 if (symndx
>= locsymcount
9119 || (elf_bad_symtab (input_bfd
)
9120 && finfo
->sections
[symndx
] == NULL
))
9122 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
9123 while (h
->root
.type
== bfd_link_hash_indirect
9124 || h
->root
.type
== bfd_link_hash_warning
)
9125 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9126 /* Arrange for symbol to be output. */
9128 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
9130 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
9132 /* We'll use the output section target_index. */
9133 asection
*sec
= finfo
->sections
[symndx
]->output_section
;
9134 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
9138 if (finfo
->indices
[symndx
] == -1)
9140 /* Otherwise output the local symbol now. */
9141 Elf_Internal_Sym sym
= isymbuf
[symndx
];
9142 asection
*sec
= finfo
->sections
[symndx
]->output_section
;
9145 name
= bfd_elf_string_from_elf_section (input_bfd
,
9146 symtab_hdr
->sh_link
,
9151 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
9153 if (sym
.st_shndx
== SHN_BAD
)
9156 sym
.st_value
+= o
->output_offset
;
9158 finfo
->indices
[symndx
] = bfd_get_symcount (output_bfd
);
9159 if (! elf_link_output_sym (finfo
, name
, &sym
, o
, NULL
))
9162 elf_section_data (osec
)->this_hdr
.sh_info
9163 = finfo
->indices
[symndx
];
9167 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
9168 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
9171 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
9173 /* Section was created by _bfd_elf_link_create_dynamic_sections
9178 /* Get the contents of the section. They have been cached by a
9179 relaxation routine. Note that o is a section in an input
9180 file, so the contents field will not have been set by any of
9181 the routines which work on output files. */
9182 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
9183 contents
= elf_section_data (o
)->this_hdr
.contents
;
9186 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
9188 contents
= finfo
->contents
;
9189 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
9193 if ((o
->flags
& SEC_RELOC
) != 0)
9195 Elf_Internal_Rela
*internal_relocs
;
9196 Elf_Internal_Rela
*rel
, *relend
;
9197 bfd_vma r_type_mask
;
9199 int action_discarded
;
9202 /* Get the swapped relocs. */
9204 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
9205 finfo
->internal_relocs
, FALSE
);
9206 if (internal_relocs
== NULL
9207 && o
->reloc_count
> 0)
9210 if (bed
->s
->arch_size
== 32)
9217 r_type_mask
= 0xffffffff;
9221 action_discarded
= -1;
9222 if (!elf_section_ignore_discarded_relocs (o
))
9223 action_discarded
= (*bed
->action_discarded
) (o
);
9225 /* Run through the relocs evaluating complex reloc symbols and
9226 looking for relocs against symbols from discarded sections
9227 or section symbols from removed link-once sections.
9228 Complain about relocs against discarded sections. Zero
9229 relocs against removed link-once sections. */
9231 rel
= internal_relocs
;
9232 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9233 for ( ; rel
< relend
; rel
++)
9235 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
9236 unsigned int s_type
;
9237 asection
**ps
, *sec
;
9238 struct elf_link_hash_entry
*h
= NULL
;
9239 const char *sym_name
;
9241 if (r_symndx
== STN_UNDEF
)
9244 if (r_symndx
>= locsymcount
9245 || (elf_bad_symtab (input_bfd
)
9246 && finfo
->sections
[r_symndx
] == NULL
))
9248 h
= sym_hashes
[r_symndx
- extsymoff
];
9250 /* Badly formatted input files can contain relocs that
9251 reference non-existant symbols. Check here so that
9252 we do not seg fault. */
9257 sprintf_vma (buffer
, rel
->r_info
);
9258 (*_bfd_error_handler
)
9259 (_("error: %B contains a reloc (0x%s) for section %A "
9260 "that references a non-existent global symbol"),
9261 input_bfd
, o
, buffer
);
9262 bfd_set_error (bfd_error_bad_value
);
9266 while (h
->root
.type
== bfd_link_hash_indirect
9267 || h
->root
.type
== bfd_link_hash_warning
)
9268 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9273 if (h
->root
.type
== bfd_link_hash_defined
9274 || h
->root
.type
== bfd_link_hash_defweak
)
9275 ps
= &h
->root
.u
.def
.section
;
9277 sym_name
= h
->root
.root
.string
;
9281 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
9283 s_type
= ELF_ST_TYPE (sym
->st_info
);
9284 ps
= &finfo
->sections
[r_symndx
];
9285 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
9289 if (s_type
== STT_RELC
|| s_type
== STT_SRELC
)
9292 bfd_vma dot
= (rel
->r_offset
9293 + o
->output_offset
+ o
->output_section
->vma
);
9295 printf ("Encountered a complex symbol!");
9296 printf (" (input_bfd %s, section %s, reloc %ld\n",
9297 input_bfd
->filename
, o
->name
, rel
- internal_relocs
);
9298 printf (" symbol: idx %8.8lx, name %s\n",
9299 r_symndx
, sym_name
);
9300 printf (" reloc : info %8.8lx, addr %8.8lx\n",
9301 (unsigned long) rel
->r_info
,
9302 (unsigned long) rel
->r_offset
);
9304 if (!eval_symbol (&val
, &sym_name
, input_bfd
, finfo
, dot
,
9305 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
9308 /* Symbol evaluated OK. Update to absolute value. */
9309 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
9314 if (action_discarded
!= -1 && ps
!= NULL
)
9316 /* Complain if the definition comes from a
9317 discarded section. */
9318 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
9320 BFD_ASSERT (r_symndx
!= 0);
9321 if (action_discarded
& COMPLAIN
)
9322 (*finfo
->info
->callbacks
->einfo
)
9323 (_("%X`%s' referenced in section `%A' of %B: "
9324 "defined in discarded section `%A' of %B\n"),
9325 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
9327 /* Try to do the best we can to support buggy old
9328 versions of gcc. Pretend that the symbol is
9329 really defined in the kept linkonce section.
9330 FIXME: This is quite broken. Modifying the
9331 symbol here means we will be changing all later
9332 uses of the symbol, not just in this section. */
9333 if (action_discarded
& PRETEND
)
9337 kept
= _bfd_elf_check_kept_section (sec
,
9349 /* Relocate the section by invoking a back end routine.
9351 The back end routine is responsible for adjusting the
9352 section contents as necessary, and (if using Rela relocs
9353 and generating a relocatable output file) adjusting the
9354 reloc addend as necessary.
9356 The back end routine does not have to worry about setting
9357 the reloc address or the reloc symbol index.
9359 The back end routine is given a pointer to the swapped in
9360 internal symbols, and can access the hash table entries
9361 for the external symbols via elf_sym_hashes (input_bfd).
9363 When generating relocatable output, the back end routine
9364 must handle STB_LOCAL/STT_SECTION symbols specially. The
9365 output symbol is going to be a section symbol
9366 corresponding to the output section, which will require
9367 the addend to be adjusted. */
9369 ret
= (*relocate_section
) (output_bfd
, finfo
->info
,
9370 input_bfd
, o
, contents
,
9378 || finfo
->info
->relocatable
9379 || finfo
->info
->emitrelocations
)
9381 Elf_Internal_Rela
*irela
;
9382 Elf_Internal_Rela
*irelaend
;
9383 bfd_vma last_offset
;
9384 struct elf_link_hash_entry
**rel_hash
;
9385 struct elf_link_hash_entry
**rel_hash_list
;
9386 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
9387 unsigned int next_erel
;
9388 bfd_boolean rela_normal
;
9390 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
9391 rela_normal
= (bed
->rela_normal
9392 && (input_rel_hdr
->sh_entsize
9393 == bed
->s
->sizeof_rela
));
9395 /* Adjust the reloc addresses and symbol indices. */
9397 irela
= internal_relocs
;
9398 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9399 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
9400 + elf_section_data (o
->output_section
)->rel_count
9401 + elf_section_data (o
->output_section
)->rel_count2
);
9402 rel_hash_list
= rel_hash
;
9403 last_offset
= o
->output_offset
;
9404 if (!finfo
->info
->relocatable
)
9405 last_offset
+= o
->output_section
->vma
;
9406 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
9408 unsigned long r_symndx
;
9410 Elf_Internal_Sym sym
;
9412 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
9418 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
9421 if (irela
->r_offset
>= (bfd_vma
) -2)
9423 /* This is a reloc for a deleted entry or somesuch.
9424 Turn it into an R_*_NONE reloc, at the same
9425 offset as the last reloc. elf_eh_frame.c and
9426 bfd_elf_discard_info rely on reloc offsets
9428 irela
->r_offset
= last_offset
;
9430 irela
->r_addend
= 0;
9434 irela
->r_offset
+= o
->output_offset
;
9436 /* Relocs in an executable have to be virtual addresses. */
9437 if (!finfo
->info
->relocatable
)
9438 irela
->r_offset
+= o
->output_section
->vma
;
9440 last_offset
= irela
->r_offset
;
9442 r_symndx
= irela
->r_info
>> r_sym_shift
;
9443 if (r_symndx
== STN_UNDEF
)
9446 if (r_symndx
>= locsymcount
9447 || (elf_bad_symtab (input_bfd
)
9448 && finfo
->sections
[r_symndx
] == NULL
))
9450 struct elf_link_hash_entry
*rh
;
9453 /* This is a reloc against a global symbol. We
9454 have not yet output all the local symbols, so
9455 we do not know the symbol index of any global
9456 symbol. We set the rel_hash entry for this
9457 reloc to point to the global hash table entry
9458 for this symbol. The symbol index is then
9459 set at the end of bfd_elf_final_link. */
9460 indx
= r_symndx
- extsymoff
;
9461 rh
= elf_sym_hashes (input_bfd
)[indx
];
9462 while (rh
->root
.type
== bfd_link_hash_indirect
9463 || rh
->root
.type
== bfd_link_hash_warning
)
9464 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
9466 /* Setting the index to -2 tells
9467 elf_link_output_extsym that this symbol is
9469 BFD_ASSERT (rh
->indx
< 0);
9477 /* This is a reloc against a local symbol. */
9480 sym
= isymbuf
[r_symndx
];
9481 sec
= finfo
->sections
[r_symndx
];
9482 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
9484 /* I suppose the backend ought to fill in the
9485 section of any STT_SECTION symbol against a
9486 processor specific section. */
9488 if (bfd_is_abs_section (sec
))
9490 else if (sec
== NULL
|| sec
->owner
== NULL
)
9492 bfd_set_error (bfd_error_bad_value
);
9497 asection
*osec
= sec
->output_section
;
9499 /* If we have discarded a section, the output
9500 section will be the absolute section. In
9501 case of discarded SEC_MERGE sections, use
9502 the kept section. relocate_section should
9503 have already handled discarded linkonce
9505 if (bfd_is_abs_section (osec
)
9506 && sec
->kept_section
!= NULL
9507 && sec
->kept_section
->output_section
!= NULL
)
9509 osec
= sec
->kept_section
->output_section
;
9510 irela
->r_addend
-= osec
->vma
;
9513 if (!bfd_is_abs_section (osec
))
9515 r_symndx
= osec
->target_index
;
9518 struct elf_link_hash_table
*htab
;
9521 htab
= elf_hash_table (finfo
->info
);
9522 oi
= htab
->text_index_section
;
9523 if ((osec
->flags
& SEC_READONLY
) == 0
9524 && htab
->data_index_section
!= NULL
)
9525 oi
= htab
->data_index_section
;
9529 irela
->r_addend
+= osec
->vma
- oi
->vma
;
9530 r_symndx
= oi
->target_index
;
9534 BFD_ASSERT (r_symndx
!= 0);
9538 /* Adjust the addend according to where the
9539 section winds up in the output section. */
9541 irela
->r_addend
+= sec
->output_offset
;
9545 if (finfo
->indices
[r_symndx
] == -1)
9547 unsigned long shlink
;
9551 if (finfo
->info
->strip
== strip_all
)
9553 /* You can't do ld -r -s. */
9554 bfd_set_error (bfd_error_invalid_operation
);
9558 /* This symbol was skipped earlier, but
9559 since it is needed by a reloc, we
9560 must output it now. */
9561 shlink
= symtab_hdr
->sh_link
;
9562 name
= (bfd_elf_string_from_elf_section
9563 (input_bfd
, shlink
, sym
.st_name
));
9567 osec
= sec
->output_section
;
9569 _bfd_elf_section_from_bfd_section (output_bfd
,
9571 if (sym
.st_shndx
== SHN_BAD
)
9574 sym
.st_value
+= sec
->output_offset
;
9575 if (! finfo
->info
->relocatable
)
9577 sym
.st_value
+= osec
->vma
;
9578 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
9580 /* STT_TLS symbols are relative to PT_TLS
9582 BFD_ASSERT (elf_hash_table (finfo
->info
)
9584 sym
.st_value
-= (elf_hash_table (finfo
->info
)
9589 finfo
->indices
[r_symndx
]
9590 = bfd_get_symcount (output_bfd
);
9592 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
9597 r_symndx
= finfo
->indices
[r_symndx
];
9600 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
9601 | (irela
->r_info
& r_type_mask
));
9604 /* Swap out the relocs. */
9605 if (input_rel_hdr
->sh_size
!= 0
9606 && !bed
->elf_backend_emit_relocs (output_bfd
, o
,
9612 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
9613 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
9615 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
9616 * bed
->s
->int_rels_per_ext_rel
);
9617 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
9618 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
9627 /* Write out the modified section contents. */
9628 if (bed
->elf_backend_write_section
9629 && (*bed
->elf_backend_write_section
) (output_bfd
, finfo
->info
, o
,
9632 /* Section written out. */
9634 else switch (o
->sec_info_type
)
9636 case ELF_INFO_TYPE_STABS
:
9637 if (! (_bfd_write_section_stabs
9639 &elf_hash_table (finfo
->info
)->stab_info
,
9640 o
, &elf_section_data (o
)->sec_info
, contents
)))
9643 case ELF_INFO_TYPE_MERGE
:
9644 if (! _bfd_write_merged_section (output_bfd
, o
,
9645 elf_section_data (o
)->sec_info
))
9648 case ELF_INFO_TYPE_EH_FRAME
:
9650 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
9657 if (! (o
->flags
& SEC_EXCLUDE
)
9658 && ! (o
->output_section
->flags
& SEC_NEVER_LOAD
)
9659 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
9661 (file_ptr
) o
->output_offset
,
9672 /* Generate a reloc when linking an ELF file. This is a reloc
9673 requested by the linker, and does not come from any input file. This
9674 is used to build constructor and destructor tables when linking
9678 elf_reloc_link_order (bfd
*output_bfd
,
9679 struct bfd_link_info
*info
,
9680 asection
*output_section
,
9681 struct bfd_link_order
*link_order
)
9683 reloc_howto_type
*howto
;
9687 struct elf_link_hash_entry
**rel_hash_ptr
;
9688 Elf_Internal_Shdr
*rel_hdr
;
9689 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
9690 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
9694 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
9697 bfd_set_error (bfd_error_bad_value
);
9701 addend
= link_order
->u
.reloc
.p
->addend
;
9703 /* Figure out the symbol index. */
9704 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
9705 + elf_section_data (output_section
)->rel_count
9706 + elf_section_data (output_section
)->rel_count2
);
9707 if (link_order
->type
== bfd_section_reloc_link_order
)
9709 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
9710 BFD_ASSERT (indx
!= 0);
9711 *rel_hash_ptr
= NULL
;
9715 struct elf_link_hash_entry
*h
;
9717 /* Treat a reloc against a defined symbol as though it were
9718 actually against the section. */
9719 h
= ((struct elf_link_hash_entry
*)
9720 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
9721 link_order
->u
.reloc
.p
->u
.name
,
9722 FALSE
, FALSE
, TRUE
));
9724 && (h
->root
.type
== bfd_link_hash_defined
9725 || h
->root
.type
== bfd_link_hash_defweak
))
9729 section
= h
->root
.u
.def
.section
;
9730 indx
= section
->output_section
->target_index
;
9731 *rel_hash_ptr
= NULL
;
9732 /* It seems that we ought to add the symbol value to the
9733 addend here, but in practice it has already been added
9734 because it was passed to constructor_callback. */
9735 addend
+= section
->output_section
->vma
+ section
->output_offset
;
9739 /* Setting the index to -2 tells elf_link_output_extsym that
9740 this symbol is used by a reloc. */
9747 if (! ((*info
->callbacks
->unattached_reloc
)
9748 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
9754 /* If this is an inplace reloc, we must write the addend into the
9756 if (howto
->partial_inplace
&& addend
!= 0)
9759 bfd_reloc_status_type rstat
;
9762 const char *sym_name
;
9764 size
= bfd_get_reloc_size (howto
);
9765 buf
= bfd_zmalloc (size
);
9768 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
9775 case bfd_reloc_outofrange
:
9778 case bfd_reloc_overflow
:
9779 if (link_order
->type
== bfd_section_reloc_link_order
)
9780 sym_name
= bfd_section_name (output_bfd
,
9781 link_order
->u
.reloc
.p
->u
.section
);
9783 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
9784 if (! ((*info
->callbacks
->reloc_overflow
)
9785 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
9786 NULL
, (bfd_vma
) 0)))
9793 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
9794 link_order
->offset
, size
);
9800 /* The address of a reloc is relative to the section in a
9801 relocatable file, and is a virtual address in an executable
9803 offset
= link_order
->offset
;
9804 if (! info
->relocatable
)
9805 offset
+= output_section
->vma
;
9807 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
9809 irel
[i
].r_offset
= offset
;
9811 irel
[i
].r_addend
= 0;
9813 if (bed
->s
->arch_size
== 32)
9814 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
9816 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
9818 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
9819 erel
= rel_hdr
->contents
;
9820 if (rel_hdr
->sh_type
== SHT_REL
)
9822 erel
+= (elf_section_data (output_section
)->rel_count
9823 * bed
->s
->sizeof_rel
);
9824 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
9828 irel
[0].r_addend
= addend
;
9829 erel
+= (elf_section_data (output_section
)->rel_count
9830 * bed
->s
->sizeof_rela
);
9831 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
9834 ++elf_section_data (output_section
)->rel_count
;
9840 /* Get the output vma of the section pointed to by the sh_link field. */
9843 elf_get_linked_section_vma (struct bfd_link_order
*p
)
9845 Elf_Internal_Shdr
**elf_shdrp
;
9849 s
= p
->u
.indirect
.section
;
9850 elf_shdrp
= elf_elfsections (s
->owner
);
9851 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
9852 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
9854 The Intel C compiler generates SHT_IA_64_UNWIND with
9855 SHF_LINK_ORDER. But it doesn't set the sh_link or
9856 sh_info fields. Hence we could get the situation
9857 where elfsec is 0. */
9860 const struct elf_backend_data
*bed
9861 = get_elf_backend_data (s
->owner
);
9862 if (bed
->link_order_error_handler
)
9863 bed
->link_order_error_handler
9864 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
9869 s
= elf_shdrp
[elfsec
]->bfd_section
;
9870 return s
->output_section
->vma
+ s
->output_offset
;
9875 /* Compare two sections based on the locations of the sections they are
9876 linked to. Used by elf_fixup_link_order. */
9879 compare_link_order (const void * a
, const void * b
)
9884 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
9885 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
9892 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
9893 order as their linked sections. Returns false if this could not be done
9894 because an output section includes both ordered and unordered
9895 sections. Ideally we'd do this in the linker proper. */
9898 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
9903 struct bfd_link_order
*p
;
9905 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9907 struct bfd_link_order
**sections
;
9908 asection
*s
, *other_sec
, *linkorder_sec
;
9912 linkorder_sec
= NULL
;
9915 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9917 if (p
->type
== bfd_indirect_link_order
)
9919 s
= p
->u
.indirect
.section
;
9921 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
9922 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
9923 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
9924 && elfsec
< elf_numsections (sub
)
9925 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
9926 && elf_elfsections (sub
)[elfsec
]->sh_link
< elf_numsections (sub
))
9940 if (seen_other
&& seen_linkorder
)
9942 if (other_sec
&& linkorder_sec
)
9943 (*_bfd_error_handler
) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
9945 linkorder_sec
->owner
, other_sec
,
9948 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
9950 bfd_set_error (bfd_error_bad_value
);
9955 if (!seen_linkorder
)
9958 sections
= (struct bfd_link_order
**)
9959 bfd_malloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
9960 if (sections
== NULL
)
9964 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9966 sections
[seen_linkorder
++] = p
;
9968 /* Sort the input sections in the order of their linked section. */
9969 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
9970 compare_link_order
);
9972 /* Change the offsets of the sections. */
9974 for (n
= 0; n
< seen_linkorder
; n
++)
9976 s
= sections
[n
]->u
.indirect
.section
;
9977 offset
&= ~(bfd_vma
) 0 << s
->alignment_power
;
9978 s
->output_offset
= offset
;
9979 sections
[n
]->offset
= offset
;
9980 offset
+= sections
[n
]->size
;
9988 /* Do the final step of an ELF link. */
9991 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9993 bfd_boolean dynamic
;
9994 bfd_boolean emit_relocs
;
9996 struct elf_final_link_info finfo
;
9997 register asection
*o
;
9998 register struct bfd_link_order
*p
;
10000 bfd_size_type max_contents_size
;
10001 bfd_size_type max_external_reloc_size
;
10002 bfd_size_type max_internal_reloc_count
;
10003 bfd_size_type max_sym_count
;
10004 bfd_size_type max_sym_shndx_count
;
10006 Elf_Internal_Sym elfsym
;
10008 Elf_Internal_Shdr
*symtab_hdr
;
10009 Elf_Internal_Shdr
*symtab_shndx_hdr
;
10010 Elf_Internal_Shdr
*symstrtab_hdr
;
10011 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10012 struct elf_outext_info eoinfo
;
10013 bfd_boolean merged
;
10014 size_t relativecount
= 0;
10015 asection
*reldyn
= 0;
10017 asection
*attr_section
= NULL
;
10018 bfd_vma attr_size
= 0;
10019 const char *std_attrs_section
;
10021 if (! is_elf_hash_table (info
->hash
))
10025 abfd
->flags
|= DYNAMIC
;
10027 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
10028 dynobj
= elf_hash_table (info
)->dynobj
;
10030 emit_relocs
= (info
->relocatable
10031 || info
->emitrelocations
);
10034 finfo
.output_bfd
= abfd
;
10035 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
10036 if (finfo
.symstrtab
== NULL
)
10041 finfo
.dynsym_sec
= NULL
;
10042 finfo
.hash_sec
= NULL
;
10043 finfo
.symver_sec
= NULL
;
10047 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
10048 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
10049 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
);
10050 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
10051 /* Note that it is OK if symver_sec is NULL. */
10054 finfo
.contents
= NULL
;
10055 finfo
.external_relocs
= NULL
;
10056 finfo
.internal_relocs
= NULL
;
10057 finfo
.external_syms
= NULL
;
10058 finfo
.locsym_shndx
= NULL
;
10059 finfo
.internal_syms
= NULL
;
10060 finfo
.indices
= NULL
;
10061 finfo
.sections
= NULL
;
10062 finfo
.symbuf
= NULL
;
10063 finfo
.symshndxbuf
= NULL
;
10064 finfo
.symbuf_count
= 0;
10065 finfo
.shndxbuf_size
= 0;
10067 /* The object attributes have been merged. Remove the input
10068 sections from the link, and set the contents of the output
10070 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
10071 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10073 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
10074 || strcmp (o
->name
, ".gnu.attributes") == 0)
10076 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10078 asection
*input_section
;
10080 if (p
->type
!= bfd_indirect_link_order
)
10082 input_section
= p
->u
.indirect
.section
;
10083 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10084 elf_link_input_bfd ignores this section. */
10085 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
10088 attr_size
= bfd_elf_obj_attr_size (abfd
);
10091 bfd_set_section_size (abfd
, o
, attr_size
);
10093 /* Skip this section later on. */
10094 o
->map_head
.link_order
= NULL
;
10097 o
->flags
|= SEC_EXCLUDE
;
10101 /* Count up the number of relocations we will output for each output
10102 section, so that we know the sizes of the reloc sections. We
10103 also figure out some maximum sizes. */
10104 max_contents_size
= 0;
10105 max_external_reloc_size
= 0;
10106 max_internal_reloc_count
= 0;
10108 max_sym_shndx_count
= 0;
10110 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10112 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
10113 o
->reloc_count
= 0;
10115 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10117 unsigned int reloc_count
= 0;
10118 struct bfd_elf_section_data
*esdi
= NULL
;
10119 unsigned int *rel_count1
;
10121 if (p
->type
== bfd_section_reloc_link_order
10122 || p
->type
== bfd_symbol_reloc_link_order
)
10124 else if (p
->type
== bfd_indirect_link_order
)
10128 sec
= p
->u
.indirect
.section
;
10129 esdi
= elf_section_data (sec
);
10131 /* Mark all sections which are to be included in the
10132 link. This will normally be every section. We need
10133 to do this so that we can identify any sections which
10134 the linker has decided to not include. */
10135 sec
->linker_mark
= TRUE
;
10137 if (sec
->flags
& SEC_MERGE
)
10140 if (info
->relocatable
|| info
->emitrelocations
)
10141 reloc_count
= sec
->reloc_count
;
10142 else if (bed
->elf_backend_count_relocs
)
10143 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
10145 if (sec
->rawsize
> max_contents_size
)
10146 max_contents_size
= sec
->rawsize
;
10147 if (sec
->size
> max_contents_size
)
10148 max_contents_size
= sec
->size
;
10150 /* We are interested in just local symbols, not all
10152 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
10153 && (sec
->owner
->flags
& DYNAMIC
) == 0)
10157 if (elf_bad_symtab (sec
->owner
))
10158 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
10159 / bed
->s
->sizeof_sym
);
10161 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
10163 if (sym_count
> max_sym_count
)
10164 max_sym_count
= sym_count
;
10166 if (sym_count
> max_sym_shndx_count
10167 && elf_symtab_shndx (sec
->owner
) != 0)
10168 max_sym_shndx_count
= sym_count
;
10170 if ((sec
->flags
& SEC_RELOC
) != 0)
10174 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
10175 if (ext_size
> max_external_reloc_size
)
10176 max_external_reloc_size
= ext_size
;
10177 if (sec
->reloc_count
> max_internal_reloc_count
)
10178 max_internal_reloc_count
= sec
->reloc_count
;
10183 if (reloc_count
== 0)
10186 o
->reloc_count
+= reloc_count
;
10188 /* MIPS may have a mix of REL and RELA relocs on sections.
10189 To support this curious ABI we keep reloc counts in
10190 elf_section_data too. We must be careful to add the
10191 relocations from the input section to the right output
10192 count. FIXME: Get rid of one count. We have
10193 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
10194 rel_count1
= &esdo
->rel_count
;
10197 bfd_boolean same_size
;
10198 bfd_size_type entsize1
;
10200 entsize1
= esdi
->rel_hdr
.sh_entsize
;
10201 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
10202 || entsize1
== bed
->s
->sizeof_rela
);
10203 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
10206 rel_count1
= &esdo
->rel_count2
;
10208 if (esdi
->rel_hdr2
!= NULL
)
10210 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
10211 unsigned int alt_count
;
10212 unsigned int *rel_count2
;
10214 BFD_ASSERT (entsize2
!= entsize1
10215 && (entsize2
== bed
->s
->sizeof_rel
10216 || entsize2
== bed
->s
->sizeof_rela
));
10218 rel_count2
= &esdo
->rel_count2
;
10220 rel_count2
= &esdo
->rel_count
;
10222 /* The following is probably too simplistic if the
10223 backend counts output relocs unusually. */
10224 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
10225 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
10226 *rel_count2
+= alt_count
;
10227 reloc_count
-= alt_count
;
10230 *rel_count1
+= reloc_count
;
10233 if (o
->reloc_count
> 0)
10234 o
->flags
|= SEC_RELOC
;
10237 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10238 set it (this is probably a bug) and if it is set
10239 assign_section_numbers will create a reloc section. */
10240 o
->flags
&=~ SEC_RELOC
;
10243 /* If the SEC_ALLOC flag is not set, force the section VMA to
10244 zero. This is done in elf_fake_sections as well, but forcing
10245 the VMA to 0 here will ensure that relocs against these
10246 sections are handled correctly. */
10247 if ((o
->flags
& SEC_ALLOC
) == 0
10248 && ! o
->user_set_vma
)
10252 if (! info
->relocatable
&& merged
)
10253 elf_link_hash_traverse (elf_hash_table (info
),
10254 _bfd_elf_link_sec_merge_syms
, abfd
);
10256 /* Figure out the file positions for everything but the symbol table
10257 and the relocs. We set symcount to force assign_section_numbers
10258 to create a symbol table. */
10259 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
10260 BFD_ASSERT (! abfd
->output_has_begun
);
10261 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
10264 /* Set sizes, and assign file positions for reloc sections. */
10265 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10267 if ((o
->flags
& SEC_RELOC
) != 0)
10269 if (!(_bfd_elf_link_size_reloc_section
10270 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
10273 if (elf_section_data (o
)->rel_hdr2
10274 && !(_bfd_elf_link_size_reloc_section
10275 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
10279 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10280 to count upwards while actually outputting the relocations. */
10281 elf_section_data (o
)->rel_count
= 0;
10282 elf_section_data (o
)->rel_count2
= 0;
10285 _bfd_elf_assign_file_positions_for_relocs (abfd
);
10287 /* We have now assigned file positions for all the sections except
10288 .symtab and .strtab. We start the .symtab section at the current
10289 file position, and write directly to it. We build the .strtab
10290 section in memory. */
10291 bfd_get_symcount (abfd
) = 0;
10292 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
10293 /* sh_name is set in prep_headers. */
10294 symtab_hdr
->sh_type
= SHT_SYMTAB
;
10295 /* sh_flags, sh_addr and sh_size all start off zero. */
10296 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
10297 /* sh_link is set in assign_section_numbers. */
10298 /* sh_info is set below. */
10299 /* sh_offset is set just below. */
10300 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
10302 off
= elf_tdata (abfd
)->next_file_pos
;
10303 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
10305 /* Note that at this point elf_tdata (abfd)->next_file_pos is
10306 incorrect. We do not yet know the size of the .symtab section.
10307 We correct next_file_pos below, after we do know the size. */
10309 /* Allocate a buffer to hold swapped out symbols. This is to avoid
10310 continuously seeking to the right position in the file. */
10311 if (! info
->keep_memory
|| max_sym_count
< 20)
10312 finfo
.symbuf_size
= 20;
10314 finfo
.symbuf_size
= max_sym_count
;
10315 amt
= finfo
.symbuf_size
;
10316 amt
*= bed
->s
->sizeof_sym
;
10317 finfo
.symbuf
= bfd_malloc (amt
);
10318 if (finfo
.symbuf
== NULL
)
10320 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
10322 /* Wild guess at number of output symbols. realloc'd as needed. */
10323 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
10324 finfo
.shndxbuf_size
= amt
;
10325 amt
*= sizeof (Elf_External_Sym_Shndx
);
10326 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
10327 if (finfo
.symshndxbuf
== NULL
)
10331 /* Start writing out the symbol table. The first symbol is always a
10333 if (info
->strip
!= strip_all
10336 elfsym
.st_value
= 0;
10337 elfsym
.st_size
= 0;
10338 elfsym
.st_info
= 0;
10339 elfsym
.st_other
= 0;
10340 elfsym
.st_shndx
= SHN_UNDEF
;
10341 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
10346 /* Output a symbol for each section. We output these even if we are
10347 discarding local symbols, since they are used for relocs. These
10348 symbols have no names. We store the index of each one in the
10349 index field of the section, so that we can find it again when
10350 outputting relocs. */
10351 if (info
->strip
!= strip_all
10354 elfsym
.st_size
= 0;
10355 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
10356 elfsym
.st_other
= 0;
10357 elfsym
.st_value
= 0;
10358 for (i
= 1; i
< elf_numsections (abfd
); i
++)
10360 o
= bfd_section_from_elf_index (abfd
, i
);
10363 o
->target_index
= bfd_get_symcount (abfd
);
10364 elfsym
.st_shndx
= i
;
10365 if (!info
->relocatable
)
10366 elfsym
.st_value
= o
->vma
;
10367 if (!elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
10373 /* Allocate some memory to hold information read in from the input
10375 if (max_contents_size
!= 0)
10377 finfo
.contents
= bfd_malloc (max_contents_size
);
10378 if (finfo
.contents
== NULL
)
10382 if (max_external_reloc_size
!= 0)
10384 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
10385 if (finfo
.external_relocs
== NULL
)
10389 if (max_internal_reloc_count
!= 0)
10391 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10392 amt
*= sizeof (Elf_Internal_Rela
);
10393 finfo
.internal_relocs
= bfd_malloc (amt
);
10394 if (finfo
.internal_relocs
== NULL
)
10398 if (max_sym_count
!= 0)
10400 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
10401 finfo
.external_syms
= bfd_malloc (amt
);
10402 if (finfo
.external_syms
== NULL
)
10405 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
10406 finfo
.internal_syms
= bfd_malloc (amt
);
10407 if (finfo
.internal_syms
== NULL
)
10410 amt
= max_sym_count
* sizeof (long);
10411 finfo
.indices
= bfd_malloc (amt
);
10412 if (finfo
.indices
== NULL
)
10415 amt
= max_sym_count
* sizeof (asection
*);
10416 finfo
.sections
= bfd_malloc (amt
);
10417 if (finfo
.sections
== NULL
)
10421 if (max_sym_shndx_count
!= 0)
10423 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
10424 finfo
.locsym_shndx
= bfd_malloc (amt
);
10425 if (finfo
.locsym_shndx
== NULL
)
10429 if (elf_hash_table (info
)->tls_sec
)
10431 bfd_vma base
, end
= 0;
10434 for (sec
= elf_hash_table (info
)->tls_sec
;
10435 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
10438 bfd_size_type size
= sec
->size
;
10441 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
10443 struct bfd_link_order
*o
= sec
->map_tail
.link_order
;
10445 size
= o
->offset
+ o
->size
;
10447 end
= sec
->vma
+ size
;
10449 base
= elf_hash_table (info
)->tls_sec
->vma
;
10450 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
10451 elf_hash_table (info
)->tls_size
= end
- base
;
10454 /* Reorder SHF_LINK_ORDER sections. */
10455 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10457 if (!elf_fixup_link_order (abfd
, o
))
10461 /* Since ELF permits relocations to be against local symbols, we
10462 must have the local symbols available when we do the relocations.
10463 Since we would rather only read the local symbols once, and we
10464 would rather not keep them in memory, we handle all the
10465 relocations for a single input file at the same time.
10467 Unfortunately, there is no way to know the total number of local
10468 symbols until we have seen all of them, and the local symbol
10469 indices precede the global symbol indices. This means that when
10470 we are generating relocatable output, and we see a reloc against
10471 a global symbol, we can not know the symbol index until we have
10472 finished examining all the local symbols to see which ones we are
10473 going to output. To deal with this, we keep the relocations in
10474 memory, and don't output them until the end of the link. This is
10475 an unfortunate waste of memory, but I don't see a good way around
10476 it. Fortunately, it only happens when performing a relocatable
10477 link, which is not the common case. FIXME: If keep_memory is set
10478 we could write the relocs out and then read them again; I don't
10479 know how bad the memory loss will be. */
10481 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10482 sub
->output_has_begun
= FALSE
;
10483 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10485 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10487 if (p
->type
== bfd_indirect_link_order
10488 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
10489 == bfd_target_elf_flavour
)
10490 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
10492 if (! sub
->output_has_begun
)
10494 if (! elf_link_input_bfd (&finfo
, sub
))
10496 sub
->output_has_begun
= TRUE
;
10499 else if (p
->type
== bfd_section_reloc_link_order
10500 || p
->type
== bfd_symbol_reloc_link_order
)
10502 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
10507 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
10513 /* Free symbol buffer if needed. */
10514 if (!info
->reduce_memory_overheads
)
10516 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10517 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
10518 && elf_tdata (sub
)->symbuf
)
10520 free (elf_tdata (sub
)->symbuf
);
10521 elf_tdata (sub
)->symbuf
= NULL
;
10525 /* Output any global symbols that got converted to local in a
10526 version script or due to symbol visibility. We do this in a
10527 separate step since ELF requires all local symbols to appear
10528 prior to any global symbols. FIXME: We should only do this if
10529 some global symbols were, in fact, converted to become local.
10530 FIXME: Will this work correctly with the Irix 5 linker? */
10531 eoinfo
.failed
= FALSE
;
10532 eoinfo
.finfo
= &finfo
;
10533 eoinfo
.localsyms
= TRUE
;
10534 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
10539 /* If backend needs to output some local symbols not present in the hash
10540 table, do it now. */
10541 if (bed
->elf_backend_output_arch_local_syms
)
10543 typedef bfd_boolean (*out_sym_func
)
10544 (void *, const char *, Elf_Internal_Sym
*, asection
*,
10545 struct elf_link_hash_entry
*);
10547 if (! ((*bed
->elf_backend_output_arch_local_syms
)
10548 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
10552 /* That wrote out all the local symbols. Finish up the symbol table
10553 with the global symbols. Even if we want to strip everything we
10554 can, we still need to deal with those global symbols that got
10555 converted to local in a version script. */
10557 /* The sh_info field records the index of the first non local symbol. */
10558 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
10561 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
10563 Elf_Internal_Sym sym
;
10564 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
10565 long last_local
= 0;
10567 /* Write out the section symbols for the output sections. */
10568 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
10574 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
10577 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
10583 dynindx
= elf_section_data (s
)->dynindx
;
10586 indx
= elf_section_data (s
)->this_idx
;
10587 BFD_ASSERT (indx
> 0);
10588 sym
.st_shndx
= indx
;
10589 if (! check_dynsym (abfd
, &sym
))
10591 sym
.st_value
= s
->vma
;
10592 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
10593 if (last_local
< dynindx
)
10594 last_local
= dynindx
;
10595 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
10599 /* Write out the local dynsyms. */
10600 if (elf_hash_table (info
)->dynlocal
)
10602 struct elf_link_local_dynamic_entry
*e
;
10603 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
10608 sym
.st_size
= e
->isym
.st_size
;
10609 sym
.st_other
= e
->isym
.st_other
;
10611 /* Copy the internal symbol as is.
10612 Note that we saved a word of storage and overwrote
10613 the original st_name with the dynstr_index. */
10616 s
= bfd_section_from_elf_index (e
->input_bfd
,
10621 elf_section_data (s
->output_section
)->this_idx
;
10622 if (! check_dynsym (abfd
, &sym
))
10624 sym
.st_value
= (s
->output_section
->vma
10626 + e
->isym
.st_value
);
10629 if (last_local
< e
->dynindx
)
10630 last_local
= e
->dynindx
;
10632 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
10633 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
10637 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
10641 /* We get the global symbols from the hash table. */
10642 eoinfo
.failed
= FALSE
;
10643 eoinfo
.localsyms
= FALSE
;
10644 eoinfo
.finfo
= &finfo
;
10645 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
10650 /* If backend needs to output some symbols not present in the hash
10651 table, do it now. */
10652 if (bed
->elf_backend_output_arch_syms
)
10654 typedef bfd_boolean (*out_sym_func
)
10655 (void *, const char *, Elf_Internal_Sym
*, asection
*,
10656 struct elf_link_hash_entry
*);
10658 if (! ((*bed
->elf_backend_output_arch_syms
)
10659 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
10663 /* Flush all symbols to the file. */
10664 if (! elf_link_flush_output_syms (&finfo
, bed
))
10667 /* Now we know the size of the symtab section. */
10668 off
+= symtab_hdr
->sh_size
;
10670 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
10671 if (symtab_shndx_hdr
->sh_name
!= 0)
10673 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
10674 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
10675 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
10676 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
10677 symtab_shndx_hdr
->sh_size
= amt
;
10679 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
10682 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
10683 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
10688 /* Finish up and write out the symbol string table (.strtab)
10690 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
10691 /* sh_name was set in prep_headers. */
10692 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
10693 symstrtab_hdr
->sh_flags
= 0;
10694 symstrtab_hdr
->sh_addr
= 0;
10695 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
10696 symstrtab_hdr
->sh_entsize
= 0;
10697 symstrtab_hdr
->sh_link
= 0;
10698 symstrtab_hdr
->sh_info
= 0;
10699 /* sh_offset is set just below. */
10700 symstrtab_hdr
->sh_addralign
= 1;
10702 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
10703 elf_tdata (abfd
)->next_file_pos
= off
;
10705 if (bfd_get_symcount (abfd
) > 0)
10707 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
10708 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
10712 /* Adjust the relocs to have the correct symbol indices. */
10713 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10715 if ((o
->flags
& SEC_RELOC
) == 0)
10718 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
10719 elf_section_data (o
)->rel_count
,
10720 elf_section_data (o
)->rel_hashes
);
10721 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
10722 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
10723 elf_section_data (o
)->rel_count2
,
10724 (elf_section_data (o
)->rel_hashes
10725 + elf_section_data (o
)->rel_count
));
10727 /* Set the reloc_count field to 0 to prevent write_relocs from
10728 trying to swap the relocs out itself. */
10729 o
->reloc_count
= 0;
10732 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
10733 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
10735 /* If we are linking against a dynamic object, or generating a
10736 shared library, finish up the dynamic linking information. */
10739 bfd_byte
*dyncon
, *dynconend
;
10741 /* Fix up .dynamic entries. */
10742 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
10743 BFD_ASSERT (o
!= NULL
);
10745 dyncon
= o
->contents
;
10746 dynconend
= o
->contents
+ o
->size
;
10747 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
10749 Elf_Internal_Dyn dyn
;
10753 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
10760 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
10762 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
10764 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
10765 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
10768 dyn
.d_un
.d_val
= relativecount
;
10775 name
= info
->init_function
;
10778 name
= info
->fini_function
;
10781 struct elf_link_hash_entry
*h
;
10783 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
10784 FALSE
, FALSE
, TRUE
);
10786 && (h
->root
.type
== bfd_link_hash_defined
10787 || h
->root
.type
== bfd_link_hash_defweak
))
10789 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
10790 o
= h
->root
.u
.def
.section
;
10791 if (o
->output_section
!= NULL
)
10792 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
10793 + o
->output_offset
);
10796 /* The symbol is imported from another shared
10797 library and does not apply to this one. */
10798 dyn
.d_un
.d_ptr
= 0;
10805 case DT_PREINIT_ARRAYSZ
:
10806 name
= ".preinit_array";
10808 case DT_INIT_ARRAYSZ
:
10809 name
= ".init_array";
10811 case DT_FINI_ARRAYSZ
:
10812 name
= ".fini_array";
10814 o
= bfd_get_section_by_name (abfd
, name
);
10817 (*_bfd_error_handler
)
10818 (_("%B: could not find output section %s"), abfd
, name
);
10822 (*_bfd_error_handler
)
10823 (_("warning: %s section has zero size"), name
);
10824 dyn
.d_un
.d_val
= o
->size
;
10827 case DT_PREINIT_ARRAY
:
10828 name
= ".preinit_array";
10830 case DT_INIT_ARRAY
:
10831 name
= ".init_array";
10833 case DT_FINI_ARRAY
:
10834 name
= ".fini_array";
10841 name
= ".gnu.hash";
10850 name
= ".gnu.version_d";
10853 name
= ".gnu.version_r";
10856 name
= ".gnu.version";
10858 o
= bfd_get_section_by_name (abfd
, name
);
10861 (*_bfd_error_handler
)
10862 (_("%B: could not find output section %s"), abfd
, name
);
10865 dyn
.d_un
.d_ptr
= o
->vma
;
10872 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
10876 dyn
.d_un
.d_val
= 0;
10877 dyn
.d_un
.d_ptr
= 0;
10878 for (i
= 1; i
< elf_numsections (abfd
); i
++)
10880 Elf_Internal_Shdr
*hdr
;
10882 hdr
= elf_elfsections (abfd
)[i
];
10883 if (hdr
->sh_type
== type
10884 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
10886 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
10887 dyn
.d_un
.d_val
+= hdr
->sh_size
;
10890 if (dyn
.d_un
.d_ptr
== 0
10891 || hdr
->sh_addr
< dyn
.d_un
.d_ptr
)
10892 dyn
.d_un
.d_ptr
= hdr
->sh_addr
;
10898 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
10902 /* If we have created any dynamic sections, then output them. */
10903 if (dynobj
!= NULL
)
10905 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
10908 /* Check for DT_TEXTREL (late, in case the backend removes it). */
10909 if (info
->warn_shared_textrel
&& info
->shared
)
10911 bfd_byte
*dyncon
, *dynconend
;
10913 /* Fix up .dynamic entries. */
10914 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
10915 BFD_ASSERT (o
!= NULL
);
10917 dyncon
= o
->contents
;
10918 dynconend
= o
->contents
+ o
->size
;
10919 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
10921 Elf_Internal_Dyn dyn
;
10923 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
10925 if (dyn
.d_tag
== DT_TEXTREL
)
10927 info
->callbacks
->einfo
10928 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
10934 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
10936 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
10938 || o
->output_section
== bfd_abs_section_ptr
)
10940 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
10942 /* At this point, we are only interested in sections
10943 created by _bfd_elf_link_create_dynamic_sections. */
10946 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
10948 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
10950 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
10952 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
10954 if (! bfd_set_section_contents (abfd
, o
->output_section
,
10956 (file_ptr
) o
->output_offset
,
10962 /* The contents of the .dynstr section are actually in a
10964 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
10965 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
10966 || ! _bfd_elf_strtab_emit (abfd
,
10967 elf_hash_table (info
)->dynstr
))
10973 if (info
->relocatable
)
10975 bfd_boolean failed
= FALSE
;
10977 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
10982 /* If we have optimized stabs strings, output them. */
10983 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
10985 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
10989 if (info
->eh_frame_hdr
)
10991 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
10995 if (finfo
.symstrtab
!= NULL
)
10996 _bfd_stringtab_free (finfo
.symstrtab
);
10997 if (finfo
.contents
!= NULL
)
10998 free (finfo
.contents
);
10999 if (finfo
.external_relocs
!= NULL
)
11000 free (finfo
.external_relocs
);
11001 if (finfo
.internal_relocs
!= NULL
)
11002 free (finfo
.internal_relocs
);
11003 if (finfo
.external_syms
!= NULL
)
11004 free (finfo
.external_syms
);
11005 if (finfo
.locsym_shndx
!= NULL
)
11006 free (finfo
.locsym_shndx
);
11007 if (finfo
.internal_syms
!= NULL
)
11008 free (finfo
.internal_syms
);
11009 if (finfo
.indices
!= NULL
)
11010 free (finfo
.indices
);
11011 if (finfo
.sections
!= NULL
)
11012 free (finfo
.sections
);
11013 if (finfo
.symbuf
!= NULL
)
11014 free (finfo
.symbuf
);
11015 if (finfo
.symshndxbuf
!= NULL
)
11016 free (finfo
.symshndxbuf
);
11017 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11019 if ((o
->flags
& SEC_RELOC
) != 0
11020 && elf_section_data (o
)->rel_hashes
!= NULL
)
11021 free (elf_section_data (o
)->rel_hashes
);
11024 elf_tdata (abfd
)->linker
= TRUE
;
11028 bfd_byte
*contents
= bfd_malloc (attr_size
);
11029 if (contents
== NULL
)
11030 return FALSE
; /* Bail out and fail. */
11031 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
11032 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
11039 if (finfo
.symstrtab
!= NULL
)
11040 _bfd_stringtab_free (finfo
.symstrtab
);
11041 if (finfo
.contents
!= NULL
)
11042 free (finfo
.contents
);
11043 if (finfo
.external_relocs
!= NULL
)
11044 free (finfo
.external_relocs
);
11045 if (finfo
.internal_relocs
!= NULL
)
11046 free (finfo
.internal_relocs
);
11047 if (finfo
.external_syms
!= NULL
)
11048 free (finfo
.external_syms
);
11049 if (finfo
.locsym_shndx
!= NULL
)
11050 free (finfo
.locsym_shndx
);
11051 if (finfo
.internal_syms
!= NULL
)
11052 free (finfo
.internal_syms
);
11053 if (finfo
.indices
!= NULL
)
11054 free (finfo
.indices
);
11055 if (finfo
.sections
!= NULL
)
11056 free (finfo
.sections
);
11057 if (finfo
.symbuf
!= NULL
)
11058 free (finfo
.symbuf
);
11059 if (finfo
.symshndxbuf
!= NULL
)
11060 free (finfo
.symshndxbuf
);
11061 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11063 if ((o
->flags
& SEC_RELOC
) != 0
11064 && elf_section_data (o
)->rel_hashes
!= NULL
)
11065 free (elf_section_data (o
)->rel_hashes
);
11071 /* Initialize COOKIE for input bfd ABFD. */
11074 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
11075 struct bfd_link_info
*info
, bfd
*abfd
)
11077 Elf_Internal_Shdr
*symtab_hdr
;
11078 const struct elf_backend_data
*bed
;
11080 bed
= get_elf_backend_data (abfd
);
11081 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11083 cookie
->abfd
= abfd
;
11084 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
11085 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
11086 if (cookie
->bad_symtab
)
11088 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
11089 cookie
->extsymoff
= 0;
11093 cookie
->locsymcount
= symtab_hdr
->sh_info
;
11094 cookie
->extsymoff
= symtab_hdr
->sh_info
;
11097 if (bed
->s
->arch_size
== 32)
11098 cookie
->r_sym_shift
= 8;
11100 cookie
->r_sym_shift
= 32;
11102 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
11103 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
11105 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
11106 cookie
->locsymcount
, 0,
11108 if (cookie
->locsyms
== NULL
)
11110 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
11113 if (info
->keep_memory
)
11114 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
11119 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
11122 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
11124 Elf_Internal_Shdr
*symtab_hdr
;
11126 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11127 if (cookie
->locsyms
!= NULL
11128 && symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
11129 free (cookie
->locsyms
);
11132 /* Initialize the relocation information in COOKIE for input section SEC
11133 of input bfd ABFD. */
11136 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
11137 struct bfd_link_info
*info
, bfd
*abfd
,
11140 const struct elf_backend_data
*bed
;
11142 if (sec
->reloc_count
== 0)
11144 cookie
->rels
= NULL
;
11145 cookie
->relend
= NULL
;
11149 bed
= get_elf_backend_data (abfd
);
11151 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
11152 info
->keep_memory
);
11153 if (cookie
->rels
== NULL
)
11155 cookie
->rel
= cookie
->rels
;
11156 cookie
->relend
= (cookie
->rels
11157 + sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
);
11159 cookie
->rel
= cookie
->rels
;
11163 /* Free the memory allocated by init_reloc_cookie_rels,
11167 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
11170 if (cookie
->rels
&& elf_section_data (sec
)->relocs
!= cookie
->rels
)
11171 free (cookie
->rels
);
11174 /* Initialize the whole of COOKIE for input section SEC. */
11177 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
11178 struct bfd_link_info
*info
,
11181 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
11183 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
11188 fini_reloc_cookie (cookie
, sec
->owner
);
11193 /* Free the memory allocated by init_reloc_cookie_for_section,
11197 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
11200 fini_reloc_cookie_rels (cookie
, sec
);
11201 fini_reloc_cookie (cookie
, sec
->owner
);
11204 /* Garbage collect unused sections. */
11206 /* Default gc_mark_hook. */
11209 _bfd_elf_gc_mark_hook (asection
*sec
,
11210 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
11211 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
11212 struct elf_link_hash_entry
*h
,
11213 Elf_Internal_Sym
*sym
)
11217 switch (h
->root
.type
)
11219 case bfd_link_hash_defined
:
11220 case bfd_link_hash_defweak
:
11221 return h
->root
.u
.def
.section
;
11223 case bfd_link_hash_common
:
11224 return h
->root
.u
.c
.p
->section
;
11231 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
11236 /* COOKIE->rel describes a relocation against section SEC, which is
11237 a section we've decided to keep. Return the section that contains
11238 the relocation symbol, or NULL if no section contains it. */
11241 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
11242 elf_gc_mark_hook_fn gc_mark_hook
,
11243 struct elf_reloc_cookie
*cookie
)
11245 unsigned long r_symndx
;
11246 struct elf_link_hash_entry
*h
;
11248 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
11252 if (r_symndx
>= cookie
->locsymcount
11253 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
11255 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
11256 while (h
->root
.type
== bfd_link_hash_indirect
11257 || h
->root
.type
== bfd_link_hash_warning
)
11258 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11259 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
11262 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
11263 &cookie
->locsyms
[r_symndx
]);
11266 /* COOKIE->rel describes a relocation against section SEC, which is
11267 a section we've decided to keep. Mark the section that contains
11268 the relocation symbol. */
11271 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
11273 elf_gc_mark_hook_fn gc_mark_hook
,
11274 struct elf_reloc_cookie
*cookie
)
11278 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
);
11279 if (rsec
&& !rsec
->gc_mark
)
11281 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
11283 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
11289 /* The mark phase of garbage collection. For a given section, mark
11290 it and any sections in this section's group, and all the sections
11291 which define symbols to which it refers. */
11294 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
11296 elf_gc_mark_hook_fn gc_mark_hook
)
11299 asection
*group_sec
, *eh_frame
;
11303 /* Mark all the sections in the group. */
11304 group_sec
= elf_section_data (sec
)->next_in_group
;
11305 if (group_sec
&& !group_sec
->gc_mark
)
11306 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
11309 /* Look through the section relocs. */
11311 eh_frame
= elf_eh_frame_section (sec
->owner
);
11312 if ((sec
->flags
& SEC_RELOC
) != 0
11313 && sec
->reloc_count
> 0
11314 && sec
!= eh_frame
)
11316 struct elf_reloc_cookie cookie
;
11318 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
11322 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
11323 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
11328 fini_reloc_cookie_for_section (&cookie
, sec
);
11332 if (ret
&& eh_frame
&& elf_fde_list (sec
))
11334 struct elf_reloc_cookie cookie
;
11336 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
11340 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
11341 gc_mark_hook
, &cookie
))
11343 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
11350 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
11352 struct elf_gc_sweep_symbol_info
11354 struct bfd_link_info
*info
;
11355 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
11360 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
11362 if (h
->root
.type
== bfd_link_hash_warning
)
11363 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11365 if ((h
->root
.type
== bfd_link_hash_defined
11366 || h
->root
.type
== bfd_link_hash_defweak
)
11367 && !h
->root
.u
.def
.section
->gc_mark
11368 && !(h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
))
11370 struct elf_gc_sweep_symbol_info
*inf
= data
;
11371 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
11377 /* The sweep phase of garbage collection. Remove all garbage sections. */
11379 typedef bfd_boolean (*gc_sweep_hook_fn
)
11380 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
11383 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
11386 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11387 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
11388 unsigned long section_sym_count
;
11389 struct elf_gc_sweep_symbol_info sweep_info
;
11391 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
11395 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
11398 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
11400 /* Keep debug and special sections. */
11401 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
11402 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
11408 /* Skip sweeping sections already excluded. */
11409 if (o
->flags
& SEC_EXCLUDE
)
11412 /* Since this is early in the link process, it is simple
11413 to remove a section from the output. */
11414 o
->flags
|= SEC_EXCLUDE
;
11416 if (info
->print_gc_sections
&& o
->size
!= 0)
11417 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub
, o
->name
);
11419 /* But we also have to update some of the relocation
11420 info we collected before. */
11422 && (o
->flags
& SEC_RELOC
) != 0
11423 && o
->reloc_count
> 0
11424 && !bfd_is_abs_section (o
->output_section
))
11426 Elf_Internal_Rela
*internal_relocs
;
11430 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
11431 info
->keep_memory
);
11432 if (internal_relocs
== NULL
)
11435 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
11437 if (elf_section_data (o
)->relocs
!= internal_relocs
)
11438 free (internal_relocs
);
11446 /* Remove the symbols that were in the swept sections from the dynamic
11447 symbol table. GCFIXME: Anyone know how to get them out of the
11448 static symbol table as well? */
11449 sweep_info
.info
= info
;
11450 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
11451 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
11454 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
11458 /* Propagate collected vtable information. This is called through
11459 elf_link_hash_traverse. */
11462 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
11464 if (h
->root
.type
== bfd_link_hash_warning
)
11465 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11467 /* Those that are not vtables. */
11468 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
11471 /* Those vtables that do not have parents, we cannot merge. */
11472 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
11475 /* If we've already been done, exit. */
11476 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
11479 /* Make sure the parent's table is up to date. */
11480 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
11482 if (h
->vtable
->used
== NULL
)
11484 /* None of this table's entries were referenced. Re-use the
11486 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
11487 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
11492 bfd_boolean
*cu
, *pu
;
11494 /* Or the parent's entries into ours. */
11495 cu
= h
->vtable
->used
;
11497 pu
= h
->vtable
->parent
->vtable
->used
;
11500 const struct elf_backend_data
*bed
;
11501 unsigned int log_file_align
;
11503 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
11504 log_file_align
= bed
->s
->log_file_align
;
11505 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
11520 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
11523 bfd_vma hstart
, hend
;
11524 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
11525 const struct elf_backend_data
*bed
;
11526 unsigned int log_file_align
;
11528 if (h
->root
.type
== bfd_link_hash_warning
)
11529 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11531 /* Take care of both those symbols that do not describe vtables as
11532 well as those that are not loaded. */
11533 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
11536 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
11537 || h
->root
.type
== bfd_link_hash_defweak
);
11539 sec
= h
->root
.u
.def
.section
;
11540 hstart
= h
->root
.u
.def
.value
;
11541 hend
= hstart
+ h
->size
;
11543 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
11545 return *(bfd_boolean
*) okp
= FALSE
;
11546 bed
= get_elf_backend_data (sec
->owner
);
11547 log_file_align
= bed
->s
->log_file_align
;
11549 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
11551 for (rel
= relstart
; rel
< relend
; ++rel
)
11552 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
11554 /* If the entry is in use, do nothing. */
11555 if (h
->vtable
->used
11556 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
11558 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
11559 if (h
->vtable
->used
[entry
])
11562 /* Otherwise, kill it. */
11563 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
11569 /* Mark sections containing dynamically referenced symbols. When
11570 building shared libraries, we must assume that any visible symbol is
11574 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
11576 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
11578 if (h
->root
.type
== bfd_link_hash_warning
)
11579 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11581 if ((h
->root
.type
== bfd_link_hash_defined
11582 || h
->root
.type
== bfd_link_hash_defweak
)
11584 || (!info
->executable
11586 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
11587 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
)))
11588 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
11593 /* Keep all sections containing symbols undefined on the command-line,
11594 and the section containing the entry symbol. */
11597 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
11599 struct bfd_sym_chain
*sym
;
11601 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
11603 struct elf_link_hash_entry
*h
;
11605 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
11606 FALSE
, FALSE
, FALSE
);
11609 && (h
->root
.type
== bfd_link_hash_defined
11610 || h
->root
.type
== bfd_link_hash_defweak
)
11611 && !bfd_is_abs_section (h
->root
.u
.def
.section
))
11612 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
11616 /* Do mark and sweep of unused sections. */
11619 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
11621 bfd_boolean ok
= TRUE
;
11623 elf_gc_mark_hook_fn gc_mark_hook
;
11624 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11626 if (!bed
->can_gc_sections
11627 || !is_elf_hash_table (info
->hash
))
11629 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
11633 bed
->gc_keep (info
);
11635 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
11636 at the .eh_frame section if we can mark the FDEs individually. */
11637 _bfd_elf_begin_eh_frame_parsing (info
);
11638 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
11641 struct elf_reloc_cookie cookie
;
11643 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
11644 if (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
11646 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
11647 if (elf_section_data (sec
)->sec_info
)
11648 elf_eh_frame_section (sub
) = sec
;
11649 fini_reloc_cookie_for_section (&cookie
, sec
);
11652 _bfd_elf_end_eh_frame_parsing (info
);
11654 /* Apply transitive closure to the vtable entry usage info. */
11655 elf_link_hash_traverse (elf_hash_table (info
),
11656 elf_gc_propagate_vtable_entries_used
,
11661 /* Kill the vtable relocations that were not used. */
11662 elf_link_hash_traverse (elf_hash_table (info
),
11663 elf_gc_smash_unused_vtentry_relocs
,
11668 /* Mark dynamically referenced symbols. */
11669 if (elf_hash_table (info
)->dynamic_sections_created
)
11670 elf_link_hash_traverse (elf_hash_table (info
),
11671 bed
->gc_mark_dynamic_ref
,
11674 /* Grovel through relocs to find out who stays ... */
11675 gc_mark_hook
= bed
->gc_mark_hook
;
11676 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
11680 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
11683 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
11684 if ((o
->flags
& (SEC_EXCLUDE
| SEC_KEEP
)) == SEC_KEEP
&& !o
->gc_mark
)
11685 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
11689 /* Allow the backend to mark additional target specific sections. */
11690 if (bed
->gc_mark_extra_sections
)
11691 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
11693 /* ... and mark SEC_EXCLUDE for those that go. */
11694 return elf_gc_sweep (abfd
, info
);
11697 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
11700 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
11702 struct elf_link_hash_entry
*h
,
11705 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
11706 struct elf_link_hash_entry
**search
, *child
;
11707 bfd_size_type extsymcount
;
11708 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11710 /* The sh_info field of the symtab header tells us where the
11711 external symbols start. We don't care about the local symbols at
11713 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
11714 if (!elf_bad_symtab (abfd
))
11715 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
11717 sym_hashes
= elf_sym_hashes (abfd
);
11718 sym_hashes_end
= sym_hashes
+ extsymcount
;
11720 /* Hunt down the child symbol, which is in this section at the same
11721 offset as the relocation. */
11722 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
11724 if ((child
= *search
) != NULL
11725 && (child
->root
.type
== bfd_link_hash_defined
11726 || child
->root
.type
== bfd_link_hash_defweak
)
11727 && child
->root
.u
.def
.section
== sec
11728 && child
->root
.u
.def
.value
== offset
)
11732 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
11733 abfd
, sec
, (unsigned long) offset
);
11734 bfd_set_error (bfd_error_invalid_operation
);
11738 if (!child
->vtable
)
11740 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
11741 if (!child
->vtable
)
11746 /* This *should* only be the absolute section. It could potentially
11747 be that someone has defined a non-global vtable though, which
11748 would be bad. It isn't worth paging in the local symbols to be
11749 sure though; that case should simply be handled by the assembler. */
11751 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
11754 child
->vtable
->parent
= h
;
11759 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
11762 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
11763 asection
*sec ATTRIBUTE_UNUSED
,
11764 struct elf_link_hash_entry
*h
,
11767 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11768 unsigned int log_file_align
= bed
->s
->log_file_align
;
11772 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
11777 if (addend
>= h
->vtable
->size
)
11779 size_t size
, bytes
, file_align
;
11780 bfd_boolean
*ptr
= h
->vtable
->used
;
11782 /* While the symbol is undefined, we have to be prepared to handle
11784 file_align
= 1 << log_file_align
;
11785 if (h
->root
.type
== bfd_link_hash_undefined
)
11786 size
= addend
+ file_align
;
11790 if (addend
>= size
)
11792 /* Oops! We've got a reference past the defined end of
11793 the table. This is probably a bug -- shall we warn? */
11794 size
= addend
+ file_align
;
11797 size
= (size
+ file_align
- 1) & -file_align
;
11799 /* Allocate one extra entry for use as a "done" flag for the
11800 consolidation pass. */
11801 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
11805 ptr
= bfd_realloc (ptr
- 1, bytes
);
11811 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
11812 * sizeof (bfd_boolean
));
11813 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
11817 ptr
= bfd_zmalloc (bytes
);
11822 /* And arrange for that done flag to be at index -1. */
11823 h
->vtable
->used
= ptr
+ 1;
11824 h
->vtable
->size
= size
;
11827 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
11832 struct alloc_got_off_arg
{
11834 struct bfd_link_info
*info
;
11837 /* We need a special top-level link routine to convert got reference counts
11838 to real got offsets. */
11841 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
11843 struct alloc_got_off_arg
*gofarg
= arg
;
11844 bfd
*obfd
= gofarg
->info
->output_bfd
;
11845 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
11847 if (h
->root
.type
== bfd_link_hash_warning
)
11848 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11850 if (h
->got
.refcount
> 0)
11852 h
->got
.offset
= gofarg
->gotoff
;
11853 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
11856 h
->got
.offset
= (bfd_vma
) -1;
11861 /* And an accompanying bit to work out final got entry offsets once
11862 we're done. Should be called from final_link. */
11865 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
11866 struct bfd_link_info
*info
)
11869 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11871 struct alloc_got_off_arg gofarg
;
11873 BFD_ASSERT (abfd
== info
->output_bfd
);
11875 if (! is_elf_hash_table (info
->hash
))
11878 /* The GOT offset is relative to the .got section, but the GOT header is
11879 put into the .got.plt section, if the backend uses it. */
11880 if (bed
->want_got_plt
)
11883 gotoff
= bed
->got_header_size
;
11885 /* Do the local .got entries first. */
11886 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
11888 bfd_signed_vma
*local_got
;
11889 bfd_size_type j
, locsymcount
;
11890 Elf_Internal_Shdr
*symtab_hdr
;
11892 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
11895 local_got
= elf_local_got_refcounts (i
);
11899 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
11900 if (elf_bad_symtab (i
))
11901 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
11903 locsymcount
= symtab_hdr
->sh_info
;
11905 for (j
= 0; j
< locsymcount
; ++j
)
11907 if (local_got
[j
] > 0)
11909 local_got
[j
] = gotoff
;
11910 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
11913 local_got
[j
] = (bfd_vma
) -1;
11917 /* Then the global .got entries. .plt refcounts are handled by
11918 adjust_dynamic_symbol */
11919 gofarg
.gotoff
= gotoff
;
11920 gofarg
.info
= info
;
11921 elf_link_hash_traverse (elf_hash_table (info
),
11922 elf_gc_allocate_got_offsets
,
11927 /* Many folk need no more in the way of final link than this, once
11928 got entry reference counting is enabled. */
11931 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
11933 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
11936 /* Invoke the regular ELF backend linker to do all the work. */
11937 return bfd_elf_final_link (abfd
, info
);
11941 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
11943 struct elf_reloc_cookie
*rcookie
= cookie
;
11945 if (rcookie
->bad_symtab
)
11946 rcookie
->rel
= rcookie
->rels
;
11948 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
11950 unsigned long r_symndx
;
11952 if (! rcookie
->bad_symtab
)
11953 if (rcookie
->rel
->r_offset
> offset
)
11955 if (rcookie
->rel
->r_offset
!= offset
)
11958 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
11959 if (r_symndx
== SHN_UNDEF
)
11962 if (r_symndx
>= rcookie
->locsymcount
11963 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
11965 struct elf_link_hash_entry
*h
;
11967 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
11969 while (h
->root
.type
== bfd_link_hash_indirect
11970 || h
->root
.type
== bfd_link_hash_warning
)
11971 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11973 if ((h
->root
.type
== bfd_link_hash_defined
11974 || h
->root
.type
== bfd_link_hash_defweak
)
11975 && elf_discarded_section (h
->root
.u
.def
.section
))
11982 /* It's not a relocation against a global symbol,
11983 but it could be a relocation against a local
11984 symbol for a discarded section. */
11986 Elf_Internal_Sym
*isym
;
11988 /* Need to: get the symbol; get the section. */
11989 isym
= &rcookie
->locsyms
[r_symndx
];
11990 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
11991 if (isec
!= NULL
&& elf_discarded_section (isec
))
11999 /* Discard unneeded references to discarded sections.
12000 Returns TRUE if any section's size was changed. */
12001 /* This function assumes that the relocations are in sorted order,
12002 which is true for all known assemblers. */
12005 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
12007 struct elf_reloc_cookie cookie
;
12008 asection
*stab
, *eh
;
12009 const struct elf_backend_data
*bed
;
12011 bfd_boolean ret
= FALSE
;
12013 if (info
->traditional_format
12014 || !is_elf_hash_table (info
->hash
))
12017 _bfd_elf_begin_eh_frame_parsing (info
);
12018 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
12020 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
12023 bed
= get_elf_backend_data (abfd
);
12025 if ((abfd
->flags
& DYNAMIC
) != 0)
12029 if (!info
->relocatable
)
12031 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
12034 || bfd_is_abs_section (eh
->output_section
)))
12038 stab
= bfd_get_section_by_name (abfd
, ".stab");
12040 && (stab
->size
== 0
12041 || bfd_is_abs_section (stab
->output_section
)
12042 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
12047 && bed
->elf_backend_discard_info
== NULL
)
12050 if (!init_reloc_cookie (&cookie
, info
, abfd
))
12054 && stab
->reloc_count
> 0
12055 && init_reloc_cookie_rels (&cookie
, info
, abfd
, stab
))
12057 if (_bfd_discard_section_stabs (abfd
, stab
,
12058 elf_section_data (stab
)->sec_info
,
12059 bfd_elf_reloc_symbol_deleted_p
,
12062 fini_reloc_cookie_rels (&cookie
, stab
);
12066 && init_reloc_cookie_rels (&cookie
, info
, abfd
, eh
))
12068 _bfd_elf_parse_eh_frame (abfd
, info
, eh
, &cookie
);
12069 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
12070 bfd_elf_reloc_symbol_deleted_p
,
12073 fini_reloc_cookie_rels (&cookie
, eh
);
12076 if (bed
->elf_backend_discard_info
!= NULL
12077 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
12080 fini_reloc_cookie (&cookie
, abfd
);
12082 _bfd_elf_end_eh_frame_parsing (info
);
12084 if (info
->eh_frame_hdr
12085 && !info
->relocatable
12086 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
12092 /* For a SHT_GROUP section, return the group signature. For other
12093 sections, return the normal section name. */
12095 static const char *
12096 section_signature (asection
*sec
)
12098 if ((sec
->flags
& SEC_GROUP
) != 0
12099 && elf_next_in_group (sec
) != NULL
12100 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
12101 return elf_group_name (elf_next_in_group (sec
));
12106 _bfd_elf_section_already_linked (bfd
*abfd
, asection
*sec
,
12107 struct bfd_link_info
*info
)
12110 const char *name
, *p
;
12111 struct bfd_section_already_linked
*l
;
12112 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
12114 if (sec
->output_section
== bfd_abs_section_ptr
)
12117 flags
= sec
->flags
;
12119 /* Return if it isn't a linkonce section. A comdat group section
12120 also has SEC_LINK_ONCE set. */
12121 if ((flags
& SEC_LINK_ONCE
) == 0)
12124 /* Don't put group member sections on our list of already linked
12125 sections. They are handled as a group via their group section. */
12126 if (elf_sec_group (sec
) != NULL
)
12129 /* FIXME: When doing a relocatable link, we may have trouble
12130 copying relocations in other sections that refer to local symbols
12131 in the section being discarded. Those relocations will have to
12132 be converted somehow; as of this writing I'm not sure that any of
12133 the backends handle that correctly.
12135 It is tempting to instead not discard link once sections when
12136 doing a relocatable link (technically, they should be discarded
12137 whenever we are building constructors). However, that fails,
12138 because the linker winds up combining all the link once sections
12139 into a single large link once section, which defeats the purpose
12140 of having link once sections in the first place.
12142 Also, not merging link once sections in a relocatable link
12143 causes trouble for MIPS ELF, which relies on link once semantics
12144 to handle the .reginfo section correctly. */
12146 name
= section_signature (sec
);
12148 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
12149 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
12154 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
12156 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
12158 /* We may have 2 different types of sections on the list: group
12159 sections and linkonce sections. Match like sections. */
12160 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
12161 && strcmp (name
, section_signature (l
->sec
)) == 0
12162 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
12164 /* The section has already been linked. See if we should
12165 issue a warning. */
12166 switch (flags
& SEC_LINK_DUPLICATES
)
12171 case SEC_LINK_DUPLICATES_DISCARD
:
12174 case SEC_LINK_DUPLICATES_ONE_ONLY
:
12175 (*_bfd_error_handler
)
12176 (_("%B: ignoring duplicate section `%A'"),
12180 case SEC_LINK_DUPLICATES_SAME_SIZE
:
12181 if (sec
->size
!= l
->sec
->size
)
12182 (*_bfd_error_handler
)
12183 (_("%B: duplicate section `%A' has different size"),
12187 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
12188 if (sec
->size
!= l
->sec
->size
)
12189 (*_bfd_error_handler
)
12190 (_("%B: duplicate section `%A' has different size"),
12192 else if (sec
->size
!= 0)
12194 bfd_byte
*sec_contents
, *l_sec_contents
;
12196 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
12197 (*_bfd_error_handler
)
12198 (_("%B: warning: could not read contents of section `%A'"),
12200 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
12202 (*_bfd_error_handler
)
12203 (_("%B: warning: could not read contents of section `%A'"),
12204 l
->sec
->owner
, l
->sec
);
12205 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
12206 (*_bfd_error_handler
)
12207 (_("%B: warning: duplicate section `%A' has different contents"),
12211 free (sec_contents
);
12212 if (l_sec_contents
)
12213 free (l_sec_contents
);
12218 /* Set the output_section field so that lang_add_section
12219 does not create a lang_input_section structure for this
12220 section. Since there might be a symbol in the section
12221 being discarded, we must retain a pointer to the section
12222 which we are really going to use. */
12223 sec
->output_section
= bfd_abs_section_ptr
;
12224 sec
->kept_section
= l
->sec
;
12226 if (flags
& SEC_GROUP
)
12228 asection
*first
= elf_next_in_group (sec
);
12229 asection
*s
= first
;
12233 s
->output_section
= bfd_abs_section_ptr
;
12234 /* Record which group discards it. */
12235 s
->kept_section
= l
->sec
;
12236 s
= elf_next_in_group (s
);
12237 /* These lists are circular. */
12247 /* A single member comdat group section may be discarded by a
12248 linkonce section and vice versa. */
12250 if ((flags
& SEC_GROUP
) != 0)
12252 asection
*first
= elf_next_in_group (sec
);
12254 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
12255 /* Check this single member group against linkonce sections. */
12256 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
12257 if ((l
->sec
->flags
& SEC_GROUP
) == 0
12258 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
12259 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
12261 first
->output_section
= bfd_abs_section_ptr
;
12262 first
->kept_section
= l
->sec
;
12263 sec
->output_section
= bfd_abs_section_ptr
;
12268 /* Check this linkonce section against single member groups. */
12269 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
12270 if (l
->sec
->flags
& SEC_GROUP
)
12272 asection
*first
= elf_next_in_group (l
->sec
);
12275 && elf_next_in_group (first
) == first
12276 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
12278 sec
->output_section
= bfd_abs_section_ptr
;
12279 sec
->kept_section
= first
;
12284 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
12285 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
12286 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
12287 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
12288 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
12289 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
12290 `.gnu.linkonce.t.F' section from a different bfd not requiring any
12291 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
12292 The reverse order cannot happen as there is never a bfd with only the
12293 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
12294 matter as here were are looking only for cross-bfd sections. */
12296 if ((flags
& SEC_GROUP
) == 0 && CONST_STRNEQ (name
, ".gnu.linkonce.r."))
12297 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
12298 if ((l
->sec
->flags
& SEC_GROUP
) == 0
12299 && CONST_STRNEQ (l
->sec
->name
, ".gnu.linkonce.t."))
12301 if (abfd
!= l
->sec
->owner
)
12302 sec
->output_section
= bfd_abs_section_ptr
;
12306 /* This is the first section with this name. Record it. */
12307 if (! bfd_section_already_linked_table_insert (already_linked_list
, sec
))
12308 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E"));
12312 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
12314 return sym
->st_shndx
== SHN_COMMON
;
12318 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
12324 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
12326 return bfd_com_section_ptr
;
12330 _bfd_elf_default_got_elt_size (bfd
*abfd
,
12331 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12332 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
12333 bfd
*ibfd ATTRIBUTE_UNUSED
,
12334 unsigned long symndx ATTRIBUTE_UNUSED
)
12336 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12337 return bed
->s
->arch_size
/ 8;
12340 /* Routines to support the creation of dynamic relocs. */
12342 /* Return true if NAME is a name of a relocation
12343 section associated with section S. */
12346 is_reloc_section (bfd_boolean rela
, const char * name
, asection
* s
)
12349 return CONST_STRNEQ (name
, ".rela")
12350 && strcmp (bfd_get_section_name (NULL
, s
), name
+ 5) == 0;
12352 return CONST_STRNEQ (name
, ".rel")
12353 && strcmp (bfd_get_section_name (NULL
, s
), name
+ 4) == 0;
12356 /* Returns the name of the dynamic reloc section associated with SEC. */
12358 static const char *
12359 get_dynamic_reloc_section_name (bfd
* abfd
,
12361 bfd_boolean is_rela
)
12364 unsigned int strndx
= elf_elfheader (abfd
)->e_shstrndx
;
12365 unsigned int shnam
= elf_section_data (sec
)->rel_hdr
.sh_name
;
12367 name
= bfd_elf_string_from_elf_section (abfd
, strndx
, shnam
);
12371 if (! is_reloc_section (is_rela
, name
, sec
))
12373 static bfd_boolean complained
= FALSE
;
12377 (*_bfd_error_handler
)
12378 (_("%B: bad relocation section name `%s\'"), abfd
, name
);
12387 /* Returns the dynamic reloc section associated with SEC.
12388 If necessary compute the name of the dynamic reloc section based
12389 on SEC's name (looked up in ABFD's string table) and the setting
12393 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
12395 bfd_boolean is_rela
)
12397 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
12399 if (reloc_sec
== NULL
)
12401 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
12405 reloc_sec
= bfd_get_section_by_name (abfd
, name
);
12407 if (reloc_sec
!= NULL
)
12408 elf_section_data (sec
)->sreloc
= reloc_sec
;
12415 /* Returns the dynamic reloc section associated with SEC. If the
12416 section does not exist it is created and attached to the DYNOBJ
12417 bfd and stored in the SRELOC field of SEC's elf_section_data
12420 ALIGNMENT is the alignment for the newly created section and
12421 IS_RELA defines whether the name should be .rela.<SEC's name>
12422 or .rel.<SEC's name>. The section name is looked up in the
12423 string table associated with ABFD. */
12426 _bfd_elf_make_dynamic_reloc_section (asection
* sec
,
12428 unsigned int alignment
,
12430 bfd_boolean is_rela
)
12432 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
12434 if (reloc_sec
== NULL
)
12436 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
12441 reloc_sec
= bfd_get_section_by_name (dynobj
, name
);
12443 if (reloc_sec
== NULL
)
12447 flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
12448 if ((sec
->flags
& SEC_ALLOC
) != 0)
12449 flags
|= SEC_ALLOC
| SEC_LOAD
;
12451 reloc_sec
= bfd_make_section_with_flags (dynobj
, name
, flags
);
12452 if (reloc_sec
!= NULL
)
12454 if (! bfd_set_section_alignment (dynobj
, reloc_sec
, alignment
))
12459 elf_section_data (sec
)->sreloc
= reloc_sec
;