1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
28 #include "safe-ctype.h"
29 #include "libiberty.h"
32 /* Define a symbol in a dynamic linkage section. */
34 struct elf_link_hash_entry
*
35 _bfd_elf_define_linkage_sym (bfd
*abfd
,
36 struct bfd_link_info
*info
,
40 struct elf_link_hash_entry
*h
;
41 struct bfd_link_hash_entry
*bh
;
42 const struct elf_backend_data
*bed
;
44 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
47 /* Zap symbol defined in an as-needed lib that wasn't linked.
48 This is a symptom of a larger problem: Absolute symbols
49 defined in shared libraries can't be overridden, because we
50 lose the link to the bfd which is via the symbol section. */
51 h
->root
.type
= bfd_link_hash_new
;
55 if (!_bfd_generic_link_add_one_symbol (info
, abfd
, name
, BSF_GLOBAL
,
57 get_elf_backend_data (abfd
)->collect
,
60 h
= (struct elf_link_hash_entry
*) bh
;
63 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
65 bed
= get_elf_backend_data (abfd
);
66 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
71 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
75 struct elf_link_hash_entry
*h
;
76 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
79 /* This function may be called more than once. */
80 s
= bfd_get_section_by_name (abfd
, ".got");
81 if (s
!= NULL
&& (s
->flags
& SEC_LINKER_CREATED
) != 0)
84 switch (bed
->s
->arch_size
)
95 bfd_set_error (bfd_error_bad_value
);
99 flags
= bed
->dynamic_sec_flags
;
101 s
= bfd_make_section_with_flags (abfd
, ".got", flags
);
103 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
106 if (bed
->want_got_plt
)
108 s
= bfd_make_section_with_flags (abfd
, ".got.plt", flags
);
110 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
114 if (bed
->want_got_sym
)
116 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
117 (or .got.plt) section. We don't do this in the linker script
118 because we don't want to define the symbol if we are not creating
119 a global offset table. */
120 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_GLOBAL_OFFSET_TABLE_");
121 elf_hash_table (info
)->hgot
= h
;
126 /* The first bit of the global offset table is the header. */
127 s
->size
+= bed
->got_header_size
;
132 /* Create a strtab to hold the dynamic symbol names. */
134 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
136 struct elf_link_hash_table
*hash_table
;
138 hash_table
= elf_hash_table (info
);
139 if (hash_table
->dynobj
== NULL
)
140 hash_table
->dynobj
= abfd
;
142 if (hash_table
->dynstr
== NULL
)
144 hash_table
->dynstr
= _bfd_elf_strtab_init ();
145 if (hash_table
->dynstr
== NULL
)
151 /* Create some sections which will be filled in with dynamic linking
152 information. ABFD is an input file which requires dynamic sections
153 to be created. The dynamic sections take up virtual memory space
154 when the final executable is run, so we need to create them before
155 addresses are assigned to the output sections. We work out the
156 actual contents and size of these sections later. */
159 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
162 register asection
*s
;
163 const struct elf_backend_data
*bed
;
165 if (! is_elf_hash_table (info
->hash
))
168 if (elf_hash_table (info
)->dynamic_sections_created
)
171 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
174 abfd
= elf_hash_table (info
)->dynobj
;
175 bed
= get_elf_backend_data (abfd
);
177 flags
= bed
->dynamic_sec_flags
;
179 /* A dynamically linked executable has a .interp section, but a
180 shared library does not. */
181 if (info
->executable
)
183 s
= bfd_make_section_with_flags (abfd
, ".interp",
184 flags
| SEC_READONLY
);
189 /* Create sections to hold version informations. These are removed
190 if they are not needed. */
191 s
= bfd_make_section_with_flags (abfd
, ".gnu.version_d",
192 flags
| SEC_READONLY
);
194 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
197 s
= bfd_make_section_with_flags (abfd
, ".gnu.version",
198 flags
| SEC_READONLY
);
200 || ! bfd_set_section_alignment (abfd
, s
, 1))
203 s
= bfd_make_section_with_flags (abfd
, ".gnu.version_r",
204 flags
| SEC_READONLY
);
206 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
209 s
= bfd_make_section_with_flags (abfd
, ".dynsym",
210 flags
| SEC_READONLY
);
212 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
215 s
= bfd_make_section_with_flags (abfd
, ".dynstr",
216 flags
| SEC_READONLY
);
220 s
= bfd_make_section_with_flags (abfd
, ".dynamic", flags
);
222 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
225 /* The special symbol _DYNAMIC is always set to the start of the
226 .dynamic section. We could set _DYNAMIC in a linker script, but we
227 only want to define it if we are, in fact, creating a .dynamic
228 section. We don't want to define it if there is no .dynamic
229 section, since on some ELF platforms the start up code examines it
230 to decide how to initialize the process. */
231 if (!_bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC"))
236 s
= bfd_make_section_with_flags (abfd
, ".hash", flags
| SEC_READONLY
);
238 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
240 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
243 if (info
->emit_gnu_hash
)
245 s
= bfd_make_section_with_flags (abfd
, ".gnu.hash",
246 flags
| SEC_READONLY
);
248 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
250 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
251 4 32-bit words followed by variable count of 64-bit words, then
252 variable count of 32-bit words. */
253 if (bed
->s
->arch_size
== 64)
254 elf_section_data (s
)->this_hdr
.sh_entsize
= 0;
256 elf_section_data (s
)->this_hdr
.sh_entsize
= 4;
259 /* Let the backend create the rest of the sections. This lets the
260 backend set the right flags. The backend will normally create
261 the .got and .plt sections. */
262 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
265 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
270 /* Create dynamic sections when linking against a dynamic object. */
273 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
275 flagword flags
, pltflags
;
276 struct elf_link_hash_entry
*h
;
278 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
280 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
281 .rel[a].bss sections. */
282 flags
= bed
->dynamic_sec_flags
;
285 if (bed
->plt_not_loaded
)
286 /* We do not clear SEC_ALLOC here because we still want the OS to
287 allocate space for the section; it's just that there's nothing
288 to read in from the object file. */
289 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
291 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
292 if (bed
->plt_readonly
)
293 pltflags
|= SEC_READONLY
;
295 s
= bfd_make_section_with_flags (abfd
, ".plt", pltflags
);
297 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
300 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
302 if (bed
->want_plt_sym
)
304 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
305 "_PROCEDURE_LINKAGE_TABLE_");
306 elf_hash_table (info
)->hplt
= h
;
311 s
= bfd_make_section_with_flags (abfd
,
312 (bed
->rela_plts_and_copies_p
313 ? ".rela.plt" : ".rel.plt"),
314 flags
| SEC_READONLY
);
316 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
319 if (! _bfd_elf_create_got_section (abfd
, info
))
322 if (bed
->want_dynbss
)
324 /* The .dynbss section is a place to put symbols which are defined
325 by dynamic objects, are referenced by regular objects, and are
326 not functions. We must allocate space for them in the process
327 image and use a R_*_COPY reloc to tell the dynamic linker to
328 initialize them at run time. The linker script puts the .dynbss
329 section into the .bss section of the final image. */
330 s
= bfd_make_section_with_flags (abfd
, ".dynbss",
332 | SEC_LINKER_CREATED
));
336 /* The .rel[a].bss section holds copy relocs. This section is not
337 normally needed. We need to create it here, though, so that the
338 linker will map it to an output section. We can't just create it
339 only if we need it, because we will not know whether we need it
340 until we have seen all the input files, and the first time the
341 main linker code calls BFD after examining all the input files
342 (size_dynamic_sections) the input sections have already been
343 mapped to the output sections. If the section turns out not to
344 be needed, we can discard it later. We will never need this
345 section when generating a shared object, since they do not use
349 s
= bfd_make_section_with_flags (abfd
,
350 (bed
->rela_plts_and_copies_p
351 ? ".rela.bss" : ".rel.bss"),
352 flags
| SEC_READONLY
);
354 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
362 /* Record a new dynamic symbol. We record the dynamic symbols as we
363 read the input files, since we need to have a list of all of them
364 before we can determine the final sizes of the output sections.
365 Note that we may actually call this function even though we are not
366 going to output any dynamic symbols; in some cases we know that a
367 symbol should be in the dynamic symbol table, but only if there is
371 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
372 struct elf_link_hash_entry
*h
)
374 if (h
->dynindx
== -1)
376 struct elf_strtab_hash
*dynstr
;
381 /* XXX: The ABI draft says the linker must turn hidden and
382 internal symbols into STB_LOCAL symbols when producing the
383 DSO. However, if ld.so honors st_other in the dynamic table,
384 this would not be necessary. */
385 switch (ELF_ST_VISIBILITY (h
->other
))
389 if (h
->root
.type
!= bfd_link_hash_undefined
390 && h
->root
.type
!= bfd_link_hash_undefweak
)
393 if (!elf_hash_table (info
)->is_relocatable_executable
)
401 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
402 ++elf_hash_table (info
)->dynsymcount
;
404 dynstr
= elf_hash_table (info
)->dynstr
;
407 /* Create a strtab to hold the dynamic symbol names. */
408 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
413 /* We don't put any version information in the dynamic string
415 name
= h
->root
.root
.string
;
416 p
= strchr (name
, ELF_VER_CHR
);
418 /* We know that the p points into writable memory. In fact,
419 there are only a few symbols that have read-only names, being
420 those like _GLOBAL_OFFSET_TABLE_ that are created specially
421 by the backends. Most symbols will have names pointing into
422 an ELF string table read from a file, or to objalloc memory. */
425 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
430 if (indx
== (bfd_size_type
) -1)
432 h
->dynstr_index
= indx
;
438 /* Mark a symbol dynamic. */
441 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
442 struct elf_link_hash_entry
*h
,
443 Elf_Internal_Sym
*sym
)
445 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
447 /* It may be called more than once on the same H. */
448 if(h
->dynamic
|| info
->relocatable
)
451 if ((info
->dynamic_data
452 && (h
->type
== STT_OBJECT
454 && ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
)))
456 && h
->root
.type
== bfd_link_hash_new
457 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
461 /* Record an assignment to a symbol made by a linker script. We need
462 this in case some dynamic object refers to this symbol. */
465 bfd_elf_record_link_assignment (bfd
*output_bfd
,
466 struct bfd_link_info
*info
,
471 struct elf_link_hash_entry
*h
, *hv
;
472 struct elf_link_hash_table
*htab
;
473 const struct elf_backend_data
*bed
;
475 if (!is_elf_hash_table (info
->hash
))
478 htab
= elf_hash_table (info
);
479 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
483 switch (h
->root
.type
)
485 case bfd_link_hash_defined
:
486 case bfd_link_hash_defweak
:
487 case bfd_link_hash_common
:
489 case bfd_link_hash_undefweak
:
490 case bfd_link_hash_undefined
:
491 /* Since we're defining the symbol, don't let it seem to have not
492 been defined. record_dynamic_symbol and size_dynamic_sections
493 may depend on this. */
494 h
->root
.type
= bfd_link_hash_new
;
495 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
496 bfd_link_repair_undef_list (&htab
->root
);
498 case bfd_link_hash_new
:
499 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
502 case bfd_link_hash_indirect
:
503 /* We had a versioned symbol in a dynamic library. We make the
504 the versioned symbol point to this one. */
505 bed
= get_elf_backend_data (output_bfd
);
507 while (hv
->root
.type
== bfd_link_hash_indirect
508 || hv
->root
.type
== bfd_link_hash_warning
)
509 hv
= (struct elf_link_hash_entry
*) hv
->root
.u
.i
.link
;
510 /* We don't need to update h->root.u since linker will set them
512 h
->root
.type
= bfd_link_hash_undefined
;
513 hv
->root
.type
= bfd_link_hash_indirect
;
514 hv
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
515 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hv
);
517 case bfd_link_hash_warning
:
522 /* If this symbol is being provided by the linker script, and it is
523 currently defined by a dynamic object, but not by a regular
524 object, then mark it as undefined so that the generic linker will
525 force the correct value. */
529 h
->root
.type
= bfd_link_hash_undefined
;
531 /* If this symbol is not being provided by the linker script, and it is
532 currently defined by a dynamic object, but not by a regular object,
533 then clear out any version information because the symbol will not be
534 associated with the dynamic object any more. */
538 h
->verinfo
.verdef
= NULL
;
542 if (provide
&& hidden
)
544 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
546 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
547 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
550 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
552 if (!info
->relocatable
554 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
555 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
561 || (info
->executable
&& elf_hash_table (info
)->is_relocatable_executable
))
564 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
567 /* If this is a weak defined symbol, and we know a corresponding
568 real symbol from the same dynamic object, make sure the real
569 symbol is also made into a dynamic symbol. */
570 if (h
->u
.weakdef
!= NULL
571 && h
->u
.weakdef
->dynindx
== -1)
573 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
581 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
582 success, and 2 on a failure caused by attempting to record a symbol
583 in a discarded section, eg. a discarded link-once section symbol. */
586 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
591 struct elf_link_local_dynamic_entry
*entry
;
592 struct elf_link_hash_table
*eht
;
593 struct elf_strtab_hash
*dynstr
;
594 unsigned long dynstr_index
;
596 Elf_External_Sym_Shndx eshndx
;
597 char esym
[sizeof (Elf64_External_Sym
)];
599 if (! is_elf_hash_table (info
->hash
))
602 /* See if the entry exists already. */
603 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
604 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
607 amt
= sizeof (*entry
);
608 entry
= bfd_alloc (input_bfd
, amt
);
612 /* Go find the symbol, so that we can find it's name. */
613 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
614 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
616 bfd_release (input_bfd
, entry
);
620 if (entry
->isym
.st_shndx
!= SHN_UNDEF
621 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
625 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
626 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
628 /* We can still bfd_release here as nothing has done another
629 bfd_alloc. We can't do this later in this function. */
630 bfd_release (input_bfd
, entry
);
635 name
= (bfd_elf_string_from_elf_section
636 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
637 entry
->isym
.st_name
));
639 dynstr
= elf_hash_table (info
)->dynstr
;
642 /* Create a strtab to hold the dynamic symbol names. */
643 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
648 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
649 if (dynstr_index
== (unsigned long) -1)
651 entry
->isym
.st_name
= dynstr_index
;
653 eht
= elf_hash_table (info
);
655 entry
->next
= eht
->dynlocal
;
656 eht
->dynlocal
= entry
;
657 entry
->input_bfd
= input_bfd
;
658 entry
->input_indx
= input_indx
;
661 /* Whatever binding the symbol had before, it's now local. */
663 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
665 /* The dynindx will be set at the end of size_dynamic_sections. */
670 /* Return the dynindex of a local dynamic symbol. */
673 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
677 struct elf_link_local_dynamic_entry
*e
;
679 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
680 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
685 /* This function is used to renumber the dynamic symbols, if some of
686 them are removed because they are marked as local. This is called
687 via elf_link_hash_traverse. */
690 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
693 size_t *count
= data
;
695 if (h
->root
.type
== bfd_link_hash_warning
)
696 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
701 if (h
->dynindx
!= -1)
702 h
->dynindx
= ++(*count
);
708 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
709 STB_LOCAL binding. */
712 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
715 size_t *count
= data
;
717 if (h
->root
.type
== bfd_link_hash_warning
)
718 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
720 if (!h
->forced_local
)
723 if (h
->dynindx
!= -1)
724 h
->dynindx
= ++(*count
);
729 /* Return true if the dynamic symbol for a given section should be
730 omitted when creating a shared library. */
732 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
733 struct bfd_link_info
*info
,
736 struct elf_link_hash_table
*htab
;
738 switch (elf_section_data (p
)->this_hdr
.sh_type
)
742 /* If sh_type is yet undecided, assume it could be
743 SHT_PROGBITS/SHT_NOBITS. */
745 htab
= elf_hash_table (info
);
746 if (p
== htab
->tls_sec
)
749 if (htab
->text_index_section
!= NULL
)
750 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
752 if (strcmp (p
->name
, ".got") == 0
753 || strcmp (p
->name
, ".got.plt") == 0
754 || strcmp (p
->name
, ".plt") == 0)
758 if (htab
->dynobj
!= NULL
759 && (ip
= bfd_get_section_by_name (htab
->dynobj
, p
->name
)) != NULL
760 && (ip
->flags
& SEC_LINKER_CREATED
)
761 && ip
->output_section
== p
)
766 /* There shouldn't be section relative relocations
767 against any other section. */
773 /* Assign dynsym indices. In a shared library we generate a section
774 symbol for each output section, which come first. Next come symbols
775 which have been forced to local binding. Then all of the back-end
776 allocated local dynamic syms, followed by the rest of the global
780 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
781 struct bfd_link_info
*info
,
782 unsigned long *section_sym_count
)
784 unsigned long dynsymcount
= 0;
786 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
788 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
790 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
791 if ((p
->flags
& SEC_EXCLUDE
) == 0
792 && (p
->flags
& SEC_ALLOC
) != 0
793 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
794 elf_section_data (p
)->dynindx
= ++dynsymcount
;
796 elf_section_data (p
)->dynindx
= 0;
798 *section_sym_count
= dynsymcount
;
800 elf_link_hash_traverse (elf_hash_table (info
),
801 elf_link_renumber_local_hash_table_dynsyms
,
804 if (elf_hash_table (info
)->dynlocal
)
806 struct elf_link_local_dynamic_entry
*p
;
807 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
808 p
->dynindx
= ++dynsymcount
;
811 elf_link_hash_traverse (elf_hash_table (info
),
812 elf_link_renumber_hash_table_dynsyms
,
815 /* There is an unused NULL entry at the head of the table which
816 we must account for in our count. Unless there weren't any
817 symbols, which means we'll have no table at all. */
818 if (dynsymcount
!= 0)
821 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
825 /* This function is called when we want to define a new symbol. It
826 handles the various cases which arise when we find a definition in
827 a dynamic object, or when there is already a definition in a
828 dynamic object. The new symbol is described by NAME, SYM, PSEC,
829 and PVALUE. We set SYM_HASH to the hash table entry. We set
830 OVERRIDE if the old symbol is overriding a new definition. We set
831 TYPE_CHANGE_OK if it is OK for the type to change. We set
832 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
833 change, we mean that we shouldn't warn if the type or size does
834 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
835 object is overridden by a regular object. */
838 _bfd_elf_merge_symbol (bfd
*abfd
,
839 struct bfd_link_info
*info
,
841 Elf_Internal_Sym
*sym
,
844 unsigned int *pold_alignment
,
845 struct elf_link_hash_entry
**sym_hash
,
847 bfd_boolean
*override
,
848 bfd_boolean
*type_change_ok
,
849 bfd_boolean
*size_change_ok
)
851 asection
*sec
, *oldsec
;
852 struct elf_link_hash_entry
*h
;
853 struct elf_link_hash_entry
*flip
;
856 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
857 bfd_boolean newweak
, oldweak
, newfunc
, oldfunc
;
858 const struct elf_backend_data
*bed
;
864 bind
= ELF_ST_BIND (sym
->st_info
);
866 /* Silently discard TLS symbols from --just-syms. There's no way to
867 combine a static TLS block with a new TLS block for this executable. */
868 if (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
869 && sec
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
875 if (! bfd_is_und_section (sec
))
876 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
878 h
= ((struct elf_link_hash_entry
*)
879 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
884 bed
= get_elf_backend_data (abfd
);
886 /* This code is for coping with dynamic objects, and is only useful
887 if we are doing an ELF link. */
888 if (!(*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
891 /* For merging, we only care about real symbols. */
893 while (h
->root
.type
== bfd_link_hash_indirect
894 || h
->root
.type
== bfd_link_hash_warning
)
895 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
897 /* We have to check it for every instance since the first few may be
898 refereences and not all compilers emit symbol type for undefined
900 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
902 /* If we just created the symbol, mark it as being an ELF symbol.
903 Other than that, there is nothing to do--there is no merge issue
904 with a newly defined symbol--so we just return. */
906 if (h
->root
.type
== bfd_link_hash_new
)
912 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
915 switch (h
->root
.type
)
922 case bfd_link_hash_undefined
:
923 case bfd_link_hash_undefweak
:
924 oldbfd
= h
->root
.u
.undef
.abfd
;
928 case bfd_link_hash_defined
:
929 case bfd_link_hash_defweak
:
930 oldbfd
= h
->root
.u
.def
.section
->owner
;
931 oldsec
= h
->root
.u
.def
.section
;
934 case bfd_link_hash_common
:
935 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
936 oldsec
= h
->root
.u
.c
.p
->section
;
940 /* In cases involving weak versioned symbols, we may wind up trying
941 to merge a symbol with itself. Catch that here, to avoid the
942 confusion that results if we try to override a symbol with
943 itself. The additional tests catch cases like
944 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
945 dynamic object, which we do want to handle here. */
947 && ((abfd
->flags
& DYNAMIC
) == 0
951 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
952 respectively, is from a dynamic object. */
954 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
958 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
959 else if (oldsec
!= NULL
)
961 /* This handles the special SHN_MIPS_{TEXT,DATA} section
962 indices used by MIPS ELF. */
963 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
966 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
967 respectively, appear to be a definition rather than reference. */
969 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
971 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
972 && h
->root
.type
!= bfd_link_hash_undefweak
973 && h
->root
.type
!= bfd_link_hash_common
);
975 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
976 respectively, appear to be a function. */
978 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
979 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
981 oldfunc
= (h
->type
!= STT_NOTYPE
982 && bed
->is_function_type (h
->type
));
984 /* When we try to create a default indirect symbol from the dynamic
985 definition with the default version, we skip it if its type and
986 the type of existing regular definition mismatch. We only do it
987 if the existing regular definition won't be dynamic. */
988 if (pold_alignment
== NULL
990 && !info
->export_dynamic
995 && (olddef
|| h
->root
.type
== bfd_link_hash_common
)
996 && ELF_ST_TYPE (sym
->st_info
) != h
->type
997 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
998 && h
->type
!= STT_NOTYPE
999 && !(newfunc
&& oldfunc
))
1005 /* Check TLS symbol. We don't check undefined symbol introduced by
1007 if ((ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
)
1008 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1012 bfd_boolean ntdef
, tdef
;
1013 asection
*ntsec
, *tsec
;
1015 if (h
->type
== STT_TLS
)
1035 (*_bfd_error_handler
)
1036 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1037 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
1038 else if (!tdef
&& !ntdef
)
1039 (*_bfd_error_handler
)
1040 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1041 tbfd
, ntbfd
, h
->root
.root
.string
);
1043 (*_bfd_error_handler
)
1044 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1045 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
1047 (*_bfd_error_handler
)
1048 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1049 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
1051 bfd_set_error (bfd_error_bad_value
);
1055 /* We need to remember if a symbol has a definition in a dynamic
1056 object or is weak in all dynamic objects. Internal and hidden
1057 visibility will make it unavailable to dynamic objects. */
1058 if (newdyn
&& !h
->dynamic_def
)
1060 if (!bfd_is_und_section (sec
))
1064 /* Check if this symbol is weak in all dynamic objects. If it
1065 is the first time we see it in a dynamic object, we mark
1066 if it is weak. Otherwise, we clear it. */
1067 if (!h
->ref_dynamic
)
1069 if (bind
== STB_WEAK
)
1070 h
->dynamic_weak
= 1;
1072 else if (bind
!= STB_WEAK
)
1073 h
->dynamic_weak
= 0;
1077 /* If the old symbol has non-default visibility, we ignore the new
1078 definition from a dynamic object. */
1080 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1081 && !bfd_is_und_section (sec
))
1084 /* Make sure this symbol is dynamic. */
1086 /* A protected symbol has external availability. Make sure it is
1087 recorded as dynamic.
1089 FIXME: Should we check type and size for protected symbol? */
1090 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1091 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1096 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1099 /* If the new symbol with non-default visibility comes from a
1100 relocatable file and the old definition comes from a dynamic
1101 object, we remove the old definition. */
1102 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1104 /* Handle the case where the old dynamic definition is
1105 default versioned. We need to copy the symbol info from
1106 the symbol with default version to the normal one if it
1107 was referenced before. */
1110 const struct elf_backend_data
*bed
1111 = get_elf_backend_data (abfd
);
1112 struct elf_link_hash_entry
*vh
= *sym_hash
;
1113 vh
->root
.type
= h
->root
.type
;
1114 h
->root
.type
= bfd_link_hash_indirect
;
1115 (*bed
->elf_backend_copy_indirect_symbol
) (info
, vh
, h
);
1116 /* Protected symbols will override the dynamic definition
1117 with default version. */
1118 if (ELF_ST_VISIBILITY (sym
->st_other
) == STV_PROTECTED
)
1120 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) vh
;
1121 vh
->dynamic_def
= 1;
1122 vh
->ref_dynamic
= 1;
1126 h
->root
.type
= vh
->root
.type
;
1127 vh
->ref_dynamic
= 0;
1128 /* We have to hide it here since it was made dynamic
1129 global with extra bits when the symbol info was
1130 copied from the old dynamic definition. */
1131 (*bed
->elf_backend_hide_symbol
) (info
, vh
, TRUE
);
1139 if ((h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1140 && bfd_is_und_section (sec
))
1142 /* If the new symbol is undefined and the old symbol was
1143 also undefined before, we need to make sure
1144 _bfd_generic_link_add_one_symbol doesn't mess
1145 up the linker hash table undefs list. Since the old
1146 definition came from a dynamic object, it is still on the
1148 h
->root
.type
= bfd_link_hash_undefined
;
1149 h
->root
.u
.undef
.abfd
= abfd
;
1153 h
->root
.type
= bfd_link_hash_new
;
1154 h
->root
.u
.undef
.abfd
= NULL
;
1163 /* FIXME: Should we check type and size for protected symbol? */
1169 /* Differentiate strong and weak symbols. */
1170 newweak
= bind
== STB_WEAK
;
1171 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1172 || h
->root
.type
== bfd_link_hash_undefweak
);
1174 /* If a new weak symbol definition comes from a regular file and the
1175 old symbol comes from a dynamic library, we treat the new one as
1176 strong. Similarly, an old weak symbol definition from a regular
1177 file is treated as strong when the new symbol comes from a dynamic
1178 library. Further, an old weak symbol from a dynamic library is
1179 treated as strong if the new symbol is from a dynamic library.
1180 This reflects the way glibc's ld.so works.
1182 Do this before setting *type_change_ok or *size_change_ok so that
1183 we warn properly when dynamic library symbols are overridden. */
1185 if (newdef
&& !newdyn
&& olddyn
)
1187 if (olddef
&& newdyn
)
1190 /* Allow changes between different types of funciton symbol. */
1191 if (newfunc
&& oldfunc
)
1192 *type_change_ok
= TRUE
;
1194 /* It's OK to change the type if either the existing symbol or the
1195 new symbol is weak. A type change is also OK if the old symbol
1196 is undefined and the new symbol is defined. */
1201 && h
->root
.type
== bfd_link_hash_undefined
))
1202 *type_change_ok
= TRUE
;
1204 /* It's OK to change the size if either the existing symbol or the
1205 new symbol is weak, or if the old symbol is undefined. */
1208 || h
->root
.type
== bfd_link_hash_undefined
)
1209 *size_change_ok
= TRUE
;
1211 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1212 symbol, respectively, appears to be a common symbol in a dynamic
1213 object. If a symbol appears in an uninitialized section, and is
1214 not weak, and is not a function, then it may be a common symbol
1215 which was resolved when the dynamic object was created. We want
1216 to treat such symbols specially, because they raise special
1217 considerations when setting the symbol size: if the symbol
1218 appears as a common symbol in a regular object, and the size in
1219 the regular object is larger, we must make sure that we use the
1220 larger size. This problematic case can always be avoided in C,
1221 but it must be handled correctly when using Fortran shared
1224 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1225 likewise for OLDDYNCOMMON and OLDDEF.
1227 Note that this test is just a heuristic, and that it is quite
1228 possible to have an uninitialized symbol in a shared object which
1229 is really a definition, rather than a common symbol. This could
1230 lead to some minor confusion when the symbol really is a common
1231 symbol in some regular object. However, I think it will be
1237 && (sec
->flags
& SEC_ALLOC
) != 0
1238 && (sec
->flags
& SEC_LOAD
) == 0
1241 newdyncommon
= TRUE
;
1243 newdyncommon
= FALSE
;
1247 && h
->root
.type
== bfd_link_hash_defined
1249 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1250 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1253 olddyncommon
= TRUE
;
1255 olddyncommon
= FALSE
;
1257 /* We now know everything about the old and new symbols. We ask the
1258 backend to check if we can merge them. */
1259 if (bed
->merge_symbol
1260 && !bed
->merge_symbol (info
, sym_hash
, h
, sym
, psec
, pvalue
,
1261 pold_alignment
, skip
, override
,
1262 type_change_ok
, size_change_ok
,
1263 &newdyn
, &newdef
, &newdyncommon
, &newweak
,
1265 &olddyn
, &olddef
, &olddyncommon
, &oldweak
,
1269 /* If both the old and the new symbols look like common symbols in a
1270 dynamic object, set the size of the symbol to the larger of the
1275 && sym
->st_size
!= h
->size
)
1277 /* Since we think we have two common symbols, issue a multiple
1278 common warning if desired. Note that we only warn if the
1279 size is different. If the size is the same, we simply let
1280 the old symbol override the new one as normally happens with
1281 symbols defined in dynamic objects. */
1283 if (! ((*info
->callbacks
->multiple_common
)
1284 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1285 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1288 if (sym
->st_size
> h
->size
)
1289 h
->size
= sym
->st_size
;
1291 *size_change_ok
= TRUE
;
1294 /* If we are looking at a dynamic object, and we have found a
1295 definition, we need to see if the symbol was already defined by
1296 some other object. If so, we want to use the existing
1297 definition, and we do not want to report a multiple symbol
1298 definition error; we do this by clobbering *PSEC to be
1299 bfd_und_section_ptr.
1301 We treat a common symbol as a definition if the symbol in the
1302 shared library is a function, since common symbols always
1303 represent variables; this can cause confusion in principle, but
1304 any such confusion would seem to indicate an erroneous program or
1305 shared library. We also permit a common symbol in a regular
1306 object to override a weak symbol in a shared object. */
1311 || (h
->root
.type
== bfd_link_hash_common
1312 && (newweak
|| newfunc
))))
1316 newdyncommon
= FALSE
;
1318 *psec
= sec
= bfd_und_section_ptr
;
1319 *size_change_ok
= TRUE
;
1321 /* If we get here when the old symbol is a common symbol, then
1322 we are explicitly letting it override a weak symbol or
1323 function in a dynamic object, and we don't want to warn about
1324 a type change. If the old symbol is a defined symbol, a type
1325 change warning may still be appropriate. */
1327 if (h
->root
.type
== bfd_link_hash_common
)
1328 *type_change_ok
= TRUE
;
1331 /* Handle the special case of an old common symbol merging with a
1332 new symbol which looks like a common symbol in a shared object.
1333 We change *PSEC and *PVALUE to make the new symbol look like a
1334 common symbol, and let _bfd_generic_link_add_one_symbol do the
1338 && h
->root
.type
== bfd_link_hash_common
)
1342 newdyncommon
= FALSE
;
1343 *pvalue
= sym
->st_size
;
1344 *psec
= sec
= bed
->common_section (oldsec
);
1345 *size_change_ok
= TRUE
;
1348 /* Skip weak definitions of symbols that are already defined. */
1349 if (newdef
&& olddef
&& newweak
)
1352 /* If the old symbol is from a dynamic object, and the new symbol is
1353 a definition which is not from a dynamic object, then the new
1354 symbol overrides the old symbol. Symbols from regular files
1355 always take precedence over symbols from dynamic objects, even if
1356 they are defined after the dynamic object in the link.
1358 As above, we again permit a common symbol in a regular object to
1359 override a definition in a shared object if the shared object
1360 symbol is a function or is weak. */
1365 || (bfd_is_com_section (sec
)
1366 && (oldweak
|| oldfunc
)))
1371 /* Change the hash table entry to undefined, and let
1372 _bfd_generic_link_add_one_symbol do the right thing with the
1375 h
->root
.type
= bfd_link_hash_undefined
;
1376 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1377 *size_change_ok
= TRUE
;
1380 olddyncommon
= FALSE
;
1382 /* We again permit a type change when a common symbol may be
1383 overriding a function. */
1385 if (bfd_is_com_section (sec
))
1389 /* If a common symbol overrides a function, make sure
1390 that it isn't defined dynamically nor has type
1393 h
->type
= STT_NOTYPE
;
1395 *type_change_ok
= TRUE
;
1398 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1401 /* This union may have been set to be non-NULL when this symbol
1402 was seen in a dynamic object. We must force the union to be
1403 NULL, so that it is correct for a regular symbol. */
1404 h
->verinfo
.vertree
= NULL
;
1407 /* Handle the special case of a new common symbol merging with an
1408 old symbol that looks like it might be a common symbol defined in
1409 a shared object. Note that we have already handled the case in
1410 which a new common symbol should simply override the definition
1411 in the shared library. */
1414 && bfd_is_com_section (sec
)
1417 /* It would be best if we could set the hash table entry to a
1418 common symbol, but we don't know what to use for the section
1419 or the alignment. */
1420 if (! ((*info
->callbacks
->multiple_common
)
1421 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1422 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1425 /* If the presumed common symbol in the dynamic object is
1426 larger, pretend that the new symbol has its size. */
1428 if (h
->size
> *pvalue
)
1431 /* We need to remember the alignment required by the symbol
1432 in the dynamic object. */
1433 BFD_ASSERT (pold_alignment
);
1434 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1437 olddyncommon
= FALSE
;
1439 h
->root
.type
= bfd_link_hash_undefined
;
1440 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1442 *size_change_ok
= TRUE
;
1443 *type_change_ok
= TRUE
;
1445 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1448 h
->verinfo
.vertree
= NULL
;
1453 /* Handle the case where we had a versioned symbol in a dynamic
1454 library and now find a definition in a normal object. In this
1455 case, we make the versioned symbol point to the normal one. */
1456 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1457 flip
->root
.type
= h
->root
.type
;
1458 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1459 h
->root
.type
= bfd_link_hash_indirect
;
1460 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1461 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1465 flip
->ref_dynamic
= 1;
1472 /* This function is called to create an indirect symbol from the
1473 default for the symbol with the default version if needed. The
1474 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1475 set DYNSYM if the new indirect symbol is dynamic. */
1478 _bfd_elf_add_default_symbol (bfd
*abfd
,
1479 struct bfd_link_info
*info
,
1480 struct elf_link_hash_entry
*h
,
1482 Elf_Internal_Sym
*sym
,
1485 bfd_boolean
*dynsym
,
1486 bfd_boolean override
)
1488 bfd_boolean type_change_ok
;
1489 bfd_boolean size_change_ok
;
1492 struct elf_link_hash_entry
*hi
;
1493 struct bfd_link_hash_entry
*bh
;
1494 const struct elf_backend_data
*bed
;
1495 bfd_boolean collect
;
1496 bfd_boolean dynamic
;
1498 size_t len
, shortlen
;
1501 /* If this symbol has a version, and it is the default version, we
1502 create an indirect symbol from the default name to the fully
1503 decorated name. This will cause external references which do not
1504 specify a version to be bound to this version of the symbol. */
1505 p
= strchr (name
, ELF_VER_CHR
);
1506 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1511 /* We are overridden by an old definition. We need to check if we
1512 need to create the indirect symbol from the default name. */
1513 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1515 BFD_ASSERT (hi
!= NULL
);
1518 while (hi
->root
.type
== bfd_link_hash_indirect
1519 || hi
->root
.type
== bfd_link_hash_warning
)
1521 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1527 bed
= get_elf_backend_data (abfd
);
1528 collect
= bed
->collect
;
1529 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1531 shortlen
= p
- name
;
1532 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1533 if (shortname
== NULL
)
1535 memcpy (shortname
, name
, shortlen
);
1536 shortname
[shortlen
] = '\0';
1538 /* We are going to create a new symbol. Merge it with any existing
1539 symbol with this name. For the purposes of the merge, act as
1540 though we were defining the symbol we just defined, although we
1541 actually going to define an indirect symbol. */
1542 type_change_ok
= FALSE
;
1543 size_change_ok
= FALSE
;
1545 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1546 NULL
, &hi
, &skip
, &override
,
1547 &type_change_ok
, &size_change_ok
))
1556 if (! (_bfd_generic_link_add_one_symbol
1557 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1558 0, name
, FALSE
, collect
, &bh
)))
1560 hi
= (struct elf_link_hash_entry
*) bh
;
1564 /* In this case the symbol named SHORTNAME is overriding the
1565 indirect symbol we want to add. We were planning on making
1566 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1567 is the name without a version. NAME is the fully versioned
1568 name, and it is the default version.
1570 Overriding means that we already saw a definition for the
1571 symbol SHORTNAME in a regular object, and it is overriding
1572 the symbol defined in the dynamic object.
1574 When this happens, we actually want to change NAME, the
1575 symbol we just added, to refer to SHORTNAME. This will cause
1576 references to NAME in the shared object to become references
1577 to SHORTNAME in the regular object. This is what we expect
1578 when we override a function in a shared object: that the
1579 references in the shared object will be mapped to the
1580 definition in the regular object. */
1582 while (hi
->root
.type
== bfd_link_hash_indirect
1583 || hi
->root
.type
== bfd_link_hash_warning
)
1584 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1586 h
->root
.type
= bfd_link_hash_indirect
;
1587 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1591 hi
->ref_dynamic
= 1;
1595 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1600 /* Now set HI to H, so that the following code will set the
1601 other fields correctly. */
1605 /* Check if HI is a warning symbol. */
1606 if (hi
->root
.type
== bfd_link_hash_warning
)
1607 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1609 /* If there is a duplicate definition somewhere, then HI may not
1610 point to an indirect symbol. We will have reported an error to
1611 the user in that case. */
1613 if (hi
->root
.type
== bfd_link_hash_indirect
)
1615 struct elf_link_hash_entry
*ht
;
1617 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1618 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
1620 /* See if the new flags lead us to realize that the symbol must
1632 if (hi
->ref_regular
)
1638 /* We also need to define an indirection from the nondefault version
1642 len
= strlen (name
);
1643 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1644 if (shortname
== NULL
)
1646 memcpy (shortname
, name
, shortlen
);
1647 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1649 /* Once again, merge with any existing symbol. */
1650 type_change_ok
= FALSE
;
1651 size_change_ok
= FALSE
;
1653 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1654 NULL
, &hi
, &skip
, &override
,
1655 &type_change_ok
, &size_change_ok
))
1663 /* Here SHORTNAME is a versioned name, so we don't expect to see
1664 the type of override we do in the case above unless it is
1665 overridden by a versioned definition. */
1666 if (hi
->root
.type
!= bfd_link_hash_defined
1667 && hi
->root
.type
!= bfd_link_hash_defweak
)
1668 (*_bfd_error_handler
)
1669 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1675 if (! (_bfd_generic_link_add_one_symbol
1676 (info
, abfd
, shortname
, BSF_INDIRECT
,
1677 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1679 hi
= (struct elf_link_hash_entry
*) bh
;
1681 /* If there is a duplicate definition somewhere, then HI may not
1682 point to an indirect symbol. We will have reported an error
1683 to the user in that case. */
1685 if (hi
->root
.type
== bfd_link_hash_indirect
)
1687 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
1689 /* See if the new flags lead us to realize that the symbol
1701 if (hi
->ref_regular
)
1711 /* This routine is used to export all defined symbols into the dynamic
1712 symbol table. It is called via elf_link_hash_traverse. */
1715 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1717 struct elf_info_failed
*eif
= data
;
1719 /* Ignore this if we won't export it. */
1720 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
1723 /* Ignore indirect symbols. These are added by the versioning code. */
1724 if (h
->root
.type
== bfd_link_hash_indirect
)
1727 if (h
->root
.type
== bfd_link_hash_warning
)
1728 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1730 if (h
->dynindx
== -1
1734 struct bfd_elf_version_tree
*t
;
1735 struct bfd_elf_version_expr
*d
;
1737 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1739 if (t
->globals
.list
!= NULL
)
1741 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1746 if (t
->locals
.list
!= NULL
)
1748 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1757 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1768 /* Look through the symbols which are defined in other shared
1769 libraries and referenced here. Update the list of version
1770 dependencies. This will be put into the .gnu.version_r section.
1771 This function is called via elf_link_hash_traverse. */
1774 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1777 struct elf_find_verdep_info
*rinfo
= data
;
1778 Elf_Internal_Verneed
*t
;
1779 Elf_Internal_Vernaux
*a
;
1782 if (h
->root
.type
== bfd_link_hash_warning
)
1783 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1785 /* We only care about symbols defined in shared objects with version
1790 || h
->verinfo
.verdef
== NULL
)
1793 /* See if we already know about this version. */
1794 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1796 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1799 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1800 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1806 /* This is a new version. Add it to tree we are building. */
1811 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1814 rinfo
->failed
= TRUE
;
1818 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1819 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1820 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1824 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1827 rinfo
->failed
= TRUE
;
1831 /* Note that we are copying a string pointer here, and testing it
1832 above. If bfd_elf_string_from_elf_section is ever changed to
1833 discard the string data when low in memory, this will have to be
1835 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1837 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1838 a
->vna_nextptr
= t
->vn_auxptr
;
1840 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1843 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1850 /* Figure out appropriate versions for all the symbols. We may not
1851 have the version number script until we have read all of the input
1852 files, so until that point we don't know which symbols should be
1853 local. This function is called via elf_link_hash_traverse. */
1856 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1858 struct elf_assign_sym_version_info
*sinfo
;
1859 struct bfd_link_info
*info
;
1860 const struct elf_backend_data
*bed
;
1861 struct elf_info_failed eif
;
1868 if (h
->root
.type
== bfd_link_hash_warning
)
1869 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1871 /* Fix the symbol flags. */
1874 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1877 sinfo
->failed
= TRUE
;
1881 /* We only need version numbers for symbols defined in regular
1883 if (!h
->def_regular
)
1886 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1887 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1888 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1890 struct bfd_elf_version_tree
*t
;
1895 /* There are two consecutive ELF_VER_CHR characters if this is
1896 not a hidden symbol. */
1898 if (*p
== ELF_VER_CHR
)
1904 /* If there is no version string, we can just return out. */
1912 /* Look for the version. If we find it, it is no longer weak. */
1913 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1915 if (strcmp (t
->name
, p
) == 0)
1919 struct bfd_elf_version_expr
*d
;
1921 len
= p
- h
->root
.root
.string
;
1922 alc
= bfd_malloc (len
);
1925 sinfo
->failed
= TRUE
;
1928 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1929 alc
[len
- 1] = '\0';
1930 if (alc
[len
- 2] == ELF_VER_CHR
)
1931 alc
[len
- 2] = '\0';
1933 h
->verinfo
.vertree
= t
;
1937 if (t
->globals
.list
!= NULL
)
1938 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1940 /* See if there is anything to force this symbol to
1942 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1944 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1947 && ! info
->export_dynamic
)
1948 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1956 /* If we are building an application, we need to create a
1957 version node for this version. */
1958 if (t
== NULL
&& info
->executable
)
1960 struct bfd_elf_version_tree
**pp
;
1963 /* If we aren't going to export this symbol, we don't need
1964 to worry about it. */
1965 if (h
->dynindx
== -1)
1969 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1972 sinfo
->failed
= TRUE
;
1977 t
->name_indx
= (unsigned int) -1;
1981 /* Don't count anonymous version tag. */
1982 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1984 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1986 t
->vernum
= version_index
;
1990 h
->verinfo
.vertree
= t
;
1994 /* We could not find the version for a symbol when
1995 generating a shared archive. Return an error. */
1996 (*_bfd_error_handler
)
1997 (_("%B: version node not found for symbol %s"),
1998 sinfo
->output_bfd
, h
->root
.root
.string
);
1999 bfd_set_error (bfd_error_bad_value
);
2000 sinfo
->failed
= TRUE
;
2008 /* If we don't have a version for this symbol, see if we can find
2010 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
2012 struct bfd_elf_version_tree
*t
;
2013 struct bfd_elf_version_tree
*local_ver
, *global_ver
, *exist_ver
;
2014 struct bfd_elf_version_expr
*d
;
2016 /* See if can find what version this symbol is in. If the
2017 symbol is supposed to be local, then don't actually register
2022 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
2024 if (t
->globals
.list
!= NULL
)
2027 while ((d
= (*t
->match
) (&t
->globals
, d
,
2028 h
->root
.root
.string
)) != NULL
)
2035 /* If the match is a wildcard pattern, keep looking for
2036 a more explicit, perhaps even local, match. */
2045 if (t
->locals
.list
!= NULL
)
2048 while ((d
= (*t
->match
) (&t
->locals
, d
,
2049 h
->root
.root
.string
)) != NULL
)
2052 /* If the match is a wildcard pattern, keep looking for
2053 a more explicit, perhaps even global, match. */
2056 /* An exact match overrides a global wildcard. */
2067 if (global_ver
!= NULL
)
2069 h
->verinfo
.vertree
= global_ver
;
2070 /* If we already have a versioned symbol that matches the
2071 node for this symbol, then we don't want to create a
2072 duplicate from the unversioned symbol. Instead hide the
2073 unversioned symbol. */
2074 if (exist_ver
== global_ver
)
2075 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2077 else if (local_ver
!= NULL
)
2079 h
->verinfo
.vertree
= local_ver
;
2080 if (!info
->export_dynamic
2081 || exist_ver
== local_ver
)
2082 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2089 /* Read and swap the relocs from the section indicated by SHDR. This
2090 may be either a REL or a RELA section. The relocations are
2091 translated into RELA relocations and stored in INTERNAL_RELOCS,
2092 which should have already been allocated to contain enough space.
2093 The EXTERNAL_RELOCS are a buffer where the external form of the
2094 relocations should be stored.
2096 Returns FALSE if something goes wrong. */
2099 elf_link_read_relocs_from_section (bfd
*abfd
,
2101 Elf_Internal_Shdr
*shdr
,
2102 void *external_relocs
,
2103 Elf_Internal_Rela
*internal_relocs
)
2105 const struct elf_backend_data
*bed
;
2106 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2107 const bfd_byte
*erela
;
2108 const bfd_byte
*erelaend
;
2109 Elf_Internal_Rela
*irela
;
2110 Elf_Internal_Shdr
*symtab_hdr
;
2113 /* Position ourselves at the start of the section. */
2114 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2117 /* Read the relocations. */
2118 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2121 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2122 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
2124 bed
= get_elf_backend_data (abfd
);
2126 /* Convert the external relocations to the internal format. */
2127 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2128 swap_in
= bed
->s
->swap_reloc_in
;
2129 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2130 swap_in
= bed
->s
->swap_reloca_in
;
2133 bfd_set_error (bfd_error_wrong_format
);
2137 erela
= external_relocs
;
2138 erelaend
= erela
+ shdr
->sh_size
;
2139 irela
= internal_relocs
;
2140 while (erela
< erelaend
)
2144 (*swap_in
) (abfd
, erela
, irela
);
2145 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2146 if (bed
->s
->arch_size
== 64)
2148 if ((size_t) r_symndx
>= nsyms
)
2150 (*_bfd_error_handler
)
2151 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2152 " for offset 0x%lx in section `%A'"),
2154 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2155 bfd_set_error (bfd_error_bad_value
);
2158 irela
+= bed
->s
->int_rels_per_ext_rel
;
2159 erela
+= shdr
->sh_entsize
;
2165 /* Read and swap the relocs for a section O. They may have been
2166 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2167 not NULL, they are used as buffers to read into. They are known to
2168 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2169 the return value is allocated using either malloc or bfd_alloc,
2170 according to the KEEP_MEMORY argument. If O has two relocation
2171 sections (both REL and RELA relocations), then the REL_HDR
2172 relocations will appear first in INTERNAL_RELOCS, followed by the
2173 REL_HDR2 relocations. */
2176 _bfd_elf_link_read_relocs (bfd
*abfd
,
2178 void *external_relocs
,
2179 Elf_Internal_Rela
*internal_relocs
,
2180 bfd_boolean keep_memory
)
2182 Elf_Internal_Shdr
*rel_hdr
;
2183 void *alloc1
= NULL
;
2184 Elf_Internal_Rela
*alloc2
= NULL
;
2185 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2187 if (elf_section_data (o
)->relocs
!= NULL
)
2188 return elf_section_data (o
)->relocs
;
2190 if (o
->reloc_count
== 0)
2193 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2195 if (internal_relocs
== NULL
)
2199 size
= o
->reloc_count
;
2200 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2202 internal_relocs
= alloc2
= bfd_alloc (abfd
, size
);
2204 internal_relocs
= alloc2
= bfd_malloc (size
);
2205 if (internal_relocs
== NULL
)
2209 if (external_relocs
== NULL
)
2211 bfd_size_type size
= rel_hdr
->sh_size
;
2213 if (elf_section_data (o
)->rel_hdr2
)
2214 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2215 alloc1
= bfd_malloc (size
);
2218 external_relocs
= alloc1
;
2221 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
2225 if (elf_section_data (o
)->rel_hdr2
2226 && (!elf_link_read_relocs_from_section
2228 elf_section_data (o
)->rel_hdr2
,
2229 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2230 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2231 * bed
->s
->int_rels_per_ext_rel
))))
2234 /* Cache the results for next time, if we can. */
2236 elf_section_data (o
)->relocs
= internal_relocs
;
2241 /* Don't free alloc2, since if it was allocated we are passing it
2242 back (under the name of internal_relocs). */
2244 return internal_relocs
;
2252 bfd_release (abfd
, alloc2
);
2259 /* Compute the size of, and allocate space for, REL_HDR which is the
2260 section header for a section containing relocations for O. */
2263 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2264 Elf_Internal_Shdr
*rel_hdr
,
2267 bfd_size_type reloc_count
;
2268 bfd_size_type num_rel_hashes
;
2270 /* Figure out how many relocations there will be. */
2271 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2272 reloc_count
= elf_section_data (o
)->rel_count
;
2274 reloc_count
= elf_section_data (o
)->rel_count2
;
2276 num_rel_hashes
= o
->reloc_count
;
2277 if (num_rel_hashes
< reloc_count
)
2278 num_rel_hashes
= reloc_count
;
2280 /* That allows us to calculate the size of the section. */
2281 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2283 /* The contents field must last into write_object_contents, so we
2284 allocate it with bfd_alloc rather than malloc. Also since we
2285 cannot be sure that the contents will actually be filled in,
2286 we zero the allocated space. */
2287 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2288 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2291 /* We only allocate one set of hash entries, so we only do it the
2292 first time we are called. */
2293 if (elf_section_data (o
)->rel_hashes
== NULL
2296 struct elf_link_hash_entry
**p
;
2298 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2302 elf_section_data (o
)->rel_hashes
= p
;
2308 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2309 originated from the section given by INPUT_REL_HDR) to the
2313 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2314 asection
*input_section
,
2315 Elf_Internal_Shdr
*input_rel_hdr
,
2316 Elf_Internal_Rela
*internal_relocs
,
2317 struct elf_link_hash_entry
**rel_hash
2320 Elf_Internal_Rela
*irela
;
2321 Elf_Internal_Rela
*irelaend
;
2323 Elf_Internal_Shdr
*output_rel_hdr
;
2324 asection
*output_section
;
2325 unsigned int *rel_countp
= NULL
;
2326 const struct elf_backend_data
*bed
;
2327 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2329 output_section
= input_section
->output_section
;
2330 output_rel_hdr
= NULL
;
2332 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2333 == input_rel_hdr
->sh_entsize
)
2335 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2336 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2338 else if (elf_section_data (output_section
)->rel_hdr2
2339 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2340 == input_rel_hdr
->sh_entsize
))
2342 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2343 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2347 (*_bfd_error_handler
)
2348 (_("%B: relocation size mismatch in %B section %A"),
2349 output_bfd
, input_section
->owner
, input_section
);
2350 bfd_set_error (bfd_error_wrong_format
);
2354 bed
= get_elf_backend_data (output_bfd
);
2355 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2356 swap_out
= bed
->s
->swap_reloc_out
;
2357 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2358 swap_out
= bed
->s
->swap_reloca_out
;
2362 erel
= output_rel_hdr
->contents
;
2363 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2364 irela
= internal_relocs
;
2365 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2366 * bed
->s
->int_rels_per_ext_rel
);
2367 while (irela
< irelaend
)
2369 (*swap_out
) (output_bfd
, irela
, erel
);
2370 irela
+= bed
->s
->int_rels_per_ext_rel
;
2371 erel
+= input_rel_hdr
->sh_entsize
;
2374 /* Bump the counter, so that we know where to add the next set of
2376 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2381 /* Make weak undefined symbols in PIE dynamic. */
2384 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2385 struct elf_link_hash_entry
*h
)
2389 && h
->root
.type
== bfd_link_hash_undefweak
)
2390 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2395 /* Fix up the flags for a symbol. This handles various cases which
2396 can only be fixed after all the input files are seen. This is
2397 currently called by both adjust_dynamic_symbol and
2398 assign_sym_version, which is unnecessary but perhaps more robust in
2399 the face of future changes. */
2402 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2403 struct elf_info_failed
*eif
)
2405 const struct elf_backend_data
*bed
;
2407 /* If this symbol was mentioned in a non-ELF file, try to set
2408 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2409 permit a non-ELF file to correctly refer to a symbol defined in
2410 an ELF dynamic object. */
2413 while (h
->root
.type
== bfd_link_hash_indirect
)
2414 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2416 if (h
->root
.type
!= bfd_link_hash_defined
2417 && h
->root
.type
!= bfd_link_hash_defweak
)
2420 h
->ref_regular_nonweak
= 1;
2424 if (h
->root
.u
.def
.section
->owner
!= NULL
2425 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2426 == bfd_target_elf_flavour
))
2429 h
->ref_regular_nonweak
= 1;
2435 if (h
->dynindx
== -1
2439 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2448 /* Unfortunately, NON_ELF is only correct if the symbol
2449 was first seen in a non-ELF file. Fortunately, if the symbol
2450 was first seen in an ELF file, we're probably OK unless the
2451 symbol was defined in a non-ELF file. Catch that case here.
2452 FIXME: We're still in trouble if the symbol was first seen in
2453 a dynamic object, and then later in a non-ELF regular object. */
2454 if ((h
->root
.type
== bfd_link_hash_defined
2455 || h
->root
.type
== bfd_link_hash_defweak
)
2457 && (h
->root
.u
.def
.section
->owner
!= NULL
2458 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2459 != bfd_target_elf_flavour
)
2460 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2461 && !h
->def_dynamic
)))
2465 /* Backend specific symbol fixup. */
2466 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2467 if (bed
->elf_backend_fixup_symbol
2468 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2471 /* If this is a final link, and the symbol was defined as a common
2472 symbol in a regular object file, and there was no definition in
2473 any dynamic object, then the linker will have allocated space for
2474 the symbol in a common section but the DEF_REGULAR
2475 flag will not have been set. */
2476 if (h
->root
.type
== bfd_link_hash_defined
2480 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2483 /* If -Bsymbolic was used (which means to bind references to global
2484 symbols to the definition within the shared object), and this
2485 symbol was defined in a regular object, then it actually doesn't
2486 need a PLT entry. Likewise, if the symbol has non-default
2487 visibility. If the symbol has hidden or internal visibility, we
2488 will force it local. */
2490 && eif
->info
->shared
2491 && is_elf_hash_table (eif
->info
->hash
)
2492 && (SYMBOLIC_BIND (eif
->info
, h
)
2493 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2496 bfd_boolean force_local
;
2498 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2499 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2500 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2503 /* If a weak undefined symbol has non-default visibility, we also
2504 hide it from the dynamic linker. */
2505 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2506 && h
->root
.type
== bfd_link_hash_undefweak
)
2507 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2509 /* If this is a weak defined symbol in a dynamic object, and we know
2510 the real definition in the dynamic object, copy interesting flags
2511 over to the real definition. */
2512 if (h
->u
.weakdef
!= NULL
)
2514 struct elf_link_hash_entry
*weakdef
;
2516 weakdef
= h
->u
.weakdef
;
2517 if (h
->root
.type
== bfd_link_hash_indirect
)
2518 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2520 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2521 || h
->root
.type
== bfd_link_hash_defweak
);
2522 BFD_ASSERT (weakdef
->def_dynamic
);
2524 /* If the real definition is defined by a regular object file,
2525 don't do anything special. See the longer description in
2526 _bfd_elf_adjust_dynamic_symbol, below. */
2527 if (weakdef
->def_regular
)
2528 h
->u
.weakdef
= NULL
;
2531 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2532 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2533 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
, h
);
2540 /* Make the backend pick a good value for a dynamic symbol. This is
2541 called via elf_link_hash_traverse, and also calls itself
2545 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2547 struct elf_info_failed
*eif
= data
;
2549 const struct elf_backend_data
*bed
;
2551 if (! is_elf_hash_table (eif
->info
->hash
))
2554 if (h
->root
.type
== bfd_link_hash_warning
)
2556 h
->got
= elf_hash_table (eif
->info
)->init_got_offset
;
2557 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2559 /* When warning symbols are created, they **replace** the "real"
2560 entry in the hash table, thus we never get to see the real
2561 symbol in a hash traversal. So look at it now. */
2562 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2565 /* Ignore indirect symbols. These are added by the versioning code. */
2566 if (h
->root
.type
== bfd_link_hash_indirect
)
2569 /* Fix the symbol flags. */
2570 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2573 /* If this symbol does not require a PLT entry, and it is not
2574 defined by a dynamic object, or is not referenced by a regular
2575 object, ignore it. We do have to handle a weak defined symbol,
2576 even if no regular object refers to it, if we decided to add it
2577 to the dynamic symbol table. FIXME: Do we normally need to worry
2578 about symbols which are defined by one dynamic object and
2579 referenced by another one? */
2584 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2586 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2590 /* If we've already adjusted this symbol, don't do it again. This
2591 can happen via a recursive call. */
2592 if (h
->dynamic_adjusted
)
2595 /* Don't look at this symbol again. Note that we must set this
2596 after checking the above conditions, because we may look at a
2597 symbol once, decide not to do anything, and then get called
2598 recursively later after REF_REGULAR is set below. */
2599 h
->dynamic_adjusted
= 1;
2601 /* If this is a weak definition, and we know a real definition, and
2602 the real symbol is not itself defined by a regular object file,
2603 then get a good value for the real definition. We handle the
2604 real symbol first, for the convenience of the backend routine.
2606 Note that there is a confusing case here. If the real definition
2607 is defined by a regular object file, we don't get the real symbol
2608 from the dynamic object, but we do get the weak symbol. If the
2609 processor backend uses a COPY reloc, then if some routine in the
2610 dynamic object changes the real symbol, we will not see that
2611 change in the corresponding weak symbol. This is the way other
2612 ELF linkers work as well, and seems to be a result of the shared
2615 I will clarify this issue. Most SVR4 shared libraries define the
2616 variable _timezone and define timezone as a weak synonym. The
2617 tzset call changes _timezone. If you write
2618 extern int timezone;
2620 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2621 you might expect that, since timezone is a synonym for _timezone,
2622 the same number will print both times. However, if the processor
2623 backend uses a COPY reloc, then actually timezone will be copied
2624 into your process image, and, since you define _timezone
2625 yourself, _timezone will not. Thus timezone and _timezone will
2626 wind up at different memory locations. The tzset call will set
2627 _timezone, leaving timezone unchanged. */
2629 if (h
->u
.weakdef
!= NULL
)
2631 /* If we get to this point, we know there is an implicit
2632 reference by a regular object file via the weak symbol H.
2633 FIXME: Is this really true? What if the traversal finds
2634 H->U.WEAKDEF before it finds H? */
2635 h
->u
.weakdef
->ref_regular
= 1;
2637 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2641 /* If a symbol has no type and no size and does not require a PLT
2642 entry, then we are probably about to do the wrong thing here: we
2643 are probably going to create a COPY reloc for an empty object.
2644 This case can arise when a shared object is built with assembly
2645 code, and the assembly code fails to set the symbol type. */
2647 && h
->type
== STT_NOTYPE
2649 (*_bfd_error_handler
)
2650 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2651 h
->root
.root
.string
);
2653 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2654 bed
= get_elf_backend_data (dynobj
);
2656 if (h
->type
== STT_IFUNC
2657 && (bed
->elf_osabi
== ELFOSABI_LINUX
2658 /* GNU/Linux is still using the default value 0. */
2659 || bed
->elf_osabi
== ELFOSABI_NONE
))
2662 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2671 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2675 _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry
*h
,
2678 unsigned int power_of_two
;
2680 asection
*sec
= h
->root
.u
.def
.section
;
2682 /* The section aligment of definition is the maximum alignment
2683 requirement of symbols defined in the section. Since we don't
2684 know the symbol alignment requirement, we start with the
2685 maximum alignment and check low bits of the symbol address
2686 for the minimum alignment. */
2687 power_of_two
= bfd_get_section_alignment (sec
->owner
, sec
);
2688 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
2689 while ((h
->root
.u
.def
.value
& mask
) != 0)
2695 if (power_of_two
> bfd_get_section_alignment (dynbss
->owner
,
2698 /* Adjust the section alignment if needed. */
2699 if (! bfd_set_section_alignment (dynbss
->owner
, dynbss
,
2704 /* We make sure that the symbol will be aligned properly. */
2705 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
2707 /* Define the symbol as being at this point in DYNBSS. */
2708 h
->root
.u
.def
.section
= dynbss
;
2709 h
->root
.u
.def
.value
= dynbss
->size
;
2711 /* Increment the size of DYNBSS to make room for the symbol. */
2712 dynbss
->size
+= h
->size
;
2717 /* Adjust all external symbols pointing into SEC_MERGE sections
2718 to reflect the object merging within the sections. */
2721 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2725 if (h
->root
.type
== bfd_link_hash_warning
)
2726 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2728 if ((h
->root
.type
== bfd_link_hash_defined
2729 || h
->root
.type
== bfd_link_hash_defweak
)
2730 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2731 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2733 bfd
*output_bfd
= data
;
2735 h
->root
.u
.def
.value
=
2736 _bfd_merged_section_offset (output_bfd
,
2737 &h
->root
.u
.def
.section
,
2738 elf_section_data (sec
)->sec_info
,
2739 h
->root
.u
.def
.value
);
2745 /* Returns false if the symbol referred to by H should be considered
2746 to resolve local to the current module, and true if it should be
2747 considered to bind dynamically. */
2750 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2751 struct bfd_link_info
*info
,
2752 bfd_boolean ignore_protected
)
2754 bfd_boolean binding_stays_local_p
;
2755 const struct elf_backend_data
*bed
;
2756 struct elf_link_hash_table
*hash_table
;
2761 while (h
->root
.type
== bfd_link_hash_indirect
2762 || h
->root
.type
== bfd_link_hash_warning
)
2763 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2765 /* If it was forced local, then clearly it's not dynamic. */
2766 if (h
->dynindx
== -1)
2768 if (h
->forced_local
)
2771 /* Identify the cases where name binding rules say that a
2772 visible symbol resolves locally. */
2773 binding_stays_local_p
= info
->executable
|| SYMBOLIC_BIND (info
, h
);
2775 switch (ELF_ST_VISIBILITY (h
->other
))
2782 hash_table
= elf_hash_table (info
);
2783 if (!is_elf_hash_table (hash_table
))
2786 bed
= get_elf_backend_data (hash_table
->dynobj
);
2788 /* Proper resolution for function pointer equality may require
2789 that these symbols perhaps be resolved dynamically, even though
2790 we should be resolving them to the current module. */
2791 if (!ignore_protected
|| !bed
->is_function_type (h
->type
))
2792 binding_stays_local_p
= TRUE
;
2799 /* If it isn't defined locally, then clearly it's dynamic. */
2800 if (!h
->def_regular
)
2803 /* Otherwise, the symbol is dynamic if binding rules don't tell
2804 us that it remains local. */
2805 return !binding_stays_local_p
;
2808 /* Return true if the symbol referred to by H should be considered
2809 to resolve local to the current module, and false otherwise. Differs
2810 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2811 undefined symbols and weak symbols. */
2814 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2815 struct bfd_link_info
*info
,
2816 bfd_boolean local_protected
)
2818 const struct elf_backend_data
*bed
;
2819 struct elf_link_hash_table
*hash_table
;
2821 /* If it's a local sym, of course we resolve locally. */
2825 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2826 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
2827 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
2830 /* Common symbols that become definitions don't get the DEF_REGULAR
2831 flag set, so test it first, and don't bail out. */
2832 if (ELF_COMMON_DEF_P (h
))
2834 /* If we don't have a definition in a regular file, then we can't
2835 resolve locally. The sym is either undefined or dynamic. */
2836 else if (!h
->def_regular
)
2839 /* Forced local symbols resolve locally. */
2840 if (h
->forced_local
)
2843 /* As do non-dynamic symbols. */
2844 if (h
->dynindx
== -1)
2847 /* At this point, we know the symbol is defined and dynamic. In an
2848 executable it must resolve locally, likewise when building symbolic
2849 shared libraries. */
2850 if (info
->executable
|| SYMBOLIC_BIND (info
, h
))
2853 /* Now deal with defined dynamic symbols in shared libraries. Ones
2854 with default visibility might not resolve locally. */
2855 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2858 hash_table
= elf_hash_table (info
);
2859 if (!is_elf_hash_table (hash_table
))
2862 bed
= get_elf_backend_data (hash_table
->dynobj
);
2864 /* STV_PROTECTED non-function symbols are local. */
2865 if (!bed
->is_function_type (h
->type
))
2868 /* Function pointer equality tests may require that STV_PROTECTED
2869 symbols be treated as dynamic symbols, even when we know that the
2870 dynamic linker will resolve them locally. */
2871 return local_protected
;
2874 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2875 aligned. Returns the first TLS output section. */
2877 struct bfd_section
*
2878 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2880 struct bfd_section
*sec
, *tls
;
2881 unsigned int align
= 0;
2883 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2884 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2888 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2889 if (sec
->alignment_power
> align
)
2890 align
= sec
->alignment_power
;
2892 elf_hash_table (info
)->tls_sec
= tls
;
2894 /* Ensure the alignment of the first section is the largest alignment,
2895 so that the tls segment starts aligned. */
2897 tls
->alignment_power
= align
;
2902 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2904 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2905 Elf_Internal_Sym
*sym
)
2907 const struct elf_backend_data
*bed
;
2909 /* Local symbols do not count, but target specific ones might. */
2910 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2911 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2914 bed
= get_elf_backend_data (abfd
);
2915 /* Function symbols do not count. */
2916 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
2919 /* If the section is undefined, then so is the symbol. */
2920 if (sym
->st_shndx
== SHN_UNDEF
)
2923 /* If the symbol is defined in the common section, then
2924 it is a common definition and so does not count. */
2925 if (bed
->common_definition (sym
))
2928 /* If the symbol is in a target specific section then we
2929 must rely upon the backend to tell us what it is. */
2930 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2931 /* FIXME - this function is not coded yet:
2933 return _bfd_is_global_symbol_definition (abfd, sym);
2935 Instead for now assume that the definition is not global,
2936 Even if this is wrong, at least the linker will behave
2937 in the same way that it used to do. */
2943 /* Search the symbol table of the archive element of the archive ABFD
2944 whose archive map contains a mention of SYMDEF, and determine if
2945 the symbol is defined in this element. */
2947 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2949 Elf_Internal_Shdr
* hdr
;
2950 bfd_size_type symcount
;
2951 bfd_size_type extsymcount
;
2952 bfd_size_type extsymoff
;
2953 Elf_Internal_Sym
*isymbuf
;
2954 Elf_Internal_Sym
*isym
;
2955 Elf_Internal_Sym
*isymend
;
2958 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2962 if (! bfd_check_format (abfd
, bfd_object
))
2965 /* If we have already included the element containing this symbol in the
2966 link then we do not need to include it again. Just claim that any symbol
2967 it contains is not a definition, so that our caller will not decide to
2968 (re)include this element. */
2969 if (abfd
->archive_pass
)
2972 /* Select the appropriate symbol table. */
2973 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2974 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2976 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2978 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2980 /* The sh_info field of the symtab header tells us where the
2981 external symbols start. We don't care about the local symbols. */
2982 if (elf_bad_symtab (abfd
))
2984 extsymcount
= symcount
;
2989 extsymcount
= symcount
- hdr
->sh_info
;
2990 extsymoff
= hdr
->sh_info
;
2993 if (extsymcount
== 0)
2996 /* Read in the symbol table. */
2997 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2999 if (isymbuf
== NULL
)
3002 /* Scan the symbol table looking for SYMDEF. */
3004 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3008 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3013 if (strcmp (name
, symdef
->name
) == 0)
3015 result
= is_global_data_symbol_definition (abfd
, isym
);
3025 /* Add an entry to the .dynamic table. */
3028 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3032 struct elf_link_hash_table
*hash_table
;
3033 const struct elf_backend_data
*bed
;
3035 bfd_size_type newsize
;
3036 bfd_byte
*newcontents
;
3037 Elf_Internal_Dyn dyn
;
3039 hash_table
= elf_hash_table (info
);
3040 if (! is_elf_hash_table (hash_table
))
3043 bed
= get_elf_backend_data (hash_table
->dynobj
);
3044 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
3045 BFD_ASSERT (s
!= NULL
);
3047 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3048 newcontents
= bfd_realloc (s
->contents
, newsize
);
3049 if (newcontents
== NULL
)
3053 dyn
.d_un
.d_val
= val
;
3054 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3057 s
->contents
= newcontents
;
3062 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3063 otherwise just check whether one already exists. Returns -1 on error,
3064 1 if a DT_NEEDED tag already exists, and 0 on success. */
3067 elf_add_dt_needed_tag (bfd
*abfd
,
3068 struct bfd_link_info
*info
,
3072 struct elf_link_hash_table
*hash_table
;
3073 bfd_size_type oldsize
;
3074 bfd_size_type strindex
;
3076 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3079 hash_table
= elf_hash_table (info
);
3080 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
3081 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3082 if (strindex
== (bfd_size_type
) -1)
3085 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
3088 const struct elf_backend_data
*bed
;
3091 bed
= get_elf_backend_data (hash_table
->dynobj
);
3092 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
3094 for (extdyn
= sdyn
->contents
;
3095 extdyn
< sdyn
->contents
+ sdyn
->size
;
3096 extdyn
+= bed
->s
->sizeof_dyn
)
3098 Elf_Internal_Dyn dyn
;
3100 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3101 if (dyn
.d_tag
== DT_NEEDED
3102 && dyn
.d_un
.d_val
== strindex
)
3104 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3112 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3115 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3119 /* We were just checking for existence of the tag. */
3120 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3125 /* Sort symbol by value and section. */
3127 elf_sort_symbol (const void *arg1
, const void *arg2
)
3129 const struct elf_link_hash_entry
*h1
;
3130 const struct elf_link_hash_entry
*h2
;
3131 bfd_signed_vma vdiff
;
3133 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3134 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3135 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3137 return vdiff
> 0 ? 1 : -1;
3140 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3142 return sdiff
> 0 ? 1 : -1;
3147 /* This function is used to adjust offsets into .dynstr for
3148 dynamic symbols. This is called via elf_link_hash_traverse. */
3151 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3153 struct elf_strtab_hash
*dynstr
= data
;
3155 if (h
->root
.type
== bfd_link_hash_warning
)
3156 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3158 if (h
->dynindx
!= -1)
3159 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3163 /* Assign string offsets in .dynstr, update all structures referencing
3167 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3169 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3170 struct elf_link_local_dynamic_entry
*entry
;
3171 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3172 bfd
*dynobj
= hash_table
->dynobj
;
3175 const struct elf_backend_data
*bed
;
3178 _bfd_elf_strtab_finalize (dynstr
);
3179 size
= _bfd_elf_strtab_size (dynstr
);
3181 bed
= get_elf_backend_data (dynobj
);
3182 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3183 BFD_ASSERT (sdyn
!= NULL
);
3185 /* Update all .dynamic entries referencing .dynstr strings. */
3186 for (extdyn
= sdyn
->contents
;
3187 extdyn
< sdyn
->contents
+ sdyn
->size
;
3188 extdyn
+= bed
->s
->sizeof_dyn
)
3190 Elf_Internal_Dyn dyn
;
3192 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3196 dyn
.d_un
.d_val
= size
;
3204 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3209 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3212 /* Now update local dynamic symbols. */
3213 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3214 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3215 entry
->isym
.st_name
);
3217 /* And the rest of dynamic symbols. */
3218 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3220 /* Adjust version definitions. */
3221 if (elf_tdata (output_bfd
)->cverdefs
)
3226 Elf_Internal_Verdef def
;
3227 Elf_Internal_Verdaux defaux
;
3229 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3233 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3235 p
+= sizeof (Elf_External_Verdef
);
3236 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3238 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3240 _bfd_elf_swap_verdaux_in (output_bfd
,
3241 (Elf_External_Verdaux
*) p
, &defaux
);
3242 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3244 _bfd_elf_swap_verdaux_out (output_bfd
,
3245 &defaux
, (Elf_External_Verdaux
*) p
);
3246 p
+= sizeof (Elf_External_Verdaux
);
3249 while (def
.vd_next
);
3252 /* Adjust version references. */
3253 if (elf_tdata (output_bfd
)->verref
)
3258 Elf_Internal_Verneed need
;
3259 Elf_Internal_Vernaux needaux
;
3261 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3265 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3267 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3268 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3269 (Elf_External_Verneed
*) p
);
3270 p
+= sizeof (Elf_External_Verneed
);
3271 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3273 _bfd_elf_swap_vernaux_in (output_bfd
,
3274 (Elf_External_Vernaux
*) p
, &needaux
);
3275 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3277 _bfd_elf_swap_vernaux_out (output_bfd
,
3279 (Elf_External_Vernaux
*) p
);
3280 p
+= sizeof (Elf_External_Vernaux
);
3283 while (need
.vn_next
);
3289 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3290 The default is to only match when the INPUT and OUTPUT are exactly
3294 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3295 const bfd_target
*output
)
3297 return input
== output
;
3300 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3301 This version is used when different targets for the same architecture
3302 are virtually identical. */
3305 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3306 const bfd_target
*output
)
3308 const struct elf_backend_data
*obed
, *ibed
;
3310 if (input
== output
)
3313 ibed
= xvec_get_elf_backend_data (input
);
3314 obed
= xvec_get_elf_backend_data (output
);
3316 if (ibed
->arch
!= obed
->arch
)
3319 /* If both backends are using this function, deem them compatible. */
3320 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3323 /* Add symbols from an ELF object file to the linker hash table. */
3326 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3328 Elf_Internal_Shdr
*hdr
;
3329 bfd_size_type symcount
;
3330 bfd_size_type extsymcount
;
3331 bfd_size_type extsymoff
;
3332 struct elf_link_hash_entry
**sym_hash
;
3333 bfd_boolean dynamic
;
3334 Elf_External_Versym
*extversym
= NULL
;
3335 Elf_External_Versym
*ever
;
3336 struct elf_link_hash_entry
*weaks
;
3337 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3338 bfd_size_type nondeflt_vers_cnt
= 0;
3339 Elf_Internal_Sym
*isymbuf
= NULL
;
3340 Elf_Internal_Sym
*isym
;
3341 Elf_Internal_Sym
*isymend
;
3342 const struct elf_backend_data
*bed
;
3343 bfd_boolean add_needed
;
3344 struct elf_link_hash_table
*htab
;
3346 void *alloc_mark
= NULL
;
3347 struct bfd_hash_entry
**old_table
= NULL
;
3348 unsigned int old_size
= 0;
3349 unsigned int old_count
= 0;
3350 void *old_tab
= NULL
;
3353 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3354 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3355 long old_dynsymcount
= 0;
3357 size_t hashsize
= 0;
3359 htab
= elf_hash_table (info
);
3360 bed
= get_elf_backend_data (abfd
);
3362 if ((abfd
->flags
& DYNAMIC
) == 0)
3368 /* You can't use -r against a dynamic object. Also, there's no
3369 hope of using a dynamic object which does not exactly match
3370 the format of the output file. */
3371 if (info
->relocatable
3372 || !is_elf_hash_table (htab
)
3373 || info
->output_bfd
->xvec
!= abfd
->xvec
)
3375 if (info
->relocatable
)
3376 bfd_set_error (bfd_error_invalid_operation
);
3378 bfd_set_error (bfd_error_wrong_format
);
3383 /* As a GNU extension, any input sections which are named
3384 .gnu.warning.SYMBOL are treated as warning symbols for the given
3385 symbol. This differs from .gnu.warning sections, which generate
3386 warnings when they are included in an output file. */
3387 if (info
->executable
)
3391 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3395 name
= bfd_get_section_name (abfd
, s
);
3396 if (CONST_STRNEQ (name
, ".gnu.warning."))
3401 name
+= sizeof ".gnu.warning." - 1;
3403 /* If this is a shared object, then look up the symbol
3404 in the hash table. If it is there, and it is already
3405 been defined, then we will not be using the entry
3406 from this shared object, so we don't need to warn.
3407 FIXME: If we see the definition in a regular object
3408 later on, we will warn, but we shouldn't. The only
3409 fix is to keep track of what warnings we are supposed
3410 to emit, and then handle them all at the end of the
3414 struct elf_link_hash_entry
*h
;
3416 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3418 /* FIXME: What about bfd_link_hash_common? */
3420 && (h
->root
.type
== bfd_link_hash_defined
3421 || h
->root
.type
== bfd_link_hash_defweak
))
3423 /* We don't want to issue this warning. Clobber
3424 the section size so that the warning does not
3425 get copied into the output file. */
3432 msg
= bfd_alloc (abfd
, sz
+ 1);
3436 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3441 if (! (_bfd_generic_link_add_one_symbol
3442 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3443 FALSE
, bed
->collect
, NULL
)))
3446 if (! info
->relocatable
)
3448 /* Clobber the section size so that the warning does
3449 not get copied into the output file. */
3452 /* Also set SEC_EXCLUDE, so that symbols defined in
3453 the warning section don't get copied to the output. */
3454 s
->flags
|= SEC_EXCLUDE
;
3463 /* If we are creating a shared library, create all the dynamic
3464 sections immediately. We need to attach them to something,
3465 so we attach them to this BFD, provided it is the right
3466 format. FIXME: If there are no input BFD's of the same
3467 format as the output, we can't make a shared library. */
3469 && is_elf_hash_table (htab
)
3470 && info
->output_bfd
->xvec
== abfd
->xvec
3471 && !htab
->dynamic_sections_created
)
3473 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3477 else if (!is_elf_hash_table (htab
))
3482 const char *soname
= NULL
;
3483 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3486 /* ld --just-symbols and dynamic objects don't mix very well.
3487 ld shouldn't allow it. */
3488 if ((s
= abfd
->sections
) != NULL
3489 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3492 /* If this dynamic lib was specified on the command line with
3493 --as-needed in effect, then we don't want to add a DT_NEEDED
3494 tag unless the lib is actually used. Similary for libs brought
3495 in by another lib's DT_NEEDED. When --no-add-needed is used
3496 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3497 any dynamic library in DT_NEEDED tags in the dynamic lib at
3499 add_needed
= (elf_dyn_lib_class (abfd
)
3500 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3501 | DYN_NO_NEEDED
)) == 0;
3503 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3508 unsigned int elfsec
;
3509 unsigned long shlink
;
3511 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3512 goto error_free_dyn
;
3514 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3515 if (elfsec
== SHN_BAD
)
3516 goto error_free_dyn
;
3517 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3519 for (extdyn
= dynbuf
;
3520 extdyn
< dynbuf
+ s
->size
;
3521 extdyn
+= bed
->s
->sizeof_dyn
)
3523 Elf_Internal_Dyn dyn
;
3525 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3526 if (dyn
.d_tag
== DT_SONAME
)
3528 unsigned int tagv
= dyn
.d_un
.d_val
;
3529 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3531 goto error_free_dyn
;
3533 if (dyn
.d_tag
== DT_NEEDED
)
3535 struct bfd_link_needed_list
*n
, **pn
;
3537 unsigned int tagv
= dyn
.d_un
.d_val
;
3539 amt
= sizeof (struct bfd_link_needed_list
);
3540 n
= bfd_alloc (abfd
, amt
);
3541 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3542 if (n
== NULL
|| fnm
== NULL
)
3543 goto error_free_dyn
;
3544 amt
= strlen (fnm
) + 1;
3545 anm
= bfd_alloc (abfd
, amt
);
3547 goto error_free_dyn
;
3548 memcpy (anm
, fnm
, amt
);
3552 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3556 if (dyn
.d_tag
== DT_RUNPATH
)
3558 struct bfd_link_needed_list
*n
, **pn
;
3560 unsigned int tagv
= dyn
.d_un
.d_val
;
3562 amt
= sizeof (struct bfd_link_needed_list
);
3563 n
= bfd_alloc (abfd
, amt
);
3564 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3565 if (n
== NULL
|| fnm
== NULL
)
3566 goto error_free_dyn
;
3567 amt
= strlen (fnm
) + 1;
3568 anm
= bfd_alloc (abfd
, amt
);
3570 goto error_free_dyn
;
3571 memcpy (anm
, fnm
, amt
);
3575 for (pn
= & runpath
;
3581 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3582 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3584 struct bfd_link_needed_list
*n
, **pn
;
3586 unsigned int tagv
= dyn
.d_un
.d_val
;
3588 amt
= sizeof (struct bfd_link_needed_list
);
3589 n
= bfd_alloc (abfd
, amt
);
3590 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3591 if (n
== NULL
|| fnm
== NULL
)
3592 goto error_free_dyn
;
3593 amt
= strlen (fnm
) + 1;
3594 anm
= bfd_alloc (abfd
, amt
);
3601 memcpy (anm
, fnm
, amt
);
3616 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3617 frees all more recently bfd_alloc'd blocks as well. */
3623 struct bfd_link_needed_list
**pn
;
3624 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3629 /* We do not want to include any of the sections in a dynamic
3630 object in the output file. We hack by simply clobbering the
3631 list of sections in the BFD. This could be handled more
3632 cleanly by, say, a new section flag; the existing
3633 SEC_NEVER_LOAD flag is not the one we want, because that one
3634 still implies that the section takes up space in the output
3636 bfd_section_list_clear (abfd
);
3638 /* Find the name to use in a DT_NEEDED entry that refers to this
3639 object. If the object has a DT_SONAME entry, we use it.
3640 Otherwise, if the generic linker stuck something in
3641 elf_dt_name, we use that. Otherwise, we just use the file
3643 if (soname
== NULL
|| *soname
== '\0')
3645 soname
= elf_dt_name (abfd
);
3646 if (soname
== NULL
|| *soname
== '\0')
3647 soname
= bfd_get_filename (abfd
);
3650 /* Save the SONAME because sometimes the linker emulation code
3651 will need to know it. */
3652 elf_dt_name (abfd
) = soname
;
3654 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3658 /* If we have already included this dynamic object in the
3659 link, just ignore it. There is no reason to include a
3660 particular dynamic object more than once. */
3665 /* If this is a dynamic object, we always link against the .dynsym
3666 symbol table, not the .symtab symbol table. The dynamic linker
3667 will only see the .dynsym symbol table, so there is no reason to
3668 look at .symtab for a dynamic object. */
3670 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3671 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3673 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3675 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3677 /* The sh_info field of the symtab header tells us where the
3678 external symbols start. We don't care about the local symbols at
3680 if (elf_bad_symtab (abfd
))
3682 extsymcount
= symcount
;
3687 extsymcount
= symcount
- hdr
->sh_info
;
3688 extsymoff
= hdr
->sh_info
;
3692 if (extsymcount
!= 0)
3694 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3696 if (isymbuf
== NULL
)
3699 /* We store a pointer to the hash table entry for each external
3701 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3702 sym_hash
= bfd_alloc (abfd
, amt
);
3703 if (sym_hash
== NULL
)
3704 goto error_free_sym
;
3705 elf_sym_hashes (abfd
) = sym_hash
;
3710 /* Read in any version definitions. */
3711 if (!_bfd_elf_slurp_version_tables (abfd
,
3712 info
->default_imported_symver
))
3713 goto error_free_sym
;
3715 /* Read in the symbol versions, but don't bother to convert them
3716 to internal format. */
3717 if (elf_dynversym (abfd
) != 0)
3719 Elf_Internal_Shdr
*versymhdr
;
3721 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3722 extversym
= bfd_malloc (versymhdr
->sh_size
);
3723 if (extversym
== NULL
)
3724 goto error_free_sym
;
3725 amt
= versymhdr
->sh_size
;
3726 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3727 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3728 goto error_free_vers
;
3732 /* If we are loading an as-needed shared lib, save the symbol table
3733 state before we start adding symbols. If the lib turns out
3734 to be unneeded, restore the state. */
3735 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
3740 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
3742 struct bfd_hash_entry
*p
;
3743 struct elf_link_hash_entry
*h
;
3745 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3747 h
= (struct elf_link_hash_entry
*) p
;
3748 entsize
+= htab
->root
.table
.entsize
;
3749 if (h
->root
.type
== bfd_link_hash_warning
)
3750 entsize
+= htab
->root
.table
.entsize
;
3754 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
3755 hashsize
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3756 old_tab
= bfd_malloc (tabsize
+ entsize
+ hashsize
);
3757 if (old_tab
== NULL
)
3758 goto error_free_vers
;
3760 /* Remember the current objalloc pointer, so that all mem for
3761 symbols added can later be reclaimed. */
3762 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
3763 if (alloc_mark
== NULL
)
3764 goto error_free_vers
;
3766 /* Make a special call to the linker "notice" function to
3767 tell it that we are about to handle an as-needed lib. */
3768 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
3770 goto error_free_vers
;
3772 /* Clone the symbol table and sym hashes. Remember some
3773 pointers into the symbol table, and dynamic symbol count. */
3774 old_hash
= (char *) old_tab
+ tabsize
;
3775 old_ent
= (char *) old_hash
+ hashsize
;
3776 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
3777 memcpy (old_hash
, sym_hash
, hashsize
);
3778 old_undefs
= htab
->root
.undefs
;
3779 old_undefs_tail
= htab
->root
.undefs_tail
;
3780 old_table
= htab
->root
.table
.table
;
3781 old_size
= htab
->root
.table
.size
;
3782 old_count
= htab
->root
.table
.count
;
3783 old_dynsymcount
= htab
->dynsymcount
;
3785 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
3787 struct bfd_hash_entry
*p
;
3788 struct elf_link_hash_entry
*h
;
3790 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3792 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
3793 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3794 h
= (struct elf_link_hash_entry
*) p
;
3795 if (h
->root
.type
== bfd_link_hash_warning
)
3797 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
3798 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3805 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3806 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3808 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3812 asection
*sec
, *new_sec
;
3815 struct elf_link_hash_entry
*h
;
3816 bfd_boolean definition
;
3817 bfd_boolean size_change_ok
;
3818 bfd_boolean type_change_ok
;
3819 bfd_boolean new_weakdef
;
3820 bfd_boolean override
;
3822 unsigned int old_alignment
;
3827 flags
= BSF_NO_FLAGS
;
3829 value
= isym
->st_value
;
3831 common
= bed
->common_definition (isym
);
3833 bind
= ELF_ST_BIND (isym
->st_info
);
3834 if (bind
== STB_LOCAL
)
3836 /* This should be impossible, since ELF requires that all
3837 global symbols follow all local symbols, and that sh_info
3838 point to the first global symbol. Unfortunately, Irix 5
3842 else if (bind
== STB_GLOBAL
)
3844 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
3847 else if (bind
== STB_WEAK
)
3851 /* Leave it up to the processor backend. */
3854 if (isym
->st_shndx
== SHN_UNDEF
)
3855 sec
= bfd_und_section_ptr
;
3856 else if (isym
->st_shndx
== SHN_ABS
)
3857 sec
= bfd_abs_section_ptr
;
3858 else if (isym
->st_shndx
== SHN_COMMON
)
3860 sec
= bfd_com_section_ptr
;
3861 /* What ELF calls the size we call the value. What ELF
3862 calls the value we call the alignment. */
3863 value
= isym
->st_size
;
3867 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3869 sec
= bfd_abs_section_ptr
;
3870 else if (sec
->kept_section
)
3872 /* Symbols from discarded section are undefined. We keep
3874 sec
= bfd_und_section_ptr
;
3875 isym
->st_shndx
= SHN_UNDEF
;
3877 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3881 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3884 goto error_free_vers
;
3886 if (isym
->st_shndx
== SHN_COMMON
3887 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
3888 && !info
->relocatable
)
3890 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3894 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon",
3897 | SEC_LINKER_CREATED
3898 | SEC_THREAD_LOCAL
));
3900 goto error_free_vers
;
3904 else if (bed
->elf_add_symbol_hook
)
3906 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
3908 goto error_free_vers
;
3910 /* The hook function sets the name to NULL if this symbol
3911 should be skipped for some reason. */
3916 /* Sanity check that all possibilities were handled. */
3919 bfd_set_error (bfd_error_bad_value
);
3920 goto error_free_vers
;
3923 if (bfd_is_und_section (sec
)
3924 || bfd_is_com_section (sec
))
3929 size_change_ok
= FALSE
;
3930 type_change_ok
= bed
->type_change_ok
;
3935 if (is_elf_hash_table (htab
))
3937 Elf_Internal_Versym iver
;
3938 unsigned int vernum
= 0;
3943 if (info
->default_imported_symver
)
3944 /* Use the default symbol version created earlier. */
3945 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
3950 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3952 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3954 /* If this is a hidden symbol, or if it is not version
3955 1, we append the version name to the symbol name.
3956 However, we do not modify a non-hidden absolute symbol
3957 if it is not a function, because it might be the version
3958 symbol itself. FIXME: What if it isn't? */
3959 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3961 && (!bfd_is_abs_section (sec
)
3962 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
3965 size_t namelen
, verlen
, newlen
;
3968 if (isym
->st_shndx
!= SHN_UNDEF
)
3970 if (vernum
> elf_tdata (abfd
)->cverdefs
)
3972 else if (vernum
> 1)
3974 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3980 (*_bfd_error_handler
)
3981 (_("%B: %s: invalid version %u (max %d)"),
3983 elf_tdata (abfd
)->cverdefs
);
3984 bfd_set_error (bfd_error_bad_value
);
3985 goto error_free_vers
;
3990 /* We cannot simply test for the number of
3991 entries in the VERNEED section since the
3992 numbers for the needed versions do not start
3994 Elf_Internal_Verneed
*t
;
3997 for (t
= elf_tdata (abfd
)->verref
;
4001 Elf_Internal_Vernaux
*a
;
4003 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4005 if (a
->vna_other
== vernum
)
4007 verstr
= a
->vna_nodename
;
4016 (*_bfd_error_handler
)
4017 (_("%B: %s: invalid needed version %d"),
4018 abfd
, name
, vernum
);
4019 bfd_set_error (bfd_error_bad_value
);
4020 goto error_free_vers
;
4024 namelen
= strlen (name
);
4025 verlen
= strlen (verstr
);
4026 newlen
= namelen
+ verlen
+ 2;
4027 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4028 && isym
->st_shndx
!= SHN_UNDEF
)
4031 newname
= bfd_hash_allocate (&htab
->root
.table
, newlen
);
4032 if (newname
== NULL
)
4033 goto error_free_vers
;
4034 memcpy (newname
, name
, namelen
);
4035 p
= newname
+ namelen
;
4037 /* If this is a defined non-hidden version symbol,
4038 we add another @ to the name. This indicates the
4039 default version of the symbol. */
4040 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4041 && isym
->st_shndx
!= SHN_UNDEF
)
4043 memcpy (p
, verstr
, verlen
+ 1);
4048 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
,
4049 &value
, &old_alignment
,
4050 sym_hash
, &skip
, &override
,
4051 &type_change_ok
, &size_change_ok
))
4052 goto error_free_vers
;
4061 while (h
->root
.type
== bfd_link_hash_indirect
4062 || h
->root
.type
== bfd_link_hash_warning
)
4063 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4065 /* Remember the old alignment if this is a common symbol, so
4066 that we don't reduce the alignment later on. We can't
4067 check later, because _bfd_generic_link_add_one_symbol
4068 will set a default for the alignment which we want to
4069 override. We also remember the old bfd where the existing
4070 definition comes from. */
4071 switch (h
->root
.type
)
4076 case bfd_link_hash_defined
:
4077 case bfd_link_hash_defweak
:
4078 old_bfd
= h
->root
.u
.def
.section
->owner
;
4081 case bfd_link_hash_common
:
4082 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
4083 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
4087 if (elf_tdata (abfd
)->verdef
!= NULL
4091 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4094 if (! (_bfd_generic_link_add_one_symbol
4095 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4096 (struct bfd_link_hash_entry
**) sym_hash
)))
4097 goto error_free_vers
;
4100 while (h
->root
.type
== bfd_link_hash_indirect
4101 || h
->root
.type
== bfd_link_hash_warning
)
4102 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4105 new_weakdef
= FALSE
;
4108 && (flags
& BSF_WEAK
) != 0
4109 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4110 && is_elf_hash_table (htab
)
4111 && h
->u
.weakdef
== NULL
)
4113 /* Keep a list of all weak defined non function symbols from
4114 a dynamic object, using the weakdef field. Later in this
4115 function we will set the weakdef field to the correct
4116 value. We only put non-function symbols from dynamic
4117 objects on this list, because that happens to be the only
4118 time we need to know the normal symbol corresponding to a
4119 weak symbol, and the information is time consuming to
4120 figure out. If the weakdef field is not already NULL,
4121 then this symbol was already defined by some previous
4122 dynamic object, and we will be using that previous
4123 definition anyhow. */
4125 h
->u
.weakdef
= weaks
;
4130 /* Set the alignment of a common symbol. */
4131 if ((common
|| bfd_is_com_section (sec
))
4132 && h
->root
.type
== bfd_link_hash_common
)
4137 align
= bfd_log2 (isym
->st_value
);
4140 /* The new symbol is a common symbol in a shared object.
4141 We need to get the alignment from the section. */
4142 align
= new_sec
->alignment_power
;
4144 if (align
> old_alignment
4145 /* Permit an alignment power of zero if an alignment of one
4146 is specified and no other alignments have been specified. */
4147 || (isym
->st_value
== 1 && old_alignment
== 0))
4148 h
->root
.u
.c
.p
->alignment_power
= align
;
4150 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4153 if (is_elf_hash_table (htab
))
4157 /* Check the alignment when a common symbol is involved. This
4158 can change when a common symbol is overridden by a normal
4159 definition or a common symbol is ignored due to the old
4160 normal definition. We need to make sure the maximum
4161 alignment is maintained. */
4162 if ((old_alignment
|| common
)
4163 && h
->root
.type
!= bfd_link_hash_common
)
4165 unsigned int common_align
;
4166 unsigned int normal_align
;
4167 unsigned int symbol_align
;
4171 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4172 if (h
->root
.u
.def
.section
->owner
!= NULL
4173 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
4175 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4176 if (normal_align
> symbol_align
)
4177 normal_align
= symbol_align
;
4180 normal_align
= symbol_align
;
4184 common_align
= old_alignment
;
4185 common_bfd
= old_bfd
;
4190 common_align
= bfd_log2 (isym
->st_value
);
4192 normal_bfd
= old_bfd
;
4195 if (normal_align
< common_align
)
4197 /* PR binutils/2735 */
4198 if (normal_bfd
== NULL
)
4199 (*_bfd_error_handler
)
4200 (_("Warning: alignment %u of common symbol `%s' in %B"
4201 " is greater than the alignment (%u) of its section %A"),
4202 common_bfd
, h
->root
.u
.def
.section
,
4203 1 << common_align
, name
, 1 << normal_align
);
4205 (*_bfd_error_handler
)
4206 (_("Warning: alignment %u of symbol `%s' in %B"
4207 " is smaller than %u in %B"),
4208 normal_bfd
, common_bfd
,
4209 1 << normal_align
, name
, 1 << common_align
);
4213 /* Remember the symbol size if it isn't undefined. */
4214 if ((isym
->st_size
!= 0 && isym
->st_shndx
!= SHN_UNDEF
)
4215 && (definition
|| h
->size
== 0))
4218 && h
->size
!= isym
->st_size
4219 && ! size_change_ok
)
4220 (*_bfd_error_handler
)
4221 (_("Warning: size of symbol `%s' changed"
4222 " from %lu in %B to %lu in %B"),
4224 name
, (unsigned long) h
->size
,
4225 (unsigned long) isym
->st_size
);
4227 h
->size
= isym
->st_size
;
4230 /* If this is a common symbol, then we always want H->SIZE
4231 to be the size of the common symbol. The code just above
4232 won't fix the size if a common symbol becomes larger. We
4233 don't warn about a size change here, because that is
4234 covered by --warn-common. Allow changed between different
4236 if (h
->root
.type
== bfd_link_hash_common
)
4237 h
->size
= h
->root
.u
.c
.size
;
4239 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4240 && (definition
|| h
->type
== STT_NOTYPE
))
4242 if (h
->type
!= STT_NOTYPE
4243 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
4244 && ! type_change_ok
)
4245 (*_bfd_error_handler
)
4246 (_("Warning: type of symbol `%s' changed"
4247 " from %d to %d in %B"),
4248 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
4250 h
->type
= ELF_ST_TYPE (isym
->st_info
);
4253 /* If st_other has a processor-specific meaning, specific
4254 code might be needed here. We never merge the visibility
4255 attribute with the one from a dynamic object. */
4256 if (bed
->elf_backend_merge_symbol_attribute
)
4257 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
4260 /* If this symbol has default visibility and the user has requested
4261 we not re-export it, then mark it as hidden. */
4262 if (definition
&& !dynamic
4264 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
4265 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4266 isym
->st_other
= (STV_HIDDEN
4267 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4269 if (ELF_ST_VISIBILITY (isym
->st_other
) != 0 && !dynamic
)
4271 unsigned char hvis
, symvis
, other
, nvis
;
4273 /* Only merge the visibility. Leave the remainder of the
4274 st_other field to elf_backend_merge_symbol_attribute. */
4275 other
= h
->other
& ~ELF_ST_VISIBILITY (-1);
4277 /* Combine visibilities, using the most constraining one. */
4278 hvis
= ELF_ST_VISIBILITY (h
->other
);
4279 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
4285 nvis
= hvis
< symvis
? hvis
: symvis
;
4287 h
->other
= other
| nvis
;
4290 /* Set a flag in the hash table entry indicating the type of
4291 reference or definition we just found. Keep a count of
4292 the number of dynamic symbols we find. A dynamic symbol
4293 is one which is referenced or defined by both a regular
4294 object and a shared object. */
4301 if (bind
!= STB_WEAK
)
4302 h
->ref_regular_nonweak
= 1;
4306 if (! info
->executable
4319 || (h
->u
.weakdef
!= NULL
4321 && h
->u
.weakdef
->dynindx
!= -1))
4325 if (definition
&& (sec
->flags
& SEC_DEBUGGING
) && !info
->relocatable
)
4327 /* We don't want to make debug symbol dynamic. */
4328 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4332 /* Check to see if we need to add an indirect symbol for
4333 the default name. */
4334 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
4335 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4336 &sec
, &value
, &dynsym
,
4338 goto error_free_vers
;
4340 if (definition
&& !dynamic
)
4342 char *p
= strchr (name
, ELF_VER_CHR
);
4343 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4345 /* Queue non-default versions so that .symver x, x@FOO
4346 aliases can be checked. */
4349 amt
= ((isymend
- isym
+ 1)
4350 * sizeof (struct elf_link_hash_entry
*));
4351 nondeflt_vers
= bfd_malloc (amt
);
4353 goto error_free_vers
;
4355 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4359 if (dynsym
&& h
->dynindx
== -1)
4361 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4362 goto error_free_vers
;
4363 if (h
->u
.weakdef
!= NULL
4365 && h
->u
.weakdef
->dynindx
== -1)
4367 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4368 goto error_free_vers
;
4371 else if (dynsym
&& h
->dynindx
!= -1)
4372 /* If the symbol already has a dynamic index, but
4373 visibility says it should not be visible, turn it into
4375 switch (ELF_ST_VISIBILITY (h
->other
))
4379 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4390 const char *soname
= elf_dt_name (abfd
);
4392 /* A symbol from a library loaded via DT_NEEDED of some
4393 other library is referenced by a regular object.
4394 Add a DT_NEEDED entry for it. Issue an error if
4395 --no-add-needed is used. */
4396 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4398 (*_bfd_error_handler
)
4399 (_("%s: invalid DSO for symbol `%s' definition"),
4401 bfd_set_error (bfd_error_bad_value
);
4402 goto error_free_vers
;
4405 elf_dyn_lib_class (abfd
) &= ~DYN_AS_NEEDED
;
4408 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4410 goto error_free_vers
;
4412 BFD_ASSERT (ret
== 0);
4417 if (extversym
!= NULL
)
4423 if (isymbuf
!= NULL
)
4429 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4433 /* Restore the symbol table. */
4434 if (bed
->as_needed_cleanup
)
4435 (*bed
->as_needed_cleanup
) (abfd
, info
);
4436 old_hash
= (char *) old_tab
+ tabsize
;
4437 old_ent
= (char *) old_hash
+ hashsize
;
4438 sym_hash
= elf_sym_hashes (abfd
);
4439 htab
->root
.table
.table
= old_table
;
4440 htab
->root
.table
.size
= old_size
;
4441 htab
->root
.table
.count
= old_count
;
4442 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4443 memcpy (sym_hash
, old_hash
, hashsize
);
4444 htab
->root
.undefs
= old_undefs
;
4445 htab
->root
.undefs_tail
= old_undefs_tail
;
4446 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4448 struct bfd_hash_entry
*p
;
4449 struct elf_link_hash_entry
*h
;
4451 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4453 h
= (struct elf_link_hash_entry
*) p
;
4454 if (h
->root
.type
== bfd_link_hash_warning
)
4455 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4456 if (h
->dynindx
>= old_dynsymcount
)
4457 _bfd_elf_strtab_delref (htab
->dynstr
, h
->dynstr_index
);
4459 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4460 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4461 h
= (struct elf_link_hash_entry
*) p
;
4462 if (h
->root
.type
== bfd_link_hash_warning
)
4464 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4465 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4470 /* Make a special call to the linker "notice" function to
4471 tell it that symbols added for crefs may need to be removed. */
4472 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
4474 goto error_free_vers
;
4477 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4479 if (nondeflt_vers
!= NULL
)
4480 free (nondeflt_vers
);
4484 if (old_tab
!= NULL
)
4486 if (!(*info
->callbacks
->notice
) (info
, NULL
, abfd
, NULL
,
4488 goto error_free_vers
;
4493 /* Now that all the symbols from this input file are created, handle
4494 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4495 if (nondeflt_vers
!= NULL
)
4497 bfd_size_type cnt
, symidx
;
4499 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4501 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4502 char *shortname
, *p
;
4504 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4506 || (h
->root
.type
!= bfd_link_hash_defined
4507 && h
->root
.type
!= bfd_link_hash_defweak
))
4510 amt
= p
- h
->root
.root
.string
;
4511 shortname
= bfd_malloc (amt
+ 1);
4513 goto error_free_vers
;
4514 memcpy (shortname
, h
->root
.root
.string
, amt
);
4515 shortname
[amt
] = '\0';
4517 hi
= (struct elf_link_hash_entry
*)
4518 bfd_link_hash_lookup (&htab
->root
, shortname
,
4519 FALSE
, FALSE
, FALSE
);
4521 && hi
->root
.type
== h
->root
.type
4522 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4523 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4525 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4526 hi
->root
.type
= bfd_link_hash_indirect
;
4527 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4528 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4529 sym_hash
= elf_sym_hashes (abfd
);
4531 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4532 if (sym_hash
[symidx
] == hi
)
4534 sym_hash
[symidx
] = h
;
4540 free (nondeflt_vers
);
4541 nondeflt_vers
= NULL
;
4544 /* Now set the weakdefs field correctly for all the weak defined
4545 symbols we found. The only way to do this is to search all the
4546 symbols. Since we only need the information for non functions in
4547 dynamic objects, that's the only time we actually put anything on
4548 the list WEAKS. We need this information so that if a regular
4549 object refers to a symbol defined weakly in a dynamic object, the
4550 real symbol in the dynamic object is also put in the dynamic
4551 symbols; we also must arrange for both symbols to point to the
4552 same memory location. We could handle the general case of symbol
4553 aliasing, but a general symbol alias can only be generated in
4554 assembler code, handling it correctly would be very time
4555 consuming, and other ELF linkers don't handle general aliasing
4559 struct elf_link_hash_entry
**hpp
;
4560 struct elf_link_hash_entry
**hppend
;
4561 struct elf_link_hash_entry
**sorted_sym_hash
;
4562 struct elf_link_hash_entry
*h
;
4565 /* Since we have to search the whole symbol list for each weak
4566 defined symbol, search time for N weak defined symbols will be
4567 O(N^2). Binary search will cut it down to O(NlogN). */
4568 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4569 sorted_sym_hash
= bfd_malloc (amt
);
4570 if (sorted_sym_hash
== NULL
)
4572 sym_hash
= sorted_sym_hash
;
4573 hpp
= elf_sym_hashes (abfd
);
4574 hppend
= hpp
+ extsymcount
;
4576 for (; hpp
< hppend
; hpp
++)
4580 && h
->root
.type
== bfd_link_hash_defined
4581 && !bed
->is_function_type (h
->type
))
4589 qsort (sorted_sym_hash
, sym_count
,
4590 sizeof (struct elf_link_hash_entry
*),
4593 while (weaks
!= NULL
)
4595 struct elf_link_hash_entry
*hlook
;
4602 weaks
= hlook
->u
.weakdef
;
4603 hlook
->u
.weakdef
= NULL
;
4605 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4606 || hlook
->root
.type
== bfd_link_hash_defweak
4607 || hlook
->root
.type
== bfd_link_hash_common
4608 || hlook
->root
.type
== bfd_link_hash_indirect
);
4609 slook
= hlook
->root
.u
.def
.section
;
4610 vlook
= hlook
->root
.u
.def
.value
;
4617 bfd_signed_vma vdiff
;
4619 h
= sorted_sym_hash
[idx
];
4620 vdiff
= vlook
- h
->root
.u
.def
.value
;
4627 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4640 /* We didn't find a value/section match. */
4644 for (i
= ilook
; i
< sym_count
; i
++)
4646 h
= sorted_sym_hash
[i
];
4648 /* Stop if value or section doesn't match. */
4649 if (h
->root
.u
.def
.value
!= vlook
4650 || h
->root
.u
.def
.section
!= slook
)
4652 else if (h
!= hlook
)
4654 hlook
->u
.weakdef
= h
;
4656 /* If the weak definition is in the list of dynamic
4657 symbols, make sure the real definition is put
4659 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4661 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4664 free (sorted_sym_hash
);
4669 /* If the real definition is in the list of dynamic
4670 symbols, make sure the weak definition is put
4671 there as well. If we don't do this, then the
4672 dynamic loader might not merge the entries for the
4673 real definition and the weak definition. */
4674 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4676 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4677 goto err_free_sym_hash
;
4684 free (sorted_sym_hash
);
4687 if (bed
->check_directives
4688 && !(*bed
->check_directives
) (abfd
, info
))
4691 /* If this object is the same format as the output object, and it is
4692 not a shared library, then let the backend look through the
4695 This is required to build global offset table entries and to
4696 arrange for dynamic relocs. It is not required for the
4697 particular common case of linking non PIC code, even when linking
4698 against shared libraries, but unfortunately there is no way of
4699 knowing whether an object file has been compiled PIC or not.
4700 Looking through the relocs is not particularly time consuming.
4701 The problem is that we must either (1) keep the relocs in memory,
4702 which causes the linker to require additional runtime memory or
4703 (2) read the relocs twice from the input file, which wastes time.
4704 This would be a good case for using mmap.
4706 I have no idea how to handle linking PIC code into a file of a
4707 different format. It probably can't be done. */
4709 && is_elf_hash_table (htab
)
4710 && bed
->check_relocs
!= NULL
4711 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
4715 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4717 Elf_Internal_Rela
*internal_relocs
;
4720 if ((o
->flags
& SEC_RELOC
) == 0
4721 || o
->reloc_count
== 0
4722 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4723 && (o
->flags
& SEC_DEBUGGING
) != 0)
4724 || bfd_is_abs_section (o
->output_section
))
4727 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4729 if (internal_relocs
== NULL
)
4732 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
4734 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4735 free (internal_relocs
);
4742 /* If this is a non-traditional link, try to optimize the handling
4743 of the .stab/.stabstr sections. */
4745 && ! info
->traditional_format
4746 && is_elf_hash_table (htab
)
4747 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4751 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4752 if (stabstr
!= NULL
)
4754 bfd_size_type string_offset
= 0;
4757 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4758 if (CONST_STRNEQ (stab
->name
, ".stab")
4759 && (!stab
->name
[5] ||
4760 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4761 && (stab
->flags
& SEC_MERGE
) == 0
4762 && !bfd_is_abs_section (stab
->output_section
))
4764 struct bfd_elf_section_data
*secdata
;
4766 secdata
= elf_section_data (stab
);
4767 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
4768 stabstr
, &secdata
->sec_info
,
4771 if (secdata
->sec_info
)
4772 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4777 if (is_elf_hash_table (htab
) && add_needed
)
4779 /* Add this bfd to the loaded list. */
4780 struct elf_link_loaded_list
*n
;
4782 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4786 n
->next
= htab
->loaded
;
4793 if (old_tab
!= NULL
)
4795 if (nondeflt_vers
!= NULL
)
4796 free (nondeflt_vers
);
4797 if (extversym
!= NULL
)
4800 if (isymbuf
!= NULL
)
4806 /* Return the linker hash table entry of a symbol that might be
4807 satisfied by an archive symbol. Return -1 on error. */
4809 struct elf_link_hash_entry
*
4810 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4811 struct bfd_link_info
*info
,
4814 struct elf_link_hash_entry
*h
;
4818 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4822 /* If this is a default version (the name contains @@), look up the
4823 symbol again with only one `@' as well as without the version.
4824 The effect is that references to the symbol with and without the
4825 version will be matched by the default symbol in the archive. */
4827 p
= strchr (name
, ELF_VER_CHR
);
4828 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4831 /* First check with only one `@'. */
4832 len
= strlen (name
);
4833 copy
= bfd_alloc (abfd
, len
);
4835 return (struct elf_link_hash_entry
*) 0 - 1;
4837 first
= p
- name
+ 1;
4838 memcpy (copy
, name
, first
);
4839 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4841 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4844 /* We also need to check references to the symbol without the
4846 copy
[first
- 1] = '\0';
4847 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4848 FALSE
, FALSE
, FALSE
);
4851 bfd_release (abfd
, copy
);
4855 /* Add symbols from an ELF archive file to the linker hash table. We
4856 don't use _bfd_generic_link_add_archive_symbols because of a
4857 problem which arises on UnixWare. The UnixWare libc.so is an
4858 archive which includes an entry libc.so.1 which defines a bunch of
4859 symbols. The libc.so archive also includes a number of other
4860 object files, which also define symbols, some of which are the same
4861 as those defined in libc.so.1. Correct linking requires that we
4862 consider each object file in turn, and include it if it defines any
4863 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4864 this; it looks through the list of undefined symbols, and includes
4865 any object file which defines them. When this algorithm is used on
4866 UnixWare, it winds up pulling in libc.so.1 early and defining a
4867 bunch of symbols. This means that some of the other objects in the
4868 archive are not included in the link, which is incorrect since they
4869 precede libc.so.1 in the archive.
4871 Fortunately, ELF archive handling is simpler than that done by
4872 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4873 oddities. In ELF, if we find a symbol in the archive map, and the
4874 symbol is currently undefined, we know that we must pull in that
4877 Unfortunately, we do have to make multiple passes over the symbol
4878 table until nothing further is resolved. */
4881 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4884 bfd_boolean
*defined
= NULL
;
4885 bfd_boolean
*included
= NULL
;
4889 const struct elf_backend_data
*bed
;
4890 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4891 (bfd
*, struct bfd_link_info
*, const char *);
4893 if (! bfd_has_map (abfd
))
4895 /* An empty archive is a special case. */
4896 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4898 bfd_set_error (bfd_error_no_armap
);
4902 /* Keep track of all symbols we know to be already defined, and all
4903 files we know to be already included. This is to speed up the
4904 second and subsequent passes. */
4905 c
= bfd_ardata (abfd
)->symdef_count
;
4909 amt
*= sizeof (bfd_boolean
);
4910 defined
= bfd_zmalloc (amt
);
4911 included
= bfd_zmalloc (amt
);
4912 if (defined
== NULL
|| included
== NULL
)
4915 symdefs
= bfd_ardata (abfd
)->symdefs
;
4916 bed
= get_elf_backend_data (abfd
);
4917 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4930 symdefend
= symdef
+ c
;
4931 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4933 struct elf_link_hash_entry
*h
;
4935 struct bfd_link_hash_entry
*undefs_tail
;
4938 if (defined
[i
] || included
[i
])
4940 if (symdef
->file_offset
== last
)
4946 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4947 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4953 if (h
->root
.type
== bfd_link_hash_common
)
4955 /* We currently have a common symbol. The archive map contains
4956 a reference to this symbol, so we may want to include it. We
4957 only want to include it however, if this archive element
4958 contains a definition of the symbol, not just another common
4961 Unfortunately some archivers (including GNU ar) will put
4962 declarations of common symbols into their archive maps, as
4963 well as real definitions, so we cannot just go by the archive
4964 map alone. Instead we must read in the element's symbol
4965 table and check that to see what kind of symbol definition
4967 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4970 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4972 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4977 /* We need to include this archive member. */
4978 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4979 if (element
== NULL
)
4982 if (! bfd_check_format (element
, bfd_object
))
4985 /* Doublecheck that we have not included this object
4986 already--it should be impossible, but there may be
4987 something wrong with the archive. */
4988 if (element
->archive_pass
!= 0)
4990 bfd_set_error (bfd_error_bad_value
);
4993 element
->archive_pass
= 1;
4995 undefs_tail
= info
->hash
->undefs_tail
;
4997 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
5000 if (! bfd_link_add_symbols (element
, info
))
5003 /* If there are any new undefined symbols, we need to make
5004 another pass through the archive in order to see whether
5005 they can be defined. FIXME: This isn't perfect, because
5006 common symbols wind up on undefs_tail and because an
5007 undefined symbol which is defined later on in this pass
5008 does not require another pass. This isn't a bug, but it
5009 does make the code less efficient than it could be. */
5010 if (undefs_tail
!= info
->hash
->undefs_tail
)
5013 /* Look backward to mark all symbols from this object file
5014 which we have already seen in this pass. */
5018 included
[mark
] = TRUE
;
5023 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5025 /* We mark subsequent symbols from this object file as we go
5026 on through the loop. */
5027 last
= symdef
->file_offset
;
5038 if (defined
!= NULL
)
5040 if (included
!= NULL
)
5045 /* Given an ELF BFD, add symbols to the global hash table as
5049 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5051 switch (bfd_get_format (abfd
))
5054 return elf_link_add_object_symbols (abfd
, info
);
5056 return elf_link_add_archive_symbols (abfd
, info
);
5058 bfd_set_error (bfd_error_wrong_format
);
5063 struct hash_codes_info
5065 unsigned long *hashcodes
;
5069 /* This function will be called though elf_link_hash_traverse to store
5070 all hash value of the exported symbols in an array. */
5073 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5075 struct hash_codes_info
*inf
= data
;
5081 if (h
->root
.type
== bfd_link_hash_warning
)
5082 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5084 /* Ignore indirect symbols. These are added by the versioning code. */
5085 if (h
->dynindx
== -1)
5088 name
= h
->root
.root
.string
;
5089 p
= strchr (name
, ELF_VER_CHR
);
5092 alc
= bfd_malloc (p
- name
+ 1);
5098 memcpy (alc
, name
, p
- name
);
5099 alc
[p
- name
] = '\0';
5103 /* Compute the hash value. */
5104 ha
= bfd_elf_hash (name
);
5106 /* Store the found hash value in the array given as the argument. */
5107 *(inf
->hashcodes
)++ = ha
;
5109 /* And store it in the struct so that we can put it in the hash table
5111 h
->u
.elf_hash_value
= ha
;
5119 struct collect_gnu_hash_codes
5122 const struct elf_backend_data
*bed
;
5123 unsigned long int nsyms
;
5124 unsigned long int maskbits
;
5125 unsigned long int *hashcodes
;
5126 unsigned long int *hashval
;
5127 unsigned long int *indx
;
5128 unsigned long int *counts
;
5131 long int min_dynindx
;
5132 unsigned long int bucketcount
;
5133 unsigned long int symindx
;
5134 long int local_indx
;
5135 long int shift1
, shift2
;
5136 unsigned long int mask
;
5140 /* This function will be called though elf_link_hash_traverse to store
5141 all hash value of the exported symbols in an array. */
5144 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5146 struct collect_gnu_hash_codes
*s
= data
;
5152 if (h
->root
.type
== bfd_link_hash_warning
)
5153 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5155 /* Ignore indirect symbols. These are added by the versioning code. */
5156 if (h
->dynindx
== -1)
5159 /* Ignore also local symbols and undefined symbols. */
5160 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5163 name
= h
->root
.root
.string
;
5164 p
= strchr (name
, ELF_VER_CHR
);
5167 alc
= bfd_malloc (p
- name
+ 1);
5173 memcpy (alc
, name
, p
- name
);
5174 alc
[p
- name
] = '\0';
5178 /* Compute the hash value. */
5179 ha
= bfd_elf_gnu_hash (name
);
5181 /* Store the found hash value in the array for compute_bucket_count,
5182 and also for .dynsym reordering purposes. */
5183 s
->hashcodes
[s
->nsyms
] = ha
;
5184 s
->hashval
[h
->dynindx
] = ha
;
5186 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5187 s
->min_dynindx
= h
->dynindx
;
5195 /* This function will be called though elf_link_hash_traverse to do
5196 final dynaminc symbol renumbering. */
5199 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5201 struct collect_gnu_hash_codes
*s
= data
;
5202 unsigned long int bucket
;
5203 unsigned long int val
;
5205 if (h
->root
.type
== bfd_link_hash_warning
)
5206 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5208 /* Ignore indirect symbols. */
5209 if (h
->dynindx
== -1)
5212 /* Ignore also local symbols and undefined symbols. */
5213 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5215 if (h
->dynindx
>= s
->min_dynindx
)
5216 h
->dynindx
= s
->local_indx
++;
5220 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5221 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5222 & ((s
->maskbits
>> s
->shift1
) - 1);
5223 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5225 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5226 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5227 if (s
->counts
[bucket
] == 1)
5228 /* Last element terminates the chain. */
5230 bfd_put_32 (s
->output_bfd
, val
,
5231 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5232 --s
->counts
[bucket
];
5233 h
->dynindx
= s
->indx
[bucket
]++;
5237 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5240 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5242 return !(h
->forced_local
5243 || h
->root
.type
== bfd_link_hash_undefined
5244 || h
->root
.type
== bfd_link_hash_undefweak
5245 || ((h
->root
.type
== bfd_link_hash_defined
5246 || h
->root
.type
== bfd_link_hash_defweak
)
5247 && h
->root
.u
.def
.section
->output_section
== NULL
));
5250 /* Array used to determine the number of hash table buckets to use
5251 based on the number of symbols there are. If there are fewer than
5252 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5253 fewer than 37 we use 17 buckets, and so forth. We never use more
5254 than 32771 buckets. */
5256 static const size_t elf_buckets
[] =
5258 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5262 /* Compute bucket count for hashing table. We do not use a static set
5263 of possible tables sizes anymore. Instead we determine for all
5264 possible reasonable sizes of the table the outcome (i.e., the
5265 number of collisions etc) and choose the best solution. The
5266 weighting functions are not too simple to allow the table to grow
5267 without bounds. Instead one of the weighting factors is the size.
5268 Therefore the result is always a good payoff between few collisions
5269 (= short chain lengths) and table size. */
5271 compute_bucket_count (struct bfd_link_info
*info
,
5272 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
5273 unsigned long int nsyms
,
5276 size_t best_size
= 0;
5277 unsigned long int i
;
5279 /* We have a problem here. The following code to optimize the table
5280 size requires an integer type with more the 32 bits. If
5281 BFD_HOST_U_64_BIT is set we know about such a type. */
5282 #ifdef BFD_HOST_U_64_BIT
5287 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5288 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5289 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5290 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5291 unsigned long int *counts
;
5294 /* Possible optimization parameters: if we have NSYMS symbols we say
5295 that the hashing table must at least have NSYMS/4 and at most
5297 minsize
= nsyms
/ 4;
5300 best_size
= maxsize
= nsyms
* 2;
5305 if ((best_size
& 31) == 0)
5309 /* Create array where we count the collisions in. We must use bfd_malloc
5310 since the size could be large. */
5312 amt
*= sizeof (unsigned long int);
5313 counts
= bfd_malloc (amt
);
5317 /* Compute the "optimal" size for the hash table. The criteria is a
5318 minimal chain length. The minor criteria is (of course) the size
5320 for (i
= minsize
; i
< maxsize
; ++i
)
5322 /* Walk through the array of hashcodes and count the collisions. */
5323 BFD_HOST_U_64_BIT max
;
5324 unsigned long int j
;
5325 unsigned long int fact
;
5327 if (gnu_hash
&& (i
& 31) == 0)
5330 memset (counts
, '\0', i
* sizeof (unsigned long int));
5332 /* Determine how often each hash bucket is used. */
5333 for (j
= 0; j
< nsyms
; ++j
)
5334 ++counts
[hashcodes
[j
] % i
];
5336 /* For the weight function we need some information about the
5337 pagesize on the target. This is information need not be 100%
5338 accurate. Since this information is not available (so far) we
5339 define it here to a reasonable default value. If it is crucial
5340 to have a better value some day simply define this value. */
5341 # ifndef BFD_TARGET_PAGESIZE
5342 # define BFD_TARGET_PAGESIZE (4096)
5345 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5347 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5350 /* Variant 1: optimize for short chains. We add the squares
5351 of all the chain lengths (which favors many small chain
5352 over a few long chains). */
5353 for (j
= 0; j
< i
; ++j
)
5354 max
+= counts
[j
] * counts
[j
];
5356 /* This adds penalties for the overall size of the table. */
5357 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5360 /* Variant 2: Optimize a lot more for small table. Here we
5361 also add squares of the size but we also add penalties for
5362 empty slots (the +1 term). */
5363 for (j
= 0; j
< i
; ++j
)
5364 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5366 /* The overall size of the table is considered, but not as
5367 strong as in variant 1, where it is squared. */
5368 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5372 /* Compare with current best results. */
5373 if (max
< best_chlen
)
5383 #endif /* defined (BFD_HOST_U_64_BIT) */
5385 /* This is the fallback solution if no 64bit type is available or if we
5386 are not supposed to spend much time on optimizations. We select the
5387 bucket count using a fixed set of numbers. */
5388 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5390 best_size
= elf_buckets
[i
];
5391 if (nsyms
< elf_buckets
[i
+ 1])
5394 if (gnu_hash
&& best_size
< 2)
5401 /* Set up the sizes and contents of the ELF dynamic sections. This is
5402 called by the ELF linker emulation before_allocation routine. We
5403 must set the sizes of the sections before the linker sets the
5404 addresses of the various sections. */
5407 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5410 const char *filter_shlib
,
5411 const char * const *auxiliary_filters
,
5412 struct bfd_link_info
*info
,
5413 asection
**sinterpptr
,
5414 struct bfd_elf_version_tree
*verdefs
)
5416 bfd_size_type soname_indx
;
5418 const struct elf_backend_data
*bed
;
5419 struct elf_assign_sym_version_info asvinfo
;
5423 soname_indx
= (bfd_size_type
) -1;
5425 if (!is_elf_hash_table (info
->hash
))
5428 bed
= get_elf_backend_data (output_bfd
);
5429 if (info
->execstack
)
5430 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
5431 else if (info
->noexecstack
)
5432 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
5436 asection
*notesec
= NULL
;
5439 for (inputobj
= info
->input_bfds
;
5441 inputobj
= inputobj
->link_next
)
5445 if (inputobj
->flags
& (DYNAMIC
| EXEC_P
| BFD_LINKER_CREATED
))
5447 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
5450 if (s
->flags
& SEC_CODE
)
5454 else if (bed
->default_execstack
)
5459 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
5460 if (exec
&& info
->relocatable
5461 && notesec
->output_section
!= bfd_abs_section_ptr
)
5462 notesec
->output_section
->flags
|= SEC_CODE
;
5466 /* Any syms created from now on start with -1 in
5467 got.refcount/offset and plt.refcount/offset. */
5468 elf_hash_table (info
)->init_got_refcount
5469 = elf_hash_table (info
)->init_got_offset
;
5470 elf_hash_table (info
)->init_plt_refcount
5471 = elf_hash_table (info
)->init_plt_offset
;
5473 /* The backend may have to create some sections regardless of whether
5474 we're dynamic or not. */
5475 if (bed
->elf_backend_always_size_sections
5476 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5479 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
5482 dynobj
= elf_hash_table (info
)->dynobj
;
5484 /* If there were no dynamic objects in the link, there is nothing to
5489 if (elf_hash_table (info
)->dynamic_sections_created
)
5491 struct elf_info_failed eif
;
5492 struct elf_link_hash_entry
*h
;
5494 struct bfd_elf_version_tree
*t
;
5495 struct bfd_elf_version_expr
*d
;
5497 bfd_boolean all_defined
;
5499 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
5500 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
5504 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5506 if (soname_indx
== (bfd_size_type
) -1
5507 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5513 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5515 info
->flags
|= DF_SYMBOLIC
;
5522 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5524 if (indx
== (bfd_size_type
) -1
5525 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
5528 if (info
->new_dtags
)
5530 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
5531 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
5536 if (filter_shlib
!= NULL
)
5540 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5541 filter_shlib
, TRUE
);
5542 if (indx
== (bfd_size_type
) -1
5543 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5547 if (auxiliary_filters
!= NULL
)
5549 const char * const *p
;
5551 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5555 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5557 if (indx
== (bfd_size_type
) -1
5558 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5564 eif
.verdefs
= verdefs
;
5567 /* If we are supposed to export all symbols into the dynamic symbol
5568 table (this is not the normal case), then do so. */
5569 if (info
->export_dynamic
5570 || (info
->executable
&& info
->dynamic
))
5572 elf_link_hash_traverse (elf_hash_table (info
),
5573 _bfd_elf_export_symbol
,
5579 /* Make all global versions with definition. */
5580 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5581 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5582 if (!d
->symver
&& d
->literal
)
5584 const char *verstr
, *name
;
5585 size_t namelen
, verlen
, newlen
;
5587 struct elf_link_hash_entry
*newh
;
5590 namelen
= strlen (name
);
5592 verlen
= strlen (verstr
);
5593 newlen
= namelen
+ verlen
+ 3;
5595 newname
= bfd_malloc (newlen
);
5596 if (newname
== NULL
)
5598 memcpy (newname
, name
, namelen
);
5600 /* Check the hidden versioned definition. */
5601 p
= newname
+ namelen
;
5603 memcpy (p
, verstr
, verlen
+ 1);
5604 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5605 newname
, FALSE
, FALSE
,
5608 || (newh
->root
.type
!= bfd_link_hash_defined
5609 && newh
->root
.type
!= bfd_link_hash_defweak
))
5611 /* Check the default versioned definition. */
5613 memcpy (p
, verstr
, verlen
+ 1);
5614 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5615 newname
, FALSE
, FALSE
,
5620 /* Mark this version if there is a definition and it is
5621 not defined in a shared object. */
5623 && !newh
->def_dynamic
5624 && (newh
->root
.type
== bfd_link_hash_defined
5625 || newh
->root
.type
== bfd_link_hash_defweak
))
5629 /* Attach all the symbols to their version information. */
5630 asvinfo
.output_bfd
= output_bfd
;
5631 asvinfo
.info
= info
;
5632 asvinfo
.verdefs
= verdefs
;
5633 asvinfo
.failed
= FALSE
;
5635 elf_link_hash_traverse (elf_hash_table (info
),
5636 _bfd_elf_link_assign_sym_version
,
5641 if (!info
->allow_undefined_version
)
5643 /* Check if all global versions have a definition. */
5645 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5646 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5647 if (d
->literal
&& !d
->symver
&& !d
->script
)
5649 (*_bfd_error_handler
)
5650 (_("%s: undefined version: %s"),
5651 d
->pattern
, t
->name
);
5652 all_defined
= FALSE
;
5657 bfd_set_error (bfd_error_bad_value
);
5662 /* Find all symbols which were defined in a dynamic object and make
5663 the backend pick a reasonable value for them. */
5664 elf_link_hash_traverse (elf_hash_table (info
),
5665 _bfd_elf_adjust_dynamic_symbol
,
5670 /* Add some entries to the .dynamic section. We fill in some of the
5671 values later, in bfd_elf_final_link, but we must add the entries
5672 now so that we know the final size of the .dynamic section. */
5674 /* If there are initialization and/or finalization functions to
5675 call then add the corresponding DT_INIT/DT_FINI entries. */
5676 h
= (info
->init_function
5677 ? elf_link_hash_lookup (elf_hash_table (info
),
5678 info
->init_function
, FALSE
,
5685 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5688 h
= (info
->fini_function
5689 ? elf_link_hash_lookup (elf_hash_table (info
),
5690 info
->fini_function
, FALSE
,
5697 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5701 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
5702 if (s
!= NULL
&& s
->linker_has_input
)
5704 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5705 if (! info
->executable
)
5710 for (sub
= info
->input_bfds
; sub
!= NULL
;
5711 sub
= sub
->link_next
)
5712 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
5713 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5714 if (elf_section_data (o
)->this_hdr
.sh_type
5715 == SHT_PREINIT_ARRAY
)
5717 (*_bfd_error_handler
)
5718 (_("%B: .preinit_array section is not allowed in DSO"),
5723 bfd_set_error (bfd_error_nonrepresentable_section
);
5727 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5728 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5731 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
5732 if (s
!= NULL
&& s
->linker_has_input
)
5734 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5735 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5738 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
5739 if (s
!= NULL
&& s
->linker_has_input
)
5741 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5742 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5746 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5747 /* If .dynstr is excluded from the link, we don't want any of
5748 these tags. Strictly, we should be checking each section
5749 individually; This quick check covers for the case where
5750 someone does a /DISCARD/ : { *(*) }. */
5751 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5753 bfd_size_type strsize
;
5755 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5756 if ((info
->emit_hash
5757 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
5758 || (info
->emit_gnu_hash
5759 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
5760 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5761 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5762 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5763 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5764 bed
->s
->sizeof_sym
))
5769 /* The backend must work out the sizes of all the other dynamic
5771 if (bed
->elf_backend_size_dynamic_sections
5772 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5775 if (elf_hash_table (info
)->dynamic_sections_created
)
5777 unsigned long section_sym_count
;
5780 /* Set up the version definition section. */
5781 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5782 BFD_ASSERT (s
!= NULL
);
5784 /* We may have created additional version definitions if we are
5785 just linking a regular application. */
5786 verdefs
= asvinfo
.verdefs
;
5788 /* Skip anonymous version tag. */
5789 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5790 verdefs
= verdefs
->next
;
5792 if (verdefs
== NULL
&& !info
->create_default_symver
)
5793 s
->flags
|= SEC_EXCLUDE
;
5798 struct bfd_elf_version_tree
*t
;
5800 Elf_Internal_Verdef def
;
5801 Elf_Internal_Verdaux defaux
;
5802 struct bfd_link_hash_entry
*bh
;
5803 struct elf_link_hash_entry
*h
;
5809 /* Make space for the base version. */
5810 size
+= sizeof (Elf_External_Verdef
);
5811 size
+= sizeof (Elf_External_Verdaux
);
5814 /* Make space for the default version. */
5815 if (info
->create_default_symver
)
5817 size
+= sizeof (Elf_External_Verdef
);
5821 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5823 struct bfd_elf_version_deps
*n
;
5825 size
+= sizeof (Elf_External_Verdef
);
5826 size
+= sizeof (Elf_External_Verdaux
);
5829 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5830 size
+= sizeof (Elf_External_Verdaux
);
5834 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5835 if (s
->contents
== NULL
&& s
->size
!= 0)
5838 /* Fill in the version definition section. */
5842 def
.vd_version
= VER_DEF_CURRENT
;
5843 def
.vd_flags
= VER_FLG_BASE
;
5846 if (info
->create_default_symver
)
5848 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5849 def
.vd_next
= sizeof (Elf_External_Verdef
);
5853 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5854 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5855 + sizeof (Elf_External_Verdaux
));
5858 if (soname_indx
!= (bfd_size_type
) -1)
5860 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5862 def
.vd_hash
= bfd_elf_hash (soname
);
5863 defaux
.vda_name
= soname_indx
;
5870 name
= lbasename (output_bfd
->filename
);
5871 def
.vd_hash
= bfd_elf_hash (name
);
5872 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5874 if (indx
== (bfd_size_type
) -1)
5876 defaux
.vda_name
= indx
;
5878 defaux
.vda_next
= 0;
5880 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5881 (Elf_External_Verdef
*) p
);
5882 p
+= sizeof (Elf_External_Verdef
);
5883 if (info
->create_default_symver
)
5885 /* Add a symbol representing this version. */
5887 if (! (_bfd_generic_link_add_one_symbol
5888 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5890 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5892 h
= (struct elf_link_hash_entry
*) bh
;
5895 h
->type
= STT_OBJECT
;
5896 h
->verinfo
.vertree
= NULL
;
5898 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5901 /* Create a duplicate of the base version with the same
5902 aux block, but different flags. */
5905 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5907 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5908 + sizeof (Elf_External_Verdaux
));
5911 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5912 (Elf_External_Verdef
*) p
);
5913 p
+= sizeof (Elf_External_Verdef
);
5915 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5916 (Elf_External_Verdaux
*) p
);
5917 p
+= sizeof (Elf_External_Verdaux
);
5919 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5922 struct bfd_elf_version_deps
*n
;
5925 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5928 /* Add a symbol representing this version. */
5930 if (! (_bfd_generic_link_add_one_symbol
5931 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5933 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5935 h
= (struct elf_link_hash_entry
*) bh
;
5938 h
->type
= STT_OBJECT
;
5939 h
->verinfo
.vertree
= t
;
5941 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5944 def
.vd_version
= VER_DEF_CURRENT
;
5946 if (t
->globals
.list
== NULL
5947 && t
->locals
.list
== NULL
5949 def
.vd_flags
|= VER_FLG_WEAK
;
5950 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5951 def
.vd_cnt
= cdeps
+ 1;
5952 def
.vd_hash
= bfd_elf_hash (t
->name
);
5953 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5955 if (t
->next
!= NULL
)
5956 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5957 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5959 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5960 (Elf_External_Verdef
*) p
);
5961 p
+= sizeof (Elf_External_Verdef
);
5963 defaux
.vda_name
= h
->dynstr_index
;
5964 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5966 defaux
.vda_next
= 0;
5967 if (t
->deps
!= NULL
)
5968 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5969 t
->name_indx
= defaux
.vda_name
;
5971 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5972 (Elf_External_Verdaux
*) p
);
5973 p
+= sizeof (Elf_External_Verdaux
);
5975 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5977 if (n
->version_needed
== NULL
)
5979 /* This can happen if there was an error in the
5981 defaux
.vda_name
= 0;
5985 defaux
.vda_name
= n
->version_needed
->name_indx
;
5986 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5989 if (n
->next
== NULL
)
5990 defaux
.vda_next
= 0;
5992 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5994 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5995 (Elf_External_Verdaux
*) p
);
5996 p
+= sizeof (Elf_External_Verdaux
);
6000 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
6001 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
6004 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6007 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
6009 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
6012 else if (info
->flags
& DF_BIND_NOW
)
6014 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
6020 if (info
->executable
)
6021 info
->flags_1
&= ~ (DF_1_INITFIRST
6024 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
6028 /* Work out the size of the version reference section. */
6030 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
6031 BFD_ASSERT (s
!= NULL
);
6033 struct elf_find_verdep_info sinfo
;
6035 sinfo
.output_bfd
= output_bfd
;
6037 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6038 if (sinfo
.vers
== 0)
6040 sinfo
.failed
= FALSE
;
6042 elf_link_hash_traverse (elf_hash_table (info
),
6043 _bfd_elf_link_find_version_dependencies
,
6048 if (elf_tdata (output_bfd
)->verref
== NULL
)
6049 s
->flags
|= SEC_EXCLUDE
;
6052 Elf_Internal_Verneed
*t
;
6057 /* Build the version definition section. */
6060 for (t
= elf_tdata (output_bfd
)->verref
;
6064 Elf_Internal_Vernaux
*a
;
6066 size
+= sizeof (Elf_External_Verneed
);
6068 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6069 size
+= sizeof (Elf_External_Vernaux
);
6073 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
6074 if (s
->contents
== NULL
)
6078 for (t
= elf_tdata (output_bfd
)->verref
;
6083 Elf_Internal_Vernaux
*a
;
6087 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6090 t
->vn_version
= VER_NEED_CURRENT
;
6092 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6093 elf_dt_name (t
->vn_bfd
) != NULL
6094 ? elf_dt_name (t
->vn_bfd
)
6095 : lbasename (t
->vn_bfd
->filename
),
6097 if (indx
== (bfd_size_type
) -1)
6100 t
->vn_aux
= sizeof (Elf_External_Verneed
);
6101 if (t
->vn_nextref
== NULL
)
6104 t
->vn_next
= (sizeof (Elf_External_Verneed
)
6105 + caux
* sizeof (Elf_External_Vernaux
));
6107 _bfd_elf_swap_verneed_out (output_bfd
, t
,
6108 (Elf_External_Verneed
*) p
);
6109 p
+= sizeof (Elf_External_Verneed
);
6111 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6113 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6114 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6115 a
->vna_nodename
, FALSE
);
6116 if (indx
== (bfd_size_type
) -1)
6119 if (a
->vna_nextptr
== NULL
)
6122 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6124 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6125 (Elf_External_Vernaux
*) p
);
6126 p
+= sizeof (Elf_External_Vernaux
);
6130 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
6131 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
6134 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6138 if ((elf_tdata (output_bfd
)->cverrefs
== 0
6139 && elf_tdata (output_bfd
)->cverdefs
== 0)
6140 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6141 §ion_sym_count
) == 0)
6143 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
6144 s
->flags
|= SEC_EXCLUDE
;
6150 /* Find the first non-excluded output section. We'll use its
6151 section symbol for some emitted relocs. */
6153 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
6157 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6158 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
6159 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6161 elf_hash_table (info
)->text_index_section
= s
;
6166 /* Find two non-excluded output sections, one for code, one for data.
6167 We'll use their section symbols for some emitted relocs. */
6169 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6173 /* Data first, since setting text_index_section changes
6174 _bfd_elf_link_omit_section_dynsym. */
6175 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6176 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
6177 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6179 elf_hash_table (info
)->data_index_section
= s
;
6183 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6184 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
6185 == (SEC_ALLOC
| SEC_READONLY
))
6186 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6188 elf_hash_table (info
)->text_index_section
= s
;
6192 if (elf_hash_table (info
)->text_index_section
== NULL
)
6193 elf_hash_table (info
)->text_index_section
6194 = elf_hash_table (info
)->data_index_section
;
6198 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6200 const struct elf_backend_data
*bed
;
6202 if (!is_elf_hash_table (info
->hash
))
6205 bed
= get_elf_backend_data (output_bfd
);
6206 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6208 if (elf_hash_table (info
)->dynamic_sections_created
)
6212 bfd_size_type dynsymcount
;
6213 unsigned long section_sym_count
;
6214 unsigned int dtagcount
;
6216 dynobj
= elf_hash_table (info
)->dynobj
;
6218 /* Assign dynsym indicies. In a shared library we generate a
6219 section symbol for each output section, which come first.
6220 Next come all of the back-end allocated local dynamic syms,
6221 followed by the rest of the global symbols. */
6223 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6224 §ion_sym_count
);
6226 /* Work out the size of the symbol version section. */
6227 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
6228 BFD_ASSERT (s
!= NULL
);
6229 if (dynsymcount
!= 0
6230 && (s
->flags
& SEC_EXCLUDE
) == 0)
6232 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6233 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
6234 if (s
->contents
== NULL
)
6237 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
6241 /* Set the size of the .dynsym and .hash sections. We counted
6242 the number of dynamic symbols in elf_link_add_object_symbols.
6243 We will build the contents of .dynsym and .hash when we build
6244 the final symbol table, because until then we do not know the
6245 correct value to give the symbols. We built the .dynstr
6246 section as we went along in elf_link_add_object_symbols. */
6247 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
6248 BFD_ASSERT (s
!= NULL
);
6249 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
6251 if (dynsymcount
!= 0)
6253 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
6254 if (s
->contents
== NULL
)
6257 /* The first entry in .dynsym is a dummy symbol.
6258 Clear all the section syms, in case we don't output them all. */
6259 ++section_sym_count
;
6260 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
6263 elf_hash_table (info
)->bucketcount
= 0;
6265 /* Compute the size of the hashing table. As a side effect this
6266 computes the hash values for all the names we export. */
6267 if (info
->emit_hash
)
6269 unsigned long int *hashcodes
;
6270 struct hash_codes_info hashinf
;
6272 unsigned long int nsyms
;
6274 size_t hash_entry_size
;
6276 /* Compute the hash values for all exported symbols. At the same
6277 time store the values in an array so that we could use them for
6279 amt
= dynsymcount
* sizeof (unsigned long int);
6280 hashcodes
= bfd_malloc (amt
);
6281 if (hashcodes
== NULL
)
6283 hashinf
.hashcodes
= hashcodes
;
6284 hashinf
.error
= FALSE
;
6286 /* Put all hash values in HASHCODES. */
6287 elf_link_hash_traverse (elf_hash_table (info
),
6288 elf_collect_hash_codes
, &hashinf
);
6295 nsyms
= hashinf
.hashcodes
- hashcodes
;
6297 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6300 if (bucketcount
== 0)
6303 elf_hash_table (info
)->bucketcount
= bucketcount
;
6305 s
= bfd_get_section_by_name (dynobj
, ".hash");
6306 BFD_ASSERT (s
!= NULL
);
6307 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6308 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6309 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
6310 if (s
->contents
== NULL
)
6313 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6314 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6315 s
->contents
+ hash_entry_size
);
6318 if (info
->emit_gnu_hash
)
6321 unsigned char *contents
;
6322 struct collect_gnu_hash_codes cinfo
;
6326 memset (&cinfo
, 0, sizeof (cinfo
));
6328 /* Compute the hash values for all exported symbols. At the same
6329 time store the values in an array so that we could use them for
6331 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6332 cinfo
.hashcodes
= bfd_malloc (amt
);
6333 if (cinfo
.hashcodes
== NULL
)
6336 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6337 cinfo
.min_dynindx
= -1;
6338 cinfo
.output_bfd
= output_bfd
;
6341 /* Put all hash values in HASHCODES. */
6342 elf_link_hash_traverse (elf_hash_table (info
),
6343 elf_collect_gnu_hash_codes
, &cinfo
);
6346 free (cinfo
.hashcodes
);
6351 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6353 if (bucketcount
== 0)
6355 free (cinfo
.hashcodes
);
6359 s
= bfd_get_section_by_name (dynobj
, ".gnu.hash");
6360 BFD_ASSERT (s
!= NULL
);
6362 if (cinfo
.nsyms
== 0)
6364 /* Empty .gnu.hash section is special. */
6365 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6366 free (cinfo
.hashcodes
);
6367 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6368 contents
= bfd_zalloc (output_bfd
, s
->size
);
6369 if (contents
== NULL
)
6371 s
->contents
= contents
;
6372 /* 1 empty bucket. */
6373 bfd_put_32 (output_bfd
, 1, contents
);
6374 /* SYMIDX above the special symbol 0. */
6375 bfd_put_32 (output_bfd
, 1, contents
+ 4);
6376 /* Just one word for bitmask. */
6377 bfd_put_32 (output_bfd
, 1, contents
+ 8);
6378 /* Only hash fn bloom filter. */
6379 bfd_put_32 (output_bfd
, 0, contents
+ 12);
6380 /* No hashes are valid - empty bitmask. */
6381 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
6382 /* No hashes in the only bucket. */
6383 bfd_put_32 (output_bfd
, 0,
6384 contents
+ 16 + bed
->s
->arch_size
/ 8);
6388 unsigned long int maskwords
, maskbitslog2
;
6389 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
6391 maskbitslog2
= bfd_log2 (cinfo
.nsyms
) + 1;
6392 if (maskbitslog2
< 3)
6394 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
6395 maskbitslog2
= maskbitslog2
+ 3;
6397 maskbitslog2
= maskbitslog2
+ 2;
6398 if (bed
->s
->arch_size
== 64)
6400 if (maskbitslog2
== 5)
6406 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
6407 cinfo
.shift2
= maskbitslog2
;
6408 cinfo
.maskbits
= 1 << maskbitslog2
;
6409 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
6410 amt
= bucketcount
* sizeof (unsigned long int) * 2;
6411 amt
+= maskwords
* sizeof (bfd_vma
);
6412 cinfo
.bitmask
= bfd_malloc (amt
);
6413 if (cinfo
.bitmask
== NULL
)
6415 free (cinfo
.hashcodes
);
6419 cinfo
.counts
= (void *) (cinfo
.bitmask
+ maskwords
);
6420 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
6421 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
6422 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
6424 /* Determine how often each hash bucket is used. */
6425 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
6426 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
6427 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
6429 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
6430 if (cinfo
.counts
[i
] != 0)
6432 cinfo
.indx
[i
] = cnt
;
6433 cnt
+= cinfo
.counts
[i
];
6435 BFD_ASSERT (cnt
== dynsymcount
);
6436 cinfo
.bucketcount
= bucketcount
;
6437 cinfo
.local_indx
= cinfo
.min_dynindx
;
6439 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
6440 s
->size
+= cinfo
.maskbits
/ 8;
6441 contents
= bfd_zalloc (output_bfd
, s
->size
);
6442 if (contents
== NULL
)
6444 free (cinfo
.bitmask
);
6445 free (cinfo
.hashcodes
);
6449 s
->contents
= contents
;
6450 bfd_put_32 (output_bfd
, bucketcount
, contents
);
6451 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
6452 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
6453 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
6454 contents
+= 16 + cinfo
.maskbits
/ 8;
6456 for (i
= 0; i
< bucketcount
; ++i
)
6458 if (cinfo
.counts
[i
] == 0)
6459 bfd_put_32 (output_bfd
, 0, contents
);
6461 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
6465 cinfo
.contents
= contents
;
6467 /* Renumber dynamic symbols, populate .gnu.hash section. */
6468 elf_link_hash_traverse (elf_hash_table (info
),
6469 elf_renumber_gnu_hash_syms
, &cinfo
);
6471 contents
= s
->contents
+ 16;
6472 for (i
= 0; i
< maskwords
; ++i
)
6474 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
6476 contents
+= bed
->s
->arch_size
/ 8;
6479 free (cinfo
.bitmask
);
6480 free (cinfo
.hashcodes
);
6484 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
6485 BFD_ASSERT (s
!= NULL
);
6487 elf_finalize_dynstr (output_bfd
, info
);
6489 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6491 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
6492 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
6499 /* Indicate that we are only retrieving symbol values from this
6503 _bfd_elf_link_just_syms (asection
*sec
, struct bfd_link_info
*info
)
6505 if (is_elf_hash_table (info
->hash
))
6506 sec
->sec_info_type
= ELF_INFO_TYPE_JUST_SYMS
;
6507 _bfd_generic_link_just_syms (sec
, info
);
6510 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6513 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
6516 BFD_ASSERT (sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
);
6517 sec
->sec_info_type
= ELF_INFO_TYPE_NONE
;
6520 /* Finish SHF_MERGE section merging. */
6523 _bfd_elf_merge_sections (bfd
*abfd
, struct bfd_link_info
*info
)
6528 if (!is_elf_hash_table (info
->hash
))
6531 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
6532 if ((ibfd
->flags
& DYNAMIC
) == 0)
6533 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6534 if ((sec
->flags
& SEC_MERGE
) != 0
6535 && !bfd_is_abs_section (sec
->output_section
))
6537 struct bfd_elf_section_data
*secdata
;
6539 secdata
= elf_section_data (sec
);
6540 if (! _bfd_add_merge_section (abfd
,
6541 &elf_hash_table (info
)->merge_info
,
6542 sec
, &secdata
->sec_info
))
6544 else if (secdata
->sec_info
)
6545 sec
->sec_info_type
= ELF_INFO_TYPE_MERGE
;
6548 if (elf_hash_table (info
)->merge_info
!= NULL
)
6549 _bfd_merge_sections (abfd
, info
, elf_hash_table (info
)->merge_info
,
6550 merge_sections_remove_hook
);
6554 /* Create an entry in an ELF linker hash table. */
6556 struct bfd_hash_entry
*
6557 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
6558 struct bfd_hash_table
*table
,
6561 /* Allocate the structure if it has not already been allocated by a
6565 entry
= bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
6570 /* Call the allocation method of the superclass. */
6571 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
6574 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
6575 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
6577 /* Set local fields. */
6580 ret
->got
= htab
->init_got_refcount
;
6581 ret
->plt
= htab
->init_plt_refcount
;
6582 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
6583 - offsetof (struct elf_link_hash_entry
, size
)));
6584 /* Assume that we have been called by a non-ELF symbol reader.
6585 This flag is then reset by the code which reads an ELF input
6586 file. This ensures that a symbol created by a non-ELF symbol
6587 reader will have the flag set correctly. */
6594 /* Copy data from an indirect symbol to its direct symbol, hiding the
6595 old indirect symbol. Also used for copying flags to a weakdef. */
6598 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
6599 struct elf_link_hash_entry
*dir
,
6600 struct elf_link_hash_entry
*ind
)
6602 struct elf_link_hash_table
*htab
;
6604 /* Copy down any references that we may have already seen to the
6605 symbol which just became indirect. */
6607 dir
->ref_dynamic
|= ind
->ref_dynamic
;
6608 dir
->ref_regular
|= ind
->ref_regular
;
6609 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
6610 dir
->non_got_ref
|= ind
->non_got_ref
;
6611 dir
->needs_plt
|= ind
->needs_plt
;
6612 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
6614 if (ind
->root
.type
!= bfd_link_hash_indirect
)
6617 /* Copy over the global and procedure linkage table refcount entries.
6618 These may have been already set up by a check_relocs routine. */
6619 htab
= elf_hash_table (info
);
6620 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
6622 if (dir
->got
.refcount
< 0)
6623 dir
->got
.refcount
= 0;
6624 dir
->got
.refcount
+= ind
->got
.refcount
;
6625 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
6628 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
6630 if (dir
->plt
.refcount
< 0)
6631 dir
->plt
.refcount
= 0;
6632 dir
->plt
.refcount
+= ind
->plt
.refcount
;
6633 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
6636 if (ind
->dynindx
!= -1)
6638 if (dir
->dynindx
!= -1)
6639 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
6640 dir
->dynindx
= ind
->dynindx
;
6641 dir
->dynstr_index
= ind
->dynstr_index
;
6643 ind
->dynstr_index
= 0;
6648 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
6649 struct elf_link_hash_entry
*h
,
6650 bfd_boolean force_local
)
6652 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
6656 h
->forced_local
= 1;
6657 if (h
->dynindx
!= -1)
6660 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
6666 /* Initialize an ELF linker hash table. */
6669 _bfd_elf_link_hash_table_init
6670 (struct elf_link_hash_table
*table
,
6672 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
6673 struct bfd_hash_table
*,
6675 unsigned int entsize
)
6678 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
6680 memset (table
, 0, sizeof * table
);
6681 table
->init_got_refcount
.refcount
= can_refcount
- 1;
6682 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
6683 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
6684 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
6685 /* The first dynamic symbol is a dummy. */
6686 table
->dynsymcount
= 1;
6688 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
6689 table
->root
.type
= bfd_link_elf_hash_table
;
6694 /* Create an ELF linker hash table. */
6696 struct bfd_link_hash_table
*
6697 _bfd_elf_link_hash_table_create (bfd
*abfd
)
6699 struct elf_link_hash_table
*ret
;
6700 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
6702 ret
= bfd_malloc (amt
);
6706 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
6707 sizeof (struct elf_link_hash_entry
)))
6716 /* This is a hook for the ELF emulation code in the generic linker to
6717 tell the backend linker what file name to use for the DT_NEEDED
6718 entry for a dynamic object. */
6721 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
6723 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
6724 && bfd_get_format (abfd
) == bfd_object
)
6725 elf_dt_name (abfd
) = name
;
6729 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
6732 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
6733 && bfd_get_format (abfd
) == bfd_object
)
6734 lib_class
= elf_dyn_lib_class (abfd
);
6741 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
6743 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
6744 && bfd_get_format (abfd
) == bfd_object
)
6745 elf_dyn_lib_class (abfd
) = lib_class
;
6748 /* Get the list of DT_NEEDED entries for a link. This is a hook for
6749 the linker ELF emulation code. */
6751 struct bfd_link_needed_list
*
6752 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
6753 struct bfd_link_info
*info
)
6755 if (! is_elf_hash_table (info
->hash
))
6757 return elf_hash_table (info
)->needed
;
6760 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6761 hook for the linker ELF emulation code. */
6763 struct bfd_link_needed_list
*
6764 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
6765 struct bfd_link_info
*info
)
6767 if (! is_elf_hash_table (info
->hash
))
6769 return elf_hash_table (info
)->runpath
;
6772 /* Get the name actually used for a dynamic object for a link. This
6773 is the SONAME entry if there is one. Otherwise, it is the string
6774 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
6777 bfd_elf_get_dt_soname (bfd
*abfd
)
6779 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
6780 && bfd_get_format (abfd
) == bfd_object
)
6781 return elf_dt_name (abfd
);
6785 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
6786 the ELF linker emulation code. */
6789 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
6790 struct bfd_link_needed_list
**pneeded
)
6793 bfd_byte
*dynbuf
= NULL
;
6794 unsigned int elfsec
;
6795 unsigned long shlink
;
6796 bfd_byte
*extdyn
, *extdynend
;
6798 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
6802 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
6803 || bfd_get_format (abfd
) != bfd_object
)
6806 s
= bfd_get_section_by_name (abfd
, ".dynamic");
6807 if (s
== NULL
|| s
->size
== 0)
6810 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
6813 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
6814 if (elfsec
== SHN_BAD
)
6817 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
6819 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
6820 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
6823 extdynend
= extdyn
+ s
->size
;
6824 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
6826 Elf_Internal_Dyn dyn
;
6828 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
6830 if (dyn
.d_tag
== DT_NULL
)
6833 if (dyn
.d_tag
== DT_NEEDED
)
6836 struct bfd_link_needed_list
*l
;
6837 unsigned int tagv
= dyn
.d_un
.d_val
;
6840 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
6845 l
= bfd_alloc (abfd
, amt
);
6866 struct elf_symbuf_symbol
6868 unsigned long st_name
; /* Symbol name, index in string tbl */
6869 unsigned char st_info
; /* Type and binding attributes */
6870 unsigned char st_other
; /* Visibilty, and target specific */
6873 struct elf_symbuf_head
6875 struct elf_symbuf_symbol
*ssym
;
6876 bfd_size_type count
;
6877 unsigned int st_shndx
;
6884 Elf_Internal_Sym
*isym
;
6885 struct elf_symbuf_symbol
*ssym
;
6890 /* Sort references to symbols by ascending section number. */
6893 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
6895 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
6896 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
6898 return s1
->st_shndx
- s2
->st_shndx
;
6902 elf_sym_name_compare (const void *arg1
, const void *arg2
)
6904 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
6905 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
6906 return strcmp (s1
->name
, s2
->name
);
6909 static struct elf_symbuf_head
*
6910 elf_create_symbuf (bfd_size_type symcount
, Elf_Internal_Sym
*isymbuf
)
6912 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
6913 struct elf_symbuf_symbol
*ssym
;
6914 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
6915 bfd_size_type i
, shndx_count
, total_size
;
6917 indbuf
= bfd_malloc2 (symcount
, sizeof (*indbuf
));
6921 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
6922 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
6923 *ind
++ = &isymbuf
[i
];
6926 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
6927 elf_sort_elf_symbol
);
6930 if (indbufend
> indbuf
)
6931 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
6932 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
6935 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
6936 + (indbufend
- indbuf
) * sizeof (*ssym
));
6937 ssymbuf
= bfd_malloc (total_size
);
6938 if (ssymbuf
== NULL
)
6944 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
6945 ssymbuf
->ssym
= NULL
;
6946 ssymbuf
->count
= shndx_count
;
6947 ssymbuf
->st_shndx
= 0;
6948 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
6950 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
6953 ssymhead
->ssym
= ssym
;
6954 ssymhead
->count
= 0;
6955 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
6957 ssym
->st_name
= (*ind
)->st_name
;
6958 ssym
->st_info
= (*ind
)->st_info
;
6959 ssym
->st_other
= (*ind
)->st_other
;
6962 BFD_ASSERT ((bfd_size_type
) (ssymhead
- ssymbuf
) == shndx_count
6963 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
6970 /* Check if 2 sections define the same set of local and global
6974 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
6975 struct bfd_link_info
*info
)
6978 const struct elf_backend_data
*bed1
, *bed2
;
6979 Elf_Internal_Shdr
*hdr1
, *hdr2
;
6980 bfd_size_type symcount1
, symcount2
;
6981 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
6982 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
6983 Elf_Internal_Sym
*isym
, *isymend
;
6984 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
6985 bfd_size_type count1
, count2
, i
;
6986 unsigned int shndx1
, shndx2
;
6992 /* Both sections have to be in ELF. */
6993 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
6994 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
6997 if (elf_section_type (sec1
) != elf_section_type (sec2
))
7000 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
7001 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
7002 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
7005 bed1
= get_elf_backend_data (bfd1
);
7006 bed2
= get_elf_backend_data (bfd2
);
7007 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
7008 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
7009 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
7010 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
7012 if (symcount1
== 0 || symcount2
== 0)
7018 ssymbuf1
= elf_tdata (bfd1
)->symbuf
;
7019 ssymbuf2
= elf_tdata (bfd2
)->symbuf
;
7021 if (ssymbuf1
== NULL
)
7023 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
7025 if (isymbuf1
== NULL
)
7028 if (!info
->reduce_memory_overheads
)
7029 elf_tdata (bfd1
)->symbuf
= ssymbuf1
7030 = elf_create_symbuf (symcount1
, isymbuf1
);
7033 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
7035 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
7037 if (isymbuf2
== NULL
)
7040 if (ssymbuf1
!= NULL
&& !info
->reduce_memory_overheads
)
7041 elf_tdata (bfd2
)->symbuf
= ssymbuf2
7042 = elf_create_symbuf (symcount2
, isymbuf2
);
7045 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
7047 /* Optimized faster version. */
7048 bfd_size_type lo
, hi
, mid
;
7049 struct elf_symbol
*symp
;
7050 struct elf_symbuf_symbol
*ssym
, *ssymend
;
7053 hi
= ssymbuf1
->count
;
7058 mid
= (lo
+ hi
) / 2;
7059 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
7061 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
7065 count1
= ssymbuf1
[mid
].count
;
7072 hi
= ssymbuf2
->count
;
7077 mid
= (lo
+ hi
) / 2;
7078 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
7080 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
7084 count2
= ssymbuf2
[mid
].count
;
7090 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7093 symtable1
= bfd_malloc (count1
* sizeof (struct elf_symbol
));
7094 symtable2
= bfd_malloc (count2
* sizeof (struct elf_symbol
));
7095 if (symtable1
== NULL
|| symtable2
== NULL
)
7099 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
7100 ssym
< ssymend
; ssym
++, symp
++)
7102 symp
->u
.ssym
= ssym
;
7103 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
7109 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
7110 ssym
< ssymend
; ssym
++, symp
++)
7112 symp
->u
.ssym
= ssym
;
7113 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
7118 /* Sort symbol by name. */
7119 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7120 elf_sym_name_compare
);
7121 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7122 elf_sym_name_compare
);
7124 for (i
= 0; i
< count1
; i
++)
7125 /* Two symbols must have the same binding, type and name. */
7126 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
7127 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
7128 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7135 symtable1
= bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
7136 symtable2
= bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
7137 if (symtable1
== NULL
|| symtable2
== NULL
)
7140 /* Count definitions in the section. */
7142 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
7143 if (isym
->st_shndx
== shndx1
)
7144 symtable1
[count1
++].u
.isym
= isym
;
7147 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
7148 if (isym
->st_shndx
== shndx2
)
7149 symtable2
[count2
++].u
.isym
= isym
;
7151 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7154 for (i
= 0; i
< count1
; i
++)
7156 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
7157 symtable1
[i
].u
.isym
->st_name
);
7159 for (i
= 0; i
< count2
; i
++)
7161 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
7162 symtable2
[i
].u
.isym
->st_name
);
7164 /* Sort symbol by name. */
7165 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7166 elf_sym_name_compare
);
7167 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7168 elf_sym_name_compare
);
7170 for (i
= 0; i
< count1
; i
++)
7171 /* Two symbols must have the same binding, type and name. */
7172 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
7173 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
7174 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7192 /* Return TRUE if 2 section types are compatible. */
7195 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
7196 bfd
*bbfd
, const asection
*bsec
)
7200 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
7201 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7204 return elf_section_type (asec
) == elf_section_type (bsec
);
7207 /* Final phase of ELF linker. */
7209 /* A structure we use to avoid passing large numbers of arguments. */
7211 struct elf_final_link_info
7213 /* General link information. */
7214 struct bfd_link_info
*info
;
7217 /* Symbol string table. */
7218 struct bfd_strtab_hash
*symstrtab
;
7219 /* .dynsym section. */
7220 asection
*dynsym_sec
;
7221 /* .hash section. */
7223 /* symbol version section (.gnu.version). */
7224 asection
*symver_sec
;
7225 /* Buffer large enough to hold contents of any section. */
7227 /* Buffer large enough to hold external relocs of any section. */
7228 void *external_relocs
;
7229 /* Buffer large enough to hold internal relocs of any section. */
7230 Elf_Internal_Rela
*internal_relocs
;
7231 /* Buffer large enough to hold external local symbols of any input
7233 bfd_byte
*external_syms
;
7234 /* And a buffer for symbol section indices. */
7235 Elf_External_Sym_Shndx
*locsym_shndx
;
7236 /* Buffer large enough to hold internal local symbols of any input
7238 Elf_Internal_Sym
*internal_syms
;
7239 /* Array large enough to hold a symbol index for each local symbol
7240 of any input BFD. */
7242 /* Array large enough to hold a section pointer for each local
7243 symbol of any input BFD. */
7244 asection
**sections
;
7245 /* Buffer to hold swapped out symbols. */
7247 /* And one for symbol section indices. */
7248 Elf_External_Sym_Shndx
*symshndxbuf
;
7249 /* Number of swapped out symbols in buffer. */
7250 size_t symbuf_count
;
7251 /* Number of symbols which fit in symbuf. */
7253 /* And same for symshndxbuf. */
7254 size_t shndxbuf_size
;
7257 /* This struct is used to pass information to elf_link_output_extsym. */
7259 struct elf_outext_info
7262 bfd_boolean localsyms
;
7263 struct elf_final_link_info
*finfo
;
7267 /* Support for evaluating a complex relocation.
7269 Complex relocations are generalized, self-describing relocations. The
7270 implementation of them consists of two parts: complex symbols, and the
7271 relocations themselves.
7273 The relocations are use a reserved elf-wide relocation type code (R_RELC
7274 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7275 information (start bit, end bit, word width, etc) into the addend. This
7276 information is extracted from CGEN-generated operand tables within gas.
7278 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7279 internal) representing prefix-notation expressions, including but not
7280 limited to those sorts of expressions normally encoded as addends in the
7281 addend field. The symbol mangling format is:
7284 | <unary-operator> ':' <node>
7285 | <binary-operator> ':' <node> ':' <node>
7288 <literal> := 's' <digits=N> ':' <N character symbol name>
7289 | 'S' <digits=N> ':' <N character section name>
7293 <binary-operator> := as in C
7294 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7297 set_symbol_value (bfd
*bfd_with_globals
,
7298 Elf_Internal_Sym
*isymbuf
,
7303 struct elf_link_hash_entry
**sym_hashes
;
7304 struct elf_link_hash_entry
*h
;
7305 size_t extsymoff
= locsymcount
;
7307 if (symidx
< locsymcount
)
7309 Elf_Internal_Sym
*sym
;
7311 sym
= isymbuf
+ symidx
;
7312 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
7314 /* It is a local symbol: move it to the
7315 "absolute" section and give it a value. */
7316 sym
->st_shndx
= SHN_ABS
;
7317 sym
->st_value
= val
;
7320 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
7324 /* It is a global symbol: set its link type
7325 to "defined" and give it a value. */
7327 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
7328 h
= sym_hashes
[symidx
- extsymoff
];
7329 while (h
->root
.type
== bfd_link_hash_indirect
7330 || h
->root
.type
== bfd_link_hash_warning
)
7331 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7332 h
->root
.type
= bfd_link_hash_defined
;
7333 h
->root
.u
.def
.value
= val
;
7334 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
7338 resolve_symbol (const char *name
,
7340 struct elf_final_link_info
*finfo
,
7342 Elf_Internal_Sym
*isymbuf
,
7345 Elf_Internal_Sym
*sym
;
7346 struct bfd_link_hash_entry
*global_entry
;
7347 const char *candidate
= NULL
;
7348 Elf_Internal_Shdr
*symtab_hdr
;
7351 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
7353 for (i
= 0; i
< locsymcount
; ++ i
)
7357 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
7360 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
7361 symtab_hdr
->sh_link
,
7364 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7365 name
, candidate
, (unsigned long) sym
->st_value
);
7367 if (candidate
&& strcmp (candidate
, name
) == 0)
7369 asection
*sec
= finfo
->sections
[i
];
7371 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
7372 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
7374 printf ("Found symbol with value %8.8lx\n",
7375 (unsigned long) *result
);
7381 /* Hmm, haven't found it yet. perhaps it is a global. */
7382 global_entry
= bfd_link_hash_lookup (finfo
->info
->hash
, name
,
7383 FALSE
, FALSE
, TRUE
);
7387 if (global_entry
->type
== bfd_link_hash_defined
7388 || global_entry
->type
== bfd_link_hash_defweak
)
7390 *result
= (global_entry
->u
.def
.value
7391 + global_entry
->u
.def
.section
->output_section
->vma
7392 + global_entry
->u
.def
.section
->output_offset
);
7394 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7395 global_entry
->root
.string
, (unsigned long) *result
);
7404 resolve_section (const char *name
,
7411 for (curr
= sections
; curr
; curr
= curr
->next
)
7412 if (strcmp (curr
->name
, name
) == 0)
7414 *result
= curr
->vma
;
7418 /* Hmm. still haven't found it. try pseudo-section names. */
7419 for (curr
= sections
; curr
; curr
= curr
->next
)
7421 len
= strlen (curr
->name
);
7422 if (len
> strlen (name
))
7425 if (strncmp (curr
->name
, name
, len
) == 0)
7427 if (strncmp (".end", name
+ len
, 4) == 0)
7429 *result
= curr
->vma
+ curr
->size
;
7433 /* Insert more pseudo-section names here, if you like. */
7441 undefined_reference (const char *reftype
, const char *name
)
7443 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7448 eval_symbol (bfd_vma
*result
,
7451 struct elf_final_link_info
*finfo
,
7453 Elf_Internal_Sym
*isymbuf
,
7462 const char *sym
= *symp
;
7464 bfd_boolean symbol_is_section
= FALSE
;
7469 if (len
< 1 || len
> sizeof (symbuf
))
7471 bfd_set_error (bfd_error_invalid_operation
);
7484 *result
= strtoul (sym
, (char **) symp
, 16);
7488 symbol_is_section
= TRUE
;
7491 symlen
= strtol (sym
, (char **) symp
, 10);
7492 sym
= *symp
+ 1; /* Skip the trailing ':'. */
7494 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
7496 bfd_set_error (bfd_error_invalid_operation
);
7500 memcpy (symbuf
, sym
, symlen
);
7501 symbuf
[symlen
] = '\0';
7502 *symp
= sym
+ symlen
;
7504 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7505 the symbol as a section, or vice-versa. so we're pretty liberal in our
7506 interpretation here; section means "try section first", not "must be a
7507 section", and likewise with symbol. */
7509 if (symbol_is_section
)
7511 if (!resolve_section (symbuf
, finfo
->output_bfd
->sections
, result
)
7512 && !resolve_symbol (symbuf
, input_bfd
, finfo
, result
,
7513 isymbuf
, locsymcount
))
7515 undefined_reference ("section", symbuf
);
7521 if (!resolve_symbol (symbuf
, input_bfd
, finfo
, result
,
7522 isymbuf
, locsymcount
)
7523 && !resolve_section (symbuf
, finfo
->output_bfd
->sections
,
7526 undefined_reference ("symbol", symbuf
);
7533 /* All that remains are operators. */
7535 #define UNARY_OP(op) \
7536 if (strncmp (sym, #op, strlen (#op)) == 0) \
7538 sym += strlen (#op); \
7542 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7543 isymbuf, locsymcount, signed_p)) \
7546 *result = op ((bfd_signed_vma) a); \
7552 #define BINARY_OP(op) \
7553 if (strncmp (sym, #op, strlen (#op)) == 0) \
7555 sym += strlen (#op); \
7559 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7560 isymbuf, locsymcount, signed_p)) \
7563 if (!eval_symbol (&b, symp, input_bfd, finfo, dot, \
7564 isymbuf, locsymcount, signed_p)) \
7567 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7597 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
7598 bfd_set_error (bfd_error_invalid_operation
);
7604 put_value (bfd_vma size
,
7605 unsigned long chunksz
,
7610 location
+= (size
- chunksz
);
7612 for (; size
; size
-= chunksz
, location
-= chunksz
, x
>>= (chunksz
* 8))
7620 bfd_put_8 (input_bfd
, x
, location
);
7623 bfd_put_16 (input_bfd
, x
, location
);
7626 bfd_put_32 (input_bfd
, x
, location
);
7630 bfd_put_64 (input_bfd
, x
, location
);
7640 get_value (bfd_vma size
,
7641 unsigned long chunksz
,
7647 for (; size
; size
-= chunksz
, location
+= chunksz
)
7655 x
= (x
<< (8 * chunksz
)) | bfd_get_8 (input_bfd
, location
);
7658 x
= (x
<< (8 * chunksz
)) | bfd_get_16 (input_bfd
, location
);
7661 x
= (x
<< (8 * chunksz
)) | bfd_get_32 (input_bfd
, location
);
7665 x
= (x
<< (8 * chunksz
)) | bfd_get_64 (input_bfd
, location
);
7676 decode_complex_addend (unsigned long *start
, /* in bits */
7677 unsigned long *oplen
, /* in bits */
7678 unsigned long *len
, /* in bits */
7679 unsigned long *wordsz
, /* in bytes */
7680 unsigned long *chunksz
, /* in bytes */
7681 unsigned long *lsb0_p
,
7682 unsigned long *signed_p
,
7683 unsigned long *trunc_p
,
7684 unsigned long encoded
)
7686 * start
= encoded
& 0x3F;
7687 * len
= (encoded
>> 6) & 0x3F;
7688 * oplen
= (encoded
>> 12) & 0x3F;
7689 * wordsz
= (encoded
>> 18) & 0xF;
7690 * chunksz
= (encoded
>> 22) & 0xF;
7691 * lsb0_p
= (encoded
>> 27) & 1;
7692 * signed_p
= (encoded
>> 28) & 1;
7693 * trunc_p
= (encoded
>> 29) & 1;
7696 bfd_reloc_status_type
7697 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
7698 asection
*input_section ATTRIBUTE_UNUSED
,
7700 Elf_Internal_Rela
*rel
,
7703 bfd_vma shift
, x
, mask
;
7704 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
7705 bfd_reloc_status_type r
;
7707 /* Perform this reloc, since it is complex.
7708 (this is not to say that it necessarily refers to a complex
7709 symbol; merely that it is a self-describing CGEN based reloc.
7710 i.e. the addend has the complete reloc information (bit start, end,
7711 word size, etc) encoded within it.). */
7713 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
7714 &chunksz
, &lsb0_p
, &signed_p
,
7715 &trunc_p
, rel
->r_addend
);
7717 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
7720 shift
= (start
+ 1) - len
;
7722 shift
= (8 * wordsz
) - (start
+ len
);
7724 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ rel
->r_offset
);
7727 printf ("Doing complex reloc: "
7728 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7729 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7730 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7731 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
7732 oplen
, x
, mask
, relocation
);
7737 /* Now do an overflow check. */
7738 r
= bfd_check_overflow ((signed_p
7739 ? complain_overflow_signed
7740 : complain_overflow_unsigned
),
7741 len
, 0, (8 * wordsz
),
7745 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
7748 printf (" relocation: %8.8lx\n"
7749 " shifted mask: %8.8lx\n"
7750 " shifted/masked reloc: %8.8lx\n"
7751 " result: %8.8lx\n",
7752 relocation
, (mask
<< shift
),
7753 ((relocation
& mask
) << shift
), x
);
7755 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ rel
->r_offset
);
7759 /* When performing a relocatable link, the input relocations are
7760 preserved. But, if they reference global symbols, the indices
7761 referenced must be updated. Update all the relocations in
7762 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
7765 elf_link_adjust_relocs (bfd
*abfd
,
7766 Elf_Internal_Shdr
*rel_hdr
,
7768 struct elf_link_hash_entry
**rel_hash
)
7771 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7773 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
7774 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
7775 bfd_vma r_type_mask
;
7778 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
7780 swap_in
= bed
->s
->swap_reloc_in
;
7781 swap_out
= bed
->s
->swap_reloc_out
;
7783 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
7785 swap_in
= bed
->s
->swap_reloca_in
;
7786 swap_out
= bed
->s
->swap_reloca_out
;
7791 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
7794 if (bed
->s
->arch_size
== 32)
7801 r_type_mask
= 0xffffffff;
7805 erela
= rel_hdr
->contents
;
7806 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
7808 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
7811 if (*rel_hash
== NULL
)
7814 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
7816 (*swap_in
) (abfd
, erela
, irela
);
7817 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
7818 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
7819 | (irela
[j
].r_info
& r_type_mask
));
7820 (*swap_out
) (abfd
, irela
, erela
);
7824 struct elf_link_sort_rela
7830 enum elf_reloc_type_class type
;
7831 /* We use this as an array of size int_rels_per_ext_rel. */
7832 Elf_Internal_Rela rela
[1];
7836 elf_link_sort_cmp1 (const void *A
, const void *B
)
7838 const struct elf_link_sort_rela
*a
= A
;
7839 const struct elf_link_sort_rela
*b
= B
;
7840 int relativea
, relativeb
;
7842 relativea
= a
->type
== reloc_class_relative
;
7843 relativeb
= b
->type
== reloc_class_relative
;
7845 if (relativea
< relativeb
)
7847 if (relativea
> relativeb
)
7849 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
7851 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
7853 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
7855 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
7861 elf_link_sort_cmp2 (const void *A
, const void *B
)
7863 const struct elf_link_sort_rela
*a
= A
;
7864 const struct elf_link_sort_rela
*b
= B
;
7867 if (a
->u
.offset
< b
->u
.offset
)
7869 if (a
->u
.offset
> b
->u
.offset
)
7871 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
7872 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
7877 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
7879 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
7885 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
7887 asection
*dynamic_relocs
;
7890 bfd_size_type count
, size
;
7891 size_t i
, ret
, sort_elt
, ext_size
;
7892 bfd_byte
*sort
, *s_non_relative
, *p
;
7893 struct elf_link_sort_rela
*sq
;
7894 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7895 int i2e
= bed
->s
->int_rels_per_ext_rel
;
7896 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
7897 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
7898 struct bfd_link_order
*lo
;
7900 bfd_boolean use_rela
;
7902 /* Find a dynamic reloc section. */
7903 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
7904 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
7905 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
7906 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
7908 bfd_boolean use_rela_initialised
= FALSE
;
7910 /* This is just here to stop gcc from complaining.
7911 It's initialization checking code is not perfect. */
7914 /* Both sections are present. Examine the sizes
7915 of the indirect sections to help us choose. */
7916 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
7917 if (lo
->type
== bfd_indirect_link_order
)
7919 asection
*o
= lo
->u
.indirect
.section
;
7921 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
7923 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
7924 /* Section size is divisible by both rel and rela sizes.
7925 It is of no help to us. */
7929 /* Section size is only divisible by rela. */
7930 if (use_rela_initialised
&& (use_rela
== FALSE
))
7933 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
7934 bfd_set_error (bfd_error_invalid_operation
);
7940 use_rela_initialised
= TRUE
;
7944 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
7946 /* Section size is only divisible by rel. */
7947 if (use_rela_initialised
&& (use_rela
== TRUE
))
7950 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
7951 bfd_set_error (bfd_error_invalid_operation
);
7957 use_rela_initialised
= TRUE
;
7962 /* The section size is not divisible by either - something is wrong. */
7964 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd
);
7965 bfd_set_error (bfd_error_invalid_operation
);
7970 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
7971 if (lo
->type
== bfd_indirect_link_order
)
7973 asection
*o
= lo
->u
.indirect
.section
;
7975 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
7977 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
7978 /* Section size is divisible by both rel and rela sizes.
7979 It is of no help to us. */
7983 /* Section size is only divisible by rela. */
7984 if (use_rela_initialised
&& (use_rela
== FALSE
))
7987 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
7988 bfd_set_error (bfd_error_invalid_operation
);
7994 use_rela_initialised
= TRUE
;
7998 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8000 /* Section size is only divisible by rel. */
8001 if (use_rela_initialised
&& (use_rela
== TRUE
))
8004 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
8005 bfd_set_error (bfd_error_invalid_operation
);
8011 use_rela_initialised
= TRUE
;
8016 /* The section size is not divisible by either - something is wrong. */
8018 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd
);
8019 bfd_set_error (bfd_error_invalid_operation
);
8024 if (! use_rela_initialised
)
8028 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
8030 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8037 dynamic_relocs
= rela_dyn
;
8038 ext_size
= bed
->s
->sizeof_rela
;
8039 swap_in
= bed
->s
->swap_reloca_in
;
8040 swap_out
= bed
->s
->swap_reloca_out
;
8044 dynamic_relocs
= rel_dyn
;
8045 ext_size
= bed
->s
->sizeof_rel
;
8046 swap_in
= bed
->s
->swap_reloc_in
;
8047 swap_out
= bed
->s
->swap_reloc_out
;
8051 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8052 if (lo
->type
== bfd_indirect_link_order
)
8053 size
+= lo
->u
.indirect
.section
->size
;
8055 if (size
!= dynamic_relocs
->size
)
8058 sort_elt
= (sizeof (struct elf_link_sort_rela
)
8059 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
8061 count
= dynamic_relocs
->size
/ ext_size
;
8062 sort
= bfd_zmalloc (sort_elt
* count
);
8066 (*info
->callbacks
->warning
)
8067 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
8071 if (bed
->s
->arch_size
== 32)
8072 r_sym_mask
= ~(bfd_vma
) 0xff;
8074 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
8076 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8077 if (lo
->type
== bfd_indirect_link_order
)
8079 bfd_byte
*erel
, *erelend
;
8080 asection
*o
= lo
->u
.indirect
.section
;
8082 if (o
->contents
== NULL
&& o
->size
!= 0)
8084 /* This is a reloc section that is being handled as a normal
8085 section. See bfd_section_from_shdr. We can't combine
8086 relocs in this case. */
8091 erelend
= o
->contents
+ o
->size
;
8092 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
8094 while (erel
< erelend
)
8096 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8098 (*swap_in
) (abfd
, erel
, s
->rela
);
8099 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
8100 s
->u
.sym_mask
= r_sym_mask
;
8106 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
8108 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
8110 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8111 if (s
->type
!= reloc_class_relative
)
8117 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
8118 for (; i
< count
; i
++, p
+= sort_elt
)
8120 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
8121 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
8123 sp
->u
.offset
= sq
->rela
->r_offset
;
8126 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
8128 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8129 if (lo
->type
== bfd_indirect_link_order
)
8131 bfd_byte
*erel
, *erelend
;
8132 asection
*o
= lo
->u
.indirect
.section
;
8135 erelend
= o
->contents
+ o
->size
;
8136 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
8137 while (erel
< erelend
)
8139 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8140 (*swap_out
) (abfd
, s
->rela
, erel
);
8147 *psec
= dynamic_relocs
;
8151 /* Flush the output symbols to the file. */
8154 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
8155 const struct elf_backend_data
*bed
)
8157 if (finfo
->symbuf_count
> 0)
8159 Elf_Internal_Shdr
*hdr
;
8163 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
8164 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
8165 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
8166 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
8167 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
8170 hdr
->sh_size
+= amt
;
8171 finfo
->symbuf_count
= 0;
8177 /* Add a symbol to the output symbol table. */
8180 elf_link_output_sym (struct elf_final_link_info
*finfo
,
8182 Elf_Internal_Sym
*elfsym
,
8183 asection
*input_sec
,
8184 struct elf_link_hash_entry
*h
)
8187 Elf_External_Sym_Shndx
*destshndx
;
8188 bfd_boolean (*output_symbol_hook
)
8189 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
8190 struct elf_link_hash_entry
*);
8191 const struct elf_backend_data
*bed
;
8193 bed
= get_elf_backend_data (finfo
->output_bfd
);
8194 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
8195 if (output_symbol_hook
!= NULL
)
8197 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
8201 if (name
== NULL
|| *name
== '\0')
8202 elfsym
->st_name
= 0;
8203 else if (input_sec
->flags
& SEC_EXCLUDE
)
8204 elfsym
->st_name
= 0;
8207 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
8209 if (elfsym
->st_name
== (unsigned long) -1)
8213 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
8215 if (! elf_link_flush_output_syms (finfo
, bed
))
8219 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
8220 destshndx
= finfo
->symshndxbuf
;
8221 if (destshndx
!= NULL
)
8223 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
8227 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
8228 destshndx
= bfd_realloc (destshndx
, amt
* 2);
8229 if (destshndx
== NULL
)
8231 finfo
->symshndxbuf
= destshndx
;
8232 memset ((char *) destshndx
+ amt
, 0, amt
);
8233 finfo
->shndxbuf_size
*= 2;
8235 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
8238 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
8239 finfo
->symbuf_count
+= 1;
8240 bfd_get_symcount (finfo
->output_bfd
) += 1;
8245 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8248 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
8250 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
8251 && sym
->st_shndx
< SHN_LORESERVE
)
8253 /* The gABI doesn't support dynamic symbols in output sections
8255 (*_bfd_error_handler
)
8256 (_("%B: Too many sections: %d (>= %d)"),
8257 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
8258 bfd_set_error (bfd_error_nonrepresentable_section
);
8264 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8265 allowing an unsatisfied unversioned symbol in the DSO to match a
8266 versioned symbol that would normally require an explicit version.
8267 We also handle the case that a DSO references a hidden symbol
8268 which may be satisfied by a versioned symbol in another DSO. */
8271 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
8272 const struct elf_backend_data
*bed
,
8273 struct elf_link_hash_entry
*h
)
8276 struct elf_link_loaded_list
*loaded
;
8278 if (!is_elf_hash_table (info
->hash
))
8281 switch (h
->root
.type
)
8287 case bfd_link_hash_undefined
:
8288 case bfd_link_hash_undefweak
:
8289 abfd
= h
->root
.u
.undef
.abfd
;
8290 if ((abfd
->flags
& DYNAMIC
) == 0
8291 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
8295 case bfd_link_hash_defined
:
8296 case bfd_link_hash_defweak
:
8297 abfd
= h
->root
.u
.def
.section
->owner
;
8300 case bfd_link_hash_common
:
8301 abfd
= h
->root
.u
.c
.p
->section
->owner
;
8304 BFD_ASSERT (abfd
!= NULL
);
8306 for (loaded
= elf_hash_table (info
)->loaded
;
8308 loaded
= loaded
->next
)
8311 Elf_Internal_Shdr
*hdr
;
8312 bfd_size_type symcount
;
8313 bfd_size_type extsymcount
;
8314 bfd_size_type extsymoff
;
8315 Elf_Internal_Shdr
*versymhdr
;
8316 Elf_Internal_Sym
*isym
;
8317 Elf_Internal_Sym
*isymend
;
8318 Elf_Internal_Sym
*isymbuf
;
8319 Elf_External_Versym
*ever
;
8320 Elf_External_Versym
*extversym
;
8322 input
= loaded
->abfd
;
8324 /* We check each DSO for a possible hidden versioned definition. */
8326 || (input
->flags
& DYNAMIC
) == 0
8327 || elf_dynversym (input
) == 0)
8330 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
8332 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8333 if (elf_bad_symtab (input
))
8335 extsymcount
= symcount
;
8340 extsymcount
= symcount
- hdr
->sh_info
;
8341 extsymoff
= hdr
->sh_info
;
8344 if (extsymcount
== 0)
8347 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
8349 if (isymbuf
== NULL
)
8352 /* Read in any version definitions. */
8353 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
8354 extversym
= bfd_malloc (versymhdr
->sh_size
);
8355 if (extversym
== NULL
)
8358 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
8359 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
8360 != versymhdr
->sh_size
))
8368 ever
= extversym
+ extsymoff
;
8369 isymend
= isymbuf
+ extsymcount
;
8370 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
8373 Elf_Internal_Versym iver
;
8374 unsigned short version_index
;
8376 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
8377 || isym
->st_shndx
== SHN_UNDEF
)
8380 name
= bfd_elf_string_from_elf_section (input
,
8383 if (strcmp (name
, h
->root
.root
.string
) != 0)
8386 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
8388 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
8390 /* If we have a non-hidden versioned sym, then it should
8391 have provided a definition for the undefined sym. */
8395 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
8396 if (version_index
== 1 || version_index
== 2)
8398 /* This is the base or first version. We can use it. */
8412 /* Add an external symbol to the symbol table. This is called from
8413 the hash table traversal routine. When generating a shared object,
8414 we go through the symbol table twice. The first time we output
8415 anything that might have been forced to local scope in a version
8416 script. The second time we output the symbols that are still
8420 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
8422 struct elf_outext_info
*eoinfo
= data
;
8423 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
8425 Elf_Internal_Sym sym
;
8426 asection
*input_sec
;
8427 const struct elf_backend_data
*bed
;
8429 if (h
->root
.type
== bfd_link_hash_warning
)
8431 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8432 if (h
->root
.type
== bfd_link_hash_new
)
8436 /* Decide whether to output this symbol in this pass. */
8437 if (eoinfo
->localsyms
)
8439 if (!h
->forced_local
)
8444 if (h
->forced_local
)
8448 bed
= get_elf_backend_data (finfo
->output_bfd
);
8450 if (h
->root
.type
== bfd_link_hash_undefined
)
8452 /* If we have an undefined symbol reference here then it must have
8453 come from a shared library that is being linked in. (Undefined
8454 references in regular files have already been handled). */
8455 bfd_boolean ignore_undef
= FALSE
;
8457 /* Some symbols may be special in that the fact that they're
8458 undefined can be safely ignored - let backend determine that. */
8459 if (bed
->elf_backend_ignore_undef_symbol
)
8460 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
8462 /* If we are reporting errors for this situation then do so now. */
8463 if (ignore_undef
== FALSE
8466 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
8467 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
8469 if (! (finfo
->info
->callbacks
->undefined_symbol
8470 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
8471 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
8473 eoinfo
->failed
= TRUE
;
8479 /* We should also warn if a forced local symbol is referenced from
8480 shared libraries. */
8481 if (! finfo
->info
->relocatable
8482 && (! finfo
->info
->shared
)
8487 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
8489 (*_bfd_error_handler
)
8490 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
8492 h
->root
.u
.def
.section
== bfd_abs_section_ptr
8493 ? finfo
->output_bfd
: h
->root
.u
.def
.section
->owner
,
8494 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
8496 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
8497 ? "hidden" : "local",
8498 h
->root
.root
.string
);
8499 eoinfo
->failed
= TRUE
;
8503 /* We don't want to output symbols that have never been mentioned by
8504 a regular file, or that we have been told to strip. However, if
8505 h->indx is set to -2, the symbol is used by a reloc and we must
8509 else if ((h
->def_dynamic
8511 || h
->root
.type
== bfd_link_hash_new
)
8515 else if (finfo
->info
->strip
== strip_all
)
8517 else if (finfo
->info
->strip
== strip_some
8518 && bfd_hash_lookup (finfo
->info
->keep_hash
,
8519 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
8521 else if (finfo
->info
->strip_discarded
8522 && (h
->root
.type
== bfd_link_hash_defined
8523 || h
->root
.type
== bfd_link_hash_defweak
)
8524 && elf_discarded_section (h
->root
.u
.def
.section
))
8529 /* If we're stripping it, and it's not a dynamic symbol, there's
8530 nothing else to do unless it is a forced local symbol. */
8533 && !h
->forced_local
)
8537 sym
.st_size
= h
->size
;
8538 sym
.st_other
= h
->other
;
8539 if (h
->forced_local
)
8540 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
8541 else if (h
->root
.type
== bfd_link_hash_undefweak
8542 || h
->root
.type
== bfd_link_hash_defweak
)
8543 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
8545 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
8547 switch (h
->root
.type
)
8550 case bfd_link_hash_new
:
8551 case bfd_link_hash_warning
:
8555 case bfd_link_hash_undefined
:
8556 case bfd_link_hash_undefweak
:
8557 input_sec
= bfd_und_section_ptr
;
8558 sym
.st_shndx
= SHN_UNDEF
;
8561 case bfd_link_hash_defined
:
8562 case bfd_link_hash_defweak
:
8564 input_sec
= h
->root
.u
.def
.section
;
8565 if (input_sec
->output_section
!= NULL
)
8568 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
8569 input_sec
->output_section
);
8570 if (sym
.st_shndx
== SHN_BAD
)
8572 (*_bfd_error_handler
)
8573 (_("%B: could not find output section %A for input section %A"),
8574 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
8575 eoinfo
->failed
= TRUE
;
8579 /* ELF symbols in relocatable files are section relative,
8580 but in nonrelocatable files they are virtual
8582 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
8583 if (! finfo
->info
->relocatable
)
8585 sym
.st_value
+= input_sec
->output_section
->vma
;
8586 if (h
->type
== STT_TLS
)
8588 asection
*tls_sec
= elf_hash_table (finfo
->info
)->tls_sec
;
8589 if (tls_sec
!= NULL
)
8590 sym
.st_value
-= tls_sec
->vma
;
8593 /* The TLS section may have been garbage collected. */
8594 BFD_ASSERT (finfo
->info
->gc_sections
8595 && !input_sec
->gc_mark
);
8602 BFD_ASSERT (input_sec
->owner
== NULL
8603 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
8604 sym
.st_shndx
= SHN_UNDEF
;
8605 input_sec
= bfd_und_section_ptr
;
8610 case bfd_link_hash_common
:
8611 input_sec
= h
->root
.u
.c
.p
->section
;
8612 sym
.st_shndx
= bed
->common_section_index (input_sec
);
8613 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
8616 case bfd_link_hash_indirect
:
8617 /* These symbols are created by symbol versioning. They point
8618 to the decorated version of the name. For example, if the
8619 symbol foo@@GNU_1.2 is the default, which should be used when
8620 foo is used with no version, then we add an indirect symbol
8621 foo which points to foo@@GNU_1.2. We ignore these symbols,
8622 since the indirected symbol is already in the hash table. */
8626 /* Give the processor backend a chance to tweak the symbol value,
8627 and also to finish up anything that needs to be done for this
8628 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
8629 forced local syms when non-shared is due to a historical quirk. */
8630 if ((h
->dynindx
!= -1
8632 && ((finfo
->info
->shared
8633 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
8634 || h
->root
.type
!= bfd_link_hash_undefweak
))
8635 || !h
->forced_local
)
8636 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
8638 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
8639 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
8641 eoinfo
->failed
= TRUE
;
8646 /* If we are marking the symbol as undefined, and there are no
8647 non-weak references to this symbol from a regular object, then
8648 mark the symbol as weak undefined; if there are non-weak
8649 references, mark the symbol as strong. We can't do this earlier,
8650 because it might not be marked as undefined until the
8651 finish_dynamic_symbol routine gets through with it. */
8652 if (sym
.st_shndx
== SHN_UNDEF
8654 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
8655 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
8659 if (h
->ref_regular_nonweak
)
8660 bindtype
= STB_GLOBAL
;
8662 bindtype
= STB_WEAK
;
8663 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
8666 /* If this is a symbol defined in a dynamic library, don't use the
8667 symbol size from the dynamic library. Relinking an executable
8668 against a new library may introduce gratuitous changes in the
8669 executable's symbols if we keep the size. */
8670 if (sym
.st_shndx
== SHN_UNDEF
8675 /* If a non-weak symbol with non-default visibility is not defined
8676 locally, it is a fatal error. */
8677 if (! finfo
->info
->relocatable
8678 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
8679 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
8680 && h
->root
.type
== bfd_link_hash_undefined
8683 (*_bfd_error_handler
)
8684 (_("%B: %s symbol `%s' isn't defined"),
8686 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
8688 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
8689 ? "internal" : "hidden",
8690 h
->root
.root
.string
);
8691 eoinfo
->failed
= TRUE
;
8695 /* If this symbol should be put in the .dynsym section, then put it
8696 there now. We already know the symbol index. We also fill in
8697 the entry in the .hash section. */
8698 if (h
->dynindx
!= -1
8699 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
8703 sym
.st_name
= h
->dynstr_index
;
8704 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
8705 if (! check_dynsym (finfo
->output_bfd
, &sym
))
8707 eoinfo
->failed
= TRUE
;
8710 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
8712 if (finfo
->hash_sec
!= NULL
)
8714 size_t hash_entry_size
;
8715 bfd_byte
*bucketpos
;
8720 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
8721 bucket
= h
->u
.elf_hash_value
% bucketcount
;
8724 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
8725 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
8726 + (bucket
+ 2) * hash_entry_size
);
8727 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
8728 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
8729 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
8730 ((bfd_byte
*) finfo
->hash_sec
->contents
8731 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
8734 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
8736 Elf_Internal_Versym iversym
;
8737 Elf_External_Versym
*eversym
;
8739 if (!h
->def_regular
)
8741 if (h
->verinfo
.verdef
== NULL
)
8742 iversym
.vs_vers
= 0;
8744 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
8748 if (h
->verinfo
.vertree
== NULL
)
8749 iversym
.vs_vers
= 1;
8751 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
8752 if (finfo
->info
->create_default_symver
)
8757 iversym
.vs_vers
|= VERSYM_HIDDEN
;
8759 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
8760 eversym
+= h
->dynindx
;
8761 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
8765 /* If we're stripping it, then it was just a dynamic symbol, and
8766 there's nothing else to do. */
8767 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
8770 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
8772 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
8774 eoinfo
->failed
= TRUE
;
8781 /* Return TRUE if special handling is done for relocs in SEC against
8782 symbols defined in discarded sections. */
8785 elf_section_ignore_discarded_relocs (asection
*sec
)
8787 const struct elf_backend_data
*bed
;
8789 switch (sec
->sec_info_type
)
8791 case ELF_INFO_TYPE_STABS
:
8792 case ELF_INFO_TYPE_EH_FRAME
:
8798 bed
= get_elf_backend_data (sec
->owner
);
8799 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
8800 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
8806 /* Return a mask saying how ld should treat relocations in SEC against
8807 symbols defined in discarded sections. If this function returns
8808 COMPLAIN set, ld will issue a warning message. If this function
8809 returns PRETEND set, and the discarded section was link-once and the
8810 same size as the kept link-once section, ld will pretend that the
8811 symbol was actually defined in the kept section. Otherwise ld will
8812 zero the reloc (at least that is the intent, but some cooperation by
8813 the target dependent code is needed, particularly for REL targets). */
8816 _bfd_elf_default_action_discarded (asection
*sec
)
8818 if (sec
->flags
& SEC_DEBUGGING
)
8821 if (strcmp (".eh_frame", sec
->name
) == 0)
8824 if (strcmp (".gcc_except_table", sec
->name
) == 0)
8827 return COMPLAIN
| PRETEND
;
8830 /* Find a match between a section and a member of a section group. */
8833 match_group_member (asection
*sec
, asection
*group
,
8834 struct bfd_link_info
*info
)
8836 asection
*first
= elf_next_in_group (group
);
8837 asection
*s
= first
;
8841 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
8844 s
= elf_next_in_group (s
);
8852 /* Check if the kept section of a discarded section SEC can be used
8853 to replace it. Return the replacement if it is OK. Otherwise return
8857 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
8861 kept
= sec
->kept_section
;
8864 if ((kept
->flags
& SEC_GROUP
) != 0)
8865 kept
= match_group_member (sec
, kept
, info
);
8867 && ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
8868 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
)))
8870 sec
->kept_section
= kept
;
8875 /* Link an input file into the linker output file. This function
8876 handles all the sections and relocations of the input file at once.
8877 This is so that we only have to read the local symbols once, and
8878 don't have to keep them in memory. */
8881 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
8883 int (*relocate_section
)
8884 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
8885 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
8887 Elf_Internal_Shdr
*symtab_hdr
;
8890 Elf_Internal_Sym
*isymbuf
;
8891 Elf_Internal_Sym
*isym
;
8892 Elf_Internal_Sym
*isymend
;
8894 asection
**ppsection
;
8896 const struct elf_backend_data
*bed
;
8897 struct elf_link_hash_entry
**sym_hashes
;
8899 output_bfd
= finfo
->output_bfd
;
8900 bed
= get_elf_backend_data (output_bfd
);
8901 relocate_section
= bed
->elf_backend_relocate_section
;
8903 /* If this is a dynamic object, we don't want to do anything here:
8904 we don't want the local symbols, and we don't want the section
8906 if ((input_bfd
->flags
& DYNAMIC
) != 0)
8909 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8910 if (elf_bad_symtab (input_bfd
))
8912 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8917 locsymcount
= symtab_hdr
->sh_info
;
8918 extsymoff
= symtab_hdr
->sh_info
;
8921 /* Read the local symbols. */
8922 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8923 if (isymbuf
== NULL
&& locsymcount
!= 0)
8925 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
8926 finfo
->internal_syms
,
8927 finfo
->external_syms
,
8928 finfo
->locsym_shndx
);
8929 if (isymbuf
== NULL
)
8933 /* Find local symbol sections and adjust values of symbols in
8934 SEC_MERGE sections. Write out those local symbols we know are
8935 going into the output file. */
8936 isymend
= isymbuf
+ locsymcount
;
8937 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
8939 isym
++, pindex
++, ppsection
++)
8943 Elf_Internal_Sym osym
;
8947 if (elf_bad_symtab (input_bfd
))
8949 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
8956 if (isym
->st_shndx
== SHN_UNDEF
)
8957 isec
= bfd_und_section_ptr
;
8958 else if (isym
->st_shndx
== SHN_ABS
)
8959 isec
= bfd_abs_section_ptr
;
8960 else if (isym
->st_shndx
== SHN_COMMON
)
8961 isec
= bfd_com_section_ptr
;
8964 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
8967 /* Don't attempt to output symbols with st_shnx in the
8968 reserved range other than SHN_ABS and SHN_COMMON. */
8972 else if (isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
8973 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
8975 _bfd_merged_section_offset (output_bfd
, &isec
,
8976 elf_section_data (isec
)->sec_info
,
8982 /* Don't output the first, undefined, symbol. */
8983 if (ppsection
== finfo
->sections
)
8986 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
8988 /* We never output section symbols. Instead, we use the
8989 section symbol of the corresponding section in the output
8994 /* If we are stripping all symbols, we don't want to output this
8996 if (finfo
->info
->strip
== strip_all
)
8999 /* If we are discarding all local symbols, we don't want to
9000 output this one. If we are generating a relocatable output
9001 file, then some of the local symbols may be required by
9002 relocs; we output them below as we discover that they are
9004 if (finfo
->info
->discard
== discard_all
)
9007 /* If this symbol is defined in a section which we are
9008 discarding, we don't need to keep it. */
9009 if (isym
->st_shndx
!= SHN_UNDEF
9010 && isym
->st_shndx
< SHN_LORESERVE
9011 && bfd_section_removed_from_list (output_bfd
,
9012 isec
->output_section
))
9015 /* Get the name of the symbol. */
9016 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
9021 /* See if we are discarding symbols with this name. */
9022 if ((finfo
->info
->strip
== strip_some
9023 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
9025 || (((finfo
->info
->discard
== discard_sec_merge
9026 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
9027 || finfo
->info
->discard
== discard_l
)
9028 && bfd_is_local_label_name (input_bfd
, name
)))
9031 /* If we get here, we are going to output this symbol. */
9035 /* Adjust the section index for the output file. */
9036 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
9037 isec
->output_section
);
9038 if (osym
.st_shndx
== SHN_BAD
)
9041 *pindex
= bfd_get_symcount (output_bfd
);
9043 /* ELF symbols in relocatable files are section relative, but
9044 in executable files they are virtual addresses. Note that
9045 this code assumes that all ELF sections have an associated
9046 BFD section with a reasonable value for output_offset; below
9047 we assume that they also have a reasonable value for
9048 output_section. Any special sections must be set up to meet
9049 these requirements. */
9050 osym
.st_value
+= isec
->output_offset
;
9051 if (! finfo
->info
->relocatable
)
9053 osym
.st_value
+= isec
->output_section
->vma
;
9054 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
9056 /* STT_TLS symbols are relative to PT_TLS segment base. */
9057 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
9058 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
9062 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
9066 /* Relocate the contents of each section. */
9067 sym_hashes
= elf_sym_hashes (input_bfd
);
9068 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
9072 if (! o
->linker_mark
)
9074 /* This section was omitted from the link. */
9078 if (finfo
->info
->relocatable
9079 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
9081 /* Deal with the group signature symbol. */
9082 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
9083 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
9084 asection
*osec
= o
->output_section
;
9086 if (symndx
>= locsymcount
9087 || (elf_bad_symtab (input_bfd
)
9088 && finfo
->sections
[symndx
] == NULL
))
9090 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
9091 while (h
->root
.type
== bfd_link_hash_indirect
9092 || h
->root
.type
== bfd_link_hash_warning
)
9093 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9094 /* Arrange for symbol to be output. */
9096 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
9098 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
9100 /* We'll use the output section target_index. */
9101 asection
*sec
= finfo
->sections
[symndx
]->output_section
;
9102 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
9106 if (finfo
->indices
[symndx
] == -1)
9108 /* Otherwise output the local symbol now. */
9109 Elf_Internal_Sym sym
= isymbuf
[symndx
];
9110 asection
*sec
= finfo
->sections
[symndx
]->output_section
;
9113 name
= bfd_elf_string_from_elf_section (input_bfd
,
9114 symtab_hdr
->sh_link
,
9119 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
9121 if (sym
.st_shndx
== SHN_BAD
)
9124 sym
.st_value
+= o
->output_offset
;
9126 finfo
->indices
[symndx
] = bfd_get_symcount (output_bfd
);
9127 if (! elf_link_output_sym (finfo
, name
, &sym
, o
, NULL
))
9130 elf_section_data (osec
)->this_hdr
.sh_info
9131 = finfo
->indices
[symndx
];
9135 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
9136 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
9139 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
9141 /* Section was created by _bfd_elf_link_create_dynamic_sections
9146 /* Get the contents of the section. They have been cached by a
9147 relaxation routine. Note that o is a section in an input
9148 file, so the contents field will not have been set by any of
9149 the routines which work on output files. */
9150 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
9151 contents
= elf_section_data (o
)->this_hdr
.contents
;
9154 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
9156 contents
= finfo
->contents
;
9157 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
9161 if ((o
->flags
& SEC_RELOC
) != 0)
9163 Elf_Internal_Rela
*internal_relocs
;
9164 Elf_Internal_Rela
*rel
, *relend
;
9165 bfd_vma r_type_mask
;
9167 int action_discarded
;
9170 /* Get the swapped relocs. */
9172 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
9173 finfo
->internal_relocs
, FALSE
);
9174 if (internal_relocs
== NULL
9175 && o
->reloc_count
> 0)
9178 if (bed
->s
->arch_size
== 32)
9185 r_type_mask
= 0xffffffff;
9189 action_discarded
= -1;
9190 if (!elf_section_ignore_discarded_relocs (o
))
9191 action_discarded
= (*bed
->action_discarded
) (o
);
9193 /* Run through the relocs evaluating complex reloc symbols and
9194 looking for relocs against symbols from discarded sections
9195 or section symbols from removed link-once sections.
9196 Complain about relocs against discarded sections. Zero
9197 relocs against removed link-once sections. */
9199 rel
= internal_relocs
;
9200 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9201 for ( ; rel
< relend
; rel
++)
9203 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
9204 unsigned int s_type
;
9205 asection
**ps
, *sec
;
9206 struct elf_link_hash_entry
*h
= NULL
;
9207 const char *sym_name
;
9209 if (r_symndx
== STN_UNDEF
)
9212 if (r_symndx
>= locsymcount
9213 || (elf_bad_symtab (input_bfd
)
9214 && finfo
->sections
[r_symndx
] == NULL
))
9216 h
= sym_hashes
[r_symndx
- extsymoff
];
9218 /* Badly formatted input files can contain relocs that
9219 reference non-existant symbols. Check here so that
9220 we do not seg fault. */
9225 sprintf_vma (buffer
, rel
->r_info
);
9226 (*_bfd_error_handler
)
9227 (_("error: %B contains a reloc (0x%s) for section %A "
9228 "that references a non-existent global symbol"),
9229 input_bfd
, o
, buffer
);
9230 bfd_set_error (bfd_error_bad_value
);
9234 while (h
->root
.type
== bfd_link_hash_indirect
9235 || h
->root
.type
== bfd_link_hash_warning
)
9236 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9241 if (h
->root
.type
== bfd_link_hash_defined
9242 || h
->root
.type
== bfd_link_hash_defweak
)
9243 ps
= &h
->root
.u
.def
.section
;
9245 sym_name
= h
->root
.root
.string
;
9249 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
9251 s_type
= ELF_ST_TYPE (sym
->st_info
);
9252 ps
= &finfo
->sections
[r_symndx
];
9253 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
9257 if (s_type
== STT_RELC
|| s_type
== STT_SRELC
)
9260 bfd_vma dot
= (rel
->r_offset
9261 + o
->output_offset
+ o
->output_section
->vma
);
9263 printf ("Encountered a complex symbol!");
9264 printf (" (input_bfd %s, section %s, reloc %ld\n",
9265 input_bfd
->filename
, o
->name
, rel
- internal_relocs
);
9266 printf (" symbol: idx %8.8lx, name %s\n",
9267 r_symndx
, sym_name
);
9268 printf (" reloc : info %8.8lx, addr %8.8lx\n",
9269 (unsigned long) rel
->r_info
,
9270 (unsigned long) rel
->r_offset
);
9272 if (!eval_symbol (&val
, &sym_name
, input_bfd
, finfo
, dot
,
9273 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
9276 /* Symbol evaluated OK. Update to absolute value. */
9277 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
9282 if (action_discarded
!= -1 && ps
!= NULL
)
9284 /* Complain if the definition comes from a
9285 discarded section. */
9286 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
9288 BFD_ASSERT (r_symndx
!= 0);
9289 if (action_discarded
& COMPLAIN
)
9290 (*finfo
->info
->callbacks
->einfo
)
9291 (_("%X`%s' referenced in section `%A' of %B: "
9292 "defined in discarded section `%A' of %B\n"),
9293 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
9295 /* Try to do the best we can to support buggy old
9296 versions of gcc. Pretend that the symbol is
9297 really defined in the kept linkonce section.
9298 FIXME: This is quite broken. Modifying the
9299 symbol here means we will be changing all later
9300 uses of the symbol, not just in this section. */
9301 if (action_discarded
& PRETEND
)
9305 kept
= _bfd_elf_check_kept_section (sec
,
9317 /* Relocate the section by invoking a back end routine.
9319 The back end routine is responsible for adjusting the
9320 section contents as necessary, and (if using Rela relocs
9321 and generating a relocatable output file) adjusting the
9322 reloc addend as necessary.
9324 The back end routine does not have to worry about setting
9325 the reloc address or the reloc symbol index.
9327 The back end routine is given a pointer to the swapped in
9328 internal symbols, and can access the hash table entries
9329 for the external symbols via elf_sym_hashes (input_bfd).
9331 When generating relocatable output, the back end routine
9332 must handle STB_LOCAL/STT_SECTION symbols specially. The
9333 output symbol is going to be a section symbol
9334 corresponding to the output section, which will require
9335 the addend to be adjusted. */
9337 ret
= (*relocate_section
) (output_bfd
, finfo
->info
,
9338 input_bfd
, o
, contents
,
9346 || finfo
->info
->relocatable
9347 || finfo
->info
->emitrelocations
)
9349 Elf_Internal_Rela
*irela
;
9350 Elf_Internal_Rela
*irelaend
;
9351 bfd_vma last_offset
;
9352 struct elf_link_hash_entry
**rel_hash
;
9353 struct elf_link_hash_entry
**rel_hash_list
;
9354 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
9355 unsigned int next_erel
;
9356 bfd_boolean rela_normal
;
9358 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
9359 rela_normal
= (bed
->rela_normal
9360 && (input_rel_hdr
->sh_entsize
9361 == bed
->s
->sizeof_rela
));
9363 /* Adjust the reloc addresses and symbol indices. */
9365 irela
= internal_relocs
;
9366 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9367 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
9368 + elf_section_data (o
->output_section
)->rel_count
9369 + elf_section_data (o
->output_section
)->rel_count2
);
9370 rel_hash_list
= rel_hash
;
9371 last_offset
= o
->output_offset
;
9372 if (!finfo
->info
->relocatable
)
9373 last_offset
+= o
->output_section
->vma
;
9374 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
9376 unsigned long r_symndx
;
9378 Elf_Internal_Sym sym
;
9380 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
9386 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
9389 if (irela
->r_offset
>= (bfd_vma
) -2)
9391 /* This is a reloc for a deleted entry or somesuch.
9392 Turn it into an R_*_NONE reloc, at the same
9393 offset as the last reloc. elf_eh_frame.c and
9394 bfd_elf_discard_info rely on reloc offsets
9396 irela
->r_offset
= last_offset
;
9398 irela
->r_addend
= 0;
9402 irela
->r_offset
+= o
->output_offset
;
9404 /* Relocs in an executable have to be virtual addresses. */
9405 if (!finfo
->info
->relocatable
)
9406 irela
->r_offset
+= o
->output_section
->vma
;
9408 last_offset
= irela
->r_offset
;
9410 r_symndx
= irela
->r_info
>> r_sym_shift
;
9411 if (r_symndx
== STN_UNDEF
)
9414 if (r_symndx
>= locsymcount
9415 || (elf_bad_symtab (input_bfd
)
9416 && finfo
->sections
[r_symndx
] == NULL
))
9418 struct elf_link_hash_entry
*rh
;
9421 /* This is a reloc against a global symbol. We
9422 have not yet output all the local symbols, so
9423 we do not know the symbol index of any global
9424 symbol. We set the rel_hash entry for this
9425 reloc to point to the global hash table entry
9426 for this symbol. The symbol index is then
9427 set at the end of bfd_elf_final_link. */
9428 indx
= r_symndx
- extsymoff
;
9429 rh
= elf_sym_hashes (input_bfd
)[indx
];
9430 while (rh
->root
.type
== bfd_link_hash_indirect
9431 || rh
->root
.type
== bfd_link_hash_warning
)
9432 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
9434 /* Setting the index to -2 tells
9435 elf_link_output_extsym that this symbol is
9437 BFD_ASSERT (rh
->indx
< 0);
9445 /* This is a reloc against a local symbol. */
9448 sym
= isymbuf
[r_symndx
];
9449 sec
= finfo
->sections
[r_symndx
];
9450 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
9452 /* I suppose the backend ought to fill in the
9453 section of any STT_SECTION symbol against a
9454 processor specific section. */
9456 if (bfd_is_abs_section (sec
))
9458 else if (sec
== NULL
|| sec
->owner
== NULL
)
9460 bfd_set_error (bfd_error_bad_value
);
9465 asection
*osec
= sec
->output_section
;
9467 /* If we have discarded a section, the output
9468 section will be the absolute section. In
9469 case of discarded SEC_MERGE sections, use
9470 the kept section. relocate_section should
9471 have already handled discarded linkonce
9473 if (bfd_is_abs_section (osec
)
9474 && sec
->kept_section
!= NULL
9475 && sec
->kept_section
->output_section
!= NULL
)
9477 osec
= sec
->kept_section
->output_section
;
9478 irela
->r_addend
-= osec
->vma
;
9481 if (!bfd_is_abs_section (osec
))
9483 r_symndx
= osec
->target_index
;
9486 struct elf_link_hash_table
*htab
;
9489 htab
= elf_hash_table (finfo
->info
);
9490 oi
= htab
->text_index_section
;
9491 if ((osec
->flags
& SEC_READONLY
) == 0
9492 && htab
->data_index_section
!= NULL
)
9493 oi
= htab
->data_index_section
;
9497 irela
->r_addend
+= osec
->vma
- oi
->vma
;
9498 r_symndx
= oi
->target_index
;
9502 BFD_ASSERT (r_symndx
!= 0);
9506 /* Adjust the addend according to where the
9507 section winds up in the output section. */
9509 irela
->r_addend
+= sec
->output_offset
;
9513 if (finfo
->indices
[r_symndx
] == -1)
9515 unsigned long shlink
;
9519 if (finfo
->info
->strip
== strip_all
)
9521 /* You can't do ld -r -s. */
9522 bfd_set_error (bfd_error_invalid_operation
);
9526 /* This symbol was skipped earlier, but
9527 since it is needed by a reloc, we
9528 must output it now. */
9529 shlink
= symtab_hdr
->sh_link
;
9530 name
= (bfd_elf_string_from_elf_section
9531 (input_bfd
, shlink
, sym
.st_name
));
9535 osec
= sec
->output_section
;
9537 _bfd_elf_section_from_bfd_section (output_bfd
,
9539 if (sym
.st_shndx
== SHN_BAD
)
9542 sym
.st_value
+= sec
->output_offset
;
9543 if (! finfo
->info
->relocatable
)
9545 sym
.st_value
+= osec
->vma
;
9546 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
9548 /* STT_TLS symbols are relative to PT_TLS
9550 BFD_ASSERT (elf_hash_table (finfo
->info
)
9552 sym
.st_value
-= (elf_hash_table (finfo
->info
)
9557 finfo
->indices
[r_symndx
]
9558 = bfd_get_symcount (output_bfd
);
9560 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
9565 r_symndx
= finfo
->indices
[r_symndx
];
9568 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
9569 | (irela
->r_info
& r_type_mask
));
9572 /* Swap out the relocs. */
9573 if (input_rel_hdr
->sh_size
!= 0
9574 && !bed
->elf_backend_emit_relocs (output_bfd
, o
,
9580 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
9581 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
9583 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
9584 * bed
->s
->int_rels_per_ext_rel
);
9585 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
9586 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
9595 /* Write out the modified section contents. */
9596 if (bed
->elf_backend_write_section
9597 && (*bed
->elf_backend_write_section
) (output_bfd
, finfo
->info
, o
,
9600 /* Section written out. */
9602 else switch (o
->sec_info_type
)
9604 case ELF_INFO_TYPE_STABS
:
9605 if (! (_bfd_write_section_stabs
9607 &elf_hash_table (finfo
->info
)->stab_info
,
9608 o
, &elf_section_data (o
)->sec_info
, contents
)))
9611 case ELF_INFO_TYPE_MERGE
:
9612 if (! _bfd_write_merged_section (output_bfd
, o
,
9613 elf_section_data (o
)->sec_info
))
9616 case ELF_INFO_TYPE_EH_FRAME
:
9618 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
9625 if (! (o
->flags
& SEC_EXCLUDE
)
9626 && ! (o
->output_section
->flags
& SEC_NEVER_LOAD
)
9627 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
9629 (file_ptr
) o
->output_offset
,
9640 /* Generate a reloc when linking an ELF file. This is a reloc
9641 requested by the linker, and does not come from any input file. This
9642 is used to build constructor and destructor tables when linking
9646 elf_reloc_link_order (bfd
*output_bfd
,
9647 struct bfd_link_info
*info
,
9648 asection
*output_section
,
9649 struct bfd_link_order
*link_order
)
9651 reloc_howto_type
*howto
;
9655 struct elf_link_hash_entry
**rel_hash_ptr
;
9656 Elf_Internal_Shdr
*rel_hdr
;
9657 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
9658 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
9662 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
9665 bfd_set_error (bfd_error_bad_value
);
9669 addend
= link_order
->u
.reloc
.p
->addend
;
9671 /* Figure out the symbol index. */
9672 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
9673 + elf_section_data (output_section
)->rel_count
9674 + elf_section_data (output_section
)->rel_count2
);
9675 if (link_order
->type
== bfd_section_reloc_link_order
)
9677 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
9678 BFD_ASSERT (indx
!= 0);
9679 *rel_hash_ptr
= NULL
;
9683 struct elf_link_hash_entry
*h
;
9685 /* Treat a reloc against a defined symbol as though it were
9686 actually against the section. */
9687 h
= ((struct elf_link_hash_entry
*)
9688 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
9689 link_order
->u
.reloc
.p
->u
.name
,
9690 FALSE
, FALSE
, TRUE
));
9692 && (h
->root
.type
== bfd_link_hash_defined
9693 || h
->root
.type
== bfd_link_hash_defweak
))
9697 section
= h
->root
.u
.def
.section
;
9698 indx
= section
->output_section
->target_index
;
9699 *rel_hash_ptr
= NULL
;
9700 /* It seems that we ought to add the symbol value to the
9701 addend here, but in practice it has already been added
9702 because it was passed to constructor_callback. */
9703 addend
+= section
->output_section
->vma
+ section
->output_offset
;
9707 /* Setting the index to -2 tells elf_link_output_extsym that
9708 this symbol is used by a reloc. */
9715 if (! ((*info
->callbacks
->unattached_reloc
)
9716 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
9722 /* If this is an inplace reloc, we must write the addend into the
9724 if (howto
->partial_inplace
&& addend
!= 0)
9727 bfd_reloc_status_type rstat
;
9730 const char *sym_name
;
9732 size
= bfd_get_reloc_size (howto
);
9733 buf
= bfd_zmalloc (size
);
9736 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
9743 case bfd_reloc_outofrange
:
9746 case bfd_reloc_overflow
:
9747 if (link_order
->type
== bfd_section_reloc_link_order
)
9748 sym_name
= bfd_section_name (output_bfd
,
9749 link_order
->u
.reloc
.p
->u
.section
);
9751 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
9752 if (! ((*info
->callbacks
->reloc_overflow
)
9753 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
9754 NULL
, (bfd_vma
) 0)))
9761 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
9762 link_order
->offset
, size
);
9768 /* The address of a reloc is relative to the section in a
9769 relocatable file, and is a virtual address in an executable
9771 offset
= link_order
->offset
;
9772 if (! info
->relocatable
)
9773 offset
+= output_section
->vma
;
9775 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
9777 irel
[i
].r_offset
= offset
;
9779 irel
[i
].r_addend
= 0;
9781 if (bed
->s
->arch_size
== 32)
9782 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
9784 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
9786 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
9787 erel
= rel_hdr
->contents
;
9788 if (rel_hdr
->sh_type
== SHT_REL
)
9790 erel
+= (elf_section_data (output_section
)->rel_count
9791 * bed
->s
->sizeof_rel
);
9792 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
9796 irel
[0].r_addend
= addend
;
9797 erel
+= (elf_section_data (output_section
)->rel_count
9798 * bed
->s
->sizeof_rela
);
9799 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
9802 ++elf_section_data (output_section
)->rel_count
;
9808 /* Get the output vma of the section pointed to by the sh_link field. */
9811 elf_get_linked_section_vma (struct bfd_link_order
*p
)
9813 Elf_Internal_Shdr
**elf_shdrp
;
9817 s
= p
->u
.indirect
.section
;
9818 elf_shdrp
= elf_elfsections (s
->owner
);
9819 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
9820 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
9822 The Intel C compiler generates SHT_IA_64_UNWIND with
9823 SHF_LINK_ORDER. But it doesn't set the sh_link or
9824 sh_info fields. Hence we could get the situation
9825 where elfsec is 0. */
9828 const struct elf_backend_data
*bed
9829 = get_elf_backend_data (s
->owner
);
9830 if (bed
->link_order_error_handler
)
9831 bed
->link_order_error_handler
9832 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
9837 s
= elf_shdrp
[elfsec
]->bfd_section
;
9838 return s
->output_section
->vma
+ s
->output_offset
;
9843 /* Compare two sections based on the locations of the sections they are
9844 linked to. Used by elf_fixup_link_order. */
9847 compare_link_order (const void * a
, const void * b
)
9852 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
9853 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
9860 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
9861 order as their linked sections. Returns false if this could not be done
9862 because an output section includes both ordered and unordered
9863 sections. Ideally we'd do this in the linker proper. */
9866 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
9871 struct bfd_link_order
*p
;
9873 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9875 struct bfd_link_order
**sections
;
9876 asection
*s
, *other_sec
, *linkorder_sec
;
9880 linkorder_sec
= NULL
;
9883 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9885 if (p
->type
== bfd_indirect_link_order
)
9887 s
= p
->u
.indirect
.section
;
9889 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
9890 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
9891 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
9892 && elfsec
< elf_numsections (sub
)
9893 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
9894 && elf_elfsections (sub
)[elfsec
]->sh_link
< elf_numsections (sub
))
9908 if (seen_other
&& seen_linkorder
)
9910 if (other_sec
&& linkorder_sec
)
9911 (*_bfd_error_handler
) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
9913 linkorder_sec
->owner
, other_sec
,
9916 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
9918 bfd_set_error (bfd_error_bad_value
);
9923 if (!seen_linkorder
)
9926 sections
= (struct bfd_link_order
**)
9927 bfd_malloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
9928 if (sections
== NULL
)
9932 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
9934 sections
[seen_linkorder
++] = p
;
9936 /* Sort the input sections in the order of their linked section. */
9937 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
9938 compare_link_order
);
9940 /* Change the offsets of the sections. */
9942 for (n
= 0; n
< seen_linkorder
; n
++)
9944 s
= sections
[n
]->u
.indirect
.section
;
9945 offset
&= ~(bfd_vma
) 0 << s
->alignment_power
;
9946 s
->output_offset
= offset
;
9947 sections
[n
]->offset
= offset
;
9948 offset
+= sections
[n
]->size
;
9956 /* Do the final step of an ELF link. */
9959 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9961 bfd_boolean dynamic
;
9962 bfd_boolean emit_relocs
;
9964 struct elf_final_link_info finfo
;
9965 register asection
*o
;
9966 register struct bfd_link_order
*p
;
9968 bfd_size_type max_contents_size
;
9969 bfd_size_type max_external_reloc_size
;
9970 bfd_size_type max_internal_reloc_count
;
9971 bfd_size_type max_sym_count
;
9972 bfd_size_type max_sym_shndx_count
;
9974 Elf_Internal_Sym elfsym
;
9976 Elf_Internal_Shdr
*symtab_hdr
;
9977 Elf_Internal_Shdr
*symtab_shndx_hdr
;
9978 Elf_Internal_Shdr
*symstrtab_hdr
;
9979 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9980 struct elf_outext_info eoinfo
;
9982 size_t relativecount
= 0;
9983 asection
*reldyn
= 0;
9985 asection
*attr_section
= NULL
;
9986 bfd_vma attr_size
= 0;
9987 const char *std_attrs_section
;
9989 if (! is_elf_hash_table (info
->hash
))
9993 abfd
->flags
|= DYNAMIC
;
9995 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
9996 dynobj
= elf_hash_table (info
)->dynobj
;
9998 emit_relocs
= (info
->relocatable
9999 || info
->emitrelocations
);
10002 finfo
.output_bfd
= abfd
;
10003 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
10004 if (finfo
.symstrtab
== NULL
)
10009 finfo
.dynsym_sec
= NULL
;
10010 finfo
.hash_sec
= NULL
;
10011 finfo
.symver_sec
= NULL
;
10015 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
10016 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
10017 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
);
10018 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
10019 /* Note that it is OK if symver_sec is NULL. */
10022 finfo
.contents
= NULL
;
10023 finfo
.external_relocs
= NULL
;
10024 finfo
.internal_relocs
= NULL
;
10025 finfo
.external_syms
= NULL
;
10026 finfo
.locsym_shndx
= NULL
;
10027 finfo
.internal_syms
= NULL
;
10028 finfo
.indices
= NULL
;
10029 finfo
.sections
= NULL
;
10030 finfo
.symbuf
= NULL
;
10031 finfo
.symshndxbuf
= NULL
;
10032 finfo
.symbuf_count
= 0;
10033 finfo
.shndxbuf_size
= 0;
10035 /* The object attributes have been merged. Remove the input
10036 sections from the link, and set the contents of the output
10038 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
10039 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10041 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
10042 || strcmp (o
->name
, ".gnu.attributes") == 0)
10044 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10046 asection
*input_section
;
10048 if (p
->type
!= bfd_indirect_link_order
)
10050 input_section
= p
->u
.indirect
.section
;
10051 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10052 elf_link_input_bfd ignores this section. */
10053 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
10056 attr_size
= bfd_elf_obj_attr_size (abfd
);
10059 bfd_set_section_size (abfd
, o
, attr_size
);
10061 /* Skip this section later on. */
10062 o
->map_head
.link_order
= NULL
;
10065 o
->flags
|= SEC_EXCLUDE
;
10069 /* Count up the number of relocations we will output for each output
10070 section, so that we know the sizes of the reloc sections. We
10071 also figure out some maximum sizes. */
10072 max_contents_size
= 0;
10073 max_external_reloc_size
= 0;
10074 max_internal_reloc_count
= 0;
10076 max_sym_shndx_count
= 0;
10078 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10080 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
10081 o
->reloc_count
= 0;
10083 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10085 unsigned int reloc_count
= 0;
10086 struct bfd_elf_section_data
*esdi
= NULL
;
10087 unsigned int *rel_count1
;
10089 if (p
->type
== bfd_section_reloc_link_order
10090 || p
->type
== bfd_symbol_reloc_link_order
)
10092 else if (p
->type
== bfd_indirect_link_order
)
10096 sec
= p
->u
.indirect
.section
;
10097 esdi
= elf_section_data (sec
);
10099 /* Mark all sections which are to be included in the
10100 link. This will normally be every section. We need
10101 to do this so that we can identify any sections which
10102 the linker has decided to not include. */
10103 sec
->linker_mark
= TRUE
;
10105 if (sec
->flags
& SEC_MERGE
)
10108 if (info
->relocatable
|| info
->emitrelocations
)
10109 reloc_count
= sec
->reloc_count
;
10110 else if (bed
->elf_backend_count_relocs
)
10111 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
10113 if (sec
->rawsize
> max_contents_size
)
10114 max_contents_size
= sec
->rawsize
;
10115 if (sec
->size
> max_contents_size
)
10116 max_contents_size
= sec
->size
;
10118 /* We are interested in just local symbols, not all
10120 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
10121 && (sec
->owner
->flags
& DYNAMIC
) == 0)
10125 if (elf_bad_symtab (sec
->owner
))
10126 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
10127 / bed
->s
->sizeof_sym
);
10129 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
10131 if (sym_count
> max_sym_count
)
10132 max_sym_count
= sym_count
;
10134 if (sym_count
> max_sym_shndx_count
10135 && elf_symtab_shndx (sec
->owner
) != 0)
10136 max_sym_shndx_count
= sym_count
;
10138 if ((sec
->flags
& SEC_RELOC
) != 0)
10142 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
10143 if (ext_size
> max_external_reloc_size
)
10144 max_external_reloc_size
= ext_size
;
10145 if (sec
->reloc_count
> max_internal_reloc_count
)
10146 max_internal_reloc_count
= sec
->reloc_count
;
10151 if (reloc_count
== 0)
10154 o
->reloc_count
+= reloc_count
;
10156 /* MIPS may have a mix of REL and RELA relocs on sections.
10157 To support this curious ABI we keep reloc counts in
10158 elf_section_data too. We must be careful to add the
10159 relocations from the input section to the right output
10160 count. FIXME: Get rid of one count. We have
10161 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
10162 rel_count1
= &esdo
->rel_count
;
10165 bfd_boolean same_size
;
10166 bfd_size_type entsize1
;
10168 entsize1
= esdi
->rel_hdr
.sh_entsize
;
10169 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
10170 || entsize1
== bed
->s
->sizeof_rela
);
10171 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
10174 rel_count1
= &esdo
->rel_count2
;
10176 if (esdi
->rel_hdr2
!= NULL
)
10178 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
10179 unsigned int alt_count
;
10180 unsigned int *rel_count2
;
10182 BFD_ASSERT (entsize2
!= entsize1
10183 && (entsize2
== bed
->s
->sizeof_rel
10184 || entsize2
== bed
->s
->sizeof_rela
));
10186 rel_count2
= &esdo
->rel_count2
;
10188 rel_count2
= &esdo
->rel_count
;
10190 /* The following is probably too simplistic if the
10191 backend counts output relocs unusually. */
10192 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
10193 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
10194 *rel_count2
+= alt_count
;
10195 reloc_count
-= alt_count
;
10198 *rel_count1
+= reloc_count
;
10201 if (o
->reloc_count
> 0)
10202 o
->flags
|= SEC_RELOC
;
10205 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10206 set it (this is probably a bug) and if it is set
10207 assign_section_numbers will create a reloc section. */
10208 o
->flags
&=~ SEC_RELOC
;
10211 /* If the SEC_ALLOC flag is not set, force the section VMA to
10212 zero. This is done in elf_fake_sections as well, but forcing
10213 the VMA to 0 here will ensure that relocs against these
10214 sections are handled correctly. */
10215 if ((o
->flags
& SEC_ALLOC
) == 0
10216 && ! o
->user_set_vma
)
10220 if (! info
->relocatable
&& merged
)
10221 elf_link_hash_traverse (elf_hash_table (info
),
10222 _bfd_elf_link_sec_merge_syms
, abfd
);
10224 /* Figure out the file positions for everything but the symbol table
10225 and the relocs. We set symcount to force assign_section_numbers
10226 to create a symbol table. */
10227 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
10228 BFD_ASSERT (! abfd
->output_has_begun
);
10229 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
10232 /* Set sizes, and assign file positions for reloc sections. */
10233 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10235 if ((o
->flags
& SEC_RELOC
) != 0)
10237 if (!(_bfd_elf_link_size_reloc_section
10238 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
10241 if (elf_section_data (o
)->rel_hdr2
10242 && !(_bfd_elf_link_size_reloc_section
10243 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
10247 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10248 to count upwards while actually outputting the relocations. */
10249 elf_section_data (o
)->rel_count
= 0;
10250 elf_section_data (o
)->rel_count2
= 0;
10253 _bfd_elf_assign_file_positions_for_relocs (abfd
);
10255 /* We have now assigned file positions for all the sections except
10256 .symtab and .strtab. We start the .symtab section at the current
10257 file position, and write directly to it. We build the .strtab
10258 section in memory. */
10259 bfd_get_symcount (abfd
) = 0;
10260 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
10261 /* sh_name is set in prep_headers. */
10262 symtab_hdr
->sh_type
= SHT_SYMTAB
;
10263 /* sh_flags, sh_addr and sh_size all start off zero. */
10264 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
10265 /* sh_link is set in assign_section_numbers. */
10266 /* sh_info is set below. */
10267 /* sh_offset is set just below. */
10268 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
10270 off
= elf_tdata (abfd
)->next_file_pos
;
10271 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
10273 /* Note that at this point elf_tdata (abfd)->next_file_pos is
10274 incorrect. We do not yet know the size of the .symtab section.
10275 We correct next_file_pos below, after we do know the size. */
10277 /* Allocate a buffer to hold swapped out symbols. This is to avoid
10278 continuously seeking to the right position in the file. */
10279 if (! info
->keep_memory
|| max_sym_count
< 20)
10280 finfo
.symbuf_size
= 20;
10282 finfo
.symbuf_size
= max_sym_count
;
10283 amt
= finfo
.symbuf_size
;
10284 amt
*= bed
->s
->sizeof_sym
;
10285 finfo
.symbuf
= bfd_malloc (amt
);
10286 if (finfo
.symbuf
== NULL
)
10288 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
10290 /* Wild guess at number of output symbols. realloc'd as needed. */
10291 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
10292 finfo
.shndxbuf_size
= amt
;
10293 amt
*= sizeof (Elf_External_Sym_Shndx
);
10294 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
10295 if (finfo
.symshndxbuf
== NULL
)
10299 /* Start writing out the symbol table. The first symbol is always a
10301 if (info
->strip
!= strip_all
10304 elfsym
.st_value
= 0;
10305 elfsym
.st_size
= 0;
10306 elfsym
.st_info
= 0;
10307 elfsym
.st_other
= 0;
10308 elfsym
.st_shndx
= SHN_UNDEF
;
10309 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
10314 /* Output a symbol for each section. We output these even if we are
10315 discarding local symbols, since they are used for relocs. These
10316 symbols have no names. We store the index of each one in the
10317 index field of the section, so that we can find it again when
10318 outputting relocs. */
10319 if (info
->strip
!= strip_all
10322 elfsym
.st_size
= 0;
10323 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
10324 elfsym
.st_other
= 0;
10325 elfsym
.st_value
= 0;
10326 for (i
= 1; i
< elf_numsections (abfd
); i
++)
10328 o
= bfd_section_from_elf_index (abfd
, i
);
10331 o
->target_index
= bfd_get_symcount (abfd
);
10332 elfsym
.st_shndx
= i
;
10333 if (!info
->relocatable
)
10334 elfsym
.st_value
= o
->vma
;
10335 if (!elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
10341 /* Allocate some memory to hold information read in from the input
10343 if (max_contents_size
!= 0)
10345 finfo
.contents
= bfd_malloc (max_contents_size
);
10346 if (finfo
.contents
== NULL
)
10350 if (max_external_reloc_size
!= 0)
10352 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
10353 if (finfo
.external_relocs
== NULL
)
10357 if (max_internal_reloc_count
!= 0)
10359 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10360 amt
*= sizeof (Elf_Internal_Rela
);
10361 finfo
.internal_relocs
= bfd_malloc (amt
);
10362 if (finfo
.internal_relocs
== NULL
)
10366 if (max_sym_count
!= 0)
10368 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
10369 finfo
.external_syms
= bfd_malloc (amt
);
10370 if (finfo
.external_syms
== NULL
)
10373 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
10374 finfo
.internal_syms
= bfd_malloc (amt
);
10375 if (finfo
.internal_syms
== NULL
)
10378 amt
= max_sym_count
* sizeof (long);
10379 finfo
.indices
= bfd_malloc (amt
);
10380 if (finfo
.indices
== NULL
)
10383 amt
= max_sym_count
* sizeof (asection
*);
10384 finfo
.sections
= bfd_malloc (amt
);
10385 if (finfo
.sections
== NULL
)
10389 if (max_sym_shndx_count
!= 0)
10391 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
10392 finfo
.locsym_shndx
= bfd_malloc (amt
);
10393 if (finfo
.locsym_shndx
== NULL
)
10397 if (elf_hash_table (info
)->tls_sec
)
10399 bfd_vma base
, end
= 0;
10402 for (sec
= elf_hash_table (info
)->tls_sec
;
10403 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
10406 bfd_size_type size
= sec
->size
;
10409 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
10411 struct bfd_link_order
*o
= sec
->map_tail
.link_order
;
10413 size
= o
->offset
+ o
->size
;
10415 end
= sec
->vma
+ size
;
10417 base
= elf_hash_table (info
)->tls_sec
->vma
;
10418 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
10419 elf_hash_table (info
)->tls_size
= end
- base
;
10422 /* Reorder SHF_LINK_ORDER sections. */
10423 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10425 if (!elf_fixup_link_order (abfd
, o
))
10429 /* Since ELF permits relocations to be against local symbols, we
10430 must have the local symbols available when we do the relocations.
10431 Since we would rather only read the local symbols once, and we
10432 would rather not keep them in memory, we handle all the
10433 relocations for a single input file at the same time.
10435 Unfortunately, there is no way to know the total number of local
10436 symbols until we have seen all of them, and the local symbol
10437 indices precede the global symbol indices. This means that when
10438 we are generating relocatable output, and we see a reloc against
10439 a global symbol, we can not know the symbol index until we have
10440 finished examining all the local symbols to see which ones we are
10441 going to output. To deal with this, we keep the relocations in
10442 memory, and don't output them until the end of the link. This is
10443 an unfortunate waste of memory, but I don't see a good way around
10444 it. Fortunately, it only happens when performing a relocatable
10445 link, which is not the common case. FIXME: If keep_memory is set
10446 we could write the relocs out and then read them again; I don't
10447 know how bad the memory loss will be. */
10449 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10450 sub
->output_has_begun
= FALSE
;
10451 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10453 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10455 if (p
->type
== bfd_indirect_link_order
10456 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
10457 == bfd_target_elf_flavour
)
10458 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
10460 if (! sub
->output_has_begun
)
10462 if (! elf_link_input_bfd (&finfo
, sub
))
10464 sub
->output_has_begun
= TRUE
;
10467 else if (p
->type
== bfd_section_reloc_link_order
10468 || p
->type
== bfd_symbol_reloc_link_order
)
10470 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
10475 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
10481 /* Free symbol buffer if needed. */
10482 if (!info
->reduce_memory_overheads
)
10484 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
10485 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
10486 && elf_tdata (sub
)->symbuf
)
10488 free (elf_tdata (sub
)->symbuf
);
10489 elf_tdata (sub
)->symbuf
= NULL
;
10493 /* Output any global symbols that got converted to local in a
10494 version script or due to symbol visibility. We do this in a
10495 separate step since ELF requires all local symbols to appear
10496 prior to any global symbols. FIXME: We should only do this if
10497 some global symbols were, in fact, converted to become local.
10498 FIXME: Will this work correctly with the Irix 5 linker? */
10499 eoinfo
.failed
= FALSE
;
10500 eoinfo
.finfo
= &finfo
;
10501 eoinfo
.localsyms
= TRUE
;
10502 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
10507 /* If backend needs to output some local symbols not present in the hash
10508 table, do it now. */
10509 if (bed
->elf_backend_output_arch_local_syms
)
10511 typedef bfd_boolean (*out_sym_func
)
10512 (void *, const char *, Elf_Internal_Sym
*, asection
*,
10513 struct elf_link_hash_entry
*);
10515 if (! ((*bed
->elf_backend_output_arch_local_syms
)
10516 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
10520 /* That wrote out all the local symbols. Finish up the symbol table
10521 with the global symbols. Even if we want to strip everything we
10522 can, we still need to deal with those global symbols that got
10523 converted to local in a version script. */
10525 /* The sh_info field records the index of the first non local symbol. */
10526 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
10529 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
10531 Elf_Internal_Sym sym
;
10532 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
10533 long last_local
= 0;
10535 /* Write out the section symbols for the output sections. */
10536 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
10542 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
10545 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
10551 dynindx
= elf_section_data (s
)->dynindx
;
10554 indx
= elf_section_data (s
)->this_idx
;
10555 BFD_ASSERT (indx
> 0);
10556 sym
.st_shndx
= indx
;
10557 if (! check_dynsym (abfd
, &sym
))
10559 sym
.st_value
= s
->vma
;
10560 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
10561 if (last_local
< dynindx
)
10562 last_local
= dynindx
;
10563 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
10567 /* Write out the local dynsyms. */
10568 if (elf_hash_table (info
)->dynlocal
)
10570 struct elf_link_local_dynamic_entry
*e
;
10571 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
10576 sym
.st_size
= e
->isym
.st_size
;
10577 sym
.st_other
= e
->isym
.st_other
;
10579 /* Copy the internal symbol as is.
10580 Note that we saved a word of storage and overwrote
10581 the original st_name with the dynstr_index. */
10584 s
= bfd_section_from_elf_index (e
->input_bfd
,
10589 elf_section_data (s
->output_section
)->this_idx
;
10590 if (! check_dynsym (abfd
, &sym
))
10592 sym
.st_value
= (s
->output_section
->vma
10594 + e
->isym
.st_value
);
10597 if (last_local
< e
->dynindx
)
10598 last_local
= e
->dynindx
;
10600 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
10601 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
10605 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
10609 /* We get the global symbols from the hash table. */
10610 eoinfo
.failed
= FALSE
;
10611 eoinfo
.localsyms
= FALSE
;
10612 eoinfo
.finfo
= &finfo
;
10613 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
10618 /* If backend needs to output some symbols not present in the hash
10619 table, do it now. */
10620 if (bed
->elf_backend_output_arch_syms
)
10622 typedef bfd_boolean (*out_sym_func
)
10623 (void *, const char *, Elf_Internal_Sym
*, asection
*,
10624 struct elf_link_hash_entry
*);
10626 if (! ((*bed
->elf_backend_output_arch_syms
)
10627 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
10631 /* Flush all symbols to the file. */
10632 if (! elf_link_flush_output_syms (&finfo
, bed
))
10635 /* Now we know the size of the symtab section. */
10636 off
+= symtab_hdr
->sh_size
;
10638 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
10639 if (symtab_shndx_hdr
->sh_name
!= 0)
10641 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
10642 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
10643 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
10644 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
10645 symtab_shndx_hdr
->sh_size
= amt
;
10647 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
10650 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
10651 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
10656 /* Finish up and write out the symbol string table (.strtab)
10658 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
10659 /* sh_name was set in prep_headers. */
10660 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
10661 symstrtab_hdr
->sh_flags
= 0;
10662 symstrtab_hdr
->sh_addr
= 0;
10663 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
10664 symstrtab_hdr
->sh_entsize
= 0;
10665 symstrtab_hdr
->sh_link
= 0;
10666 symstrtab_hdr
->sh_info
= 0;
10667 /* sh_offset is set just below. */
10668 symstrtab_hdr
->sh_addralign
= 1;
10670 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
10671 elf_tdata (abfd
)->next_file_pos
= off
;
10673 if (bfd_get_symcount (abfd
) > 0)
10675 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
10676 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
10680 /* Adjust the relocs to have the correct symbol indices. */
10681 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10683 if ((o
->flags
& SEC_RELOC
) == 0)
10686 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
10687 elf_section_data (o
)->rel_count
,
10688 elf_section_data (o
)->rel_hashes
);
10689 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
10690 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
10691 elf_section_data (o
)->rel_count2
,
10692 (elf_section_data (o
)->rel_hashes
10693 + elf_section_data (o
)->rel_count
));
10695 /* Set the reloc_count field to 0 to prevent write_relocs from
10696 trying to swap the relocs out itself. */
10697 o
->reloc_count
= 0;
10700 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
10701 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
10703 /* If we are linking against a dynamic object, or generating a
10704 shared library, finish up the dynamic linking information. */
10707 bfd_byte
*dyncon
, *dynconend
;
10709 /* Fix up .dynamic entries. */
10710 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
10711 BFD_ASSERT (o
!= NULL
);
10713 dyncon
= o
->contents
;
10714 dynconend
= o
->contents
+ o
->size
;
10715 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
10717 Elf_Internal_Dyn dyn
;
10721 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
10728 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
10730 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
10732 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
10733 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
10736 dyn
.d_un
.d_val
= relativecount
;
10743 name
= info
->init_function
;
10746 name
= info
->fini_function
;
10749 struct elf_link_hash_entry
*h
;
10751 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
10752 FALSE
, FALSE
, TRUE
);
10754 && (h
->root
.type
== bfd_link_hash_defined
10755 || h
->root
.type
== bfd_link_hash_defweak
))
10757 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
10758 o
= h
->root
.u
.def
.section
;
10759 if (o
->output_section
!= NULL
)
10760 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
10761 + o
->output_offset
);
10764 /* The symbol is imported from another shared
10765 library and does not apply to this one. */
10766 dyn
.d_un
.d_ptr
= 0;
10773 case DT_PREINIT_ARRAYSZ
:
10774 name
= ".preinit_array";
10776 case DT_INIT_ARRAYSZ
:
10777 name
= ".init_array";
10779 case DT_FINI_ARRAYSZ
:
10780 name
= ".fini_array";
10782 o
= bfd_get_section_by_name (abfd
, name
);
10785 (*_bfd_error_handler
)
10786 (_("%B: could not find output section %s"), abfd
, name
);
10790 (*_bfd_error_handler
)
10791 (_("warning: %s section has zero size"), name
);
10792 dyn
.d_un
.d_val
= o
->size
;
10795 case DT_PREINIT_ARRAY
:
10796 name
= ".preinit_array";
10798 case DT_INIT_ARRAY
:
10799 name
= ".init_array";
10801 case DT_FINI_ARRAY
:
10802 name
= ".fini_array";
10809 name
= ".gnu.hash";
10818 name
= ".gnu.version_d";
10821 name
= ".gnu.version_r";
10824 name
= ".gnu.version";
10826 o
= bfd_get_section_by_name (abfd
, name
);
10829 (*_bfd_error_handler
)
10830 (_("%B: could not find output section %s"), abfd
, name
);
10833 dyn
.d_un
.d_ptr
= o
->vma
;
10840 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
10844 dyn
.d_un
.d_val
= 0;
10845 dyn
.d_un
.d_ptr
= 0;
10846 for (i
= 1; i
< elf_numsections (abfd
); i
++)
10848 Elf_Internal_Shdr
*hdr
;
10850 hdr
= elf_elfsections (abfd
)[i
];
10851 if (hdr
->sh_type
== type
10852 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
10854 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
10855 dyn
.d_un
.d_val
+= hdr
->sh_size
;
10858 if (dyn
.d_un
.d_ptr
== 0
10859 || hdr
->sh_addr
< dyn
.d_un
.d_ptr
)
10860 dyn
.d_un
.d_ptr
= hdr
->sh_addr
;
10866 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
10870 /* If we have created any dynamic sections, then output them. */
10871 if (dynobj
!= NULL
)
10873 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
10876 /* Check for DT_TEXTREL (late, in case the backend removes it). */
10877 if (info
->warn_shared_textrel
&& info
->shared
)
10879 bfd_byte
*dyncon
, *dynconend
;
10881 /* Fix up .dynamic entries. */
10882 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
10883 BFD_ASSERT (o
!= NULL
);
10885 dyncon
= o
->contents
;
10886 dynconend
= o
->contents
+ o
->size
;
10887 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
10889 Elf_Internal_Dyn dyn
;
10891 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
10893 if (dyn
.d_tag
== DT_TEXTREL
)
10895 info
->callbacks
->einfo
10896 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
10902 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
10904 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
10906 || o
->output_section
== bfd_abs_section_ptr
)
10908 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
10910 /* At this point, we are only interested in sections
10911 created by _bfd_elf_link_create_dynamic_sections. */
10914 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
10916 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
10918 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
10920 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
10922 if (! bfd_set_section_contents (abfd
, o
->output_section
,
10924 (file_ptr
) o
->output_offset
,
10930 /* The contents of the .dynstr section are actually in a
10932 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
10933 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
10934 || ! _bfd_elf_strtab_emit (abfd
,
10935 elf_hash_table (info
)->dynstr
))
10941 if (info
->relocatable
)
10943 bfd_boolean failed
= FALSE
;
10945 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
10950 /* If we have optimized stabs strings, output them. */
10951 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
10953 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
10957 if (info
->eh_frame_hdr
)
10959 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
10963 if (finfo
.symstrtab
!= NULL
)
10964 _bfd_stringtab_free (finfo
.symstrtab
);
10965 if (finfo
.contents
!= NULL
)
10966 free (finfo
.contents
);
10967 if (finfo
.external_relocs
!= NULL
)
10968 free (finfo
.external_relocs
);
10969 if (finfo
.internal_relocs
!= NULL
)
10970 free (finfo
.internal_relocs
);
10971 if (finfo
.external_syms
!= NULL
)
10972 free (finfo
.external_syms
);
10973 if (finfo
.locsym_shndx
!= NULL
)
10974 free (finfo
.locsym_shndx
);
10975 if (finfo
.internal_syms
!= NULL
)
10976 free (finfo
.internal_syms
);
10977 if (finfo
.indices
!= NULL
)
10978 free (finfo
.indices
);
10979 if (finfo
.sections
!= NULL
)
10980 free (finfo
.sections
);
10981 if (finfo
.symbuf
!= NULL
)
10982 free (finfo
.symbuf
);
10983 if (finfo
.symshndxbuf
!= NULL
)
10984 free (finfo
.symshndxbuf
);
10985 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10987 if ((o
->flags
& SEC_RELOC
) != 0
10988 && elf_section_data (o
)->rel_hashes
!= NULL
)
10989 free (elf_section_data (o
)->rel_hashes
);
10992 elf_tdata (abfd
)->linker
= TRUE
;
10996 bfd_byte
*contents
= bfd_malloc (attr_size
);
10997 if (contents
== NULL
)
10998 return FALSE
; /* Bail out and fail. */
10999 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
11000 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
11007 if (finfo
.symstrtab
!= NULL
)
11008 _bfd_stringtab_free (finfo
.symstrtab
);
11009 if (finfo
.contents
!= NULL
)
11010 free (finfo
.contents
);
11011 if (finfo
.external_relocs
!= NULL
)
11012 free (finfo
.external_relocs
);
11013 if (finfo
.internal_relocs
!= NULL
)
11014 free (finfo
.internal_relocs
);
11015 if (finfo
.external_syms
!= NULL
)
11016 free (finfo
.external_syms
);
11017 if (finfo
.locsym_shndx
!= NULL
)
11018 free (finfo
.locsym_shndx
);
11019 if (finfo
.internal_syms
!= NULL
)
11020 free (finfo
.internal_syms
);
11021 if (finfo
.indices
!= NULL
)
11022 free (finfo
.indices
);
11023 if (finfo
.sections
!= NULL
)
11024 free (finfo
.sections
);
11025 if (finfo
.symbuf
!= NULL
)
11026 free (finfo
.symbuf
);
11027 if (finfo
.symshndxbuf
!= NULL
)
11028 free (finfo
.symshndxbuf
);
11029 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11031 if ((o
->flags
& SEC_RELOC
) != 0
11032 && elf_section_data (o
)->rel_hashes
!= NULL
)
11033 free (elf_section_data (o
)->rel_hashes
);
11039 /* Initialize COOKIE for input bfd ABFD. */
11042 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
11043 struct bfd_link_info
*info
, bfd
*abfd
)
11045 Elf_Internal_Shdr
*symtab_hdr
;
11046 const struct elf_backend_data
*bed
;
11048 bed
= get_elf_backend_data (abfd
);
11049 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11051 cookie
->abfd
= abfd
;
11052 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
11053 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
11054 if (cookie
->bad_symtab
)
11056 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
11057 cookie
->extsymoff
= 0;
11061 cookie
->locsymcount
= symtab_hdr
->sh_info
;
11062 cookie
->extsymoff
= symtab_hdr
->sh_info
;
11065 if (bed
->s
->arch_size
== 32)
11066 cookie
->r_sym_shift
= 8;
11068 cookie
->r_sym_shift
= 32;
11070 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
11071 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
11073 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
11074 cookie
->locsymcount
, 0,
11076 if (cookie
->locsyms
== NULL
)
11078 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
11081 if (info
->keep_memory
)
11082 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
11087 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
11090 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
11092 Elf_Internal_Shdr
*symtab_hdr
;
11094 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11095 if (cookie
->locsyms
!= NULL
11096 && symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
11097 free (cookie
->locsyms
);
11100 /* Initialize the relocation information in COOKIE for input section SEC
11101 of input bfd ABFD. */
11104 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
11105 struct bfd_link_info
*info
, bfd
*abfd
,
11108 const struct elf_backend_data
*bed
;
11110 if (sec
->reloc_count
== 0)
11112 cookie
->rels
= NULL
;
11113 cookie
->relend
= NULL
;
11117 bed
= get_elf_backend_data (abfd
);
11119 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
11120 info
->keep_memory
);
11121 if (cookie
->rels
== NULL
)
11123 cookie
->rel
= cookie
->rels
;
11124 cookie
->relend
= (cookie
->rels
11125 + sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
);
11127 cookie
->rel
= cookie
->rels
;
11131 /* Free the memory allocated by init_reloc_cookie_rels,
11135 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
11138 if (cookie
->rels
&& elf_section_data (sec
)->relocs
!= cookie
->rels
)
11139 free (cookie
->rels
);
11142 /* Initialize the whole of COOKIE for input section SEC. */
11145 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
11146 struct bfd_link_info
*info
,
11149 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
11151 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
11156 fini_reloc_cookie (cookie
, sec
->owner
);
11161 /* Free the memory allocated by init_reloc_cookie_for_section,
11165 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
11168 fini_reloc_cookie_rels (cookie
, sec
);
11169 fini_reloc_cookie (cookie
, sec
->owner
);
11172 /* Garbage collect unused sections. */
11174 /* Default gc_mark_hook. */
11177 _bfd_elf_gc_mark_hook (asection
*sec
,
11178 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
11179 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
11180 struct elf_link_hash_entry
*h
,
11181 Elf_Internal_Sym
*sym
)
11185 switch (h
->root
.type
)
11187 case bfd_link_hash_defined
:
11188 case bfd_link_hash_defweak
:
11189 return h
->root
.u
.def
.section
;
11191 case bfd_link_hash_common
:
11192 return h
->root
.u
.c
.p
->section
;
11199 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
11204 /* COOKIE->rel describes a relocation against section SEC, which is
11205 a section we've decided to keep. Return the section that contains
11206 the relocation symbol, or NULL if no section contains it. */
11209 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
11210 elf_gc_mark_hook_fn gc_mark_hook
,
11211 struct elf_reloc_cookie
*cookie
)
11213 unsigned long r_symndx
;
11214 struct elf_link_hash_entry
*h
;
11216 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
11220 if (r_symndx
>= cookie
->locsymcount
11221 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
11223 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
11224 while (h
->root
.type
== bfd_link_hash_indirect
11225 || h
->root
.type
== bfd_link_hash_warning
)
11226 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11227 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
11230 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
11231 &cookie
->locsyms
[r_symndx
]);
11234 /* COOKIE->rel describes a relocation against section SEC, which is
11235 a section we've decided to keep. Mark the section that contains
11236 the relocation symbol. */
11239 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
11241 elf_gc_mark_hook_fn gc_mark_hook
,
11242 struct elf_reloc_cookie
*cookie
)
11246 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
);
11247 if (rsec
&& !rsec
->gc_mark
)
11249 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
11251 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
11257 /* The mark phase of garbage collection. For a given section, mark
11258 it and any sections in this section's group, and all the sections
11259 which define symbols to which it refers. */
11262 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
11264 elf_gc_mark_hook_fn gc_mark_hook
)
11267 asection
*group_sec
, *eh_frame
;
11271 /* Mark all the sections in the group. */
11272 group_sec
= elf_section_data (sec
)->next_in_group
;
11273 if (group_sec
&& !group_sec
->gc_mark
)
11274 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
11277 /* Look through the section relocs. */
11279 eh_frame
= elf_eh_frame_section (sec
->owner
);
11280 if ((sec
->flags
& SEC_RELOC
) != 0
11281 && sec
->reloc_count
> 0
11282 && sec
!= eh_frame
)
11284 struct elf_reloc_cookie cookie
;
11286 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
11290 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
11291 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
11296 fini_reloc_cookie_for_section (&cookie
, sec
);
11300 if (ret
&& eh_frame
&& elf_fde_list (sec
))
11302 struct elf_reloc_cookie cookie
;
11304 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
11308 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
11309 gc_mark_hook
, &cookie
))
11311 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
11318 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
11320 struct elf_gc_sweep_symbol_info
11322 struct bfd_link_info
*info
;
11323 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
11328 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
11330 if (h
->root
.type
== bfd_link_hash_warning
)
11331 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11333 if ((h
->root
.type
== bfd_link_hash_defined
11334 || h
->root
.type
== bfd_link_hash_defweak
)
11335 && !h
->root
.u
.def
.section
->gc_mark
11336 && !(h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
))
11338 struct elf_gc_sweep_symbol_info
*inf
= data
;
11339 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
11345 /* The sweep phase of garbage collection. Remove all garbage sections. */
11347 typedef bfd_boolean (*gc_sweep_hook_fn
)
11348 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
11351 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
11354 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11355 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
11356 unsigned long section_sym_count
;
11357 struct elf_gc_sweep_symbol_info sweep_info
;
11359 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
11363 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
11366 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
11368 /* Keep debug and special sections. */
11369 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
11370 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
11376 /* Skip sweeping sections already excluded. */
11377 if (o
->flags
& SEC_EXCLUDE
)
11380 /* Since this is early in the link process, it is simple
11381 to remove a section from the output. */
11382 o
->flags
|= SEC_EXCLUDE
;
11384 if (info
->print_gc_sections
&& o
->size
!= 0)
11385 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub
, o
->name
);
11387 /* But we also have to update some of the relocation
11388 info we collected before. */
11390 && (o
->flags
& SEC_RELOC
) != 0
11391 && o
->reloc_count
> 0
11392 && !bfd_is_abs_section (o
->output_section
))
11394 Elf_Internal_Rela
*internal_relocs
;
11398 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
11399 info
->keep_memory
);
11400 if (internal_relocs
== NULL
)
11403 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
11405 if (elf_section_data (o
)->relocs
!= internal_relocs
)
11406 free (internal_relocs
);
11414 /* Remove the symbols that were in the swept sections from the dynamic
11415 symbol table. GCFIXME: Anyone know how to get them out of the
11416 static symbol table as well? */
11417 sweep_info
.info
= info
;
11418 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
11419 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
11422 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
11426 /* Propagate collected vtable information. This is called through
11427 elf_link_hash_traverse. */
11430 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
11432 if (h
->root
.type
== bfd_link_hash_warning
)
11433 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11435 /* Those that are not vtables. */
11436 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
11439 /* Those vtables that do not have parents, we cannot merge. */
11440 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
11443 /* If we've already been done, exit. */
11444 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
11447 /* Make sure the parent's table is up to date. */
11448 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
11450 if (h
->vtable
->used
== NULL
)
11452 /* None of this table's entries were referenced. Re-use the
11454 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
11455 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
11460 bfd_boolean
*cu
, *pu
;
11462 /* Or the parent's entries into ours. */
11463 cu
= h
->vtable
->used
;
11465 pu
= h
->vtable
->parent
->vtable
->used
;
11468 const struct elf_backend_data
*bed
;
11469 unsigned int log_file_align
;
11471 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
11472 log_file_align
= bed
->s
->log_file_align
;
11473 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
11488 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
11491 bfd_vma hstart
, hend
;
11492 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
11493 const struct elf_backend_data
*bed
;
11494 unsigned int log_file_align
;
11496 if (h
->root
.type
== bfd_link_hash_warning
)
11497 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11499 /* Take care of both those symbols that do not describe vtables as
11500 well as those that are not loaded. */
11501 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
11504 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
11505 || h
->root
.type
== bfd_link_hash_defweak
);
11507 sec
= h
->root
.u
.def
.section
;
11508 hstart
= h
->root
.u
.def
.value
;
11509 hend
= hstart
+ h
->size
;
11511 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
11513 return *(bfd_boolean
*) okp
= FALSE
;
11514 bed
= get_elf_backend_data (sec
->owner
);
11515 log_file_align
= bed
->s
->log_file_align
;
11517 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
11519 for (rel
= relstart
; rel
< relend
; ++rel
)
11520 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
11522 /* If the entry is in use, do nothing. */
11523 if (h
->vtable
->used
11524 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
11526 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
11527 if (h
->vtable
->used
[entry
])
11530 /* Otherwise, kill it. */
11531 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
11537 /* Mark sections containing dynamically referenced symbols. When
11538 building shared libraries, we must assume that any visible symbol is
11542 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
11544 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
11546 if (h
->root
.type
== bfd_link_hash_warning
)
11547 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11549 if ((h
->root
.type
== bfd_link_hash_defined
11550 || h
->root
.type
== bfd_link_hash_defweak
)
11552 || (!info
->executable
11554 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
11555 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
)))
11556 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
11561 /* Keep all sections containing symbols undefined on the command-line,
11562 and the section containing the entry symbol. */
11565 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
11567 struct bfd_sym_chain
*sym
;
11569 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
11571 struct elf_link_hash_entry
*h
;
11573 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
11574 FALSE
, FALSE
, FALSE
);
11577 && (h
->root
.type
== bfd_link_hash_defined
11578 || h
->root
.type
== bfd_link_hash_defweak
)
11579 && !bfd_is_abs_section (h
->root
.u
.def
.section
))
11580 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
11584 /* Do mark and sweep of unused sections. */
11587 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
11589 bfd_boolean ok
= TRUE
;
11591 elf_gc_mark_hook_fn gc_mark_hook
;
11592 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11594 if (!bed
->can_gc_sections
11595 || !is_elf_hash_table (info
->hash
))
11597 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
11601 bed
->gc_keep (info
);
11603 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
11604 at the .eh_frame section if we can mark the FDEs individually. */
11605 _bfd_elf_begin_eh_frame_parsing (info
);
11606 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
11609 struct elf_reloc_cookie cookie
;
11611 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
11612 if (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
11614 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
11615 if (elf_section_data (sec
)->sec_info
)
11616 elf_eh_frame_section (sub
) = sec
;
11617 fini_reloc_cookie_for_section (&cookie
, sec
);
11620 _bfd_elf_end_eh_frame_parsing (info
);
11622 /* Apply transitive closure to the vtable entry usage info. */
11623 elf_link_hash_traverse (elf_hash_table (info
),
11624 elf_gc_propagate_vtable_entries_used
,
11629 /* Kill the vtable relocations that were not used. */
11630 elf_link_hash_traverse (elf_hash_table (info
),
11631 elf_gc_smash_unused_vtentry_relocs
,
11636 /* Mark dynamically referenced symbols. */
11637 if (elf_hash_table (info
)->dynamic_sections_created
)
11638 elf_link_hash_traverse (elf_hash_table (info
),
11639 bed
->gc_mark_dynamic_ref
,
11642 /* Grovel through relocs to find out who stays ... */
11643 gc_mark_hook
= bed
->gc_mark_hook
;
11644 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
11648 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
11651 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
11652 if ((o
->flags
& (SEC_EXCLUDE
| SEC_KEEP
)) == SEC_KEEP
&& !o
->gc_mark
)
11653 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
11657 /* Allow the backend to mark additional target specific sections. */
11658 if (bed
->gc_mark_extra_sections
)
11659 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
11661 /* ... and mark SEC_EXCLUDE for those that go. */
11662 return elf_gc_sweep (abfd
, info
);
11665 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
11668 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
11670 struct elf_link_hash_entry
*h
,
11673 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
11674 struct elf_link_hash_entry
**search
, *child
;
11675 bfd_size_type extsymcount
;
11676 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11678 /* The sh_info field of the symtab header tells us where the
11679 external symbols start. We don't care about the local symbols at
11681 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
11682 if (!elf_bad_symtab (abfd
))
11683 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
11685 sym_hashes
= elf_sym_hashes (abfd
);
11686 sym_hashes_end
= sym_hashes
+ extsymcount
;
11688 /* Hunt down the child symbol, which is in this section at the same
11689 offset as the relocation. */
11690 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
11692 if ((child
= *search
) != NULL
11693 && (child
->root
.type
== bfd_link_hash_defined
11694 || child
->root
.type
== bfd_link_hash_defweak
)
11695 && child
->root
.u
.def
.section
== sec
11696 && child
->root
.u
.def
.value
== offset
)
11700 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
11701 abfd
, sec
, (unsigned long) offset
);
11702 bfd_set_error (bfd_error_invalid_operation
);
11706 if (!child
->vtable
)
11708 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
11709 if (!child
->vtable
)
11714 /* This *should* only be the absolute section. It could potentially
11715 be that someone has defined a non-global vtable though, which
11716 would be bad. It isn't worth paging in the local symbols to be
11717 sure though; that case should simply be handled by the assembler. */
11719 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
11722 child
->vtable
->parent
= h
;
11727 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
11730 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
11731 asection
*sec ATTRIBUTE_UNUSED
,
11732 struct elf_link_hash_entry
*h
,
11735 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11736 unsigned int log_file_align
= bed
->s
->log_file_align
;
11740 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
11745 if (addend
>= h
->vtable
->size
)
11747 size_t size
, bytes
, file_align
;
11748 bfd_boolean
*ptr
= h
->vtable
->used
;
11750 /* While the symbol is undefined, we have to be prepared to handle
11752 file_align
= 1 << log_file_align
;
11753 if (h
->root
.type
== bfd_link_hash_undefined
)
11754 size
= addend
+ file_align
;
11758 if (addend
>= size
)
11760 /* Oops! We've got a reference past the defined end of
11761 the table. This is probably a bug -- shall we warn? */
11762 size
= addend
+ file_align
;
11765 size
= (size
+ file_align
- 1) & -file_align
;
11767 /* Allocate one extra entry for use as a "done" flag for the
11768 consolidation pass. */
11769 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
11773 ptr
= bfd_realloc (ptr
- 1, bytes
);
11779 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
11780 * sizeof (bfd_boolean
));
11781 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
11785 ptr
= bfd_zmalloc (bytes
);
11790 /* And arrange for that done flag to be at index -1. */
11791 h
->vtable
->used
= ptr
+ 1;
11792 h
->vtable
->size
= size
;
11795 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
11800 struct alloc_got_off_arg
{
11802 struct bfd_link_info
*info
;
11805 /* We need a special top-level link routine to convert got reference counts
11806 to real got offsets. */
11809 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
11811 struct alloc_got_off_arg
*gofarg
= arg
;
11812 bfd
*obfd
= gofarg
->info
->output_bfd
;
11813 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
11815 if (h
->root
.type
== bfd_link_hash_warning
)
11816 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11818 if (h
->got
.refcount
> 0)
11820 h
->got
.offset
= gofarg
->gotoff
;
11821 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
11824 h
->got
.offset
= (bfd_vma
) -1;
11829 /* And an accompanying bit to work out final got entry offsets once
11830 we're done. Should be called from final_link. */
11833 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
11834 struct bfd_link_info
*info
)
11837 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11839 struct alloc_got_off_arg gofarg
;
11841 BFD_ASSERT (abfd
== info
->output_bfd
);
11843 if (! is_elf_hash_table (info
->hash
))
11846 /* The GOT offset is relative to the .got section, but the GOT header is
11847 put into the .got.plt section, if the backend uses it. */
11848 if (bed
->want_got_plt
)
11851 gotoff
= bed
->got_header_size
;
11853 /* Do the local .got entries first. */
11854 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
11856 bfd_signed_vma
*local_got
;
11857 bfd_size_type j
, locsymcount
;
11858 Elf_Internal_Shdr
*symtab_hdr
;
11860 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
11863 local_got
= elf_local_got_refcounts (i
);
11867 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
11868 if (elf_bad_symtab (i
))
11869 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
11871 locsymcount
= symtab_hdr
->sh_info
;
11873 for (j
= 0; j
< locsymcount
; ++j
)
11875 if (local_got
[j
] > 0)
11877 local_got
[j
] = gotoff
;
11878 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
11881 local_got
[j
] = (bfd_vma
) -1;
11885 /* Then the global .got entries. .plt refcounts are handled by
11886 adjust_dynamic_symbol */
11887 gofarg
.gotoff
= gotoff
;
11888 gofarg
.info
= info
;
11889 elf_link_hash_traverse (elf_hash_table (info
),
11890 elf_gc_allocate_got_offsets
,
11895 /* Many folk need no more in the way of final link than this, once
11896 got entry reference counting is enabled. */
11899 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
11901 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
11904 /* Invoke the regular ELF backend linker to do all the work. */
11905 return bfd_elf_final_link (abfd
, info
);
11909 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
11911 struct elf_reloc_cookie
*rcookie
= cookie
;
11913 if (rcookie
->bad_symtab
)
11914 rcookie
->rel
= rcookie
->rels
;
11916 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
11918 unsigned long r_symndx
;
11920 if (! rcookie
->bad_symtab
)
11921 if (rcookie
->rel
->r_offset
> offset
)
11923 if (rcookie
->rel
->r_offset
!= offset
)
11926 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
11927 if (r_symndx
== SHN_UNDEF
)
11930 if (r_symndx
>= rcookie
->locsymcount
11931 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
11933 struct elf_link_hash_entry
*h
;
11935 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
11937 while (h
->root
.type
== bfd_link_hash_indirect
11938 || h
->root
.type
== bfd_link_hash_warning
)
11939 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11941 if ((h
->root
.type
== bfd_link_hash_defined
11942 || h
->root
.type
== bfd_link_hash_defweak
)
11943 && elf_discarded_section (h
->root
.u
.def
.section
))
11950 /* It's not a relocation against a global symbol,
11951 but it could be a relocation against a local
11952 symbol for a discarded section. */
11954 Elf_Internal_Sym
*isym
;
11956 /* Need to: get the symbol; get the section. */
11957 isym
= &rcookie
->locsyms
[r_symndx
];
11958 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
11959 if (isec
!= NULL
&& elf_discarded_section (isec
))
11967 /* Discard unneeded references to discarded sections.
11968 Returns TRUE if any section's size was changed. */
11969 /* This function assumes that the relocations are in sorted order,
11970 which is true for all known assemblers. */
11973 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
11975 struct elf_reloc_cookie cookie
;
11976 asection
*stab
, *eh
;
11977 const struct elf_backend_data
*bed
;
11979 bfd_boolean ret
= FALSE
;
11981 if (info
->traditional_format
11982 || !is_elf_hash_table (info
->hash
))
11985 _bfd_elf_begin_eh_frame_parsing (info
);
11986 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
11988 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
11991 bed
= get_elf_backend_data (abfd
);
11993 if ((abfd
->flags
& DYNAMIC
) != 0)
11997 if (!info
->relocatable
)
11999 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
12002 || bfd_is_abs_section (eh
->output_section
)))
12006 stab
= bfd_get_section_by_name (abfd
, ".stab");
12008 && (stab
->size
== 0
12009 || bfd_is_abs_section (stab
->output_section
)
12010 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
12015 && bed
->elf_backend_discard_info
== NULL
)
12018 if (!init_reloc_cookie (&cookie
, info
, abfd
))
12022 && stab
->reloc_count
> 0
12023 && init_reloc_cookie_rels (&cookie
, info
, abfd
, stab
))
12025 if (_bfd_discard_section_stabs (abfd
, stab
,
12026 elf_section_data (stab
)->sec_info
,
12027 bfd_elf_reloc_symbol_deleted_p
,
12030 fini_reloc_cookie_rels (&cookie
, stab
);
12034 && init_reloc_cookie_rels (&cookie
, info
, abfd
, eh
))
12036 _bfd_elf_parse_eh_frame (abfd
, info
, eh
, &cookie
);
12037 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
12038 bfd_elf_reloc_symbol_deleted_p
,
12041 fini_reloc_cookie_rels (&cookie
, eh
);
12044 if (bed
->elf_backend_discard_info
!= NULL
12045 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
12048 fini_reloc_cookie (&cookie
, abfd
);
12050 _bfd_elf_end_eh_frame_parsing (info
);
12052 if (info
->eh_frame_hdr
12053 && !info
->relocatable
12054 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
12060 /* For a SHT_GROUP section, return the group signature. For other
12061 sections, return the normal section name. */
12063 static const char *
12064 section_signature (asection
*sec
)
12066 if ((sec
->flags
& SEC_GROUP
) != 0
12067 && elf_next_in_group (sec
) != NULL
12068 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
12069 return elf_group_name (elf_next_in_group (sec
));
12074 _bfd_elf_section_already_linked (bfd
*abfd
, asection
*sec
,
12075 struct bfd_link_info
*info
)
12078 const char *name
, *p
;
12079 struct bfd_section_already_linked
*l
;
12080 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
12082 if (sec
->output_section
== bfd_abs_section_ptr
)
12085 flags
= sec
->flags
;
12087 /* Return if it isn't a linkonce section. A comdat group section
12088 also has SEC_LINK_ONCE set. */
12089 if ((flags
& SEC_LINK_ONCE
) == 0)
12092 /* Don't put group member sections on our list of already linked
12093 sections. They are handled as a group via their group section. */
12094 if (elf_sec_group (sec
) != NULL
)
12097 /* FIXME: When doing a relocatable link, we may have trouble
12098 copying relocations in other sections that refer to local symbols
12099 in the section being discarded. Those relocations will have to
12100 be converted somehow; as of this writing I'm not sure that any of
12101 the backends handle that correctly.
12103 It is tempting to instead not discard link once sections when
12104 doing a relocatable link (technically, they should be discarded
12105 whenever we are building constructors). However, that fails,
12106 because the linker winds up combining all the link once sections
12107 into a single large link once section, which defeats the purpose
12108 of having link once sections in the first place.
12110 Also, not merging link once sections in a relocatable link
12111 causes trouble for MIPS ELF, which relies on link once semantics
12112 to handle the .reginfo section correctly. */
12114 name
= section_signature (sec
);
12116 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
12117 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
12122 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
12124 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
12126 /* We may have 2 different types of sections on the list: group
12127 sections and linkonce sections. Match like sections. */
12128 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
12129 && strcmp (name
, section_signature (l
->sec
)) == 0
12130 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
12132 /* The section has already been linked. See if we should
12133 issue a warning. */
12134 switch (flags
& SEC_LINK_DUPLICATES
)
12139 case SEC_LINK_DUPLICATES_DISCARD
:
12142 case SEC_LINK_DUPLICATES_ONE_ONLY
:
12143 (*_bfd_error_handler
)
12144 (_("%B: ignoring duplicate section `%A'"),
12148 case SEC_LINK_DUPLICATES_SAME_SIZE
:
12149 if (sec
->size
!= l
->sec
->size
)
12150 (*_bfd_error_handler
)
12151 (_("%B: duplicate section `%A' has different size"),
12155 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
12156 if (sec
->size
!= l
->sec
->size
)
12157 (*_bfd_error_handler
)
12158 (_("%B: duplicate section `%A' has different size"),
12160 else if (sec
->size
!= 0)
12162 bfd_byte
*sec_contents
, *l_sec_contents
;
12164 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
12165 (*_bfd_error_handler
)
12166 (_("%B: warning: could not read contents of section `%A'"),
12168 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
12170 (*_bfd_error_handler
)
12171 (_("%B: warning: could not read contents of section `%A'"),
12172 l
->sec
->owner
, l
->sec
);
12173 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
12174 (*_bfd_error_handler
)
12175 (_("%B: warning: duplicate section `%A' has different contents"),
12179 free (sec_contents
);
12180 if (l_sec_contents
)
12181 free (l_sec_contents
);
12186 /* Set the output_section field so that lang_add_section
12187 does not create a lang_input_section structure for this
12188 section. Since there might be a symbol in the section
12189 being discarded, we must retain a pointer to the section
12190 which we are really going to use. */
12191 sec
->output_section
= bfd_abs_section_ptr
;
12192 sec
->kept_section
= l
->sec
;
12194 if (flags
& SEC_GROUP
)
12196 asection
*first
= elf_next_in_group (sec
);
12197 asection
*s
= first
;
12201 s
->output_section
= bfd_abs_section_ptr
;
12202 /* Record which group discards it. */
12203 s
->kept_section
= l
->sec
;
12204 s
= elf_next_in_group (s
);
12205 /* These lists are circular. */
12215 /* A single member comdat group section may be discarded by a
12216 linkonce section and vice versa. */
12218 if ((flags
& SEC_GROUP
) != 0)
12220 asection
*first
= elf_next_in_group (sec
);
12222 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
12223 /* Check this single member group against linkonce sections. */
12224 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
12225 if ((l
->sec
->flags
& SEC_GROUP
) == 0
12226 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
12227 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
12229 first
->output_section
= bfd_abs_section_ptr
;
12230 first
->kept_section
= l
->sec
;
12231 sec
->output_section
= bfd_abs_section_ptr
;
12236 /* Check this linkonce section against single member groups. */
12237 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
12238 if (l
->sec
->flags
& SEC_GROUP
)
12240 asection
*first
= elf_next_in_group (l
->sec
);
12243 && elf_next_in_group (first
) == first
12244 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
12246 sec
->output_section
= bfd_abs_section_ptr
;
12247 sec
->kept_section
= first
;
12252 /* This is the first section with this name. Record it. */
12253 if (! bfd_section_already_linked_table_insert (already_linked_list
, sec
))
12254 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E"));
12258 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
12260 return sym
->st_shndx
== SHN_COMMON
;
12264 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
12270 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
12272 return bfd_com_section_ptr
;
12276 _bfd_elf_default_got_elt_size (bfd
*abfd
,
12277 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12278 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
12279 bfd
*ibfd ATTRIBUTE_UNUSED
,
12280 unsigned long symndx ATTRIBUTE_UNUSED
)
12282 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12283 return bed
->s
->arch_size
/ 8;
12286 /* Routines to support the creation of dynamic relocs. */
12288 /* Return true if NAME is a name of a relocation
12289 section associated with section S. */
12292 is_reloc_section (bfd_boolean rela
, const char * name
, asection
* s
)
12295 return CONST_STRNEQ (name
, ".rela")
12296 && strcmp (bfd_get_section_name (NULL
, s
), name
+ 5) == 0;
12298 return CONST_STRNEQ (name
, ".rel")
12299 && strcmp (bfd_get_section_name (NULL
, s
), name
+ 4) == 0;
12302 /* Returns the name of the dynamic reloc section associated with SEC. */
12304 static const char *
12305 get_dynamic_reloc_section_name (bfd
* abfd
,
12307 bfd_boolean is_rela
)
12310 unsigned int strndx
= elf_elfheader (abfd
)->e_shstrndx
;
12311 unsigned int shnam
= elf_section_data (sec
)->rel_hdr
.sh_name
;
12313 name
= bfd_elf_string_from_elf_section (abfd
, strndx
, shnam
);
12317 if (! is_reloc_section (is_rela
, name
, sec
))
12319 static bfd_boolean complained
= FALSE
;
12323 (*_bfd_error_handler
)
12324 (_("%B: bad relocation section name `%s\'"), abfd
, name
);
12333 /* Returns the dynamic reloc section associated with SEC.
12334 If necessary compute the name of the dynamic reloc section based
12335 on SEC's name (looked up in ABFD's string table) and the setting
12339 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
12341 bfd_boolean is_rela
)
12343 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
12345 if (reloc_sec
== NULL
)
12347 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
12351 reloc_sec
= bfd_get_section_by_name (abfd
, name
);
12353 if (reloc_sec
!= NULL
)
12354 elf_section_data (sec
)->sreloc
= reloc_sec
;
12361 /* Returns the dynamic reloc section associated with SEC. If the
12362 section does not exist it is created and attached to the DYNOBJ
12363 bfd and stored in the SRELOC field of SEC's elf_section_data
12366 ALIGNMENT is the alignment for the newly created section and
12367 IS_RELA defines whether the name should be .rela.<SEC's name>
12368 or .rel.<SEC's name>. The section name is looked up in the
12369 string table associated with ABFD. */
12372 _bfd_elf_make_dynamic_reloc_section (asection
* sec
,
12374 unsigned int alignment
,
12376 bfd_boolean is_rela
)
12378 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
12380 if (reloc_sec
== NULL
)
12382 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
12387 reloc_sec
= bfd_get_section_by_name (dynobj
, name
);
12389 if (reloc_sec
== NULL
)
12393 flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
12394 if ((sec
->flags
& SEC_ALLOC
) != 0)
12395 flags
|= SEC_ALLOC
| SEC_LOAD
;
12397 reloc_sec
= bfd_make_section_with_flags (dynobj
, name
, flags
);
12398 if (reloc_sec
!= NULL
)
12400 if (! bfd_set_section_alignment (dynobj
, reloc_sec
, alignment
))
12405 elf_section_data (sec
)->sreloc
= reloc_sec
;
12411 #define IFUNC_INFIX ".ifunc"
12413 /* Returns the name of the ifunc-using-dynamic-reloc section associated with SEC. */
12415 static const char *
12416 get_ifunc_reloc_section_name (bfd
* abfd
,
12421 const char * base_name
;
12422 unsigned int strndx
= elf_elfheader (abfd
)->e_shstrndx
;
12423 unsigned int shnam
= elf_section_data (sec
)->rel_hdr
.sh_name
;
12425 base_name
= bfd_elf_string_from_elf_section (abfd
, strndx
, shnam
);
12426 if (base_name
== NULL
)
12429 dot
= strchr (base_name
+ 1, '.');
12430 name
= bfd_alloc (abfd
, strlen (base_name
) + strlen (IFUNC_INFIX
) + 1);
12431 sprintf (name
, "%.*s%s%s", (int)(dot
- base_name
), base_name
, IFUNC_INFIX
, dot
);
12436 /* Like _bfd_elf_make_dynamic_reloc_section but it creates a
12437 section for holding relocs against symbols with the STT_IFUNC
12438 type. The section is attached to the OWNER bfd but it is created
12439 with a name based on SEC from ABFD. */
12442 _bfd_elf_make_ifunc_reloc_section (bfd
* abfd
,
12445 unsigned int align
)
12447 asection
* reloc_sec
= elf_section_data (sec
)->indirect_relocs
;
12449 if (reloc_sec
== NULL
)
12451 const char * name
= get_ifunc_reloc_section_name (abfd
, sec
);
12456 reloc_sec
= bfd_get_section_by_name (owner
, name
);
12458 if (reloc_sec
== NULL
)
12462 flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
12463 if ((sec
->flags
& SEC_ALLOC
) != 0)
12464 flags
|= SEC_ALLOC
| SEC_LOAD
;
12466 reloc_sec
= bfd_make_section_with_flags (owner
, name
, flags
);
12468 if (reloc_sec
!= NULL
12469 && ! bfd_set_section_alignment (owner
, reloc_sec
, align
))
12473 elf_section_data (sec
)->indirect_relocs
= reloc_sec
;